From d65c290748d49ab14fa8a4f537213eb75d7949bb Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Wed, 1 Apr 2026 03:20:28 +0100 Subject: [PATCH] test: merge temp download path cases --- src/plugin-sdk/temp-path.test.ts | 53 ++++++++++++++++---------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/src/plugin-sdk/temp-path.test.ts b/src/plugin-sdk/temp-path.test.ts index 4255abf67f9..cc51cc381ba 100644 --- a/src/plugin-sdk/temp-path.test.ts +++ b/src/plugin-sdk/temp-path.test.ts @@ -51,35 +51,36 @@ describe("buildRandomTempFilePath", () => { }); describe("withTempDownloadPath", () => { - it("creates a temp path under tmp dir and cleans up the temp directory", async () => { + it.each([ + { + name: "creates a temp path under tmp dir and cleans up the temp directory", + input: { prefix: "line-media" }, + expectCleanup: true, + expectedBasename: undefined, + }, + { + name: "sanitizes prefix and fileName", + input: { prefix: "../../channels/../media", fileName: "../../evil.bin" }, + expectCleanup: false, + expectedBasename: "evil.bin", + }, + ])("$name", async ({ input, expectCleanup, expectedBasename }) => { let capturedPath = ""; - await withTempDownloadPath( - { - prefix: "line-media", - }, - async (tmpPath) => { - capturedPath = tmpPath; + await withTempDownloadPath(input, async (tmpPath) => { + capturedPath = tmpPath; + if (expectCleanup) { await fs.writeFile(tmpPath, "ok"); - }, - ); - - expect(capturedPath).toContain(path.join(resolvePreferredOpenClawTmpDir(), "line-media-")); - await expect(fs.stat(capturedPath)).rejects.toMatchObject({ code: "ENOENT" }); - }); - - it("sanitizes prefix and fileName", async () => { - let capturedPath = ""; - await withTempDownloadPath( - { - prefix: "../../channels/../media", - fileName: "../../evil.bin", - }, - async (tmpPath) => { - capturedPath = tmpPath; - }, - ); + } + }); expectPathInsideTmpRoot(capturedPath); - expect(path.basename(capturedPath)).toBe("evil.bin"); + if (expectedBasename) { + expect(path.basename(capturedPath)).toBe(expectedBasename); + } else { + expect(capturedPath).toContain(path.join(resolvePreferredOpenClawTmpDir(), "line-media-")); + } + if (expectCleanup) { + await expect(fs.stat(capturedPath)).rejects.toMatchObject({ code: "ENOENT" }); + } }); });