import { describe, expect, it } from "vitest"; import { endpointSilenceAnchor, turnPolicy } from "../src/turn-policy.ts"; describe("turn policy", () => { it.each([ ["complete", 300], ["trailing", 2_000], ["mid-clause", 2_000], ] as const)( "maps host-chat %s label without confidence", (choice, waitMs) => { const policy = turnPolicy({ available: true, source: "host-chat", choice, pComplete: 1, scores: { complete: 1 }, }); expect(policy).toMatchObject({ waitMs, probabilityMilli: -1, outcome: "labelled", }); expect(policy.description).toContain("non-native, uncalibrated"); }, ); it.each([ [0, 2_000], [0.5, 1_150], [1, 300], ] as const)( "preserves native probability mapping p=%s", (pComplete, waitMs) => { expect(turnPolicy({ available: true, pComplete })).toMatchObject({ waitMs, probabilityMilli: pComplete * 1_000, outcome: "scored", }); }, ); it("accepts legacy native responses without source", () => { expect(turnPolicy({ available: true, pComplete: 0.25 })).toMatchObject({ waitMs: 1_575, outcome: "scored", }); }); it("credits valid native endpoint age without crossing browser clocks", () => { expect(endpointSilenceAnchor(1_500, 450, 1_200)).toBe(1_050); expect(endpointSilenceAnchor(300, 450, 0)).toBe(-150); }); it.each([undefined, -1, 1_001, Number.NaN, Number.POSITIVE_INFINITY])( "falls back to receipt time for invalid endpoint age %#", (age) => { expect(endpointSilenceAnchor(1_500, age, 1_000)).toBe(1_500); }, ); it("keeps zero as an absent-anchor sentinel", () => { expect(endpointSilenceAnchor(0, undefined, 0)).not.toBe(0); expect(endpointSilenceAnchor(450, 450, 0)).not.toBe(0); }); it.each([ undefined, null, {}, { available: false, pComplete: 1 }, { available: true, pComplete: Number.NaN }, { available: true, pComplete: -0.1 }, { available: true, pComplete: 1.1 }, { available: true, source: "host-chat", choice: "unknown" }, ])("falls back for invalid results %#", (result) => { expect(turnPolicy(result)).toMatchObject({ waitMs: 1_500, probabilityMilli: -1, outcome: "fallback", }); }); });