Files
initiative/packages/domain/src/__tests__/roll-initiative.test.ts
Lukas 6584d8d064
All checks were successful
CI / check (push) Successful in 1m17s
CI / build-image (push) Successful in 19s
Add advantage/disadvantage rolling for initiative
Right-click or long-press the d20 button (per-combatant or Roll All)
to open a context menu with Advantage and Disadvantage options.
Normal left-click behavior is unchanged.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 09:16:04 +01:00

94 lines
2.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from "vitest";
import { rollInitiative, selectRoll } from "../roll-initiative.js";
import { isDomainError } from "../types.js";
import { expectDomainError } from "./test-helpers.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);
expectDomainError(result, "invalid-dice-roll");
});
it("rejects 21", () => {
const result = rollInitiative(21, 5);
expectDomainError(result, "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));
});
});
});
describe("selectRoll", () => {
it("normal mode returns the first roll", () => {
expect(selectRoll(8, 15, "normal")).toBe(8);
});
it("advantage returns the higher roll", () => {
expect(selectRoll(8, 15, "advantage")).toBe(15);
});
it("advantage returns the higher roll (reversed)", () => {
expect(selectRoll(15, 8, "advantage")).toBe(15);
});
it("disadvantage returns the lower roll", () => {
expect(selectRoll(8, 15, "disadvantage")).toBe(8);
});
it("disadvantage returns the lower roll (reversed)", () => {
expect(selectRoll(15, 8, "disadvantage")).toBe(8);
});
it("equal rolls return the same value for all modes", () => {
expect(selectRoll(12, 12, "normal")).toBe(12);
expect(selectRoll(12, 12, "advantage")).toBe(12);
expect(selectRoll(12, 12, "disadvantage")).toBe(12);
});
});