| 1 | import * as path from "path"; |
| 2 | |
| 3 | import { describe, expect, it, vi } from "vitest"; |
| 4 | |
| 5 | // Test the missing file handling logic directly |
| 6 | describe("UserInput - Missing File Handling", () => { |
| 7 | it("should handle selecting non-existent files gracefully", async () => { |
| 8 | const mockOnFileAttached = vi.fn(); |
| 9 | |
| 10 | // Simulate the selectFile logic for a missing file |
| 11 | const filePath = "missing-file.txt"; |
| 12 | |
| 13 | try { |
| 14 | const fs = await import("fs/promises"); |
| 15 | const path = await import("path"); |
| 16 | const absolutePath = path.resolve(filePath); |
| 17 | const content = await fs.readFile(absolutePath, "utf-8"); |
| 18 | mockOnFileAttached(absolutePath, content); |
| 19 | } catch (error) { |
| 20 | // If file doesn't exist, just attach the filename without content |
| 21 | if (error instanceof Error && (error as any).code === "ENOENT") { |
| 22 | const path = await import("path"); |
| 23 | const absolutePath = path.resolve(filePath); |
| 24 | mockOnFileAttached(absolutePath, filePath); // Just the filename for missing file |
| 25 | } else { |
| 26 | console.error(`Error reading file ${filePath}:`, error); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // Should have called onFileAttached with filename as content |
| 31 | expect(mockOnFileAttached).toHaveBeenCalledWith( |
| 32 | path.resolve(filePath), |
| 33 | filePath, |
| 34 | ); |
| 35 | }); |
| 36 | }); |