Add pi CLI coverage with DeepSeek

This commit is contained in:
Patrick Wozniak
2026-05-05 13:22:15 +02:00
parent 145e2678b6
commit 3630b85298
4 changed files with 4606 additions and 61 deletions
+4234
View File
File diff suppressed because it is too large Load Diff
+15 -4
View File
@@ -11,15 +11,26 @@
], ],
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"test": "npx tsx tests/test-pure-functions.ts && npx tsx tests/test-abort.ts && npx tsx tests/test-stream.ts", "test": "npm run typecheck && tsx tests/test-pure-functions.ts && tsx tests/test-abort.ts && tsx tests/test-stream.ts && node tests/test-pi-local.mjs",
"test:unit": "npx tsx tests/test-pure-functions.ts", "typecheck": "tsc --noEmit --target ES2022 --module NodeNext --moduleResolution NodeNext --allowImportingTsExtensions --skipLibCheck --types node src/core.ts tests/*.ts",
"test:abort": "npx tsx tests/test-abort.ts", "test:unit": "tsx tests/test-pure-functions.ts",
"test:stream": "npx tsx tests/test-stream.ts", "test:abort": "tsx tests/test-abort.ts",
"test:stream": "tsx tests/test-stream.ts",
"test:pi-local": "node tests/test-pi-local.mjs",
"test:smoke": "node tests/test-smoke.mjs" "test:smoke": "node tests/test-smoke.mjs"
}, },
"pi": { "pi": {
"extensions": [ "extensions": [
"./index.ts" "./index.ts"
] ]
},
"devDependencies": {
"@mariozechner/pi-coding-agent": "0.72.0",
"@types/node": "25.6.0",
"tsx": "4.21.0",
"typescript": "6.0.3"
},
"dependencies": {
"@mariozechner/pi-ai": "0.72.0"
} }
} }
+259
View File
@@ -0,0 +1,259 @@
#!/usr/bin/env node
/**
* Local end-to-end test: loads the real extension through the pi CLI while the
* Command Code API is replaced by a deterministic local mock server.
*/
import assert from "node:assert/strict";
import { spawn, spawnSync } from "node:child_process";
import { accessSync, constants, existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { createServer } from "node:http";
import { homedir, tmpdir } from "node:os";
import { delimiter, dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = dirname(fileURLToPath(import.meta.url));
const PROJECT_DIR = resolve(__dirname, "..");
const EXT_PATH = resolve(PROJECT_DIR, "index.ts");
const TEST_MODEL = "deepseek/deepseek-v4-flash";
function findPiBinary() {
if (process.env.PI_BIN) return process.env.PI_BIN;
const localBin = resolve(PROJECT_DIR, "node_modules", ".bin");
const candidates = (process.env.PATH ?? "")
.split(delimiter)
.map((entry) => resolve(entry, "pi"))
.filter((candidate) => !candidate.startsWith(localBin));
for (const candidate of candidates) {
try {
accessSync(candidate, constants.X_OK);
return candidate;
} catch {
// Try next PATH entry.
}
}
return undefined;
}
const PI_BIN = findPiBinary();
if (!PI_BIN) {
console.log("[pi-local] SKIP — pi is not on PATH");
process.exit(0);
}
const piCheck = spawnSync(PI_BIN, ["--help"], { stdio: "ignore" });
if (piCheck.error) {
console.log(`[pi-local] SKIP — pi failed to start: ${piCheck.error.message}`);
process.exit(0);
}
let requestCount = 0;
let lastRequestBody;
let lastRequestHeaders = {};
const server = createServer((req, res) => {
if (req.method !== "POST" || req.url !== "/alpha/generate") {
res.writeHead(404);
res.end("Not found");
return;
}
requestCount += 1;
lastRequestHeaders = Object.fromEntries(Object.entries(req.headers).map(([key, value]) => [key, Array.isArray(value) ? value.join(", ") : value ?? ""]));
let body = "";
req.on("data", (chunk) => {
body += chunk.toString("utf-8");
});
req.on("end", () => {
try {
lastRequestBody = JSON.parse(body);
} catch {
lastRequestBody = undefined;
}
res.writeHead(200, {
"Content-Type": "text/plain; charset=utf-8",
"Transfer-Encoding": "chunked",
});
res.write(`${JSON.stringify({ type: "text-delta", text: "mock-pi-ok" })}\n`);
res.write(`${JSON.stringify({ type: "finish", finishReason: "stop", totalUsage: { inputTokens: 1, outputTokens: 1 } })}\n`);
res.end();
});
});
await new Promise((resolve) => server.listen(0, resolve));
const address = server.address();
const port = typeof address === "object" && address ? address.port : 0;
const apiBase = `http://127.0.0.1:${port}`;
function hasLivePiAuth() {
return !!process.env.COMMANDCODE_API_KEY ||
existsSync(join(homedir(), ".commandcode", "auth.json")) ||
existsSync(join(homedir(), ".pi", "agent", "auth.json"));
}
let tempHome;
const env = {
...process.env,
COMMANDCODE_API_BASE: apiBase,
};
if (hasLivePiAuth()) {
console.log("[pi-local] using live pi auth");
} else {
console.log("[pi-local] live pi auth not found; using mock auth fallback");
tempHome = mkdtempSync(join(tmpdir(), "pi-cc-home-"));
mkdirSync(join(tempHome, ".commandcode"), { recursive: true });
writeFileSync(join(tempHome, ".commandcode", "auth.json"), JSON.stringify({ apiKey: "mock-key" }));
env.HOME = tempHome;
env.USERPROFILE = tempHome;
env.COMMANDCODE_API_KEY = "mock-key";
}
function runPi(args, timeoutMs = 30_000) {
return new Promise((resolve) => {
const child = spawn(PI_BIN, args, {
cwd: PROJECT_DIR,
env,
stdio: ["ignore", "pipe", "pipe"],
});
let stdout = "";
let stderr = "";
const timer = setTimeout(() => {
child.kill();
resolve({ code: -1, stdout, stderr: `${stderr}\nTIMEOUT after ${timeoutMs}ms` });
}, timeoutMs);
child.stdout.on("data", (chunk) => {
stdout += chunk.toString("utf-8");
});
child.stderr.on("data", (chunk) => {
stderr += chunk.toString("utf-8");
});
child.on("close", (code) => {
clearTimeout(timer);
resolve({ code, stdout, stderr });
});
});
}
async function runRpcQuery(timeoutMs = 30_000) {
const child = spawn(PI_BIN, [
"--mode", "rpc",
"-e", EXT_PATH,
"--provider", "commandcode",
"--model", TEST_MODEL,
], {
cwd: PROJECT_DIR,
env,
stdio: ["pipe", "pipe", "pipe"],
});
let stdout = "";
let stderr = "";
let buffer = "";
let sawPromptAccepted = false;
let sawAssistantMessage = false;
let sawTextDelta = false;
const events = [];
const done = new Promise((resolve) => {
const timer = setTimeout(() => {
child.kill();
resolve(false);
}, timeoutMs);
const finish = (ok) => {
clearTimeout(timer);
try {
child.stdin.write(`${JSON.stringify({ type: "quit" })}\n`);
} catch {
// ignore shutdown race
}
child.kill();
resolve(ok);
};
child.stdin.write(`${JSON.stringify({ id: "prompt-1", type: "prompt", message: "say mock token" })}\n`);
child.stdout.on("data", (chunk) => {
const text = chunk.toString("utf-8");
stdout += text;
buffer += text;
const lines = buffer.split("\n");
buffer = lines.pop() ?? "";
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed) continue;
try {
const event = JSON.parse(trimmed);
events.push(event);
if (event.type === "response" && event.id === "prompt-1" && event.success === true) {
sawPromptAccepted = true;
}
if (event.type === "message_update" && event.assistantMessageEvent?.type === "text_delta") {
sawTextDelta = true;
}
if (event.type === "message_end" && event.message?.role === "assistant") {
sawAssistantMessage = true;
finish(true);
}
} catch {
// ignore non-JSON output
}
}
});
child.stderr.on("data", (chunk) => {
stderr += chunk.toString("utf-8");
});
child.on("close", () => {
if (!sawAssistantMessage) finish(false);
});
});
const ok = await done;
return { ok, stdout, stderr, events, sawPromptAccepted, sawAssistantMessage, sawTextDelta };
}
try {
console.log("[pi-local] list models through real extension");
const list = await runPi(["-e", EXT_PATH, "--list-models"], 20_000);
assert.equal(list.code, 0, list.stderr);
assert.match(list.stdout, /commandcode/);
assert.match(list.stdout, /deepseek\/deepseek-v4-flash/);
console.log("[pi-local] print mode through real extension and mock API");
requestCount = 0;
const print = await runPi([
"-e", EXT_PATH,
"-p", "say mock token",
"--provider", "commandcode",
"--model", TEST_MODEL,
], 30_000);
assert.equal(print.code, 0, print.stderr);
assert.match(print.stdout, /mock-pi-ok/);
assert.equal(requestCount, 1);
assert.ok(
typeof lastRequestHeaders.authorization === "string" && lastRequestHeaders.authorization.startsWith("Bearer "),
"should send a bearer Authorization header",
);
assert.equal(lastRequestBody?.params?.model, TEST_MODEL);
console.log("[pi-local] RPC prompt through real extension and mock API");
requestCount = 0;
const rpc = await runRpcQuery();
assert.equal(
rpc.ok,
true,
JSON.stringify({ stderr: rpc.stderr, stdout: rpc.stdout, events: rpc.events.slice(-10) }, null, 2),
);
assert.equal(rpc.sawPromptAccepted, true);
assert.equal(rpc.sawAssistantMessage, true);
assert.equal(rpc.sawTextDelta, true);
assert.equal(requestCount, 1);
console.log("[pi-local] PASS");
} finally {
await new Promise((resolve) => server.close(resolve));
if (tempHome) rmSync(tempHome, { recursive: true, force: true });
}
+98 -57
View File
@@ -4,25 +4,55 @@
* Tests that the extension: * Tests that the extension:
* 1. Loads without crashing in print mode * 1. Loads without crashing in print mode
* 2. Registers the provider and models * 2. Registers the provider and models
* 3. Can complete a simple query (requires COMMANDCODE_API_KEY) * 3. Can complete a simple prompt (requires Command Code auth)
* *
* Run with: node tests/test-smoke.mjs * Run with: node tests/test-smoke.mjs
* Requires: pi on PATH, COMMANDCODE_API_KEY env var set (or test is skipped) * Requires: pi on PATH plus COMMANDCODE_API_KEY or live pi auth files.
*/ */
import { spawn } from "node:child_process"; import { spawn } from "node:child_process";
import { resolve, dirname } from "node:path"; import { accessSync, constants, existsSync } from "node:fs";
import { homedir } from "node:os";
import { delimiter, resolve, dirname, join } from "node:path";
import { fileURLToPath } from "node:url"; import { fileURLToPath } from "node:url";
const __dirname = dirname(fileURLToPath(import.meta.url)); const __dirname = dirname(fileURLToPath(import.meta.url));
const PROJECT_DIR = resolve(__dirname, ".."); const PROJECT_DIR = resolve(__dirname, "..");
const EXT_PATH = resolve(PROJECT_DIR, "index.ts"); const EXT_PATH = resolve(PROJECT_DIR, "index.ts");
const TEST_MODEL = "deepseek/deepseek-v4-flash";
function findPiBinary() {
if (process.env.PI_BIN) return process.env.PI_BIN;
const localBin = resolve(PROJECT_DIR, "node_modules", ".bin");
const candidates = (process.env.PATH ?? "")
.split(delimiter)
.map((entry) => resolve(entry, "pi"))
.filter((candidate) => !candidate.startsWith(localBin));
for (const candidate of candidates) {
try {
accessSync(candidate, constants.X_OK);
return candidate;
} catch {
// Try next PATH entry.
}
}
return undefined;
}
const PI_BIN = findPiBinary();
const HAS_PI = !!PI_BIN;
const PRINT_MODE_TIMEOUT = 120_000; // 2 minutes for print mode const PRINT_MODE_TIMEOUT = 120_000; // 2 minutes for print mode
const RPC_START_TIMEOUT = 15_000; const RPC_START_TIMEOUT = 15_000;
const RPC_QUERY_TIMEOUT = 60_000; const RPC_QUERY_TIMEOUT = 60_000;
const HAS_API_KEY = !!process.env.COMMANDCODE_API_KEY; function hasCommandCodeAuth() {
return !!process.env.COMMANDCODE_API_KEY ||
existsSync(join(homedir(), ".commandcode", "auth.json")) ||
existsSync(join(homedir(), ".pi", "agent", "auth.json"));
}
const HAS_AUTH = hasCommandCodeAuth();
let passed = 0; let passed = 0;
let failed = 0; let failed = 0;
@@ -45,21 +75,25 @@ function kill(child) {
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
async function runPrintMode() { async function runPrintMode() {
if (!HAS_API_KEY) { if (!HAS_AUTH) {
console.log("[smoke] SKIP — COMMANDCODE_API_KEY not set, skipping print mode test\n"); console.log("[smoke] SKIP — Command Code auth not found, skipping print mode test\n");
skipped++;
return;
}
if (!HAS_PI) {
console.log("[smoke] SKIP — pi is not on PATH, skipping print mode test\n");
skipped++; skipped++;
return; return;
} }
console.log(`[smoke] Running pi in print mode with extension: ${EXT_PATH}`); console.log(`[smoke] Running pi in print mode with extension: ${EXT_PATH}`);
console.log(`[smoke] pi -e ${EXT_PATH} -p "say hi" --provider commandcode --model claude-sonnet-4-6\n`); console.log(`[smoke] ${PI_BIN} -e ${EXT_PATH} -p "say hi" --provider commandcode --model ${TEST_MODEL}\n`);
const child = spawn("pi", [ const child = spawn(PI_BIN, [
"-e", EXT_PATH, "-e", EXT_PATH,
"-p", "say hi in one word", "-p", "say hi in one word",
"--provider", "commandcode", "--provider", "commandcode",
"--model", "claude-sonnet-4-6", "--model", TEST_MODEL,
"--thinking-level", "minimal",
], { ], {
env: { ...process.env }, env: { ...process.env },
stdio: ["ignore", "pipe", "pipe"], stdio: ["ignore", "pipe", "pipe"],
@@ -101,15 +135,20 @@ async function runPrintMode() {
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
async function runListModels() { async function runListModels() {
if (!HAS_API_KEY) { if (!HAS_AUTH) {
console.log("[smoke] SKIP — no API key, skipping model list test\n"); console.log("[smoke] SKIP — Command Code auth not found, skipping model list test\n");
skipped++;
return;
}
if (!HAS_PI) {
console.log("[smoke] SKIP — pi is not on PATH, skipping model list test\n");
skipped++; skipped++;
return; return;
} }
console.log(`[smoke] Checking that models are discoverable via pi --list-models\n`); console.log(`[smoke] Checking that models are discoverable via pi --list-models\n`);
const child = spawn("pi", [ const child = spawn(PI_BIN, [
"-e", EXT_PATH, "-e", EXT_PATH,
"--list-models", "--list-models",
], { ], {
@@ -146,20 +185,25 @@ async function runListModels() {
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
// Test 3: RPC mode — extension loads, session starts // Test 3: RPC mode — extension loads and answers get_state
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
async function runRpcStartup() { async function runRpcStartup() {
if (!HAS_API_KEY) { if (!HAS_AUTH) {
console.log("[smoke] SKIP — no API key, skipping RPC startup test\n"); console.log("[smoke] SKIP — Command Code auth not found, skipping RPC startup test\n");
skipped++;
return;
}
if (!HAS_PI) {
console.log("[smoke] SKIP — pi is not on PATH, skipping RPC startup test\n");
skipped++; skipped++;
return; return;
} }
console.log(`[smoke] Testing RPC mode startup with extension\n`); console.log(`[smoke] Testing RPC mode startup with extension\n`);
console.log(`[smoke] pi --mode rpc -e ${EXT_PATH}\n`); console.log(`[smoke] ${PI_BIN} --mode rpc -e ${EXT_PATH}\n`);
const child = spawn("pi", [ const child = spawn(PI_BIN, [
"--mode", "rpc", "--mode", "rpc",
"-e", EXT_PATH, "-e", EXT_PATH,
], { ], {
@@ -167,7 +211,7 @@ async function runRpcStartup() {
stdio: ["pipe", "pipe", "pipe"], stdio: ["pipe", "pipe", "pipe"],
}); });
let sawSessionStart = false; let sawStateResponse = false;
let sawError = false; let sawError = false;
const events = []; const events = [];
@@ -182,9 +226,9 @@ async function runRpcStartup() {
try { try {
const msg = JSON.parse(trimmed); const msg = JSON.parse(trimmed);
events.push(msg); events.push(msg);
if (msg.type === "session_start") { if (msg.type === "response" && msg.id === "state-1" && msg.command === "get_state" && msg.success === true) {
sawSessionStart = true; sawStateResponse = true;
console.log("[smoke] RPC received session_start event"); console.log("[smoke] RPC received get_state response");
} }
if (msg.type === "error" || msg.type === "fatal") { if (msg.type === "error" || msg.type === "fatal") {
sawError = true; sawError = true;
@@ -197,12 +241,14 @@ async function runRpcStartup() {
}); });
const result = new Promise((resolve) => { const result = new Promise((resolve) => {
child.stdin.write(JSON.stringify({ id: "state-1", type: "get_state" }) + "\n");
const timer = setTimeout(async () => { const timer = setTimeout(async () => {
if (sawSessionStart) { if (sawStateResponse) {
console.log("[smoke] PASS — extension loaded, session started in RPC mode"); console.log("[smoke] PASS — extension loaded and RPC get_state works");
resolve(true); resolve(true);
} else { } else {
console.log("[smoke] FAIL — session_start not received within 5s"); console.log("[smoke] FAIL — get_state response not received");
resolve(false); resolve(false);
} }
// Send quit // Send quit
@@ -212,8 +258,8 @@ async function runRpcStartup() {
child.on("close", (code) => { child.on("close", (code) => {
clearTimeout(timer); clearTimeout(timer);
if (!sawSessionStart && !sawError) { if (!sawStateResponse && !sawError) {
console.log(`[smoke] FAIL — pi exited with code ${code} before session_start`); console.log(`[smoke] FAIL — pi exited with code ${code} before get_state response`);
resolve(false); resolve(false);
} }
}); });
@@ -225,33 +271,36 @@ async function runRpcStartup() {
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
// Test 4: RPC mode — send query and receive assistant message // Test 4: RPC mode — send prompt and receive assistant message
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
async function runRpcQuery() { async function runRpcQuery() {
if (!HAS_API_KEY) { if (!HAS_AUTH) {
console.log("[smoke] SKIP — no API key, skipping RPC query test\n"); console.log("[smoke] SKIP — Command Code auth not found, skipping RPC prompt test\n");
skipped++;
return;
}
if (!HAS_PI) {
console.log("[smoke] SKIP — pi is not on PATH, skipping RPC prompt test\n");
skipped++; skipped++;
return; return;
} }
console.log(`[smoke] Testing RPC query flow\n`); console.log(`[smoke] Testing RPC prompt flow\n`);
console.log(`[smoke] pi --mode rpc -e ${EXT_PATH}query "say hi" → expect response\n`); console.log(`[smoke] pi --mode rpc -e ${EXT_PATH}prompt "say hi" → expect response\n`);
const child = spawn("pi", [ const child = spawn(PI_BIN, [
"--mode", "rpc", "--mode", "rpc",
"-e", EXT_PATH, "-e", EXT_PATH,
"--provider", "commandcode", "--provider", "commandcode",
"--model", "claude-sonnet-4-6", "--model", TEST_MODEL,
"--thinking-level", "minimal",
], { ], {
env: { ...process.env }, env: { ...process.env },
stdio: ["pipe", "pipe", "pipe"], stdio: ["pipe", "pipe", "pipe"],
}); });
let sawSessionStart = false; let sawPromptAccepted = false;
let sawAssistantMessage = false; let sawAssistantMessage = false;
let querySent = false;
let buf = ""; let buf = "";
child.stdout.on("data", (chunk) => { child.stdout.on("data", (chunk) => {
@@ -263,23 +312,12 @@ async function runRpcQuery() {
if (!trimmed) continue; if (!trimmed) continue;
try { try {
const msg = JSON.parse(trimmed); const msg = JSON.parse(trimmed);
if (msg.type === "session_start") { if (msg.type === "response" && msg.id === "prompt-1" && msg.command === "prompt" && msg.success === true) {
sawSessionStart = true; sawPromptAccepted = true;
// Now send a query
if (!querySent) {
querySent = true;
const query = {
type: "query",
query: "say hi in one word",
sessionId: msg.sessionId,
};
child.stdin.write(JSON.stringify(query) + "\n");
console.log("[smoke] Sent RPC query");
}
} }
if (msg.type === "assistant_message") { if (msg.type === "message_end" && msg.message?.role === "assistant") {
sawAssistantMessage = true; sawAssistantMessage = true;
console.log("[smoke] PASS — received assistant_message in RPC mode"); console.log("[smoke] PASS — received assistant message_end in RPC mode");
} }
} catch { } catch {
// ignore // ignore
@@ -288,12 +326,15 @@ async function runRpcQuery() {
}); });
const result = new Promise((resolve) => { const result = new Promise((resolve) => {
child.stdin.write(JSON.stringify({ id: "prompt-1", type: "prompt", message: "say hi in one word" }) + "\n");
console.log("[smoke] Sent RPC prompt");
const timer = setTimeout(() => { const timer = setTimeout(() => {
if (sawAssistantMessage) { if (sawPromptAccepted && sawAssistantMessage) {
console.log("[smoke] PASS — full RPC query/response cycle works"); console.log("[smoke] PASS — full RPC prompt/response cycle works");
resolve(true); resolve(true);
} else { } else {
console.log("[smoke] WARN — no assistant_message received (may still be streaming)"); console.log("[smoke] WARN — no assistant message_end received (may still be streaming)");
resolve(false); resolve(false);
} }
try { child.stdin.write(JSON.stringify({ type: "quit" }) + "\n"); } catch {} try { child.stdin.write(JSON.stringify({ type: "quit" }) + "\n"); } catch {}
@@ -303,7 +344,7 @@ async function runRpcQuery() {
child.on("close", (code) => { child.on("close", (code) => {
clearTimeout(timer); clearTimeout(timer);
if (!sawAssistantMessage) { if (!sawAssistantMessage) {
console.log(`[smoke] FAIL — pi exited before assistant_message`); console.log(`[smoke] FAIL — pi exited before assistant message_end`);
resolve(false); resolve(false);
} }
}); });
@@ -321,7 +362,7 @@ async function runRpcQuery() {
console.log("=".repeat(60)); console.log("=".repeat(60));
console.log(" pi-commandcode-provider Integration Smoke Test"); console.log(" pi-commandcode-provider Integration Smoke Test");
console.log("=".repeat(60)); console.log("=".repeat(60));
console.log(` API key: ${HAS_API_KEY ? "✓ found" : "✗ not set (tests will be skipped)"}`); console.log(` Auth: ${HAS_AUTH ? "✓ found" : "✗ not found (tests will be skipped)"}`);
console.log(` Extension: ${EXT_PATH}`); console.log(` Extension: ${EXT_PATH}`);
console.log("=".repeat(60)); console.log("=".repeat(60));
console.log(""); console.log("");