|
1 | 1 | import * as NodeEvents from "node:events"; |
| 2 | +import * as NodeVM from "node:vm"; |
2 | 3 | // @effect-diagnostics-next-line nodeBuiltinImport:off - Playwright download mocks perform real file operations outside the Effect runtime. |
3 | 4 | import * as NodeFSP from "node:fs/promises"; |
4 | 5 |
|
5 | 6 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
6 | 7 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
7 | 8 | import { it } from "@effect/vitest"; |
8 | 9 | import { |
| 10 | + type BrowserRecordingFrameRate, |
9 | 11 | EnvironmentId, |
10 | 12 | PreviewTabId, |
11 | 13 | ProviderInstanceId, |
@@ -49,7 +51,7 @@ vi.mock("./ServerBrowserContexts.ts", () => ({ |
49 | 51 | return context as unknown as BrowserContext; |
50 | 52 | } |
51 | 53 | async scratchPage() { |
52 | | - return makeContext().page as unknown as Page; |
| 54 | + return makeEncoderPage() as unknown as Page; |
53 | 55 | } |
54 | 56 | async connectDesktopPage(endpoint: string) { |
55 | 57 | const context = makeContext(); |
@@ -212,7 +214,50 @@ function makeContext(onClose?: (context: BrowserContext) => void) { |
212 | 214 | return context; |
213 | 215 | } |
214 | 216 |
|
| 217 | +// Runs the real encoder script against fake canvas and MediaRecorder globals. |
| 218 | +function makeEncoderPage() { |
| 219 | + const canvas = { |
| 220 | + width: 0, |
| 221 | + height: 0, |
| 222 | + getContext: () => ({ drawImage() {}, fillRect() {} }), |
| 223 | + captureStream: (frameRate?: number) => ({ frameRate, getTracks: () => [] }), |
| 224 | + }; |
| 225 | + class FakeMediaRecorder { |
| 226 | + static isTypeSupported = (type: string) => type === "video/webm"; |
| 227 | + onstart: (() => void) | null = null; |
| 228 | + constructor(stream: { frameRate?: number }, options: { videoBitsPerSecond: number }) { |
| 229 | + encoderStreams.push({ |
| 230 | + frameRate: stream.frameRate, |
| 231 | + bitsPerSecond: options.videoBitsPerSecond, |
| 232 | + }); |
| 233 | + } |
| 234 | + start() { |
| 235 | + this.onstart?.(); |
| 236 | + } |
| 237 | + } |
| 238 | + const sandbox: Record<string, unknown> = { |
| 239 | + document: { createElement: () => canvas }, |
| 240 | + Path2D: function Path2D() {}, |
| 241 | + MediaRecorder: FakeMediaRecorder, |
| 242 | + createImageBitmap: async () => ({ width: 1280, height: 800, close() {} }), |
| 243 | + atob, |
| 244 | + Blob, |
| 245 | + performance, |
| 246 | + }; |
| 247 | + sandbox.window = sandbox; |
| 248 | + const context = NodeVM.createContext(sandbox); |
| 249 | + return { |
| 250 | + ...makeContext().page, |
| 251 | + evaluate: vi.fn(async (script: string | ((arg: unknown) => unknown), arg?: unknown) => |
| 252 | + typeof script === "string" |
| 253 | + ? NodeVM.runInContext(script, context) |
| 254 | + : NodeVM.runInContext(`(${script.toString()})`, context)(arg), |
| 255 | + ), |
| 256 | + }; |
| 257 | +} |
| 258 | + |
215 | 259 | const contexts: ReturnType<typeof makeContext>[] = []; |
| 260 | +const encoderStreams: Array<{ frameRate: number | undefined; bitsPerSecond: number }> = []; |
216 | 261 | const contextRequests: Array<{ profileId: string; isolated: boolean }> = []; |
217 | 262 | let contextGate: PromiseWithResolvers<void> | null = null; |
218 | 263 | let pageCopy = ""; |
@@ -320,6 +365,7 @@ const viewerInput = (tabId: string, canOperate: boolean) => ({ |
320 | 365 |
|
321 | 366 | beforeEach(() => { |
322 | 367 | contexts.length = 0; |
| 368 | + encoderStreams.length = 0; |
323 | 369 | contextRequests.length = 0; |
324 | 370 | contextGate = null; |
325 | 371 | contextFailure = null; |
@@ -418,6 +464,36 @@ it.live.each([ |
418 | 464 | ).pipe(Effect.provide(layer)), |
419 | 465 | ); |
420 | 466 |
|
| 467 | +// The fake encoder decodes every frame at 1280x800, so 30 fps hits the bitrate floor. |
| 468 | +it.live.each([ |
| 469 | + { reports: [], frameRate: 30, bitsPerSecond: 2.5e6 }, |
| 470 | + { reports: [30], frameRate: 30, bitsPerSecond: 2.5e6 }, |
| 471 | + { reports: [60], frameRate: 60, bitsPerSecond: 1280 * 800 * 60 * 0.05 }, |
| 472 | + { reports: [60, undefined], frameRate: 60, bitsPerSecond: 1280 * 800 * 60 * 0.05 }, |
| 473 | + { reports: [60, 30], frameRate: 30, bitsPerSecond: 2.5e6 }, |
| 474 | +] satisfies ReadonlyArray<{ |
| 475 | + reports: ReadonlyArray<BrowserRecordingFrameRate | undefined>; |
| 476 | + frameRate: number; |
| 477 | + bitsPerSecond: number; |
| 478 | +}>)( |
| 479 | + "agent recordings encode at the last reported frame rate ($reports)", |
| 480 | + ({ reports, frameRate, bitsPerSecond }) => |
| 481 | + Effect.scoped( |
| 482 | + Effect.gen(function* () { |
| 483 | + const { browser, broker, tabId } = yield* ready; |
| 484 | + for (const recordingFrameRate of reports) { |
| 485 | + yield* browser.reportProfiles({ |
| 486 | + profiles: [], |
| 487 | + defaultProfileId: "default", |
| 488 | + ...(recordingFrameRate === undefined ? {} : { recordingFrameRate }), |
| 489 | + }); |
| 490 | + } |
| 491 | + yield* broker.invoke({ scope, tabId, operation: "recordingStart", input: {} }); |
| 492 | + expect(encoderStreams).toEqual([{ frameRate, bitsPerSecond }]); |
| 493 | + }), |
| 494 | + ).pipe(Effect.provide(layer)), |
| 495 | +); |
| 496 | + |
421 | 497 | it.live("enforces provider ownership and explicit targets when a session has multiple tabs", () => |
422 | 498 | Effect.scoped( |
423 | 499 | Effect.gen(function* () { |
|
0 commit comments