|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { expect, test, vi } from 'vitest'; |
| 4 | +import { mkdtempForTestSync } from '@agent-device/test-kit'; |
| 5 | +import { AppError } from '@agent-device/kernel/errors'; |
| 6 | +import { |
| 7 | + createOwnerScopedDeviceClaimReconciler, |
| 8 | + type OwnerScopedClaimRecovery, |
| 9 | +} from './device-claim-owner-recovery.ts'; |
| 10 | +import type { DeviceClaim } from './device-claims.ts'; |
| 11 | +import { resolveDeviceClaimPath } from './device-claim-paths.ts'; |
| 12 | +import { writeDeviceClaim } from './device-claim-store.ts'; |
| 13 | +import { settleVerifiedOrphanedClaim } from './device-claim-settlement.ts'; |
| 14 | + |
| 15 | +function fixture(reconcile: OwnerScopedClaimRecovery['reconcile']) { |
| 16 | + const stateDir = mkdtempForTestSync('claim-recovery-dispose-'); |
| 17 | + const claim: DeviceClaim = { |
| 18 | + schemaVersion: 3, |
| 19 | + claimOwnerKind: 'open-session', |
| 20 | + deviceKey: 'local:android:none:emulator-5554', |
| 21 | + device: { family: 'android', id: 'emulator-5554', name: 'Pixel', kind: 'emulator' }, |
| 22 | + session: 'owner-session', |
| 23 | + workspace: '/worktrees/dead', |
| 24 | + stateDir, |
| 25 | + ownerPid: 999_999_999, |
| 26 | + ownerStartTime: 'dead-start', |
| 27 | + ownerToken: 'claim-token', |
| 28 | + createdAtMs: 1, |
| 29 | + updatedAtMs: 1, |
| 30 | + }; |
| 31 | + const env = { AGENT_DEVICE_CLAIMS_DIR: path.join(stateDir, 'claims') }; |
| 32 | + writeDeviceClaim(claim, env); |
| 33 | + const cleanupError = new AppError('COMMAND_FAILED', 'owner disposal failed', { |
| 34 | + reason: 'owner-cleanup-refused', |
| 35 | + }); |
| 36 | + const emit = vi.fn(); |
| 37 | + const dispose = vi.fn(async () => { |
| 38 | + throw cleanupError; |
| 39 | + }); |
| 40 | + const recovery = createOwnerScopedDeviceClaimReconciler({ |
| 41 | + scope: { |
| 42 | + signal: new AbortController().signal, |
| 43 | + diagnostics: { emit }, |
| 44 | + progress: { report: () => {} }, |
| 45 | + }, |
| 46 | + composeGateway: () => { |
| 47 | + throw new Error('the supplied recovery owns composition'); |
| 48 | + }, |
| 49 | + composeRecovery: () => ({ reconcile, dispose }), |
| 50 | + }); |
| 51 | + return { claim, env, cleanupError, emit, dispose, recovery }; |
| 52 | +} |
| 53 | + |
| 54 | +test('cleanup-only failure prevents a reconciled claim from being released', async () => { |
| 55 | + const f = fixture(async () => ({ status: 'reconciled' })); |
| 56 | + |
| 57 | + await expect(settleVerifiedOrphanedClaim(f.claim, f.recovery, f.env)).rejects.toBe( |
| 58 | + f.cleanupError, |
| 59 | + ); |
| 60 | + expect(fs.existsSync(resolveDeviceClaimPath(f.claim.deviceKey, f.env))).toBe(true); |
| 61 | + expect(f.dispose).toHaveBeenCalledOnce(); |
| 62 | +}); |
| 63 | + |
| 64 | +test('disposal failure preserves the primary reconciliation error and records secondary evidence', async () => { |
| 65 | + const primary = new AppError('DEVICE_IN_USE', 'reconciliation refused', { |
| 66 | + reason: 'primary-refusal', |
| 67 | + }); |
| 68 | + const f = fixture(async () => { |
| 69 | + throw primary; |
| 70 | + }); |
| 71 | + |
| 72 | + await expect(f.recovery(f.claim)).rejects.toBe(primary); |
| 73 | + expect(f.emit).toHaveBeenCalledWith( |
| 74 | + expect.objectContaining({ |
| 75 | + phase: 'device_claim_recovery_dispose_failed', |
| 76 | + data: expect.objectContaining({ |
| 77 | + error: expect.objectContaining({ details: { reason: 'owner-cleanup-refused' } }), |
| 78 | + }), |
| 79 | + }), |
| 80 | + ); |
| 81 | + expect(f.dispose).toHaveBeenCalledOnce(); |
| 82 | +}); |
| 83 | + |
| 84 | +test('disposal failure preserves an existing retained-claim reason', async () => { |
| 85 | + const retained = { status: 'retained', reason: 'screen-recording-owner-mismatch' } as const; |
| 86 | + const f = fixture(async () => retained); |
| 87 | + |
| 88 | + await expect(settleVerifiedOrphanedClaim(f.claim, f.recovery, f.env)).resolves.toBe(retained); |
| 89 | + expect(fs.existsSync(resolveDeviceClaimPath(f.claim.deviceKey, f.env))).toBe(true); |
| 90 | + expect(f.emit).toHaveBeenCalledWith( |
| 91 | + expect.objectContaining({ |
| 92 | + phase: 'device_claim_recovery_dispose_failed', |
| 93 | + data: expect.objectContaining({ retainedReason: retained.reason }), |
| 94 | + }), |
| 95 | + ); |
| 96 | + expect(f.dispose).toHaveBeenCalledOnce(); |
| 97 | +}); |
0 commit comments