import { describe, expect, test } from "bun:test"; import { choosePrompt } from "../static/prompt.js"; type Snap = ReturnType; const snapshot = (over: Record = {}, me: Record = {}, spike: Record = {}) => ({ phase: "live", clock: 40, result: null, me: { side: "atk", alive: true, blind: 0, planting: 0, spike: false, ...me }, spike: { planted: false, carried: false, fuse: 0, defuse: 0, x: 100, y: 100, ...spike }, ...over, }); describe("the one prompt on screen", () => { test("the buy phase says what to do, which side you are on, and how long there is", () => { const text = choosePrompt(snapshot({ phase: "buy", clock: 14.2 })); expect(text).toBe("Buy phase — 15s left. You attack: plant the spike on A or B. Click a gun you can afford."); expect(choosePrompt(snapshot({ phase: "buy", clock: 14.2 }, { side: "def" }))).toContain("You defend: hold A and B."); }); test("the terminal is told about keys, not clicks", () => { const text = choosePrompt(snapshot({ phase: "buy", clock: 14.2 }), { input: "keys" }); expect(text).toContain("Press b and buy a gun you can afford."); }); test("carrying the spike off a site sends you to one", () => { expect(choosePrompt(snapshot({}, { spike: true }), { onSite: false })).toBe( "You have the spike — get to site A or B", ); }); test("carrying it onto a site says how to plant", () => { expect(choosePrompt(snapshot({}, { spike: true }), { onSite: true })).toBe("Hold F to plant"); }); test("a defender near a planted spike is told to defuse, with the fuse", () => { const text = choosePrompt(snapshot({}, { side: "def" }, { planted: true, fuse: 31.4 }), { nearSpike: true }); expect(text).toBe("Spike down, 32s — hold F to defuse"); }); test("a defender across the map is told to get to it", () => { const text = choosePrompt(snapshot({}, { side: "def" }, { planted: true, fuse: 31.4 }), { nearSpike: false }); expect(text).toBe("Spike down, 32s — get to it and hold F"); }); test("an attacker with it planted is told to hold the site", () => { expect(choosePrompt(snapshot({}, {}, { planted: true, fuse: 10 }))).toBe( "Spike down, 10s — keep them off it", ); }); test("blind beats everything else on screen", () => { const text = choosePrompt(snapshot({}, { blind: 1.4, spike: true }), { onSite: true }); expect(text).toBe("You are blind — back off until it clears"); }); test("dead in a live round says you are still in the match", () => { expect(choosePrompt(snapshot({}, { alive: false }))).toBe("Waiting for the next round — you're in it"); }); test("the end of a round says whether you won, why, and the countdown", () => { // The server's `result` is only ever set when the MATCH is over; the round's // outcome travels as `last`. The old test fed a snapshot the server never // sends, and the real one read "Round over" and nothing else. const last = { round: 3, winner: "blue", why: "Spike defused" }; const lost = choosePrompt(snapshot({ phase: "end", clock: 4.1, last }, { team: "red" })); expect(lost).toBe("Round lost — spike defused · next round in 5s"); const won = choosePrompt(snapshot({ phase: "end", clock: 4.1, last }, { team: "blue" })); expect(won).toBe("Round won — spike defused · next round in 5s"); // Dead or alive, the outcome is the thing to say at that moment. expect(choosePrompt(snapshot({ phase: "end", clock: 4.1, last }, { team: "red", alive: false }))).toBe(lost); expect(choosePrompt(snapshot({ phase: "end", clock: 4.1, last: null }))).toBe("Round over · next round in 5s"); }); test("the end of the match names the winner and says a new one is coming", () => { const text = choosePrompt(snapshot({ phase: "over", clock: 11.2, result: "red win 13–7" })); expect(text).toBe("Match over — red win 13–7 · a new match starts in 12s"); }); test("a live defender is told what defending is", () => { expect(choosePrompt(snapshot({}, { side: "def" }))).toBe("Defending — hold A and B, 40s"); }); test("an attacker beside a loose spike is told to walk onto it", () => { expect(choosePrompt(snapshot(), { nearSpike: true })).toBe("Walk onto the spike to pick it up"); }); test("defusing counts down the defuse, not the fuse", () => { const text = choosePrompt(snapshot({}, { side: "def" }, { planted: true, fuse: 20, defuse: 3.5 }), { nearSpike: true, }); expect(text).toBe("Defusing — keep holding F, 4s"); }); test("no snapshot yet is no prompt", () => { expect(choosePrompt(null as unknown as Snap)).toBe(""); }); });