|
| 1 | +import { |
| 2 | + ANDROID_DEVICE, |
| 3 | + ANDROID_EMULATOR, |
| 4 | + IOS_DEVICE, |
| 5 | + IOS_SIMULATOR, |
| 6 | + LINUX_DEVICE, |
| 7 | + MACOS_DEVICE, |
| 8 | + TVOS_SIMULATOR, |
| 9 | + WEB_DESKTOP_DEVICE, |
| 10 | +} from '../../__tests__/test-utils/device-fixtures.ts'; |
| 11 | +import { SCREENSHOT_CROP_REASONS } from '@agent-device/contracts/capture'; |
| 12 | +import type { SessionSurface } from '@agent-device/contracts/session'; |
| 13 | +import type { DeviceInfo } from '@agent-device/kernel/device'; |
| 14 | +import { expect, test } from 'vitest'; |
| 15 | +import { |
| 16 | + SCREENSHOT_CROP_TARGET_CELLS, |
| 17 | + assertScreenshotCropPolicy, |
| 18 | + classifyScreenshotCropTarget, |
| 19 | +} from '../screenshot-crop-target.ts'; |
| 20 | + |
| 21 | +type CropTargetDevice = Readonly<{ |
| 22 | + target: (typeof SCREENSHOT_CROP_TARGET_CELLS)[number]['target']; |
| 23 | + device: DeviceInfo; |
| 24 | + surface: SessionSurface | undefined; |
| 25 | +}>; |
| 26 | + |
| 27 | +/** One device per matrix cell: the classifier's whole reachable output, named by its cell. */ |
| 28 | +const CROP_TARGET_DEVICES: readonly CropTargetDevice[] = [ |
| 29 | + { target: 'ios-simulator', device: IOS_SIMULATOR, surface: undefined }, |
| 30 | + { target: 'android-emulator', device: ANDROID_EMULATOR, surface: undefined }, |
| 31 | + { target: 'android-device', device: ANDROID_DEVICE, surface: undefined }, |
| 32 | + { target: 'macos-app-window', device: MACOS_DEVICE, surface: 'app' }, |
| 33 | + { target: 'ios-physical', device: IOS_DEVICE, surface: undefined }, |
| 34 | + { target: 'macos-helper', device: MACOS_DEVICE, surface: undefined }, |
| 35 | + { target: 'web', device: WEB_DESKTOP_DEVICE, surface: undefined }, |
| 36 | + { target: 'linux', device: LINUX_DEVICE, surface: undefined }, |
| 37 | + { target: 'tvos', device: TVOS_SIMULATOR, surface: undefined }, |
| 38 | + { |
| 39 | + target: 'harmonyos', |
| 40 | + device: { platform: 'harmonyos', id: 'hmy-1', name: 'HarmonyOS', kind: 'device' }, |
| 41 | + surface: undefined, |
| 42 | + }, |
| 43 | + { |
| 44 | + target: 'vega', |
| 45 | + device: { platform: 'vega', id: 'vega-1', name: 'Vega TV', kind: 'device', target: 'tv' }, |
| 46 | + surface: undefined, |
| 47 | + }, |
| 48 | +]; |
| 49 | + |
| 50 | +test('the classifier and the acceptance matrix agree one-to-one, and the accepted cells are exactly the evidenced ones', () => { |
| 51 | + const matrixTargets = SCREENSHOT_CROP_TARGET_CELLS.map((cell) => cell.target); |
| 52 | + expect(new Set(matrixTargets).size).toBe(matrixTargets.length); |
| 53 | + for (const row of CROP_TARGET_DEVICES) { |
| 54 | + expect(classifyScreenshotCropTarget(row.device, row.surface)).toBe(row.target); |
| 55 | + } |
| 56 | + expect(CROP_TARGET_DEVICES.map((row) => row.target)).toEqual(matrixTargets); |
| 57 | + for (const cell of SCREENSHOT_CROP_TARGET_CELLS) { |
| 58 | + if (cell.target === 'ios-simulator' || cell.target === 'android-emulator') { |
| 59 | + expect(cell.status).toBe('accepted'); |
| 60 | + } else { |
| 61 | + expect(cell).toEqual({ |
| 62 | + target: cell.target, |
| 63 | + status: 'rejected', |
| 64 | + rejectionReason: SCREENSHOT_CROP_REASONS.pendingPixelIdentityEvidence, |
| 65 | + }); |
| 66 | + } |
| 67 | + } |
| 68 | +}); |
| 69 | + |
| 70 | +test('an apple device with an unpopulated reserved OS is a typed refusal, not a guess', () => { |
| 71 | + const device: DeviceInfo = { |
| 72 | + platform: 'apple', |
| 73 | + id: 'watch-1', |
| 74 | + name: 'Watch', |
| 75 | + kind: 'simulator', |
| 76 | + appleOs: 'watchos', |
| 77 | + }; |
| 78 | + let refusal: unknown; |
| 79 | + try { |
| 80 | + classifyScreenshotCropTarget(device, undefined); |
| 81 | + } catch (error) { |
| 82 | + refusal = error; |
| 83 | + } |
| 84 | + expect(refusal).toMatchObject({ |
| 85 | + code: 'UNSUPPORTED_OPERATION', |
| 86 | + details: { reason: SCREENSHOT_CROP_REASONS.targetNotAccepted }, |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +function expectPolicyRefusal( |
| 91 | + params: Readonly<{ |
| 92 | + device: DeviceInfo; |
| 93 | + surface: SessionSurface | undefined; |
| 94 | + cropOn: string; |
| 95 | + overlayRefs: boolean; |
| 96 | + fullscreen: boolean; |
| 97 | + }>, |
| 98 | + expected: Record<string, unknown>, |
| 99 | +): void { |
| 100 | + try { |
| 101 | + assertScreenshotCropPolicy(params); |
| 102 | + } catch (error) { |
| 103 | + expect(error).toMatchObject(expected); |
| 104 | + return; |
| 105 | + } |
| 106 | + throw new Error('the crop policy must refuse before device work'); |
| 107 | +} |
| 108 | + |
| 109 | +const POLICY_BASE = { cropOn: 'label="Save"', overlayRefs: false, fullscreen: false } as const; |
| 110 | + |
| 111 | +test('combination refusals are answered before selector validation and the matrix', () => { |
| 112 | + expectPolicyRefusal( |
| 113 | + { device: MACOS_DEVICE, surface: 'app', ...POLICY_BASE, overlayRefs: true }, |
| 114 | + { code: 'INVALID_ARGS', details: { reason: SCREENSHOT_CROP_REASONS.frameMismatch } }, |
| 115 | + ); |
| 116 | + expectPolicyRefusal( |
| 117 | + { device: ANDROID_EMULATOR, surface: undefined, ...POLICY_BASE, fullscreen: true }, |
| 118 | + { code: 'INVALID_ARGS', details: { reason: SCREENSHOT_CROP_REASONS.frameMismatch } }, |
| 119 | + ); |
| 120 | +}); |
| 121 | + |
| 122 | +test('an invalid selector expression is refused with the selector reason', () => { |
| 123 | + expectPolicyRefusal( |
| 124 | + { |
| 125 | + device: ANDROID_EMULATOR, |
| 126 | + surface: undefined, |
| 127 | + cropOn: 'label="unterminated', |
| 128 | + overlayRefs: false, |
| 129 | + fullscreen: false, |
| 130 | + }, |
| 131 | + { code: 'INVALID_ARGS', details: { reason: SCREENSHOT_CROP_REASONS.selectorInvalid } }, |
| 132 | + ); |
| 133 | +}); |
| 134 | + |
| 135 | +test('a matrix-rejected target is refused with the pending-evidence reason', () => { |
| 136 | + expectPolicyRefusal( |
| 137 | + { device: ANDROID_DEVICE, surface: undefined, ...POLICY_BASE }, |
| 138 | + { |
| 139 | + code: 'UNSUPPORTED_OPERATION', |
| 140 | + details: { |
| 141 | + reason: SCREENSHOT_CROP_REASONS.targetNotAccepted, |
| 142 | + rejectionReason: SCREENSHOT_CROP_REASONS.pendingPixelIdentityEvidence, |
| 143 | + }, |
| 144 | + }, |
| 145 | + ); |
| 146 | +}); |
| 147 | + |
| 148 | +test('an accepted target with a valid selector passes the policy', () => { |
| 149 | + expect(() => |
| 150 | + assertScreenshotCropPolicy({ device: ANDROID_EMULATOR, surface: undefined, ...POLICY_BASE }), |
| 151 | + ).not.toThrow(); |
| 152 | +}); |
0 commit comments