election-game/web/test/creator-points.test.ts

39 lines
1.8 KiB
TypeScript
Raw Normal View History

import { DEFAULT_POINTS, START_POINTS, START_POINT_MAX } from "@election2026/protocol";
import { describe, expect, it } from "vitest";
import { withPoints, type SavedCharacter } from "../src/game/creator/characters.ts";
import { ABILITY_IDS, bumpPoint, randomPoints } from "../src/game/creator/options.ts";
/** docs/27 第 6 節:捏人物的開局能力就是政治生涯那五項 */
const sum = (p: Record<string, number>) => Object.values(p).reduce((a, b) => a + b, 0);
describe("捏人物的開局能力", () => {
it("每項最多 2 點、總共 5 點;超過時只能減", () => {
const two = bumpPoint(bumpPoint({ ...DEFAULT_POINTS, eloquence: 0 }, "media", 1), "media", 1);
expect(two.media).toBe(START_POINT_MAX);
expect(bumpPoint(two, "media", 1)).toBe(two);
expect(bumpPoint(DEFAULT_POINTS, "finance", 1)).toBe(DEFAULT_POINTS);
expect(bumpPoint(DEFAULT_POINTS, "finance", -1).finance).toBe(0);
});
it("隨機配點一定分完、每項不超過上限", () => {
let seed = 1;
const rand = () => ((seed = (seed * 16807) % 2147483647) / 2147483647);
for (let i = 0; i < 50; i++) {
const p = randomPoints(rand);
expect(sum(p)).toBe(START_POINTS);
for (const k of ABILITY_IDS) expect(p[k]).toBeLessThanOrEqual(START_POINT_MAX);
}
});
it("舊存檔(知名度、謀略那五項)讀進來換成開局能力點", () => {
const old = { id: "x", name: "林小美", party: null, background: "律師", stats: { fame: 10, eloquence: 4, strategy: 3, wealth: 4, network: 4 }, avatar: {} } as unknown as SavedCharacter;
const next = withPoints(old);
expect("stats" in next).toBe(false);
expect(next.points.media).toBe(2);
expect(sum(next.points)).toBe(START_POINTS);
const fresh = { ...next };
expect(withPoints(fresh)).toBe(fresh);
});
});