Implement the 026-roll-initiative feature that adds d20 roll buttons for bestiary combatants' initiative using a click-to-edit pattern (d20 icon when empty, plain text when set), plus a Roll All button in the top bar that batch-rolls for all unrolled bestiary combatants, with randomness confined to the adapter layer and the domain receiving pre-resolved dice values

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Lukas
2026-03-10 16:29:09 +01:00
parent 5b0bac880d
commit d5f7b6ee36
20 changed files with 926 additions and 27 deletions

View File

@@ -0,0 +1,70 @@
import { describe, expect, it } from "vitest";
import { rollInitiative } from "../roll-initiative.js";
import { isDomainError } from "../types.js";
describe("rollInitiative", () => {
describe("valid rolls", () => {
it("normal roll: 15 + modifier 7 = 22", () => {
expect(rollInitiative(15, 7)).toBe(22);
});
it("boundary: roll 1 + modifier 0 = 1", () => {
expect(rollInitiative(1, 0)).toBe(1);
});
it("boundary: roll 20 + modifier 0 = 20", () => {
expect(rollInitiative(20, 0)).toBe(20);
});
it("negative modifier: roll 1 + (3) = 2", () => {
expect(rollInitiative(1, -3)).toBe(-2);
});
it("zero modifier: roll 10 + 0 = 10", () => {
expect(rollInitiative(10, 0)).toBe(10);
});
it("large positive modifier: roll 20 + 12 = 32", () => {
expect(rollInitiative(20, 12)).toBe(32);
});
});
describe("invalid dice rolls", () => {
it("rejects 0", () => {
const result = rollInitiative(0, 5);
expect(isDomainError(result)).toBe(true);
if (isDomainError(result)) {
expect(result.code).toBe("invalid-dice-roll");
}
});
it("rejects 21", () => {
const result = rollInitiative(21, 5);
expect(isDomainError(result)).toBe(true);
if (isDomainError(result)) {
expect(result.code).toBe("invalid-dice-roll");
}
});
it("rejects non-integer (3.5)", () => {
const result = rollInitiative(3.5, 0);
expect(isDomainError(result)).toBe(true);
});
it("rejects negative dice roll", () => {
const result = rollInitiative(-1, 0);
expect(isDomainError(result)).toBe(true);
});
it("rejects NaN", () => {
const result = rollInitiative(Number.NaN, 0);
expect(isDomainError(result)).toBe(true);
});
});
describe("determinism", () => {
it("same input produces same output", () => {
expect(rollInitiative(10, 5)).toBe(rollInitiative(10, 5));
});
});
});