|
1 | 1 | import assert from 'node:assert/strict'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import os from 'node:os'; |
| 4 | +import path from 'node:path'; |
2 | 5 | import { test } from 'vitest'; |
| 6 | +import { readCurrentOwnerIdentity } from '../utils/owner-identity.ts'; |
3 | 7 | import { runCliCapture } from './cli-capture.ts'; |
4 | 8 |
|
5 | 9 | test('device status is daemonless and does not send a daemon request', async () => { |
6 | 10 | const result = await runCliCapture(['device', 'status', '--json']); |
7 | 11 | assert.equal(result.code, null); |
8 | 12 | assert.equal(result.calls.length, 0); |
9 | 13 | const payload = JSON.parse(result.stdout); |
10 | | - assert.deepEqual(payload, { success: true, data: { claims: [] } }); |
| 14 | + assert.deepEqual(payload, { success: true, data: { claims: [], hiddenStaleClaims: 0 } }); |
| 15 | +}); |
| 16 | + |
| 17 | +test('keeps normal status compact while retaining proven-stale claims for explicit inspection', async () => { |
| 18 | + const claimsDir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-cli-claims-')); |
| 19 | + const owner = readCurrentOwnerIdentity(); |
| 20 | + try { |
| 21 | + fs.writeFileSync( |
| 22 | + path.join(claimsDir, 'live.json'), |
| 23 | + JSON.stringify({ |
| 24 | + schemaVersion: 1, |
| 25 | + deviceKey: 'local:android:none:live', |
| 26 | + device: { platform: 'android', id: 'live', name: 'Live Pixel', kind: 'emulator' }, |
| 27 | + session: 'live-session', |
| 28 | + workspace: '/worktrees/live', |
| 29 | + stateDir: process.cwd(), |
| 30 | + ownerPid: owner.pid, |
| 31 | + ownerStartTime: owner.startTime, |
| 32 | + ownerToken: 'live-token', |
| 33 | + createdAtMs: 1, |
| 34 | + updatedAtMs: 1, |
| 35 | + }), |
| 36 | + ); |
| 37 | + fs.writeFileSync( |
| 38 | + path.join(claimsDir, 'stale.json'), |
| 39 | + JSON.stringify({ |
| 40 | + schemaVersion: 1, |
| 41 | + deviceKey: 'local:android:none:stale', |
| 42 | + device: { |
| 43 | + platform: 'android', |
| 44 | + id: 'stale', |
| 45 | + name: 'Stale Pixel; echo no', |
| 46 | + kind: 'emulator', |
| 47 | + }, |
| 48 | + session: 'stale-session', |
| 49 | + workspace: '/worktrees/stale', |
| 50 | + stateDir: process.cwd(), |
| 51 | + ownerPid: 999_999_999, |
| 52 | + ownerStartTime: 'old-start-time', |
| 53 | + ownerToken: 'stale-token', |
| 54 | + createdAtMs: 1, |
| 55 | + updatedAtMs: 1, |
| 56 | + }), |
| 57 | + ); |
| 58 | + |
| 59 | + const normal = await runCliCapture(['device', 'status'], { |
| 60 | + env: { AGENT_DEVICE_CLAIMS_DIR: claimsDir }, |
| 61 | + }); |
| 62 | + assert.equal(normal.code, null); |
| 63 | + assert.equal(normal.calls.length, 0); |
| 64 | + assert.match(normal.stdout, /Live Pixel: live/); |
| 65 | + assert.match( |
| 66 | + normal.stdout, |
| 67 | + /1 stale claim hidden; inspect with: agent-device device status --stale/, |
| 68 | + ); |
| 69 | + assert.doesNotMatch(normal.stdout, /Stale Pixel/); |
| 70 | + |
| 71 | + const stale = await runCliCapture(['device', 'status', '--stale', '--json'], { |
| 72 | + env: { AGENT_DEVICE_CLAIMS_DIR: claimsDir }, |
| 73 | + }); |
| 74 | + assert.equal(stale.code, null); |
| 75 | + assert.equal(stale.calls.length, 0); |
| 76 | + const payload = JSON.parse(stale.stdout); |
| 77 | + assert.equal(payload.data.claims.length, 1); |
| 78 | + assert.equal(payload.data.claims[0].device.name, 'Stale Pixel; echo no'); |
| 79 | + assert.equal(payload.data.claims[0].recovery, undefined); |
| 80 | + |
| 81 | + const scoped = await runCliCapture(['device', 'status', '--device', 'Stale Pixel; echo no'], { |
| 82 | + env: { AGENT_DEVICE_CLAIMS_DIR: claimsDir }, |
| 83 | + }); |
| 84 | + assert.match( |
| 85 | + scoped.stdout, |
| 86 | + /inspect with: agent-device device status --device 'Stale Pixel; echo no' --stale/, |
| 87 | + ); |
| 88 | + } finally { |
| 89 | + fs.rmSync(claimsDir, { recursive: true, force: true }); |
| 90 | + } |
| 91 | +}); |
| 92 | + |
| 93 | +test('keeps corrupt and state-dir-gone claims visible in normal status', async () => { |
| 94 | + const claimsDir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-cli-claims-')); |
| 95 | + const owner = readCurrentOwnerIdentity(); |
| 96 | + try { |
| 97 | + fs.writeFileSync(path.join(claimsDir, 'corrupt.json'), '{bad json'); |
| 98 | + fs.writeFileSync( |
| 99 | + path.join(claimsDir, 'state-dir-gone.json'), |
| 100 | + JSON.stringify({ |
| 101 | + schemaVersion: 1, |
| 102 | + deviceKey: 'local:android:none:state-dir-gone', |
| 103 | + device: { |
| 104 | + platform: 'android', |
| 105 | + id: 'state-dir-gone', |
| 106 | + name: 'State-dir-gone Pixel', |
| 107 | + kind: 'emulator', |
| 108 | + }, |
| 109 | + session: 'state-dir-gone-session', |
| 110 | + workspace: '/worktrees/state-dir-gone', |
| 111 | + stateDir: path.join(claimsDir, 'missing-state-dir'), |
| 112 | + ownerPid: owner.pid, |
| 113 | + ownerStartTime: owner.startTime, |
| 114 | + ownerToken: 'state-dir-gone-token', |
| 115 | + createdAtMs: 1, |
| 116 | + updatedAtMs: 1, |
| 117 | + }), |
| 118 | + ); |
| 119 | + |
| 120 | + const normal = await runCliCapture(['device', 'status'], { |
| 121 | + env: { AGENT_DEVICE_CLAIMS_DIR: claimsDir }, |
| 122 | + }); |
| 123 | + assert.match(normal.stdout, /corrupt.json: inconsistent/); |
| 124 | + assert.doesNotMatch(normal.stdout, /State-dir-gone Pixel/); |
| 125 | + assert.match(normal.stdout, /1 stale claim hidden/); |
| 126 | + |
| 127 | + const stale = await runCliCapture(['device', 'status', '--stale'], { |
| 128 | + env: { AGENT_DEVICE_CLAIMS_DIR: claimsDir }, |
| 129 | + }); |
| 130 | + assert.match(stale.stdout, /State-dir-gone Pixel: owner-state-dir-gone/); |
| 131 | + } finally { |
| 132 | + fs.rmSync(claimsDir, { recursive: true, force: true }); |
| 133 | + } |
11 | 134 | }); |
0 commit comments