|
1 | 1 | // @vitest-environment node |
2 | 2 | import { describe, it, expect, vi } from "vitest"; |
| 3 | +import { parseHTML } from "linkedom"; |
3 | 4 | import { type Page } from "puppeteer-core"; |
4 | | -import { pageScreenshotCapture, cdpSessionCache } from "./screenshotService.js"; |
| 5 | +import { |
| 6 | + pageScreenshotCapture, |
| 7 | + cdpSessionCache, |
| 8 | + injectVideoFramesBatch, |
| 9 | +} from "./screenshotService.js"; |
5 | 10 |
|
6 | 11 | // Stub a Page + CDPSession just enough that pageScreenshotCapture can call |
7 | 12 | // `client.send("Page.captureScreenshot", ...)` and we can inspect the args. |
@@ -90,3 +95,97 @@ describe("pageScreenshotCapture supersample plumbing", () => { |
90 | 95 | expect(params.clip?.scale).toBe(3); |
91 | 96 | }); |
92 | 97 | }); |
| 98 | + |
| 99 | +describe("injectVideoFramesBatch replacement layout", () => { |
| 100 | + it("does not copy opposing inset constraints onto the injected frame image", async () => { |
| 101 | + const { window, document } = parseHTML( |
| 102 | + '<html><body><div id="root"><video id="clip" style="position:absolute;inset:0;width:100%;height:100%;object-fit:cover"></video></div></body></html>', |
| 103 | + ); |
| 104 | + |
| 105 | + Object.defineProperty(window.HTMLImageElement.prototype, "decode", { |
| 106 | + configurable: true, |
| 107 | + value: () => Promise.resolve(), |
| 108 | + }); |
| 109 | + |
| 110 | + const video = document.getElementById("clip") as HTMLVideoElement; |
| 111 | + Object.defineProperties(video, { |
| 112 | + offsetLeft: { configurable: true, get: () => 0 }, |
| 113 | + offsetTop: { configurable: true, get: () => 0 }, |
| 114 | + offsetWidth: { configurable: true, get: () => 1920 }, |
| 115 | + offsetHeight: { configurable: true, get: () => 1080 }, |
| 116 | + }); |
| 117 | + video.getBoundingClientRect = () => |
| 118 | + ({ |
| 119 | + x: 0, |
| 120 | + y: 0, |
| 121 | + left: 0, |
| 122 | + top: 0, |
| 123 | + right: 1920, |
| 124 | + bottom: 1080, |
| 125 | + width: 1920, |
| 126 | + height: 1080, |
| 127 | + toJSON: () => ({}), |
| 128 | + }) as DOMRect; |
| 129 | + |
| 130 | + const computedStyle = document.createElement("div").style; |
| 131 | + computedStyle.position = "absolute"; |
| 132 | + computedStyle.width = "1920px"; |
| 133 | + computedStyle.height = "1080px"; |
| 134 | + computedStyle.top = "0px"; |
| 135 | + computedStyle.left = "0px"; |
| 136 | + computedStyle.right = "0px"; |
| 137 | + computedStyle.bottom = "0px"; |
| 138 | + computedStyle.inset = "0px"; |
| 139 | + computedStyle.objectFit = "cover"; |
| 140 | + computedStyle.objectPosition = "center center"; |
| 141 | + computedStyle.zIndex = "3"; |
| 142 | + computedStyle.opacity = "1"; |
| 143 | + Object.defineProperty(window, "getComputedStyle", { |
| 144 | + configurable: true, |
| 145 | + value: () => computedStyle, |
| 146 | + }); |
| 147 | + |
| 148 | + const globals = globalThis as unknown as { |
| 149 | + window?: typeof window; |
| 150 | + document?: Document; |
| 151 | + }; |
| 152 | + const previousWindow = globals.window; |
| 153 | + const previousDocument = globals.document; |
| 154 | + globals.window = window; |
| 155 | + globals.document = document; |
| 156 | + try { |
| 157 | + const page = { |
| 158 | + evaluate: async ( |
| 159 | + fn: ( |
| 160 | + updates: Array<{ videoId: string; dataUri: string }>, |
| 161 | + visualProperties: string[], |
| 162 | + ) => Promise<void>, |
| 163 | + updates: Array<{ videoId: string; dataUri: string }>, |
| 164 | + visualProperties: string[], |
| 165 | + ) => fn(updates, visualProperties), |
| 166 | + } as unknown as Page; |
| 167 | + |
| 168 | + await injectVideoFramesBatch(page, [ |
| 169 | + { |
| 170 | + videoId: "clip", |
| 171 | + dataUri: |
| 172 | + "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkAAIAAAoAAv/lxKUAAAAASUVORK5CYII=", |
| 173 | + }, |
| 174 | + ]); |
| 175 | + } finally { |
| 176 | + globals.window = previousWindow; |
| 177 | + globals.document = previousDocument; |
| 178 | + } |
| 179 | + |
| 180 | + const img = video.nextElementSibling as HTMLImageElement | null; |
| 181 | + expect(img).not.toBeNull(); |
| 182 | + expect(img?.style.position).toBe("absolute"); |
| 183 | + expect(img?.style.left).toBe("0px"); |
| 184 | + expect(img?.style.top).toBe("0px"); |
| 185 | + expect(img?.style.width).toBe("1920px"); |
| 186 | + expect(img?.style.height).toBe("1080px"); |
| 187 | + expect(img?.style.right).toBe("auto"); |
| 188 | + expect(img?.style.bottom).toBe("auto"); |
| 189 | + expect(img?.style.inset).toBe("auto"); |
| 190 | + }); |
| 191 | +}); |
0 commit comments