|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import { DESKTOP_PREVIEW_SCREENSHOT_ACTIVITY } from "@t3tools/contracts"; |
| 3 | +import * as Cause from "effect/Cause"; |
| 4 | +import * as Effect from "effect/Effect"; |
| 5 | +import * as Exit from "effect/Exit"; |
| 6 | +import * as Fiber from "effect/Fiber"; |
| 7 | +import { TestClock } from "effect/testing"; |
| 8 | +import { vi } from "vite-plus/test"; |
| 9 | + |
| 10 | +import { captureFreshScreenshot } from "./ScreenshotCapture.ts"; |
| 11 | + |
| 12 | +const makeHarness = () => { |
| 13 | + const reply = Promise.withResolvers<unknown>(); |
| 14 | + const copying = Promise.withResolvers<void>(); |
| 15 | + const copy = Promise.withResolvers<Electron.NativeImage>(); |
| 16 | + const activity: unknown[][] = []; |
| 17 | + const image = { isEmpty: () => false } as Electron.NativeImage; |
| 18 | + const globals = { |
| 19 | + [DESKTOP_PREVIEW_SCREENSHOT_ACTIVITY]: (...args: unknown[]) => { |
| 20 | + activity.push(args); |
| 21 | + return true; |
| 22 | + }, |
| 23 | + }; |
| 24 | + const sendCommand = vi.fn(() => reply.promise); |
| 25 | + const capturePage = vi.fn(() => { |
| 26 | + copying.resolve(); |
| 27 | + return copy.promise; |
| 28 | + }); |
| 29 | + const contents = { |
| 30 | + hostWebContents: { |
| 31 | + isDestroyed: () => false, |
| 32 | + executeJavaScript: async (script: string) => |
| 33 | + new Function("globalThis", `return ${script}`)(globals), |
| 34 | + }, |
| 35 | + capturePage, |
| 36 | + } as unknown as Electron.WebContents; |
| 37 | + const debuggerSession = { sendCommand } as unknown as Electron.Debugger; |
| 38 | + const capture = (params: Record<string, unknown> = {}) => |
| 39 | + captureFreshScreenshot(contents, debuggerSession, params, "runtime-tab", "capture-token"); |
| 40 | + return { reply, copying, copy, image, activity, sendCommand, capturePage, capture }; |
| 41 | +}; |
| 42 | + |
| 43 | +describe("captureFreshScreenshot", () => { |
| 44 | + it.effect("drives an occluded capture without replacing the CDP image or its parameters", () => |
| 45 | + Effect.gen(function* () { |
| 46 | + const h = makeHarness(); |
| 47 | + const params = { |
| 48 | + format: "jpeg", |
| 49 | + quality: 65, |
| 50 | + clip: { x: 2, y: 3, width: 40, height: 50, scale: 0.5 }, |
| 51 | + }; |
| 52 | + const fiber = yield* h.capture(params).pipe(Effect.forkChild); |
| 53 | + yield* Effect.promise(() => h.copying.promise); |
| 54 | + h.reply.resolve({ data: "fresh-cdp-image" }); |
| 55 | + expect(yield* Fiber.join(fiber)).toEqual({ data: "fresh-cdp-image" }); |
| 56 | + h.copy.resolve(h.image); |
| 57 | + expect(h.sendCommand).toHaveBeenCalledExactlyOnceWith("Page.captureScreenshot", params); |
| 58 | + expect(h.activity.map((args) => args[2])).toEqual([true, false]); |
| 59 | + expect(h.activity[0]?.slice(0, 2)).toEqual(h.activity[1]?.slice(0, 2)); |
| 60 | + expect(h.capturePage).toHaveBeenCalledTimes(1); |
| 61 | + }), |
| 62 | + ); |
| 63 | + |
| 64 | + it.effect("retries a cold guest copy while preserving the pending CDP capture", () => |
| 65 | + Effect.gen(function* () { |
| 66 | + const h = makeHarness(); |
| 67 | + const firstCopy = Promise.withResolvers<void>(); |
| 68 | + h.capturePage.mockImplementationOnce(() => { |
| 69 | + firstCopy.resolve(); |
| 70 | + return Promise.reject(new Error("UnknownVizError")); |
| 71 | + }); |
| 72 | + const fiber = yield* h.capture().pipe(Effect.forkChild); |
| 73 | + yield* Effect.promise(() => firstCopy.promise); |
| 74 | + yield* TestClock.adjust("120 millis"); |
| 75 | + yield* Effect.promise(() => h.copying.promise); |
| 76 | + h.reply.resolve({ data: "fresh-after-cold-start" }); |
| 77 | + expect(yield* Fiber.join(fiber)).toEqual({ data: "fresh-after-cold-start" }); |
| 78 | + h.copy.resolve(h.image); |
| 79 | + expect(h.capturePage).toHaveBeenCalledTimes(2); |
| 80 | + expect(h.sendCommand).toHaveBeenCalledTimes(1); |
| 81 | + }), |
| 82 | + ); |
| 83 | + |
| 84 | + it.effect("bounds a stalled native copy and releases renderer activity", () => |
| 85 | + Effect.gen(function* () { |
| 86 | + const h = makeHarness(); |
| 87 | + const fiber = yield* h.capture().pipe(Effect.forkChild); |
| 88 | + yield* Effect.promise(() => h.copying.promise); |
| 89 | + yield* TestClock.adjust("3 seconds"); |
| 90 | + expect(Exit.isFailure(yield* Fiber.await(fiber))).toBe(true); |
| 91 | + expect(h.activity.map((args) => args[2])).toEqual([true, false]); |
| 92 | + h.copy.resolve(h.image); |
| 93 | + expect(h.capturePage).toHaveBeenCalledTimes(1); |
| 94 | + }), |
| 95 | + ); |
| 96 | + |
| 97 | + it.effect("releases activity when its relay is interrupted while a copy is pending", () => |
| 98 | + Effect.gen(function* () { |
| 99 | + const h = makeHarness(); |
| 100 | + const fiber = yield* h.capture().pipe(Effect.forkChild); |
| 101 | + yield* Effect.promise(() => h.copying.promise); |
| 102 | + yield* Fiber.interrupt(fiber); |
| 103 | + h.copy.resolve(h.image); |
| 104 | + expect(h.activity.map((args) => args[2])).toEqual([true, false]); |
| 105 | + expect(h.capturePage).toHaveBeenCalledTimes(1); |
| 106 | + }), |
| 107 | + ); |
| 108 | + |
| 109 | + it.effect("stops on an unavailable native surface instead of repeatedly copying it", () => |
| 110 | + Effect.gen(function* () { |
| 111 | + const h = makeHarness(); |
| 112 | + h.copy.resolve({ isEmpty: () => true } as Electron.NativeImage); |
| 113 | + const result = yield* h.capture().pipe(Effect.exit); |
| 114 | + expect(Exit.isFailure(result)).toBe(true); |
| 115 | + if (Exit.isFailure(result)) |
| 116 | + expect(Cause.pretty(result.cause)).toContain("surface is unavailable"); |
| 117 | + expect(h.activity.map((args) => args[2])).toEqual([true, false]); |
| 118 | + expect(h.capturePage).toHaveBeenCalledTimes(1); |
| 119 | + }), |
| 120 | + ); |
| 121 | +}); |
0 commit comments