repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/voice/__tests__/voice-widget.test.ts
Rawimport {
Container,
stripTerminalSequences,
type TUI,
type TuiMouseEvent,
} from "@earendil-works/pi-tui";
import { afterEach, describe, expect, it, vi } from "vitest";
import {
createNativeVoiceWidget,
type NativeVoiceWidgetState,
renderNativeVoiceWidget,
widgetVisibleWidth,
} from "../src/voice-widget.ts";
const theme = {
fg: (_color: string, text: string) => `\u001b[31m${text}\u001b[0m`,
bold: (text: string) => text,
} as never;
function state(
overrides: Partial<NativeVoiceWidgetState> = {},
): NativeVoiceWidgetState {
return {
status: "ready",
activity: "ready",
error: null,
partial: "",
spoken: "",
waveform: [0, 0.5, 1],
inputLevel: 0,
outputLevel: 0,
playing: false,
muted: false,
...overrides,
};
}
function fixture(input = state()) {
const requestRender = vi.fn();
const terminal = { rows: 24 };
const widget = createNativeVoiceWidget(
{ requestRender, terminal } as unknown as TUI,
theme,
() => input,
);
return {
input,
widget,
requestRender,
terminal,
text: (width = 60) => widget.render(width).map(stripTerminalSequences),
};
}
function wheel(y: number, wheelDelta: number, height = 7): TuiMouseEvent {
return {
type: "wheel",
button: "none",
x: 5,
y,
screenX: 5,
screenY: y,
width: 60,
height,
shift: false,
alt: false,
ctrl: false,
wheelDelta,
};
}
const numbered = (prefix: string, count: number) =>
Array.from({ length: count }, (_, i) => `${prefix}${i}`).join("\n");
afterEach(() => vi.useRealTimers());
describe("native voice widget", () => {
it("keeps non-speech flat and muted despite changing microphone peaks", () => {
const f = fixture(
state({ humanSpeaking: false, waveform: [0.1, 0.8, 0.01] }),
);
const idle = f.text()[0];
f.input.waveform = [0.9, 0.01, 0.4];
f.input.inputLevel = 0.9;
f.input.outputLevel = 0.8;
expect(f.text()[0]).toBe(idle);
expect(idle).toContain("───");
expect(idle).not.toMatch(/[▁▂▃▄▅▆▇█]/u);
f.widget.dispose();
});
it("amplifies detected speech visually without changing measured audio", () => {
const input = state({ humanSpeaking: true, waveform: [0.001, 0.01, 0.1] });
const bold = vi.fn((text: string) => text);
const fg = vi.fn((_color: string, text: string) => text);
expect(renderNativeVoiceWidget(input, { bold, fg }, 80)[0]).toContain(
"▂▅█",
);
expect(bold).toHaveBeenCalledWith("▂▅█");
expect(fg).toHaveBeenCalledWith("accent", "▂▅█");
expect(input.waveform).toEqual([0.001, 0.01, 0.1]);
input.muted = true;
expect(renderNativeVoiceWidget(input, { bold, fg }, 80)[0]).toContain(
"───",
);
expect(fg).toHaveBeenCalledWith("muted", "───");
});
it("keeps startup compact and expands only when the call is ready", () => {
const f = fixture(state({ status: "loading" }));
expect(f.text()).toHaveLength(2);
expect(f.text()[0]).toContain("Loading");
expect(f.text()[1]).toContain("Starting speech");
expect(f.text()[1]).toContain("/voice stop");
expect(f.text().join("\n")).not.toMatch(
/Paused|Nothing spoken|Provisioning/,
);
expect(f.widget.handleMouse?.(wheel(1, 1))).toBeUndefined();
f.input.status = "ready";
expect(f.text()).toHaveLength(7);
f.widget.dispose();
});
it("lowercases provisional display only, preserving raw text, final casing and spoken captions", () => {
const input = state({
partial: "USE ONNX CAFÉ İ 你好",
spoken: "Keep ONNX casing.",
provisional: true,
});
const f = fixture(input);
for (const lines of [
f.text(),
renderNativeVoiceWidget(input, theme, 60).map(stripTerminalSequences),
]) {
expect(lines.slice(1, 4).join(" ")).toContain(
"use onnx café i\u0307 你好",
);
expect(lines.slice(4).join(" ")).toContain("Keep ONNX casing.");
}
expect(input.partial).toBe("USE ONNX CAFÉ İ 你好");
input.provisional = false;
expect(f.text()[1]).toContain("USE ONNX CAFÉ İ 你好");
for (const width of [1, 8, 20])
for (const line of f.text(width))
expect(widgetVisibleWidth(line)).toBeLessThanOrEqual(width);
f.widget.dispose();
});
it("uses Nerd Font icons without jargon or a command footer", () => {
const lines = renderNativeVoiceWidget(
state({ partial: "hello", spoken: "welcome" }),
theme,
60,
).map(stripTerminalSequences);
expect(lines).toHaveLength(7);
expect(lines[1]).toContain(" hello");
expect(lines[4]).toContain(" welcome");
expect(lines.join("\n")).not.toMatch(/ASR|SAY|\/voice help/);
expect(
renderNativeVoiceWidget(state({ muted: true }), theme, 60).join("\n"),
).toContain("");
});
it.each([
{ activity: "ready", playing: false, label: " Listening" },
{ activity: "ready", playing: true, label: " Speaking" },
{ activity: "thinking", playing: false, label: " Thinking" },
{ activity: "thinking", playing: true, label: " Speaking · Thinking" },
])("shows Nerd Font state icons: $label", ({ activity, playing, label }) => {
const input = state({ activity, playing });
const f = fixture(input);
for (const first of [
f.text(80)[0],
...renderNativeVoiceWidget(input, theme, 80)
.map(stripTerminalSequences)
.slice(0, 1),
])
expect(first).toContain(label);
expect(f.text().join("\n")).not.toMatch(/[🎤🔇🔊💭]/u);
f.widget.dispose();
});
it("keeps ANSI, emoji, combining and wide Unicode output within resized widths", () => {
const input = state({
partial: "你好 👨👩👧👦 café e\u0301 world",
spoken: "Dziękuję. こんにちは。",
});
for (const width of [0, 1, 2, 8, 20, 80, Number.NaN, Infinity])
for (const line of renderNativeVoiceWidget(input, theme, width))
expect(widgetVisibleWidth(line)).toBeLessThanOrEqual(width);
});
it("wraps rather than truncating and exposes text beyond the old 256-character cutoff", () => {
const f = fixture(
state({ partial: `BEGIN ${"word ".repeat(100)}END_TOKEN` }),
);
expect(f.text(20).slice(1, 4).join(" ")).toContain("END_TOKEN");
f.widget.scroll("heard", -10000);
expect(f.text(20)[1]).toContain("BEGIN");
expect(
f
.text(20)
.slice(1, 4)
.every((line) => !line.includes("…")),
).toBe(true);
f.widget.dispose();
});
it("scrolls through the mounted Container path without requesting editor focus", () => {
const f = fixture(
state({ partial: numbered("heard", 10), spoken: numbered("spoken", 10) }),
);
const mounted = new Container();
mounted.addChild({ render: () => ["other widget"], invalidate() {} });
mounted.addChild(f.widget);
mounted.render(60);
const result = mounted.handleMouse(wheel(5, -2, 8));
expect(result?.handled).toBe(true);
expect(result?.focus).not.toBe(true);
expect(f.widget.handleInput).toBeUndefined();
const lines = f.text();
expect(lines[1]).toContain("heard7");
expect(lines[4]).toContain("spoken5");
const boundary = mounted.handleMouse(wheel(5, 1000, 8));
expect(boundary?.handled).toBe(true);
expect(f.widget.handleMouse?.(wheel(0, 1))).toBeUndefined();
f.widget.dispose();
});
it("holds the scrolled region while new text arrives and resumes follow at the bottom", () => {
const f = fixture(
state({ partial: numbered("heard", 10), spoken: numbered("spoken", 10) }),
);
f.text();
f.widget.handleMouse?.(wheel(2, -3));
f.input.partial += "\nheard10";
f.input.spoken += "\nspoken10";
let lines = f.text();
expect(lines[1]).toContain("heard4");
expect(lines[4]).toContain("spoken8");
expect(lines[1]).toContain("↑");
expect(lines[3]).toContain("↓");
f.widget.scroll("heard", 10000);
f.input.partial += "\nheard11";
lines = f.text();
expect(lines[1]).toContain("heard9");
expect(lines[3]).toContain("heard11");
f.widget.dispose();
});
it("adapts height to short terminals and clamps scrolling after replacement and resize", () => {
const f = fixture(state({ spoken: numbered("caption", 30) }));
f.text();
f.widget.scroll("spoken", -10000);
f.terminal.rows = 12;
expect(f.text(20)).toHaveLength(4);
f.input.spoken = "tiny";
// 10 columns leave 8 for content inside the one-column padding.
expect(f.text(10).join("\n")).toContain("tiny");
for (const line of f.text(10)) {
expect(line.startsWith(" ")).toBe(true);
expect(widgetVisibleWidth(line)).toBeLessThanOrEqual(10);
}
f.widget.dispose();
});
it("bounds caption layout work and strips terminal controls while retaining newlines", () => {
const f = fixture(
state({
partial: `\u001b]0;untrusted title\u0007\u001b[31mfirst\u001b[0m\nsecond\u0000`,
spoken: "x".repeat(50000),
}),
);
const lines = f.text();
expect(lines[1]).toContain("first");
expect(lines[2]).toContain("second");
expect(lines.join("\n")).not.toContain("\u0000");
expect(lines.join("\n")).not.toContain("untrusted title");
expect(lines[6]).toContain("…");
f.widget.dispose();
});
it("throttles render requests and cancels pending work on disposal", () => {
vi.useFakeTimers();
const f = fixture();
f.widget.invalidate();
f.widget.invalidate();
f.widget.invalidate();
expect(f.requestRender).toHaveBeenCalledOnce();
vi.advanceTimersByTime(100);
expect(f.requestRender).toHaveBeenCalledTimes(2);
f.widget.invalidate();
f.widget.dispose();
vi.advanceTimersByTime(100);
expect(f.requestRender).toHaveBeenCalledTimes(2);
expect(f.widget.handleMouse?.(wheel(2, 1))).toBeUndefined();
});
it("dims provisional captions and reveals final text before bounded accent sweep completes", () => {
vi.useFakeTimers();
const colorTheme = {
fg: (color: string, text: string) =>
`\u001b[${color === "muted" ? "32" : "31"}m${text}\u001b[0m`,
bold: (text: string) => text,
} as never;
const f = fixture(
state({ partial: "draft", provisional: true, captionRevision: 0 }),
);
const widget = createNativeVoiceWidget(
{
requestRender: f.requestRender,
terminal: f.terminal,
} as unknown as TUI,
colorTheme,
() => f.input,
);
const draft = widget.render(60)[1];
expect(draft).toContain("[32m");
f.input.partial = "final café 👩💻 你好";
f.input.provisional = false;
f.input.captionRevision = 1;
const final = widget.render(60)[1] ?? "";
expect(stripTerminalSequences(final)).toContain("final café 👩💻 你好");
expect(final).toContain("[31m");
for (let frame = 0; frame < 5; frame++) {
vi.advanceTimersByTime(50);
expect(stripTerminalSequences(widget.render(60)[1] ?? "")).toContain(
"final café 👩💻 你好",
);
}
vi.advanceTimersByTime(50);
widget.render(60);
expect(vi.getTimerCount()).toBe(0);
widget.dispose();
f.widget.dispose();
});
it("cancels accent sweep on new provisional speech and disposal without changing scroll", () => {
vi.useFakeTimers();
const f = fixture(
state({
partial: numbered("heard", 10),
provisional: true,
captionRevision: 0,
}),
);
f.text();
f.widget.scroll("heard", -3);
const before = f.text()[1];
f.input.partial = numbered("final", 10);
f.input.provisional = false;
f.input.captionRevision = 1;
f.widget.render(60);
f.input.partial = numbered("later", 10);
f.input.provisional = true;
f.widget.render(60);
vi.advanceTimersByTime(300);
expect((f.text()[1] ?? "").replace(/later(\d+)/u, "heard$1")).toBe(before);
f.input.provisional = false;
f.input.captionRevision = 2;
f.widget.render(60);
expect(vi.getTimerCount()).toBe(1);
f.widget.dispose();
expect(vi.getTimerCount()).toBe(0);
});
it("shows actual unavailable and reconnecting state", () => {
expect(
renderNativeVoiceWidget(
state({ status: "reconnecting" }),
theme,
80,
).join("\n"),
).toContain("Reconnecting");
expect(
renderNativeVoiceWidget(
state({ activity: "thinking", error: "device lost" }),
theme,
80,
).join("\n"),
).toContain("Error: device lost");
});
});