|
| 1 | +import { afterEach, beforeEach, expect, test, vi } from "vite-plus/test"; |
| 2 | +import { |
| 3 | + chmodSync, |
| 4 | + mkdtempSync, |
| 5 | + readFileSync, |
| 6 | + readdirSync, |
| 7 | + rmSync, |
| 8 | + statSync, |
| 9 | + writeFileSync, |
| 10 | +} from "node:fs"; |
| 11 | +import { tmpdir } from "node:os"; |
| 12 | +import { join } from "node:path"; |
| 13 | +import { httpTransport } from "bailian-cli-core"; |
| 14 | +import { downloadFile } from "../src/utils/download.ts"; |
| 15 | + |
| 16 | +// Publish-path failures (thrown after the body is fully downloaded) must never |
| 17 | +// lose the previous destination bytes. renameSync/copyFileSync errnos are |
| 18 | +// injected without touching real permissions: `renameCodes` is consumed in call |
| 19 | +// order, null lets the real rename run. |
| 20 | +const injection = vi.hoisted(() => ({ |
| 21 | + renameCodes: [] as Array<string | null>, |
| 22 | + copyErrorCode: undefined as string | undefined, |
| 23 | +})); |
| 24 | + |
| 25 | +vi.mock("fs", async (importOriginal) => { |
| 26 | + const actual = await importOriginal<typeof import("node:fs")>(); |
| 27 | + const fail = (code: string, action: string): never => { |
| 28 | + const error = new Error(`injected ${code}: ${action}`) as NodeJS.ErrnoException; |
| 29 | + error.code = code; |
| 30 | + throw error; |
| 31 | + }; |
| 32 | + return { |
| 33 | + ...actual, |
| 34 | + renameSync: ((from: string, to: string) => { |
| 35 | + const code = injection.renameCodes.shift(); |
| 36 | + if (code !== undefined && code !== null) fail(code, `rename ${from} -> ${to}`); |
| 37 | + return actual.renameSync(from, to); |
| 38 | + }) as typeof actual.renameSync, |
| 39 | + copyFileSync: ((from: string, to: string) => { |
| 40 | + if (injection.copyErrorCode !== undefined) { |
| 41 | + fail(injection.copyErrorCode, `copy ${from} -> ${to}`); |
| 42 | + } |
| 43 | + return actual.copyFileSync(from, to); |
| 44 | + }) as typeof actual.copyFileSync, |
| 45 | + }; |
| 46 | +}); |
| 47 | + |
| 48 | +function serveBody(text: string): void { |
| 49 | + vi.spyOn(httpTransport, "fetch").mockResolvedValue( |
| 50 | + new Response( |
| 51 | + new ReadableStream({ |
| 52 | + start(controller) { |
| 53 | + controller.enqueue(new TextEncoder().encode(text)); |
| 54 | + controller.close(); |
| 55 | + }, |
| 56 | + }), |
| 57 | + { headers: { "content-length": String(text.length) } }, |
| 58 | + ), |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +let directory = ""; |
| 63 | +let destination = ""; |
| 64 | + |
| 65 | +beforeEach(() => { |
| 66 | + injection.renameCodes = []; |
| 67 | + injection.copyErrorCode = undefined; |
| 68 | + directory = mkdtempSync(join(tmpdir(), "bl-download-publish-")); |
| 69 | + destination = join(directory, "take.wav"); |
| 70 | + writeFileSync(destination, "original-audio", { mode: 0o600 }); |
| 71 | + chmodSync(destination, 0o600); |
| 72 | +}); |
| 73 | + |
| 74 | +afterEach(() => { |
| 75 | + vi.restoreAllMocks(); |
| 76 | + rmSync(directory, { recursive: true, force: true }); |
| 77 | +}); |
| 78 | + |
| 79 | +test("backup rename EPERM aborts publish with the destination untouched", async () => { |
| 80 | + serveBody("new-audio"); |
| 81 | + injection.renameCodes = ["EPERM", "EIO"]; |
| 82 | + await expect( |
| 83 | + downloadFile("https://example.invalid/file", destination, { quiet: true }), |
| 84 | + ).rejects.toThrow("injected EPERM"); |
| 85 | + expect(readFileSync(destination, "utf-8")).toBe("original-audio"); |
| 86 | + expect(statSync(destination).mode & 0o777).toBe(0o600); |
| 87 | + expect(readdirSync(directory)).toEqual(["take.wav"]); |
| 88 | +}); |
| 89 | + |
| 90 | +test("publish rename EPERM then copy EIO restores the previous file", async () => { |
| 91 | + serveBody("new-audio"); |
| 92 | + injection.renameCodes = [null, "EPERM"]; |
| 93 | + injection.copyErrorCode = "EIO"; |
| 94 | + await expect( |
| 95 | + downloadFile("https://example.invalid/file", destination, { quiet: true }), |
| 96 | + ).rejects.toThrow("injected EIO"); |
| 97 | + expect(readFileSync(destination, "utf-8")).toBe("original-audio"); |
| 98 | + expect(statSync(destination).mode & 0o777).toBe(0o600); |
| 99 | + expect(readdirSync(directory)).toEqual(["take.wav"]); |
| 100 | +}); |
| 101 | + |
| 102 | +test("publish rename EIO without fallback restores the previous file", async () => { |
| 103 | + serveBody("new-audio"); |
| 104 | + injection.renameCodes = [null, "EIO"]; |
| 105 | + await expect( |
| 106 | + downloadFile("https://example.invalid/file", destination, { quiet: true }), |
| 107 | + ).rejects.toThrow("injected EIO"); |
| 108 | + expect(readFileSync(destination, "utf-8")).toBe("original-audio"); |
| 109 | + expect(readdirSync(directory)).toEqual(["take.wav"]); |
| 110 | +}); |
| 111 | + |
| 112 | +test("publish rename EPERM falls back to copy and keeps the previous mode", async () => { |
| 113 | + serveBody("new-audio"); |
| 114 | + injection.renameCodes = [null, "EPERM"]; |
| 115 | + expect(await downloadFile("https://example.invalid/file", destination, { quiet: true })).toEqual({ |
| 116 | + size: 9, |
| 117 | + }); |
| 118 | + expect(readFileSync(destination, "utf-8")).toBe("new-audio"); |
| 119 | + expect(statSync(destination).mode & 0o777).toBe(0o600); |
| 120 | + expect(readdirSync(directory)).toEqual(["take.wav"]); |
| 121 | +}); |
0 commit comments