|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test, vi } from 'vitest'; |
| 3 | +import { createScreenRecordingLiveHandle } from './screen-recording-live-handle.ts'; |
| 4 | + |
| 5 | +test('keeps mutable gesture evidence on the live handle and settles cleanup once', async () => { |
| 6 | + let cleanupCalls = 0; |
| 7 | + const handle = createScreenRecordingLiveHandle( |
| 8 | + { |
| 9 | + backend: 'fixture', |
| 10 | + outPath: '/tmp/recording.mp4', |
| 11 | + startedAt: 1, |
| 12 | + scope: 'app', |
| 13 | + showTouches: true, |
| 14 | + recordOnlySession: false, |
| 15 | + gestureEvents: [], |
| 16 | + }, |
| 17 | + { |
| 18 | + finish: async () => ({ |
| 19 | + status: 'completed', |
| 20 | + result: { |
| 21 | + backend: 'fixture', |
| 22 | + outPath: '/tmp/recording.mp4', |
| 23 | + startedAt: 1, |
| 24 | + completedAt: 2, |
| 25 | + scope: 'app', |
| 26 | + showTouches: true, |
| 27 | + recordOnlySession: false, |
| 28 | + }, |
| 29 | + }), |
| 30 | + forceCleanup: async () => { |
| 31 | + cleanupCalls += 1; |
| 32 | + return { status: 'cleaned' } as const; |
| 33 | + }, |
| 34 | + }, |
| 35 | + ); |
| 36 | + handle.appendGestureEvents([{ kind: 'tap', tMs: 3, x: 4, y: 5 }]); |
| 37 | + handle.setRunnerSessionId('runner-1'); |
| 38 | + handle.invalidate('runner restarted'); |
| 39 | + assert.deepEqual(handle.inspect().gestureEvents, [{ kind: 'tap', tMs: 3, x: 4, y: 5 }]); |
| 40 | + assert.equal(handle.inspect().invalidatedReason, 'runner restarted'); |
| 41 | + assert.equal(handle.inspect().runnerSessionId, 'runner-1'); |
| 42 | + await handle.forceCleanup(); |
| 43 | + await handle.forceCleanup(); |
| 44 | + assert.equal(cleanupCalls, 1); |
| 45 | +}); |
| 46 | + |
| 47 | +test('successful finish makes concurrent disposal inert', async () => { |
| 48 | + let resolveFinish: ((outcome: ReturnType<typeof completed>) => void) | undefined; |
| 49 | + const finish = vi.fn( |
| 50 | + async () => |
| 51 | + await new Promise<ReturnType<typeof completed>>((resolve) => { |
| 52 | + resolveFinish = resolve; |
| 53 | + }), |
| 54 | + ); |
| 55 | + const cleanup = vi.fn(async () => ({ status: 'cleaned' }) as const); |
| 56 | + const handle = createScreenRecordingLiveHandle(snapshot(), { finish, forceCleanup: cleanup }); |
| 57 | + |
| 58 | + const finishing = handle.finish(); |
| 59 | + const disposing = handle[Symbol.asyncDispose](); |
| 60 | + resolveFinish?.(completed()); |
| 61 | + |
| 62 | + await assert.doesNotReject(async () => await disposing); |
| 63 | + assert.deepEqual(await finishing, completed()); |
| 64 | + assert.equal(finish.mock.calls.length, 1); |
| 65 | + assert.equal(cleanup.mock.calls.length, 0); |
| 66 | +}); |
| 67 | + |
| 68 | +test('failed finish permits one forced cleanup', async () => { |
| 69 | + const finish = vi.fn(async () => { |
| 70 | + throw new Error('finalization failed'); |
| 71 | + }); |
| 72 | + const cleanup = vi.fn(async () => ({ status: 'cleaned' }) as const); |
| 73 | + const handle = createScreenRecordingLiveHandle(snapshot(), { finish, forceCleanup: cleanup }); |
| 74 | + |
| 75 | + await assert.rejects(async () => await handle.finish(), /finalization failed/); |
| 76 | + await handle[Symbol.asyncDispose](); |
| 77 | + await handle[Symbol.asyncDispose](); |
| 78 | + |
| 79 | + assert.equal(finish.mock.calls.length, 1); |
| 80 | + assert.equal(cleanup.mock.calls.length, 1); |
| 81 | +}); |
| 82 | + |
| 83 | +function snapshot() { |
| 84 | + return { |
| 85 | + backend: 'fixture', |
| 86 | + outPath: '/tmp/recording.mp4', |
| 87 | + startedAt: 1, |
| 88 | + scope: 'app' as const, |
| 89 | + showTouches: false, |
| 90 | + recordOnlySession: false, |
| 91 | + gestureEvents: [], |
| 92 | + }; |
| 93 | +} |
| 94 | + |
| 95 | +function completed() { |
| 96 | + return { |
| 97 | + status: 'completed' as const, |
| 98 | + result: { |
| 99 | + backend: 'fixture', |
| 100 | + outPath: '/tmp/recording.mp4', |
| 101 | + startedAt: 1, |
| 102 | + completedAt: 2, |
| 103 | + scope: 'app' as const, |
| 104 | + showTouches: false, |
| 105 | + recordOnlySession: false, |
| 106 | + }, |
| 107 | + }; |
| 108 | +} |
0 commit comments