Skip to content

Commit 5b12609

Browse files
committed
test: preserve scoped repair evidence and restart probe cancellation
1 parent 951c22a commit 5b12609

2 files changed

Lines changed: 56 additions & 2 deletions

File tree

‎src/daemon-client/__tests__/daemon-client-abort-timeout.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* abort path's `clearTimeout` and only the abort case fails.
1212
*/
1313
import assert from 'node:assert/strict';
14+
import http from 'node:http';
15+
import { DAEMON_HTTP_INSTANCE_MISMATCH_HEADER } from '@agent-device/contracts/daemon-http';
1416
import { test, vi } from 'vitest';
1517
import { AppError, isRequestCanceledError } from '@agent-device/kernel/errors';
1618
import { getRequestSignal } from '@agent-device/host-kit/request';
@@ -20,6 +22,7 @@ import { sendRequest } from '../daemon-client-transport.ts';
2022
import { resolveDaemonPaths } from '../../daemon-resolution.ts';
2123
import {
2224
closeLoopbackServer,
25+
listenOnLoopback,
2326
skipWhenLoopbackUnavailable,
2427
trackLoopbackSockets,
2528
type SkippableTestContext,
@@ -111,3 +114,54 @@ test('an abort with a timeout armed clears the timer instead of running the time
111114
test('the same budget without a signal reaches the timeout seam', async (t) => {
112115
await runTimerDiscipline(t, 'timed-out');
113116
});
117+
118+
test('an abort during a restart health probe never retires the daemon or redispatches', async (t) => {
119+
if (await skipWhenLoopbackUnavailable(t)) return;
120+
handleRequestTimeoutCalls.length = 0;
121+
const controller = new AbortController();
122+
let rpcCount = 0;
123+
let healthCount = 0;
124+
let reachedHealth!: () => void;
125+
const healthStarted = new Promise<void>((resolve) => {
126+
reachedHealth = resolve;
127+
});
128+
const server = http.createServer((req, res) => {
129+
if (req.url === '/health') {
130+
healthCount += 1;
131+
reachedHealth();
132+
return;
133+
}
134+
rpcCount += 1;
135+
res.statusCode = 409;
136+
res.setHeader(DAEMON_HTTP_INSTANCE_MISMATCH_HEADER, 'true');
137+
res.end();
138+
});
139+
const destroySockets = trackLoopbackSockets(server);
140+
try {
141+
const port = await listenOnLoopback(server);
142+
const request = sendRequest(
143+
{
144+
baseUrl: `http://127.0.0.1:${port}`,
145+
token: 'secret',
146+
pid: 1,
147+
remoteInstanceId: 'previous-instance',
148+
},
149+
{ token: 'secret', command: 'devices', session: 'default', positionals: [], flags: {} },
150+
'auto',
151+
STATE_PATHS,
152+
1_000,
153+
{ signal: controller.signal },
154+
);
155+
const rejection = assert.rejects(request, (error: unknown) => isRequestCanceledError(error));
156+
await healthStarted;
157+
controller.abort();
158+
await rejection;
159+
assert.equal(rpcCount, 1);
160+
assert.equal(healthCount, 1);
161+
assert.deepEqual(handleRequestTimeoutCalls, []);
162+
} finally {
163+
controller.abort();
164+
destroySockets();
165+
await closeLoopbackServer(server);
166+
}
167+
});

‎src/session-repair-tombstone.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ test.each([
4444
};
4545
const { sessionsDir, tombstonePath } = writeRepairTombstone(`${JSON.stringify(tombstone)}\n`);
4646

47-
expect(readRepairTombstoneFile(tombstonePath)).toEqual(tombstone);
47+
expect(readRepairTombstoneFile(tombstonePath, 'session-a')).toEqual(tombstone);
4848
expect(findUnrecoveredRepairCommitFailure(sessionsDir)).toEqual({
4949
sessionName: 'session-a',
5050
tombstone,
@@ -61,7 +61,7 @@ test.each([
6161
const raw = rawRepairTombstone(fields);
6262
const { sessionsDir, tombstonePath } = writeRepairTombstone(raw);
6363

64-
expect(readRepairTombstoneFile(tombstonePath)).toBeUndefined();
64+
expect(readRepairTombstoneFile(tombstonePath, 'session-a')).toBeUndefined();
6565
assert.throws(
6666
() => findUnrecoveredRepairCommitFailure(sessionsDir),
6767
(error: unknown) => {

0 commit comments

Comments
 (0)