-
-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathcli-device-status.test.ts
More file actions
172 lines (160 loc) · 6.11 KB
/
Copy pathcli-device-status.test.ts
File metadata and controls
172 lines (160 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { test, vi } from 'vitest';
import { readCurrentOwnerIdentity } from '../utils/owner-identity.ts';
import { runCliCapture } from './cli-capture.ts';
import { mkdtempForTestSync } from './test-utils/tmp-dir.ts';
vi.mock('../utils/host-process.ts', async (importOriginal) =>
(await import('./test-utils/host-process-mock.ts')).pinOwnProcessStartTime(importOriginal),
);
test('device status is daemonless and does not send a daemon request', async () => {
const result = await runCliCapture(['device', 'status', '--json']);
assert.equal(result.code, null);
assert.equal(result.calls.length, 0);
const payload = JSON.parse(result.stdout);
assert.deepEqual(payload, { success: true, data: { claims: [], hiddenStaleClaims: 0 } });
});
test('keeps normal status compact while retaining proven-stale claims for explicit inspection', async () => {
const claimsDir = mkdtempForTestSync('agent-device-cli-claims-');
const owner = readCurrentOwnerIdentity();
try {
fs.writeFileSync(
path.join(claimsDir, 'live.json'),
JSON.stringify({
schemaVersion: 1,
deviceKey: 'local:android:none:live',
device: { platform: 'android', id: 'live', name: 'Live Pixel', kind: 'emulator' },
session: 'live-session',
workspace: '/worktrees/live',
stateDir: process.cwd(),
ownerPid: owner.pid,
ownerStartTime: owner.startTime,
ownerToken: 'live-token',
createdAtMs: 1,
updatedAtMs: 1,
}),
);
fs.writeFileSync(
path.join(claimsDir, 'stale.json'),
JSON.stringify({
schemaVersion: 1,
deviceKey: 'local:android:none:stale',
device: {
platform: 'android',
id: 'stale',
name: 'Stale Pixel; echo no',
kind: 'emulator',
},
session: 'stale-session',
workspace: '/worktrees/stale',
stateDir: process.cwd(),
ownerPid: 999_999_999,
ownerStartTime: 'old-start-time',
ownerToken: 'stale-token',
createdAtMs: 1,
updatedAtMs: 1,
}),
);
const normal = await runCliCapture(['device', 'status'], {
env: { AGENT_DEVICE_CLAIMS_DIR: claimsDir },
});
assert.equal(normal.code, null);
assert.equal(normal.calls.length, 0);
assert.match(normal.stdout, /Live Pixel: live/);
assert.match(
normal.stdout,
/1 stale claim hidden; inspect with: agent-device device status --stale/,
);
assert.doesNotMatch(normal.stdout, /Stale Pixel/);
const stale = await runCliCapture(['device', 'status', '--stale', '--json'], {
env: { AGENT_DEVICE_CLAIMS_DIR: claimsDir },
});
assert.equal(stale.code, null);
assert.equal(stale.calls.length, 0);
const payload = JSON.parse(stale.stdout);
assert.equal(payload.data.claims.length, 1);
assert.equal(payload.data.claims[0].device.name, 'Stale Pixel; echo no');
assert.equal(payload.data.claims[0].recovery, undefined);
const scoped = await runCliCapture(['device', 'status', '--device', 'Stale Pixel; echo no'], {
env: { AGENT_DEVICE_CLAIMS_DIR: claimsDir },
});
assert.match(
scoped.stdout,
/inspect with: agent-device device status --device 'Stale Pixel; echo no' --stale/,
);
} finally {
fs.rmSync(claimsDir, { recursive: true, force: true });
}
});
test('keeps corrupt and state-dir-gone claims visible in normal status', async () => {
const claimsDir = mkdtempForTestSync('agent-device-cli-claims-');
const owner = readCurrentOwnerIdentity();
try {
fs.writeFileSync(path.join(claimsDir, 'corrupt.json'), '{bad json');
fs.writeFileSync(
path.join(claimsDir, 'state-dir-gone.json'),
JSON.stringify({
schemaVersion: 1,
deviceKey: 'local:android:none:state-dir-gone',
device: {
platform: 'android',
id: 'state-dir-gone',
name: 'State-dir-gone Pixel',
kind: 'emulator',
},
session: 'state-dir-gone-session',
workspace: '/worktrees/state-dir-gone',
stateDir: path.join(claimsDir, 'missing-state-dir'),
ownerPid: owner.pid,
ownerStartTime: owner.startTime,
ownerToken: 'state-dir-gone-token',
createdAtMs: 1,
updatedAtMs: 1,
}),
);
const normal = await runCliCapture(['device', 'status'], {
env: { AGENT_DEVICE_CLAIMS_DIR: claimsDir },
});
assert.match(normal.stdout, /corrupt.json: inconsistent/);
assert.doesNotMatch(normal.stdout, /State-dir-gone Pixel/);
assert.match(normal.stdout, /1 stale claim hidden/);
const stale = await runCliCapture(['device', 'status', '--stale'], {
env: { AGENT_DEVICE_CLAIMS_DIR: claimsDir },
});
assert.match(stale.stdout, /State-dir-gone Pixel: owner-state-dir-gone/);
} finally {
fs.rmSync(claimsDir, { recursive: true, force: true });
}
});
test('states that nothing is claimed when every claim is stale', async () => {
// Regression: the all-stale case rendered only the hidden-claim notice, so a
// user asking what holds a device saw a maintenance warning and no answer.
const claimsDir = mkdtempForTestSync('agent-device-cli-claims-');
try {
fs.writeFileSync(
path.join(claimsDir, 'stale.json'),
JSON.stringify({
schemaVersion: 1,
deviceKey: 'local:apple:ios:stale-phone',
device: { platform: 'ios', id: 'stale-phone', name: 'Dead iPhone', kind: 'device' },
session: 'dead-session',
workspace: '/worktrees/dead',
stateDir: process.cwd(),
ownerPid: 999_999_999,
ownerStartTime: 'old-start-time',
ownerToken: 'stale-token',
createdAtMs: 1,
updatedAtMs: 1,
}),
);
const result = await runCliCapture(['device', 'status'], {
env: { AGENT_DEVICE_CLAIMS_DIR: claimsDir },
});
assert.match(result.stdout, /No live local advisory device claims found\./);
assert.match(result.stdout, /1 stale claim hidden/);
assert.doesNotMatch(result.stdout, /Dead iPhone/);
} finally {
fs.rmSync(claimsDir, { recursive: true, force: true });
}
});