test(cli): dedupe camera fetch stubs and cover empty-body download rejection

This commit is contained in:
Peter Steinberger
2026-02-21 19:36:52 +00:00
parent 31a0449f69
commit bdfb979940

View File

@@ -22,6 +22,13 @@ async function withTempDir<T>(prefix: string, run: (dir: string) => Promise<T>):
} }
describe("nodes camera helpers", () => { describe("nodes camera helpers", () => {
function stubFetchResponse(response: Response) {
vi.stubGlobal(
"fetch",
vi.fn(async () => response),
);
}
it("parses camera.snap payload", () => { it("parses camera.snap payload", () => {
expect( expect(
parseCameraSnapPayload({ parseCameraSnapPayload({
@@ -97,10 +104,7 @@ describe("nodes camera helpers", () => {
}); });
it("writes url payload to file", async () => { it("writes url payload to file", async () => {
vi.stubGlobal( stubFetchResponse(new Response("url-content", { status: 200 }));
"fetch",
vi.fn(async () => new Response("url-content", { status: 200 })),
);
await withTempDir("openclaw-test-", async (dir) => { await withTempDir("openclaw-test-", async (dir) => {
const out = path.join(dir, "x.bin"); const out = path.join(dir, "x.bin");
await writeUrlToFile(out, "https://example.com/clip.mp4"); await writeUrlToFile(out, "https://example.com/clip.mp4");
@@ -115,15 +119,11 @@ describe("nodes camera helpers", () => {
}); });
it("rejects oversized content-length for url payload", async () => { it("rejects oversized content-length for url payload", async () => {
vi.stubGlobal( stubFetchResponse(
"fetch",
vi.fn(
async () =>
new Response("tiny", { new Response("tiny", {
status: 200, status: 200,
headers: { "content-length": String(999_999_999) }, headers: { "content-length": String(999_999_999) },
}), }),
),
); );
await expect(writeUrlToFile("/tmp/ignored", "https://example.com/huge.bin")).rejects.toThrow( await expect(writeUrlToFile("/tmp/ignored", "https://example.com/huge.bin")).rejects.toThrow(
/exceeds max/i, /exceeds max/i,
@@ -131,14 +131,18 @@ describe("nodes camera helpers", () => {
}); });
it("rejects non-ok https url payload responses", async () => { it("rejects non-ok https url payload responses", async () => {
vi.stubGlobal( stubFetchResponse(new Response("down", { status: 503, statusText: "Service Unavailable" }));
"fetch",
vi.fn(async () => new Response("down", { status: 503, statusText: "Service Unavailable" })),
);
await expect(writeUrlToFile("/tmp/ignored", "https://example.com/down.bin")).rejects.toThrow( await expect(writeUrlToFile("/tmp/ignored", "https://example.com/down.bin")).rejects.toThrow(
/503/i, /503/i,
); );
}); });
it("rejects empty https response body", async () => {
stubFetchResponse(new Response(null, { status: 200 }));
await expect(writeUrlToFile("/tmp/ignored", "https://example.com/empty.bin")).rejects.toThrow(
/empty response body/i,
);
});
}); });
describe("nodes screen helpers", () => { describe("nodes screen helpers", () => {