|
| 1 | +import { beforeEach, expect, test, vi } from 'vitest'; |
| 2 | + |
| 3 | +vi.mock('./pose.ts', () => ({ setAppleFoldPose: vi.fn() })); |
| 4 | + |
| 5 | +import { |
| 6 | + localRuntimeOwner, |
| 7 | + narrowDeviceBinding, |
| 8 | + type DeviceBinding, |
| 9 | + type RuntimeFacts, |
| 10 | +} from '@agent-device/contracts/platform-runtime'; |
| 11 | +import { |
| 12 | + foldRuntimeUse, |
| 13 | + type PlatformRuntimeOperations, |
| 14 | +} from '@agent-device/contracts/platform-runtime-operations'; |
| 15 | +import { deviceShape, type DeviceInfo } from '@agent-device/kernel/device'; |
| 16 | + |
| 17 | +import { appleFoldableFacts, createAppleFoldableOperations } from './runtime.ts'; |
| 18 | +import { setAppleFoldPose } from './pose.ts'; |
| 19 | + |
| 20 | +const mockPose = vi.mocked(setAppleFoldPose); |
| 21 | + |
| 22 | +const duo: DeviceInfo = { |
| 23 | + platform: 'apple', |
| 24 | + appleOs: 'ios', |
| 25 | + id: '4F879835-4AB3-4046-B033-5AB769209DD4', |
| 26 | + name: 'iPhone Duo', |
| 27 | + kind: 'simulator', |
| 28 | + target: 'mobile', |
| 29 | + booted: true, |
| 30 | +}; |
| 31 | +const scopedDuo: DeviceInfo = { ...duo, simulatorSetPath: '/tmp/scoped-set' }; |
| 32 | + |
| 33 | +/** A full owner binding whose fold cell is derived only from the fact under test. */ |
| 34 | +function foldBinding(device: DeviceInfo): DeviceBinding<PlatformRuntimeOperations> { |
| 35 | + const { setFoldPose } = appleFoldableFacts(device); |
| 36 | + return { |
| 37 | + device, |
| 38 | + owner: localRuntimeOwner('apple'), |
| 39 | + facts: { |
| 40 | + device: { ...deviceShape(device), providerMode: 'local' }, |
| 41 | + operations: { setFoldPose } as RuntimeFacts<PlatformRuntimeOperations>['operations'], |
| 42 | + }, |
| 43 | + operations: createAppleFoldableOperations({ device, signal: new AbortController().signal }), |
| 44 | + [Symbol.asyncDispose]: async () => {}, |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +function thrownBy<T>(run: () => T): unknown { |
| 49 | + try { |
| 50 | + run(); |
| 51 | + } catch (error) { |
| 52 | + return error; |
| 53 | + } |
| 54 | + throw new Error('expected the routed fold binding to refuse'); |
| 55 | +} |
| 56 | + |
| 57 | +beforeEach(() => { |
| 58 | + mockPose.mockReset(); |
| 59 | +}); |
| 60 | + |
| 61 | +test('the default-set iPhone Duo admits setFoldPose and binds the pose operation', () => { |
| 62 | + expect(appleFoldableFacts(duo).setFoldPose).toEqual({ available: true }); |
| 63 | + expect( |
| 64 | + createAppleFoldableOperations({ device: duo, signal: new AbortController().signal }), |
| 65 | + ).toHaveProperty('setFoldPose', expect.any(Function)); |
| 66 | +}); |
| 67 | + |
| 68 | +test('a scoped simulator set refuses setFoldPose with the typed unsupported-device-scope fact', () => { |
| 69 | + expect(appleFoldableFacts(scopedDuo).setFoldPose).toMatchObject({ |
| 70 | + available: false, |
| 71 | + reason: 'unsupported-device-scope', |
| 72 | + hint: expect.stringContaining('/tmp/scoped-set'), |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +test('a scoped simulator set binds no pose operation and never reaches a hinge', () => { |
| 77 | + const operations = createAppleFoldableOperations({ |
| 78 | + device: scopedDuo, |
| 79 | + signal: new AbortController().signal, |
| 80 | + }); |
| 81 | + expect(operations).not.toHaveProperty('setFoldPose'); |
| 82 | + expect(mockPose).not.toHaveBeenCalled(); |
| 83 | +}); |
| 84 | + |
| 85 | +test('the routed fold response refuses a scoped simulator set with the typed reason before any tool call', () => { |
| 86 | + const refusal = thrownBy(() => narrowDeviceBinding(foldBinding(scopedDuo), foldRuntimeUse)) as { |
| 87 | + code?: string; |
| 88 | + details?: { reason?: string; hint?: string }; |
| 89 | + }; |
| 90 | + expect(refusal.code).toBe('UNSUPPORTED_OPERATION'); |
| 91 | + expect(refusal.details?.reason).toBe('unsupported-device-scope'); |
| 92 | + expect(refusal.details?.hint).toContain('/tmp/scoped-set'); |
| 93 | + expect(mockPose).not.toHaveBeenCalled(); |
| 94 | +}); |
| 95 | + |
| 96 | +test('a scoped set never outranks the existing kind and OS refusals', () => { |
| 97 | + expect(appleFoldableFacts({ ...scopedDuo, kind: 'device' }).setFoldPose).toMatchObject({ |
| 98 | + available: false, |
| 99 | + reason: 'unsupported-device-kind', |
| 100 | + }); |
| 101 | + expect( |
| 102 | + appleFoldableFacts({ ...scopedDuo, appleOs: 'tvos', target: 'tv' }).setFoldPose, |
| 103 | + ).toMatchObject({ available: false, reason: 'unsupported-platform-leaf' }); |
| 104 | +}); |
0 commit comments