|
| 1 | +import { describe, it, expect, beforeEach, vi } from "vitest"; |
| 2 | +import { importerRegistry } from "./ImporterRegistry"; |
| 3 | +import { ImportEvent, IImporter } from "./types"; |
| 4 | +import { RootState } from "@/store"; |
| 5 | + |
| 6 | +const mockState = {} as unknown as RootState; |
| 7 | + |
| 8 | +describe("ImporterRegistry", () => { |
| 9 | + let mockFile: unknown & File; |
| 10 | + let arrayBufferMock: ArrayBuffer; |
| 11 | + let importEvent: ImportEvent; |
| 12 | + |
| 13 | + let mockImporter: IImporter; |
| 14 | + let fallbackImporter: IImporter; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + arrayBufferMock = new ArrayBuffer(8); |
| 18 | + |
| 19 | + mockFile = { |
| 20 | + name: "test.txt", |
| 21 | + arrayBuffer: vi.fn().mockResolvedValue(arrayBufferMock), |
| 22 | + } as unknown as File; |
| 23 | + |
| 24 | + vi.spyOn(mockFile, "arrayBuffer").mockResolvedValue(arrayBufferMock); |
| 25 | + |
| 26 | + importEvent = { |
| 27 | + file: mockFile, |
| 28 | + path: "/mock/path", |
| 29 | + position: { x: 10, y: 20 }, |
| 30 | + dispatch: vi.fn(), |
| 31 | + getState: vi.fn(() => mockState), |
| 32 | + }; |
| 33 | + |
| 34 | + importerRegistry.clearImporters(); |
| 35 | + |
| 36 | + mockImporter = { |
| 37 | + canHandle: vi.fn().mockResolvedValue(true), |
| 38 | + importData: vi.fn().mockResolvedValue({ |
| 39 | + success: true, |
| 40 | + message: "Handled by mockImporter", |
| 41 | + }), |
| 42 | + }; |
| 43 | + |
| 44 | + fallbackImporter = { |
| 45 | + canHandle: vi.fn().mockResolvedValue(false), |
| 46 | + importData: vi.fn(), |
| 47 | + }; |
| 48 | + }); |
| 49 | + |
| 50 | + it("registers and sorts importers by priority", () => { |
| 51 | + importerRegistry.registerImporter(mockImporter, 1); |
| 52 | + importerRegistry.registerImporter(fallbackImporter, 10); |
| 53 | + |
| 54 | + const importers = importerRegistry.getImporters(); |
| 55 | + expect(importers.length).toBe(2); |
| 56 | + expect(importers[0].importer).toBe(fallbackImporter); // highest priority first |
| 57 | + }); |
| 58 | + |
| 59 | + it("unregisters an importer", () => { |
| 60 | + importerRegistry.registerImporter(mockImporter, 1); |
| 61 | + importerRegistry.unregisterImporter(mockImporter); |
| 62 | + |
| 63 | + const importers = importerRegistry.getImporters(); |
| 64 | + expect(importers.length).toBe(0); |
| 65 | + }); |
| 66 | + |
| 67 | + it("uses the first importer that can handle the file", async () => { |
| 68 | + importerRegistry.registerImporter(fallbackImporter, 1); |
| 69 | + importerRegistry.registerImporter(mockImporter, 5); |
| 70 | + |
| 71 | + const result = await importerRegistry.import(importEvent); |
| 72 | + |
| 73 | + expect(mockFile.arrayBuffer).toHaveBeenCalledOnce(); |
| 74 | + expect(mockImporter.canHandle).toHaveBeenCalledWith( |
| 75 | + mockFile, |
| 76 | + arrayBufferMock |
| 77 | + ); |
| 78 | + expect(mockImporter.importData).toHaveBeenCalledWith(importEvent); |
| 79 | + expect(result).toEqual({ |
| 80 | + success: true, |
| 81 | + message: "Handled by mockImporter", |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it("returns failure when no importer can handle the file", async () => { |
| 86 | + importerRegistry.registerImporter(fallbackImporter, 1); |
| 87 | + |
| 88 | + const result = await importerRegistry.import(importEvent); |
| 89 | + |
| 90 | + expect(result.success).toBe(false); |
| 91 | + expect(result.message).toBe("No suitable importer found."); |
| 92 | + expect(fallbackImporter.importData).not.toHaveBeenCalled(); |
| 93 | + }); |
| 94 | +}); |
0 commit comments