Elffuss BraidHub

kikocis/continue public

main · 7.6 KiBRawPermalink
1import { beforeEach, describe, expect, it, vi } from "vitest";
2import * as vscode from "vscode";
3import { GhostTextAcceptanceTracker } from "./GhostTextAcceptanceTracker";
4
5// Mock the vscode module
6vi.mock("vscode", () => ({
7 Position: vi.fn((line: number, character: number) => ({
8 line,
9 character,
10 isEqual: vi.fn(
11 (other: any) => other.line === line && other.character === character,
12 ),
13 })),
14 Range: vi.fn((start: any, end: any) => ({ start, end })),
15}));
16
17describe("GhostTextAcceptanceTracker", () => {
18 let tracker: GhostTextAcceptanceTracker;
19 let mockDocument: vscode.TextDocument;
20
21 beforeEach(() => {
22 // Clear any existing instance
23 GhostTextAcceptanceTracker.clearInstance();
24 tracker = GhostTextAcceptanceTracker.getInstance();
25
26 // Create mock document
27 mockDocument = {
28 uri: {
29 toString: () => "file:///test.ts",
30 },
31 version: 1,
32 getText: vi.fn(),
33 } as any;
34 });
35
36 describe("getInstance", () => {
37 it("should return the same instance when called multiple times", () => {
38 const instance1 = GhostTextAcceptanceTracker.getInstance();
39 const instance2 = GhostTextAcceptanceTracker.getInstance();
40 expect(instance1).toBe(instance2);
41 });
42
43 it("should create a new instance after clearInstance is called", () => {
44 const instance1 = GhostTextAcceptanceTracker.getInstance();
45 GhostTextAcceptanceTracker.clearInstance();
46 const instance2 = GhostTextAcceptanceTracker.getInstance();
47 expect(instance1).not.toBe(instance2);
48 });
49 });
50
51 describe("setExpectedGhostTextAcceptance", () => {
52 it("should set expected acceptance for single line text", () => {
53 const startPosition = new vscode.Position(5, 10);
54 const text = "console.log('hello')";
55
56 tracker.setExpectedGhostTextAcceptance(mockDocument, text, startPosition);
57
58 // Verify internal state by testing checkGhostTextWasAccepted.
59 const newDocument = {
60 ...mockDocument,
61 version: 2,
62 getText: vi.fn().mockReturnValue(text),
63 } as any;
64 const endPosition = new vscode.Position(5, 30); // 10 in startPosition + 20 characters in text
65
66 expect(tracker.checkGhostTextWasAccepted(newDocument, endPosition)).toBe(
67 true,
68 );
69 });
70
71 it("should set expected acceptance for multi-line text", () => {
72 const startPosition = new vscode.Position(5, 10);
73 const text = "function test() {\n return 'hello';\n}";
74
75 tracker.setExpectedGhostTextAcceptance(mockDocument, text, startPosition);
76
77 // For multi-line, end position should be at line 7 (5 + 3 lines - 1), character 1 (length of last line).
78 const newDocument = {
79 ...mockDocument,
80 version: 2,
81 getText: vi.fn().mockReturnValue(text),
82 } as any;
83 const endPosition = new vscode.Position(7, 1);
84
85 expect(tracker.checkGhostTextWasAccepted(newDocument, endPosition)).toBe(
86 true,
87 );
88 });
89 });
90
91 describe("checkGhostTextWasAccepted", () => {
92 beforeEach(() => {
93 const startPosition = new vscode.Position(5, 10);
94 const text = "console.log('test')";
95 tracker.setExpectedGhostTextAcceptance(mockDocument, text, startPosition);
96 });
97
98 it("should return false when no expected acceptance is set", () => {
99 GhostTextAcceptanceTracker.clearInstance();
100 const newTracker = GhostTextAcceptanceTracker.getInstance();
101
102 const newDocument = { ...mockDocument, version: 2 } as any;
103 const position = new vscode.Position(5, 29);
104
105 expect(newTracker.checkGhostTextWasAccepted(newDocument, position)).toBe(
106 false,
107 );
108 });
109
110 it("should return false when document URI doesn't match", () => {
111 const differentDocument = {
112 ...mockDocument,
113 uri: { toString: () => "file:///different.ts" },
114 version: 2,
115 } as any;
116 const position = new vscode.Position(5, 29);
117
118 expect(
119 tracker.checkGhostTextWasAccepted(differentDocument, position),
120 ).toBe(false);
121 });
122
123 it("should return false when document version is not newer", () => {
124 const sameVersionDocument = {
125 ...mockDocument,
126 version: 1, // same version
127 } as any;
128 const position = new vscode.Position(5, 29);
129
130 expect(
131 tracker.checkGhostTextWasAccepted(sameVersionDocument, position),
132 ).toBe(false);
133
134 const olderVersionDocument = {
135 ...mockDocument,
136 version: 0, // older version
137 } as any;
138
139 expect(
140 tracker.checkGhostTextWasAccepted(olderVersionDocument, position),
141 ).toBe(false);
142 });
143
144 it("should return false when cursor position doesn't match expected end position", () => {
145 const newDocument = {
146 ...mockDocument,
147 version: 2,
148 getText: vi.fn().mockReturnValue("console.log('test')"),
149 } as any;
150 const wrongPosition = new vscode.Position(5, 20); // wrong position
151
152 expect(
153 tracker.checkGhostTextWasAccepted(newDocument, wrongPosition),
154 ).toBe(false);
155 });
156
157 it("should return false when text content doesn't match", () => {
158 const newDocument = {
159 ...mockDocument,
160 version: 2,
161 getText: vi.fn().mockReturnValue("different text"), // wrong text
162 } as any;
163 const correctPosition = new vscode.Position(5, 29);
164
165 expect(
166 tracker.checkGhostTextWasAccepted(newDocument, correctPosition),
167 ).toBe(false);
168 });
169
170 it("should return true when all conditions match and clear expectation", () => {
171 const expectedText = "console.log('test')";
172 const newDocument = {
173 ...mockDocument,
174 version: 2,
175 getText: vi.fn().mockReturnValue(expectedText),
176 } as any;
177 const correctPosition = new vscode.Position(5, 29); // 10 + 19 characters
178
179 expect(
180 tracker.checkGhostTextWasAccepted(newDocument, correctPosition),
181 ).toBe(true);
182
183 // Check for clearing logic.
184 expect(
185 tracker.checkGhostTextWasAccepted(newDocument, correctPosition),
186 ).toBe(false);
187 });
188
189 it("should handle getText throwing an error gracefully", () => {
190 const newDocument = {
191 ...mockDocument,
192 version: 2,
193 getText: vi.fn().mockImplementation(() => {
194 throw new Error("Range invalid");
195 }),
196 } as any;
197 const correctPosition = new vscode.Position(5, 29);
198
199 expect(
200 tracker.checkGhostTextWasAccepted(newDocument, correctPosition),
201 ).toBe(false);
202 });
203
204 it("should work with multi-line ghost text acceptance", () => {
205 const startPosition = new vscode.Position(0, 0);
206 const multiLineText = "if (condition) {\n doSomething();\n}";
207 tracker.setExpectedGhostTextAcceptance(
208 mockDocument,
209 multiLineText,
210 startPosition,
211 );
212
213 const newDocument = {
214 ...mockDocument,
215 version: 2,
216 getText: vi.fn().mockReturnValue(multiLineText),
217 } as any;
218 const endPosition = new vscode.Position(2, 1); // line 2, character 1 (length of "}")
219
220 expect(tracker.checkGhostTextWasAccepted(newDocument, endPosition)).toBe(
221 true,
222 );
223 });
224
225 it("should handle empty text", () => {
226 const startPosition = new vscode.Position(10, 5);
227 const emptyText = "";
228 tracker.setExpectedGhostTextAcceptance(
229 mockDocument,
230 emptyText,
231 startPosition,
232 );
233
234 const newDocument = {
235 ...mockDocument,
236 version: 2,
237 getText: vi.fn().mockReturnValue(""),
238 } as any;
239 const samePosition = new vscode.Position(10, 5); // should be same as start for empty text
240
241 expect(tracker.checkGhostTextWasAccepted(newDocument, samePosition)).toBe(
242 true,
243 );
244 });
245 });
246});