|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test } from 'vitest'; |
| 3 | +import { iosTargetActivationDisclosure } from '@agent-device/contracts/ios-target-activation'; |
| 4 | +import type { IosTargetActivation } from '@agent-device/kernel/snapshot'; |
| 5 | +import { withCaptureDisclosures, withTargetActivationDisclosure } from '../capture-disclosure.ts'; |
| 6 | +import type { DaemonResponse } from '../daemon-request.ts'; |
| 7 | + |
| 8 | +const FACT: IosTargetActivation = { |
| 9 | + reason: 'stale_target', |
| 10 | + priorState: 'runningBackground', |
| 11 | + foregroundPid: 4562, |
| 12 | +}; |
| 13 | + |
| 14 | +function okResponse(data: Record<string, unknown>): DaemonResponse { |
| 15 | + return { ok: true, data }; |
| 16 | +} |
| 17 | + |
| 18 | +function dataOf(response: DaemonResponse): Record<string, unknown> { |
| 19 | + assert.ok(response.ok); |
| 20 | + assert.ok(response.data); |
| 21 | + return response.data; |
| 22 | +} |
| 23 | + |
| 24 | +test('a consumed repair is disclosed on the response without replacing earlier warnings', () => { |
| 25 | + const response = withTargetActivationDisclosure( |
| 26 | + okResponse({ warnings: ['Refs are stale'], targetActivation: undefined }), |
| 27 | + { targetActivation: FACT }, |
| 28 | + ); |
| 29 | + const data = dataOf(response); |
| 30 | + assert.deepEqual(data.warnings, ['Refs are stale', iosTargetActivationDisclosure(FACT)]); |
| 31 | + assert.deepEqual(data.targetActivation, FACT); |
| 32 | +}); |
| 33 | + |
| 34 | +test('a capture that already disclosed the identical repair is not told twice', () => { |
| 35 | + const already = okResponse({ warnings: [iosTargetActivationDisclosure(FACT)] }); |
| 36 | + const response = withTargetActivationDisclosure(already, { targetActivation: FACT }); |
| 37 | + assert.deepEqual(dataOf(response).warnings, [iosTargetActivationDisclosure(FACT)]); |
| 38 | +}); |
| 39 | + |
| 40 | +test('a failure response appends the repair to its hint and keeps the original details', () => { |
| 41 | + const response = withTargetActivationDisclosure( |
| 42 | + { |
| 43 | + ok: false, |
| 44 | + error: { |
| 45 | + code: 'COMMAND_FAILED', |
| 46 | + message: 'selector missed', |
| 47 | + details: { blockedBy: 'android_foreground_surface' }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + { targetActivation: FACT }, |
| 51 | + ); |
| 52 | + assert.equal(response.ok, false); |
| 53 | + if (response.ok) return; |
| 54 | + assert.equal(response.error.message, 'selector missed'); |
| 55 | + assert.equal(response.error.code, 'COMMAND_FAILED'); |
| 56 | + assert.equal(response.error.details?.blockedBy, 'android_foreground_surface'); |
| 57 | + assert.match(String(response.error.details?.hint), /prior state runningBackground/); |
| 58 | +}); |
| 59 | + |
| 60 | +test('a capture with no repair leaves the response byte-identical', () => { |
| 61 | + const untouched = okResponse({ nodes: [], warnings: ['Refs are stale'] }); |
| 62 | + const response = withTargetActivationDisclosure(untouched, { targetActivation: undefined }); |
| 63 | + assert.deepEqual(response, untouched); |
| 64 | +}); |
| 65 | + |
| 66 | +/** |
| 67 | + * The two carriers are pre-existing: the surface disclosure extends `data.warning`, the capture's |
| 68 | + * own warnings arrive as `data.warnings` (#2438 predates the array). A response can therefore carry |
| 69 | + * both, and each disclosure must survive in the carrier it belongs to. |
| 70 | + */ |
| 71 | +test('surface and foreground disclosures ride one response together', () => { |
| 72 | + const response = withCaptureDisclosures(okResponse({ nodes: [] }), { |
| 73 | + iosSystemSurfaceBundleId: 'com.apple.SafariViewService', |
| 74 | + targetActivation: FACT, |
| 75 | + }); |
| 76 | + const data = dataOf(response); |
| 77 | + assert.match(String(data.warning), /system web sign-in sheet/); |
| 78 | + assert.deepEqual(data.warnings, [iosTargetActivationDisclosure(FACT)]); |
| 79 | + assert.deepEqual(data.targetActivation, FACT); |
| 80 | +}); |
0 commit comments