Files
2D6-Dungeon/src/rules/tables.test.ts
Keith Solomon 6bf48df74c Feature: add character creation logic and tests
- Implement character creation functions to handle adventurer setup.
- Add validation for adventurer name and selection of starting items.
- Introduce new items in sample content pack: Flint and Steel, Pouch, Wax Sealing Kit, and Backpack.
- Create tests for character creation functionality to ensure valid options and error handling.

Feature: implement dice rolling mechanics and tests

- Add functions for rolling various dice types (d3, d6, 2d6, d66) with validation.
- Implement modifier application with clamping for roll results.
- Create tests to verify correct behavior of dice rolling functions.

Feature: add table lookup functionality and tests

- Implement table lookup and resolution logic for defined tables.
- Support for modified ranges and fallback to nearest entries.
- Create tests to ensure correct table entry resolution based on roll results.
2026-03-15 11:59:50 -05:00

63 lines
1.5 KiB
TypeScript

import { describe, expect, it } from "vitest";
import type { TableDefinition } from "@/types/content";
import { lookupTable, resolveTableEntry } from "./tables";
function createSequenceRoller(values: number[]) {
let index = 0;
return () => {
const next = values[index];
index += 1;
return next;
};
}
const d6Table: TableDefinition = {
id: "table.sample",
code: "SAMPLE1",
name: "Sample Table",
category: "generic",
page: 1,
diceKind: "d6",
usesModifiedRangesRule: true,
entries: [
{ key: "1", exact: 1, label: "One" },
{ key: "2-5", min: 2, max: 5, label: "Two to Five" },
{ key: "6", exact: 6, label: "Six" },
],
mvp: true,
};
describe("table lookup rules", () => {
it("finds a matching exact entry", () => {
const result = lookupTable(d6Table, { roller: createSequenceRoller([6]) });
expect(result.entry.label).toBe("Six");
expect(result.roll.total).toBe(6);
});
it("clamps modified results when the table uses modified ranges", () => {
const result = lookupTable(d6Table, {
roller: createSequenceRoller([6]),
modifier: 3,
});
expect(result.entry.label).toBe("Six");
expect(result.roll.modifiedTotal).toBe(6);
expect(result.roll.clamped).toBe(true);
});
it("falls back to the nearest numeric entry for direct resolution", () => {
const result = resolveTableEntry(d6Table, {
diceKind: "d6",
rolls: [0],
total: 4,
modifiedTotal: 7,
});
expect(result.entry.label).toBe("Six");
});
});