Fix PF2e stat block senses and attack trait rendering
All checks were successful
CI / check (push) Successful in 2m20s
CI / build-image (push) Successful in 17s

- 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:
Lukas
2026-04-07 12:23:08 +02:00
parent 8dbff66ce1
commit 65e4db153b
3 changed files with 124 additions and 30 deletions

View File

@@ -1,11 +1,11 @@
import { describe, expect, it } from "vitest";
import { normalizePf2eBestiary } from "../pf2e-bestiary-adapter.js";
function minimalCreature(defenses?: Record<string, unknown>) {
function minimalCreature(overrides?: Record<string, unknown>) {
return {
name: "Test Creature",
source: "TST",
defenses,
...overrides,
};
}
@@ -15,7 +15,9 @@ describe("normalizePf2eBestiary", () => {
const [creature] = normalizePf2eBestiary({
creature: [
minimalCreature({
weaknesses: [{ name: "fire", amount: 5 }],
defenses: {
weaknesses: [{ name: "fire", amount: 5 }],
},
}),
],
});
@@ -26,7 +28,9 @@ describe("normalizePf2eBestiary", () => {
const [creature] = normalizePf2eBestiary({
creature: [
minimalCreature({
weaknesses: [{ name: "smoke susceptibility" }],
defenses: {
weaknesses: [{ name: "smoke susceptibility" }],
},
}),
],
});
@@ -37,9 +41,11 @@ describe("normalizePf2eBestiary", () => {
const [creature] = normalizePf2eBestiary({
creature: [
minimalCreature({
weaknesses: [
{ name: "cold iron", amount: 5, note: "except daggers" },
],
defenses: {
weaknesses: [
{ name: "cold iron", amount: 5, note: "except daggers" },
],
},
}),
],
});
@@ -50,7 +56,9 @@ describe("normalizePf2eBestiary", () => {
const [creature] = normalizePf2eBestiary({
creature: [
minimalCreature({
weaknesses: [{ name: "smoke susceptibility", note: "see below" }],
defenses: {
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", () => {
it("formats resistance without amount", () => {
const [creature] = normalizePf2eBestiary({
creature: [
minimalCreature({
resistances: [{ name: "physical" }],
defenses: {
resistances: [{ name: "physical" }],
},
}),
],
});
@@ -81,7 +160,9 @@ describe("normalizePf2eBestiary", () => {
const [creature] = normalizePf2eBestiary({
creature: [
minimalCreature({
resistances: [{ name: "fire", amount: 10 }],
defenses: {
resistances: [{ name: "fire", amount: 10 }],
},
}),
],
});