Test real Command Code core behavior
This commit is contained in:
+179
-590
@@ -1,611 +1,180 @@
|
||||
/**
|
||||
* Unit tests for pi-commandcode-provider pure functions.
|
||||
*
|
||||
* These tests DON'T require pi's runtime or any network access.
|
||||
* They verify message/tool/schema conversion logic in isolation.
|
||||
*
|
||||
* Run with: npx tsx tests/test-pure-functions.ts
|
||||
* Or: node --import tsx tests/test-pure-functions.ts
|
||||
* Unit tests for the real pure helpers exported by src/core.ts.
|
||||
* These are hermetic: no pi runtime and no network.
|
||||
*/
|
||||
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { describe, it } from "node:test";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Pure functions copied from index.ts (standalone, no pi imports needed)
|
||||
// ---------------------------------------------------------------------------
|
||||
import {
|
||||
getApiKey,
|
||||
getEnvironmentInfo,
|
||||
mapFinishReason,
|
||||
messagesToCC,
|
||||
parseStreamEventLine,
|
||||
textContent,
|
||||
toJsonSchema,
|
||||
toolsToJson,
|
||||
} from "../src/core.ts";
|
||||
|
||||
function uuid(): string {
|
||||
return crypto.randomUUID();
|
||||
}
|
||||
import { objectAt } from "./helpers.ts";
|
||||
|
||||
function textContent(m: { content: any[] }): string {
|
||||
return (m.content ?? [])
|
||||
.filter((c: any) => c.type === "text")
|
||||
.map((c: any) => c.text ?? "")
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
function getEnvironmentInfo(): string {
|
||||
return `${process.platform}-${process.arch}, Node.js ${process.version}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Minimal typebox → JSON Schema converter.
|
||||
* Handles Object, String, Number, Boolean, Array, Union, Optional, Enum.
|
||||
*/
|
||||
function toJsonSchema(schema: any): any {
|
||||
if (!schema) return {};
|
||||
const s = schema as Record<string, any>;
|
||||
const kind = s.kind ?? s.type;
|
||||
|
||||
if (s.enum) {
|
||||
return { type: typeof s.enum[0], enum: s.enum };
|
||||
}
|
||||
|
||||
switch (kind) {
|
||||
case "string":
|
||||
case "String":
|
||||
return { type: "string" };
|
||||
case "number":
|
||||
case "Number":
|
||||
return { type: "number" };
|
||||
case "boolean":
|
||||
case "Boolean":
|
||||
return { type: "boolean" };
|
||||
case "object":
|
||||
case "Object": {
|
||||
const props: Record<string, any> = {};
|
||||
const inferredRequired: string[] = [];
|
||||
if (s.properties) {
|
||||
for (const [k, v] of Object.entries(s.properties)) {
|
||||
props[k] = toJsonSchema(v);
|
||||
if (!(v as any).optional && !s.optional?.includes?.(k))
|
||||
inferredRequired.push(k);
|
||||
}
|
||||
}
|
||||
const required = Array.isArray(s.required) ? s.required : inferredRequired;
|
||||
const out: any = { type: "object" };
|
||||
if (Object.keys(props).length) out.properties = props;
|
||||
if (required.length) out.required = required;
|
||||
return out;
|
||||
}
|
||||
case "array":
|
||||
case "Array":
|
||||
return { type: "array", items: toJsonSchema(s.items ?? s.element) };
|
||||
case "union":
|
||||
case "Union": {
|
||||
const variants = s.variants ?? s.anyOf ?? [];
|
||||
for (const v of variants) {
|
||||
const schema = toJsonSchema(v);
|
||||
if (schema && Object.keys(schema).length) return schema;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
case "optional":
|
||||
case "Optional":
|
||||
return toJsonSchema(s.wrapped ?? s.inner);
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function toolsToJson(tools: any[]): any[] {
|
||||
if (!tools) return [];
|
||||
return tools.map((t) => {
|
||||
const schema = t.parameters ? toJsonSchema(t.parameters) : {};
|
||||
return {
|
||||
type: "function",
|
||||
name: t.name,
|
||||
description: t.description,
|
||||
input_schema: schema,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function messagesToCC(msgs: any[]): any[] {
|
||||
const out: any[] = [];
|
||||
for (const m of msgs) {
|
||||
if (m.role === "user") {
|
||||
out.push({
|
||||
role: "user",
|
||||
content: typeof m.content === "string" ? m.content : m.content,
|
||||
});
|
||||
} else if (m.role === "assistant") {
|
||||
const parts: any[] = [];
|
||||
for (const c of m.content) {
|
||||
if (c.type === "text") {
|
||||
parts.push({ type: "text", text: c.text });
|
||||
} else if (c.type === "thinking") {
|
||||
parts.push({ type: "reasoning", text: c.thinking });
|
||||
} else if (c.type === "toolCall") {
|
||||
parts.push({
|
||||
type: "tool-call",
|
||||
toolCallId: c.id,
|
||||
toolName: c.name,
|
||||
input: c.arguments,
|
||||
});
|
||||
}
|
||||
}
|
||||
out.push({ role: "assistant", content: parts });
|
||||
} else if (m.role === "toolResult") {
|
||||
out.push({
|
||||
role: "tool",
|
||||
content: [
|
||||
{
|
||||
type: "tool-result",
|
||||
toolCallId: m.toolCallId,
|
||||
toolName: m.toolName,
|
||||
output: m.isError
|
||||
? { type: "error-text", value: textContent(m) }
|
||||
: { type: "text", value: textContent(m) },
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// ===========================================================================
|
||||
// Tests
|
||||
// ===========================================================================
|
||||
|
||||
describe("uuid()", () => {
|
||||
it("returns a valid UUID string", () => {
|
||||
const id = uuid();
|
||||
assert.match(id, /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i);
|
||||
describe("getApiKey()", () => {
|
||||
it("uses COMMANDCODE_API_KEY from provided env", () => {
|
||||
assert.equal(getApiKey({ env: { COMMANDCODE_API_KEY: "env-key" }, authPaths: [] }), "env-key");
|
||||
});
|
||||
|
||||
it("returns unique values on each call", () => {
|
||||
const a = uuid();
|
||||
const b = uuid();
|
||||
assert.notEqual(a, b);
|
||||
it("reads apiKey and commandcode fields from explicit auth paths", () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "cc-auth-"));
|
||||
try {
|
||||
const first = join(dir, "first.json");
|
||||
const second = join(dir, "second.json");
|
||||
writeFileSync(first, JSON.stringify({ apiKey: "file-key" }));
|
||||
writeFileSync(second, JSON.stringify({ commandcode: "fallback-key" }));
|
||||
assert.equal(getApiKey({ env: {}, authPaths: [first, second] }), "file-key");
|
||||
assert.equal(getApiKey({ env: {}, authPaths: [second] }), "fallback-key");
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("ignores malformed auth files", () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "cc-auth-bad-"));
|
||||
try {
|
||||
const bad = join(dir, "bad.json");
|
||||
writeFileSync(bad, "not json");
|
||||
assert.equal(getApiKey({ env: {}, authPaths: [bad] }), undefined);
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("uses injected homeDir for default auth paths", () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "cc-home-"));
|
||||
try {
|
||||
const authDir = join(dir, ".pi", "agent");
|
||||
mkdirSync(authDir, { recursive: true });
|
||||
writeFileSync(join(authDir, "auth.json"), JSON.stringify({ commandcode: "pi-key" }));
|
||||
assert.equal(getApiKey({ env: {}, homeDir: () => dir }), "pi-key");
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------
|
||||
// textContent
|
||||
// ---------------------------------------------------------------
|
||||
|
||||
describe("textContent()", () => {
|
||||
it("extracts text from content array", () => {
|
||||
const msg = { content: [{ type: "text", text: "hello" }, { type: "text", text: "world" }] };
|
||||
assert.equal(textContent(msg), "hello\nworld");
|
||||
});
|
||||
|
||||
it("filters non-text content", () => {
|
||||
const msg = { content: [{ type: "text", text: "hello" }, { type: "image", data: "x" }] };
|
||||
assert.equal(textContent(msg), "hello");
|
||||
});
|
||||
|
||||
it("returns empty string for empty content", () => {
|
||||
assert.equal(textContent({ content: [] }), "");
|
||||
});
|
||||
|
||||
it("handles missing content gracefully", () => {
|
||||
assert.equal(textContent({ content: undefined as any }) ?? "", "");
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------
|
||||
// getEnvironmentInfo
|
||||
// ---------------------------------------------------------------
|
||||
|
||||
describe("getEnvironmentInfo()", () => {
|
||||
it("returns string with platform, arch, and node version", () => {
|
||||
const info = getEnvironmentInfo();
|
||||
assert.match(info, /^(darwin|linux|win32)-/);
|
||||
assert.ok(info.includes("Node.js"), `expected Node.js in: ${info}`);
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------
|
||||
// toJsonSchema
|
||||
// ---------------------------------------------------------------
|
||||
|
||||
describe("toJsonSchema — scalar types", () => {
|
||||
it("handles string (lowercase kind)", () => {
|
||||
assert.deepEqual(toJsonSchema({ kind: "string" }), { type: "string" });
|
||||
});
|
||||
it("handles String (capitalized)", () => {
|
||||
assert.deepEqual(toJsonSchema({ kind: "String" }), { type: "string" });
|
||||
});
|
||||
it("handles number", () => {
|
||||
assert.deepEqual(toJsonSchema({ kind: "number" }), { type: "number" });
|
||||
});
|
||||
it("handles Number", () => {
|
||||
assert.deepEqual(toJsonSchema({ kind: "Number" }), { type: "number" });
|
||||
});
|
||||
it("handles boolean", () => {
|
||||
assert.deepEqual(toJsonSchema({ kind: "boolean" }), { type: "boolean" });
|
||||
});
|
||||
it("handles Boolean", () => {
|
||||
assert.deepEqual(toJsonSchema({ kind: "Boolean" }), { type: "boolean" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("toJsonSchema — enum", () => {
|
||||
it("detects enum by property (string values)", () => {
|
||||
const schema = { kind: "string", enum: ["left", "right"] };
|
||||
assert.deepEqual(toJsonSchema(schema), { type: "string", enum: ["left", "right"] });
|
||||
});
|
||||
it("detects enum by property (number values)", () => {
|
||||
const schema = { kind: "number", enum: [1, 2, 3] };
|
||||
assert.deepEqual(toJsonSchema(schema), { type: "number", enum: [1, 2, 3] });
|
||||
});
|
||||
});
|
||||
|
||||
describe("toJsonSchema — object", () => {
|
||||
it("converts simple object with string props", () => {
|
||||
const schema = {
|
||||
kind: "object",
|
||||
properties: {
|
||||
name: { kind: "string" },
|
||||
age: { kind: "number" },
|
||||
},
|
||||
};
|
||||
assert.deepEqual(toJsonSchema(schema), {
|
||||
type: "object",
|
||||
properties: { name: { type: "string" }, age: { type: "number" } },
|
||||
required: ["name", "age"],
|
||||
});
|
||||
});
|
||||
|
||||
it("marks optional properties correctly", () => {
|
||||
const schema = {
|
||||
kind: "object",
|
||||
properties: {
|
||||
name: { kind: "string" },
|
||||
nickname: { kind: "string", optional: true },
|
||||
},
|
||||
};
|
||||
const result = toJsonSchema(schema);
|
||||
assert.deepEqual(result.required, ["name"]);
|
||||
assert.deepEqual(result.properties?.nickname, { type: "string" });
|
||||
});
|
||||
|
||||
it("handles optional via top-level optional array", () => {
|
||||
const schema = {
|
||||
kind: "Object",
|
||||
properties: {
|
||||
name: { kind: "string" },
|
||||
age: { kind: "number" },
|
||||
},
|
||||
optional: ["age"],
|
||||
};
|
||||
const result = toJsonSchema(schema);
|
||||
assert.deepEqual(result.required, ["name"]);
|
||||
});
|
||||
|
||||
it("preserves TypeBox required arrays", () => {
|
||||
const schema = {
|
||||
type: "object",
|
||||
properties: {
|
||||
name: { type: "string" },
|
||||
nickname: { type: "string" },
|
||||
},
|
||||
required: ["name"],
|
||||
};
|
||||
const result = toJsonSchema(schema);
|
||||
assert.deepEqual(result.required, ["name"]);
|
||||
});
|
||||
|
||||
it("handles empty object", () => {
|
||||
assert.deepEqual(toJsonSchema({ kind: "object" }), { type: "object" });
|
||||
});
|
||||
|
||||
it("handles Object (capitalized)", () => {
|
||||
assert.deepEqual(toJsonSchema({ kind: "Object" }), { type: "object" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("toJsonSchema — array", () => {
|
||||
it("converts array with items", () => {
|
||||
const schema = { kind: "array", items: { kind: "string" } };
|
||||
assert.deepEqual(toJsonSchema(schema), { type: "array", items: { type: "string" } });
|
||||
});
|
||||
|
||||
it("converts array with element (alternative prop name)", () => {
|
||||
const schema = { kind: "Array", element: { kind: "number" } };
|
||||
assert.deepEqual(toJsonSchema(schema), { type: "array", items: { type: "number" } });
|
||||
});
|
||||
});
|
||||
|
||||
describe("toJsonSchema — union", () => {
|
||||
it("uses first non-empty variant", () => {
|
||||
const schema = { kind: "union", variants: [{}, { kind: "string" }] };
|
||||
assert.deepEqual(toJsonSchema(schema), { type: "string" });
|
||||
});
|
||||
|
||||
it("uses anyOf as fallback property name", () => {
|
||||
const schema = { kind: "Union", anyOf: [{ kind: "number" }] };
|
||||
assert.deepEqual(toJsonSchema(schema), { type: "number" });
|
||||
});
|
||||
|
||||
it("returns empty object for empty union", () => {
|
||||
assert.deepEqual(toJsonSchema({ kind: "union", variants: [] }), {});
|
||||
});
|
||||
});
|
||||
|
||||
describe("toJsonSchema — optional", () => {
|
||||
it("unwraps optional via wrapped", () => {
|
||||
const schema = { kind: "optional", wrapped: { kind: "string" } };
|
||||
assert.deepEqual(toJsonSchema(schema), { type: "string" });
|
||||
});
|
||||
|
||||
it("unwraps optional via inner", () => {
|
||||
const schema = { kind: "Optional", inner: { kind: "number" } };
|
||||
assert.deepEqual(toJsonSchema(schema), { type: "number" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("toJsonSchema — edge cases", () => {
|
||||
it("returns empty object for null", () => {
|
||||
assert.deepEqual(toJsonSchema(null), {});
|
||||
});
|
||||
|
||||
it("returns empty object for undefined", () => {
|
||||
assert.deepEqual(toJsonSchema(undefined), {});
|
||||
});
|
||||
|
||||
it("handles unknown kind gracefully", () => {
|
||||
assert.deepEqual(toJsonSchema({ kind: "foobar" }), {});
|
||||
});
|
||||
|
||||
it("handles nested objects recursively", () => {
|
||||
const schema = {
|
||||
kind: "object",
|
||||
properties: {
|
||||
user: {
|
||||
kind: "object",
|
||||
properties: {
|
||||
name: { kind: "string" },
|
||||
address: {
|
||||
kind: "object",
|
||||
properties: { city: { kind: "string" } },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
const result = toJsonSchema(schema);
|
||||
assert.equal(result.properties.user.type, "object");
|
||||
assert.equal(result.properties.user.properties.address.type, "object");
|
||||
it("extracts and joins text blocks", () => {
|
||||
assert.equal(
|
||||
result.properties.user.properties.address.properties.city.type,
|
||||
"string",
|
||||
textContent({ content: [{ type: "text", text: "hello" }, { type: "image", data: "x" }, { type: "text", text: "world" }] }),
|
||||
"hello\nworld",
|
||||
);
|
||||
});
|
||||
|
||||
it("handles type property as fallback for kind", () => {
|
||||
assert.deepEqual(toJsonSchema({ type: "string" }), { type: "string" });
|
||||
it("handles empty or missing content", () => {
|
||||
assert.equal(textContent({ content: [] }), "");
|
||||
assert.equal(textContent({}), "");
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------
|
||||
// toolsToJson
|
||||
// ---------------------------------------------------------------
|
||||
describe("getEnvironmentInfo()", () => {
|
||||
it("returns platform, arch, and Node version", () => {
|
||||
const info = getEnvironmentInfo();
|
||||
assert.match(info, /^(darwin|linux|win32)-/);
|
||||
assert.ok(info.includes("Node.js"));
|
||||
});
|
||||
});
|
||||
|
||||
describe("toJsonSchema()", () => {
|
||||
it("converts scalar, enum, object, optional, array, and union schema shapes", () => {
|
||||
assert.deepEqual(toJsonSchema({ kind: "string" }), { type: "string" });
|
||||
assert.deepEqual(toJsonSchema({ kind: "Number" }), { type: "number" });
|
||||
assert.deepEqual(toJsonSchema({ kind: "boolean" }), { type: "boolean" });
|
||||
assert.deepEqual(toJsonSchema({ kind: "string", enum: ["left", "right"] }), {
|
||||
type: "string",
|
||||
enum: ["left", "right"],
|
||||
});
|
||||
assert.deepEqual(
|
||||
toJsonSchema({
|
||||
kind: "object",
|
||||
properties: {
|
||||
name: { kind: "string" },
|
||||
tags: { kind: "array", items: { kind: "string" }, optional: true },
|
||||
},
|
||||
}),
|
||||
{
|
||||
type: "object",
|
||||
properties: { name: { type: "string" }, tags: { type: "array", items: { type: "string" } } },
|
||||
required: ["name"],
|
||||
},
|
||||
);
|
||||
assert.deepEqual(toJsonSchema({ kind: "optional", wrapped: { kind: "string" } }), { type: "string" });
|
||||
assert.deepEqual(toJsonSchema({ kind: "union", variants: [{}, { kind: "number" }] }), { type: "number" });
|
||||
});
|
||||
|
||||
it("preserves explicit required arrays and handles unknown values", () => {
|
||||
assert.deepEqual(
|
||||
toJsonSchema({
|
||||
type: "object",
|
||||
properties: { name: { type: "string" }, nickname: { type: "string" } },
|
||||
required: ["name"],
|
||||
}),
|
||||
{
|
||||
type: "object",
|
||||
properties: { name: { type: "string" }, nickname: { type: "string" } },
|
||||
required: ["name"],
|
||||
},
|
||||
);
|
||||
assert.deepEqual(toJsonSchema(undefined), {});
|
||||
assert.deepEqual(toJsonSchema({ kind: "wat" }), {});
|
||||
});
|
||||
});
|
||||
|
||||
describe("toolsToJson()", () => {
|
||||
it("returns empty array for undefined", () => {
|
||||
assert.deepEqual(toolsToJson(undefined as any), []);
|
||||
});
|
||||
|
||||
it("returns empty array for null", () => {
|
||||
assert.deepEqual(toolsToJson(null as any), []);
|
||||
});
|
||||
|
||||
it("returns empty array for empty array", () => {
|
||||
assert.deepEqual(toolsToJson([]), []);
|
||||
});
|
||||
|
||||
it("converts a tool with object parameters", () => {
|
||||
const tools = [
|
||||
{
|
||||
name: "get_weather",
|
||||
description: "Get the weather for a city",
|
||||
parameters: {
|
||||
kind: "object",
|
||||
properties: {
|
||||
city: { kind: "string" },
|
||||
it("converts pi tools to Command Code tool JSON", () => {
|
||||
assert.deepEqual(
|
||||
toolsToJson([
|
||||
{
|
||||
name: "get_weather",
|
||||
description: "Get weather",
|
||||
parameters: {
|
||||
kind: "object",
|
||||
properties: { city: { kind: "string" } },
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
const result = toolsToJson(tools);
|
||||
assert.equal(result.length, 1);
|
||||
assert.equal(result[0].type, "function");
|
||||
assert.equal(result[0].name, "get_weather");
|
||||
assert.equal(result[0].description, "Get the weather for a city");
|
||||
assert.deepEqual(result[0].input_schema, {
|
||||
type: "object",
|
||||
properties: { city: { type: "string" } },
|
||||
required: ["city"],
|
||||
});
|
||||
});
|
||||
|
||||
it("handles tool without parameters", () => {
|
||||
const tools = [{ name: "ping", description: "Check connectivity" }];
|
||||
const result = toolsToJson(tools);
|
||||
assert.equal(result.length, 1);
|
||||
assert.deepEqual(result[0].input_schema, {});
|
||||
});
|
||||
|
||||
it("converts multiple tools", () => {
|
||||
const tools = [
|
||||
{ name: "tool_a", description: "A", parameters: { kind: "string" } },
|
||||
{ name: "tool_b", description: "B", parameters: { kind: "number" } },
|
||||
];
|
||||
const result = toolsToJson(tools);
|
||||
assert.equal(result.length, 2);
|
||||
assert.equal(result[0].name, "tool_a");
|
||||
assert.equal(result[1].name, "tool_b");
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------
|
||||
// messagesToCC
|
||||
// ---------------------------------------------------------------
|
||||
|
||||
describe("messagesToCC() — user messages", () => {
|
||||
it("converts string content user message", () => {
|
||||
const msgs = [{ role: "user", content: "hello" }];
|
||||
const result = messagesToCC(msgs);
|
||||
assert.equal(result.length, 1);
|
||||
assert.equal(result[0].role, "user");
|
||||
assert.equal(result[0].content, "hello");
|
||||
});
|
||||
|
||||
it("passes through array content user message", () => {
|
||||
const content = [{ type: "text", text: "hello" }];
|
||||
const msgs = [{ role: "user", content }];
|
||||
const result = messagesToCC(msgs);
|
||||
assert.equal(result[0].role, "user");
|
||||
assert.equal(result[0].content, content);
|
||||
});
|
||||
});
|
||||
|
||||
describe("messagesToCC() — assistant messages", () => {
|
||||
it("converts text content", () => {
|
||||
const msgs = [
|
||||
{
|
||||
role: "assistant",
|
||||
content: [{ type: "text", text: "Hello from assistant" }],
|
||||
},
|
||||
];
|
||||
const result = messagesToCC(msgs);
|
||||
assert.equal(result.length, 1);
|
||||
assert.equal(result[0].role, "assistant");
|
||||
assert.deepEqual(result[0].content, [
|
||||
{ type: "text", text: "Hello from assistant" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("converts thinking content to reasoning", () => {
|
||||
const msgs = [
|
||||
{
|
||||
role: "assistant",
|
||||
content: [{ type: "thinking", thinking: "Let me think..." }],
|
||||
},
|
||||
];
|
||||
const result = messagesToCC(msgs);
|
||||
assert.deepEqual(result[0].content, [
|
||||
{ type: "reasoning", text: "Let me think..." },
|
||||
]);
|
||||
});
|
||||
|
||||
it("converts toolCall content", () => {
|
||||
const msgs = [
|
||||
{
|
||||
role: "assistant",
|
||||
content: [
|
||||
{
|
||||
type: "toolCall",
|
||||
id: "call_123",
|
||||
name: "read_file",
|
||||
arguments: { path: "/tmp/test" },
|
||||
]),
|
||||
[
|
||||
{
|
||||
type: "function",
|
||||
name: "get_weather",
|
||||
description: "Get weather",
|
||||
input_schema: {
|
||||
type: "object",
|
||||
properties: { city: { type: "string" } },
|
||||
required: ["city"],
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
const result = messagesToCC(msgs);
|
||||
assert.deepEqual(result[0].content, [
|
||||
{
|
||||
type: "tool-call",
|
||||
toolCallId: "call_123",
|
||||
toolName: "read_file",
|
||||
input: { path: "/tmp/test" },
|
||||
},
|
||||
]);
|
||||
},
|
||||
],
|
||||
);
|
||||
});
|
||||
|
||||
it("converts mixed content (thinking + text + toolCall)", () => {
|
||||
const msgs = [
|
||||
{
|
||||
role: "assistant",
|
||||
content: [
|
||||
{ type: "thinking", thinking: "planning..." },
|
||||
{ type: "text", text: "result" },
|
||||
{ type: "toolCall", id: "t1", name: "ls", arguments: {} },
|
||||
],
|
||||
},
|
||||
];
|
||||
const result = messagesToCC(msgs);
|
||||
assert.equal(result[0].role, "assistant");
|
||||
assert.equal(result[0].content.length, 3);
|
||||
assert.equal(result[0].content[0].type, "reasoning");
|
||||
assert.equal(result[0].content[1].type, "text");
|
||||
assert.equal(result[0].content[2].type, "tool-call");
|
||||
it("returns an empty array for missing tools", () => {
|
||||
assert.deepEqual(toolsToJson(), []);
|
||||
});
|
||||
});
|
||||
|
||||
describe("messagesToCC() — toolResult messages", () => {
|
||||
it("converts successful tool result", () => {
|
||||
const msgs = [
|
||||
{
|
||||
role: "toolResult",
|
||||
toolCallId: "call_123",
|
||||
toolName: "read_file",
|
||||
isError: false,
|
||||
content: [{ type: "text", text: "file contents here" }],
|
||||
},
|
||||
];
|
||||
const result = messagesToCC(msgs);
|
||||
assert.equal(result.length, 1);
|
||||
assert.equal(result[0].role, "tool");
|
||||
assert.equal(result[0].content[0].type, "tool-result");
|
||||
assert.equal(result[0].content[0].output.type, "text");
|
||||
assert.equal(result[0].content[0].output.value, "file contents here");
|
||||
});
|
||||
|
||||
it("converts error tool result", () => {
|
||||
const msgs = [
|
||||
{
|
||||
role: "toolResult",
|
||||
toolCallId: "call_456",
|
||||
toolName: "bad_tool",
|
||||
isError: true,
|
||||
content: [{ type: "text", text: "something went wrong" }],
|
||||
},
|
||||
];
|
||||
const result = messagesToCC(msgs);
|
||||
assert.equal(result[0].content[0].output.type, "error-text");
|
||||
assert.equal(result[0].content[0].output.value, "something went wrong");
|
||||
});
|
||||
|
||||
it("joins multiple text parts", () => {
|
||||
const msgs = [
|
||||
{
|
||||
role: "toolResult",
|
||||
toolCallId: "call_789",
|
||||
toolName: "grep",
|
||||
isError: false,
|
||||
content: [
|
||||
{ type: "text", text: "line 1" },
|
||||
{ type: "text", text: "line 2" },
|
||||
],
|
||||
},
|
||||
];
|
||||
const result = messagesToCC(msgs);
|
||||
assert.equal(result[0].content[0].output.value, "line 1\nline 2");
|
||||
});
|
||||
});
|
||||
|
||||
describe("messagesToCC() — full conversation", () => {
|
||||
it("handles user → assistant(toolCall) → tool → assistant", () => {
|
||||
const msgs = [
|
||||
describe("messagesToCC()", () => {
|
||||
it("converts user, assistant, and tool result messages", () => {
|
||||
const result = messagesToCC([
|
||||
{ role: "user", content: "read /tmp/test" },
|
||||
{
|
||||
role: "assistant",
|
||||
content: [
|
||||
{ type: "thinking", thinking: "I will read the file" },
|
||||
{
|
||||
type: "toolCall",
|
||||
id: "c1",
|
||||
name: "read",
|
||||
arguments: { path: "/tmp/test" },
|
||||
},
|
||||
{ type: "thinking", thinking: "I will read" },
|
||||
{ type: "text", text: "Sure" },
|
||||
{ type: "toolCall", id: "c1", name: "read", arguments: { path: "/tmp/test" } },
|
||||
],
|
||||
},
|
||||
{
|
||||
@@ -613,25 +182,45 @@ describe("messagesToCC() — full conversation", () => {
|
||||
toolCallId: "c1",
|
||||
toolName: "read",
|
||||
isError: false,
|
||||
content: [{ type: "text", text: "hello world" }],
|
||||
content: [{ type: "text", text: "hello" }, { type: "text", text: "world" }],
|
||||
},
|
||||
{
|
||||
role: "assistant",
|
||||
content: [{ type: "text", text: "The file contains: hello world" }],
|
||||
},
|
||||
];
|
||||
const result = messagesToCC(msgs);
|
||||
assert.equal(result.length, 4);
|
||||
assert.equal(result[0].role, "user");
|
||||
assert.equal(result[1].role, "assistant");
|
||||
assert.equal(result[1].content.length, 2);
|
||||
assert.equal(result[2].role, "tool");
|
||||
assert.equal(result[3].role, "assistant");
|
||||
assert.equal(result[3].content[0].text, "The file contains: hello world");
|
||||
]);
|
||||
|
||||
assert.equal(objectAt(result, ["0", "role"]), "user");
|
||||
assert.equal(objectAt(result, ["1", "role"]), "assistant");
|
||||
assert.equal(objectAt(result, ["1", "content", "0", "type"]), "reasoning");
|
||||
assert.equal(objectAt(result, ["1", "content", "2", "type"]), "tool-call");
|
||||
assert.equal(objectAt(result, ["2", "role"]), "tool");
|
||||
assert.equal(objectAt(result, ["2", "content", "0", "output", "value"]), "hello\nworld");
|
||||
});
|
||||
|
||||
it("handles empty message array", () => {
|
||||
const result = messagesToCC([]);
|
||||
assert.deepEqual(result, []);
|
||||
it("handles empty conversations", () => {
|
||||
assert.deepEqual(messagesToCC([]), []);
|
||||
});
|
||||
});
|
||||
|
||||
describe("parseStreamEventLine()", () => {
|
||||
it("parses plain JSON and SSE data lines", () => {
|
||||
assert.deepEqual(parseStreamEventLine('{"type":"text-delta","text":"x"}'), { type: "text-delta", text: "x" });
|
||||
assert.deepEqual(parseStreamEventLine('data: {"type":"finish","finishReason":"stop"}'), {
|
||||
type: "finish",
|
||||
finishReason: "stop",
|
||||
});
|
||||
});
|
||||
|
||||
it("ignores comments, event labels, done markers, and malformed JSON", () => {
|
||||
assert.equal(parseStreamEventLine(":"), undefined);
|
||||
assert.equal(parseStreamEventLine("event: message"), undefined);
|
||||
assert.equal(parseStreamEventLine("data: [DONE]"), undefined);
|
||||
assert.equal(parseStreamEventLine("not-json"), undefined);
|
||||
});
|
||||
});
|
||||
|
||||
describe("mapFinishReason()", () => {
|
||||
it("maps provider finish reasons to pi stop reasons", () => {
|
||||
assert.equal(mapFinishReason("stop"), "stop");
|
||||
assert.equal(mapFinishReason("tool-calls"), "toolUse");
|
||||
assert.equal(mapFinishReason("max_tokens"), "length");
|
||||
assert.equal(mapFinishReason("max_output_tokens"), "length");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user