|
1 | | -import { describe, it, expect, vi, beforeEach, afterEach } from "vitest" |
2 | | -import { normalizeImageRefsToDataUrls, normalizeDataUrlsToFilePaths } from "../imageDataUrl" |
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest" |
| 2 | +import { normalizeImageRefsToDataUrls } from "../imageDataUrl" |
3 | 3 | import * as fs from "fs/promises" |
4 | 4 | import * as path from "path" |
5 | | -import * as os from "os" |
6 | 5 |
|
7 | 6 | // Mock fs module |
8 | 7 | vi.mock("fs/promises") |
@@ -64,97 +63,3 @@ describe("normalizeImageRefsToDataUrls", () => { |
64 | 63 | expect(result).toEqual([]) |
65 | 64 | }) |
66 | 65 | }) |
67 | | - |
68 | | -describe("normalizeDataUrlsToFilePaths", () => { |
69 | | - const testGlobalStoragePath = path.join(os.tmpdir(), "test-roo-code-storage") |
70 | | - |
71 | | - beforeEach(async () => { |
72 | | - vi.clearAllMocks() |
73 | | - // Mock mkdir to succeed |
74 | | - vi.mocked(fs.mkdir).mockResolvedValue(undefined) |
75 | | - // Mock writeFile to succeed |
76 | | - vi.mocked(fs.writeFile).mockResolvedValue(undefined) |
77 | | - // Mock access to fail initially (file doesn't exist) |
78 | | - vi.mocked(fs.access).mockRejectedValue(new Error("File not found")) |
79 | | - }) |
80 | | - |
81 | | - afterEach(() => { |
82 | | - vi.clearAllMocks() |
83 | | - }) |
84 | | - |
85 | | - it("should pass through non-data URLs unchanged", async () => { |
86 | | - const filePath = "/path/to/image.png" |
87 | | - const result = await normalizeDataUrlsToFilePaths([filePath], testGlobalStoragePath) |
88 | | - |
89 | | - expect(result).toEqual([filePath]) |
90 | | - expect(fs.writeFile).not.toHaveBeenCalled() |
91 | | - }) |
92 | | - |
93 | | - it("should convert data URLs to file paths", async () => { |
94 | | - const dataUrl = |
95 | | - "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==" |
96 | | - |
97 | | - const result = await normalizeDataUrlsToFilePaths([dataUrl], testGlobalStoragePath) |
98 | | - |
99 | | - expect(result).toHaveLength(1) |
100 | | - expect(result[0]).toMatch(/temp-images/) |
101 | | - expect(result[0]).toMatch(/\.png$/) |
102 | | - expect(fs.mkdir).toHaveBeenCalledWith( |
103 | | - expect.stringContaining("temp-images"), |
104 | | - expect.objectContaining({ recursive: true }), |
105 | | - ) |
106 | | - expect(fs.writeFile).toHaveBeenCalledWith(expect.stringMatching(/temp-images.*\.png$/), expect.any(Buffer)) |
107 | | - }) |
108 | | - |
109 | | - it("should handle mixed arrays of data URLs and file paths", async () => { |
110 | | - const dataUrl = "data:image/jpeg;base64,test123" |
111 | | - const filePath = "/existing/image.png" |
112 | | - |
113 | | - const result = await normalizeDataUrlsToFilePaths([filePath, dataUrl], testGlobalStoragePath) |
114 | | - |
115 | | - expect(result).toHaveLength(2) |
116 | | - expect(result[0]).toBe(filePath) // File path unchanged |
117 | | - expect(result[1]).toMatch(/temp-images/) // Data URL converted |
118 | | - expect(result[1]).toMatch(/\.jpeg$/) // Correct extension from MIME type |
119 | | - }) |
120 | | - |
121 | | - it("should handle errors gracefully", async () => { |
122 | | - const dataUrl = "data:image/png;base64,test" |
123 | | - |
124 | | - // Mock writeFile to fail |
125 | | - vi.mocked(fs.writeFile).mockRejectedValue(new Error("Write failed")) |
126 | | - |
127 | | - const result = await normalizeDataUrlsToFilePaths([dataUrl], testGlobalStoragePath) |
128 | | - |
129 | | - // Should return original data URL as fallback |
130 | | - expect(result).toEqual([dataUrl]) |
131 | | - }) |
132 | | - |
133 | | - it("should handle invalid data URL format", async () => { |
134 | | - const invalidDataUrl = "data:image/png" // Missing base64 data |
135 | | - |
136 | | - const result = await normalizeDataUrlsToFilePaths([invalidDataUrl], testGlobalStoragePath) |
137 | | - |
138 | | - // Should skip invalid URLs |
139 | | - expect(result).toHaveLength(0) |
140 | | - }) |
141 | | - |
142 | | - it("should handle empty arrays", async () => { |
143 | | - const result = await normalizeDataUrlsToFilePaths([], testGlobalStoragePath) |
144 | | - expect(result).toEqual([]) |
145 | | - }) |
146 | | - |
147 | | - it("should extract correct file extensions from MIME types", async () => { |
148 | | - const testCases = [ |
149 | | - { dataUrl: "data:image/png;base64,test", expectedExt: ".png" }, |
150 | | - { dataUrl: "data:image/jpeg;base64,test", expectedExt: ".jpeg" }, |
151 | | - { dataUrl: "data:image/gif;base64,test", expectedExt: ".gif" }, |
152 | | - { dataUrl: "data:image/webp;base64,test", expectedExt: ".webp" }, |
153 | | - ] |
154 | | - |
155 | | - for (const { dataUrl, expectedExt } of testCases) { |
156 | | - const result = await normalizeDataUrlsToFilePaths([dataUrl], testGlobalStoragePath) |
157 | | - expect(result[0]).toMatch(new RegExp(`${expectedExt.replace(".", "\\.")}$`)) |
158 | | - } |
159 | | - }) |
160 | | -}) |
0 commit comments