-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnippets.test.ts
More file actions
121 lines (101 loc) · 4.76 KB
/
Copy pathsnippets.test.ts
File metadata and controls
121 lines (101 loc) · 4.76 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
// secret-scan: allow-fixture
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
import { NeuralSnippets } from "../src/memory/neural-snippets.js";
describe("NeuralSnippets", () => {
let tmpDir: string;
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "snippets-test-"));
NeuralSnippets.reset();
});
afterEach(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});
it("should parse TypeScript symbols correctly", async () => {
const tsCode = `
export class UserRepo {
async save(user: User) {
return db.save(user);
}
}
export function validate(input: string) {
return input.length > 0;
}
`;
fs.writeFileSync(path.join(tmpDir, "repo.ts"), tsCode);
await NeuralSnippets.initialize(tmpDir);
const snippets = await NeuralSnippets.search("UserRepo", tmpDir);
expect(snippets.length).toBeGreaterThan(0);
expect(snippets.some(s => s.symbolName === "UserRepo")).toBe(true);
});
it("should parse Python symbols correctly", async () => {
const pyCode = `
class Service:
def execute(self):
print("Executing")
def helper():
return True
`;
fs.writeFileSync(path.join(tmpDir, "service.py"), pyCode);
await NeuralSnippets.initialize(tmpDir);
const snippets = await NeuralSnippets.search("Service", tmpDir);
expect(snippets.length).toBeGreaterThan(0);
expect(snippets.some(s => s.symbolName === "Service")).toBe(true);
});
it("does not traverse symlinks or index sensitive filenames and content", async () => {
const outside = fs.mkdtempSync(path.join(os.tmpdir(), "snippets-outside-"));
fs.writeFileSync(path.join(outside, "escaped.ts"), "export function escapedSecret() {}");
fs.symlinkSync(outside, path.join(tmpDir, "linked-outside"), "junction");
fs.writeFileSync(path.join(tmpDir, "credentials.ts"), "export function filenameSecret() {}");
fs.writeFileSync(path.join(tmpDir, "literal.ts"), 'const apiKey = "literal-secret"; export function contentSecret() {}');
fs.writeFileSync(path.join(tmpDir, "safe.ts"), "interface Login { password: string }\nexport function safeSource() {}");
try {
await NeuralSnippets.initialize(tmpDir);
expect(await NeuralSnippets.search("escapedSecret", tmpDir)).toEqual([]);
expect(await NeuralSnippets.search("filenameSecret", tmpDir)).toEqual([]);
expect(await NeuralSnippets.search("contentSecret", tmpDir)).toEqual([]);
expect(await NeuralSnippets.search("safeSource", tmpDir)).toMatchObject([{ symbolName: "safeSource" }]);
} finally {
fs.rmSync(outside, { recursive: true, force: true });
}
});
it("rejects missing, outside, and swapped traversal results during canonical checks", async () => {
const outside = fs.mkdtempSync(path.join(os.tmpdir(), "snippets-canonical-outside-"));
const outsideFile = path.join(outside, "outside.ts");
const linkedOutside = path.join(tmpDir, "linked-outside");
const insideFile = path.join(tmpDir, "inside.ts");
fs.writeFileSync(outsideFile, "export function outsideSource() {}");
fs.writeFileSync(insideFile, "export function insideSource() {}");
fs.symlinkSync(outside, linkedOutside, "junction");
const walkDir = (NeuralSnippets as any).walkDir.bind(NeuralSnippets);
expect(await walkDir(path.join(tmpDir, "missing"), tmpDir)).toEqual([]);
expect(await walkDir(tmpDir, outside)).toEqual([]);
const originalWalkDir = (NeuralSnippets as any).walkDir;
(NeuralSnippets as any).walkDir = async () => [linkedOutside, outsideFile];
try {
await NeuralSnippets.initialize(tmpDir);
expect(await NeuralSnippets.search("outsideSource", tmpDir)).toEqual([]);
} finally {
(NeuralSnippets as any).walkDir = originalWalkDir;
fs.rmSync(outside, { recursive: true, force: true });
}
});
it("skips entries when canonical path resolution fails", async () => {
const safeFile = path.join(tmpDir, "safe.ts");
const brokenFile = path.join(tmpDir, "broken.ts");
fs.writeFileSync(safeFile, "export function safeResolution() {}");
fs.writeFileSync(brokenFile, "export function brokenResolution() {}");
const originalRealpath = fs.realpathSync.native;
vi.spyOn(fs.realpathSync, "native").mockImplementation((target) => {
if (String(target).endsWith("broken.ts")) {
throw new Error("transient filesystem error");
}
return originalRealpath(target);
});
await NeuralSnippets.initialize(tmpDir);
expect(await NeuralSnippets.search("safeResolution", tmpDir)).toMatchObject([{ symbolName: "safeResolution" }]);
expect(await NeuralSnippets.search("brokenResolution", tmpDir)).toEqual([]);
});
});