| 1 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | import Parser from "web-tree-sitter"; |
| 3 | import { DocumentHistoryTracker } from "./DocumentHistoryTracker"; |
| 4 | |
| 5 | // Mock the Parser.Tree class |
| 6 | vi.mock("web-tree-sitter", () => { |
| 7 | return { |
| 8 | default: { |
| 9 | Tree: class MockTree { |
| 10 | constructor() { |
| 11 | // Mock implementation |
| 12 | } |
| 13 | }, |
| 14 | }, |
| 15 | }; |
| 16 | }); |
| 17 | |
| 18 | describe("DocumentHistoryTracker", () => { |
| 19 | let tracker: DocumentHistoryTracker; |
| 20 | let mockAst1: Parser.Tree; |
| 21 | let mockAst2: Parser.Tree; |
| 22 | const testDocPath = "/test/document.ts"; |
| 23 | const testContent1 = "const x = 1;"; |
| 24 | const testContent2 = "const x = 2;"; |
| 25 | |
| 26 | beforeEach(() => { |
| 27 | // Reset singleton instance for each test |
| 28 | // @ts-ignore - accessing private static property for testing |
| 29 | DocumentHistoryTracker.instance = null; |
| 30 | tracker = DocumentHistoryTracker.getInstance(); |
| 31 | |
| 32 | // Create mock ASTs |
| 33 | //@ts-ignore |
| 34 | mockAst1 = new Parser.Tree() as Parser.Tree; |
| 35 | //@ts-ignore |
| 36 | mockAst2 = new Parser.Tree() as Parser.Tree; |
| 37 | }); |
| 38 | |
| 39 | afterEach(() => { |
| 40 | // Clear the tracker after each test |
| 41 | tracker.clearMap(); |
| 42 | }); |
| 43 | |
| 44 | describe("getInstance", () => { |
| 45 | it("should return a singleton instance", () => { |
| 46 | const instance1 = DocumentHistoryTracker.getInstance(); |
| 47 | const instance2 = DocumentHistoryTracker.getInstance(); |
| 48 | expect(instance1).toBe(instance2); |
| 49 | }); |
| 50 | }); |
| 51 | |
| 52 | describe("addDocument", () => { |
| 53 | it("should add a document to the tracker", () => { |
| 54 | tracker.addDocument(testDocPath, testContent1, mockAst1); |
| 55 | |
| 56 | const ast = tracker.getMostRecentAst(testDocPath); |
| 57 | const content = tracker.getMostRecentDocumentHistory(testDocPath); |
| 58 | |
| 59 | expect(ast).toBe(mockAst1); |
| 60 | expect(content).toBe(testContent1); |
| 61 | }); |
| 62 | }); |
| 63 | |
| 64 | describe("push", () => { |
| 65 | it("should push a new AST to an existing document's history stack", () => { |
| 66 | tracker.addDocument(testDocPath, testContent1, mockAst1); |
| 67 | tracker.push(testDocPath, testContent2, mockAst2); |
| 68 | |
| 69 | const ast = tracker.getMostRecentAst(testDocPath); |
| 70 | const content = tracker.getMostRecentDocumentHistory(testDocPath); |
| 71 | |
| 72 | expect(ast).toBe(mockAst2); |
| 73 | expect(content).toBe(testContent2); |
| 74 | }); |
| 75 | |
| 76 | it("should add a document if it doesn't exist when pushing", () => { |
| 77 | tracker.push(testDocPath, testContent1, mockAst1); |
| 78 | |
| 79 | // Check if document was actually added |
| 80 | const ast = tracker.getMostRecentAst(testDocPath); |
| 81 | expect(ast).toBe(mockAst1); |
| 82 | }); |
| 83 | }); |
| 84 | |
| 85 | describe("getMostRecentAst", () => { |
| 86 | it("should return the most recent AST of a document", () => { |
| 87 | tracker.addDocument(testDocPath, testContent1, mockAst1); |
| 88 | tracker.push(testDocPath, testContent2, mockAst2); |
| 89 | |
| 90 | const ast = tracker.getMostRecentAst(testDocPath); |
| 91 | |
| 92 | expect(ast).toBe(mockAst2); |
| 93 | }); |
| 94 | |
| 95 | it("should return null if the document doesn't exist", () => { |
| 96 | // Spy on console.error |
| 97 | const consoleErrorSpy = vi |
| 98 | .spyOn(console, "error") |
| 99 | .mockImplementation(() => {}); |
| 100 | |
| 101 | const ast = tracker.getMostRecentAst("nonexistent-path"); |
| 102 | |
| 103 | expect(ast).toBeNull(); |
| 104 | expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 105 | "Document nonexistent-path not found in AST tracker", |
| 106 | ); |
| 107 | |
| 108 | consoleErrorSpy.mockRestore(); |
| 109 | }); |
| 110 | |
| 111 | it("should return null if the document has no ASTs", () => { |
| 112 | // This test would require modifying private properties |
| 113 | // So we'll mock an empty array situation |
| 114 | tracker.addDocument(testDocPath, testContent1, mockAst1); |
| 115 | |
| 116 | // @ts-ignore - accessing private property for testing |
| 117 | tracker.documentAstMap.set(testDocPath, []); |
| 118 | |
| 119 | // Spy on console.error |
| 120 | const consoleErrorSpy = vi |
| 121 | .spyOn(console, "error") |
| 122 | .mockImplementation(() => {}); |
| 123 | |
| 124 | const ast = tracker.getMostRecentAst(testDocPath); |
| 125 | |
| 126 | expect(ast).toBeNull(); |
| 127 | expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 128 | `Document ${testDocPath} has no ASTs`, |
| 129 | ); |
| 130 | |
| 131 | consoleErrorSpy.mockRestore(); |
| 132 | }); |
| 133 | }); |
| 134 | |
| 135 | describe("getMostRecentDocumentHistory", () => { |
| 136 | it("should return the most recent document history", () => { |
| 137 | tracker.addDocument(testDocPath, testContent1, mockAst1); |
| 138 | tracker.push(testDocPath, testContent2, mockAst2); |
| 139 | |
| 140 | const content = tracker.getMostRecentDocumentHistory(testDocPath); |
| 141 | |
| 142 | expect(content).toBe(testContent2); |
| 143 | }); |
| 144 | |
| 145 | it("should return null if the document doesn't exist", () => { |
| 146 | // Spy on console.error |
| 147 | const consoleErrorSpy = vi |
| 148 | .spyOn(console, "error") |
| 149 | .mockImplementation(() => {}); |
| 150 | |
| 151 | const content = tracker.getMostRecentDocumentHistory("nonexistent-path"); |
| 152 | |
| 153 | expect(content).toBeNull(); |
| 154 | expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 155 | "Document nonexistent-path not found in AST tracker", |
| 156 | ); |
| 157 | |
| 158 | consoleErrorSpy.mockRestore(); |
| 159 | }); |
| 160 | |
| 161 | it("should return null if the document has no history", () => { |
| 162 | tracker.addDocument(testDocPath, testContent1, mockAst1); |
| 163 | |
| 164 | // @ts-ignore - accessing private property for testing |
| 165 | tracker.documentContentHistoryMap.set(testDocPath, []); |
| 166 | |
| 167 | // Spy on console.error |
| 168 | const consoleErrorSpy = vi |
| 169 | .spyOn(console, "error") |
| 170 | .mockImplementation(() => {}); |
| 171 | |
| 172 | const content = tracker.getMostRecentDocumentHistory(testDocPath); |
| 173 | |
| 174 | expect(content).toBeNull(); |
| 175 | expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 176 | `Document ${testDocPath} has no history`, |
| 177 | ); |
| 178 | |
| 179 | consoleErrorSpy.mockRestore(); |
| 180 | }); |
| 181 | }); |
| 182 | |
| 183 | describe("deleteDocument", () => { |
| 184 | it("should delete a document from the tracker", () => { |
| 185 | tracker.addDocument(testDocPath, testContent1, mockAst1); |
| 186 | tracker.deleteDocument(testDocPath); |
| 187 | |
| 188 | // Spy on console.error |
| 189 | const consoleErrorSpy = vi |
| 190 | .spyOn(console, "error") |
| 191 | .mockImplementation(() => {}); |
| 192 | |
| 193 | const ast = tracker.getMostRecentAst(testDocPath); |
| 194 | const content = tracker.getMostRecentDocumentHistory(testDocPath); |
| 195 | |
| 196 | expect(ast).toBeNull(); |
| 197 | expect(content).toBeNull(); |
| 198 | expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 199 | `Document ${testDocPath} not found in AST tracker`, |
| 200 | ); |
| 201 | |
| 202 | consoleErrorSpy.mockRestore(); |
| 203 | }); |
| 204 | }); |
| 205 | |
| 206 | describe("clearMap", () => { |
| 207 | it("should clear all documents from the tracker", () => { |
| 208 | const anotherDocPath = "/test/another-document.ts"; |
| 209 | |
| 210 | tracker.addDocument(testDocPath, testContent1, mockAst1); |
| 211 | tracker.addDocument(anotherDocPath, testContent1, mockAst1); |
| 212 | |
| 213 | tracker.clearMap(); |
| 214 | |
| 215 | // Spy on console.error |
| 216 | const consoleErrorSpy = vi |
| 217 | .spyOn(console, "error") |
| 218 | .mockImplementation(() => {}); |
| 219 | |
| 220 | expect(tracker.getMostRecentAst(testDocPath)).toBeNull(); |
| 221 | expect(tracker.getMostRecentAst(anotherDocPath)).toBeNull(); |
| 222 | expect(tracker.getMostRecentDocumentHistory(testDocPath)).toBeNull(); |
| 223 | expect(tracker.getMostRecentDocumentHistory(anotherDocPath)).toBeNull(); |
| 224 | |
| 225 | consoleErrorSpy.mockRestore(); |
| 226 | }); |
| 227 | }); |
| 228 | }); |