image-resize-callers.test.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { mkdirSync, rmSync, writeFileSync } from "node:fs";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  5. vi.mock("../src/utils/image-resize.js", () => {
  6. class MockImageProcessorUnavailableError extends Error {}
  7. return {
  8. ImageProcessorUnavailableError: MockImageProcessorUnavailableError,
  9. resizeImage: vi.fn(),
  10. formatDimensionNote: vi.fn(() => undefined),
  11. };
  12. });
  13. import { processFileArguments } from "../src/cli/file-processor.ts";
  14. import { createReadTool } from "../src/core/tools/read.ts";
  15. import { ImageProcessorUnavailableError, resizeImage } from "../src/utils/image-resize.ts";
  16. const TINY_PNG_BASE64 =
  17. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg==";
  18. describe("image resize callers", () => {
  19. let testDir: string;
  20. beforeEach(() => {
  21. testDir = join(tmpdir(), `image-resize-callers-${Date.now()}`);
  22. mkdirSync(testDir, { recursive: true });
  23. vi.mocked(resizeImage).mockReset();
  24. vi.mocked(resizeImage).mockResolvedValue(null);
  25. });
  26. afterEach(() => {
  27. rmSync(testDir, { recursive: true, force: true });
  28. });
  29. it("read tool returns text-only output when auto-resize cannot produce a safe image", async () => {
  30. const imagePath = join(testDir, "test.png");
  31. writeFileSync(imagePath, Buffer.from(TINY_PNG_BASE64, "base64"));
  32. const tool = createReadTool(testDir);
  33. const result = await tool.execute("test-read-image", { path: imagePath });
  34. expect(result.content).toHaveLength(1);
  35. expect(result.content[0].type).toBe("text");
  36. expect((result.content[0] as { type: "text"; text: string }).text).toContain("Image omitted");
  37. });
  38. it("file processor omits image attachments when auto-resize cannot produce a safe image", async () => {
  39. const imagePath = join(testDir, "test.png");
  40. writeFileSync(imagePath, Buffer.from(TINY_PNG_BASE64, "base64"));
  41. const result = await processFileArguments([imagePath]);
  42. expect(result.images).toHaveLength(0);
  43. expect(result.text).toContain("Image omitted");
  44. });
  45. it("read tool reports a missing image processor separately from the inline size limit", async () => {
  46. const imagePath = join(testDir, "test.png");
  47. writeFileSync(imagePath, Buffer.from(TINY_PNG_BASE64, "base64"));
  48. vi.mocked(resizeImage).mockRejectedValue(new ImageProcessorUnavailableError());
  49. const tool = createReadTool(testDir);
  50. const result = await tool.execute("test-read-image", { path: imagePath });
  51. const output = (result.content[0] as { type: "text"; text: string }).text;
  52. expect(result.content).toHaveLength(1);
  53. expect(output).toContain("image processing runtime is unavailable");
  54. expect(output).not.toContain("inline image size limit");
  55. });
  56. it("file processor reports a missing image processor separately from the inline size limit", async () => {
  57. const imagePath = join(testDir, "test.png");
  58. writeFileSync(imagePath, Buffer.from(TINY_PNG_BASE64, "base64"));
  59. vi.mocked(resizeImage).mockRejectedValue(new ImageProcessorUnavailableError());
  60. const result = await processFileArguments([imagePath]);
  61. expect(result.images).toHaveLength(0);
  62. expect(result.text).toContain("image processing runtime is unavailable");
  63. expect(result.text).not.toContain("inline image size limit");
  64. });
  65. });