|
| 1 | +import path from 'node:path'; |
| 2 | +import { expect, test, vi } from 'vitest'; |
| 3 | +import type { SnapshotQualityVerdict } from '@agent-device/kernel/snapshot'; |
| 4 | +import { makeIosSession, mkdtempForTest } from '../../__tests__/test-utils/index.ts'; |
| 5 | +import { SessionStore } from '../session-store.ts'; |
| 6 | +import { dispatchSnapshotViaRuntime } from '../snapshot-runtime.ts'; |
| 7 | + |
| 8 | +const dispatchCommandMock = vi.hoisted(() => vi.fn()); |
| 9 | + |
| 10 | +vi.mock('../../core/dispatch.ts', async (importOriginal) => { |
| 11 | + const actual = await importOriginal<typeof import('../../core/dispatch.ts')>(); |
| 12 | + return { |
| 13 | + ...actual, |
| 14 | + dispatchCommand: dispatchCommandMock, |
| 15 | + }; |
| 16 | +}); |
| 17 | + |
| 18 | +const SPARSE: SnapshotQualityVerdict = { |
| 19 | + state: 'sparse', |
| 20 | + backend: 'private-ax', |
| 21 | + reason: 'snapshot returned no semantic controls or content', |
| 22 | + reasonCode: 'sparse-tree', |
| 23 | +}; |
| 24 | + |
| 25 | +async function scenario() { |
| 26 | + const root = await mkdtempForTest('agent-device-sparse-fallback-'); |
| 27 | + const sessionStore = new SessionStore(path.join(root, 'sessions')); |
| 28 | + const sessionName = 'default'; |
| 29 | + sessionStore.set(sessionName, makeIosSession(sessionName, { appBundleId: 'com.example.app' })); |
| 30 | + return { sessionStore, sessionName, logPath: path.join(root, 'daemon.log') }; |
| 31 | +} |
| 32 | + |
| 33 | +/** Snapshot captures answer with the seeded verdict; screenshot captures answer per `screenshot`. */ |
| 34 | +function seed( |
| 35 | + verdict: SnapshotQualityVerdict, |
| 36 | + screenshot: () => Promise<Record<string, unknown>> = async () => ({ width: 390, height: 844 }), |
| 37 | +) { |
| 38 | + dispatchCommandMock.mockReset(); |
| 39 | + dispatchCommandMock.mockImplementation(async (_device: unknown, command: string) => |
| 40 | + command === 'screenshot' |
| 41 | + ? await screenshot() |
| 42 | + : { |
| 43 | + backend: 'xctest', |
| 44 | + truncated: false, |
| 45 | + quality: verdict, |
| 46 | + nodes: [{ index: 0, depth: 0, type: 'Application', label: 'Demo' }], |
| 47 | + }, |
| 48 | + ); |
| 49 | +} |
| 50 | + |
| 51 | +async function dispatch(input: Awaited<ReturnType<typeof scenario>>, internalObservation = false) { |
| 52 | + const response = await dispatchSnapshotViaRuntime({ |
| 53 | + req: { |
| 54 | + command: 'snapshot', |
| 55 | + positionals: [], |
| 56 | + token: 't', |
| 57 | + session: input.sessionName, |
| 58 | + ...(internalObservation ? { internal: { observationOnly: true } } : {}), |
| 59 | + }, |
| 60 | + sessionName: input.sessionName, |
| 61 | + logPath: input.logPath, |
| 62 | + sessionStore: input.sessionStore, |
| 63 | + }); |
| 64 | + if (!response.ok) throw new Error('expected ok response'); |
| 65 | + return response.data ?? {}; |
| 66 | +} |
| 67 | + |
| 68 | +function screenshotCalls() { |
| 69 | + return dispatchCommandMock.mock.calls.filter((call) => call[1] === 'screenshot'); |
| 70 | +} |
| 71 | + |
| 72 | +test('a sparse snapshot captures the screenshot its own remedy asks for and links it', async () => { |
| 73 | + const input = await scenario(); |
| 74 | + seed(SPARSE); |
| 75 | + |
| 76 | + const data = await dispatch(input); |
| 77 | + const warnings = (data.warnings ?? []) as string[]; |
| 78 | + |
| 79 | + expect(screenshotCalls()).toHaveLength(1); |
| 80 | + expect(data.fallbackScreenshotPath).toMatch(/\.png$/); |
| 81 | + expect(data.artifacts).toEqual([ |
| 82 | + { |
| 83 | + field: 'fallbackScreenshotPath', |
| 84 | + artifactType: 'screenshot', |
| 85 | + path: data.fallbackScreenshotPath, |
| 86 | + fileName: 'snapshot-fallback.png', |
| 87 | + }, |
| 88 | + ]); |
| 89 | + // The verdict's own two lines still stand: the refs are invalid, and a screen that |
| 90 | + // publishes nothing is an app defect worth reporting. |
| 91 | + expect(warnings.some((line) => line.startsWith('No snapshot backend could read'))).toBe(true); |
| 92 | + expect(warnings.some((line) => line.includes('app accessibility bug'))).toBe(true); |
| 93 | +}); |
| 94 | + |
| 95 | +test('a readable snapshot never pays for a screenshot', async () => { |
| 96 | + const input = await scenario(); |
| 97 | + seed({ state: 'healthy', backend: 'tree' }); |
| 98 | + |
| 99 | + const data = await dispatch(input); |
| 100 | + |
| 101 | + expect(screenshotCalls()).toHaveLength(0); |
| 102 | + expect(data.fallbackScreenshotPath).toBeUndefined(); |
| 103 | + expect(data.artifacts).toBeUndefined(); |
| 104 | +}); |
| 105 | + |
| 106 | +test('internal observations stay silent so a polling wait cannot shoot once per poll', async () => { |
| 107 | + const input = await scenario(); |
| 108 | + seed(SPARSE); |
| 109 | + |
| 110 | + const data = await dispatch(input, true); |
| 111 | + |
| 112 | + expect(screenshotCalls()).toHaveLength(0); |
| 113 | + expect(data.fallbackScreenshotPath).toBeUndefined(); |
| 114 | + expect(data.artifacts).toBeUndefined(); |
| 115 | +}); |
| 116 | + |
| 117 | +test('a failed fallback screenshot does not fail the snapshot that was asked for', async () => { |
| 118 | + const input = await scenario(); |
| 119 | + seed(SPARSE, async () => { |
| 120 | + throw new Error('screenshot dispatch exploded'); |
| 121 | + }); |
| 122 | + |
| 123 | + const data = await dispatch(input); |
| 124 | + const warnings = (data.warnings ?? []) as string[]; |
| 125 | + |
| 126 | + expect(screenshotCalls()).toHaveLength(1); |
| 127 | + expect(data.fallbackScreenshotPath).toBeUndefined(); |
| 128 | + expect(data.artifacts).toBeUndefined(); |
| 129 | + // The manual remedy is still on the response, so the caller is not left without one. |
| 130 | + expect(warnings.some((line) => line.includes('Use screenshot as visual truth'))).toBe(true); |
| 131 | +}); |
0 commit comments