-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsync.test.ts
More file actions
175 lines (156 loc) · 5.28 KB
/
sync.test.ts
File metadata and controls
175 lines (156 loc) · 5.28 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
import path from "node:path";
import { Lang, parse } from "@ast-grep/napi";
import { describe, expect, it } from "vitest";
import { isWithinDirectory, parseImports, parsePluginUsages } from "./sync";
describe("plugin sync", () => {
describe("isWithinDirectory", () => {
it("returns true when filePath equals boundary", () => {
const dir = path.resolve("/project/root");
expect(isWithinDirectory(dir, dir)).toBe(true);
});
it("returns true when filePath is inside boundary", () => {
expect(
isWithinDirectory("/project/root/sub/file.ts", "/project/root"),
).toBe(true);
expect(isWithinDirectory("/project/root/foo", "/project/root")).toBe(
true,
);
});
it("returns false when filePath escapes boundary", () => {
expect(
isWithinDirectory("/project/root/../etc/passwd", "/project/root"),
).toBe(false);
expect(isWithinDirectory("/other/file.ts", "/project/root")).toBe(false);
});
it("returns false when path is sibling (prefix edge case)", () => {
const root = path.resolve("/project/root");
const sibling = path.resolve("/project/root-bar/file.ts");
expect(isWithinDirectory(sibling, root)).toBe(false);
});
it("handles relative paths by resolving them", () => {
const cwd = process.cwd();
expect(isWithinDirectory("package.json", cwd)).toBe(true);
});
});
describe("parseImports", () => {
function parseCode(code: string) {
const ast = parse(Lang.TypeScript, code);
return parseImports(ast.root());
}
it("extracts named imports from a single statement", () => {
const imports = parseCode(
`import { createApp, server, analytics } from "@databricks/appkit";`,
);
expect(imports).toHaveLength(3);
expect(imports.map((i) => i.name)).toEqual([
"createApp",
"server",
"analytics",
]);
expect(imports.map((i) => i.originalName)).toEqual([
"createApp",
"server",
"analytics",
]);
expect(imports[0].source).toBe("@databricks/appkit");
});
it("extracts aliased imports", () => {
const imports = parseCode(
`import { createApp as initApp, server as srv } from "@databricks/appkit";`,
);
expect(imports).toHaveLength(2);
expect(imports[0]).toEqual({
name: "initApp",
originalName: "createApp",
source: "@databricks/appkit",
});
expect(imports[1]).toEqual({
name: "srv",
originalName: "server",
source: "@databricks/appkit",
});
});
it("extracts relative imports", () => {
const imports = parseCode(
`import { myPlugin } from "./plugins/my-plugin";`,
);
expect(imports).toHaveLength(1);
expect(imports[0].name).toBe("myPlugin");
expect(imports[0].source).toBe("./plugins/my-plugin");
});
it("extracts relative imports with file extension (.js)", () => {
const imports = parseCode(
`import { myPlugin } from "./my-plugin/my-plugin.js";`,
);
expect(imports).toHaveLength(1);
expect(imports[0].name).toBe("myPlugin");
expect(imports[0].source).toBe("./my-plugin/my-plugin.js");
});
it("handles double-quoted specifiers", () => {
const imports = parseCode(`import { foo } from "@databricks/appkit";`);
expect(imports[0].source).toBe("@databricks/appkit");
});
it("returns empty array when no named imports", () => {
const imports = parseCode(`const x = 1;`);
expect(imports).toHaveLength(0);
});
it("handles multiple import statements", () => {
const imports = parseCode(`
import { createApp } from "@databricks/appkit";
import { myPlugin } from "./my-plugin";
`);
expect(imports).toHaveLength(2);
expect(imports[0].source).toBe("@databricks/appkit");
expect(imports[1].source).toBe("./my-plugin");
});
});
describe("parsePluginUsages", () => {
function parseCode(code: string) {
const ast = parse(Lang.TypeScript, code);
return parsePluginUsages(ast.root());
}
it("extracts plugin names used in createApp plugins array", () => {
const used = parseCode(`
createApp({
plugins: [
server(),
analytics(),
],
});
`);
expect(Array.from(used)).toEqual(
expect.arrayContaining(["server", "analytics"]),
);
expect(used.size).toBe(2);
});
it("ignores non-plugin call expressions in the same object", () => {
const used = parseCode(`
createApp({
plugins: [server()],
telemetry: { enabled: true },
});
`);
expect(Array.from(used)).toEqual(["server"]);
});
it("returns empty set when no plugins key with array", () => {
const used = parseCode(`createApp({});`);
expect(used.size).toBe(0);
});
it("returns empty set when plugins is not an array of calls", () => {
const used = parseCode(`
createApp({
plugins: [],
});
`);
expect(used.size).toBe(0);
});
it("extracts single plugin usage", () => {
const used = parseCode(`
createApp({
plugins: [server()],
});
`);
expect(Array.from(used)).toEqual(["server"]);
});
});
});