forked from mxx1111/remote-code-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject-catalog.test.mjs
More file actions
149 lines (125 loc) · 4.67 KB
/
Copy pathproject-catalog.test.mjs
File metadata and controls
149 lines (125 loc) · 4.67 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
import test from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, mkdirSync, writeFileSync, rmSync, symlinkSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { spawnSync } from "node:child_process";
import { createProjectCatalog } from "./project-catalog.mjs";
test("createProjectCatalog tests", async (t) => {
let tempRoot;
t.beforeEach(() => {
tempRoot = mkdtempSync(join(tmpdir(), "rcm-catalog-test-"));
});
t.afterEach(() => {
if (tempRoot) {
rmSync(tempRoot, { recursive: true, force: true });
}
});
await t.test("lists directories and excludes dotfile directories and regular files", () => {
mkdirSync(join(tempRoot, "proj-a"));
mkdirSync(join(tempRoot, ".hidden-dir"));
writeFileSync(join(tempRoot, "some-file.txt"), "hello");
const catalog = createProjectCatalog({
machineName: "macbook",
rootPath: tempRoot,
sessionName: "default-session",
});
const projects = catalog.list();
assert.equal(projects.length, 1);
assert.equal(projects[0].id, "proj-a");
assert.equal(projects[0].name, "proj-a");
assert.equal(projects[0].machine, "macbook");
assert.equal(projects[0].session, "default-session");
});
await t.test("symlink pointing outside the root does not produce a project escaping resolvedRoot", () => {
const outsideDir = mkdtempSync(join(tmpdir(), "rcm-outside-"));
try {
symlinkSync(outsideDir, join(tempRoot, "symlink-escape"), "dir");
const catalog = createProjectCatalog({
machineName: "macbook",
rootPath: tempRoot,
sessionName: "default-session",
});
const projects = catalog.list();
// symlink cwd resolves outside tempRoot, so containment check fails and filters it out
assert.equal(projects.find((p) => p.id === "symlink-escape"), undefined);
} finally {
rmSync(outsideDir, { recursive: true, force: true });
}
});
await t.test("get() returns project details for valid id and null for unknown id", () => {
mkdirSync(join(tempRoot, "alpha"));
const catalog = createProjectCatalog({
machineName: "macbook",
rootPath: tempRoot,
sessionName: "default-session",
});
const found = catalog.get("alpha");
assert.notEqual(found, null);
assert.equal(found?.id, "alpha");
const missing = catalog.get("non-existent");
assert.equal(missing, null);
});
await t.test("non-git directory reports branch 'folder', git directory reports actual branch", () => {
mkdirSync(join(tempRoot, "plain-dir"));
const gitDir = join(tempRoot, "git-dir");
mkdirSync(gitDir);
spawnSync("git", ["init", "-b", "feature/my-branch", gitDir], { encoding: "utf8" });
const catalog = createProjectCatalog({
machineName: "macbook",
rootPath: tempRoot,
sessionName: "default-session",
});
const plain = catalog.get("plain-dir");
assert.equal(plain?.branch, "folder");
const gitProject = catalog.get("git-dir");
assert.equal(gitProject?.branch, "feature/my-branch");
});
await t.test("sorting respects numeric: true (e.g. project2 before project10)", () => {
mkdirSync(join(tempRoot, "project10"));
mkdirSync(join(tempRoot, "project2"));
mkdirSync(join(tempRoot, "project1"));
const catalog = createProjectCatalog({
machineName: "macbook",
rootPath: tempRoot,
sessionName: "default-session",
});
const list = catalog.list();
assert.deepEqual(
list.map((p) => p.name),
["project1", "project2", "project10"]
);
});
await t.test("5-second cache prevents rescan unless refresh: true is passed", async () => {
mkdirSync(join(tempRoot, "first"));
const catalog = createProjectCatalog({
machineName: "macbook",
rootPath: tempRoot,
sessionName: "default-session",
});
const initial = catalog.list();
assert.equal(initial.length, 1);
mkdirSync(join(tempRoot, "second"));
const cached = catalog.list();
assert.equal(cached.length, 1);
assert.equal(cached[0].id, "first");
const refreshed = catalog.list({ refresh: true });
assert.equal(refreshed.length, 2);
assert.deepEqual(
refreshed.map((p) => p.name),
["first", "second"]
);
});
await t.test("handles non-existent root directory gracefully or when root does not exist", () => {
const nonExistentPath = join(tmpdir(), "non-existent-dir-" + Date.now());
const catalog = createProjectCatalog({
machineName: "macbook",
rootPath: nonExistentPath,
sessionName: "default-session",
});
assert.ok(catalog.rootLabel);
assert.throws(() => {
catalog.list();
}, /ENOENT/);
});
});