|
| 1 | +import { expect, test } from 'vitest'; |
| 2 | +import { localRuntimeOwner } from '@agent-device/contracts/platform'; |
| 3 | +import { |
| 4 | + androidRecordingDevice, |
| 5 | + recordingHost, |
| 6 | + recordingInput, |
| 7 | + recordingProcess, |
| 8 | +} from './fixtures.ts'; |
| 9 | +import { bindAndroidScreenRecordingRuntime } from './runtime.ts'; |
| 10 | + |
| 11 | +const start = async (overrides: Record<string, unknown>, signal = new AbortController().signal) => |
| 12 | + await bindAndroidScreenRecordingRuntime({ |
| 13 | + host: recordingHost(overrides), |
| 14 | + device: androidRecordingDevice, |
| 15 | + owner: localRuntimeOwner('android'), |
| 16 | + signal, |
| 17 | + }); |
| 18 | + |
| 19 | +test('cancellation during active manifest publication retires evidence after confirmed rollback', async () => { |
| 20 | + const controller = new AbortController(); |
| 21 | + const reason = new Error('recording canceled during active manifest publication'); |
| 22 | + const calls: string[] = []; |
| 23 | + let manifest = ''; |
| 24 | + let writes = 0; |
| 25 | + const runtime = await start( |
| 26 | + { |
| 27 | + writeManifest: async ({ contents }: { contents: string }) => { |
| 28 | + manifest = contents; |
| 29 | + writes += 1; |
| 30 | + if (writes === 2) { |
| 31 | + controller.abort(reason); |
| 32 | + throw reason; |
| 33 | + } |
| 34 | + }, |
| 35 | + readManifest: async () => |
| 36 | + manifest ? { status: 'read' as const, contents: manifest } : { status: 'missing' as const }, |
| 37 | + removeManifest: async () => { |
| 38 | + calls.push('remove-manifest'); |
| 39 | + manifest = ''; |
| 40 | + return true; |
| 41 | + }, |
| 42 | + stop: async ({ pid }: { pid: string }) => { |
| 43 | + calls.push(`signal:${pid}`); |
| 44 | + return 'already-missing' as const; |
| 45 | + }, |
| 46 | + remove: async () => { |
| 47 | + calls.push('remove-artifact'); |
| 48 | + return true; |
| 49 | + }, |
| 50 | + }, |
| 51 | + controller.signal, |
| 52 | + ); |
| 53 | + |
| 54 | + await expect(runtime.screenRecordingStart(recordingInput())).rejects.toBe(reason); |
| 55 | + expect(calls).toEqual(['signal:42', 'remove-artifact', 'remove-manifest']); |
| 56 | + expect(manifest).toBe(''); |
| 57 | + |
| 58 | + const replacement = await start({ |
| 59 | + readManifest: async () => |
| 60 | + manifest ? { status: 'read' as const, contents: manifest } : { status: 'missing' as const }, |
| 61 | + writeManifest: async ({ contents }: { contents: string }) => { |
| 62 | + manifest = contents; |
| 63 | + }, |
| 64 | + }); |
| 65 | + await expect(replacement.screenRecordingStart(recordingInput())).resolves.toMatchObject({ |
| 66 | + envelope: expect.any(Object), |
| 67 | + }); |
| 68 | + expect(manifest).not.toBe(''); |
| 69 | +}); |
| 70 | + |
| 71 | +test('cancellation during active manifest publication retains evidence after uncertain rollback', async () => { |
| 72 | + const controller = new AbortController(); |
| 73 | + const reason = new Error('recording canceled with uncertain active cleanup'); |
| 74 | + let manifest = ''; |
| 75 | + let writes = 0; |
| 76 | + const runtime = await start( |
| 77 | + { |
| 78 | + writeManifest: async ({ contents }: { contents: string }) => { |
| 79 | + manifest = contents; |
| 80 | + writes += 1; |
| 81 | + if (writes === 2) { |
| 82 | + controller.abort(reason); |
| 83 | + throw reason; |
| 84 | + } |
| 85 | + }, |
| 86 | + readManifest: async () => |
| 87 | + manifest ? { status: 'read' as const, contents: manifest } : { status: 'missing' as const }, |
| 88 | + stop: async () => 'ownership-lost' as const, |
| 89 | + remove: async () => true, |
| 90 | + }, |
| 91 | + controller.signal, |
| 92 | + ); |
| 93 | + |
| 94 | + await expect(runtime.screenRecordingStart(recordingInput())).rejects.toBe(reason); |
| 95 | + expect(manifest).not.toBe(''); |
| 96 | + |
| 97 | + const replacement = await start({ |
| 98 | + readManifest: async () => ({ status: 'read' as const, contents: manifest }), |
| 99 | + start: async () => recordingProcess('43'), |
| 100 | + }); |
| 101 | + await expect(replacement.screenRecordingStart(recordingInput())).rejects.toThrow( |
| 102 | + 'native recovery evidence already exists', |
| 103 | + ); |
| 104 | +}); |
0 commit comments