-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.test.ts
More file actions
204 lines (180 loc) · 7.18 KB
/
Copy pathcli.test.ts
File metadata and controls
204 lines (180 loc) · 7.18 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
import { describe, expect, it, beforeAll, afterAll } from "vitest";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { execFileSync } from "node:child_process";
import { fileURLToPath } from "node:url";
import { bootstrapHarness } from "../src/bootstrap.js";
import { executeSlashCommand, SLASH_COMMAND_NAMES } from "../src/tui/slash.js";
import type { SlashContext } from "../src/tui/slash.js";
const projectRoot = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"../fixtures/sample-project",
);
let context!: SlashContext;
let oldHome: string | undefined;
beforeAll(async () => {
oldHome = process.env.TINYCODE_HOME;
process.env.TINYCODE_HOME = fs.mkdtempSync(path.join(os.tmpdir(), "tc-slash-home-"));
const harness = await bootstrapHarness({
projectRoot,
config: { permissionMode: "auto" },
mock: true,
session: { mode: "new" },
});
context = {
runtime: harness.runtime,
models: harness.models,
permissions: harness.permissions,
session: harness.session,
skills: harness.skills,
mcp: harness.mcp,
subAgents: harness.subAgents,
projectRoot,
loadSession: async (id: string) => [`resumed ${id}`],
startNewSession: () => harness.session!.start(projectRoot, "mock/tinycode-mock"),
requestExit: () => {},
};
});
afterAll(() => {
if (oldHome === undefined) delete process.env.TINYCODE_HOME;
else process.env.TINYCODE_HOME = oldHome;
});
describe("slash commands", () => {
it("exposes the full required command set", () => {
for (const name of ["/help", "/new", "/clear", "/resume", "/sessions", "/model", "/skills", "/mcp", "/agents", "/compact", "/status", "/exit"]) {
expect(SLASH_COMMAND_NAMES, name).toContain(name);
}
});
it("/help lists every command with a description", async () => {
const lines = await executeSlashCommand("/help", context);
const text = lines.join("\n");
expect(text).toContain("/help");
expect(text).toContain("/compact");
expect(text).toContain("/mcp");
});
it("/skills lists discovered fixture skills", async () => {
const lines = await executeSlashCommand("/skills", context);
expect(lines.join("\n")).toContain("code-review");
});
it("/status reports model, tools, permissions and session", async () => {
const lines = await executeSlashCommand("/status", context);
const text = lines.join("\n");
expect(text).toContain(`project root : ${projectRoot}`);
expect(text).toContain("model : mock/tinycode-mock");
expect(text).toContain("permissions : mode=auto");
expect(text).toContain("tools : read");
});
it("/sessions lists the live session and /new starts another", async () => {
await context.runtime.prompt("first prompt");
const list = await executeSlashCommand("/sessions", context);
expect(list.length).toBeGreaterThan(0);
const before = context.session!.id!;
const lines = await executeSlashCommand("/new", context);
expect(lines[0]).toMatch(/Started new session/);
expect(context.session!.id!).not.toBe(before);
});
it("/resume loads a previous session into the runtime transcript", async () => {
const target = context.session!.id!;
context.runtime.agent.reset();
const lines = await context.loadSession(target);
expect(lines.join(" ")).toContain(target);
});
it("/model lists mock and switches back by ref", async () => {
const listing = (await executeSlashCommand("/model", context)).join("\n");
expect(listing).toContain("current:");
// The mock provider has auth configured implicitly.
const switchResult = (await executeSlashCommand("/model mock/tinycode-mock", context)).join("\n");
expect(switchResult.toLowerCase()).toContain("switched");
});
it("/agents reports none spawned; /mcp reports unconfigured", async () => {
const agents = await executeSlashCommand("/agents", context);
expect(agents[0]).toContain("SUB-AGENTS 0/3 RUNNING");
const mcp = await executeSlashCommand("/mcp", context);
expect(mcp.join(" ")).toContain("not configured");
});
it("/compact works on an empty conversation without crashing", async () => {
context.runtime.agent.reset();
const lines = await executeSlashCommand("/compact", context);
expect(lines.length).toBeGreaterThan(0);
});
it("unknown commands are rejected with guidance", async () => {
const lines = await executeSlashCommand("/frobnicate", context);
expect(lines[0]).toContain("Unknown command");
expect(lines[0]).toContain("/help");
});
it("/exit requests shutdown", async () => {
let exited = false;
const exiting: SlashContext = { ...context, requestExit: () => (exited = true) };
await executeSlashCommand("/exit", exiting);
expect(exited).toBe(true);
});
});
describe("CLI smoke (built dist)", () => {
const projectRootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const cli = path.join(projectRootDir, "dist", "cli", "index.js");
// `npm test` must work on a fresh clone without a prior `npm run build`.
beforeAll(() => {
if (!fs.existsSync(cli)) {
execFileSync("npx", ["tsc", "-p", "tsconfig.build.json"], { cwd: projectRootDir, stdio: "pipe" });
}
expect(fs.existsSync(cli)).toBe(true);
});
function run(args: string[], opts: { cwd?: string; env?: Record<string, string> } = {}): string {
return execFileSync(process.execPath, [cli, ...args], {
cwd: opts.cwd ?? projectRoot,
env: { ...process.env, TINYCODE_HOME: os.tmpdir() + "/tc-cli-home", ...opts.env },
encoding: "utf8",
timeout: 60000,
stdio: ["ignore", "pipe", "pipe"],
});
}
it("--version prints the version", () => {
expect(run(["--version"])).toMatch(/^tinycode \d+\.\d+\.\d+/);
});
it("--help prints usage with all documented flags", () => {
const help = run(["--help"]);
for (const flag of ["--continue", "--session", "--model", "--permission-mode", "-p", "--list-models"]) {
expect(help).toContain(flag);
}
});
it("-p runs one-shot with the mock model", () => {
const out = run(["-p", "describe this project"], { env: { TINYCODE_MODEL: "mock" }, cwd: os.tmpdir() });
expect(out).toContain("TinyCode mock model");
});
it("errors gracefully without any API key or mock", () => {
let failed = false;
try {
run(["-p", "hello"], {
cwd: os.tmpdir(),
env: {
TINYCODE_MODEL: "",
ANTHROPIC_API_KEY: "",
OPENAI_API_KEY: "",
GROQ_API_KEY: "",
GOOGLE_API_KEY: "",
XAI_API_KEY: "",
MISTRAL_API_KEY: "",
DEEPSEEK_API_KEY: "",
OPENROUTER_API_KEY: "",
},
});
} catch (error) {
failed = true;
const output = String((error as { stderr?: Buffer }).stderr ?? error);
expect(output).toContain("No API key found");
}
expect(failed).toBe(true);
});
it("rejects unknown flags with exit code 2-style message", () => {
let failed = false;
try {
run(["--definitely-not-a-flag"]);
} catch (error) {
failed = true;
const output = String((error as { stderr?: Buffer }).stderr ?? error);
expect(output).toContain("Unknown option");
}
expect(failed).toBe(true);
});
});