|
| 1 | +import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | + |
| 3 | +import { createRecordingCompositor } from "./recordingCompositor"; |
| 4 | + |
| 5 | +const paintSource = (canvas: HTMLCanvasElement, color: string) => { |
| 6 | + const context = canvas.getContext("2d"); |
| 7 | + if (!context) throw new Error("Source canvas is unavailable."); |
| 8 | + context.fillStyle = color; |
| 9 | + context.fillRect(0, 0, canvas.width, canvas.height); |
| 10 | +}; |
| 11 | + |
| 12 | +const samplePixel = (video: HTMLVideoElement, x: number, y: number) => { |
| 13 | + const canvas = document.createElement("canvas"); |
| 14 | + canvas.width = video.videoWidth; |
| 15 | + canvas.height = video.videoHeight; |
| 16 | + const context = canvas.getContext("2d", { willReadFrequently: true }); |
| 17 | + if (!context) throw new Error("Sample canvas is unavailable."); |
| 18 | + context.drawImage(video, 0, 0); |
| 19 | + return Array.from(context.getImageData(x, y, 1, 1).data.slice(0, 3)); |
| 20 | +}; |
| 21 | + |
| 22 | +describe("recording compositor", () => { |
| 23 | + it("keeps the starting size and letterboxes a resized source", async () => { |
| 24 | + const source = document.createElement("canvas"); |
| 25 | + source.width = 320; |
| 26 | + source.height = 180; |
| 27 | + paintSource(source, "#f00"); |
| 28 | + const sourceStream = source.captureStream(30); |
| 29 | + const compositor = await createRecordingCompositor( |
| 30 | + sourceStream, |
| 31 | + { showKeyPresses: false, showMousePresses: false, frameRate: 30 }, |
| 32 | + () => () => {}, |
| 33 | + ); |
| 34 | + const output = document.createElement("video"); |
| 35 | + output.muted = true; |
| 36 | + output.srcObject = compositor.stream; |
| 37 | + try { |
| 38 | + await output.play(); |
| 39 | + await vi.waitFor(() => { |
| 40 | + expect([output.videoWidth, output.videoHeight]).toEqual([320, 180]); |
| 41 | + expect(samplePixel(output, 160, 90)).toEqual([255, 0, 0]); |
| 42 | + }); |
| 43 | + |
| 44 | + source.width = 90; |
| 45 | + source.height = 160; |
| 46 | + paintSource(source, "#0f0"); |
| 47 | + |
| 48 | + await vi.waitFor(() => { |
| 49 | + paintSource(source, "#0f0"); |
| 50 | + expect(samplePixel(output, 160, 90)).toEqual([0, 255, 0]); |
| 51 | + }); |
| 52 | + expect([output.videoWidth, output.videoHeight]).toEqual([320, 180]); |
| 53 | + expect(samplePixel(output, 10, 90)).toEqual([0, 0, 0]); |
| 54 | + } finally { |
| 55 | + output.srcObject = null; |
| 56 | + compositor.dispose(); |
| 57 | + for (const track of sourceStream.getTracks()) track.stop(); |
| 58 | + } |
| 59 | + }); |
| 60 | +}); |
0 commit comments