-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepofs.test.ts
More file actions
230 lines (185 loc) · 6.92 KB
/
Copy pathrepofs.test.ts
File metadata and controls
230 lines (185 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { execFile } from "node:child_process";
import { mkdir, mkdtemp, rm, symlink, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { afterAll, beforeAll, describe, expect, it } from "vite-plus/test";
import { RepoFs, RepoFsError } from "./repofs.ts";
function git(args: string[], cwd: string): Promise<string> {
return new Promise((res, rej) => {
execFile("git", args, { cwd }, (err, stdout) => (err ? rej(err) : res(stdout)));
});
}
let rootDir: string;
let rfs: RepoFs;
beforeAll(async () => {
rootDir = await mkdtemp(join(tmpdir(), "repofs-test-"));
await mkdir(join(rootDir, "src/nested"), { recursive: true });
await mkdir(join(rootDir, "vendor"), { recursive: true });
await mkdir(join(rootDir, ".hidden"), { recursive: true });
await writeFile(join(rootDir, "README.md"), "# Test\nSecond line\n");
await writeFile(join(rootDir, "src/main.ts"), 'console.log("hello");\nconsole.log("world");\n');
await writeFile(join(rootDir, "src/nested/deep.ts"), "export const x = 1;\n");
await writeFile(join(rootDir, "vendor/lib.js"), "module.exports = {};\n");
await writeFile(join(rootDir, ".hidden/config"), "secret\n");
await writeFile(join(rootDir, "empty.txt"), "");
await writeFile(join(rootDir, "image.png"), Buffer.from([0x89, 0x50, 0x4e, 0x47]));
await symlink(tmpdir(), join(rootDir, "escape-link"));
await symlink(join(rootDir, "src/main.ts"), join(rootDir, "link-to-file"));
await git(["init"], rootDir);
await git(["-c", "user.name=test", "-c", "user.email=test@test.com", "add", "."], rootDir);
await git(["-c", "user.name=test", "-c", "user.email=test@test.com", "commit", "-m", "init"], rootDir);
rfs = await RepoFs.open(rootDir);
});
afterAll(async () => {
await rm(rootDir, { recursive: true, force: true });
});
describe("RepoFs.open", () => {
it("opens a valid git repo", () => {
expect(rfs.root).toBe(resolve(rootDir));
expect(rfs.git).toBeDefined();
expect(rfs.index).toBeDefined();
});
it("rejects non-existent path", async () => {
await expect(RepoFs.open("/tmp/nonexistent-repofs-test-xyz")).rejects.toThrow(RepoFsError);
});
it("rejects file as root", async () => {
const filePath = join(rootDir, "README.md");
await expect(RepoFs.open(filePath)).rejects.toThrow("not a directory");
});
});
describe("resolve", () => {
it("returns relative path from root", () => {
expect(rfs.resolve("src/main.ts")).toBe("src/main.ts");
});
it("resolves . to empty string", () => {
expect(rfs.resolve(".")).toBe("");
});
it("normalizes redundant segments", () => {
expect(rfs.resolve("src/../README.md")).toBe("README.md");
});
it("normalizes backslash separators", () => {
expect(rfs.resolve("src\\main.ts")).toBe("src/main.ts");
});
it("normalizes mixed separators", () => {
expect(rfs.resolve("src\\nested/deep.ts")).toBe("src/nested/deep.ts");
});
it("accepts absolute path inside root", () => {
expect(rfs.resolve(join(resolve(rootDir), "src/main.ts"))).toBe("src/main.ts");
});
it("rejects .. escape", () => {
expect(() => rfs.resolve("../etc/passwd")).toThrow("path escapes root");
});
it("rejects deep .. escape", () => {
expect(() => rfs.resolve("src/../../etc/passwd")).toThrow("path escapes root");
});
it("rejects absolute path outside root", () => {
expect(() => rfs.resolve("/etc/passwd")).toThrow("path escapes root");
});
it("resolves empty path to empty string", () => {
expect(rfs.resolve("")).toBe("");
});
});
describe("fileExists", () => {
it("returns true for existing file", () => {
expect(rfs.fileExists("README.md")).toBe(true);
});
it("returns false for directory", () => {
expect(rfs.fileExists("src")).toBe(false);
});
it("returns false for non-existent", () => {
expect(rfs.fileExists("nonexistent.ts")).toBe(false);
});
});
describe("dirExists", () => {
it("returns true for existing directory", () => {
expect(rfs.dirExists("src")).toBe(true);
});
it("returns true for root", () => {
expect(rfs.dirExists(".")).toBe(true);
});
it("returns false for file", () => {
expect(rfs.dirExists("README.md")).toBe(false);
});
it("returns false for non-existent", () => {
expect(rfs.dirExists("nonexistent")).toBe(false);
});
it("throws on escape attempt", () => {
expect(() => rfs.dirExists("../../etc")).toThrow("path escapes root");
});
});
describe("readFile", () => {
it("returns file content as Buffer", async () => {
const buf = await rfs.readFile("README.md");
expect(buf).toBeInstanceOf(Buffer);
expect(buf.toString("utf-8")).toBe("# Test\nSecond line\n");
});
it("reads binary content without rejection", async () => {
const buf = await rfs.readFile("image.png");
expect(buf).toEqual(Buffer.from([0x89, 0x50, 0x4e, 0x47]));
});
it("rejects non-existent file", async () => {
await expect(rfs.readFile("nonexistent.ts")).rejects.toThrow("file not found");
});
it("rejects directory", async () => {
await expect(rfs.readFile("src")).rejects.toThrow("not a regular file");
});
it("rejects symlink", async () => {
await expect(rfs.readFile("link-to-file")).rejects.toThrow("not a regular file");
});
it("provides suggestions for not found", async () => {
try {
await rfs.readFile("src/mian.ts");
} catch (err) {
expect(err).toBeInstanceOf(RepoFsError);
expect((err as Error).message).toContain("file not found");
}
});
it("records read paths for flush", async () => {
rfs.flushReadPaths();
await rfs.readFile("README.md");
await rfs.readFile("src/main.ts");
const paths = rfs.flushReadPaths();
expect(paths).toEqual(["README.md", "src/main.ts"]);
expect(rfs.flushReadPaths()).toEqual([]);
});
});
describe("findFiles", () => {
it("finds files by fuzzy pattern", () => {
const results = rfs.findFiles("main");
expect(results.length).toBeGreaterThan(0);
expect(results.some((r) => r.includes("main.ts"))).toBe(true);
});
it("respects maxResults", () => {
const results = rfs.findFiles("ts", { maxResults: 1 });
expect(results).toHaveLength(1);
});
});
describe("globFiles", () => {
it("filters by glob pattern", () => {
const results = rfs.globFiles({ includes: ["*.ts"] });
expect(results.length).toBeGreaterThan(0);
expect(results.every((r) => r.endsWith(".ts"))).toBe(true);
});
it("supports exclude patterns", () => {
const all = rfs.globFiles({ includes: ["*.ts"] });
const filtered = rfs.globFiles({ includes: ["*.ts"], excludes: ["**/nested/**"] });
expect(filtered.length).toBeLessThan(all.length);
});
});
describe("grep", () => {
it("searches file contents", async () => {
const result = await rfs.grep("hello");
expect(result.lineCount).toBeGreaterThan(0);
expect(result.output).toContain("main.ts");
});
it("returns empty for no matches", async () => {
const result = await rfs.grep("zzz_no_match_zzz");
expect(result.lineCount).toBe(0);
});
});
describe("flushReadPaths", () => {
it("returns empty when no reads happened", () => {
rfs.flushReadPaths();
expect(rfs.flushReadPaths()).toEqual([]);
});
});