block-images.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { mkdirSync, rmSync, writeFileSync } from "fs";
  2. import { tmpdir } from "os";
  3. import { join } from "path";
  4. import { afterEach, beforeEach, describe, expect, it } from "vitest";
  5. import { processFileArguments } from "../src/cli/file-processor.ts";
  6. import { SettingsManager } from "../src/core/settings-manager.ts";
  7. import { createReadTool } from "../src/core/tools/read.ts";
  8. // 1x1 red PNG image as base64 (smallest valid PNG)
  9. const TINY_PNG_BASE64 =
  10. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg==";
  11. describe("blockImages setting", () => {
  12. describe("SettingsManager", () => {
  13. it("should default blockImages to false", () => {
  14. const manager = SettingsManager.inMemory({});
  15. expect(manager.getBlockImages()).toBe(false);
  16. });
  17. it("should return true when blockImages is set to true", () => {
  18. const manager = SettingsManager.inMemory({ images: { blockImages: true } });
  19. expect(manager.getBlockImages()).toBe(true);
  20. });
  21. it("should persist blockImages setting via setBlockImages", () => {
  22. const manager = SettingsManager.inMemory({});
  23. expect(manager.getBlockImages()).toBe(false);
  24. manager.setBlockImages(true);
  25. expect(manager.getBlockImages()).toBe(true);
  26. manager.setBlockImages(false);
  27. expect(manager.getBlockImages()).toBe(false);
  28. });
  29. it("should handle blockImages alongside autoResize", () => {
  30. const manager = SettingsManager.inMemory({
  31. images: { autoResize: true, blockImages: true },
  32. });
  33. expect(manager.getImageAutoResize()).toBe(true);
  34. expect(manager.getBlockImages()).toBe(true);
  35. });
  36. });
  37. describe("Read tool", () => {
  38. let testDir: string;
  39. beforeEach(() => {
  40. testDir = join(tmpdir(), `block-images-test-${Date.now()}`);
  41. mkdirSync(testDir, { recursive: true });
  42. });
  43. afterEach(() => {
  44. rmSync(testDir, { recursive: true, force: true });
  45. });
  46. it("should always read images (filtering happens at convertToLlm layer)", async () => {
  47. // Create test image
  48. const imagePath = join(testDir, "test.png");
  49. writeFileSync(imagePath, Buffer.from(TINY_PNG_BASE64, "base64"));
  50. const tool = createReadTool(testDir);
  51. const result = await tool.execute("test-1", { path: imagePath });
  52. // Should have text note + image content
  53. expect(result.content.length).toBeGreaterThanOrEqual(1);
  54. const hasImage = result.content.some((c) => c.type === "image");
  55. expect(hasImage).toBe(true);
  56. });
  57. it("should read text files normally", async () => {
  58. // Create test text file
  59. const textPath = join(testDir, "test.txt");
  60. writeFileSync(textPath, "Hello, world!");
  61. const tool = createReadTool(testDir);
  62. const result = await tool.execute("test-2", { path: textPath });
  63. expect(result.content).toHaveLength(1);
  64. expect(result.content[0].type).toBe("text");
  65. const textContent = result.content[0] as { type: "text"; text: string };
  66. expect(textContent.text).toContain("Hello, world!");
  67. });
  68. });
  69. describe("processFileArguments", () => {
  70. let testDir: string;
  71. beforeEach(() => {
  72. testDir = join(tmpdir(), `block-images-process-test-${Date.now()}`);
  73. mkdirSync(testDir, { recursive: true });
  74. });
  75. afterEach(() => {
  76. rmSync(testDir, { recursive: true, force: true });
  77. });
  78. it("should always process images (filtering happens at convertToLlm layer)", async () => {
  79. // Create test image
  80. const imagePath = join(testDir, "test.png");
  81. writeFileSync(imagePath, Buffer.from(TINY_PNG_BASE64, "base64"));
  82. const result = await processFileArguments([imagePath]);
  83. expect(result.images).toHaveLength(1);
  84. expect(result.images[0].type).toBe("image");
  85. });
  86. it("should process text files normally", async () => {
  87. // Create test text file
  88. const textPath = join(testDir, "test.txt");
  89. writeFileSync(textPath, "Hello, world!");
  90. const result = await processFileArguments([textPath]);
  91. expect(result.images).toHaveLength(0);
  92. expect(result.text).toContain("Hello, world!");
  93. });
  94. });
  95. });