| 1 |
import { describe, expect, test } from "bun:test"; |
| 2 |
import { roster, rosterKey } from "../static/roster.js"; |
| 3 |
|
| 4 |
const snapshot = (players: unknown[]) => ({ |
| 5 |
me: { id: "p3" }, |
| 6 |
players, |
| 7 |
}); |
| 8 |
|
| 9 |
const mate = (id: string, over: Record<string, unknown> = {}) => ({ |
| 10 |
id, |
| 11 |
name: id.toUpperCase(), |
| 12 |
agent: "veil", |
| 13 |
hp: 100, |
| 14 |
alive: true, |
| 15 |
mate: true, |
| 16 |
spike: false, |
| 17 |
...over, |
| 18 |
}); |
| 19 |
|
| 20 |
describe("the team roster", () => { |
| 21 |
test("is your own side, everybody, seen or not", () => { |
| 22 |
const rows = roster( |
| 23 |
snapshot([mate("p1"), mate("p2"), mate("p3"), mate("p4"), mate("p5"), mate("e1", { mate: false })]), |
| 24 |
); |
| 25 |
expect(rows.length).toBe(5); |
| 26 |
expect(rows.some((r) => r.id === "e1")).toBe(false); |
| 27 |
}); |
| 28 |
|
| 29 |
test("keeps one order whatever order the snapshot arrives in", () => { |
| 30 |
const one = roster(snapshot([mate("p3"), mate("p1"), mate("p2")])); |
| 31 |
const two = roster(snapshot([mate("p2"), mate("p3"), mate("p1")])); |
| 32 |
expect(one.map((r) => r.id)).toEqual(two.map((r) => r.id)); |
| 33 |
expect(one.map((r) => r.id)).toEqual(["p1", "p2", "p3"]); |
| 34 |
}); |
| 35 |
|
| 36 |
test("marks the dead and gives them no health", () => { |
| 37 |
const rows = roster(snapshot([mate("p1", { alive: false, hp: 37 })])); |
| 38 |
expect(rows[0]!.alive).toBe(false); |
| 39 |
expect(rows[0]!.hp).toBe(0); |
| 40 |
}); |
| 41 |
|
| 42 |
test("clamps a health the server rounded past the ends", () => { |
| 43 |
expect(roster(snapshot([mate("p1", { hp: 140 })]))[0]!.hp).toBe(100); |
| 44 |
expect(roster(snapshot([mate("p1", { hp: -5 })]))[0]!.hp).toBe(0); |
| 45 |
}); |
| 46 |
|
| 47 |
test("knows which row is you, and who has the spike", () => { |
| 48 |
const rows = roster(snapshot([mate("p1"), mate("p3", { spike: true })])); |
| 49 |
expect(rows.find((r) => r.id === "p3")!.mine).toBe(true); |
| 50 |
expect(rows.find((r) => r.id === "p3")!.spike).toBe(true); |
| 51 |
expect(rows.find((r) => r.id === "p1")!.mine).toBe(false); |
| 52 |
}); |
| 53 |
|
| 54 |
test("no snapshot is no rows, not a crash", () => { |
| 55 |
expect(roster(null)).toEqual([]); |
| 56 |
}); |
| 57 |
|
| 58 |
test("the key changes when anything on the panel changes, and not otherwise", () => { |
| 59 |
const before = roster(snapshot([mate("p1", { hp: 80 })])); |
| 60 |
expect(rosterKey(before)).toBe(rosterKey(roster(snapshot([mate("p1", { hp: 80 })])))); |
| 61 |
expect(rosterKey(before)).not.toBe(rosterKey(roster(snapshot([mate("p1", { hp: 40 })])))); |
| 62 |
expect(rosterKey(before)).not.toBe(rosterKey(roster(snapshot([mate("p1", { hp: 80, alive: false })])))); |
| 63 |
}); |
| 64 |
}); |