Fix PF2e stat block senses and attack trait rendering
- Format senses with type (imprecise/precise) and range in feet,
and strip {@ability} tags (e.g. tremorsense)
- Strip angle-bracket dice notation in attack traits (<d8> → d8)
- Fix existing weakness/resistance tests to nest under defenses
- Fix non-null assertions in 5e bestiary adapter tests
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -6,7 +6,8 @@ import {
|
|||||||
} from "../bestiary-adapter.js";
|
} from "../bestiary-adapter.js";
|
||||||
|
|
||||||
/** Flatten segments to a single string for simple text assertions. */
|
/** Flatten segments to a single string for simple text assertions. */
|
||||||
function flatText(trait: TraitBlock): string {
|
function flatText(trait: TraitBlock | undefined): string {
|
||||||
|
if (!trait) return "";
|
||||||
return trait.segments
|
return trait.segments
|
||||||
.map((s) =>
|
.map((s) =>
|
||||||
s.type === "text"
|
s.type === "text"
|
||||||
@@ -88,11 +89,11 @@ describe("normalizeBestiary", () => {
|
|||||||
expect(c.senses).toBe("Darkvision 60 ft.");
|
expect(c.senses).toBe("Darkvision 60 ft.");
|
||||||
expect(c.languages).toBe("Common, Goblin");
|
expect(c.languages).toBe("Common, Goblin");
|
||||||
expect(c.actions).toHaveLength(1);
|
expect(c.actions).toHaveLength(1);
|
||||||
expect(flatText(c.actions![0])).toContain("Melee Attack Roll:");
|
expect(flatText(c.actions?.[0])).toContain("Melee Attack Roll:");
|
||||||
expect(flatText(c.actions![0])).not.toContain("{@");
|
expect(flatText(c.actions?.[0])).not.toContain("{@");
|
||||||
expect(c.bonusActions).toHaveLength(1);
|
expect(c.bonusActions).toHaveLength(1);
|
||||||
expect(flatText(c.bonusActions![0])).toContain("Disengage");
|
expect(flatText(c.bonusActions?.[0])).toContain("Disengage");
|
||||||
expect(flatText(c.bonusActions![0])).not.toContain("{@");
|
expect(flatText(c.bonusActions?.[0])).not.toContain("{@");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("normalizes a creature with legendary actions", () => {
|
it("normalizes a creature with legendary actions", () => {
|
||||||
@@ -347,9 +348,9 @@ describe("normalizeBestiary", () => {
|
|||||||
|
|
||||||
const creatures = normalizeBestiary(raw);
|
const creatures = normalizeBestiary(raw);
|
||||||
const bite = creatures[0].actions?.[0];
|
const bite = creatures[0].actions?.[0];
|
||||||
expect(flatText(bite!)).toContain("Melee Weapon Attack:");
|
expect(flatText(bite)).toContain("Melee Weapon Attack:");
|
||||||
expect(flatText(bite!)).not.toContain("mw");
|
expect(flatText(bite)).not.toContain("mw");
|
||||||
expect(flatText(bite!)).not.toContain("{@");
|
expect(flatText(bite)).not.toContain("{@");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("handles fly speed with hover condition", () => {
|
it("handles fly speed with hover condition", () => {
|
||||||
@@ -438,14 +439,15 @@ describe("normalizeBestiary", () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const creatures = normalizeBestiary(raw);
|
const creatures = normalizeBestiary(raw);
|
||||||
const trait = creatures[0].traits![0];
|
const trait = creatures[0].traits?.[0];
|
||||||
expect(trait.name).toBe("Confusing Burble");
|
expect(trait).toBeDefined();
|
||||||
expect(trait.segments).toHaveLength(2);
|
expect(trait?.name).toBe("Confusing Burble");
|
||||||
expect(trait.segments[0]).toEqual({
|
expect(trait?.segments).toHaveLength(2);
|
||||||
|
expect(trait?.segments[0]).toEqual({
|
||||||
type: "text",
|
type: "text",
|
||||||
value: expect.stringContaining("d4"),
|
value: expect.stringContaining("d4"),
|
||||||
});
|
});
|
||||||
expect(trait.segments[1]).toEqual({
|
expect(trait?.segments[1]).toEqual({
|
||||||
type: "list",
|
type: "list",
|
||||||
items: [
|
items: [
|
||||||
{ label: "1-2", text: "The creature does nothing." },
|
{ label: "1-2", text: "The creature does nothing." },
|
||||||
@@ -498,8 +500,9 @@ describe("normalizeBestiary", () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const creatures = normalizeBestiary(raw);
|
const creatures = normalizeBestiary(raw);
|
||||||
const trait = creatures[0].traits![0];
|
const trait = creatures[0].traits?.[0];
|
||||||
expect(trait.segments[1]).toEqual({
|
expect(trait).toBeDefined();
|
||||||
|
expect(trait?.segments[1]).toEqual({
|
||||||
type: "list",
|
type: "list",
|
||||||
items: [
|
items: [
|
||||||
{ label: "1", text: "Nothing happens." },
|
{ label: "1", text: "Nothing happens." },
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { normalizePf2eBestiary } from "../pf2e-bestiary-adapter.js";
|
import { normalizePf2eBestiary } from "../pf2e-bestiary-adapter.js";
|
||||||
|
|
||||||
function minimalCreature(defenses?: Record<string, unknown>) {
|
function minimalCreature(overrides?: Record<string, unknown>) {
|
||||||
return {
|
return {
|
||||||
name: "Test Creature",
|
name: "Test Creature",
|
||||||
source: "TST",
|
source: "TST",
|
||||||
defenses,
|
...overrides,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -15,7 +15,9 @@ describe("normalizePf2eBestiary", () => {
|
|||||||
const [creature] = normalizePf2eBestiary({
|
const [creature] = normalizePf2eBestiary({
|
||||||
creature: [
|
creature: [
|
||||||
minimalCreature({
|
minimalCreature({
|
||||||
|
defenses: {
|
||||||
weaknesses: [{ name: "fire", amount: 5 }],
|
weaknesses: [{ name: "fire", amount: 5 }],
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
@@ -26,7 +28,9 @@ describe("normalizePf2eBestiary", () => {
|
|||||||
const [creature] = normalizePf2eBestiary({
|
const [creature] = normalizePf2eBestiary({
|
||||||
creature: [
|
creature: [
|
||||||
minimalCreature({
|
minimalCreature({
|
||||||
|
defenses: {
|
||||||
weaknesses: [{ name: "smoke susceptibility" }],
|
weaknesses: [{ name: "smoke susceptibility" }],
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
@@ -37,9 +41,11 @@ describe("normalizePf2eBestiary", () => {
|
|||||||
const [creature] = normalizePf2eBestiary({
|
const [creature] = normalizePf2eBestiary({
|
||||||
creature: [
|
creature: [
|
||||||
minimalCreature({
|
minimalCreature({
|
||||||
|
defenses: {
|
||||||
weaknesses: [
|
weaknesses: [
|
||||||
{ name: "cold iron", amount: 5, note: "except daggers" },
|
{ name: "cold iron", amount: 5, note: "except daggers" },
|
||||||
],
|
],
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
@@ -50,7 +56,9 @@ describe("normalizePf2eBestiary", () => {
|
|||||||
const [creature] = normalizePf2eBestiary({
|
const [creature] = normalizePf2eBestiary({
|
||||||
creature: [
|
creature: [
|
||||||
minimalCreature({
|
minimalCreature({
|
||||||
|
defenses: {
|
||||||
weaknesses: [{ name: "smoke susceptibility", note: "see below" }],
|
weaknesses: [{ name: "smoke susceptibility", note: "see below" }],
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
@@ -65,12 +73,83 @@ describe("normalizePf2eBestiary", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("senses formatting", () => {
|
||||||
|
it("strips tags and includes type and range", () => {
|
||||||
|
const [creature] = normalizePf2eBestiary({
|
||||||
|
creature: [
|
||||||
|
minimalCreature({
|
||||||
|
senses: [
|
||||||
|
{
|
||||||
|
type: "imprecise",
|
||||||
|
name: "{@ability tremorsense}",
|
||||||
|
range: 30,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
expect(creature.senses).toBe("Tremorsense (imprecise) 30 feet");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("formats sense with only a name", () => {
|
||||||
|
const [creature] = normalizePf2eBestiary({
|
||||||
|
creature: [
|
||||||
|
minimalCreature({
|
||||||
|
senses: [{ name: "darkvision" }],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
expect(creature.senses).toBe("Darkvision");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("formats sense with name and range but no type", () => {
|
||||||
|
const [creature] = normalizePf2eBestiary({
|
||||||
|
creature: [
|
||||||
|
minimalCreature({
|
||||||
|
senses: [{ name: "scent", range: 60 }],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
expect(creature.senses).toBe("Scent 60 feet");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("attack traits formatting", () => {
|
||||||
|
it("strips angle-bracket dice notation from traits", () => {
|
||||||
|
const [creature] = normalizePf2eBestiary({
|
||||||
|
creature: [
|
||||||
|
minimalCreature({
|
||||||
|
attacks: [
|
||||||
|
{
|
||||||
|
name: "stinger",
|
||||||
|
range: "Melee",
|
||||||
|
attack: 11,
|
||||||
|
traits: ["deadly <d8>"],
|
||||||
|
damage: "1d6+4 piercing",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
const attack = creature.attacks?.[0];
|
||||||
|
expect(attack).toBeDefined();
|
||||||
|
expect(attack?.segments[0]).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
type: "text",
|
||||||
|
value: expect.stringContaining("(deadly d8)"),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("resistances formatting", () => {
|
describe("resistances formatting", () => {
|
||||||
it("formats resistance without amount", () => {
|
it("formats resistance without amount", () => {
|
||||||
const [creature] = normalizePf2eBestiary({
|
const [creature] = normalizePf2eBestiary({
|
||||||
creature: [
|
creature: [
|
||||||
minimalCreature({
|
minimalCreature({
|
||||||
|
defenses: {
|
||||||
resistances: [{ name: "physical" }],
|
resistances: [{ name: "physical" }],
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
@@ -81,7 +160,9 @@ describe("normalizePf2eBestiary", () => {
|
|||||||
const [creature] = normalizePf2eBestiary({
|
const [creature] = normalizePf2eBestiary({
|
||||||
creature: [
|
creature: [
|
||||||
minimalCreature({
|
minimalCreature({
|
||||||
|
defenses: {
|
||||||
resistances: [{ name: "fire", amount: 10 }],
|
resistances: [{ name: "fire", amount: 10 }],
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ interface RawPf2eCreature {
|
|||||||
level?: number;
|
level?: number;
|
||||||
traits?: string[];
|
traits?: string[];
|
||||||
perception?: { std?: number };
|
perception?: { std?: number };
|
||||||
senses?: { name?: string; type?: string }[];
|
senses?: { name?: string; type?: string; range?: number }[];
|
||||||
languages?: { languages?: string[] };
|
languages?: { languages?: string[] };
|
||||||
skills?: Record<string, { std?: number }>;
|
skills?: Record<string, { std?: number }>;
|
||||||
abilityMods?: Record<string, number>;
|
abilityMods?: Record<string, number>;
|
||||||
@@ -79,6 +79,10 @@ function capitalize(s: string): string {
|
|||||||
return s.charAt(0).toUpperCase() + s.slice(1);
|
return s.charAt(0).toUpperCase() + s.slice(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function stripDiceBrackets(s: string): string {
|
||||||
|
return s.replaceAll(/<(\d*d\d+)>/g, "$1");
|
||||||
|
}
|
||||||
|
|
||||||
function makeCreatureId(source: string, name: string): CreatureId {
|
function makeCreatureId(source: string, name: string): CreatureId {
|
||||||
const slug = name
|
const slug = name
|
||||||
.toLowerCase()
|
.toLowerCase()
|
||||||
@@ -119,13 +123,19 @@ function formatSkills(
|
|||||||
}
|
}
|
||||||
|
|
||||||
function formatSenses(
|
function formatSenses(
|
||||||
senses: readonly { name?: string; type?: string }[] | undefined,
|
senses:
|
||||||
|
| readonly { name?: string; type?: string; range?: number }[]
|
||||||
|
| undefined,
|
||||||
): string | undefined {
|
): string | undefined {
|
||||||
if (!senses || senses.length === 0) return undefined;
|
if (!senses || senses.length === 0) return undefined;
|
||||||
return senses
|
return senses
|
||||||
.map((s) => {
|
.map((s) => {
|
||||||
const label = s.name ?? s.type ?? "";
|
const label = stripTags(s.name ?? s.type ?? "");
|
||||||
return label ? capitalize(label) : "";
|
if (!label) return "";
|
||||||
|
const parts = [capitalize(label)];
|
||||||
|
if (s.type && s.name) parts.push(`(${s.type})`);
|
||||||
|
if (s.range != null) parts.push(`${s.range} feet`);
|
||||||
|
return parts.join(" ");
|
||||||
})
|
})
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.join(", ");
|
.join(", ");
|
||||||
@@ -253,7 +263,7 @@ function normalizeAttacks(
|
|||||||
const attackMod = a.attack == null ? "" : ` +${a.attack}`;
|
const attackMod = a.attack == null ? "" : ` +${a.attack}`;
|
||||||
const traits =
|
const traits =
|
||||||
a.traits && a.traits.length > 0
|
a.traits && a.traits.length > 0
|
||||||
? ` (${a.traits.map((t) => stripTags(t)).join(", ")})`
|
? ` (${a.traits.map((t) => stripDiceBrackets(stripTags(t))).join(", ")})`
|
||||||
: "";
|
: "";
|
||||||
const damage = a.damage ? `, ${stripTags(a.damage)}` : "";
|
const damage = a.damage ? `, ${stripTags(a.damage)}` : "";
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user