Elffuss BraidHub

kikocis/continue public

80eede9a6f83 · 5.6 KiBRawPermalink
1import { expect, test, vi } from "vitest";
2import { ToolExtras } from "../..";
3import { readFileRangeImpl } from "./readFileRange";
4
5// Mock the dependencies
6vi.mock("../../util/ideUtils", () => ({
7 resolveRelativePathInDir: vi.fn(),
8}));
9
10vi.mock("../../util/uri", () => ({
11 getUriPathBasename: vi.fn(),
12}));
13
14vi.mock("./readFileLimit", () => ({
15 throwIfFileExceedsHalfOfContext: vi.fn(),
16}));
17
18test("readFileRangeImpl handles out-of-bounds ranges gracefully", async () => {
19 const { resolveRelativePathInDir } = await import("../../util/ideUtils");
20 const { getUriPathBasename } = await import("../../util/uri");
21 const { throwIfFileExceedsHalfOfContext } = await import("./readFileLimit");
22
23 // Mock the utility functions
24 vi.mocked(resolveRelativePathInDir).mockResolvedValue("file:///test.txt");
25 vi.mocked(getUriPathBasename).mockReturnValue("test.txt");
26 vi.mocked(throwIfFileExceedsHalfOfContext).mockResolvedValue(undefined);
27
28 // Test case 1: Start line beyond end of file
29 const mockIdeOutOfBounds = {
30 readRangeInFile: vi.fn().mockResolvedValue(""), // IDE returns empty string
31 };
32
33 const mockExtras1 = {
34 ide: mockIdeOutOfBounds,
35 config: { selectedModelByRole: { chat: { contextLength: 8192 } } },
36 } as unknown as ToolExtras;
37
38 const result1 = await readFileRangeImpl(
39 {
40 filepath: "test.txt",
41 startLine: 100, // Beyond end of file
42 endLine: 105,
43 },
44 mockExtras1,
45 );
46
47 expect(result1).toHaveLength(1);
48 expect(result1[0].content).toBe("");
49 expect(result1[0].description).toBe("test.txt (lines 100-105)");
50
51 // Test case 2: End line beyond end of file
52 const mockIdePartialRange = {
53 readRangeInFile: vi.fn().mockResolvedValue("line5\nline6"), // IDE returns available content
54 };
55
56 const mockExtras2 = {
57 ide: mockIdePartialRange,
58 config: { selectedModelByRole: { chat: { contextLength: 8192 } } },
59 } as unknown as ToolExtras;
60
61 const result2 = await readFileRangeImpl(
62 {
63 filepath: "test.txt",
64 startLine: 5,
65 endLine: 100, // Beyond end of file
66 },
67 mockExtras2,
68 );
69
70 expect(result2).toHaveLength(1);
71 expect(result2[0].content).toBe("line5\nline6");
72 expect(result2[0].description).toBe("test.txt (lines 5-100)");
73
74 // Verify that IDE methods were called with correct 0-based ranges
75 expect(mockIdeOutOfBounds.readRangeInFile).toHaveBeenCalledWith(
76 "file:///test.txt",
77 {
78 start: { line: 99, character: 0 }, // 100 - 1
79 end: { line: 104, character: Number.MAX_SAFE_INTEGER }, // 105 - 1
80 },
81 );
82
83 expect(mockIdePartialRange.readRangeInFile).toHaveBeenCalledWith(
84 "file:///test.txt",
85 {
86 start: { line: 4, character: 0 }, // 5 - 1
87 end: { line: 99, character: Number.MAX_SAFE_INTEGER }, // 100 - 1
88 },
89 );
90});
91
92test("readFileRangeImpl validates line number constraints", async () => {
93 const mockExtras = {
94 ide: { readRangeInFile: vi.fn(), readFile: vi.fn() },
95 config: { selectedModelByRole: { chat: { contextLength: 8192 } } },
96 } as unknown as ToolExtras;
97
98 // Test startLine < 1 (invalid)
99 await expect(
100 readFileRangeImpl(
101 {
102 filepath: "test.txt",
103 startLine: 0,
104 endLine: 5,
105 },
106 mockExtras,
107 ),
108 ).rejects.toThrow("startLine must be 1 or greater");
109
110 // Test negative startLine (no longer supported)
111 await expect(
112 readFileRangeImpl(
113 {
114 filepath: "test.txt",
115 startLine: -1,
116 endLine: 5,
117 },
118 mockExtras,
119 ),
120 ).rejects.toThrow("Negative line numbers are not supported");
121
122 // Test endLine < 1 (invalid)
123 await expect(
124 readFileRangeImpl(
125 {
126 filepath: "test.txt",
127 startLine: 1,
128 endLine: 0,
129 },
130 mockExtras,
131 ),
132 ).rejects.toThrow("endLine must be 1 or greater");
133
134 // Test negative endLine (no longer supported)
135 await expect(
136 readFileRangeImpl(
137 {
138 filepath: "test.txt",
139 startLine: 1,
140 endLine: -1,
141 },
142 mockExtras,
143 ),
144 ).rejects.toThrow("Negative line numbers are not supported");
145
146 // Test endLine < startLine
147 await expect(
148 readFileRangeImpl(
149 {
150 filepath: "test.txt",
151 startLine: 10,
152 endLine: 5,
153 },
154 mockExtras,
155 ),
156 ).rejects.toThrow(
157 "endLine (5) must be greater than or equal to startLine (10)",
158 );
159});
160
161test("readFileRangeImpl handles normal ranges correctly", async () => {
162 const { resolveRelativePathInDir } = await import("../../util/ideUtils");
163 const { getUriPathBasename } = await import("../../util/uri");
164 const { throwIfFileExceedsHalfOfContext } = await import("./readFileLimit");
165
166 vi.mocked(resolveRelativePathInDir).mockResolvedValue("file:///test.txt");
167 vi.mocked(getUriPathBasename).mockReturnValue("test.txt");
168 vi.mocked(throwIfFileExceedsHalfOfContext).mockResolvedValue(undefined);
169
170 const mockIde = {
171 readRangeInFile: vi.fn().mockResolvedValue("line2\nline3\nline4"),
172 };
173
174 const mockExtras = {
175 ide: mockIde,
176 config: { selectedModelByRole: { chat: { contextLength: 8192 } } },
177 } as unknown as ToolExtras;
178
179 const result = await readFileRangeImpl(
180 {
181 filepath: "src/test.py",
182 startLine: 2,
183 endLine: 4,
184 },
185 mockExtras,
186 );
187
188 expect(result).toHaveLength(1);
189 expect(result[0]).toEqual({
190 name: "test.txt",
191 description: "src/test.py (lines 2-4)",
192 content: "line2\nline3\nline4",
193 uri: {
194 type: "file",
195 value: "file:///test.txt",
196 },
197 });
198
199 // Verify correct 0-based conversion
200 expect(mockIde.readRangeInFile).toHaveBeenCalledWith("file:///test.txt", {
201 start: { line: 1, character: 0 }, // 2 - 1
202 end: { line: 3, character: Number.MAX_SAFE_INTEGER }, // 4 - 1
203 });
204});