repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/voice/__tests__/progress-summary.test.ts
Rawimport { afterEach, describe, expect, it, vi } from "vitest";
import { repeatsProgress, summarizeProgress } from "../src/progress-summary.ts";
const model = { provider: "test", id: "gpt-6-luna" } as never;
const reply = (text: string, stopReason = "stop") => ({
stopReason,
content: [{ type: "text", text }],
});
function host(result: () => Promise<any>) {
const stream = vi.fn((..._args: unknown[]) => ({ result }));
return { ctx: { modelRegistry: { streamSimple: stream } } as never, stream };
}
afterEach(() => vi.useRealTimers());
describe("semantic executor progress", () => {
it("uses the existing chat path with bounded output and no tools", async () => {
const h = host(async () =>
reply("I’ve stopped long pauses from cutting off your sentence."),
);
expect(
await summarizeProgress(
h.ctx,
model,
'{"task":"voice capture"}',
new AbortController().signal,
),
).toBe("I’ve stopped long pauses from cutting off your sentence.");
expect(h.stream.mock.calls[0]?.[0]).toBe(model);
expect(h.stream.mock.calls[0]?.[1]).toMatchObject({
tools: [],
systemPrompt: expect.stringContaining("untrusted data"),
messages: [
{
role: "user",
content: '{"task":"voice capture"}',
timestamp: expect.any(Number),
},
],
});
const prompt = h.stream.mock.calls[0]?.[1] as { systemPrompt: string };
expect(prompt.systemPrompt).toContain("what it means for the human's task");
expect(prompt.systemPrompt).toContain("not a technical tool report");
expect(prompt.systemPrompt).toContain("return SKIP for those");
expect(h.stream.mock.calls[0]?.[2]).toMatchObject({
maxTokens: 64,
signal: expect.any(AbortSignal),
});
});
it.each([
"",
"First sentence. Second sentence.",
"Pi is checking the caption pipeline.",
"I’m waiting for Pi to finish.",
`I ${"a ".repeat(20)}`,
"I’m checking input, measuring output, and reading logs.",
"I’m still working on your request.",
"I’m reading the next file.",
"I’m running regression tests.",
"I’ve finished the latest tool call.",
"I confirmed 42 tests passed.",
"Two\nlines",
"x".repeat(321),
"I approved the question.",
"I answered the Pi question.",
"The Pi question has been answered.",
"API_KEY=example-secret",
"```code```",
'{"summary":"JSON"}',
])("rejects unsafe or non-sentence output %s", async (text) => {
const h = host(async () => reply(text));
expect(
await summarizeProgress(h.ctx, model, "{}", new AbortController().signal),
).toBeUndefined();
});
it("returns deliberate silence rather than failure for SKIP", async () => {
const h = host(async () => reply("SKIP"));
expect(
await summarizeProgress(h.ctx, model, "{}", new AbortController().signal),
).toBeNull();
expect(h.stream.mock.calls[0]?.[1]).toMatchObject({
systemPrompt: expect.stringContaining("previousUpdate"),
});
});
it.each([
[
"I’m checking caption latency.",
"I’m still checking the caption latency.",
true,
],
[
"I found the caption queue bottleneck.",
"I found the caption queue bottleneck!",
true,
],
["I measured 129 MB available.", "I measured 123 MB available.", false],
["I can reproduce the delay.", "I cannot reproduce the delay.", false],
["I’ve fixed the queue.", "I haven’t fixed the queue.", false],
["I’m tracing input.", "I’m running regression tests.", false],
] as const)(
"compares incremental update %s / %s",
(previous, next, expected) => {
expect(repeatsProgress(previous, next)).toBe(expected);
},
);
it("rejects truncated output and tool calls", async () => {
for (const result of [
reply("An unfinished response", "length"),
{
stopReason: "stop",
content: [
{ type: "toolCall", name: "approve", id: "1", arguments: {} },
],
},
]) {
const h = host(async () => result);
expect(
await summarizeProgress(
h.ctx,
model,
"{}",
new AbortController().signal,
),
).toBeUndefined();
}
});
it("does not call a model for oversized context or an already cancelled job", async () => {
const h = host(async () => reply("Working on voice capture."));
const abort = new AbortController();
abort.abort();
await summarizeProgress(h.ctx, model, "{}", abort.signal);
await summarizeProgress(
h.ctx,
model,
"x".repeat(8_001),
new AbortController().signal,
);
expect(h.stream).not.toHaveBeenCalled();
});
it("bounds waiting to five seconds even if the provider ignores abort", async () => {
vi.useFakeTimers();
const h = host(() => new Promise(() => {}));
const pending = summarizeProgress(
h.ctx,
model,
"{}",
new AbortController().signal,
);
await vi.advanceTimersByTimeAsync(5_000);
await expect(pending).resolves.toBeUndefined();
const options = h.stream.mock.calls[0]?.[2] as
| { signal: AbortSignal }
| undefined;
expect(options?.signal.aborted).toBe(true);
});
it("observes a late provider rejection after external cancellation", async () => {
let reject!: (error: Error) => void;
const h = host(
() =>
new Promise((_resolve, fail) => {
reject = fail;
}),
);
const abort = new AbortController();
const pending = summarizeProgress(h.ctx, model, "{}", abort.signal);
abort.abort();
await expect(pending).resolves.toBeUndefined();
reject(new Error("late provider rejection"));
await Promise.resolve();
});
it("treats provider failure as a missing summary", async () => {
const h = host(async () => {
throw new Error("offline");
});
expect(
await summarizeProgress(h.ctx, model, "{}", new AbortController().signal),
).toBeUndefined();
});
});