|
| 1 | +import os from 'node:os'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { expect, test, vi } from 'vitest'; |
| 4 | +import type { SnapshotQualityVerdict } from '@agent-device/kernel/snapshot'; |
| 5 | +import { makeIosSession } from '../../__tests__/test-utils/session-factories.ts'; |
| 6 | +import { |
| 7 | + recoveredSnapshotQualityWarning, |
| 8 | + renderSnapshotQualityWarnings, |
| 9 | +} from '../../snapshot/snapshot-quality.ts'; |
| 10 | +import { |
| 11 | + applyRecoveredWarningLatch, |
| 12 | + resolveRecoveredWarningLatch, |
| 13 | +} from '../snapshot-quality-latch.ts'; |
| 14 | +import { dispatchSnapshotViaRuntime } from '../snapshot-runtime.ts'; |
| 15 | +import { SessionStore } from '../session-store.ts'; |
| 16 | +import type { SessionState } from '../types.ts'; |
| 17 | + |
| 18 | +const dispatchCommandMock = vi.hoisted(() => vi.fn()); |
| 19 | + |
| 20 | +vi.mock('../../core/dispatch.ts', async (importOriginal) => { |
| 21 | + const actual = await importOriginal<typeof import('../../core/dispatch.ts')>(); |
| 22 | + return { |
| 23 | + ...actual, |
| 24 | + dispatchCommand: dispatchCommandMock, |
| 25 | + }; |
| 26 | +}); |
| 27 | + |
| 28 | +const FULL_WARNING = recoveredSnapshotQualityWarning('private-ax'); |
| 29 | + |
| 30 | +function deferredVerdict(): SnapshotQualityVerdict { |
| 31 | + return { |
| 32 | + state: 'recovered', |
| 33 | + backend: 'private-ax', |
| 34 | + reason: 'XCTest-backed snapshot tiers were deferred after recent slow accessibility work', |
| 35 | + reasonCode: 'deferred', |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +function genuineRecoveredVerdict(): SnapshotQualityVerdict { |
| 40 | + return { |
| 41 | + state: 'recovered', |
| 42 | + backend: 'private-ax', |
| 43 | + reason: 'iOS XCTest snapshot failed while serializing the accessibility tree', |
| 44 | + reasonCode: 'ax-rejected', |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +test('the injected warning is the exact line a genuine recovery renders', () => { |
| 49 | + // The latch exists to restore the warning an internal arming capture never rendered; |
| 50 | + // wording drift between the two paths would defeat one-shot deduplication. |
| 51 | + expect(renderSnapshotQualityWarnings(genuineRecoveredVerdict(), [])).toEqual([FULL_WARNING]); |
| 52 | +}); |
| 53 | + |
| 54 | +test('resolveRecoveredWarningLatch transitions', () => { |
| 55 | + const app = 'com.example.app'; |
| 56 | + |
| 57 | + // First deferred verdict with no latch held: inject once and hold the latch. |
| 58 | + const first = resolveRecoveredWarningLatch({ |
| 59 | + verdict: deferredVerdict(), |
| 60 | + appBundleId: app, |
| 61 | + latch: undefined, |
| 62 | + }); |
| 63 | + expect(first.warning).toBe(FULL_WARNING); |
| 64 | + expect(first.latch).toEqual({ appBundleId: app }); |
| 65 | + |
| 66 | + // Held latch: further deferred verdicts stay quiet. |
| 67 | + const repeat = resolveRecoveredWarningLatch({ |
| 68 | + verdict: deferredVerdict(), |
| 69 | + appBundleId: app, |
| 70 | + latch: first.latch, |
| 71 | + }); |
| 72 | + expect(repeat.warning).toBeUndefined(); |
| 73 | + expect(repeat.latch).toEqual({ appBundleId: app }); |
| 74 | + |
| 75 | + // A genuine recovery renders through the runtime, so it sets the latch silently. |
| 76 | + const genuine = resolveRecoveredWarningLatch({ |
| 77 | + verdict: genuineRecoveredVerdict(), |
| 78 | + appBundleId: app, |
| 79 | + latch: undefined, |
| 80 | + }); |
| 81 | + expect(genuine.warning).toBeUndefined(); |
| 82 | + expect(genuine.latch).toEqual({ appBundleId: app }); |
| 83 | + |
| 84 | + // Healthy ends the penalty window; the next arming warns again. |
| 85 | + const healthy = resolveRecoveredWarningLatch({ |
| 86 | + verdict: { state: 'healthy', backend: 'tree' }, |
| 87 | + appBundleId: app, |
| 88 | + latch: first.latch, |
| 89 | + }); |
| 90 | + expect(healthy.latch).toBeUndefined(); |
| 91 | + |
| 92 | + // Sparse and verdict-less captures carry no penalty signal either way. |
| 93 | + const sparse = resolveRecoveredWarningLatch({ |
| 94 | + verdict: { state: 'sparse', backend: 'private-ax' }, |
| 95 | + appBundleId: app, |
| 96 | + latch: first.latch, |
| 97 | + }); |
| 98 | + expect(sparse.warning).toBeUndefined(); |
| 99 | + expect(sparse.latch).toEqual({ appBundleId: app }); |
| 100 | + const absent = resolveRecoveredWarningLatch({ |
| 101 | + verdict: undefined, |
| 102 | + appBundleId: app, |
| 103 | + latch: first.latch, |
| 104 | + }); |
| 105 | + expect(absent.warning).toBeUndefined(); |
| 106 | + expect(absent.latch).toEqual({ appBundleId: app }); |
| 107 | + |
| 108 | + // The runner clears its penalty on app switch, so a latch held for another |
| 109 | + // app never suppresses the new app's first deferred verdict. |
| 110 | + const switched = resolveRecoveredWarningLatch({ |
| 111 | + verdict: deferredVerdict(), |
| 112 | + appBundleId: 'com.example.other', |
| 113 | + latch: first.latch, |
| 114 | + }); |
| 115 | + expect(switched.warning).toBe(FULL_WARNING); |
| 116 | + expect(switched.latch).toEqual({ appBundleId: 'com.example.other' }); |
| 117 | +}); |
| 118 | + |
| 119 | +test('internal observation responses neither consume nor clear the latch', () => { |
| 120 | + const session = makeIosSession('default', { appBundleId: 'com.example.app' }); |
| 121 | + |
| 122 | + const internal = applyRecoveredWarningLatch({ |
| 123 | + session, |
| 124 | + data: { snapshotQuality: deferredVerdict() }, |
| 125 | + internalObservation: true, |
| 126 | + }); |
| 127 | + expect(internal.warnings).toBeUndefined(); |
| 128 | + expect(session.recoveredSnapshotWarningLatch).toBeUndefined(); |
| 129 | + |
| 130 | + session.recoveredSnapshotWarningLatch = { appBundleId: 'com.example.app' }; |
| 131 | + applyRecoveredWarningLatch({ |
| 132 | + session, |
| 133 | + data: { snapshotQuality: { state: 'healthy', backend: 'tree' } }, |
| 134 | + internalObservation: true, |
| 135 | + }); |
| 136 | + expect(session.recoveredSnapshotWarningLatch).toEqual({ appBundleId: 'com.example.app' }); |
| 137 | +}); |
| 138 | + |
| 139 | +test('sessionless responses pass through unchanged', () => { |
| 140 | + const data = { snapshotQuality: deferredVerdict() }; |
| 141 | + expect( |
| 142 | + applyRecoveredWarningLatch({ session: undefined, data, internalObservation: false }), |
| 143 | + ).toBe(data); |
| 144 | +}); |
| 145 | + |
| 146 | +function scenario() { |
| 147 | + const root = path.join(os.tmpdir(), `agent-device-quality-latch-${crypto.randomUUID()}`); |
| 148 | + const sessionStore = new SessionStore(path.join(root, 'sessions')); |
| 149 | + const sessionName = 'default'; |
| 150 | + const session = makeIosSession(sessionName, { appBundleId: 'com.example.app' }); |
| 151 | + sessionStore.set(sessionName, session); |
| 152 | + return { sessionStore, sessionName, logPath: path.join(root, 'daemon.log') }; |
| 153 | +} |
| 154 | + |
| 155 | +function seedCapture(verdict: SnapshotQualityVerdict) { |
| 156 | + dispatchCommandMock.mockResolvedValue({ |
| 157 | + backend: 'xctest', |
| 158 | + truncated: false, |
| 159 | + quality: verdict, |
| 160 | + nodes: [ |
| 161 | + { |
| 162 | + index: 0, |
| 163 | + depth: 0, |
| 164 | + type: 'Button', |
| 165 | + label: 'Continue', |
| 166 | + rect: { x: 0, y: 0, width: 100, height: 44 }, |
| 167 | + hittable: true, |
| 168 | + }, |
| 169 | + ], |
| 170 | + }); |
| 171 | +} |
| 172 | + |
| 173 | +async function dispatchPublicSnapshot(input: ReturnType<typeof scenario>) { |
| 174 | + return await dispatchSnapshotViaRuntime({ |
| 175 | + req: { command: 'snapshot', positionals: [], token: 't', session: input.sessionName }, |
| 176 | + sessionName: input.sessionName, |
| 177 | + logPath: input.logPath, |
| 178 | + sessionStore: input.sessionStore, |
| 179 | + }); |
| 180 | +} |
| 181 | + |
| 182 | +function responseWarnings(response: Awaited<ReturnType<typeof dispatchSnapshotViaRuntime>>) { |
| 183 | + if (!response.ok) throw new Error('expected ok response'); |
| 184 | + return (response.data?.warnings ?? []) as string[]; |
| 185 | +} |
| 186 | + |
| 187 | +function storedLatch(input: ReturnType<typeof scenario>): SessionState['recoveredSnapshotWarningLatch'] { |
| 188 | + return input.sessionStore.get(input.sessionName)?.recoveredSnapshotWarningLatch; |
| 189 | +} |
| 190 | + |
| 191 | +test('an internally armed penalty warns once on the first public deferred snapshot', async () => { |
| 192 | + const input = scenario(); |
| 193 | + seedCapture(deferredVerdict()); |
| 194 | + |
| 195 | + // The internal capture that armed the penalty (settle observation, selector |
| 196 | + // resolution) rendered nothing; without the latch the deferred suppression |
| 197 | + // would hide the warning from the user entirely. |
| 198 | + const internal = await dispatchSnapshotViaRuntime({ |
| 199 | + req: { |
| 200 | + command: 'snapshot', |
| 201 | + positionals: [], |
| 202 | + token: 't', |
| 203 | + session: input.sessionName, |
| 204 | + internal: { observationOnly: true }, |
| 205 | + }, |
| 206 | + sessionName: input.sessionName, |
| 207 | + logPath: input.logPath, |
| 208 | + sessionStore: input.sessionStore, |
| 209 | + }); |
| 210 | + expect(responseWarnings(internal)).not.toContain(FULL_WARNING); |
| 211 | + expect(storedLatch(input)).toBeUndefined(); |
| 212 | + |
| 213 | + const first = await dispatchPublicSnapshot(input); |
| 214 | + expect(responseWarnings(first).filter((line) => line === FULL_WARNING)).toHaveLength(1); |
| 215 | + expect(storedLatch(input)).toEqual({ appBundleId: 'com.example.app' }); |
| 216 | + |
| 217 | + const second = await dispatchPublicSnapshot(input); |
| 218 | + expect(responseWarnings(second)).not.toContain(FULL_WARNING); |
| 219 | +}); |
| 220 | + |
| 221 | +test('a healthy public capture re-arms the one-shot warning', async () => { |
| 222 | + const input = scenario(); |
| 223 | + seedCapture(deferredVerdict()); |
| 224 | + await dispatchPublicSnapshot(input); |
| 225 | + expect(storedLatch(input)).toEqual({ appBundleId: 'com.example.app' }); |
| 226 | + |
| 227 | + seedCapture({ state: 'healthy', backend: 'tree' }); |
| 228 | + const healthy = await dispatchPublicSnapshot(input); |
| 229 | + expect(responseWarnings(healthy)).not.toContain(FULL_WARNING); |
| 230 | + expect(storedLatch(input)).toBeUndefined(); |
| 231 | + |
| 232 | + seedCapture(deferredVerdict()); |
| 233 | + const rearmed = await dispatchPublicSnapshot(input); |
| 234 | + expect(responseWarnings(rearmed).filter((line) => line === FULL_WARNING)).toHaveLength(1); |
| 235 | +}); |
| 236 | + |
| 237 | +test('a genuine recovered render keeps later deferred captures quiet without doubling', async () => { |
| 238 | + const input = scenario(); |
| 239 | + seedCapture(genuineRecoveredVerdict()); |
| 240 | + |
| 241 | + const genuine = await dispatchPublicSnapshot(input); |
| 242 | + expect(responseWarnings(genuine).filter((line) => line === FULL_WARNING)).toHaveLength(1); |
| 243 | + expect(storedLatch(input)).toEqual({ appBundleId: 'com.example.app' }); |
| 244 | + |
| 245 | + seedCapture(deferredVerdict()); |
| 246 | + const deferred = await dispatchPublicSnapshot(input); |
| 247 | + expect(responseWarnings(deferred)).not.toContain(FULL_WARNING); |
| 248 | +}); |
0 commit comments