-
Notifications
You must be signed in to change notification settings - Fork 520
Expand file tree
/
Copy pathdoctor-script.test.ts
More file actions
179 lines (162 loc) · 6.31 KB
/
doctor-script.test.ts
File metadata and controls
179 lines (162 loc) · 6.31 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
import { describe, it, expect } from "vitest";
import {
chmodSync,
existsSync,
mkdtempSync,
mkdirSync,
readFileSync,
rmSync,
utimesSync,
writeFileSync,
} from "node:fs";
import { dirname, join, resolve } from "node:path";
import { tmpdir } from "node:os";
import { spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";
const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "../../../..");
const scriptPath = join(repoRoot, "scripts", "ao-doctor.sh");
function writeExecutable(path: string, content: string): void {
writeFileSync(path, content);
chmodSync(path, 0o755);
}
function createFakeBinary(binDir: string, name: string, body: string): void {
writeExecutable(join(binDir, name), `#!/bin/bash\nset -e\n${body}\n`);
}
function createHealthyRepo(tempRoot: string): string {
const fakeRepo = join(tempRoot, "repo");
mkdirSync(join(fakeRepo, "node_modules"), { recursive: true });
mkdirSync(join(fakeRepo, "packages", "core", "dist"), { recursive: true });
mkdirSync(join(fakeRepo, "packages", "cli", "dist"), { recursive: true });
mkdirSync(join(fakeRepo, "packages", "agent-orchestrator", "bin"), { recursive: true });
mkdirSync(join(fakeRepo, "packages", "web"), { recursive: true });
writeFileSync(join(fakeRepo, "packages", "core", "dist", "index.js"), "export {};\n");
writeFileSync(join(fakeRepo, "packages", "cli", "dist", "index.js"), "export {};\n");
writeFileSync(
join(fakeRepo, "packages", "agent-orchestrator", "bin", "ao.js"),
'#!/usr/bin/env node\nconsole.log("0.1.0");\n',
);
chmodSync(join(fakeRepo, "packages", "agent-orchestrator", "bin", "ao.js"), 0o755);
return fakeRepo;
}
function createHealthyPath(binDir: string): void {
createFakeBinary(
binDir,
"node",
'if [ "$1" = "--version" ]; then\n printf "v20.11.1\\n"\n exit 0\nfi\nexit 0',
);
createFakeBinary(
binDir,
"git",
'if [ "$1" = "--version" ]; then\n printf "git version 2.43.0\\n"\n exit 0\nfi\nexit 0',
);
createFakeBinary(
binDir,
"pnpm",
'if [ "$1" = "--version" ]; then\n printf "9.15.4\\n"\n exit 0\nfi\nexit 0',
);
createFakeBinary(
binDir,
"npm",
'if [ "$1" = "bin" ]; then\n printf "/tmp/npm-bin\\n"\n exit 0\nfi\nexit 0',
);
createFakeBinary(
binDir,
"tmux",
'if [ "$1" = "-V" ]; then\n printf "tmux 3.4\\n"\n exit 0\nfi\nif [ "$1" = "list-sessions" ]; then\n exit 1\nfi\nexit 0',
);
createFakeBinary(
binDir,
"gh",
'if [ "$1" = "--version" ]; then\n printf "gh version 2.50.0\\n"\n exit 0\nfi\nif [ "$1" = "auth" ] && [ "$2" = "status" ]; then\n exit 0\nfi\nexit 0',
);
createFakeBinary(binDir, "ao", 'printf "/fake/ao\\n" >/dev/null\nexit 0');
}
describe("scripts/ao-doctor.sh", () => {
it("reports a healthy install as PASS", () => {
const tempRoot = mkdtempSync(join(tmpdir(), "ao-doctor-script-"));
const fakeRepo = createHealthyRepo(tempRoot);
const binDir = join(tempRoot, "bin");
mkdirSync(binDir, { recursive: true });
createHealthyPath(binDir);
const configPath = join(tempRoot, "agent-orchestrator.yaml");
const dataDir = join(tempRoot, "data");
const worktreeDir = join(tempRoot, "worktrees");
mkdirSync(dataDir, { recursive: true });
mkdirSync(worktreeDir, { recursive: true });
writeFileSync(
configPath,
[`dataDir: ${dataDir}`, `worktreeDir: ${worktreeDir}`, "projects: {}"].join("\n"),
);
const result = spawnSync("bash", [scriptPath], {
env: {
...process.env,
PATH: `${binDir}:/usr/bin:/bin`,
AO_REPO_ROOT: fakeRepo,
AO_CONFIG_PATH: configPath,
},
encoding: "utf8",
});
rmSync(tempRoot, { recursive: true, force: true });
expect(result.status).toBe(0);
expect(result.stdout).toContain("PASS");
expect(result.stdout).toContain("Environment looks healthy");
});
it("applies safe fixes for missing launcher, missing dirs, and stale temp files", () => {
const tempRoot = mkdtempSync(join(tmpdir(), "ao-doctor-fix-"));
const fakeRepo = createHealthyRepo(tempRoot);
const binDir = join(tempRoot, "bin");
mkdirSync(binDir, { recursive: true });
createHealthyPath(binDir);
rmSync(join(binDir, "ao"), { force: true });
const npmLog = join(tempRoot, "npm.log");
createFakeBinary(
binDir,
"npm",
`printf '%s\\n' "$*" >> ${JSON.stringify(npmLog)}\nif [ "$1" = "bin" ]; then\n printf "/tmp/npm-bin\\n"\nfi\nexit 0`,
);
const configPath = join(tempRoot, "agent-orchestrator.yaml");
const dataDir = join(tempRoot, "data");
const worktreeDir = join(tempRoot, "worktrees");
const commentedDataDir = `${dataDir} # session metadata`;
const commentedWorktreeDir = `${worktreeDir} # ephemeral worktrees`;
writeFileSync(
configPath,
[`dataDir: ${commentedDataDir}`, `worktreeDir: ${commentedWorktreeDir}`, "projects: {}"].join(
"\n",
),
);
const tmpRoot = join(tempRoot, "tmp-root");
mkdirSync(tmpRoot, { recursive: true });
const staleFile = join(tmpRoot, "ao-stale.tmp");
writeFileSync(staleFile, "stale\n");
const oldTimestamp = new Date(Date.now() - 2 * 60 * 60 * 1000);
utimesSync(staleFile, oldTimestamp, oldTimestamp);
const result = spawnSync("bash", [scriptPath, "--fix"], {
env: {
...process.env,
PATH: `${binDir}:/usr/bin:/bin`,
AO_REPO_ROOT: fakeRepo,
AO_CONFIG_PATH: configPath,
AO_DOCTOR_TMP_ROOT: tmpRoot,
},
encoding: "utf8",
});
const npmCommands = readFileSync(npmLog, "utf8");
const staleStillExists = existsSync(staleFile);
const dataDirExists = existsSync(dataDir);
const worktreeDirExists = existsSync(worktreeDir);
const commentedDataDirExists = existsSync(commentedDataDir);
const commentedWorktreeDirExists = existsSync(commentedWorktreeDir);
rmSync(tempRoot, { recursive: true, force: true });
expect(result.status).toBe(0);
expect(result.stdout).toContain("FIXED");
expect(npmCommands).toContain("link");
expect(result.stdout).toContain("launcher");
expect(result.stdout).toContain("stale temp files");
expect(staleStillExists).toBe(false);
expect(dataDirExists).toBe(true);
expect(worktreeDirExists).toBe(true);
expect(commentedDataDirExists).toBe(false);
expect(commentedWorktreeDirExists).toBe(false);
});
});