Skip to content

Commit 5c9cbcb

Browse files
committed
test(remote): fail directory downloads with a malformed archive so staging cleanup is real
Review finding: a non-2xx response rejects before materialization, so the hidden staging directory is never created and its removal is untested. Serve 200 + a non-gzip body instead, which creates staging, fails extraction inside it, and proves the finally-block cleanup.
1 parent b018181 commit 5c9cbcb

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

‎src/remote/__tests__/artifact-download.test.ts‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,19 @@ test('downloadRemoteArtifactFromUrl times out before response headers arrive wit
121121
}
122122
});
123123

124-
test('downloadRemoteArtifactFromUrl leaves a caller-owned directory root untouched when the download fails', async (t) => {
125-
if (await skipWhenLoopbackUnavailable(t, 'artifact download directory-cleanup coverage')) return;
126-
const tempRoot = mkdtempForTestSync('agent-device-artifact-directory-error-');
124+
test('downloadRemoteArtifactFromUrl removes directory staging when a 2xx body fails extraction', async (t) => {
125+
if (await skipWhenLoopbackUnavailable(t, 'artifact download directory-staging coverage')) return;
126+
const tempRoot = mkdtempForTestSync('agent-device-artifact-directory-staging-');
127127
const artifactsRoot = path.join(tempRoot, 'remote-device-artifacts');
128128
fs.mkdirSync(artifactsRoot, { recursive: true });
129129
fs.writeFileSync(path.join(artifactsRoot, 'previous-run-marker.txt'), 'keep me');
130+
// A 200 with a body that is not a gzip archive: materialization starts, staging is created,
131+
// and extraction fails inside it — the path where the hidden staging directory really exists
132+
// and must be removed while the caller-owned root stays untouched.
130133
const server = http.createServer((_req, res) => {
131-
res.statusCode = 500;
132-
res.end('daemon exploded');
134+
res.statusCode = 200;
135+
res.setHeader('content-type', 'application/gzip');
136+
res.end('this is not a gzip archive');
133137
});
134138
const port = await listenOnLoopback(server);
135139

@@ -141,7 +145,10 @@ test('downloadRemoteArtifactFromUrl leaves a caller-owned directory root untouch
141145
isDirectory: true,
142146
}),
143147
(error: unknown) => {
144-
assert.match(String((error as Error).message), /Failed to download remote artifact/);
148+
assert.equal(error instanceof Error, true);
149+
// The extraction failure surfaces as-is (not the HTTP-error AppError): the status was
150+
// already 2xx, so the only defensible description left is the materializer's own error.
151+
assert.doesNotMatch(String((error as Error).message), /Failed to download remote artifact/);
145152
return true;
146153
},
147154
);
@@ -152,7 +159,7 @@ test('downloadRemoteArtifactFromUrl leaves a caller-owned directory root untouch
152159
assert.deepEqual(
153160
fs.readdirSync(artifactsRoot),
154161
['previous-run-marker.txt'],
155-
'a failed directory download must not leave staging behind',
162+
'failed extraction must remove the hidden staging directory it created',
156163
);
157164
} finally {
158165
await closeLoopbackServer(server);

0 commit comments

Comments
 (0)