| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- import { mkdirSync, rmSync, writeFileSync } from "node:fs";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
- vi.mock("../src/utils/image-resize.js", () => {
- class MockImageProcessorUnavailableError extends Error {}
- return {
- ImageProcessorUnavailableError: MockImageProcessorUnavailableError,
- resizeImage: vi.fn(),
- formatDimensionNote: vi.fn(() => undefined),
- };
- });
- import { processFileArguments } from "../src/cli/file-processor.ts";
- import { createReadTool } from "../src/core/tools/read.ts";
- import { ImageProcessorUnavailableError, resizeImage } from "../src/utils/image-resize.ts";
- const TINY_PNG_BASE64 =
- "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg==";
- describe("image resize callers", () => {
- let testDir: string;
- beforeEach(() => {
- testDir = join(tmpdir(), `image-resize-callers-${Date.now()}`);
- mkdirSync(testDir, { recursive: true });
- vi.mocked(resizeImage).mockReset();
- vi.mocked(resizeImage).mockResolvedValue(null);
- });
- afterEach(() => {
- rmSync(testDir, { recursive: true, force: true });
- });
- it("read tool returns text-only output when auto-resize cannot produce a safe image", async () => {
- const imagePath = join(testDir, "test.png");
- writeFileSync(imagePath, Buffer.from(TINY_PNG_BASE64, "base64"));
- const tool = createReadTool(testDir);
- const result = await tool.execute("test-read-image", { path: imagePath });
- expect(result.content).toHaveLength(1);
- expect(result.content[0].type).toBe("text");
- expect((result.content[0] as { type: "text"; text: string }).text).toContain("Image omitted");
- });
- it("file processor omits image attachments when auto-resize cannot produce a safe image", async () => {
- const imagePath = join(testDir, "test.png");
- writeFileSync(imagePath, Buffer.from(TINY_PNG_BASE64, "base64"));
- const result = await processFileArguments([imagePath]);
- expect(result.images).toHaveLength(0);
- expect(result.text).toContain("Image omitted");
- });
- it("read tool reports a missing image processor separately from the inline size limit", async () => {
- const imagePath = join(testDir, "test.png");
- writeFileSync(imagePath, Buffer.from(TINY_PNG_BASE64, "base64"));
- vi.mocked(resizeImage).mockRejectedValue(new ImageProcessorUnavailableError());
- const tool = createReadTool(testDir);
- const result = await tool.execute("test-read-image", { path: imagePath });
- const output = (result.content[0] as { type: "text"; text: string }).text;
- expect(result.content).toHaveLength(1);
- expect(output).toContain("image processing runtime is unavailable");
- expect(output).not.toContain("inline image size limit");
- });
- it("file processor reports a missing image processor separately from the inline size limit", async () => {
- const imagePath = join(testDir, "test.png");
- writeFileSync(imagePath, Buffer.from(TINY_PNG_BASE64, "base64"));
- vi.mocked(resizeImage).mockRejectedValue(new ImageProcessorUnavailableError());
- const result = await processFileArguments([imagePath]);
- expect(result.images).toHaveLength(0);
- expect(result.text).toContain("image processing runtime is unavailable");
- expect(result.text).not.toContain("inline image size limit");
- });
- });
|