Repository navigation
Expand file tree
/
Copy pathdaemon-stop.test.ts
More file actions
177 lines (160 loc) · 5.94 KB
/
Copy pathdaemon-stop.test.ts
File metadata and controls
177 lines (160 loc) · 5.94 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
173
174
175
176
177
import fs from 'node:fs';
import { afterEach, expect, test, vi } from 'vitest';
import { mkdtempForTestSync } from '../../__tests__/test-utils/tmp-dir.ts';
const mocks = vi.hoisted(() => ({ stopAndRetireDaemon: vi.fn() }));
vi.mock('../../daemon-registration-owner.ts', () => ({
stopAndRetireDaemon: mocks.stopAndRetireDaemon,
}));
import { resolveDaemonPaths } from '../../daemon-resolution.ts';
import { stopDaemon } from '../daemon-stop.ts';
afterEach(() => {
vi.resetAllMocks();
});
function createDaemonPaths(): ReturnType<typeof resolveDaemonPaths> {
const stateDir = mkdtempForTestSync('agent-device-daemon-stop-');
const paths = resolveDaemonPaths(stateDir);
fs.mkdirSync(paths.baseDir, { recursive: true });
fs.writeFileSync(paths.infoPath, JSON.stringify({ pid: 123, processStartTime: 'start-time' }));
return paths;
}
function removeDaemonPaths(paths: ReturnType<typeof resolveDaemonPaths>): void {
fs.rmSync(paths.baseDir, { recursive: true, force: true });
}
test('reports not-running when daemon metadata is absent', async () => {
const paths = resolveDaemonPaths(mkdtempForTestSync('agent-device-daemon-stop-'));
try {
const result = await stopDaemon({ paths });
expect(result).toMatchObject({ stopped: false, mode: 'not-running' });
} finally {
removeDaemonPaths(paths);
}
});
test('retained identity verification is reported as failure without known cleanup', async () => {
const paths = createDaemonPaths();
mocks.stopAndRetireDaemon.mockResolvedValue(retainedExit('identity-unverified'));
await expect(stopDaemon({ paths })).rejects.toMatchObject({
code: 'COMMAND_FAILED',
details: { reason: 'daemon_exit_unconfirmed', terminationReason: 'identity-unverified' },
});
expect(mocks.stopAndRetireDaemon).toHaveBeenCalledWith({
paths,
observed: { pid: 123, startTime: 'start-time' },
mode: 'graceful',
termTimeoutMs: 10_000,
killTimeoutMs: 2_000,
});
});
test('missing start-time identity is passed to the owning termination operation', async () => {
const paths = createDaemonPaths();
fs.writeFileSync(paths.infoPath, JSON.stringify({ pid: 123, processStartTime: ' ' }));
mocks.stopAndRetireDaemon.mockResolvedValue(retainedExit('missing-start-time'));
await expect(stopDaemon({ paths })).rejects.toMatchObject({
details: { terminationReason: 'missing-start-time' },
});
expect(mocks.stopAndRetireDaemon).toHaveBeenCalledWith(
expect.objectContaining({ paths, observed: { pid: 123, startTime: null } }),
);
});
test('a previously exited verified lifetime is reported as not-running', async () => {
mocks.stopAndRetireDaemon.mockResolvedValue(retired('already-exited'));
expect(await stopDaemon({ paths: createDaemonPaths() })).toMatchObject({
stopped: false,
mode: 'not-running',
});
});
test('an already released pid without start time remains not-running without cleanup proof', async () => {
const paths = createDaemonPaths();
fs.writeFileSync(paths.infoPath, JSON.stringify({ pid: 123 }));
mocks.stopAndRetireDaemon.mockResolvedValue({
status: 'retained',
reason: 'exit-unconfirmed',
removedInfo: false,
termination: { status: 'not-running' },
});
expect(await stopDaemon({ paths })).toMatchObject({
stopped: false,
mode: 'not-running',
});
});
test('confirmed TERM exit preserves graceful report behavior and configured budgets', async () => {
const paths = createDaemonPaths();
mocks.stopAndRetireDaemon.mockResolvedValue(retired('graceful'));
expect(await stopDaemon({ paths, graceTimeoutMs: 11, killTimeoutMs: 7 })).toMatchObject({
stopped: true,
mode: 'graceful',
cleanupConfidence: 'known',
providerReleases: { pending: [] },
});
expect(mocks.stopAndRetireDaemon).toHaveBeenCalledWith({
paths,
observed: { pid: 123, startTime: 'start-time' },
mode: 'graceful',
termTimeoutMs: 11,
killTimeoutMs: 7,
});
});
test('confirmed KILL exit preserves unknown provider cleanup', async () => {
mocks.stopAndRetireDaemon.mockResolvedValue(retired('forced'));
expect(await stopDaemon({ paths: createDaemonPaths() })).toMatchObject({
stopped: true,
mode: 'forced',
cleanupConfidence: 'unknown',
providerReleases: { status: 'unknown', pending: null },
warnings: [expect.stringContaining('force-killed')],
});
});
test.each(['signal-failed', 'exit-timeout'])(
'%s cannot become a successful stop',
async (reason) => {
mocks.stopAndRetireDaemon.mockResolvedValue(retainedExit(reason));
await expect(stopDaemon({ paths: createDaemonPaths() })).rejects.toMatchObject({
code: 'COMMAND_FAILED',
details: { reason: 'daemon_exit_unconfirmed', terminationReason: reason },
});
},
);
function retainedExit(reason: string) {
return {
status: 'retained',
reason: 'exit-unconfirmed',
removedInfo: false,
termination: { status: 'retained', reason },
};
}
function retired(mode: string) {
return {
status: 'retired',
removedInfo: true,
termination: { status: 'exited', mode, identity: { pid: 123, startTime: 'start-time' } },
};
}
test.each(['registration-replaced', 'retirement-unconfirmed'])(
'%s after confirmed exit cannot report a completed retirement',
async (reason) => {
const paths = createDaemonPaths();
mocks.stopAndRetireDaemon.mockResolvedValue({
...retired('forced'),
status: 'retained',
reason,
removedInfo: false,
error: {
code: 'UNKNOWN',
message: 'retained',
hint: 'Inspect retained state.',
diagnosticId: 'diag-retire',
logPath: '/retained/daemon.log',
},
});
await expect(stopDaemon({ paths })).rejects.toMatchObject({
code: 'COMMAND_FAILED',
details: {
reason: 'daemon_retirement_unconfirmed',
retirement: { status: 'retained', reason },
hint: 'Inspect retained state.',
diagnosticId: 'diag-retire',
logPath: '/retained/daemon.log',
},
});
expect(fs.existsSync(paths.infoPath)).toBe(true);
},
);