Implement the 021-bestiary-statblock feature that adds a searchable D&D 2024 Monster Manual creature library with inline autocomplete suggestions, full stat block display in a fixed side panel, auto-numbering of duplicate creature names, HP/AC pre-fill from bestiary data, and automatic stat block display on turn change for wide viewports
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
326
apps/web/src/adapters/__tests__/bestiary-adapter.test.ts
Normal file
326
apps/web/src/adapters/__tests__/bestiary-adapter.test.ts
Normal file
@@ -0,0 +1,326 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { normalizeBestiary } from "../bestiary-adapter.js";
|
||||
|
||||
describe("normalizeBestiary", () => {
|
||||
it("normalizes a simple creature", () => {
|
||||
const raw = {
|
||||
monster: [
|
||||
{
|
||||
name: "Goblin Warrior",
|
||||
source: "XMM",
|
||||
size: ["S"],
|
||||
type: { type: "fey", tags: ["goblinoid"] },
|
||||
alignment: ["C", "N"],
|
||||
ac: [15],
|
||||
hp: { average: 10, formula: "3d6" },
|
||||
speed: { walk: 30 },
|
||||
str: 8,
|
||||
dex: 15,
|
||||
con: 10,
|
||||
int: 10,
|
||||
wis: 8,
|
||||
cha: 8,
|
||||
skill: { stealth: "+6" },
|
||||
senses: ["Darkvision 60 ft."],
|
||||
passive: 9,
|
||||
languages: ["Common", "Goblin"],
|
||||
cr: "1/4",
|
||||
action: [
|
||||
{
|
||||
name: "Scimitar",
|
||||
entries: [
|
||||
"{@atkr m} {@hit 4}, reach 5 ft. {@h}5 ({@damage 1d6 + 2}) Slashing damage.",
|
||||
],
|
||||
},
|
||||
],
|
||||
bonus: [
|
||||
{
|
||||
name: "Nimble Escape",
|
||||
entries: [
|
||||
"The goblin takes the {@action Disengage|XPHB} or {@action Hide|XPHB} action.",
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const creatures = normalizeBestiary(raw);
|
||||
expect(creatures).toHaveLength(1);
|
||||
|
||||
const c = creatures[0];
|
||||
expect(c.id).toBe("xmm:goblin-warrior");
|
||||
expect(c.name).toBe("Goblin Warrior");
|
||||
expect(c.source).toBe("XMM");
|
||||
expect(c.sourceDisplayName).toBe("MM 2024");
|
||||
expect(c.size).toBe("Small");
|
||||
expect(c.type).toBe("Fey (Goblinoid)");
|
||||
expect(c.alignment).toBe("Chaotic Neutral");
|
||||
expect(c.ac).toBe(15);
|
||||
expect(c.hp).toEqual({ average: 10, formula: "3d6" });
|
||||
expect(c.speed).toBe("30 ft.");
|
||||
expect(c.abilities.dex).toBe(15);
|
||||
expect(c.cr).toBe("1/4");
|
||||
expect(c.proficiencyBonus).toBe(2);
|
||||
expect(c.passive).toBe(9);
|
||||
expect(c.skills).toBe("Stealth +6");
|
||||
expect(c.senses).toBe("Darkvision 60 ft.");
|
||||
expect(c.languages).toBe("Common, Goblin");
|
||||
expect(c.actions).toHaveLength(1);
|
||||
expect(c.actions?.[0].text).toContain("Melee Attack Roll:");
|
||||
expect(c.actions?.[0].text).not.toContain("{@");
|
||||
expect(c.bonusActions).toHaveLength(1);
|
||||
expect(c.bonusActions?.[0].text).toContain("Disengage");
|
||||
expect(c.bonusActions?.[0].text).not.toContain("{@");
|
||||
});
|
||||
|
||||
it("normalizes a creature with legendary actions", () => {
|
||||
const raw = {
|
||||
monster: [
|
||||
{
|
||||
name: "Aboleth",
|
||||
source: "XMM",
|
||||
size: ["L"],
|
||||
type: "aberration",
|
||||
alignment: ["L", "E"],
|
||||
ac: [17],
|
||||
hp: { average: 135, formula: "18d10 + 36" },
|
||||
speed: { walk: 10, swim: 40 },
|
||||
str: 21,
|
||||
dex: 9,
|
||||
con: 15,
|
||||
int: 18,
|
||||
wis: 15,
|
||||
cha: 18,
|
||||
save: { con: "+6", int: "+8", wis: "+6" },
|
||||
senses: ["Darkvision 120 ft."],
|
||||
passive: 12,
|
||||
languages: ["Deep Speech", "Telepathy 120 ft."],
|
||||
cr: "10",
|
||||
legendary: [
|
||||
{
|
||||
name: "Lash",
|
||||
entries: ["The aboleth makes one Tentacle attack."],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const creatures = normalizeBestiary(raw);
|
||||
const c = creatures[0];
|
||||
expect(c.legendaryActions).toBeDefined();
|
||||
expect(c.legendaryActions?.preamble).toContain("3 Legendary Actions");
|
||||
expect(c.legendaryActions?.entries).toHaveLength(1);
|
||||
expect(c.legendaryActions?.entries[0].name).toBe("Lash");
|
||||
});
|
||||
|
||||
it("normalizes a creature with spellcasting", () => {
|
||||
const raw = {
|
||||
monster: [
|
||||
{
|
||||
name: "Test Caster",
|
||||
source: "XMM",
|
||||
size: ["M"],
|
||||
type: "humanoid",
|
||||
ac: [12],
|
||||
hp: { average: 40, formula: "9d8" },
|
||||
speed: { walk: 30 },
|
||||
str: 10,
|
||||
dex: 14,
|
||||
con: 10,
|
||||
int: 17,
|
||||
wis: 12,
|
||||
cha: 11,
|
||||
passive: 11,
|
||||
cr: "6",
|
||||
spellcasting: [
|
||||
{
|
||||
name: "Spellcasting",
|
||||
headerEntries: [
|
||||
"The caster casts spells using Intelligence (spell save {@dc 15}):",
|
||||
],
|
||||
will: ["{@spell Detect Magic|XPHB}", "{@spell Mage Hand|XPHB}"],
|
||||
daily: {
|
||||
"2e": ["{@spell Fireball|XPHB}"],
|
||||
"1": ["{@spell Dimension Door|XPHB}"],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const creatures = normalizeBestiary(raw);
|
||||
const c = creatures[0];
|
||||
expect(c.spellcasting).toHaveLength(1);
|
||||
const sc = c.spellcasting?.[0];
|
||||
expect(sc).toBeDefined();
|
||||
expect(sc?.name).toBe("Spellcasting");
|
||||
expect(sc?.headerText).toContain("DC 15");
|
||||
expect(sc?.headerText).not.toContain("{@");
|
||||
expect(sc?.atWill).toEqual(["Detect Magic", "Mage Hand"]);
|
||||
expect(sc?.daily).toHaveLength(2);
|
||||
expect(sc?.daily).toContainEqual({
|
||||
uses: 2,
|
||||
each: true,
|
||||
spells: ["Fireball"],
|
||||
});
|
||||
expect(sc?.daily).toContainEqual({
|
||||
uses: 1,
|
||||
each: false,
|
||||
spells: ["Dimension Door"],
|
||||
});
|
||||
});
|
||||
|
||||
it("normalizes a creature with object-type type field", () => {
|
||||
const raw = {
|
||||
monster: [
|
||||
{
|
||||
name: "Swarm of Bats",
|
||||
source: "XMM",
|
||||
size: ["L"],
|
||||
type: { type: "beast", swarmSize: "T" },
|
||||
ac: [12],
|
||||
hp: { average: 11, formula: "2d10" },
|
||||
speed: { walk: 5, fly: 30 },
|
||||
str: 5,
|
||||
dex: 15,
|
||||
con: 10,
|
||||
int: 2,
|
||||
wis: 12,
|
||||
cha: 4,
|
||||
passive: 11,
|
||||
resist: ["bludgeoning", "piercing", "slashing"],
|
||||
conditionImmune: ["charmed", "frightened"],
|
||||
cr: "1/4",
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const creatures = normalizeBestiary(raw);
|
||||
const c = creatures[0];
|
||||
expect(c.type).toBe("Swarm of Tiny Beasts");
|
||||
expect(c.resist).toBe("Bludgeoning, Piercing, Slashing");
|
||||
expect(c.conditionImmune).toBe("Charmed, Frightened");
|
||||
});
|
||||
|
||||
it("normalizes a creature with conditional resistances", () => {
|
||||
const raw = {
|
||||
monster: [
|
||||
{
|
||||
name: "Half-Dragon",
|
||||
source: "XMM",
|
||||
size: ["M"],
|
||||
type: "humanoid",
|
||||
ac: [18],
|
||||
hp: { average: 65, formula: "10d8 + 20" },
|
||||
speed: { walk: 30 },
|
||||
str: 16,
|
||||
dex: 13,
|
||||
con: 14,
|
||||
int: 10,
|
||||
wis: 11,
|
||||
cha: 10,
|
||||
passive: 10,
|
||||
cr: "5",
|
||||
resist: [
|
||||
{
|
||||
special: "Damage type chosen for the Draconic Origin trait",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const creatures = normalizeBestiary(raw);
|
||||
const c = creatures[0];
|
||||
expect(c.resist).toBe("Damage type chosen for the Draconic Origin trait");
|
||||
});
|
||||
|
||||
it("normalizes a creature with multiple sizes", () => {
|
||||
const raw = {
|
||||
monster: [
|
||||
{
|
||||
name: "Aberrant Cultist",
|
||||
source: "XMM",
|
||||
size: ["S", "M"],
|
||||
type: "humanoid",
|
||||
ac: [13],
|
||||
hp: { average: 22, formula: "4d8 + 4" },
|
||||
speed: { walk: 30 },
|
||||
str: 11,
|
||||
dex: 14,
|
||||
con: 12,
|
||||
int: 10,
|
||||
wis: 13,
|
||||
cha: 8,
|
||||
passive: 11,
|
||||
cr: "1/2",
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const creatures = normalizeBestiary(raw);
|
||||
expect(creatures[0].size).toBe("Small or Medium");
|
||||
});
|
||||
|
||||
it("normalizes a creature with CR as object", () => {
|
||||
const raw = {
|
||||
monster: [
|
||||
{
|
||||
name: "Dragon",
|
||||
source: "XMM",
|
||||
size: ["H"],
|
||||
type: "dragon",
|
||||
ac: [19],
|
||||
hp: { average: 256, formula: "19d12 + 133" },
|
||||
speed: { walk: 40 },
|
||||
str: 27,
|
||||
dex: 10,
|
||||
con: 25,
|
||||
int: 16,
|
||||
wis: 13,
|
||||
cha: 23,
|
||||
passive: 23,
|
||||
cr: { cr: "17", xpLair: 20000 },
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const creatures = normalizeBestiary(raw);
|
||||
expect(creatures[0].cr).toBe("17");
|
||||
expect(creatures[0].proficiencyBonus).toBe(6);
|
||||
});
|
||||
|
||||
it("handles fly speed with hover condition", () => {
|
||||
const raw = {
|
||||
monster: [
|
||||
{
|
||||
name: "Air Elemental",
|
||||
source: "XMM",
|
||||
size: ["L"],
|
||||
type: "elemental",
|
||||
ac: [15],
|
||||
hp: { average: 90, formula: "12d10 + 24" },
|
||||
speed: {
|
||||
walk: 10,
|
||||
fly: { number: 90, condition: "(hover)" },
|
||||
canHover: true,
|
||||
},
|
||||
str: 14,
|
||||
dex: 20,
|
||||
con: 14,
|
||||
int: 6,
|
||||
wis: 10,
|
||||
cha: 6,
|
||||
passive: 10,
|
||||
cr: "5",
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const creatures = normalizeBestiary(raw);
|
||||
expect(creatures[0].speed).toBe("10 ft., fly 90 ft. (hover)");
|
||||
});
|
||||
});
|
||||
16
apps/web/src/adapters/__tests__/bestiary-full.test.ts
Normal file
16
apps/web/src/adapters/__tests__/bestiary-full.test.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { expect, it } from "vitest";
|
||||
import rawData from "../../../../../data/bestiary/xmm.json";
|
||||
import { normalizeBestiary } from "../bestiary-adapter.js";
|
||||
|
||||
it("normalizes all 503 monsters without error", () => {
|
||||
const creatures = normalizeBestiary(
|
||||
rawData as unknown as Parameters<typeof normalizeBestiary>[0],
|
||||
);
|
||||
expect(creatures.length).toBe(503);
|
||||
for (const c of creatures) {
|
||||
expect(c.name).toBeTruthy();
|
||||
expect(c.id).toBeTruthy();
|
||||
expect(c.ac).toBeGreaterThanOrEqual(0);
|
||||
expect(c.hp.average).toBeGreaterThan(0);
|
||||
}
|
||||
});
|
||||
138
apps/web/src/adapters/__tests__/strip-tags.test.ts
Normal file
138
apps/web/src/adapters/__tests__/strip-tags.test.ts
Normal file
@@ -0,0 +1,138 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { stripTags } from "../strip-tags.js";
|
||||
|
||||
describe("stripTags", () => {
|
||||
it("returns text unchanged when no tags present", () => {
|
||||
expect(stripTags("Hello world")).toBe("Hello world");
|
||||
});
|
||||
|
||||
it("strips {@spell Name|Source} to Name", () => {
|
||||
expect(stripTags("{@spell Fireball|XPHB}")).toBe("Fireball");
|
||||
});
|
||||
|
||||
it("strips {@condition Name|Source} to Name", () => {
|
||||
expect(stripTags("{@condition Frightened|XPHB}")).toBe("Frightened");
|
||||
});
|
||||
|
||||
it("strips {@damage dice} to dice", () => {
|
||||
expect(stripTags("{@damage 2d10}")).toBe("2d10");
|
||||
});
|
||||
|
||||
it("strips {@dice value} to value", () => {
|
||||
expect(stripTags("{@dice 5d10}")).toBe("5d10");
|
||||
});
|
||||
|
||||
it("strips {@dc N} to DC N", () => {
|
||||
expect(stripTags("{@dc 15}")).toBe("DC 15");
|
||||
});
|
||||
|
||||
it("strips {@hit N} to +N", () => {
|
||||
expect(stripTags("{@hit 5}")).toBe("+5");
|
||||
});
|
||||
|
||||
it("strips {@h} to Hit: ", () => {
|
||||
expect(stripTags("{@h}")).toBe("Hit: ");
|
||||
});
|
||||
|
||||
it("strips {@hom} to Hit or Miss: ", () => {
|
||||
expect(stripTags("{@hom}")).toBe("Hit or Miss: ");
|
||||
});
|
||||
|
||||
it("strips {@atkr m} to Melee Attack Roll:", () => {
|
||||
expect(stripTags("{@atkr m}")).toBe("Melee Attack Roll:");
|
||||
});
|
||||
|
||||
it("strips {@atkr r} to Ranged Attack Roll:", () => {
|
||||
expect(stripTags("{@atkr r}")).toBe("Ranged Attack Roll:");
|
||||
});
|
||||
|
||||
it("strips {@atkr m,r} to Melee or Ranged Attack Roll:", () => {
|
||||
expect(stripTags("{@atkr m,r}")).toBe("Melee or Ranged Attack Roll:");
|
||||
});
|
||||
|
||||
it("strips {@recharge 5} to (Recharge 5-6)", () => {
|
||||
expect(stripTags("{@recharge 5}")).toBe("(Recharge 5-6)");
|
||||
});
|
||||
|
||||
it("strips {@recharge} to (Recharge 6)", () => {
|
||||
expect(stripTags("{@recharge}")).toBe("(Recharge 6)");
|
||||
});
|
||||
|
||||
it("strips {@actSave wis} to Wisdom saving throw", () => {
|
||||
expect(stripTags("{@actSave wis}")).toBe("Wisdom saving throw");
|
||||
});
|
||||
|
||||
it("strips {@actSaveFail} to Failure:", () => {
|
||||
expect(stripTags("{@actSaveFail}")).toBe("Failure:");
|
||||
});
|
||||
|
||||
it("strips {@actSaveFail 2} to Failure by 2 or More:", () => {
|
||||
expect(stripTags("{@actSaveFail 2}")).toBe("Failure by 2 or More:");
|
||||
});
|
||||
|
||||
it("strips {@actSaveSuccess} to Success:", () => {
|
||||
expect(stripTags("{@actSaveSuccess}")).toBe("Success:");
|
||||
});
|
||||
|
||||
it("strips {@actTrigger} to Trigger:", () => {
|
||||
expect(stripTags("{@actTrigger}")).toBe("Trigger:");
|
||||
});
|
||||
|
||||
it("strips {@actResponse} to Response:", () => {
|
||||
expect(stripTags("{@actResponse}")).toBe("Response:");
|
||||
});
|
||||
|
||||
it("strips {@variantrule Name|Source|Display} to Display", () => {
|
||||
expect(stripTags("{@variantrule Cone [Area of Effect]|XPHB|Cone}")).toBe(
|
||||
"Cone",
|
||||
);
|
||||
});
|
||||
|
||||
it("strips {@action Name|Source|Display} to Display", () => {
|
||||
expect(stripTags("{@action Disengage|XPHB|Disengage}")).toBe("Disengage");
|
||||
});
|
||||
|
||||
it("strips {@skill Name|Source} to Name", () => {
|
||||
expect(stripTags("{@skill Perception|XPHB}")).toBe("Perception");
|
||||
});
|
||||
|
||||
it("strips {@creature Name|Source} to Name", () => {
|
||||
expect(stripTags("{@creature Goblin|XPHB}")).toBe("Goblin");
|
||||
});
|
||||
|
||||
it("strips {@hazard Name|Source} to Name", () => {
|
||||
expect(stripTags("{@hazard Lava|XPHB}")).toBe("Lava");
|
||||
});
|
||||
|
||||
it("strips {@status Name|Source} to Name", () => {
|
||||
expect(stripTags("{@status Bloodied|XPHB}")).toBe("Bloodied");
|
||||
});
|
||||
|
||||
it("handles unknown tags by extracting first segment", () => {
|
||||
expect(stripTags("{@unknown Something|else}")).toBe("Something");
|
||||
});
|
||||
|
||||
it("handles multiple tags in the same string", () => {
|
||||
expect(stripTags("{@hit 4}, reach 5 ft. {@h}5 ({@damage 1d6 + 2})")).toBe(
|
||||
"+4, reach 5 ft. Hit: 5 (1d6 + 2)",
|
||||
);
|
||||
});
|
||||
|
||||
it("handles nested tags gracefully", () => {
|
||||
expect(
|
||||
stripTags("The spell {@spell Fireball|XPHB} deals {@damage 8d6}."),
|
||||
).toBe("The spell Fireball deals 8d6.");
|
||||
});
|
||||
|
||||
it("handles text with no tags", () => {
|
||||
expect(stripTags("Just plain text.")).toBe("Just plain text.");
|
||||
});
|
||||
|
||||
it("strips {@actSaveSuccessOrFail} to Success or Failure:", () => {
|
||||
expect(stripTags("{@actSaveSuccessOrFail}")).toBe("Success or Failure:");
|
||||
});
|
||||
|
||||
it("strips {@action Name|Source} to Name when no display text", () => {
|
||||
expect(stripTags("{@action Disengage|XPHB}")).toBe("Disengage");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user