-
-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathworkspaces.test.ts
More file actions
253 lines (235 loc) · 9.82 KB
/
Copy pathworkspaces.test.ts
File metadata and controls
253 lines (235 loc) · 9.82 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { execFile } from "node:child_process";
import { mkdtemp, mkdir, rm, stat, symlink, writeFile } from "node:fs/promises";
import { platform, tmpdir } from "node:os";
import { join } from "node:path";
import { promisify } from "node:util";
import assert from "node:assert/strict";
import { loadConfig } from "./config.js";
import { GitWorktreeError } from "./git-worktrees.js";
import { SqliteWorkspaceStore } from "./workspace-store.js";
import { ensureCheckoutWorkspaceRoot, WorkspaceRegistry } from "./workspaces.js";
const execFileAsync = promisify(execFile);
const root = await mkdtemp(join(tmpdir(), "devspace-workspace-test-"));
const outsideRoot = await mkdtemp(join(tmpdir(), "devspace-workspace-outside-test-"));
const worktreeRoot = await mkdtemp(join(tmpdir(), "devspace-worktree-test-"));
try {
const agentDir = join(root, ".pi", "agent");
await mkdir(agentDir, { recursive: true });
if (platform() === "win32") {
await writeFile(join(agentDir, "AGENTS.md"), "global instructions\n");
} else {
await mkdir(join(agentDir, "skills"), { recursive: true });
await writeFile(join(agentDir, "skills", "AGENTS.md"), "global instructions\n");
await symlink("skills/AGENTS.md", join(agentDir, "AGENTS.md"));
}
await writeFile(join(root, "AGENTS.md"), "root instructions\n");
await mkdir(join(root, ".devspace", "agents"), { recursive: true });
await writeFile(
join(root, ".devspace", "agents", "reviewer.md"),
[
"---",
"name: reviewer",
"description: Read-only project reviewer.",
"provider: codex",
"---",
"",
"Review only.",
"",
].join("\n"),
);
await mkdir(join(root, "nested"));
await writeFile(join(root, "nested", "AGENTS.md"), "nested instructions\n");
await writeFile(join(root, "nested", "file.txt"), "hello\n");
const config = loadConfig({
DEVSPACE_CONFIG_DIR: join(root, ".devspace-home"),
DEVSPACE_ALLOWED_ROOTS: root,
DEVSPACE_WORKTREE_ROOT: worktreeRoot,
DEVSPACE_AGENT_DIR: agentDir,
DEVSPACE_SUBAGENTS: "1",
DEVSPACE_OAUTH_OWNER_TOKEN: "test-owner-token-that-is-long-enough",
PORT: "1",
});
const registry = new WorkspaceRegistry(config);
const { workspace, agentsFiles, availableAgentsFiles } = await registry.openWorkspace(root);
assert.equal(workspace.mode, "checkout");
assert.deepEqual(
agentsFiles.map((file) => file.content),
["global instructions\n", "root instructions\n"],
);
assert.deepEqual(
availableAgentsFiles.map((file) => file.path),
[join(root, "nested", "AGENTS.md")],
);
assert.deepEqual(
workspace.agentProfiles.map((profile) => ({
name: profile.name,
description: profile.description,
provider: profile.provider,
body: profile.body,
})),
[
{
name: "reviewer",
description: "Read-only project reviewer.",
provider: "codex",
body: "Review only.",
},
],
);
if (platform() !== "win32") {
const unsafeAgentDir = join(root, ".pi", "unsafe-agent");
await mkdir(unsafeAgentDir, { recursive: true });
await writeFile(join(outsideRoot, "secret.txt"), "outside secret\n");
await symlink(join(outsideRoot, "secret.txt"), join(unsafeAgentDir, "AGENTS.md"));
const unsafeConfig = loadConfig({
DEVSPACE_CONFIG_DIR: join(root, ".devspace-unsafe-home"),
DEVSPACE_ALLOWED_ROOTS: root,
DEVSPACE_WORKTREE_ROOT: join(root, ".devspace", "unsafe-worktrees"),
DEVSPACE_AGENT_DIR: unsafeAgentDir,
DEVSPACE_OAUTH_OWNER_TOKEN: "test-owner-token-that-is-long-enough",
PORT: "1",
});
const unsafeWorkspace = await new WorkspaceRegistry(unsafeConfig).openWorkspace(root);
assert.deepEqual(
unsafeWorkspace.agentsFiles.map((file) => file.content),
["root instructions\n"],
);
}
const missingWorkspaceRoot = join(root, "missing", "workspace");
const missingWorkspace = await registry.openWorkspace(missingWorkspaceRoot);
assert.equal(missingWorkspace.workspace.root, missingWorkspaceRoot);
assert.equal(missingWorkspace.workspace.mode, "checkout");
assert.equal((await stat(missingWorkspaceRoot)).isDirectory(), true);
{
let mkdirCalls = 0;
const existingStats = await ensureCheckoutWorkspaceRoot(root, {
stat: async (path) => {
assert.equal(path, root);
return await stat(path);
},
mkdir: async () => {
mkdirCalls += 1;
},
});
assert.equal(existingStats.isDirectory(), true);
assert.equal(mkdirCalls, 0);
}
await assert.rejects(
() => registry.openWorkspace({ path: root, mode: "worktree" }),
(error: unknown) =>
error instanceof GitWorktreeError && error.code === "GIT_REPOSITORY_NOT_FOUND",
);
const gitRoot = join(root, "git-project");
await mkdir(gitRoot);
await writeFile(join(gitRoot, "AGENTS.md"), "git root instructions\n");
await writeFile(join(gitRoot, "README.md"), "hello\n");
await git(gitRoot, ["init"]);
await git(gitRoot, ["config", "user.email", "devspace@example.com"]);
await git(gitRoot, ["config", "user.name", "DevSpace Test"]);
await git(gitRoot, ["add", "."]);
await git(gitRoot, ["commit", "-m", "Initial commit"]);
await writeFile(join(gitRoot, "dirty.txt"), "not copied\n");
const worktreeWorkspace = await registry.openWorkspace({
path: gitRoot,
mode: "worktree",
});
assert.equal(worktreeWorkspace.workspace.mode, "worktree");
assert.notEqual(worktreeWorkspace.workspace.root, gitRoot);
assert.match(worktreeWorkspace.workspace.root, /git-project-[a-f0-9]{8}$/);
assert.equal(worktreeWorkspace.workspace.sourceRoot, gitRoot);
assert.equal(worktreeWorkspace.workspace.worktree?.baseRef, "HEAD");
assert.equal(worktreeWorkspace.workspace.worktree?.dirtySource, true);
assert.equal(worktreeWorkspace.workspace.worktree?.managed, true);
assert.equal((await stat(worktreeWorkspace.workspace.root)).isDirectory(), true);
assert.match(worktreeWorkspace.agentsFiles.map((file) => file.content).join("\n"), /global instructions/);
assert.match(worktreeWorkspace.agentsFiles.map((file) => file.content).join("\n"), /git root instructions/);
const worktreeReadmePath = registry.resolvePath(worktreeWorkspace.workspace, "README.md");
assert.equal(worktreeReadmePath.startsWith(worktreeWorkspace.workspace.root), true);
const stateDir = join(root, ".state");
const firstStore = new SqliteWorkspaceStore(stateDir);
const persistentRegistry = new WorkspaceRegistry(config, firstStore);
const persistentWorkspace = await persistentRegistry.openWorkspace(root);
const persistentWorktree = await persistentRegistry.openWorkspace({
path: gitRoot,
mode: "worktree",
});
const persistentWorktreeAgentsDir = join(
persistentWorktree.workspace.root,
".devspace",
"agents",
);
await mkdir(persistentWorktreeAgentsDir, { recursive: true });
await writeFile(
join(persistentWorktreeAgentsDir, "restored.md"),
[
"---",
"name: restored",
"description: Available after persisted workspace restoration.",
"provider: codex",
"---",
"",
"Restore worktree context.",
"",
].join("\n"),
);
firstStore.close();
const secondStore = new SqliteWorkspaceStore(stateDir);
const restoredRegistry = new WorkspaceRegistry(config, secondStore);
const restoredWorkspace = restoredRegistry.getWorkspace(persistentWorkspace.workspace.id);
assert.equal(restoredWorkspace.root, root);
assert.equal(restoredWorkspace.mode, "checkout");
const restoredWorktree = restoredRegistry.getWorkspace(persistentWorktree.workspace.id);
assert.equal(restoredWorktree.mode, "worktree");
assert.equal(restoredWorktree.sourceRoot, gitRoot);
assert.equal(restoredWorktree.root, persistentWorktree.workspace.root);
assert.equal(restoredWorktree.worktree?.managed, true);
secondStore.close();
const reopenedStore = new SqliteWorkspaceStore(stateDir);
const reopenedRegistry = new WorkspaceRegistry(config, reopenedStore);
try {
const reopenedWorktree = await reopenedRegistry.openWorkspace(persistentWorktree.workspace.root);
assert.equal(reopenedWorktree.workspace.id, persistentWorktree.workspace.id);
assert.equal(reopenedWorktree.workspace.mode, "worktree");
assert.equal(reopenedWorktree.workspace.sourceRoot, gitRoot);
assert.equal(reopenedWorktree.workspace.root, persistentWorktree.workspace.root);
assert.equal(reopenedWorktree.workspace.worktree?.managed, true);
assert.deepEqual(
reopenedWorktree.workspace.agentProfiles.map((profile) => profile.name),
["restored"],
);
await assert.rejects(
() => reopenedRegistry.openWorkspace(join(worktreeRoot, "unregistered-worktree")),
/Path is outside allowed roots/,
);
} finally {
reopenedStore.close();
}
if (platform() !== "win32") {
const aliasRoot = join(root, "alias-root");
await symlink(root, aliasRoot, "dir");
const aliasConfig = loadConfig({
DEVSPACE_ALLOWED_ROOTS: aliasRoot,
DEVSPACE_WORKTREE_ROOT: join(aliasRoot, ".devspace", "alias-worktrees"),
DEVSPACE_AGENT_DIR: agentDir,
DEVSPACE_OAUTH_OWNER_TOKEN: "test-owner-token-that-is-long-enough",
PORT: "1",
});
const aliasWorkspace = await new WorkspaceRegistry(aliasConfig).openWorkspace({
path: join(aliasRoot, "git-project"),
mode: "worktree",
});
assert.equal(aliasWorkspace.workspace.sourceRoot, join(aliasRoot, "git-project"));
const aliasCheckout = await new WorkspaceRegistry(aliasConfig).openWorkspace(aliasRoot);
assert.deepEqual(
aliasCheckout.agentsFiles.map((file) => file.content),
["global instructions\n", "root instructions\n"],
);
}
} finally {
await rm(root, { recursive: true, force: true });
await rm(outsideRoot, { recursive: true, force: true });
await rm(worktreeRoot, { recursive: true, force: true });
}
async function git(cwd: string, args: string[]): Promise<void> {
await execFileAsync("git", args, { cwd });
}