|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | + |
| 5 | +import { Canvas } from "@canvas/shotstack-canvas"; |
| 6 | + |
| 7 | +// pixi.js ships untransformed ESM that jest can't parse; captureFrame only needs the renderer stubbed below. |
| 8 | +jest.mock("pixi.js", () => ({ |
| 9 | + Application: jest.fn(), |
| 10 | + Container: jest.fn(), |
| 11 | + Graphics: jest.fn(), |
| 12 | + Rectangle: jest.fn() |
| 13 | +})); |
| 14 | +jest.mock("pixi.js/app", () => ({})); |
| 15 | +jest.mock("pixi.js/events", () => ({})); |
| 16 | +jest.mock("pixi.js/graphics", () => ({})); |
| 17 | +jest.mock("pixi.js/text", () => ({})); |
| 18 | +jest.mock("pixi.js/text-html", () => ({})); |
| 19 | +jest.mock("pixi.js/sprite-tiling", () => ({})); |
| 20 | +jest.mock("pixi.js/filters", () => ({})); |
| 21 | +jest.mock("pixi.js/mesh", () => ({})); |
| 22 | + |
| 23 | +const point = (x: number, y: number) => ({ |
| 24 | + x, |
| 25 | + y, |
| 26 | + set(nextX: number, nextY: number) { |
| 27 | + this.x = nextX; |
| 28 | + this.y = nextY; |
| 29 | + } |
| 30 | +}); |
| 31 | + |
| 32 | +describe("Canvas.captureFrame", () => { |
| 33 | + function setup({ started = true, extractFails = false } = {}) { |
| 34 | + const ticker = { |
| 35 | + started, |
| 36 | + stop: jest.fn(() => { |
| 37 | + ticker.started = false; |
| 38 | + }), |
| 39 | + start: jest.fn(() => { |
| 40 | + ticker.started = true; |
| 41 | + }), |
| 42 | + add: jest.fn(), |
| 43 | + remove: jest.fn() |
| 44 | + }; |
| 45 | + const viewportContainer = { position: point(40, 30), scale: point(0.5, 0.5) }; |
| 46 | + // What the screen would show if Pixi drew a frame while the extract is in flight. |
| 47 | + const duringExtract: { drawing: boolean; x: number; scale: number }[] = []; |
| 48 | + const base64 = jest.fn(async () => { |
| 49 | + duringExtract.push({ drawing: ticker.started, x: viewportContainer.position.x, scale: viewportContainer.scale.x }); |
| 50 | + if (extractFails) throw new Error("extract failed"); |
| 51 | + return "data:image/jpeg;base64,AAAA"; |
| 52 | + }); |
| 53 | + const canvas = Object.create(Canvas.prototype) as Canvas; |
| 54 | + Object.assign(canvas, { |
| 55 | + edit: { size: { width: 1920, height: 1080 } }, |
| 56 | + application: { ticker, renderer: { extract: { base64 } } }, |
| 57 | + viewportContainer, |
| 58 | + onTickBound: jest.fn() |
| 59 | + }); |
| 60 | + return { canvas, ticker, viewportContainer, duringExtract }; |
| 61 | + } |
| 62 | + |
| 63 | + it("stops Pixi drawing while the viewport is moved for the extract", async () => { |
| 64 | + const { canvas, ticker, duringExtract } = setup(); |
| 65 | + |
| 66 | + await expect(canvas.captureFrame({ format: "jpeg" })).resolves.toBe("data:image/jpeg;base64,AAAA"); |
| 67 | + |
| 68 | + expect(duringExtract).toEqual([{ drawing: false, x: 0, scale: 1 }]); |
| 69 | + expect(ticker.started).toBe(true); |
| 70 | + }); |
| 71 | + |
| 72 | + it("restores the viewport and restarts drawing when the extract fails", async () => { |
| 73 | + const { canvas, ticker, viewportContainer } = setup({ extractFails: true }); |
| 74 | + |
| 75 | + await expect(canvas.captureFrame()).rejects.toThrow("extract failed"); |
| 76 | + |
| 77 | + expect(viewportContainer.position).toMatchObject({ x: 40, y: 30 }); |
| 78 | + expect(viewportContainer.scale).toMatchObject({ x: 0.5, y: 0.5 }); |
| 79 | + expect(ticker.started).toBe(true); |
| 80 | + }); |
| 81 | + |
| 82 | + it("leaves a stopped ticker stopped", async () => { |
| 83 | + const { canvas, ticker } = setup({ started: false }); |
| 84 | + |
| 85 | + await canvas.captureFrame(); |
| 86 | + |
| 87 | + expect(ticker.start).not.toHaveBeenCalled(); |
| 88 | + expect(ticker.started).toBe(false); |
| 89 | + }); |
| 90 | +}); |
0 commit comments