|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import * as fs from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { captionImagesWithGemini } from "./contentExtractor.js"; |
| 6 | + |
| 7 | +const hooks = vi.hoisted(() => ({ |
| 8 | + afterStat: () => {}, |
| 9 | + beforeRead: () => {}, |
| 10 | + descriptor: -1, |
| 11 | +})); |
| 12 | + |
| 13 | +vi.mock("node:fs", async (importOriginal) => { |
| 14 | + const actual = await importOriginal<typeof fs>(); |
| 15 | + return { |
| 16 | + ...actual, |
| 17 | + statSync: (path: fs.PathLike) => { |
| 18 | + const stat = actual.statSync(path); |
| 19 | + hooks.afterStat(); |
| 20 | + return stat; |
| 21 | + }, |
| 22 | + fstatSync: (fd: number) => { |
| 23 | + hooks.descriptor = fd; |
| 24 | + const stat = actual.fstatSync(fd); |
| 25 | + hooks.afterStat(); |
| 26 | + return stat; |
| 27 | + }, |
| 28 | + readFileSync: (path: fs.PathOrFileDescriptor) => { |
| 29 | + hooks.beforeRead(); |
| 30 | + return actual.readFileSync(path); |
| 31 | + }, |
| 32 | + }; |
| 33 | +}); |
| 34 | + |
| 35 | +describe("caption image file reads", () => { |
| 36 | + let dir: string; |
| 37 | + let file: string; |
| 38 | + let sentBody: string; |
| 39 | + const fetchMock = vi.fn(async (_url: string, init?: RequestInit) => { |
| 40 | + sentBody = typeof init?.body === "string" ? init.body : ""; |
| 41 | + return new Response(JSON.stringify({ choices: [{ message: { content: "A hero." } }] })); |
| 42 | + }); |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + dir = fs.mkdtempSync(join(tmpdir(), "hf-caption-read-")); |
| 46 | + fs.mkdirSync(join(dir, "assets")); |
| 47 | + file = join(dir, "assets", "hero.png"); |
| 48 | + fs.writeFileSync(file, "original image"); |
| 49 | + hooks.afterStat = () => {}; |
| 50 | + hooks.beforeRead = () => {}; |
| 51 | + hooks.descriptor = -1; |
| 52 | + sentBody = ""; |
| 53 | + fetchMock.mockClear(); |
| 54 | + vi.stubEnv("OPENROUTER_API_KEY", "test-key"); |
| 55 | + vi.stubGlobal("fetch", fetchMock); |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(() => { |
| 59 | + hooks.afterStat = () => {}; |
| 60 | + hooks.beforeRead = () => {}; |
| 61 | + vi.unstubAllGlobals(); |
| 62 | + vi.unstubAllEnvs(); |
| 63 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 64 | + }); |
| 65 | + |
| 66 | + async function expectClosed() { |
| 67 | + const actual = await vi.importActual<typeof fs>("node:fs"); |
| 68 | + expect(hooks.descriptor).toBeGreaterThanOrEqual(0); |
| 69 | + expect(() => actual.fstatSync(hooks.descriptor)).toThrow(/EBADF/); |
| 70 | + } |
| 71 | + |
| 72 | + it("reads the checked image when its directory entry is replaced", async () => { |
| 73 | + hooks.afterStat = () => { |
| 74 | + fs.renameSync(file, join(dir, "original.png")); |
| 75 | + fs.writeFileSync(file, "replacement image"); |
| 76 | + }; |
| 77 | + expect(await captionImagesWithGemini(dir, () => {}, [])).toEqual({ "hero.png": "A hero." }); |
| 78 | + expect(sentBody).toContain(Buffer.from("original image").toString("base64")); |
| 79 | + expect(sentBody).not.toContain(Buffer.from("replacement image").toString("base64")); |
| 80 | + await expectClosed(); |
| 81 | + }); |
| 82 | + |
| 83 | + it.each([4_000_000, 4_000_001])("preserves the inline size limit for %i bytes", async (size) => { |
| 84 | + fs.writeFileSync(file, Buffer.alloc(size)); |
| 85 | + const read = vi.fn(); |
| 86 | + hooks.beforeRead = read; |
| 87 | + const captions = await captionImagesWithGemini(dir, () => {}, []); |
| 88 | + expect(captions).toEqual(size === 4_000_000 ? { "hero.png": "A hero." } : {}); |
| 89 | + expect(fetchMock).toHaveBeenCalledTimes(size === 4_000_000 ? 1 : 0); |
| 90 | + expect(read).toHaveBeenCalledTimes(size === 4_000_000 ? 1 : 0); |
| 91 | + await expectClosed(); |
| 92 | + }); |
| 93 | + |
| 94 | + it.each(["stat", "read"])("closes the image and reports a failed %s", async (step) => { |
| 95 | + const fail = () => { |
| 96 | + throw new Error("Injected file failure"); |
| 97 | + }; |
| 98 | + if (step === "stat") hooks.afterStat = fail; |
| 99 | + else hooks.beforeRead = fail; |
| 100 | + const warnings: string[] = []; |
| 101 | + expect(await captionImagesWithGemini(dir, () => {}, warnings)).toEqual({}); |
| 102 | + expect(warnings).toEqual(["OpenRouter vision failed for 1 asset(s); captions omitted."]); |
| 103 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 104 | + await expectClosed(); |
| 105 | + }); |
| 106 | +}); |
0 commit comments