import { Theme } from "@earendil-works/pi-coding-agent"; import { visibleWidth } from "@earendil-works/pi-tui"; import { describe, expect, it } from "vitest"; import { blend, type CompanionState, createCompanion, FOREARM, figure, framing, H, MAX_ZOOM, MIN_ZOOM, onHip, type Pose, type Rig, rig, solve, UPPER_ARM, W, } from "../src/companion.ts"; const theme = new Theme( { accent: "#d080a0", dim: "#666666", muted: "#888888", warning: "#c08040", thinkingXhigh: "#888888", text: "#eeeeee", } as ConstructorParameters[0], { selectedBg: "" } as ConstructorParameters[1], "truecolor", { appearance: "dark" }, ); const poses: Pose[] = ["posing", "peek", "pout", "lounge", "celebrate"]; const strip = (line: string) => line.replace(/\x1b\[[0-9;]*m/gu, ""); function state(overrides: Partial = {}): CompanionState { return { status: "active", pending: false, attempts: 3, turns: 6, budgets: { minutes: null, turns: 30, attempts: 10 }, failed: { gate: "tests", at: -10_000 }, updatedAt: 0, ...overrides, }; } /** Leftmost painted column of her body; hearts only float above it. */ function leftEdge(lines: string[]): number { return Math.min( ...lines .slice(6, H / 2) .map((line) => strip(line).search(/\S/u)) .filter((index) => index >= 0), ); } describe("companion figure", () => { it("paints a bounded figure from a small, deliberate palette", () => { for (const pose of poses) { const pixels = figure(rig(pose, 1.3), 1.3); expect(pixels).toHaveLength(H); expect(pixels.every((row) => row.length === W)).toBe(true); const painted = pixels.flat().filter(Boolean); expect(painted.length).toBeGreaterThan(120); // Hand-picked ramps: enough tones for form, never blended mush. const tones = new Set(painted.map((c) => c?.join())).size; expect(tones).toBeGreaterThan(8); expect(tones).toBeLessThanOrEqual(32); } }); it("animates over time and eases between key poses", () => { // Held poses still breathe. const a = JSON.stringify(figure(rig("posing", 0), 0, 3)); const b = JSON.stringify(figure(rig("posing", 1.4), 1.4, 3)); expect(a).not.toBe(b); const from = rig("posing", 0); const to = rig("celebrate", 0, 1); expect(blend(from, to, 0)).toMatchObject({ sway: from.sway, eyes: from.eyes, }); expect(blend(from, to, 1)).toMatchObject({ lift: to.lift, eyes: to.eyes }); const halfway = blend(from, to, 0.5).lift; expect(halfway).toBeCloseTo((from.lift + to.lift) / 2); }); it("renders more detail the closer the camera gets", () => { const pose = rig("peek", 1); const tones = (zoom: number) => new Set( figure(pose, 1, zoom) .flat() .filter(Boolean) .map((c) => c?.join()), ); const far = figure(pose, 1, MIN_ZOOM); const near = figure(pose, 1, MAX_ZOOM); expect(far).toHaveLength(H); expect(near).toHaveLength(H); expect(near[0]?.length).toBeGreaterThan(far[0]?.length ?? 0); const painted = (zoom: number) => figure(pose, 1, zoom).flat().filter(Boolean).length; expect(painted(MAX_ZOOM)).toBeGreaterThan(painted(MIN_ZOOM) * 4); // Green irises and white sclera only exist in close-ups. const iris = "62,132,84"; expect(tones(MAX_ZOOM).has(iris)).toBe(true); expect(tones(1).has(iris)).toBe(false); // The camera tilts up so her head stays in frame. expect(near.slice(0, 3).flat().some(Boolean)).toBe(true); }); it("holds a pose, then eases slowly into the next", () => { const held = [2, 6, 10].map((t) => rig("posing", t).sway); expect(Math.max(...held) - Math.min(...held)).toBeLessThan(0.7); expect( Math.abs(rig("posing", 10).sway - rig("posing", 20).sway), ).toBeGreaterThan(1); // The change takes seconds, not a frame. const step = Math.abs(rig("posing", 16.5).sway - rig("posing", 16.4).sway); expect(step).toBeLessThan(0.2); }); it("gives her bust gravity, arm lift and collision", () => { const bikini = "255,186,142"; const cups = (r: Rig) => { const px = figure(r, 0, 3, { width: 80, center: 40, facing: 1, focus: 12, }); const hits: Array<[number, number]> = []; px.forEach((row, y) => row.forEach((c, x) => { if (c?.join() === bikini) hits.push([x, y]); }), ); const mean = (i: 0 | 1) => hits.reduce((sum, h) => sum + h[i], 0) / (hits.length || 1); return { x: mean(0), y: mean(1), top: Math.min(...hits.map((h) => h[1])), }; }; const base = rig("pout", 0); // Arms raised over her head lift the bust. const raised = cups({ ...base, hands: [ [10.4, 2.8], [13.6, 2.8], ], }); expect(raised.top).toBeLessThan(cups(base).top); // A forearm pressed across her chest pushes the soft tissue aside. const pressed = cups({ ...base, hands: [base.hands[0], [10.5, 12.5]] }); expect( Math.abs(pressed.x - cups(base).x) + Math.abs(pressed.y - cups(base).y), ).toBeGreaterThan(0.5); }); it("keeps bone lengths fixed whatever the target", () => { for (const target of [ [7, 17.6], [13.8, 2.6], [30, 30], [8.3, 9.6], ] as const) { const root = [8.4, 9.4] as const; const { joint, end } = solve(root, target, UPPER_ARM, FOREARM, -1); expect(Math.hypot(joint[0] - root[0], joint[1] - root[1])).toBeCloseTo( UPPER_ARM, 3, ); expect(Math.hypot(end[0] - joint[0], end[1] - joint[1])).toBeCloseTo( FOREARM, 3, ); } }); it("frames close on her face, torso and hips, never the whole figure", () => { let close = 0; for (let t = 0; t < 96; t++) { const shot = framing("posing", t, t); expect(shot.zoom).toBeGreaterThanOrEqual(1.7); expect(shot.zoom).toBeLessThanOrEqual(MAX_ZOOM); expect(shot.focus).toBeGreaterThanOrEqual(5); expect(shot.focus).toBeLessThanOrEqual(17); if (shot.zoom >= 2.8) close++; } expect(close / 96).toBeGreaterThan(0.85); expect(framing("posing", 15, 15).focus).toBe(5); for (const pose of ["peek", "pout", "lounge"] as const) expect(framing(pose, 5, 5).zoom).toBeGreaterThanOrEqual(2.4); expect(framing("celebrate", 0, 1).zoom).toBeLessThan( framing("celebrate", 0, 8).zoom, ); }); it("dips in anticipation, stretches overhead, then blows kisses away from her face", () => { expect(rig("celebrate", 0, 0.3).lift).toBeGreaterThan(0.2); expect(rig("celebrate", 0, 0.3).squash).toBeLessThan(1); for (const since of [1, 2, 3, 5]) expect(rig("celebrate", since, since).lift).toBeGreaterThanOrEqual(-0.15); // Both hands end up above her head during the stretch. const stretch = rig("celebrate", 4, 4); expect(stretch.hands[0][1]).toBeLessThan(3); expect(stretch.hands[1][1]).toBeLessThan(3); // The kiss arrives after the stretch, and the hand never covers her face. expect([7.2, 7.5, 8].some((s) => rig("celebrate", s, s).kiss)).toBe(true); for (let since = 6; since < 10; since += 0.1) { const [x, y] = rig("celebrate", since, since).hands[1]; expect(Math.hypot(x - 12, y - 5)).toBeGreaterThan(2.5); } }); it("rests her hands on her hips in power poses", () => { for (const pose of ["pout", "peek", "lounge"] as const) { const { hands, sway } = rig(pose, 1); expect(hands[0]).toEqual(onHip(sway, -1)); } }); }); describe("companion widget", () => { it("stays put and lets the camera move calmly", () => { let now = 0; const widget = createCompanion( { requestRender() {} } as never, theme, () => state(), () => now, ); try { const width = 100; let previous = leftEdge(widget.render(width)); for (let i = 0; i < 200; i++) { now += 80; const lines = widget.render(width); for (const line of lines) expect(visibleWidth(line)).toBeLessThanOrEqual(width); const edge = leftEdge(lines); // No jumps: the frame changes by at most a cell or two at a time. expect(Math.abs(edge - previous)).toBeLessThanOrEqual(2); previous = edge; } } finally { widget.dispose(); } }); it("shows subtle run facts and reacts to loop states", () => { let now = 0; let current = state({ failed: { gate: "tests", at: 0 } }); const widget = createCompanion( { requestRender() {} } as never, theme, () => current, () => now, ); try { const info = (lines: string[]) => strip(lines.at(-1) ?? ""); const pout = widget.render(100); expect(pout).toHaveLength(H / 2 + 1); expect(info(pout)).toMatch(/3\/10.*6\/30.*tests/u); expect(info(pout)).toMatch(/tests|again/u); current = state({ status: "paused" }); now = 10_000; expect(info(widget.render(100))).toMatch(/wait|ready/u); current = state({ status: "succeeded", updatedAt: now }); expect(info(widget.render(100))).toMatch(/done|winner/u); expect(info(widget.render(100))).not.toMatch(/tests$/u); now += 41_000; expect(widget.render(100)).toEqual([]); current = state({ status: "stopped" }); expect(widget.render(100)).toEqual([]); } finally { widget.dispose(); } }); it("moves the camera smoothly between framings", () => { let now = 0; let current = state(); const widget = createCompanion( { requestRender() {} } as never, theme, () => current, () => now, ); const extent = (lines: string[]) => { const rows = lines.slice(0, H / 2).map(strip); const left = Math.min( ...rows.map((r) => r.search(/\S/u)).filter((i) => i >= 0), ); const right = Math.max(...rows.map((r) => r.trimEnd().length)); const top = rows.findIndex((r) => r.trim()); return { width: right - left, top }; }; try { widget.render(120); // Gates start running: the camera dollies in for a close-up. current = state({ pending: true }); let previous = extent(widget.render(120)); let grew = 0; for (let i = 0; i < 120; i++) { now += 80; const next = extent(widget.render(120)); // No snaps: each frame changes size by at most a couple of cells. expect(Math.abs(next.width - previous.width)).toBeLessThanOrEqual(3); grew += next.width - previous.width; previous = next; } expect(grew).toBeGreaterThan(6); } finally { widget.dispose(); } }); it("draws soft-tissue offsets for bust and hips and keeps them stable", () => { // The widget's springs feed these offsets; close-ups must show them. const r = rig("posing", 0); const at = (focus: number, rr = r) => JSON.stringify( figure(rr, 0, 3, { width: 80, center: 40, facing: 1, focus }), ); expect(at(12, { ...r, bust: [0, 0.5] })).not.toBe(at(12)); expect(at(19, { ...r, jiggle: 0.5 })).not.toBe(at(19)); // Long, uneven frame gaps must not destabilise the springs. let now = 0; const widget = createCompanion( { requestRender() {} } as never, theme, () => state(), () => now, ); try { for (const gap of [80, 500, 40, 900, 80, 80, 1_500, 80]) { now += gap; const lines = widget.render(100); expect(lines).toHaveLength(H / 2 + 1); expect(lines.join("")).not.toMatch(/NaN/u); } } finally { widget.dispose(); } }); it("renders a frame quickly enough for 8 fps", () => { let now = 0; const widget = createCompanion( { requestRender() {} } as never, theme, () => state(), () => now, ); try { const start = performance.now(); for (let i = 0; i < 40; i++) { now += 125; widget.render(160); } expect((performance.now() - start) / 40).toBeLessThan(25); } finally { widget.dispose(); } }); });