Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/voice/__tests__/native-call.test.ts

Raw
import { describe, expect, it, vi } from "vitest";
import { NativeVoiceCall } from "../src/native-call.ts";

class FakeProcess {
	onEvent: ((event: any) => void) | undefined;
	calls: string[] = [];
	cueCalls: string[] = [];
	start = vi.fn(async (options: any) => {
		this.onEvent = options.onEvent;
		this.calls.push("start");
	});
	async speak(request: any) {
		this.calls.push(`speak:${request.text}`);
	}
	async complete(id: string) {
		this.calls.push(`complete:${id}`);
	}
	async setHotwords(terms: readonly string[]) {
		this.calls.push(`hotwords:${terms.join(",")}`);
	}
	async consumeFinal(_generation: number, id: number) {
		this.calls.push(`consume:${id}`);
	}
	async interrupt() {
		this.calls.push("interrupt");
	}
	async setMuted(muted: boolean) {
		this.calls.push(`mute:${muted}`);
	}
	async setCueState(state: string) {
		this.cueCalls.push(state);
	}
	async close() {
		this.calls.push("close");
	}
}

function controller() {
	const listeners = new Set<(event: any) => void>();
	const snapshot = {
		status: "ready",
		error: null,
		cueState: "listening",
		humanSpeaking: false,
	};
	let progressId: string | undefined;
	const control = {
		beginNative: vi.fn(),
		narrateExecutorProgress: vi.fn((_isIdle?: () => boolean) => false),
		isNativeProgressCurrent: vi.fn((id: string) => progressId === id),
		cancelNativeProgress: vi.fn(() => {
			if (!progressId) return;
			const id = progressId;
			progressId = undefined;
			for (const listener of listeners)
				listener({ type: "status-interrupt", assistantTurnId: id });
		}),
		endNative: vi.fn(),
		receiveNative: vi.fn((_text: string) => {}),
		interruptNative: vi.fn(),
		onHumanSpeech: vi.fn((active: boolean) => {
			snapshot.humanSpeaking = active;
			if (active) control.cancelNativeProgress();
		}),
		onEmptySpeechBoundary: vi.fn(),
		markNativePlayback: vi.fn(),
		setPlaybackDuration: vi.fn(),
		turnBoundary: vi.fn(),
		classifyTurn: vi.fn(async () => ({ available: false })),
		nativeSnapshot: () => snapshot,
		snapshot,
		nativeHistory: () => [],
		subscribe(listener: (event: any) => void) {
			listeners.add(listener);
			return () => listeners.delete(listener);
		},
		emit(event: any) {
			if (event.type === "assistant-speech" && event.kind === "status")
				progressId = event.assistantTurnId;
			if (event.type === "status-interrupt") progressId = undefined;
			for (const listener of listeners) listener(event);
		},
	};
	return control;
}

const options = {
	backend: { client: { close: vi.fn(async () => {}) } } as any,
	modelSetting: "auto" as const,
	historyDirectory: "/approved/history",
};

async function flush(): Promise<void> {
	await Promise.resolve();
	await Promise.resolve();
	await Promise.resolve();
}

async function completed(promise: Promise<void>): Promise<void> {
	let timer: ReturnType<typeof setTimeout> | undefined;
	try {
		await Promise.race([
			promise,
			new Promise<never>((_resolve, reject) => {
				timer = setTimeout(
					() => reject(new Error("Mock operation did not complete within 1s")),
					1000,
				);
			}),
		]);
	} finally {
		clearTimeout(timer);
	}
}

describe("NativeVoiceCall", () => {
	it("accepts recognition in the readiness callback batch, including immediately after unmute", async () => {
		vi.useFakeTimers();
		const control = controller(),
			process = new FakeProcess();
		const call = new NativeVoiceCall(control as never, () => process as never);
		process.start.mockImplementation(async (options: any) => {
			process.onEvent = options.onEvent;
			options.onEvent({ type: "ready", muted: false });
			options.onEvent({
				type: "preview",
				captionId: 1,
				revision: 1,
				text: "First preview",
			});
		});
		process.setMuted = async (muted) => {
			if (!muted) {
				process.onEvent?.({ type: "ready", muted: false });
				process.onEvent?.({
					type: "final",
					generation: 3,
					requestId: 1,
					text: "Fresh input.",
				});
			}
		};
		try {
			await call.start({} as never, options);
			expect(Reflect.get(call, "state").partial).toBe("First preview");
			await call.mute(true);
			await call.mute(false);
			await vi.advanceTimersByTimeAsync(4_000);
			expect(control.receiveNative).toHaveBeenCalledExactlyOnceWith(
				"Fresh input.",
			);
			expect(process.calls).toContain("consume:1");
		} finally {
			await call.stop();
			vi.useRealTimers();
		}
	});

	it("barge-in fences queued output without discarding unrelated queued input controls", async () => {
		const control = controller(),
			process = new FakeProcess();
		const call = new NativeVoiceCall(control as never, () => process as never);
		let release!: () => void;
		const pending = new Promise<void>((resolve) => {
			release = resolve;
		});
		let hotwordsApplied!: () => void;
		const hotwords = new Promise<void>((resolve) => {
			hotwordsApplied = resolve;
		});
		process.speak = async (request: any) => {
			process.calls.push(`speak:${request.text}`);
			await pending;
		};
		process.setHotwords = async (terms) => {
			process.calls.push(`hotwords:${terms.join(",")}`);
			hotwordsApplied();
		};
		try {
			await call.start({} as never, options);
			control.emit({
				type: "assistant-speech",
				assistantTurnId: "reply",
				userTurnId: "user",
				text: "Already pending.",
			});
			await flush();
			control.emit({
				type: "assistant-speech",
				assistantTurnId: "reply",
				userTurnId: "user",
				text: "Stale queued output.",
			});
			control.emit({
				type: "assistant-complete",
				assistantTurnId: "reply",
				userTurnId: "user",
			});
			process.onEvent?.({ type: "speech", active: true });
			control.emit({ type: "hotwords", terms: ["Nugu"] });
			release();
			await completed(hotwords);
			expect(process.calls).toContain("interrupt");
			expect(process.calls).not.toContain("speak:Stale queued output.");
			expect(process.calls).not.toContain("complete:reply");
		} finally {
			release();
			await call.stop();
		}
	});
	it("contains a failed backend while recovery replaces its process", async () => {
		const initial = new FakeProcess();
		const replacement = new FakeProcess();
		const factory = vi
			.fn()
			.mockReturnValueOnce(initial)
			.mockReturnValue(replacement);
		const call = new NativeVoiceCall(controller() as never, factory);
		try {
			await call.start({} as never, options);
			expect(initial.start.mock.calls[0]![0].backend).toBe(options.backend);
			initial.onEvent?.({
				type: "error",
				error: "test socket closed",
				recoverable: true,
			});
			await flush();
			await flush();
			expect(factory).toHaveBeenCalledTimes(2);
			expect(replacement.start).toHaveBeenCalledOnce();
			expect(initial.calls).toContain("close");
			expect(Reflect.get(call, "state").status).toBe("ready");
		} finally {
			await call.stop();
		}
	});
	it("feeds speech detection into waveform emphasis and clears it on silence and mute", async () => {
		const control = controller(),
			process = new FakeProcess();
		const call = new NativeVoiceCall(control as never, () => process as never);
		try {
			await call.start({} as never, options);
			process.onEvent?.({ type: "speech", active: true });
			expect(Reflect.get(call, "state").humanSpeaking).toBe(true);
			process.onEvent?.({ type: "speech", active: false });
			expect(Reflect.get(call, "state").humanSpeaking).toBe(false);
			process.onEvent?.({ type: "speech", active: true });
			await call.mute(true);
			expect(Reflect.get(call, "state").humanSpeaking).toBe(false);
			await call.mute(false);
			expect(Reflect.get(call, "state").humanSpeaking).toBe(false);
		} finally {
			await call.stop();
		}
	});
	it("does not confuse command acceptance, caption onset or a boundary with confirmed playback", async () => {
		const control = controller();
		const process = new FakeProcess();
		const call = new NativeVoiceCall(control as never, () => process as never);
		try {
			await call.start({} as never, options);
			control.emit({
				type: "assistant-speech",
				assistantTurnId: "reply",
				userTurnId: "user",
				text: "A real answer.",
			});
			control.emit({
				type: "assistant-complete",
				assistantTurnId: "reply",
				userTurnId: "user",
			});
			await vi.waitFor(() => expect(process.calls).toContain("complete:reply"));
			process.onEvent?.({
				type: "phrase",
				assistantTurnId: "reply",
				userTurnId: "user",
				text: "A real answer.",
			});
			process.onEvent?.({ type: "boundary", assistantTurnId: "reply" });
			expect(control.markNativePlayback).not.toHaveBeenCalled();
			process.onEvent?.({
				type: "playback",
				assistantTurnId: "reply",
				userTurnId: "user",
				playedText: "A real answer.",
				cumulativeMs: 500,
			});
			expect(control.markNativePlayback).toHaveBeenCalledExactlyOnceWith(
				"reply",
				"A real answer.",
			);
		} finally {
			await call.stop();
		}
	});
	it("retires an empty cosmetic segment without introducing an authoritative empty boundary", async () => {
		const control = controller(),
			process = new FakeProcess();
		const call = new NativeVoiceCall(control as never, () => process as never);
		try {
			await call.start({} as never, options);
			process.onEvent?.({
				type: "final",
				generation: 1,
				requestId: 1,
				captionId: 1,
				previewRevision: 1,
				text: "Pending final A",
			});
			process.onEvent?.({ type: "speech", active: true, captionId: 2 });
			process.onEvent?.({
				type: "preview",
				captionId: 2,
				revision: 3,
				text: "Unconfirmed B",
			});
			process.onEvent?.({ type: "speech", active: false });
			const classifyCalls = control.classifyTurn.mock.calls.length;
			process.onEvent?.({
				type: "caption-empty",
				captionId: 2,
				previewRevision: 3,
			});
			expect(Reflect.get(call, "state").partial).toBe("");
			expect(
				Reflect.get(call, "finals").map(
					(final: { text: string }) => final.text,
				),
			).toEqual(["Pending final A"]);
			expect(control.onEmptySpeechBoundary).not.toHaveBeenCalled();
			expect(control.classifyTurn).toHaveBeenCalledTimes(classifyCalls);
			process.onEvent?.({
				type: "preview",
				captionId: 2,
				revision: 3,
				text: "Late hallucinated B",
			});
			expect(Reflect.get(call, "state").partial).toBe("");
		} finally {
			await call.stop();
		}
	});

	it("clears older provisional words on a new authoritative caption even if its cosmetic clear was dropped", async () => {
		const control = controller(),
			process = new FakeProcess();
		const call = new NativeVoiceCall(control as never, () => process as never);
		try {
			await call.start({} as never, options);
			process.onEvent?.({
				type: "preview",
				captionId: 1,
				revision: 1,
				text: "Previous words",
			});
			process.onEvent?.({ type: "speech", active: true, captionId: 2 });
			expect(Reflect.get(call, "state")).toMatchObject({
				partial: "",
				provisional: true,
			});
			process.onEvent?.({
				type: "preview",
				captionId: 1,
				revision: 99,
				text: "Previous words",
			});
			expect(Reflect.get(call, "state").partial).toBe("");
		} finally {
			await call.stop();
		}
	});
	it("keeps previews cosmetic, commits finals immediately and fences late caption revisions", async () => {
		vi.useFakeTimers();
		const control = controller(),
			process = new FakeProcess();
		const call = new NativeVoiceCall(control as never, () => process as never);
		try {
			await call.start({} as never, options);
			process.onEvent?.({
				type: "preview",
				captionId: 1,
				revision: 2,
				text: "Uncertain draft",
			});
			expect(Reflect.get(call, "state")).toMatchObject({
				partial: "Uncertain draft",
				provisional: true,
			});
			expect(Reflect.get(call, "inputPending")).toBe(false);
			expect(Reflect.get(call, "finals")).toEqual([]);
			expect(control.cancelNativeProgress).not.toHaveBeenCalled();
			expect(control.classifyTurn).not.toHaveBeenCalled();
			expect(control.receiveNative).not.toHaveBeenCalled();
			expect(control.interruptNative).not.toHaveBeenCalled();
			process.onEvent?.({
				type: "final",
				captionId: 1,
				previewRevision: 2,
				generation: 1,
				requestId: 1,
				text: "Accurate final",
			});
			expect(Reflect.get(call, "state")).toMatchObject({
				partial: "Accurate final",
				provisional: false,
				captionRevision: 1,
			});
			process.onEvent?.({
				type: "preview",
				captionId: 1,
				revision: 2,
				text: "Obsolete draft",
			});
			expect(Reflect.get(call, "state").partial).toBe("Accurate final");
			// A fresh worker revision can show the remaining live monologue.
			process.onEvent?.({
				type: "preview",
				captionId: 1,
				revision: 3,
				text: "Newer ongoing speech",
			});
			expect(Reflect.get(call, "state").partial).toBe("Newer ongoing speech");
			await vi.advanceTimersByTimeAsync(4_000);
			expect(control.receiveNative).toHaveBeenCalledWith("Accurate final");
			expect(control.receiveNative).not.toHaveBeenCalledWith("Uncertain draft");
		} finally {
			await call.stop();
			vi.useRealTimers();
		}
	});

	it("does not let an older authoritative segment overwrite the newer utterance preview", async () => {
		const control = controller(),
			process = new FakeProcess();
		const call = new NativeVoiceCall(control as never, () => process as never);
		try {
			await call.start({} as never, options);
			process.onEvent?.({ type: "speech", active: true, captionId: 2 });
			process.onEvent?.({
				type: "preview",
				captionId: 2,
				revision: 4,
				text: "Current speech",
			});
			process.onEvent?.({
				type: "final",
				captionId: 1,
				previewRevision: 2,
				generation: 1,
				requestId: 1,
				text: "Earlier final",
			});
			expect(Reflect.get(call, "state").partial).toBe("Current speech");
			expect(Reflect.get(call, "finals")[0]?.text).toBe("Earlier final");
			process.onEvent?.({
				type: "preview",
				captionId: 1,
				revision: 99,
				text: "Stale utterance",
			});
			expect(Reflect.get(call, "state").partial).toBe("Current speech");
			await call.mute(true);
			process.onEvent?.({
				type: "preview",
				captionId: 3,
				revision: 100,
				text: "Muted draft",
			});
			expect(Reflect.get(call, "state").partial).not.toBe("Muted draft");
		} finally {
			await call.stop();
		}
	});
	it("uses heartbeats only when synthesis, capture and final delivery are idle", async () => {
		const control = controller(),
			process = new FakeProcess();
		const call = new NativeVoiceCall(control as never, () => process as never);
		const heartbeat = (busy = false, playing = false) =>
			process.onEvent?.({
				type: "audio-state",
				busy,
				playing,
				inputLevel: 0,
				outputLevel: 0,
				waveform: [],
			});
		try {
			await call.start({} as never, options);
			process.onEvent?.({ type: "input-state", pending: true });
			heartbeat();
			expect(control.narrateExecutorProgress).not.toHaveBeenCalled();
			process.onEvent?.({ type: "input-state", pending: false });
			heartbeat();
			expect(control.narrateExecutorProgress).toHaveBeenCalledTimes(1);
			const recheck = control.narrateExecutorProgress.mock.calls[0]?.[0];
			expect(recheck?.()).toBe(true);
			heartbeat(true);
			expect(recheck?.()).toBe(false);
			heartbeat(false, true);
			expect(control.narrateExecutorProgress).toHaveBeenCalledTimes(1);
			await call.mute(true);
			heartbeat();
			expect(control.narrateExecutorProgress).toHaveBeenCalledTimes(1);
			await call.mute(false);
			process.onEvent?.({
				type: "final",
				text: "pending input",
				generation: 1,
				requestId: 1,
			});
			heartbeat();
			expect(control.narrateExecutorProgress).toHaveBeenCalledTimes(1);
		} finally {
			await call.stop();
		}
	});

	it.each(["speak", "complete"] as const)(
		"releases status ownership when %s rejects without a worker error or boundary",
		async (command) => {
			const control = controller(),
				process = new FakeProcess();
			const call = new NativeVoiceCall(
				control as never,
				() => process as never,
			);
			try {
				await call.start({} as never, options);
				vi.spyOn(process, command).mockRejectedValueOnce(
					new Error("status command rejected"),
				);
				const cancelNativeProgress =
					control.cancelNativeProgress.getMockImplementation();
				let ownershipReleased!: () => void;
				const ownershipRelease = new Promise<void>((resolve) => {
					ownershipReleased = resolve;
				});
				control.cancelNativeProgress.mockImplementation(() => {
					cancelNativeProgress?.();
					ownershipReleased();
				});
				control.emit({
					type: "assistant-speech",
					kind: "status",
					assistantTurnId: "status",
					userTurnId: "status-user",
					text: "Checking voice tests.",
				});
				control.emit({
					type: "assistant-complete",
					kind: "status",
					assistantTurnId: "status",
					userTurnId: "status-user",
				});
				await completed(ownershipRelease);
				expect(control.isNativeProgressCurrent("status")).toBe(false);
				expect(process.calls).toContain("interrupt");
				let replyCompleted!: () => void;
				const replyCompletion = new Promise<void>((resolve) => {
					replyCompleted = resolve;
				});
				process.complete = async (id) => {
					process.calls.push(`complete:${id}`);
					if (id === "reply") replyCompleted();
				};
				control.emit({
					type: "assistant-speech",
					assistantTurnId: "reply",
					userTurnId: "user",
					text: "Your reply.",
				});
				control.emit({
					type: "assistant-complete",
					assistantTurnId: "reply",
					userTurnId: "user",
				});
				await completed(replyCompletion);
				// A failed status utterance must not restart capture or disturb the call.
				expect(process.start).toHaveBeenCalledOnce();
			} finally {
				await call.stop();
			}
		},
	);

	it("cannot resurrect queued status speech or completion after cancellation", async () => {
		const control = controller(),
			process = new FakeProcess();
		const call = new NativeVoiceCall(control as never, () => process as never);
		try {
			await call.start({} as never, options);
			control.emit({
				type: "assistant-speech",
				kind: "status",
				assistantTurnId: "status",
				userTurnId: "status-user",
				text: "Still working.",
			});
			control.emit({
				type: "assistant-complete",
				kind: "status",
				assistantTurnId: "status",
				userTurnId: "status-user",
			});
			process.onEvent?.({ type: "input-state", pending: true });
			await flush();
			await flush();
			expect(process.calls).toContain("interrupt");
			expect(process.calls).not.toContain("speak:Still working.");
			expect(process.calls).not.toContain("complete:status");
		} finally {
			await call.stop();
		}
	});

	it("interrupts in-flight status before replacement speech and skips stale completion", async () => {
		const control = controller(),
			process = new FakeProcess();
		const call = new NativeVoiceCall(control as never, () => process as never);
		let release!: () => void;
		let replacementStarted!: () => void;
		const replacementStart = new Promise<void>((resolve) => {
			replacementStarted = resolve;
		});
		vi.spyOn(process, "speak").mockImplementation(async (request) => {
			process.calls.push(`speak:${request.text}`);
			if (request.assistantTurnId === "status") {
				await new Promise<void>((resolve) => {
					release = resolve;
				});
			} else replacementStarted();
		});
		try {
			await call.start({} as never, options);
			control.emit({
				type: "assistant-speech",
				kind: "status",
				assistantTurnId: "status",
				userTurnId: "s",
				text: "Still working.",
			});
			control.emit({
				type: "assistant-complete",
				kind: "status",
				assistantTurnId: "status",
				userTurnId: "s",
			});
			await flush();
			process.onEvent?.({ type: "input-state", pending: true });
			process.onEvent?.({ type: "input-state", pending: false });
			control.emit({
				type: "assistant-speech",
				assistantTurnId: "normal",
				userTurnId: "human",
				text: "Your reply.",
			});
			release();
			await completed(replacementStart);
			expect(process.calls).not.toContain("complete:status");
			expect(process.calls.indexOf("interrupt")).toBeGreaterThan(
				process.calls.indexOf("speak:Still working."),
			);
			expect(process.calls.indexOf("speak:Your reply.")).toBeGreaterThan(
				process.calls.indexOf("interrupt"),
			);
		} finally {
			await call.stop();
		}
	});

	it("scrolls only the active call's widget without touching the conversation or audio", async () => {
		const process = new FakeProcess();
		const call = new NativeVoiceCall(
			controller() as never,
			() => process as never,
		);
		const widget = call.createWidget(
			{ requestRender: vi.fn(), terminal: { rows: 24 } } as never,
			{
				fg: (_color: string, text: string) => text,
				bold: (text: string) => text,
			} as never,
		);
		const scroll = vi.spyOn(widget, "scroll");
		try {
			call.scrollWidget("spoken", -3);
			expect(scroll).not.toHaveBeenCalled();
			await call.start({} as never, options);
			call.scrollWidget("spoken", -3);
			expect(scroll).toHaveBeenCalledExactlyOnceWith("spoken", -3);
			await call.stop();
			call.scrollWidget("heard", 3);
			expect(scroll).toHaveBeenCalledTimes(1);
			expect(process.calls).toEqual(["start", "close"]);
		} finally {
			widget.dispose();
		}
	});
	it("contains conversation initialization failure and permits explicit retry without opening audio early", async () => {
		const control = controller(),
			process = new FakeProcess();
		control.beginNative.mockImplementationOnce(() => {
			throw new Error("history unavailable");
		});
		const call = new NativeVoiceCall(control as never, () => process as never);
		await expect(call.start({} as never, options)).rejects.toThrow(
			"history unavailable",
		);
		expect(process.start).not.toHaveBeenCalled();
		await call.resume();
		expect(process.start).toHaveBeenCalledOnce();
		await call.stop();
	});

	it("does not run recovery concurrently with a rejecting initial startup", async () => {
		const control = controller(),
			initial = new FakeProcess(),
			replacement = new FakeProcess();
		let rejectStart: ((error: Error) => void) | undefined;
		initial.start.mockImplementation((options) => {
			initial.onEvent = options.onEvent;
			return new Promise<void>((_resolve, reject) => {
				rejectStart = reject;
			});
		});
		const processes = [initial, replacement];
		const call = new NativeVoiceCall(
			control as never,
			() => processes.shift() as never,
		);
		const starting = call.start({} as never, options);
		await flush();
		initial.onEvent?.({
			type: "error",
			error: "startup failed",
			recoverable: true,
		});
		rejectStart?.(new Error("startup failed"));
		await expect(starting).rejects.toThrow("startup failed");
		expect(replacement.start).not.toHaveBeenCalled();
		await call.resume();
		expect(replacement.start).toHaveBeenCalledOnce();
		await call.stop();
	});

	it("rejects resume during recovery without invalidating its owner epoch", async () => {
		const control = controller(),
			initial = new FakeProcess(),
			replacement = new FakeProcess();
		const processes = [initial, replacement];
		const call = new NativeVoiceCall(
			control as never,
			() => processes.shift() as never,
		);
		await call.start({} as never, options);
		let finishClose: (() => void) | undefined;
		vi.spyOn(initial, "close").mockImplementation(
			() =>
				new Promise<void>((resolve) => {
					finishClose = resolve;
				}),
		);
		initial.onEvent?.({ type: "error", error: "lost", recoverable: true });
		await expect(call.resume()).rejects.toThrow("already recovering");
		finishClose?.();
		await flush();
		await flush();
		expect(replacement.start).toHaveBeenCalledOnce();
		await call.resume();
		expect(replacement.calls).toContain("mute:false");
		await call.stop();
	});
	it("does not resurrect an older start after its slow stop completes", async () => {
		const control = controller(),
			initial = new FakeProcess(),
			newest = new FakeProcess(),
			obsolete = new FakeProcess();
		const processes = [initial, newest, obsolete];
		const call = new NativeVoiceCall(
			control as never,
			() => processes.shift() as never,
		);
		await call.start({} as never, options);
		let finishClose: (() => void) | undefined;
		vi.spyOn(initial, "close").mockImplementation(
			() =>
				new Promise<void>((resolve) => {
					finishClose = resolve;
				}),
		);
		const older = call.start({} as never, options);
		await flush();
		const latest = call.start({} as never, options);
		await flush();
		expect(newest.start).not.toHaveBeenCalled();
		finishClose?.();
		await Promise.all([older, latest]);
		expect(control.beginNative).toHaveBeenCalledTimes(2);
		expect(obsolete.start).not.toHaveBeenCalled();
		expect(newest.calls).not.toContain("close");
		await call.stop();
	});

	it("late recovery failure closes only its own process, never a newer call", async () => {
		const control = controller(),
			initial = new FakeProcess(),
			delayed = new FakeProcess(),
			newest = new FakeProcess();
		let rejectStart: ((error: Error) => void) | undefined;
		delayed.start.mockImplementation(
			() =>
				new Promise<void>((_resolve, reject) => {
					rejectStart = reject;
				}),
		);
		const processes = [initial, delayed, newest];
		const call = new NativeVoiceCall(
			control as never,
			() => processes.shift() as never,
		);
		await call.start({} as never, options);
		initial.onEvent?.({ type: "error", error: "lost", recoverable: true });
		await flush();
		expect(delayed.start).toHaveBeenCalledOnce();
		await call.stop();
		await call.start({} as never, options);
		rejectStart?.(new Error("late startup rejection"));
		await flush();
		await flush();
		expect(delayed.calls.filter((entry) => entry === "close")).toHaveLength(2);
		expect(newest.calls).not.toContain("close");
		await call.stop();
	});

	it.each(["speech", "error"] as const)(
		"contains background persistence failure during %s",
		async (type) => {
			const control = controller(),
				process = new FakeProcess();
			const call = new NativeVoiceCall(
				control as never,
				() => process as never,
			);
			await call.start({} as never, options);
			control.interruptNative.mockImplementation(() => {
				throw new Error("history write failed");
			});
			control.endNative.mockImplementation(() => {
				throw new Error("history write failed");
			});
			process.onEvent?.(
				type === "speech"
					? { type, active: true }
					: { type, error: "lost", recoverable: true },
			);
			await flush();
			await flush();
			expect(process.calls).toContain("close");
		},
	);

	it("closes audio even when explicit stop cannot persist history", async () => {
		const control = controller(),
			process = new FakeProcess();
		const call = new NativeVoiceCall(control as never, () => process as never);
		await call.start({} as never, options);
		control.endNative.mockImplementation(() => {
			throw new Error("history write failed");
		});
		await expect(call.stop()).rejects.toThrow("history write failed");
		expect(process.calls).toContain("close");
	});
	it("holds grouped finals through classifier then consumes accepted final", async () => {
		vi.useFakeTimers();
		const control = controller();
		const process = new FakeProcess();
		const call = new NativeVoiceCall(control as never, () => process as never);
		await call.start({} as never, options);
		process.onEvent?.({
			type: "final",
			text: "hello",
			generation: 1,
			requestId: 3,
		});
		await flush();
		expect(control.receiveNative).not.toHaveBeenCalled();
		await vi.advanceTimersByTimeAsync(1_500);
		expect(control.receiveNative).toHaveBeenCalledWith("hello");
		expect(process.calls).toContain("consume:3");
		vi.useRealTimers();
	});

	it("holds early finals through continued speech and starts the hard cap only after speech ends", async () => {
		vi.useFakeTimers();
		const control = controller(),
			process = new FakeProcess();
		control.classifyTurn.mockImplementation(
			() => new Promise<{ available: boolean }>(() => {}),
		);
		const call = new NativeVoiceCall(control as never, () => process as never);
		try {
			await call.start({} as never, options);
			process.onEvent?.({
				type: "final",
				text: "first part",
				generation: 1,
				requestId: 1,
			});
			await vi.advanceTimersByTimeAsync(1_000);
			process.onEvent?.({ type: "speech", active: true });
			process.onEvent?.({ type: "partial", text: "continuing" });
			await vi.advanceTimersByTimeAsync(1_000);
			process.onEvent?.({
				type: "final",
				text: "second part",
				generation: 1,
				requestId: 2,
			});
			await vi.advanceTimersByTimeAsync(999);
			expect(control.receiveNative).not.toHaveBeenCalled();
			process.onEvent?.({ type: "partial", text: "still speaking" });
			await vi.advanceTimersByTimeAsync(4_001);
			expect(control.receiveNative).not.toHaveBeenCalled();
			expect(
				process.calls.filter((value) => value.startsWith("consume:")),
			).toEqual([]);
			process.onEvent?.({ type: "speech", active: false });
			await vi.advanceTimersByTimeAsync(2_999);
			expect(control.receiveNative).not.toHaveBeenCalled();
			await vi.advanceTimersByTimeAsync(1);
			expect(control.receiveNative).toHaveBeenCalledExactlyOnceWith(
				"first part second part",
			);
			expect(control.interruptNative).toHaveBeenCalledWith(true);
			expect(
				process.calls.filter((value) => value.startsWith("consume:")),
			).toEqual(["consume:1", "consume:2"]);
		} finally {
			await call.stop();
			vi.useRealTimers();
		}
	});

	it("ignores a stale classifier decision when speech resumes mid-thought", async () => {
		vi.useFakeTimers();
		const control = controller(),
			process = new FakeProcess();
		let finish!: (result: { available: boolean }) => void;
		control.classifyTurn.mockImplementationOnce(
			() =>
				new Promise((resolve) => {
					finish = resolve;
				}),
		);
		const call = new NativeVoiceCall(control as never, () => process as never);
		try {
			await call.start({} as never, options);
			process.onEvent?.({
				type: "final",
				text: "first part",
				generation: 1,
				requestId: 1,
			});
			const decision = Reflect.get(call, "classifyAbort") as AbortController;
			process.onEvent?.({ type: "speech", active: true });
			expect(decision.signal.aborted).toBe(true);
			finish({ available: false });
			await vi.advanceTimersByTimeAsync(4_000);
			expect(control.receiveNative).not.toHaveBeenCalled();
			process.onEvent?.({
				type: "final",
				text: "second part",
				generation: 1,
				requestId: 2,
			});
			process.onEvent?.({ type: "speech", active: false });
			await vi.advanceTimersByTimeAsync(1_500);
			expect(control.receiveNative).toHaveBeenCalledExactlyOnceWith(
				"first part second part",
			);
		} finally {
			await call.stop();
			vi.useRealTimers();
		}
	});

	it.each([0, 1_500])(
		"retains ordered queued speech when a newer deadline waits %i ms",
		async (delay) => {
			vi.useFakeTimers();
			const control = controller(),
				process = new FakeProcess();
			const call = new NativeVoiceCall(
				control as never,
				() => process as never,
			);
			let release!: () => void;
			try {
				await call.start({} as never, options);
				Reflect.set(
					call,
					"serial",
					new Promise<void>((resolve) => {
						release = resolve;
					}),
				);
				process.onEvent?.({
					type: "final",
					text: "first part",
					generation: 1,
					requestId: 1,
				});
				await vi.advanceTimersByTimeAsync(1_500);
				expect(control.receiveNative).not.toHaveBeenCalled();
				process.onEvent?.({ type: "speech", active: true });
				process.onEvent?.({
					type: "final",
					text: "second part",
					generation: 1,
					requestId: 2,
				});
				process.onEvent?.({ type: "speech", active: false });
				await vi.advanceTimersByTimeAsync(delay);
				release();
				await vi.advanceTimersByTimeAsync(1_499);
				expect(control.receiveNative).not.toHaveBeenCalled();
				await vi.advanceTimersByTimeAsync(1);
				expect(control.receiveNative).toHaveBeenCalledExactlyOnceWith(
					"first part second part",
				);
				expect(
					process.calls.filter((value) => value.startsWith("consume:")),
				).toEqual(["consume:1", "consume:2"]);
			} finally {
				release?.();
				await call.stop();
				vi.useRealTimers();
			}
		},
	);

	it("never forces a mid-speech commit to relieve the bounded final queue", async () => {
		vi.useFakeTimers();
		const control = controller(),
			process = new FakeProcess();
		const call = new NativeVoiceCall(control as never, () => process as never);
		try {
			await call.start({} as never, options);
			process.onEvent?.({ type: "speech", active: true });
			for (let requestId = 1; requestId <= 9; requestId++)
				process.onEvent?.({
					type: "final",
					text: "continued speech",
					generation: 1,
					requestId,
				});
			await vi.advanceTimersByTimeAsync(5_000);
			expect(control.receiveNative).not.toHaveBeenCalled();
			expect(control.classifyTurn).not.toHaveBeenCalled();
			expect(
				process.calls.filter((value) => value.startsWith("consume:")),
			).toEqual([]);
			expect(Reflect.get(call, "state")).toMatchObject({
				status: "unavailable",
				error: expect.stringContaining("input bound"),
			});
		} finally {
			await call.stop();
			vi.useRealTimers();
		}
	});

	it("submits large final batches without slicing and splits before the 100k bound", async () => {
		vi.useFakeTimers();
		const control = controller(),
			process = new FakeProcess();
		const call = new NativeVoiceCall(control as never, () => process as never);
		const parts = Array.from({ length: 7 }, (_, i) => String(i).repeat(16_000));
		try {
			await call.start({} as never, options);
			for (const [i, text] of parts.entries())
				process.onEvent?.({
					type: "final",
					text,
					generation: 1,
					requestId: i + 1,
				});
			await vi.advanceTimersByTimeAsync(3_000);
			const submitted = control.receiveNative.mock.calls.map((args) => args[0]);
			expect(submitted).toHaveLength(2);
			expect(submitted.join(" ")).toBe(parts.join(" "));
			expect(submitted.every((text) => String(text).length <= 100_000)).toBe(
				true,
			);
			expect(
				process.calls.filter((value) => value.startsWith("consume:")),
			).toHaveLength(7);
		} finally {
			await call.stop();
			vi.useRealTimers();
		}
	});

	it("preserves final batches when more than eight finals arrive before the deadline", async () => {
		vi.useFakeTimers();
		const control = controller(),
			process = new FakeProcess();
		const call = new NativeVoiceCall(control as never, () => process as never);
		try {
			await call.start({} as never, options);
			for (let i = 0; i < 9; i++)
				process.onEvent?.({
					type: "final",
					text: `part-${i}`,
					generation: 1,
					requestId: i + 1,
				});
			await vi.advanceTimersByTimeAsync(3_000);
			expect(
				control.receiveNative.mock.calls.map((args) => args[0]).join(" "),
			).toBe(Array.from({ length: 9 }, (_, i) => `part-${i}`).join(" "));
			expect(process.calls).not.toContain("close");
		} finally {
			await call.stop();
			vi.useRealTimers();
		}
	});

	it("sets current caption only after first PCM phrase", async () => {
		const control = controller();
		const process = new FakeProcess();
		const call = new NativeVoiceCall(control as never, () => process as never);
		const widget = call.createWidget(
			{ requestRender() {}, terminal: { rows: 24 } } as never,
			{
				fg: (_: string, text: string) => text,
				bold: (text: string) => text,
			} as never,
		);
		await call.start({} as never, options);
		control.emit({
			type: "assistant-speech",
			assistantTurnId: "a",
			userTurnId: "u",
			text: "queued",
		});
		await flush();
		expect(widget.render(120).join("\n")).toContain("Nothing spoken yet");
		process.onEvent?.({
			type: "phrase",
			assistantTurnId: "a",
			userTurnId: "u",
			text: "played",
		});
		expect(widget.render(120).join("\n")).toContain(" played");
		await call.stop();
		widget.dispose();
	});

	it("fences callbacks after stop without touching editor or native questions", async () => {
		const control = controller();
		const process = new FakeProcess();
		const call = new NativeVoiceCall(control as never, () => process as never);
		await call.start({} as never, options);
		await call.stop();
		process.onEvent?.({
			type: "final",
			text: "late",
			generation: 1,
			requestId: 1,
		});
		expect(control.receiveNative).not.toHaveBeenCalled();
		expect(control).not.toHaveProperty("setEditorText");
		expect(control).not.toHaveProperty("answerQuestion");
	});

	it("detaches a timed-out recovery before slow close permits late readiness", async () => {
		vi.useFakeTimers();
		const control = controller(),
			initial = new FakeProcess(),
			delayed = new FakeProcess(),
			replacement = new FakeProcess();
		let finishStart!: () => void;
		let finishClose!: () => void;
		delayed.start.mockImplementation((options: any) => {
			delayed.onEvent = options.onEvent;
			return new Promise<void>((resolve) => {
				finishStart = resolve;
			});
		});
		const closing = new Promise<void>((resolve) => {
			finishClose = resolve;
		});
		delayed.close = async () => {
			await closing;
		};
		const processes = [initial, delayed, replacement];
		const call = new NativeVoiceCall(
			control as never,
			() => processes.shift() as never,
		);
		try {
			await call.start({} as never, options);
			initial.onEvent?.({
				type: "error",
				error: "Disconnected",
				recoverable: true,
			});
			await flush();
			await flush();
			await vi.advanceTimersByTimeAsync(15_000);
			delayed.onEvent?.({ type: "ready", muted: false });
			delayed.onEvent?.({
				type: "final",
				text: "Stale input.",
				generation: 1,
				requestId: 1,
			});
			finishStart();
			await flush();
			expect(Reflect.get(call, "state").status).toBe("reconnecting");
			expect(control.receiveNative).not.toHaveBeenCalled();
			finishClose();
			await flush();
			await flush();
			expect(replacement.start).toHaveBeenCalledOnce();
			expect(Reflect.get(call, "state").status).toBe("ready");
		} finally {
			finishClose();
			await call.stop();
			vi.useRealTimers();
		}
	});

	it("closes late recovery startups, exhausts recovery, then explicitly recreates call", async () => {
		vi.useFakeTimers();
		const control = controller();
		const initial = new FakeProcess();
		const stuck = new FakeProcess();
		stuck.start.mockImplementation(() => new Promise<void>(() => undefined));
		const resumed = new FakeProcess();
		const replacements = [initial, stuck, stuck, resumed];
		const call = new NativeVoiceCall(
			control as never,
			() => replacements.shift() as never,
		);
		await call.start({} as never, options);
		initial.onEvent?.({ type: "error", error: "lost", recoverable: true });
		await vi.advanceTimersByTimeAsync(15_000);
		await vi.advanceTimersByTimeAsync(15_000);
		await flush();
		expect(stuck.calls).toContain("close");
		expect(control.endNative).toHaveBeenCalled();
		await call.resume();
		expect(control.beginNative).toHaveBeenCalledTimes(2);
		vi.useRealTimers();
	});

	it("combines final segments into one voice turn before acknowledging each", async () => {
		vi.useFakeTimers();
		const control = controller(),
			process = new FakeProcess();
		const call = new NativeVoiceCall(control as never, () => process as never);
		try {
			await call.start({} as never, options);
			process.onEvent?.({
				type: "final",
				text: "please inspect",
				generation: 1,
				requestId: 1,
			});
			process.onEvent?.({
				type: "final",
				text: "this file",
				generation: 1,
				requestId: 2,
			});
			await flush();
			await vi.advanceTimersByTimeAsync(1500);
			expect(control.receiveNative).toHaveBeenCalledExactlyOnceWith(
				"please inspect this file",
			);
			expect(
				process.calls.filter((value) => value.startsWith("consume:")),
			).toEqual(["consume:1", "consume:2"]);
		} finally {
			await call.stop();
			vi.useRealTimers();
		}
	});

	it("keeps classifier delay inside the hard endpoint deadline", async () => {
		vi.useFakeTimers();
		const control = controller(),
			process = new FakeProcess();
		let finish: ((value: { available: boolean }) => void) | undefined;
		control.classifyTurn.mockImplementation(
			() =>
				new Promise((resolve) => {
					finish = resolve;
				}),
		);
		const call = new NativeVoiceCall(control as never, () => process as never);
		try {
			await call.start({} as never, options);
			process.onEvent?.({
				type: "final",
				text: "hello",
				generation: 1,
				requestId: 1,
			});
			await vi.advanceTimersByTimeAsync(2000);
			finish?.({ available: false });
			await flush();
			await vi.advanceTimersByTimeAsync(999);
			expect(control.receiveNative).not.toHaveBeenCalled();
			await vi.advanceTimersByTimeAsync(1);
			expect(control.receiveNative).toHaveBeenCalledExactlyOnceWith("hello");
		} finally {
			await call.stop();
			vi.useRealTimers();
		}
	});

	it("deduplicates semantic cues and yields them to human speech", async () => {
		const control = controller(),
			process = new FakeProcess();
		const call = new NativeVoiceCall(control as never, () => process as never);
		await call.start({} as never, options);
		await flush();
		expect(process.cueCalls).toEqual(["listening"]);
		control.emit({ type: "native-state" });
		await flush();
		expect(process.cueCalls).toEqual(["listening"]);
		control.snapshot.cueState = "working";
		control.emit({ type: "native-state" });
		await flush();
		expect(process.cueCalls).toEqual(["listening", "working"]);
		process.onEvent?.({ type: "speech", active: true });
		await flush();
		expect(process.cueCalls).toEqual(["listening", "working", "off"]);
		await call.stop();
	});

	it("clears human-speaking state before recovery after a lost speech-end", async () => {
		const control = controller(),
			initial = new FakeProcess(),
			replacement = new FakeProcess();
		const processes = [initial, replacement];
		const call = new NativeVoiceCall(
			control as never,
			() => processes.shift() as never,
		);
		await call.start({} as never, options);
		initial.onEvent?.({ type: "speech", active: true });
		await flush();
		expect(control.snapshot.humanSpeaking).toBe(true);
		initial.onEvent?.({ type: "error", error: "lost", recoverable: true });
		await flush();
		await flush();
		expect(control.snapshot.humanSpeaking).toBe(false);
		expect(replacement.cueCalls).toContain("listening");
		await call.stop();
	});

	it("preserves mute while recovery waits for old close and new startup", async () => {
		const control = controller(),
			initial = new FakeProcess(),
			replacement = new FakeProcess();
		let finishClose: (() => void) | undefined;
		let releaseStart: (() => void) | undefined;
		vi.spyOn(initial, "close").mockImplementation(
			() =>
				new Promise<void>((resolve) => {
					finishClose = resolve;
				}),
		);
		replacement.start.mockImplementation(
			(startOptions) =>
				new Promise<void>((resolve) => {
					replacement.onEvent = startOptions.onEvent;
					releaseStart = resolve;
				}),
		);
		const processes = [initial, replacement];
		const call = new NativeVoiceCall(
			control as never,
			() => processes.shift() as never,
		);
		await call.start({} as never, options);
		initial.onEvent?.({ type: "error", error: "lost", recoverable: true });
		await flush();
		await call.mute(true);
		finishClose?.();
		await flush();
		expect(replacement.start).toHaveBeenCalledOnce();
		expect(replacement.calls).toContain("mute:true");
		releaseStart?.();
		await flush();
		expect(
			replacement.calls.filter((entry) => entry === "mute:true"),
		).toHaveLength(1);
		await call.stop();
	});

	it("stops without waiting for an unresolved cue acknowledgement", async () => {
		const control = controller(),
			process = new FakeProcess();
		process.setCueState = vi.fn(() => new Promise<void>(() => undefined));
		const call = new NativeVoiceCall(control as never, () => process as never);
		await call.start({} as never, options);
		await call.stop();
		expect(process.calls).toContain("close");
	});

	it("forwards opening mute without aborting startup", async () => {
		const control = controller(),
			process = new FakeProcess();
		let release: (() => void) | undefined;
		process.start.mockImplementation(
			(options) =>
				new Promise<void>((resolve) => {
					process.onEvent = options.onEvent;
					release = resolve;
				}),
		);
		const call = new NativeVoiceCall(control as never, () => process as never);
		const opening = call.start({} as never, options);
		await flush();
		await call.mute(true);
		expect(process.calls).toContain("mute:true");
		release?.();
		await opening;
		expect(process.start).toHaveBeenCalledOnce();
		await call.stop();
	});

	it("propagates mute to recognition and refuses fake readiness after hangup", async () => {
		vi.useFakeTimers();
		const control = controller(),
			process = new FakeProcess();
		const call = new NativeVoiceCall(control as never, () => process as never);
		try {
			await call.start({} as never, options);
			await call.mute(true);
			expect(process.calls).toContain("mute:true");
			process.onEvent?.({
				type: "final",
				text: "private",
				generation: 1,
				requestId: 1,
			});
			await flush();
			await vi.advanceTimersByTimeAsync(3000);
			expect(control.receiveNative).not.toHaveBeenCalled();
			await call.mute(false);
			expect(process.calls).toContain("mute:false");
			await call.stop();
			await expect(call.mute(false)).rejects.toThrow("Start native voice");
		} finally {
			await call.stop();
			vi.useRealTimers();
		}
	});
	it("permits executor progress across a live cue but not pending recognition or synthesis", async () => {
		const control = controller(),
			process = new FakeProcess();
		const call = new NativeVoiceCall(control as never, () => process as never);
		try {
			await call.start({} as never, options);
			control.snapshot.cueState = "working";
			control.emit({ type: "native-state" });
			process.onEvent?.({
				type: "audio-state",
				inputLevel: 0,
				outputLevel: 0,
				waveform: [],
				playing: true,
				busy: false,
				cueOnly: true,
			});
			expect(control.narrateExecutorProgress).toHaveBeenCalledOnce();
			control.narrateExecutorProgress.mockClear();
			process.onEvent?.({ type: "input-state", pending: true });
			process.onEvent?.({
				type: "audio-state",
				inputLevel: 0,
				outputLevel: 0,
				waveform: [],
				playing: false,
				busy: false,
				cueOnly: true,
			});
			expect(control.narrateExecutorProgress).not.toHaveBeenCalled();
			process.onEvent?.({ type: "input-state", pending: false });
			process.onEvent?.({
				type: "audio-state",
				inputLevel: 0,
				outputLevel: 0,
				waveform: [],
				playing: true,
				busy: true,
				cueOnly: false,
			});
			expect(control.narrateExecutorProgress).not.toHaveBeenCalled();
		} finally {
			await call.stop();
		}
	});
});