-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.test.js
More file actions
178 lines (168 loc) · 6.07 KB
/
Copy pathcli.test.js
File metadata and controls
178 lines (168 loc) · 6.07 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
"use strict";
const assert = require("node:assert/strict");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const { describe, it } = require("node:test");
const { parseArgs, pathsFor, run } = require("../src/cli");
const {
createHeadlessRuntime,
createProcessLock,
defaultUserData,
} = require("../src/lib/headless-runtime");
const { publicModelOptions, runFirstSetup } = require("../src/cli/setup");
const { verifyPassword } = require("../src/lib/password");
function memoryOutput() {
let value = "";
return {
isTTY: false,
write(chunk) {
value += chunk;
return true;
},
value: () => value,
};
}
describe("ReRouted CLI", () => {
it("builds route choices by provider and model instead of by account", () => {
const options = publicModelOptions({
providers: [
{
id: "prov_chatgpt_one",
type: "chatgpt",
name: "ChatGPT Plus",
enabled: true,
models: [{ id: "gpt-5.6", name: "GPT 5.6", enabled: true }],
},
{
id: "prov_chatgpt_two",
type: "codex",
name: "ChatGPT Team",
enabled: true,
models: [{ id: "gpt-5.6", name: "GPT 5.6", enabled: true }],
},
{
id: "prov_lab",
type: "openai-compat",
name: "Local Lab",
enabled: true,
models: [{ id: "lab-model", name: "Lab model", enabled: true }],
},
],
});
assert.deepEqual(options, [
{ label: "ChatGPT: GPT 5.6", providerType: "chatgpt", model: "gpt-5.6" },
{ label: "Local Lab: Lab model", providerId: "prov_lab", model: "lab-model" },
]);
});
it("parses start options and validates network boundaries", () => {
assert.deepEqual(parseArgs(["--host", "localhost", "--port", "5050", "--no-interactive"]), {
command: "start",
host: "127.0.0.1",
port: 5050,
dataDir: null,
interactive: false,
});
assert.throws(() => parseArgs(["--host", "192.168.1.5"]), /--host must be/);
assert.throws(() => parseArgs(["--port", "70000"]), /--port must be/);
assert.throws(() => parseArgs(["--port"]), /requires a number/);
assert.throws(() => parseArgs(["--data-dir"]), /requires a path/);
assert.throws(() => parseArgs(["--wat"]), /Unknown option/);
});
it("uses the XDG config directory on Linux and exposes stable data paths", () => {
const data = defaultUserData({
platform: "linux",
env: { XDG_CONFIG_HOME: "/tmp/xdg" },
homedir: "/home/dev",
});
assert.equal(data, "/tmp/xdg/rerouted");
assert.deepEqual(pathsFor(data), {
data,
config: "/tmp/xdg/rerouted/config.json",
usage: "/tmp/xdg/rerouted/usage.sqlite",
logs: "/tmp/xdg/rerouted/rerouted.log",
});
});
it("runs the real gateway and dashboard in non-interactive mode", async () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rerouted-cli-"));
const output = memoryOutput();
const error = memoryOutput();
try {
const code = await run(
["--data-dir", root, "--host", "127.0.0.1", "--port", "0", "--no-interactive"],
{
input: { isTTY: false },
output,
error,
waitForSignal: false,
}
);
assert.equal(code, 0, error.value());
assert.match(output.value(), /Gateway\s+http:\/\/127\.0\.0\.1:\d+\/v1/);
assert.match(output.value(), /Dashboard http:\/\/127\.0\.0\.1:\d+\/dashboard\//);
assert.match(output.value(), /First-time setup is waiting/);
assert.equal(fs.existsSync(path.join(root, "config.json")), true);
assert.equal(fs.existsSync(path.join(root, "rerouted.pid")), false);
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});
it("completes first-run terminal setup without requiring a provider", async () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rerouted-setup-"));
const runtime = createHeadlessRuntime({ userData: root, version: "test" });
const output = memoryOutput();
const secrets = ["terminal-password", "terminal-password"];
const confirmations = [false, false];
const prompts = {
secret: async () => secrets.shift(),
confirm: async () => confirmations.shift(),
text: async () => "",
select: async () => 0,
multiSelect: async () => [],
};
try {
const completed = await runFirstSetup({
prompts,
controlPlane: runtime.controlPlane,
dashboardUrl: "http://127.0.0.1:4949/dashboard/",
output,
});
assert.equal(completed, true);
const cfg = runtime.store.load();
assert.equal(cfg.onboardingComplete, true);
assert.equal(cfg.onboardingStep, "done");
assert.equal(await verifyPassword("terminal-password", cfg.adminPasswordHash), true);
assert.match(output.value(), /Setup complete/);
assert.match(output.value(), /No provider models are available yet/);
} finally {
await runtime.close();
fs.rmSync(root, { recursive: true, force: true });
}
});
});
describe("headless process lock", () => {
it("rejects a live duplicate and cleans up the owning lock", () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rerouted-lock-"));
try {
const first = createProcessLock(root);
assert.throws(() => createProcessLock(root), (error) => error.code === "ALREADY_RUNNING");
first.release();
const second = createProcessLock(root);
second.release();
assert.equal(fs.existsSync(path.join(root, "rerouted.pid")), false);
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});
it("replaces a stale PID lock", () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rerouted-lock-"));
try {
fs.writeFileSync(path.join(root, "rerouted.pid"), "999999999\n", { mode: 0o600 });
const lock = createProcessLock(root);
assert.equal(Number(fs.readFileSync(lock.path, "utf8").trim()), process.pid);
lock.release();
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});
});