Compare commits

...

20 Commits

Author SHA1 Message Date
Lukas
48795071f7 Hide concentration UI in PF2e mode
All checks were successful
CI / check (push) Successful in 2m26s
CI / build-image (push) Successful in 18s
PF2e uses action-based spell sustaining, not damage-triggered
concentration checks. The Brain icon, purple border accent, and
damage pulse animation are now hidden when PF2e is active, and
the freed gutter column is reclaimed for row content. Concentration
state is preserved so switching back to D&D restores it.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-09 00:25:54 +02:00
Lukas
f721d7e5da Allow /commit skill to be invoked by other skills
All checks were successful
CI / check (push) Successful in 2m26s
CI / build-image (push) Successful in 5s
Remove disable-model-invocation so /ship can delegate to /commit.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-09 00:13:59 +02:00
Lukas
e7930a1431 Add /ship skill for commit, tag, and push workflow
All checks were successful
CI / check (push) Successful in 2m27s
CI / build-image (push) Successful in 18s
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-09 00:09:03 +02:00
Lukas
553e09f280 Enforce maximum values for PF2e numbered conditions
Cap dying (4), doomed (3), wounded (3), and slowed (3) at their
rule-defined maximums. The domain clamps values in setConditionValue
and the condition picker disables the [+] button at the cap.

Closes #31

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-09 00:04:47 +02:00
Lukas
1c107a500b Switch PF2e data source from Pf2eTools to Foundry VTT PF2e
All checks were successful
CI / check (push) Successful in 2m25s
CI / build-image (push) Successful in 23s
Replace the stagnant Pf2eTools bestiary with Foundry VTT PF2e system
data (github.com/foundryvtt/pf2e, v13-dev branch). This gives us 4,355
remaster-era creatures across 49 sources including Monster Core 1+2 and
all adventure paths.

Changes:
- Rewrite index generation script to walk Foundry pack directories
- Rewrite PF2e normalization adapter for Foundry JSON shape (system.*
  fields, items[] for attacks/abilities/spells)
- Add stripFoundryTags utility for Foundry HTML + enrichment syntax
- Implement multi-file source fetching (one request per creature file)
- Add spellcasting section to PF2e stat block (ranked spells + cantrips)
- Add saveConditional and hpDetails to PF2e domain type and stat block
- Add size and rarity to PF2e trait tags
- Filter redundant glossary abilities (healing when in hp.details,
  spell mechanic reminders, allSaves duplicates)
- Add PF2e stat block component tests (22 tests)
- Bump IndexedDB cache version to 5 for clean migration

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-08 21:05:00 +02:00
Lukas
0c235112ee Improve PF2e stat block action icons, triggers, and tag handling
- Replace unicode action cost chars with custom SVG icons (diamond
  with chevron for actions, outlined diamond for free, curved arrow
  for reaction) rendered inline via ActivityCost on TraitBlock
- Add activity icons to attacks (all Strikes default to single action)
- Add trigger/effect rendering for reaction abilities (bold labels)
- Fix nested tag stripping ({@b ...{@spell ...}...}) by looping
- Move icon after ability name to match AoN format

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-08 10:36:30 +02:00
Lukas
57278e0c82 Add PF2e action cost icons to ability names
All checks were successful
CI / check (push) Successful in 2m21s
CI / build-image (push) Successful in 17s
Show Unicode action icons (◆/◆◆/◆◆◆ for actions, ◇ for free,
↺ for reaction) in ability names from the activity field.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 16:31:24 +02:00
Lukas
f9cfaa2570 Include traits on PF2e ability blocks
Parse and display traits (concentrate, divine, polymorph, etc.)
on ability entries, matching how attack traits are already shown.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 16:29:08 +02:00
Lukas
3e62e54274 Strip all angle brackets in PF2e attack traits and damage
All checks were successful
CI / check (push) Successful in 2m23s
CI / build-image (push) Successful in 17s
Broaden stripDiceBrackets to stripAngleBrackets to handle all
PF2e tools angle-bracket formatting (e.g. <10 feet>, <15 feet>),
not just dice notation. Also strip in damage text.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 15:34:28 +02:00
Lukas
12a089dfd7 Fix PF2e condition tooltip descriptions and sort picker alphabetically
Correct inaccurate PF2e condition descriptions against official AoN
rules (blinded, deafened, confused, grabbed, hidden, paralyzed,
unconscious, drained, fascinated, enfeebled, stunned). Sort condition
picker alphabetically per game system.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 14:29:49 +02:00
Lukas
65e4db153b 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>
2026-04-07 12:23:08 +02:00
Lukas
8dbff66ce1 Fix "undefined" in PF2e stat block weaknesses/resistances
All checks were successful
CI / check (push) Successful in 2m23s
CI / build-image (push) Successful in 30s
Some PF2e creatures (e.g. Giant Mining Bee) have qualitative
weaknesses without a numeric amount, causing "undefined" to
render in the stat block. Handle missing amounts gracefully.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 12:06:22 +02:00
Lukas
e62c49434c Add Pathfinder 2e game system mode
All checks were successful
CI / check (push) Successful in 2m21s
CI / build-image (push) Successful in 24s
Implements PF2e as an alternative game system alongside D&D 5e/5.5e.
Settings modal "Game System" selector switches conditions, bestiary,
stat block layout, and initiative calculation between systems.

- Valued conditions with increment/decrement UX (Clumsy 2, Frightened 3)
- 2,502 PF2e creatures from bundled search index (77 sources)
- PF2e stat block: level, traits, Perception, Fort/Ref/Will, ability mods
- Perception-based initiative rolling
- System-scoped source cache (D&D and PF2e sources don't collide)
- Backwards-compatible condition rehydration (ConditionId[] → ConditionEntry[])
- Difficulty indicator hidden in PF2e mode (excluded from MVP)

Closes dostulata/initiative#19

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 01:26:22 +02:00
Lukas
8f6eebc43b Render structured list and table entries in stat block traits
All checks were successful
CI / check (push) Successful in 2m21s
CI / build-image (push) Successful in 17s
Stat block traits containing 5etools list (e.g. Confusing Burble d4
effects) or table entries were silently dropped. The adapter now
produces structured TraitSegment[] instead of flat text, preserving
lists and tables as first-class data. The stat block component renders
labeled list items inline (bold label + flowing text) matching the
5etools layout. Also fixes support for the singular "entry" field on
list items and bumps the bestiary cache version to force re-normalize.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 22:09:11 +02:00
Lukas
817cfddabc Add 2014 DMG encounter difficulty calculation
All checks were successful
CI / check (push) Successful in 2m18s
CI / build-image (push) Successful in 17s
Support the 2014 DMG encounter difficulty as an alternative to the 5.5e
system behind the existing Rules Edition toggle. The 2014 system uses
Easy/Medium/Hard/Deadly thresholds, an encounter multiplier based on
monster count, and party size adjustment (×0.5–×5 range).

- Extract RulesEdition to its own domain module
- Refactor DifficultyTier to abstract numeric values (0–3)
- Restructure DifficultyResult with thresholds array
- Add 2014 XP thresholds table and encounter multiplier logic
- Wire edition from context into difficulty hooks
- Edition-aware labels in indicator and breakdown panel
- Show multiplier, adjusted XP, and party size note for 2014
- Rename settings label from "Conditions" to "Rules Edition"
- Update spec 008 with issue #23 requirements

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 14:52:23 +02:00
Lukas
94e1806112 Add combatant side assignment for encounter difficulty
All checks were successful
CI / check (push) Successful in 2m18s
CI / build-image (push) Successful in 17s
Combatants can now be assigned to party or enemy side via a toggle
in the difficulty breakdown panel. Party-side NPCs subtract their XP
from the encounter total, letting allied NPCs reduce difficulty.
PCs default to party, non-PCs to enemy — users who don't use sides
see no change. Side persists across reload and export/import.

Closes #22

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-03 14:15:12 +02:00
Lukas
30e7ed4121 Stabilize turn navigation bar layout with CSS grid
All checks were successful
CI / check (push) Successful in 2m18s
CI / build-image (push) Successful in 17s
Use a three-column grid (1fr / auto / 1fr) so the active combatant
name stays centered while round badge and difficulty indicator are
anchored in the left and right zones. Prevents layout jumps when
the name changes between turns.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-03 02:15:16 +02:00
Lukas
5540baf14c Show concentration icon on mobile as grey affordance
On touch devices, the Brain icon was fully hidden (opacity-0) unlike
the edit and condition buttons. Add pointer-coarse:opacity-50 so it
appears as a discoverable grey icon, matching the other action buttons.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-03 01:02:16 +02:00
Lukas
1ae9e12cff Add manual CR assignment and difficulty breakdown panel
All checks were successful
CI / check (push) Successful in 2m20s
CI / build-image (push) Successful in 17s
Implement issue #21: custom combatants can now have a challenge rating
assigned via a new breakdown panel, opened by tapping the difficulty
indicator. Bestiary-linked combatants show read-only CR with source name;
custom combatants get a CR picker with all standard 5e values. CR persists
across reloads and round-trips through JSON export/import.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-02 17:03:33 +02:00
Lukas
2c643cc98b Introduce adapter injection and migrate test suite
All checks were successful
CI / check (push) Successful in 2m13s
CI / build-image (push) Has been skipped
Replace direct adapter/persistence imports with context-based injection
(AdapterContext + useAdapters) so tests use in-memory implementations
instead of vi.mock. Migrate component tests from context mocking to
AllProviders with real hooks. Extract export/import logic from ActionBar
into useEncounterExportImport hook. Add bestiary-cache and
bestiary-index-adapter test suites. Raise adapter coverage thresholds
(68→80 lines, 56→62 branches).

77 test files, 891 tests, all passing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-01 23:55:45 +02:00
121 changed files with 9975 additions and 2067 deletions

View File

@@ -1,7 +1,6 @@
--- ---
name: commit name: commit
description: Create a git commit with pre-commit hooks (bypasses sandbox restrictions). description: Create a git commit with pre-commit hooks (bypasses sandbox restrictions).
disable-model-invocation: true
allowed-tools: Bash(git *), Bash(pnpm *) allowed-tools: Bash(git *), Bash(pnpm *)
--- ---

View File

@@ -0,0 +1,54 @@
---
name: ship
description: Commit, tag with the next version, and push to remote.
disable-model-invocation: true
allowed-tools: Bash(git *), Bash(pnpm *), Skill
---
## Instructions
Commit current changes, create the next version tag, and push everything to remote.
### Step 1 — Commit
Use the `/commit` skill to stage and commit changes. Pass along any user arguments as the commit message.
```
/commit $ARGUMENTS
```
### Step 2 — Tag
Get the latest tag and increment the patch number (e.g., `0.9.27``0.9.28`). Create the tag:
```bash
git tag --sort=-v:refname | head -1
```
```bash
git tag <next-version>
```
### Step 3 — Push
Push the commit and tag to remote:
```bash
git push && git push --tags
```
### Step 4 — Verify
Confirm the tag exists on the pushed commit:
```bash
git log --oneline -1 --decorate
```
## User arguments
```text
$ARGUMENTS
```
If the user provided arguments, treat them as the commit message or guidance for what to commit.

View File

@@ -69,12 +69,62 @@ docs/agents/ RPI skill artifacts (research reports, plans)
- **oxlint** for type-aware linting that Biome can't do. Configured in `.oxlintrc.json`. - **oxlint** for type-aware linting that Biome can't do. Configured in `.oxlintrc.json`.
- **TypeScript strict mode** with `verbatimModuleSyntax`. Use `.js` extensions in relative imports. - **TypeScript strict mode** with `verbatimModuleSyntax`. Use `.js` extensions in relative imports.
- **Branded types** for identity values (e.g., `CombatantId`). Prefer immutability/`readonly` where practical. - **Branded types** for identity values (e.g., `CombatantId`). Prefer immutability/`readonly` where practical.
- **Reuse UI primitives** — before creating custom interactive elements (buttons, inputs, selects, dialogs), check `apps/web/src/components/ui/` for existing components with established variants and hover styles.
- **Domain events** are plain data objects with a `type` discriminant — no classes. - **Domain events** are plain data objects with a `type` discriminant — no classes.
- **Tests** live in `packages/*/src/__tests__/*.test.ts`. Test pure functions directly; map acceptance scenarios from specs to individual `it()` blocks. - **Tests** live in `__tests__/` directories adjacent to source. See the Testing section below for conventions on mocking, assertions, and per-layer approach.
- **Quality gates** are enforced at pre-commit via Lefthook (parallel jobs). No gate may exist only as a CI step or manual process. - **Quality gates** are enforced at pre-commit via Lefthook (parallel jobs). No gate may exist only as a CI step or manual process.
For component prop rules, export format compatibility, and ADRs, see [`docs/conventions.md`](docs/conventions.md). For component prop rules, export format compatibility, and ADRs, see [`docs/conventions.md`](docs/conventions.md).
## Testing
### Philosophy
Test **user-visible behavior**, not implementation details. A good test answers "does this feature work?" not "does this internal function get called?"
### Adapter Injection
Adapters (storage, cache, browser APIs) are provided via `AdapterContext`. Production wires real implementations; tests wire in-memory implementations. This means:
- No `vi.mock()` for adapter or persistence modules
- Tests control adapter behavior by configuring the in-memory implementation
- Type changes in adapter interfaces are caught at compile time
### Per-Layer Approach
| Layer | How to test |
|---|---|
| Domain (`packages/domain`) | Pure unit tests, no mocks, test invariants and acceptance scenarios |
| Application (`packages/application`) | Mock port interfaces only, use real domain logic |
| Hooks (context-wrapped) | Test via `renderHook` with `AllProviders` wrapping in-memory adapters |
| Hooks (component-specific) | Test through the component that uses them |
| Components | Render with `AllProviders`, use in-memory adapters, use `userEvent` for interactions |
### Test Data
Use factory functions from `apps/web/src/__tests__/factories/` to construct domain objects. Each factory provides sensible defaults overridden via `Partial<T>`:
```typescript
import { buildEncounter } from "../../__tests__/factories/build-encounter.js";
import { buildCombatant } from "../../__tests__/factories/build-combatant.js";
const encounter = buildEncounter({
combatants: [buildCombatant({ name: "Goblin" })],
activeIndex: 0,
roundNumber: 1,
});
```
Add new factory files as needed (one per domain type). Don't inline test data construction — use factories so type changes are caught at compile time.
### Anti-Patterns
- **`vi.mock()` for adapters**: Use in-memory adapter implementations via `AdapterContext` instead
- **Mocking contexts**: Use `AllProviders` and drive state through real hooks instead of `vi.mock("../../contexts/...")`. Exception: context mocks are acceptable when the component under test requires specific state machine states that cannot be reached through adapter configuration alone — document the reason in a comment at the top of the test file.
- **Stubbing child components**: Render real children; stub only if the child has heavy I/O that can't be mocked at the adapter level
- **Asserting mock call counts**: Prefer asserting what the user sees (`screen.getByText(...)`) over `expect(mockFn).toHaveBeenCalledWith(...)`
- **Testing internal state**: Don't assert `result.current.suggestionIndex === 0`; assert the first suggestion is highlighted
- **Assertion-free tests**: Every `it()` block must contain at least one `expect()`. Tests that render without asserting inflate coverage without catching bugs.
## Self-Review Checklist ## Self-Review Checklist
Before finishing a change, consider: Before finishing a change, consider:

View File

@@ -29,6 +29,6 @@
"@vitejs/plugin-react": "^6.0.1", "@vitejs/plugin-react": "^6.0.1",
"jsdom": "^29.0.1", "jsdom": "^29.0.1",
"tailwindcss": "^4.2.2", "tailwindcss": "^4.2.2",
"vite": "^8.0.1" "vite": "^8.0.5"
} }
} }

View File

@@ -0,0 +1,121 @@
import {
type AnyCreature,
type CreatureId,
EMPTY_UNDO_REDO_STATE,
type Encounter,
type PlayerCharacter,
type UndoRedoState,
} from "@initiative/domain";
import type { Adapters } from "../../contexts/adapter-context.js";
export function createTestAdapters(options?: {
encounter?: Encounter | null;
undoRedoState?: UndoRedoState;
playerCharacters?: PlayerCharacter[];
creatures?: Map<CreatureId, AnyCreature>;
sources?: Map<
string,
{ displayName: string; creatures: AnyCreature[]; cachedAt: number }
>;
}): Adapters {
let storedEncounter = options?.encounter ?? null;
let storedUndoRedo = options?.undoRedoState ?? EMPTY_UNDO_REDO_STATE;
let storedPCs = options?.playerCharacters ?? [];
const sourceStore =
options?.sources ??
new Map<
string,
{ displayName: string; creatures: AnyCreature[]; cachedAt: number }
>();
// Pre-populate sourceStore from creatures map if provided
if (options?.creatures && !options?.sources) {
// No-op: creatures are accessed directly from the map
}
const creatureMap = options?.creatures ?? new Map<CreatureId, AnyCreature>();
return {
encounterPersistence: {
load: () => storedEncounter,
save: (e) => {
storedEncounter = e;
},
},
undoRedoPersistence: {
load: () => storedUndoRedo,
save: (state) => {
storedUndoRedo = state;
},
},
playerCharacterPersistence: {
load: () => [...storedPCs],
save: (pcs) => {
storedPCs = pcs;
},
},
bestiaryCache: {
cacheSource(system, sourceCode, displayName, creatures) {
const key = `${system}:${sourceCode}`;
sourceStore.set(key, {
displayName,
creatures,
cachedAt: Date.now(),
});
for (const c of creatures) {
creatureMap.set(c.id, c);
}
return Promise.resolve();
},
isSourceCached(system, sourceCode) {
return Promise.resolve(sourceStore.has(`${system}:${sourceCode}`));
},
getCachedSources(system) {
return Promise.resolve(
[...sourceStore.entries()]
.filter(([key]) => !system || key.startsWith(`${system}:`))
.map(([key, info]) => ({
sourceCode: key.includes(":")
? key.slice(key.indexOf(":") + 1)
: key,
displayName: info.displayName,
creatureCount: info.creatures.length,
cachedAt: info.cachedAt,
})),
);
},
clearSource(system, sourceCode) {
sourceStore.delete(`${system}:${sourceCode}`);
return Promise.resolve();
},
clearAll() {
sourceStore.clear();
return Promise.resolve();
},
loadAllCachedCreatures() {
return Promise.resolve(new Map(creatureMap));
},
},
bestiaryIndex: {
loadIndex: () => ({ sources: {}, creatures: [] }),
getAllSourceCodes: () => [],
getDefaultFetchUrl: (sourceCode, baseUrl) => {
const filename = `bestiary-${sourceCode.toLowerCase()}.json`;
if (baseUrl !== undefined) {
const normalized = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
return `${normalized}${filename}`;
}
return `https://example.com/${filename}`;
},
getSourceDisplayName: (sourceCode) => sourceCode,
},
pf2eBestiaryIndex: {
loadIndex: () => ({ sources: {}, creatures: [] }),
getAllSourceCodes: () => [],
getDefaultFetchUrl: (sourceCode) =>
`https://example.com/creatures-${sourceCode.toLowerCase()}.json`,
getSourceDisplayName: (sourceCode) => sourceCode,
getCreaturePathsForSource: () => [],
},
};
}

View File

@@ -7,34 +7,6 @@ import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import { App } from "../App.js"; import { App } from "../App.js";
import { AllProviders } from "./test-providers.js"; import { AllProviders } from "./test-providers.js";
// Mock persistence — no localStorage interaction
vi.mock("../persistence/encounter-storage.js", () => ({
loadEncounter: () => null,
saveEncounter: () => {},
}));
vi.mock("../persistence/player-character-storage.js", () => ({
loadPlayerCharacters: () => [],
savePlayerCharacters: () => {},
}));
// Mock bestiary — no IndexedDB or JSON index
vi.mock("../adapters/bestiary-cache.js", () => ({
loadAllCachedCreatures: () => Promise.resolve(new Map()),
isSourceCached: () => Promise.resolve(false),
cacheSource: () => Promise.resolve(),
getCachedSources: () => Promise.resolve([]),
clearSource: () => Promise.resolve(),
clearAll: () => Promise.resolve(),
}));
vi.mock("../adapters/bestiary-index-adapter.js", () => ({
loadBestiaryIndex: () => ({ sources: {}, creatures: [] }),
getAllSourceCodes: () => [],
getDefaultFetchUrl: () => "",
getSourceDisplayName: (code: string) => code,
}));
// DOM API stubs — jsdom doesn't implement these // DOM API stubs — jsdom doesn't implement these
beforeAll(() => { beforeAll(() => {
Object.defineProperty(globalThis, "matchMedia", { Object.defineProperty(globalThis, "matchMedia", {

View File

@@ -1,40 +0,0 @@
import { describe, expect, it } from "vitest";
import {
getAllSourceCodes,
getDefaultFetchUrl,
} from "../adapters/bestiary-index-adapter.js";
describe("getAllSourceCodes", () => {
it("returns all keys from the index sources object", () => {
const codes = getAllSourceCodes();
expect(codes.length).toBeGreaterThan(0);
expect(Array.isArray(codes)).toBe(true);
for (const code of codes) {
expect(typeof code).toBe("string");
}
});
});
describe("getDefaultFetchUrl", () => {
it("returns the default URL when no baseUrl is provided", () => {
const url = getDefaultFetchUrl("XMM");
expect(url).toBe(
"https://raw.githubusercontent.com/5etools-mirror-3/5etools-src/main/data/bestiary/bestiary-xmm.json",
);
});
it("constructs URL from baseUrl with trailing slash", () => {
const url = getDefaultFetchUrl("PHB", "https://example.com/data/");
expect(url).toBe("https://example.com/data/bestiary-phb.json");
});
it("normalizes baseUrl without trailing slash", () => {
const url = getDefaultFetchUrl("PHB", "https://example.com/data");
expect(url).toBe("https://example.com/data/bestiary-phb.json");
});
it("lowercases the source code in the filename", () => {
const url = getDefaultFetchUrl("MM", "https://example.com/");
expect(url).toBe("https://example.com/bestiary-mm.json");
});
});

View File

@@ -209,6 +209,82 @@ describe("round-trip: export then import", () => {
expect(imported.playerCharacters).toEqual(bundle.playerCharacters); expect(imported.playerCharacters).toEqual(bundle.playerCharacters);
}); });
it("round-trips a combatant with cr field", () => {
const encounterWithCr: Encounter = {
combatants: [
{
id: combatantId("c-1"),
name: "Custom Thug",
cr: "2",
},
],
activeIndex: 0,
roundNumber: 1,
};
const emptyUndoRedo: UndoRedoState = {
undoStack: [],
redoStack: [],
};
const bundle = assembleExportBundle(encounterWithCr, emptyUndoRedo, []);
const serialized = JSON.parse(JSON.stringify(bundle));
const result = validateImportBundle(serialized);
expect(typeof result).toBe("object");
const imported = result as ExportBundle;
expect(imported.encounter.combatants[0].cr).toBe("2");
});
it("round-trips a combatant with side field", () => {
const encounterWithSide: Encounter = {
combatants: [
{
id: combatantId("c-1"),
name: "Allied Guard",
cr: "2",
side: "party",
},
{
id: combatantId("c-2"),
name: "Goblin",
side: "enemy",
},
],
activeIndex: 0,
roundNumber: 1,
};
const emptyUndoRedo: UndoRedoState = {
undoStack: [],
redoStack: [],
};
const bundle = assembleExportBundle(encounterWithSide, emptyUndoRedo, []);
const serialized = JSON.parse(JSON.stringify(bundle));
const result = validateImportBundle(serialized);
expect(typeof result).toBe("object");
const imported = result as ExportBundle;
expect(imported.encounter.combatants[0].side).toBe("party");
expect(imported.encounter.combatants[1].side).toBe("enemy");
});
it("round-trips a combatant without side field as undefined", () => {
const encounterNoSide: Encounter = {
combatants: [{ id: combatantId("c-1"), name: "Custom" }],
activeIndex: 0,
roundNumber: 1,
};
const emptyUndoRedo: UndoRedoState = {
undoStack: [],
redoStack: [],
};
const bundle = assembleExportBundle(encounterNoSide, emptyUndoRedo, []);
const serialized = JSON.parse(JSON.stringify(bundle));
const result = validateImportBundle(serialized);
expect(typeof result).toBe("object");
const imported = result as ExportBundle;
expect(imported.encounter.combatants[0].side).toBeUndefined();
});
it("round-trips an empty encounter", () => { it("round-trips an empty encounter", () => {
const emptyEncounter: Encounter = { const emptyEncounter: Encounter = {
combatants: [], combatants: [],

View File

@@ -0,0 +1,12 @@
import type { Combatant } from "@initiative/domain";
import { combatantId } from "@initiative/domain";
let counter = 0;
export function buildCombatant(overrides?: Partial<Combatant>): Combatant {
return {
id: combatantId(`c-${++counter}`),
name: "Combatant",
...overrides,
};
}

View File

@@ -0,0 +1,26 @@
import type { Creature } from "@initiative/domain";
import { creatureId } from "@initiative/domain";
let counter = 0;
export function buildCreature(overrides?: Partial<Creature>): Creature {
const id = ++counter;
return {
id: creatureId(`creature-${id}`),
name: `Creature ${id}`,
source: "srd",
sourceDisplayName: "SRD",
size: "Medium",
type: "humanoid",
alignment: "neutral",
ac: 13,
hp: { average: 7, formula: "2d6" },
speed: "30 ft.",
abilities: { str: 10, dex: 14, con: 10, int: 10, wis: 10, cha: 10 },
cr: "1/4",
initiativeProficiency: 0,
proficiencyBonus: 2,
passive: 10,
...overrides,
};
}

View File

@@ -0,0 +1,10 @@
import type { Encounter } from "@initiative/domain";
export function buildEncounter(overrides?: Partial<Encounter>): Encounter {
return {
combatants: [],
activeIndex: 0,
roundNumber: 1,
...overrides,
};
}

View File

@@ -0,0 +1,3 @@
export { buildCombatant } from "./build-combatant.js";
export { buildCreature } from "./build-creature.js";
export { buildEncounter } from "./build-encounter.js";

View File

@@ -5,7 +5,9 @@ import type { Creature, CreatureId } from "@initiative/domain";
import { cleanup, fireEvent, render, screen } from "@testing-library/react"; import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
// Mock the context modules // Uses context mocks because StatBlockPanel requires fine-grained control over
// panel state (collapsed/expanded, pinned/unpinned, wide/narrow desktop) that
// would need extensive setup to drive through real providers.
vi.mock("../contexts/side-panel-context.js", () => ({ vi.mock("../contexts/side-panel-context.js", () => ({
useSidePanelContext: vi.fn(), useSidePanelContext: vi.fn(),
})); }));
@@ -14,14 +16,6 @@ vi.mock("../contexts/bestiary-context.js", () => ({
useBestiaryContext: vi.fn(), useBestiaryContext: vi.fn(),
})); }));
// Mock adapters to avoid IndexedDB
vi.mock("../adapters/bestiary-index-adapter.js", () => ({
loadBestiaryIndex: () => ({ sources: {}, creatures: [] }),
getAllSourceCodes: () => [],
getDefaultFetchUrl: () => "",
getSourceDisplayName: (code: string) => code,
}));
import { StatBlockPanel } from "../components/stat-block-panel.js"; import { StatBlockPanel } from "../components/stat-block-panel.js";
import { useBestiaryContext } from "../contexts/bestiary-context.js"; import { useBestiaryContext } from "../contexts/bestiary-context.js";
import { useSidePanelContext } from "../contexts/side-panel-context.js"; import { useSidePanelContext } from "../contexts/side-panel-context.js";

View File

@@ -1,4 +1,6 @@
import type { ReactNode } from "react"; import type { ReactNode } from "react";
import type { Adapters } from "../contexts/adapter-context.js";
import { AdapterProvider } from "../contexts/adapter-context.js";
import { import {
BestiaryProvider, BestiaryProvider,
BulkImportProvider, BulkImportProvider,
@@ -9,9 +11,18 @@ import {
SidePanelProvider, SidePanelProvider,
ThemeProvider, ThemeProvider,
} from "../contexts/index.js"; } from "../contexts/index.js";
import { createTestAdapters } from "./adapters/in-memory-adapters.js";
export function AllProviders({ children }: { children: ReactNode }) { export function AllProviders({
adapters,
children,
}: {
adapters?: Adapters;
children: ReactNode;
}) {
const resolved = adapters ?? createTestAdapters();
return ( return (
<AdapterProvider adapters={resolved}>
<ThemeProvider> <ThemeProvider>
<RulesEditionProvider> <RulesEditionProvider>
<EncounterProvider> <EncounterProvider>
@@ -19,7 +30,9 @@ export function AllProviders({ children }: { children: ReactNode }) {
<PlayerCharactersProvider> <PlayerCharactersProvider>
<BulkImportProvider> <BulkImportProvider>
<SidePanelProvider> <SidePanelProvider>
<InitiativeRollsProvider>{children}</InitiativeRollsProvider> <InitiativeRollsProvider>
{children}
</InitiativeRollsProvider>
</SidePanelProvider> </SidePanelProvider>
</BulkImportProvider> </BulkImportProvider>
</PlayerCharactersProvider> </PlayerCharactersProvider>
@@ -27,5 +40,6 @@ export function AllProviders({ children }: { children: ReactNode }) {
</EncounterProvider> </EncounterProvider>
</RulesEditionProvider> </RulesEditionProvider>
</ThemeProvider> </ThemeProvider>
</AdapterProvider>
); );
} }

View File

@@ -1,9 +1,24 @@
import type { TraitBlock } from "@initiative/domain";
import { beforeAll, describe, expect, it } from "vitest"; import { beforeAll, describe, expect, it } from "vitest";
import { import {
normalizeBestiary, normalizeBestiary,
setSourceDisplayNames, setSourceDisplayNames,
} from "../bestiary-adapter.js"; } from "../bestiary-adapter.js";
/** Flatten segments to a single string for simple text assertions. */
function flatText(trait: TraitBlock | undefined): string {
if (!trait) return "";
return trait.segments
.map((s) =>
s.type === "text"
? s.value
: s.items
.map((i) => (i.label ? `${i.label}. ${i.text}` : i.text))
.join(" "),
)
.join(" ");
}
beforeAll(() => { beforeAll(() => {
setSourceDisplayNames({ XMM: "MM 2024" }); setSourceDisplayNames({ XMM: "MM 2024" });
}); });
@@ -74,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(c.actions?.[0].text).toContain("Melee Attack Roll:"); expect(flatText(c.actions?.[0])).toContain("Melee Attack Roll:");
expect(c.actions?.[0].text).not.toContain("{@"); expect(flatText(c.actions?.[0])).not.toContain("{@");
expect(c.bonusActions).toHaveLength(1); expect(c.bonusActions).toHaveLength(1);
expect(c.bonusActions?.[0].text).toContain("Disengage"); expect(flatText(c.bonusActions?.[0])).toContain("Disengage");
expect(c.bonusActions?.[0].text).not.toContain("{@"); expect(flatText(c.bonusActions?.[0])).not.toContain("{@");
}); });
it("normalizes a creature with legendary actions", () => { it("normalizes a creature with legendary actions", () => {
@@ -333,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(bite?.text).toContain("Melee Weapon Attack:"); expect(flatText(bite)).toContain("Melee Weapon Attack:");
expect(bite?.text).not.toContain("mw"); expect(flatText(bite)).not.toContain("mw");
expect(bite?.text).not.toContain("{@"); expect(flatText(bite)).not.toContain("{@");
}); });
it("handles fly speed with hover condition", () => { it("handles fly speed with hover condition", () => {
@@ -368,4 +383,131 @@ describe("normalizeBestiary", () => {
const creatures = normalizeBestiary(raw); const creatures = normalizeBestiary(raw);
expect(creatures[0].speed).toBe("10 ft., fly 90 ft. (hover)"); expect(creatures[0].speed).toBe("10 ft., fly 90 ft. (hover)");
}); });
it("renders list items with singular entry field (e.g. Confusing Burble d4 effects)", () => {
const raw = {
monster: [
{
name: "Jabberwock",
source: "WBtW",
size: ["H"],
type: "dragon",
ac: [18],
hp: { average: 115, formula: "10d12 + 50" },
speed: { walk: 30 },
str: 22,
dex: 15,
con: 20,
int: 8,
wis: 14,
cha: 16,
passive: 12,
cr: "13",
trait: [
{
name: "Confusing Burble",
entries: [
"The jabberwock burbles unless {@condition incapacitated}. Roll a {@dice d4}:",
{
type: "list",
style: "list-hang-notitle",
items: [
{
type: "item",
name: "1-2",
entry: "The creature does nothing.",
},
{
type: "item",
name: "3",
entry:
"The creature uses all its movement to move in a random direction.",
},
{
type: "item",
name: "4",
entry:
"The creature makes one melee attack against a random creature.",
},
],
},
],
},
],
},
],
};
const creatures = normalizeBestiary(raw);
const trait = creatures[0].traits?.[0];
expect(trait).toBeDefined();
expect(trait?.name).toBe("Confusing Burble");
expect(trait?.segments).toHaveLength(2);
expect(trait?.segments[0]).toEqual({
type: "text",
value: expect.stringContaining("d4"),
});
expect(trait?.segments[1]).toEqual({
type: "list",
items: [
{ label: "1-2", text: "The creature does nothing." },
{
label: "3",
text: expect.stringContaining("random direction"),
},
{ label: "4", text: expect.stringContaining("melee attack") },
],
});
});
it("renders table entries as structured list segments", () => {
const raw = {
monster: [
{
name: "Test Creature",
source: "XMM",
size: ["M"],
type: "humanoid",
ac: [12],
hp: { average: 40, formula: "9d8" },
speed: { walk: 30 },
str: 10,
dex: 10,
con: 10,
int: 10,
wis: 10,
cha: 10,
passive: 10,
cr: "1",
trait: [
{
name: "Random Effect",
entries: [
"Roll on the table:",
{
type: "table",
colLabels: ["d4", "Effect"],
rows: [
["1", "Nothing happens."],
["2", "Something happens."],
],
},
],
},
],
},
],
};
const creatures = normalizeBestiary(raw);
const trait = creatures[0].traits?.[0];
expect(trait).toBeDefined();
expect(trait?.segments[1]).toEqual({
type: "list",
items: [
{ label: "1", text: "Nothing happens." },
{ label: "2", text: "Something happens." },
],
});
});
}); });

View File

@@ -0,0 +1,95 @@
import type { Creature } from "@initiative/domain";
import { creatureId } from "@initiative/domain";
import { beforeEach, describe, expect, it, vi } from "vitest";
// Mock idb to reject — simulates IndexedDB unavailable.
// This must be a separate file from bestiary-cache.test.ts because the
// module caches the db connection in a singleton; once openDB succeeds
// in one test, the fallback path is unreachable.
vi.mock("idb", () => ({
openDB: vi.fn().mockRejectedValue(new Error("IndexedDB unavailable")),
}));
const {
cacheSource,
isSourceCached,
getCachedSources,
clearSource,
clearAll,
loadAllCachedCreatures,
} = await import("../bestiary-cache.js");
function makeCreature(id: string, name: string): Creature {
return {
id: creatureId(id),
name,
source: "MM",
sourceDisplayName: "Monster Manual",
size: "Small",
type: "humanoid",
alignment: "neutral evil",
ac: 15,
hp: { average: 7, formula: "2d6" },
speed: "30 ft.",
abilities: { str: 8, dex: 14, con: 10, int: 10, wis: 8, cha: 8 },
cr: "1/4",
initiativeProficiency: 0,
proficiencyBonus: 2,
passive: 9,
};
}
describe("bestiary-cache fallback (IndexedDB unavailable)", () => {
beforeEach(async () => {
await clearAll();
});
it("cacheSource falls back to in-memory store", async () => {
const creatures = [makeCreature("mm:goblin", "Goblin")];
await cacheSource("dnd", "MM", "Monster Manual", creatures);
expect(await isSourceCached("dnd", "MM")).toBe(true);
});
it("isSourceCached returns false for uncached source", async () => {
expect(await isSourceCached("dnd", "XGE")).toBe(false);
});
it("getCachedSources returns sources from in-memory store", async () => {
await cacheSource("dnd", "MM", "Monster Manual", [
makeCreature("mm:goblin", "Goblin"),
]);
const sources = await getCachedSources();
expect(sources).toHaveLength(1);
expect(sources[0].sourceCode).toBe("MM");
expect(sources[0].creatureCount).toBe(1);
});
it("loadAllCachedCreatures assembles creatures from in-memory store", async () => {
const goblin = makeCreature("mm:goblin", "Goblin");
await cacheSource("dnd", "MM", "Monster Manual", [goblin]);
const map = await loadAllCachedCreatures();
expect(map.size).toBe(1);
expect(map.get(creatureId("mm:goblin"))?.name).toBe("Goblin");
});
it("clearSource removes a single source from in-memory store", async () => {
await cacheSource("dnd", "MM", "Monster Manual", []);
await cacheSource("dnd", "VGM", "Volo's Guide", []);
await clearSource("dnd", "MM");
expect(await isSourceCached("dnd", "MM")).toBe(false);
expect(await isSourceCached("dnd", "VGM")).toBe(true);
});
it("clearAll removes all data from in-memory store", async () => {
await cacheSource("dnd", "MM", "Monster Manual", []);
await clearAll();
const sources = await getCachedSources();
expect(sources).toEqual([]);
});
});

View File

@@ -0,0 +1,174 @@
import type { Creature } from "@initiative/domain";
import { creatureId } from "@initiative/domain";
import { beforeEach, describe, expect, it, vi } from "vitest";
// Mock idb — the one legitimate use of vi.mock for a third-party I/O library.
// We can't use real IndexedDB in jsdom; this tests the cache logic through
// all public API methods with an in-memory backing store.
const fakeStore = new Map<string, unknown>();
vi.mock("idb", () => ({
openDB: vi.fn().mockResolvedValue({
put: vi.fn((_storeName: string, value: unknown) => {
const record = value as { sourceCode: string };
fakeStore.set(record.sourceCode, value);
return Promise.resolve();
}),
get: vi.fn((_storeName: string, key: string) =>
Promise.resolve(fakeStore.get(key)),
),
getAll: vi.fn((_storeName: string) =>
Promise.resolve([...fakeStore.values()]),
),
delete: vi.fn((_storeName: string, key: string) => {
fakeStore.delete(key);
return Promise.resolve();
}),
clear: vi.fn((_storeName: string) => {
fakeStore.clear();
return Promise.resolve();
}),
}),
}));
// Import after mocking
const {
cacheSource,
isSourceCached,
getCachedSources,
clearSource,
clearAll,
loadAllCachedCreatures,
} = await import("../bestiary-cache.js");
function makeCreature(id: string, name: string): Creature {
return {
id: creatureId(id),
name,
source: "MM",
sourceDisplayName: "Monster Manual",
size: "Small",
type: "humanoid",
alignment: "neutral evil",
ac: 15,
hp: { average: 7, formula: "2d6" },
speed: "30 ft.",
abilities: { str: 8, dex: 14, con: 10, int: 10, wis: 8, cha: 8 },
cr: "1/4",
initiativeProficiency: 0,
proficiencyBonus: 2,
passive: 9,
};
}
describe("bestiary-cache", () => {
beforeEach(() => {
fakeStore.clear();
});
describe("cacheSource", () => {
it("stores creatures and metadata", async () => {
const creatures = [makeCreature("mm:goblin", "Goblin")];
await cacheSource("dnd", "MM", "Monster Manual", creatures);
expect(fakeStore.has("dnd:MM")).toBe(true);
const record = fakeStore.get("dnd:MM") as {
sourceCode: string;
displayName: string;
creatures: Creature[];
creatureCount: number;
cachedAt: number;
};
expect(record.sourceCode).toBe("dnd:MM");
expect(record.displayName).toBe("Monster Manual");
expect(record.creatures).toHaveLength(1);
expect(record.creatureCount).toBe(1);
expect(record.cachedAt).toBeGreaterThan(0);
});
});
describe("isSourceCached", () => {
it("returns false for uncached source", async () => {
expect(await isSourceCached("dnd", "XGE")).toBe(false);
});
it("returns true after caching", async () => {
await cacheSource("dnd", "MM", "Monster Manual", []);
expect(await isSourceCached("dnd", "MM")).toBe(true);
});
});
describe("getCachedSources", () => {
it("returns empty array when no sources cached", async () => {
const sources = await getCachedSources();
expect(sources).toEqual([]);
});
it("returns source info with creature counts", async () => {
await cacheSource("dnd", "MM", "Monster Manual", [
makeCreature("mm:goblin", "Goblin"),
makeCreature("mm:orc", "Orc"),
]);
await cacheSource("dnd", "VGM", "Volo's Guide", [
makeCreature("vgm:flind", "Flind"),
]);
const sources = await getCachedSources();
expect(sources).toHaveLength(2);
const mm = sources.find((s) => s.sourceCode === "MM");
expect(mm).toBeDefined();
expect(mm?.displayName).toBe("Monster Manual");
expect(mm?.creatureCount).toBe(2);
const vgm = sources.find((s) => s.sourceCode === "VGM");
expect(vgm?.creatureCount).toBe(1);
});
});
describe("loadAllCachedCreatures", () => {
it("returns empty map when nothing cached", async () => {
const map = await loadAllCachedCreatures();
expect(map.size).toBe(0);
});
it("assembles creatures from all cached sources", async () => {
const goblin = makeCreature("mm:goblin", "Goblin");
const orc = makeCreature("mm:orc", "Orc");
const flind = makeCreature("vgm:flind", "Flind");
await cacheSource("dnd", "MM", "Monster Manual", [goblin, orc]);
await cacheSource("dnd", "VGM", "Volo's Guide", [flind]);
const map = await loadAllCachedCreatures();
expect(map.size).toBe(3);
expect(map.get(creatureId("mm:goblin"))?.name).toBe("Goblin");
expect(map.get(creatureId("mm:orc"))?.name).toBe("Orc");
expect(map.get(creatureId("vgm:flind"))?.name).toBe("Flind");
});
});
describe("clearSource", () => {
it("removes a single source", async () => {
await cacheSource("dnd", "MM", "Monster Manual", []);
await cacheSource("dnd", "VGM", "Volo's Guide", []);
await clearSource("dnd", "MM");
expect(await isSourceCached("dnd", "MM")).toBe(false);
expect(await isSourceCached("dnd", "VGM")).toBe(true);
});
});
describe("clearAll", () => {
it("removes all cached data", async () => {
await cacheSource("dnd", "MM", "Monster Manual", []);
await cacheSource("dnd", "VGM", "Volo's Guide", []);
await clearAll();
const sources = await getCachedSources();
expect(sources).toEqual([]);
});
});
});

View File

@@ -0,0 +1,107 @@
import { describe, expect, it } from "vitest";
import {
getAllSourceCodes,
getDefaultFetchUrl,
getSourceDisplayName,
loadBestiaryIndex,
} from "../bestiary-index-adapter.js";
describe("loadBestiaryIndex", () => {
it("returns an object with sources and creatures", () => {
const index = loadBestiaryIndex();
expect(index.sources).toBeDefined();
expect(index.creatures).toBeDefined();
expect(Array.isArray(index.creatures)).toBe(true);
});
it("creatures have the expected shape", () => {
const index = loadBestiaryIndex();
expect(index.creatures.length).toBeGreaterThan(0);
const first = index.creatures[0];
expect(first).toHaveProperty("name");
expect(first).toHaveProperty("source");
expect(first).toHaveProperty("ac");
expect(first).toHaveProperty("hp");
expect(first).toHaveProperty("dex");
expect(first).toHaveProperty("cr");
expect(first).toHaveProperty("initiativeProficiency");
expect(first).toHaveProperty("size");
expect(first).toHaveProperty("type");
});
it("returns the same cached instance on subsequent calls", () => {
const a = loadBestiaryIndex();
const b = loadBestiaryIndex();
expect(a).toBe(b);
});
it("sources is a record of source code to display name", () => {
const index = loadBestiaryIndex();
const entries = Object.entries(index.sources);
expect(entries.length).toBeGreaterThan(0);
for (const [code, name] of entries) {
expect(typeof code).toBe("string");
expect(typeof name).toBe("string");
expect(code.length).toBeGreaterThan(0);
expect(name.length).toBeGreaterThan(0);
}
});
});
describe("getAllSourceCodes", () => {
it("returns all keys from the index sources", () => {
const codes = getAllSourceCodes();
const index = loadBestiaryIndex();
expect(codes).toEqual(Object.keys(index.sources));
});
it("returns only strings", () => {
for (const code of getAllSourceCodes()) {
expect(typeof code).toBe("string");
}
});
});
describe("getDefaultFetchUrl", () => {
it("returns default GitHub URL when no baseUrl provided", () => {
const url = getDefaultFetchUrl("MM");
expect(url).toBe(
"https://raw.githubusercontent.com/5etools-mirror-3/5etools-src/main/data/bestiary/bestiary-mm.json",
);
});
it("constructs URL from baseUrl with trailing slash", () => {
const url = getDefaultFetchUrl("PHB", "https://example.com/data/");
expect(url).toBe("https://example.com/data/bestiary-phb.json");
});
it("normalizes baseUrl without trailing slash", () => {
const url = getDefaultFetchUrl("PHB", "https://example.com/data");
expect(url).toBe("https://example.com/data/bestiary-phb.json");
});
it("lowercases the source code in the filename", () => {
const url = getDefaultFetchUrl("XMM");
expect(url).toContain("bestiary-xmm.json");
});
it("applies filename override for Plane Shift sources", () => {
expect(getDefaultFetchUrl("PSA")).toContain("bestiary-ps-a.json");
expect(getDefaultFetchUrl("PSD")).toContain("bestiary-ps-d.json");
expect(getDefaultFetchUrl("PSK")).toContain("bestiary-ps-k.json");
});
});
describe("getSourceDisplayName", () => {
it("returns display name for a known source", () => {
const index = loadBestiaryIndex();
const [code, expectedName] = Object.entries(index.sources)[0];
expect(getSourceDisplayName(code)).toBe(expectedName);
});
it("falls back to source code for unknown source", () => {
expect(getSourceDisplayName("UNKNOWN_SOURCE_XYZ")).toBe(
"UNKNOWN_SOURCE_XYZ",
);
});
});

View File

@@ -0,0 +1,645 @@
import { describe, expect, it } from "vitest";
import { normalizeFoundryCreature } from "../pf2e-bestiary-adapter.js";
function minimalCreature(overrides?: Record<string, unknown>) {
return {
_id: "test-id",
name: "Test Creature",
type: "npc",
system: {
abilities: {
str: { mod: 3 },
dex: { mod: 2 },
con: { mod: 1 },
int: { mod: 0 },
wis: { mod: -1 },
cha: { mod: -2 },
},
attributes: {
ac: { value: 18 },
hp: { max: 45 },
speed: { value: 25 },
},
details: {
level: { value: 3 },
languages: { value: ["common"] },
publication: {
license: "ORC",
remaster: true,
title: "Test Source",
},
},
perception: { mod: 8 },
saves: {
fortitude: { value: 10 },
reflex: { value: 8 },
will: { value: 6 },
},
skills: {},
traits: { rarity: "common", size: { value: "med" }, value: [] },
},
items: [],
...overrides,
};
}
describe("normalizeFoundryCreature", () => {
describe("basic fields", () => {
it("maps top-level fields correctly", () => {
const creature = normalizeFoundryCreature(minimalCreature());
expect(creature.system).toBe("pf2e");
expect(creature.name).toBe("Test Creature");
expect(creature.level).toBe(3);
expect(creature.ac).toBe(18);
expect(creature.hp).toBe(45);
expect(creature.perception).toBe(8);
expect(creature.saveFort).toBe(10);
expect(creature.saveRef).toBe(8);
expect(creature.saveWill).toBe(6);
});
it("maps ability modifiers", () => {
const creature = normalizeFoundryCreature(minimalCreature());
expect(creature.abilityMods).toEqual({
str: 3,
dex: 2,
con: 1,
int: 0,
wis: -1,
cha: -2,
});
});
it("maps AC conditional from details", () => {
const creature = normalizeFoundryCreature(
minimalCreature({
system: {
...minimalCreature().system,
attributes: {
...minimalCreature().system.attributes,
ac: { value: 20, details: "+2 with shield raised" },
},
},
}),
);
expect(creature.acConditional).toBe("+2 with shield raised");
});
});
describe("senses formatting", () => {
it("formats darkvision", () => {
const creature = normalizeFoundryCreature(
minimalCreature({
system: {
...minimalCreature().system,
perception: {
mod: 8,
senses: [{ type: "darkvision" }],
},
},
}),
);
expect(creature.senses).toBe("Darkvision");
});
it("formats sense with acuity and range", () => {
const creature = normalizeFoundryCreature(
minimalCreature({
system: {
...minimalCreature().system,
perception: {
mod: 8,
senses: [{ type: "tremorsense", acuity: "imprecise", range: 30 }],
},
},
}),
);
expect(creature.senses).toBe("Tremorsense (imprecise) 30 feet");
});
it("omits precise acuity", () => {
const creature = normalizeFoundryCreature(
minimalCreature({
system: {
...minimalCreature().system,
perception: {
mod: 8,
senses: [{ type: "scent", acuity: "precise", range: 60 }],
},
},
}),
);
expect(creature.senses).toBe("Scent 60 feet");
});
});
describe("languages formatting", () => {
it("formats language list", () => {
const creature = normalizeFoundryCreature(
minimalCreature({
system: {
...minimalCreature().system,
details: {
...minimalCreature().system.details,
languages: { value: ["common", "draconic"] },
},
},
}),
);
expect(creature.languages).toBe("Common, Draconic");
});
it("includes details", () => {
const creature = normalizeFoundryCreature(
minimalCreature({
system: {
...minimalCreature().system,
details: {
...minimalCreature().system.details,
languages: {
value: ["common"],
details: "telepathy 100 feet",
},
},
},
}),
);
expect(creature.languages).toBe("Common (telepathy 100 feet)");
});
});
describe("skills formatting", () => {
it("formats and sorts skills", () => {
const creature = normalizeFoundryCreature(
minimalCreature({
system: {
...minimalCreature().system,
skills: {
stealth: { base: 10 },
athletics: { base: 8 },
},
},
}),
);
expect(creature.skills).toBe("Athletics +8, Stealth +10");
});
});
describe("defenses formatting", () => {
it("formats immunities with exceptions", () => {
const creature = normalizeFoundryCreature(
minimalCreature({
system: {
...minimalCreature().system,
attributes: {
...minimalCreature().system.attributes,
immunities: [
{ type: "paralyzed", exceptions: [] },
{
type: "physical",
exceptions: ["adamantine"],
},
],
},
},
}),
);
expect(creature.immunities).toBe(
"Paralyzed, Physical (except Adamantine)",
);
});
it("formats resistances with value", () => {
const creature = normalizeFoundryCreature(
minimalCreature({
system: {
...minimalCreature().system,
attributes: {
...minimalCreature().system.attributes,
resistances: [{ type: "fire", value: 10, exceptions: [] }],
},
},
}),
);
expect(creature.resistances).toBe("Fire 10");
});
it("formats weaknesses", () => {
const creature = normalizeFoundryCreature(
minimalCreature({
system: {
...minimalCreature().system,
attributes: {
...minimalCreature().system.attributes,
weaknesses: [{ type: "cold-iron", value: 5 }],
},
},
}),
);
expect(creature.weaknesses).toBe("Cold iron 5");
});
});
describe("speed formatting", () => {
it("formats base speed", () => {
const creature = normalizeFoundryCreature(minimalCreature());
expect(creature.speed).toBe("25 feet");
});
it("includes other speeds", () => {
const creature = normalizeFoundryCreature(
minimalCreature({
system: {
...minimalCreature().system,
attributes: {
...minimalCreature().system.attributes,
speed: {
value: 40,
otherSpeeds: [
{ type: "fly", value: 120 },
{ type: "swim", value: 40 },
],
},
},
},
}),
);
expect(creature.speed).toBe("40 feet, Fly 120 feet, Swim 40 feet");
});
it("includes speed details", () => {
const creature = normalizeFoundryCreature(
minimalCreature({
system: {
...minimalCreature().system,
attributes: {
...minimalCreature().system.attributes,
speed: {
value: 25,
details: "ignores difficult terrain",
},
},
},
}),
);
expect(creature.speed).toBe("25 feet (ignores difficult terrain)");
});
});
describe("attack normalization", () => {
it("normalizes melee attacks with traits and damage", () => {
const creature = normalizeFoundryCreature(
minimalCreature({
items: [
{
_id: "atk1",
name: "dogslicer",
type: "melee",
system: {
bonus: { value: 7 },
damageRolls: {
abc: {
damage: "1d6",
damageType: "slashing",
},
},
traits: {
value: ["agile", "backstabber", "finesse"],
},
},
},
],
}),
);
const attack = creature.attacks?.[0];
expect(attack).toBeDefined();
expect(attack?.name).toBe("Dogslicer");
expect(attack?.activity).toEqual({ number: 1, unit: "action" });
expect(attack?.segments[0]).toEqual({
type: "text",
value: "+7 (agile, backstabber, finesse), 1d6 slashing",
});
});
it("expands slugified trait names", () => {
const creature = normalizeFoundryCreature(
minimalCreature({
items: [
{
_id: "atk1",
name: "claw",
type: "melee",
system: {
bonus: { value: 18 },
damageRolls: {
abc: {
damage: "2d8+6",
damageType: "slashing",
},
},
traits: {
value: ["reach-10", "deadly-d10", "versatile-p"],
},
},
},
],
}),
);
const attack = creature.attacks?.[0];
expect(attack?.segments[0]).toEqual({
type: "text",
value: "+18 (reach 10 feet, deadly d10, versatile P), 2d8+6 slashing",
});
});
it("handles multiple damage types", () => {
const creature = normalizeFoundryCreature(
minimalCreature({
items: [
{
_id: "atk1",
name: "flaming sword",
type: "melee",
system: {
bonus: { value: 15 },
damageRolls: {
abc: {
damage: "2d8+5",
damageType: "slashing",
},
def: {
damage: "1d6",
damageType: "fire",
},
},
traits: { value: [] },
},
},
],
}),
);
const attack = creature.attacks?.[0];
expect(attack?.segments[0]).toEqual(
expect.objectContaining({
type: "text",
value: "+15, 2d8+5 slashing plus 1d6 fire",
}),
);
});
});
describe("ability normalization", () => {
it("routes abilities by category", () => {
const creature = normalizeFoundryCreature(
minimalCreature({
items: [
{
_id: "a1",
name: "Sense Motive",
type: "action",
system: {
category: "interaction",
actionType: { value: "passive" },
actions: { value: null },
traits: { value: [] },
description: { value: "<p>Can sense lies.</p>" },
},
},
{
_id: "a2",
name: "Shield Block",
type: "action",
system: {
category: "defensive",
actionType: { value: "reaction" },
actions: { value: null },
traits: { value: [] },
description: {
value: "<p>Blocks with shield.</p>",
},
},
},
{
_id: "a3",
name: "Breath Weapon",
type: "action",
system: {
category: "offensive",
actionType: { value: "action" },
actions: { value: 2 },
traits: { value: ["arcane", "fire"] },
description: {
value:
"<p>@Damage[8d6[fire]] in a @Template[cone|distance:40].</p>",
},
},
},
],
}),
);
expect(creature.abilitiesTop).toHaveLength(1);
expect(creature.abilitiesTop?.[0]?.name).toBe("Sense Motive");
expect(creature.abilitiesTop?.[0]?.activity).toBeUndefined();
expect(creature.abilitiesMid).toHaveLength(1);
expect(creature.abilitiesMid?.[0]?.name).toBe("Shield Block");
expect(creature.abilitiesMid?.[0]?.activity).toEqual({
number: 1,
unit: "reaction",
});
expect(creature.abilitiesBot).toHaveLength(1);
expect(creature.abilitiesBot?.[0]?.name).toBe("Breath Weapon");
expect(creature.abilitiesBot?.[0]?.activity).toEqual({
number: 2,
unit: "action",
});
});
it("strips Foundry enrichment tags from descriptions", () => {
const creature = normalizeFoundryCreature(
minimalCreature({
items: [
{
_id: "a1",
name: "Flame Burst",
type: "action",
system: {
category: "offensive",
actionType: { value: "action" },
actions: { value: 2 },
traits: { value: [] },
description: {
value:
"<p>Deal @Damage[3d6[fire]] damage, @Check[reflex|dc:20|basic] save.</p>",
},
},
},
],
}),
);
expect(
creature.abilitiesBot?.[0]?.segments[0]?.type === "text"
? creature.abilitiesBot[0].segments[0].value
: undefined,
).toBe("Deal 3d6 fire damage, DC 20 basic Reflex save.");
});
it("parses free action activity", () => {
const creature = normalizeFoundryCreature(
minimalCreature({
items: [
{
_id: "a1",
name: "Quick Draw",
type: "action",
system: {
category: "offensive",
actionType: { value: "free" },
actions: { value: null },
traits: { value: [] },
description: { value: "" },
},
},
],
}),
);
expect(creature.abilitiesBot?.[0]?.activity).toEqual({
number: 1,
unit: "free",
});
});
it("includes traits in ability text", () => {
const creature = normalizeFoundryCreature(
minimalCreature({
items: [
{
_id: "a1",
name: "Change Shape",
type: "action",
system: {
category: "offensive",
actionType: { value: "action" },
actions: { value: 1 },
traits: {
value: ["concentrate", "polymorph"],
},
description: {
value: "<p>Takes a new form.</p>",
},
},
},
],
}),
);
expect(
creature.abilitiesBot?.[0]?.segments[0]?.type === "text"
? creature.abilitiesBot[0].segments[0].value
: undefined,
).toBe("(Concentrate, Polymorph) Takes a new form.");
});
});
describe("spellcasting normalization", () => {
it("normalizes prepared spells by rank", () => {
const creature = normalizeFoundryCreature(
minimalCreature({
items: [
{
_id: "entry1",
name: "Primal Prepared Spells",
type: "spellcastingEntry",
system: {
tradition: { value: "primal" },
prepared: { value: "prepared" },
spelldc: { dc: 30, value: 22 },
},
},
{
_id: "s1",
name: "Earthquake",
type: "spell",
system: {
location: { value: "entry1" },
level: { value: 6 },
traits: { value: [] },
},
},
{
_id: "s2",
name: "Heal",
type: "spell",
system: {
location: { value: "entry1" },
level: { value: 3 },
traits: { value: [] },
},
},
{
_id: "s3",
name: "Detect Magic",
type: "spell",
system: {
location: { value: "entry1" },
level: { value: 1 },
traits: { value: ["cantrip"] },
},
},
],
}),
);
expect(creature.spellcasting).toHaveLength(1);
const sc = creature.spellcasting?.[0];
expect(sc?.name).toBe("Primal Prepared Spells");
expect(sc?.headerText).toBe("DC 30, attack +22");
expect(sc?.daily).toEqual([
{ uses: 6, each: true, spells: ["Earthquake"] },
{ uses: 3, each: true, spells: ["Heal"] },
]);
expect(sc?.atWill).toEqual(["Detect Magic"]);
});
it("normalizes innate spells with uses", () => {
const creature = normalizeFoundryCreature(
minimalCreature({
items: [
{
_id: "entry1",
name: "Divine Innate Spells",
type: "spellcastingEntry",
system: {
tradition: { value: "divine" },
prepared: { value: "innate" },
spelldc: { dc: 32 },
},
},
{
_id: "s1",
name: "Sure Strike",
type: "spell",
system: {
location: {
value: "entry1",
heightenedLevel: 1,
uses: { max: 3, value: 3 },
},
level: { value: 1 },
traits: { value: [] },
},
},
],
}),
);
const sc = creature.spellcasting?.[0];
expect(sc?.headerText).toBe("DC 32");
expect(sc?.daily).toEqual([
{
uses: 1,
each: true,
spells: ["Sure Strike (\u00d73)"],
},
]);
});
});
});

View File

@@ -0,0 +1,103 @@
import { describe, expect, it } from "vitest";
const PACK_DIR_PREFIX = /^pathfinder-monster-core\//;
const JSON_EXTENSION = /\.json$/;
import {
getAllPf2eSourceCodes,
getCreaturePathsForSource,
getDefaultPf2eFetchUrl,
getPf2eSourceDisplayName,
loadPf2eBestiaryIndex,
} from "../pf2e-bestiary-index-adapter.js";
describe("loadPf2eBestiaryIndex", () => {
it("returns an object with sources and creatures", () => {
const index = loadPf2eBestiaryIndex();
expect(index.sources).toBeDefined();
expect(index.creatures).toBeDefined();
expect(Array.isArray(index.creatures)).toBe(true);
});
it("creatures have the expected PF2e shape", () => {
const index = loadPf2eBestiaryIndex();
expect(index.creatures.length).toBeGreaterThan(0);
const first = index.creatures[0];
expect(first).toHaveProperty("name");
expect(first).toHaveProperty("source");
expect(first).toHaveProperty("level");
expect(first).toHaveProperty("ac");
expect(first).toHaveProperty("hp");
expect(first).toHaveProperty("perception");
expect(first).toHaveProperty("size");
expect(first).toHaveProperty("type");
});
it("contains a substantial number of creatures", () => {
const index = loadPf2eBestiaryIndex();
expect(index.creatures.length).toBeGreaterThan(2500);
});
it("creatures have size and type populated", () => {
const index = loadPf2eBestiaryIndex();
const withSize = index.creatures.filter((c) => c.size !== "");
const withType = index.creatures.filter((c) => c.type !== "");
expect(withSize.length).toBeGreaterThan(index.creatures.length * 0.9);
expect(withType.length).toBeGreaterThan(index.creatures.length * 0.8);
});
it("returns the same cached instance on subsequent calls", () => {
const a = loadPf2eBestiaryIndex();
const b = loadPf2eBestiaryIndex();
expect(a).toBe(b);
});
});
describe("getAllPf2eSourceCodes", () => {
it("returns all keys from the index sources", () => {
const codes = getAllPf2eSourceCodes();
const index = loadPf2eBestiaryIndex();
expect(codes).toEqual(Object.keys(index.sources));
});
});
describe("getDefaultPf2eFetchUrl", () => {
it("returns Foundry VTT PF2e base URL", () => {
const url = getDefaultPf2eFetchUrl("pathfinder-monster-core");
expect(url).toBe(
"https://raw.githubusercontent.com/foundryvtt/pf2e/v13-dev/packs/pf2e/",
);
});
it("normalizes custom base URL with trailing slash", () => {
const url = getDefaultPf2eFetchUrl(
"pathfinder-monster-core",
"https://example.com/pf2e",
);
expect(url).toBe("https://example.com/pf2e/");
});
});
describe("getPf2eSourceDisplayName", () => {
it("returns display name for a known source", () => {
const name = getPf2eSourceDisplayName("pathfinder-monster-core");
expect(name).toBe("Monster Core");
});
it("falls back to source code for unknown source", () => {
expect(getPf2eSourceDisplayName("UNKNOWN")).toBe("UNKNOWN");
});
});
describe("getCreaturePathsForSource", () => {
it("returns file paths for a known source", () => {
const paths = getCreaturePathsForSource("pathfinder-monster-core");
expect(paths.length).toBeGreaterThan(100);
expect(paths[0]).toMatch(PACK_DIR_PREFIX);
expect(paths[0]).toMatch(JSON_EXTENSION);
});
it("returns empty array for unknown source", () => {
expect(getCreaturePathsForSource("nonexistent")).toEqual([]);
});
});

View File

@@ -0,0 +1,162 @@
import { describe, expect, it } from "vitest";
import { stripFoundryTags } from "../strip-foundry-tags.js";
describe("stripFoundryTags", () => {
describe("@Damage tags", () => {
it("formats damage with type bracket", () => {
expect(stripFoundryTags("@Damage[3d6+10[fire]]")).toBe("3d6+10 fire");
});
it("prefers display text when present", () => {
expect(
stripFoundryTags("@Damage[3d6+10[fire]]{3d6+10 fire damage}"),
).toBe("3d6+10 fire damage");
});
it("handles multiple damage types", () => {
expect(
stripFoundryTags("@Damage[2d8+5[slashing]] plus @Damage[1d6[fire]]"),
).toBe("2d8+5 slashing plus 1d6 fire");
});
});
describe("@Check tags", () => {
it("formats basic saving throw", () => {
expect(stripFoundryTags("@Check[reflex|dc:33|basic]")).toBe(
"DC 33 basic Reflex",
);
});
it("formats non-basic check", () => {
expect(stripFoundryTags("@Check[athletics|dc:25]")).toBe(
"DC 25 Athletics",
);
});
it("formats check without DC", () => {
expect(stripFoundryTags("@Check[fortitude]")).toBe("Fortitude");
});
});
describe("@UUID tags", () => {
it("extracts display text", () => {
expect(
stripFoundryTags(
"@UUID[Compendium.pf2e.conditionitems.Item.Grabbed]{Grabbed}",
),
).toBe("Grabbed");
});
it("extracts last segment when no display text", () => {
expect(
stripFoundryTags("@UUID[Compendium.pf2e.conditionitems.Item.Grabbed]"),
).toBe("Grabbed");
});
});
describe("@Template tags", () => {
it("formats cone template", () => {
expect(stripFoundryTags("@Template[cone|distance:40]")).toBe(
"40-foot cone",
);
});
it("formats emanation template", () => {
expect(stripFoundryTags("@Template[emanation|distance:10]")).toBe(
"10-foot emanation",
);
});
it("prefers display text", () => {
expect(
stripFoundryTags("@Template[cone|distance:40]{40-foot cone}"),
).toBe("40-foot cone");
});
});
describe("unknown @Tag patterns", () => {
it("uses display text for unknown tags", () => {
expect(stripFoundryTags("@Localize[some.key]{Some Text}")).toBe(
"Some Text",
);
});
it("strips unknown tags without display text", () => {
expect(stripFoundryTags("@Localize[some.key]")).toBe("");
});
});
describe("HTML stripping", () => {
it("strips paragraph tags", () => {
expect(stripFoundryTags("<p>text</p>")).toBe("text");
});
it("converts br to newline", () => {
expect(stripFoundryTags("line1<br />line2")).toBe("line1\nline2");
});
it("converts hr to newline", () => {
expect(stripFoundryTags("before<hr />after")).toBe("before\nafter");
});
it("strips strong and em tags", () => {
expect(stripFoundryTags("<strong>bold</strong> <em>italic</em>")).toBe(
"bold italic",
);
});
it("converts p-to-p transitions to newlines", () => {
expect(stripFoundryTags("<p>first</p><p>second</p>")).toBe(
"first\nsecond",
);
});
it("strips action-glyph spans", () => {
expect(
stripFoundryTags('<span class="action-glyph">1</span> Strike'),
).toBe("Strike");
});
});
describe("HTML entities", () => {
it("decodes &amp;", () => {
expect(stripFoundryTags("fire &amp; ice")).toBe("fire & ice");
});
it("decodes &lt; and &gt;", () => {
expect(stripFoundryTags("&lt;tag&gt;")).toBe("<tag>");
});
it("decodes &quot;", () => {
expect(stripFoundryTags("&quot;hello&quot;")).toBe('"hello"');
});
});
describe("whitespace handling", () => {
it("collapses multiple spaces", () => {
expect(stripFoundryTags("a b c")).toBe("a b c");
});
it("collapses multiple blank lines", () => {
expect(stripFoundryTags("a\n\n\nb")).toBe("a\nb");
});
it("trims leading and trailing whitespace", () => {
expect(stripFoundryTags(" hello ")).toBe("hello");
});
});
describe("combined/edge cases", () => {
it("handles enrichment tags inside HTML", () => {
expect(
stripFoundryTags(
"<p>Deal @Damage[2d6[fire]] damage, @Check[reflex|dc:20|basic] save.</p>",
),
).toBe("Deal 2d6 fire damage, DC 20 basic Reflex save.");
});
it("handles empty string", () => {
expect(stripFoundryTags("")).toBe("");
});
});
});

View File

@@ -138,12 +138,20 @@ describe("stripTags", () => {
); );
}); });
it("handles nested tags gracefully", () => { it("handles sibling tags in the same string", () => {
expect( expect(
stripTags("The spell {@spell Fireball|XPHB} deals {@damage 8d6}."), stripTags("The spell {@spell Fireball|XPHB} deals {@damage 8d6}."),
).toBe("The spell Fireball deals 8d6."); ).toBe("The spell Fireball deals 8d6.");
}); });
it("handles nested tags (outer wrapping inner)", () => {
expect(
stripTags(
"{@b Arcane Innate Spells DC 24; 3rd {@spell fireball}, {@spell slow}}",
),
).toBe("Arcane Innate Spells DC 24; 3rd fireball, slow");
});
it("handles text with no tags", () => { it("handles text with no tags", () => {
expect(stripTags("Just plain text.")).toBe("Just plain text."); expect(stripTags("Just plain text.")).toBe("Just plain text.");
}); });

View File

@@ -5,6 +5,8 @@ import type {
LegendaryBlock, LegendaryBlock,
SpellcastingBlock, SpellcastingBlock,
TraitBlock, TraitBlock,
TraitListItem,
TraitSegment,
} from "@initiative/domain"; } from "@initiative/domain";
import { creatureId, proficiencyBonus } from "@initiative/domain"; import { creatureId, proficiencyBonus } from "@initiative/domain";
import { stripTags } from "./strip-tags.js"; import { stripTags } from "./strip-tags.js";
@@ -63,11 +65,18 @@ interface RawEntryObject {
type: string; type: string;
items?: ( items?: (
| string | string
| { type: string; name?: string; entries?: (string | RawEntryObject)[] } | {
type: string;
name?: string;
entry?: string;
entries?: (string | RawEntryObject)[];
}
)[]; )[];
style?: string; style?: string;
name?: string; name?: string;
entries?: (string | RawEntryObject)[]; entries?: (string | RawEntryObject)[];
colLabels?: string[];
rows?: (string | RawEntryObject)[][];
} }
interface RawSpellcasting { interface RawSpellcasting {
@@ -257,23 +266,34 @@ function formatConditionImmunities(
.join(", "); .join(", ");
} }
function renderListItem(item: string | RawEntryObject): string | undefined { function toListItem(
item:
| string
| {
type: string;
name?: string;
entry?: string;
entries?: (string | RawEntryObject)[];
},
): TraitListItem | undefined {
if (typeof item === "string") { if (typeof item === "string") {
return `${stripTags(item)}`; return { text: stripTags(item) };
} }
if (item.name && item.entries) { if (item.name && item.entries) {
return `${stripTags(item.name)}: ${renderEntries(item.entries)}`; return { label: stripTags(item.name), text: renderEntries(item.entries) };
}
if (item.name && item.entry) {
return { label: stripTags(item.name), text: stripTags(item.entry) };
} }
return undefined; return undefined;
} }
function renderEntryObject(entry: RawEntryObject, parts: string[]): void { function renderEntryObject(entry: RawEntryObject, parts: string[]): void {
if (entry.type === "list") { if (entry.type === "list" || entry.type === "table") {
for (const item of entry.items ?? []) { // Handled structurally in segmentizeEntries
const rendered = renderListItem(item); return;
if (rendered) parts.push(rendered);
} }
} else if (entry.type === "item" && entry.name && entry.entries) { if (entry.type === "item" && entry.name && entry.entries) {
parts.push(`${stripTags(entry.name)}: ${renderEntries(entry.entries)}`); parts.push(`${stripTags(entry.name)}: ${renderEntries(entry.entries)}`);
} else if (entry.entries) { } else if (entry.entries) {
parts.push(renderEntries(entry.entries)); parts.push(renderEntries(entry.entries));
@@ -292,11 +312,67 @@ function renderEntries(entries: (string | RawEntryObject)[]): string {
return parts.join(" "); return parts.join(" ");
} }
function tableRowToListItem(row: (string | RawEntryObject)[]): TraitListItem {
return {
label: typeof row[0] === "string" ? stripTags(row[0]) : undefined,
text: row
.slice(1)
.map((cell) =>
typeof cell === "string" ? stripTags(cell) : renderEntries([cell]),
)
.join(" "),
};
}
function entryToListSegment(entry: RawEntryObject): TraitSegment | undefined {
if (entry.type === "list") {
const items = (entry.items ?? [])
.map(toListItem)
.filter((i): i is TraitListItem => i !== undefined);
return items.length > 0 ? { type: "list", items } : undefined;
}
if (entry.type === "table" && entry.rows) {
const items = entry.rows.map(tableRowToListItem);
return items.length > 0 ? { type: "list", items } : undefined;
}
return undefined;
}
function segmentizeEntries(
entries: (string | RawEntryObject)[],
): TraitSegment[] {
const segments: TraitSegment[] = [];
const textParts: string[] = [];
const flushText = () => {
if (textParts.length > 0) {
segments.push({ type: "text", value: textParts.join(" ") });
textParts.length = 0;
}
};
for (const entry of entries) {
if (typeof entry === "string") {
textParts.push(stripTags(entry));
continue;
}
const listSeg = entryToListSegment(entry);
if (listSeg) {
flushText();
segments.push(listSeg);
} else {
renderEntryObject(entry, textParts);
}
}
flushText();
return segments;
}
function normalizeTraits(raw?: RawEntry[]): TraitBlock[] | undefined { function normalizeTraits(raw?: RawEntry[]): TraitBlock[] | undefined {
if (!raw || raw.length === 0) return undefined; if (!raw || raw.length === 0) return undefined;
return raw.map((t) => ({ return raw.map((t) => ({
name: stripTags(t.name), name: stripTags(t.name),
text: renderEntries(t.entries), segments: segmentizeEntries(t.entries),
})); }));
} }
@@ -361,7 +437,7 @@ function normalizeLegendary(
preamble, preamble,
entries: raw.map((e) => ({ entries: raw.map((e) => ({
name: stripTags(e.name), name: stripTags(e.name),
text: renderEntries(e.entries), segments: segmentizeEntries(e.entries),
})), })),
}; };
} }

View File

@@ -1,23 +1,25 @@
import type { Creature, CreatureId } from "@initiative/domain"; import type { AnyCreature, CreatureId } from "@initiative/domain";
import { type IDBPDatabase, openDB } from "idb"; import { type IDBPDatabase, openDB } from "idb";
const DB_NAME = "initiative-bestiary"; const DB_NAME = "initiative-bestiary";
const STORE_NAME = "sources"; const STORE_NAME = "sources";
const DB_VERSION = 2; const DB_VERSION = 5;
export interface CachedSourceInfo { interface CachedSourceInfo {
readonly sourceCode: string; readonly sourceCode: string;
readonly displayName: string; readonly displayName: string;
readonly creatureCount: number; readonly creatureCount: number;
readonly cachedAt: number; readonly cachedAt: number;
readonly system?: string;
} }
interface CachedSourceRecord { interface CachedSourceRecord {
sourceCode: string; sourceCode: string;
displayName: string; displayName: string;
creatures: Creature[]; creatures: AnyCreature[];
cachedAt: number; cachedAt: number;
creatureCount: number; creatureCount: number;
system?: string;
} }
let db: IDBPDatabase | null = null; let db: IDBPDatabase | null = null;
@@ -26,6 +28,10 @@ let dbFailed = false;
// In-memory fallback when IndexedDB is unavailable // In-memory fallback when IndexedDB is unavailable
const memoryStore = new Map<string, CachedSourceRecord>(); const memoryStore = new Map<string, CachedSourceRecord>();
function scopedKey(system: string, sourceCode: string): string {
return `${system}:${sourceCode}`;
}
async function getDb(): Promise<IDBPDatabase | null> { async function getDb(): Promise<IDBPDatabase | null> {
if (db) return db; if (db) return db;
if (dbFailed) return null; if (dbFailed) return null;
@@ -38,8 +44,11 @@ async function getDb(): Promise<IDBPDatabase | null> {
keyPath: "sourceCode", keyPath: "sourceCode",
}); });
} }
if (oldVersion < 2 && database.objectStoreNames.contains(STORE_NAME)) { if (
// Clear cached creatures to pick up improved tag processing oldVersion < DB_VERSION &&
database.objectStoreNames.contains(STORE_NAME)
) {
// Clear cached creatures so they get re-normalized with latest rendering
void transaction.objectStore(STORE_NAME).clear(); void transaction.objectStore(STORE_NAME).clear();
} }
}, },
@@ -55,60 +64,77 @@ async function getDb(): Promise<IDBPDatabase | null> {
} }
export async function cacheSource( export async function cacheSource(
system: string,
sourceCode: string, sourceCode: string,
displayName: string, displayName: string,
creatures: Creature[], creatures: AnyCreature[],
): Promise<void> { ): Promise<void> {
const key = scopedKey(system, sourceCode);
const record: CachedSourceRecord = { const record: CachedSourceRecord = {
sourceCode, sourceCode: key,
displayName, displayName,
creatures, creatures,
cachedAt: Date.now(), cachedAt: Date.now(),
creatureCount: creatures.length, creatureCount: creatures.length,
system,
}; };
const database = await getDb(); const database = await getDb();
if (database) { if (database) {
await database.put(STORE_NAME, record); await database.put(STORE_NAME, record);
} else { } else {
memoryStore.set(sourceCode, record); memoryStore.set(key, record);
} }
} }
export async function isSourceCached(sourceCode: string): Promise<boolean> { export async function isSourceCached(
system: string,
sourceCode: string,
): Promise<boolean> {
const key = scopedKey(system, sourceCode);
const database = await getDb(); const database = await getDb();
if (database) { if (database) {
const record = await database.get(STORE_NAME, sourceCode); const record = await database.get(STORE_NAME, key);
return record !== undefined; return record !== undefined;
} }
return memoryStore.has(sourceCode); return memoryStore.has(key);
} }
export async function getCachedSources(): Promise<CachedSourceInfo[]> { export async function getCachedSources(
system?: string,
): Promise<CachedSourceInfo[]> {
const database = await getDb(); const database = await getDb();
let records: CachedSourceRecord[];
if (database) { if (database) {
const all: CachedSourceRecord[] = await database.getAll(STORE_NAME); records = await database.getAll(STORE_NAME);
return all.map((r) => ({
sourceCode: r.sourceCode,
displayName: r.displayName,
creatureCount: r.creatureCount,
cachedAt: r.cachedAt,
}));
}
return [...memoryStore.values()].map((r) => ({
sourceCode: r.sourceCode,
displayName: r.displayName,
creatureCount: r.creatureCount,
cachedAt: r.cachedAt,
}));
}
export async function clearSource(sourceCode: string): Promise<void> {
const database = await getDb();
if (database) {
await database.delete(STORE_NAME, sourceCode);
} else { } else {
memoryStore.delete(sourceCode); records = [...memoryStore.values()];
}
const filtered = system
? records.filter((r) => r.system === system)
: records;
return filtered.map((r) => ({
sourceCode: r.system
? r.sourceCode.slice(r.system.length + 1)
: r.sourceCode,
displayName: r.displayName,
creatureCount: r.creatureCount,
cachedAt: r.cachedAt,
system: r.system,
}));
}
export async function clearSource(
system: string,
sourceCode: string,
): Promise<void> {
const key = scopedKey(system, sourceCode);
const database = await getDb();
if (database) {
await database.delete(STORE_NAME, key);
} else {
memoryStore.delete(key);
} }
} }
@@ -122,9 +148,9 @@ export async function clearAll(): Promise<void> {
} }
export async function loadAllCachedCreatures(): Promise< export async function loadAllCachedCreatures(): Promise<
Map<CreatureId, Creature> Map<CreatureId, AnyCreature>
> { > {
const map = new Map<CreatureId, Creature>(); const map = new Map<CreatureId, AnyCreature>();
const database = await getDb(); const database = await getDb();
let records: CachedSourceRecord[]; let records: CachedSourceRecord[];

View File

@@ -0,0 +1,531 @@
import type {
CreatureId,
Pf2eCreature,
SpellcastingBlock,
TraitBlock,
} from "@initiative/domain";
import { creatureId } from "@initiative/domain";
import { stripFoundryTags } from "./strip-foundry-tags.js";
// -- Raw Foundry VTT types (minimal, for parsing) --
interface RawFoundryCreature {
_id: string;
name: string;
type: string;
system: {
abilities: Record<string, { mod: number }>;
attributes: {
ac: { value: number; details?: string };
hp: { max: number; details?: string };
speed: {
value: number;
otherSpeeds?: { type: string; value: number }[];
details?: string;
};
immunities?: { type: string; exceptions?: string[] }[];
resistances?: { type: string; value: number; exceptions?: string[] }[];
weaknesses?: { type: string; value: number }[];
allSaves?: { value: string };
};
details: {
level: { value: number };
languages: { value?: string[]; details?: string };
publication: { license: string; remaster: boolean; title: string };
};
perception: {
mod: number;
details?: string;
senses?: { type: string; acuity?: string; range?: number }[];
};
saves: {
fortitude: { value: number; saveDetail?: string };
reflex: { value: number; saveDetail?: string };
will: { value: number; saveDetail?: string };
};
skills: Record<string, { base: number; note?: string }>;
traits: { rarity: string; size: { value: string }; value: string[] };
};
items: RawFoundryItem[];
}
interface RawFoundryItem {
_id: string;
name: string;
type: string;
system: Record<string, unknown>;
sort?: number;
}
interface MeleeSystem {
bonus?: { value: number };
damageRolls?: Record<string, { damage: string; damageType: string }>;
traits?: { value: string[] };
}
interface ActionSystem {
category?: string;
actionType?: { value: string };
actions?: { value: number | null };
traits?: { value: string[] };
description?: { value: string };
}
interface SpellcastingEntrySystem {
tradition?: { value: string };
prepared?: { value: string };
spelldc?: { dc: number; value?: number };
}
interface SpellSystem {
location?: {
value: string;
heightenedLevel?: number;
uses?: { max: number; value: number };
};
level?: { value: number };
traits?: { value: string[] };
}
const SIZE_MAP: Record<string, string> = {
tiny: "tiny",
sm: "small",
med: "medium",
lg: "large",
huge: "huge",
grg: "gargantuan",
};
// -- Helpers --
function capitalize(s: string): string {
return s.charAt(0).toUpperCase() + s.slice(1);
}
function makeCreatureId(source: string, name: string): CreatureId {
const slug = name
.toLowerCase()
.replaceAll(/[^a-z0-9]+/g, "-")
.replaceAll(/(^-|-$)/g, "");
return creatureId(`${source}:${slug}`);
}
const NUMERIC_SLUG = /^(.+)-(\d+)$/;
const LETTER_SLUG = /^(.+)-([a-z])$/;
/** Format rules for traits with a numeric suffix: "reach-10" → "reach 10 feet" */
const NUMERIC_TRAIT_FORMATS: Record<string, (n: string) => string> = {
reach: (n) => `reach ${n} feet`,
range: (n) => `range ${n} feet`,
"range-increment": (n) => `range increment ${n} feet`,
versatile: (n) => `versatile ${n}`,
deadly: (n) => `deadly d${n}`,
fatal: (n) => `fatal d${n}`,
"fatal-aim": (n) => `fatal aim d${n}`,
reload: (n) => `reload ${n}`,
};
/** Format rules for traits with a letter suffix: "versatile-p" → "versatile P" */
const LETTER_TRAIT_FORMATS: Record<string, (l: string) => string> = {
versatile: (l) => `versatile ${l.toUpperCase()}`,
deadly: (l) => `deadly d${l}`,
};
/** Expand slugified trait names: "reach-10" → "reach 10 feet" */
function formatTrait(slug: string): string {
const numMatch = NUMERIC_SLUG.exec(slug);
if (numMatch) {
const [, base, num] = numMatch;
const fmt = NUMERIC_TRAIT_FORMATS[base];
return fmt ? fmt(num) : `${base} ${num}`;
}
const letterMatch = LETTER_SLUG.exec(slug);
if (letterMatch) {
const [, base, letter] = letterMatch;
const fmt = LETTER_TRAIT_FORMATS[base];
if (fmt) return fmt(letter);
}
return slug.replaceAll("-", " ");
}
// -- Formatting --
function formatSenses(
senses: { type: string; acuity?: string; range?: number }[] | undefined,
): string | undefined {
if (!senses || senses.length === 0) return undefined;
return senses
.map((s) => {
const parts = [capitalize(s.type.replaceAll("-", " "))];
if (s.acuity && s.acuity !== "precise") {
parts.push(`(${s.acuity})`);
}
if (s.range != null) parts.push(`${s.range} feet`);
return parts.join(" ");
})
.join(", ");
}
function formatLanguages(
languages: { value?: string[]; details?: string } | undefined,
): string | undefined {
if (!languages?.value || languages.value.length === 0) return undefined;
const list = languages.value.map(capitalize).join(", ");
return languages.details ? `${list} (${languages.details})` : list;
}
function formatSkills(
skills: Record<string, { base: number; note?: string }> | undefined,
): string | undefined {
if (!skills) return undefined;
const entries = Object.entries(skills);
if (entries.length === 0) return undefined;
return entries
.map(([name, val]) => {
const label = capitalize(name.replaceAll("-", " "));
return `${label} +${val.base}`;
})
.sort()
.join(", ");
}
function formatImmunities(
immunities: { type: string; exceptions?: string[] }[] | undefined,
): string | undefined {
if (!immunities || immunities.length === 0) return undefined;
return immunities
.map((i) => {
const base = capitalize(i.type.replaceAll("-", " "));
if (i.exceptions && i.exceptions.length > 0) {
return `${base} (except ${i.exceptions.map((e) => capitalize(e.replaceAll("-", " "))).join(", ")})`;
}
return base;
})
.join(", ");
}
function formatResistances(
resistances:
| { type: string; value: number; exceptions?: string[] }[]
| undefined,
): string | undefined {
if (!resistances || resistances.length === 0) return undefined;
return resistances
.map((r) => {
const base = `${capitalize(r.type.replaceAll("-", " "))} ${r.value}`;
if (r.exceptions && r.exceptions.length > 0) {
return `${base} (except ${r.exceptions.map((e) => capitalize(e.replaceAll("-", " "))).join(", ")})`;
}
return base;
})
.join(", ");
}
function formatWeaknesses(
weaknesses: { type: string; value: number }[] | undefined,
): string | undefined {
if (!weaknesses || weaknesses.length === 0) return undefined;
return weaknesses
.map((w) => `${capitalize(w.type.replaceAll("-", " "))} ${w.value}`)
.join(", ");
}
function formatSpeed(speed: {
value: number;
otherSpeeds?: { type: string; value: number }[];
details?: string;
}): string {
const parts = [`${speed.value} feet`];
if (speed.otherSpeeds) {
for (const s of speed.otherSpeeds) {
parts.push(`${capitalize(s.type)} ${s.value} feet`);
}
}
const base = parts.join(", ");
return speed.details ? `${base} (${speed.details})` : base;
}
// -- Attack normalization --
function normalizeAttack(item: RawFoundryItem): TraitBlock {
const sys = item.system as unknown as MeleeSystem;
const bonus = sys.bonus?.value ?? 0;
const traits = sys.traits?.value ?? [];
const damageEntries = Object.values(sys.damageRolls ?? {});
const damage = damageEntries
.map((d) => `${d.damage} ${d.damageType}`)
.join(" plus ");
const traitStr =
traits.length > 0 ? ` (${traits.map(formatTrait).join(", ")})` : "";
return {
name: capitalize(item.name),
activity: { number: 1, unit: "action" },
segments: [
{
type: "text",
value: `+${bonus}${traitStr}, ${damage}`,
},
],
};
}
function parseActivity(
actionType: string | undefined,
actionCount: number | null | undefined,
): { number: number; unit: "action" | "free" | "reaction" } | undefined {
if (actionType === "action") {
return { number: actionCount ?? 1, unit: "action" };
}
if (actionType === "reaction") {
return { number: 1, unit: "reaction" };
}
if (actionType === "free") {
return { number: 1, unit: "free" };
}
return undefined;
}
// -- Ability normalization --
function normalizeAbility(item: RawFoundryItem): TraitBlock {
const sys = item.system as unknown as ActionSystem;
const actionType = sys.actionType?.value;
const actionCount = sys.actions?.value;
const description = stripFoundryTags(sys.description?.value ?? "");
const traits = sys.traits?.value ?? [];
const activity = parseActivity(actionType, actionCount);
const traitStr =
traits.length > 0
? `(${traits.map((t) => capitalize(formatTrait(t))).join(", ")}) `
: "";
const text = traitStr ? `${traitStr}${description}` : description;
const segments: { type: "text"; value: string }[] = text
? [{ type: "text", value: text }]
: [];
return { name: item.name, activity, segments };
}
// -- Spellcasting normalization --
function classifySpell(spell: RawFoundryItem): {
isCantrip: boolean;
rank: number;
label: string;
} {
const sys = spell.system as unknown as SpellSystem;
const isCantrip = (sys.traits?.value ?? []).includes("cantrip");
const rank = sys.location?.heightenedLevel ?? sys.level?.value ?? 0;
const uses = sys.location?.uses;
const label =
uses && uses.max > 1 ? `${spell.name} (\u00d7${uses.max})` : spell.name;
return { isCantrip, rank, label };
}
function normalizeSpellcastingEntry(
entry: RawFoundryItem,
allSpells: readonly RawFoundryItem[],
): SpellcastingBlock {
const sys = entry.system as unknown as SpellcastingEntrySystem;
const tradition = capitalize(sys.tradition?.value ?? "");
const prepared = sys.prepared?.value ?? "";
const dc = sys.spelldc?.dc ?? 0;
const attack = sys.spelldc?.value ?? 0;
const name = entry.name || `${tradition} ${capitalize(prepared)} Spells`;
const headerText = `DC ${dc}${attack ? `, attack +${attack}` : ""}`;
const linkedSpells = allSpells.filter(
(s) => (s.system as unknown as SpellSystem).location?.value === entry._id,
);
const byRank = new Map<number, string[]>();
const cantrips: string[] = [];
for (const spell of linkedSpells) {
const { isCantrip, rank, label } = classifySpell(spell);
if (isCantrip) {
cantrips.push(spell.name);
continue;
}
const existing = byRank.get(rank) ?? [];
existing.push(label);
byRank.set(rank, existing);
}
const daily = [...byRank.entries()]
.sort(([a], [b]) => b - a)
.map(([rank, spellNames]) => ({
uses: rank,
each: true,
spells: spellNames,
}));
return {
name,
headerText,
atWill: orUndefined(cantrips),
daily: orUndefined(daily),
};
}
function normalizeSpellcasting(
items: readonly RawFoundryItem[],
): SpellcastingBlock[] {
const entries = items.filter((i) => i.type === "spellcastingEntry");
const spells = items.filter((i) => i.type === "spell");
return entries.map((entry) => normalizeSpellcastingEntry(entry, spells));
}
// -- Main normalization --
function orUndefined<T>(arr: T[]): T[] | undefined {
return arr.length > 0 ? arr : undefined;
}
/** Build display traits: [rarity (if not common), size, ...type traits] */
function buildTraits(traits: {
rarity: string;
size: { value: string };
value: string[];
}): string[] {
const result: string[] = [];
if (traits.rarity && traits.rarity !== "common") {
result.push(traits.rarity);
}
const size = SIZE_MAP[traits.size.value] ?? "medium";
result.push(size);
result.push(...traits.value);
return result;
}
const HEALING_GLOSSARY =
/^<p>@Localize\[PF2E\.NPC\.Abilities\.Glossary\.(FastHealing|Regeneration|NegativeHealing)\]/;
/** Glossary-only abilities that duplicate structured data shown elsewhere. */
const REDUNDANT_GLOSSARY =
/^<p>@Localize\[PF2E\.NPC\.Abilities\.Glossary\.(ConstantSpells|AtWillSpells)\]/;
const STRIP_GLOSSARY_AND_P = /<p>@Localize\[[^\]]+\]<\/p>|<\/?p>/g;
/** True when the description has no user-visible content beyond glossary tags. */
function isGlossaryOnly(desc: string | undefined): boolean {
if (!desc) return true;
return desc.replace(STRIP_GLOSSARY_AND_P, "").trim() === "";
}
function isRedundantAbility(
item: RawFoundryItem,
excludeName: string | undefined,
hpDetails: string | undefined,
): boolean {
const sys = item.system as unknown as ActionSystem;
const desc = sys.description?.value;
// Ability duplicates the allSaves line — suppress only if glossary-only
if (excludeName && item.name.toLowerCase() === excludeName.toLowerCase()) {
return isGlossaryOnly(desc);
}
if (!desc) return false;
// Healing/regen glossary when hp.details already shows the info
if (hpDetails && HEALING_GLOSSARY.test(desc)) return true;
// Spell mechanic glossary reminders shown in the spellcasting section
if (REDUNDANT_GLOSSARY.test(desc)) return true;
return false;
}
function actionsByCategory(
items: readonly RawFoundryItem[],
category: string,
excludeName?: string,
hpDetails?: string,
): TraitBlock[] {
return items
.filter(
(a) =>
a.type === "action" &&
(a.system as unknown as ActionSystem).category === category &&
!isRedundantAbility(a, excludeName, hpDetails),
)
.map(normalizeAbility);
}
function extractAbilityMods(
mods: Record<string, { mod: number }>,
): Pf2eCreature["abilityMods"] {
return {
str: mods.str?.mod ?? 0,
dex: mods.dex?.mod ?? 0,
con: mods.con?.mod ?? 0,
int: mods.int?.mod ?? 0,
wis: mods.wis?.mod ?? 0,
cha: mods.cha?.mod ?? 0,
};
}
export function normalizeFoundryCreature(
raw: unknown,
sourceCode?: string,
sourceDisplayName?: string,
): Pf2eCreature {
const r = raw as RawFoundryCreature;
const sys = r.system;
const publication = sys.details?.publication;
const source = sourceCode ?? publication?.title ?? "";
const items = r.items ?? [];
const allSavesText = sys.attributes.allSaves?.value ?? "";
return {
system: "pf2e",
id: makeCreatureId(source, r.name),
name: r.name,
source,
sourceDisplayName: sourceDisplayName ?? publication?.title ?? "",
level: sys.details?.level?.value ?? 0,
traits: buildTraits(sys.traits),
perception: sys.perception?.mod ?? 0,
senses: formatSenses(sys.perception?.senses),
languages: formatLanguages(sys.details?.languages),
skills: formatSkills(sys.skills),
abilityMods: extractAbilityMods(sys.abilities ?? {}),
ac: sys.attributes.ac.value,
acConditional: sys.attributes.ac.details || undefined,
saveFort: sys.saves.fortitude.value,
saveRef: sys.saves.reflex.value,
saveWill: sys.saves.will.value,
saveConditional: allSavesText || undefined,
hp: sys.attributes.hp.max,
hpDetails: sys.attributes.hp.details || undefined,
immunities: formatImmunities(sys.attributes.immunities),
resistances: formatResistances(sys.attributes.resistances),
weaknesses: formatWeaknesses(sys.attributes.weaknesses),
speed: formatSpeed(sys.attributes.speed),
attacks: orUndefined(
items.filter((i) => i.type === "melee").map(normalizeAttack),
),
abilitiesTop: orUndefined(actionsByCategory(items, "interaction")),
abilitiesMid: orUndefined(
actionsByCategory(
items,
"defensive",
allSavesText || undefined,
sys.attributes.hp.details || undefined,
),
),
abilitiesBot: orUndefined(actionsByCategory(items, "offensive")),
spellcasting: orUndefined(normalizeSpellcasting(items)),
};
}
export function normalizeFoundryCreatures(
rawCreatures: unknown[],
sourceCode?: string,
sourceDisplayName?: string,
): Pf2eCreature[] {
return rawCreatures.map((raw) =>
normalizeFoundryCreature(raw, sourceCode, sourceDisplayName),
);
}

View File

@@ -0,0 +1,75 @@
import type {
Pf2eBestiaryIndex,
Pf2eBestiaryIndexEntry,
} from "@initiative/domain";
import rawIndex from "../../../../data/bestiary/pf2e-index.json";
interface CompactCreature {
readonly n: string;
readonly s: string;
readonly lv: number;
readonly ac: number;
readonly hp: number;
readonly pc: number;
readonly sz: string;
readonly tp: string;
readonly f: string;
readonly li: string;
}
interface CompactIndex {
readonly sources: Record<string, string>;
readonly creatures: readonly CompactCreature[];
}
function mapCreature(c: CompactCreature): Pf2eBestiaryIndexEntry {
return {
name: c.n,
source: c.s,
level: c.lv,
ac: c.ac,
hp: c.hp,
perception: c.pc,
size: c.sz,
type: c.tp,
};
}
let cachedIndex: Pf2eBestiaryIndex | undefined;
export function loadPf2eBestiaryIndex(): Pf2eBestiaryIndex {
if (cachedIndex) return cachedIndex;
const compact = rawIndex as unknown as CompactIndex;
cachedIndex = {
sources: compact.sources,
creatures: compact.creatures.map(mapCreature),
};
return cachedIndex;
}
export function getAllPf2eSourceCodes(): string[] {
const index = loadPf2eBestiaryIndex();
return Object.keys(index.sources);
}
export function getDefaultPf2eFetchUrl(
_sourceCode: string,
baseUrl?: string,
): string {
if (baseUrl !== undefined) {
return baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
}
return "https://raw.githubusercontent.com/foundryvtt/pf2e/v13-dev/packs/pf2e/";
}
export function getCreaturePathsForSource(sourceCode: string): string[] {
const compact = rawIndex as unknown as CompactIndex;
return compact.creatures.filter((c) => c.s === sourceCode).map((c) => c.f);
}
export function getPf2eSourceDisplayName(sourceCode: string): string {
const index = loadPf2eBestiaryIndex();
return index.sources[sourceCode] ?? sourceCode;
}

View File

@@ -0,0 +1,60 @@
import type {
AnyCreature,
BestiaryIndex,
CreatureId,
Encounter,
Pf2eBestiaryIndex,
PlayerCharacter,
UndoRedoState,
} from "@initiative/domain";
export interface EncounterPersistence {
load(): Encounter | null;
save(encounter: Encounter): void;
}
export interface UndoRedoPersistence {
load(): UndoRedoState;
save(state: UndoRedoState): void;
}
export interface PlayerCharacterPersistence {
load(): PlayerCharacter[];
save(characters: PlayerCharacter[]): void;
}
export interface CachedSourceInfo {
readonly sourceCode: string;
readonly displayName: string;
readonly creatureCount: number;
readonly cachedAt: number;
}
export interface BestiaryCachePort {
cacheSource(
system: string,
sourceCode: string,
displayName: string,
creatures: AnyCreature[],
): Promise<void>;
isSourceCached(system: string, sourceCode: string): Promise<boolean>;
getCachedSources(system?: string): Promise<CachedSourceInfo[]>;
clearSource(system: string, sourceCode: string): Promise<void>;
clearAll(): Promise<void>;
loadAllCachedCreatures(): Promise<Map<CreatureId, AnyCreature>>;
}
export interface BestiaryIndexPort {
loadIndex(): BestiaryIndex;
getAllSourceCodes(): string[];
getDefaultFetchUrl(sourceCode: string, baseUrl?: string): string;
getSourceDisplayName(sourceCode: string): string;
}
export interface Pf2eBestiaryIndexPort {
loadIndex(): Pf2eBestiaryIndex;
getAllSourceCodes(): string[];
getDefaultFetchUrl(sourceCode: string, baseUrl?: string): string;
getSourceDisplayName(sourceCode: string): string;
getCreaturePathsForSource(sourceCode: string): string[];
}

View File

@@ -0,0 +1,52 @@
import type { Adapters } from "../contexts/adapter-context.js";
import {
loadEncounter,
saveEncounter,
} from "../persistence/encounter-storage.js";
import {
loadPlayerCharacters,
savePlayerCharacters,
} from "../persistence/player-character-storage.js";
import {
loadUndoRedoStacks,
saveUndoRedoStacks,
} from "../persistence/undo-redo-storage.js";
import * as bestiaryCache from "./bestiary-cache.js";
import * as bestiaryIndex from "./bestiary-index-adapter.js";
import * as pf2eBestiaryIndex from "./pf2e-bestiary-index-adapter.js";
export const productionAdapters: Adapters = {
encounterPersistence: {
load: loadEncounter,
save: saveEncounter,
},
undoRedoPersistence: {
load: loadUndoRedoStacks,
save: saveUndoRedoStacks,
},
playerCharacterPersistence: {
load: loadPlayerCharacters,
save: savePlayerCharacters,
},
bestiaryCache: {
cacheSource: bestiaryCache.cacheSource,
isSourceCached: bestiaryCache.isSourceCached,
getCachedSources: bestiaryCache.getCachedSources,
clearSource: bestiaryCache.clearSource,
clearAll: bestiaryCache.clearAll,
loadAllCachedCreatures: bestiaryCache.loadAllCachedCreatures,
},
bestiaryIndex: {
loadIndex: bestiaryIndex.loadBestiaryIndex,
getAllSourceCodes: bestiaryIndex.getAllSourceCodes,
getDefaultFetchUrl: bestiaryIndex.getDefaultFetchUrl,
getSourceDisplayName: bestiaryIndex.getSourceDisplayName,
},
pf2eBestiaryIndex: {
loadIndex: pf2eBestiaryIndex.loadPf2eBestiaryIndex,
getAllSourceCodes: pf2eBestiaryIndex.getAllPf2eSourceCodes,
getDefaultFetchUrl: pf2eBestiaryIndex.getDefaultPf2eFetchUrl,
getSourceDisplayName: pf2eBestiaryIndex.getPf2eSourceDisplayName,
getCreaturePathsForSource: pf2eBestiaryIndex.getCreaturePathsForSource,
},
};

View File

@@ -0,0 +1,99 @@
/**
* Strips Foundry VTT HTML descriptions with enrichment syntax to plain
* readable text. Handles @Damage, @Check, @UUID, @Template and generic
* @Tag patterns as well as common HTML elements.
*/
// -- Enrichment-param helpers --
function formatDamage(params: string): string {
// "3d6+10[fire]" → "3d6+10 fire"
return params.replaceAll(/\[([^\]]*)\]/g, " $1").trim();
}
function formatCheck(params: string): string {
// "reflex|dc:33|basic" → "DC 33 basic Reflex"
const parts = params.split("|");
const type = parts[0] ?? "";
let dc = "";
let basic = false;
for (const part of parts.slice(1)) {
if (part.startsWith("dc:")) {
dc = part.slice(3);
} else if (part === "basic") {
basic = true;
}
}
const label = type.charAt(0).toUpperCase() + type.slice(1);
const dcStr = dc ? `DC ${dc} ` : "";
const basicStr = basic ? "basic " : "";
return `${dcStr}${basicStr}${label}`;
}
function formatTemplate(params: string): string {
// "cone|distance:40" → "40-foot cone"
const parts = params.split("|");
const shape = parts[0] ?? "";
let distance = "";
for (const part of parts.slice(1)) {
if (part.startsWith("distance:")) {
distance = part.slice(9);
}
}
return distance ? `${distance}-foot ${shape}` : shape;
}
export function stripFoundryTags(html: string): string {
if (typeof html !== "string") return String(html);
let result = html;
// Strip Foundry enrichment tags (with optional display text)
// @Tag[params]{display} → display (prefer display text)
// @Tag[params] → extracted content
// @Damage has nested brackets: @Damage[3d6+10[fire]]
result = result.replaceAll(
/@Damage\[((?:[^[\]]|\[[^\]]*\])*)\](?:\{([^}]+)\})?/g,
(_, params: string, display: string | undefined) =>
display ?? formatDamage(params),
);
result = result.replaceAll(
/@Check\[([^\]]+)\](?:\{([^}]*)\})?/g,
(_, params: string) => formatCheck(params),
);
result = result.replaceAll(
/@UUID\[[^\]]+?([^./\]]+)\](?:\{([^}]+)\})?/g,
(_, lastSegment: string, display: string | undefined) =>
display ?? lastSegment,
);
result = result.replaceAll(
/@Template\[([^\]]+)\](?:\{([^}]+)\})?/g,
(_, params: string, display: string | undefined) =>
display ?? formatTemplate(params),
);
// Catch-all for unknown @Tag patterns
result = result.replaceAll(
/@\w+\[[^\]]*\](?:\{([^}]+)\})?/g,
(_, display: string | undefined) => display ?? "",
);
// Strip action-glyph spans (content is a number the renderer handles)
result = result.replaceAll(/<span class="action-glyph">[^<]*<\/span>/gi, "");
// Strip HTML tags
result = result.replaceAll(/<br\s*\/?>/gi, "\n");
result = result.replaceAll(/<hr\s*\/?>/gi, "\n");
result = result.replaceAll(/<\/p>\s*<p[^>]*>/gi, "\n");
result = result.replaceAll(/<[^>]+>/g, "");
// Decode common HTML entities
result = result.replaceAll("&amp;", "&");
result = result.replaceAll("&lt;", "<");
result = result.replaceAll("&gt;", ">");
result = result.replaceAll("&quot;", '"');
// Collapse whitespace
result = result.replaceAll(/[ \t]+/g, " ");
result = result.replaceAll(/\n\s*\n/g, "\n");
return result.trim();
}

View File

@@ -98,20 +98,26 @@ export function stripTags(text: string): string {
// Generic tags: {@tag Display|Source|...} → Display (first segment before |) // Generic tags: {@tag Display|Source|...} → Display (first segment before |)
// Covers: spell, condition, damage, dice, variantrule, action, skill, // Covers: spell, condition, damage, dice, variantrule, action, skill,
// creature, hazard, status, plus any unknown tags // creature, hazard, status, plus any unknown tags
// Run in a loop to resolve nested tags (e.g. {@b ... {@spell fireball} ...})
// from innermost to outermost.
const tagPattern = /\{@(\w+)\s+([^}]+)\}/g;
while (tagPattern.test(result)) {
result = result.replaceAll( result = result.replaceAll(
/\{@(\w+)\s+([^}]+)\}/g, tagPattern,
(_, tag: string, content: string) => { (_, tag: string, content: string) => {
// For tags with Display|Source format, extract first segment
const segments = content.split("|"); const segments = content.split("|");
// Some tags have a third segment as display text: {@variantrule Name|Source|Display} if (
if ((tag === "variantrule" || tag === "action") && segments.length >= 3) { (tag === "variantrule" || tag === "action") &&
segments.length >= 3
) {
return segments[2]; return segments[2];
} }
return segments[0]; return segments[0];
}, },
); );
}
return result; return result;
} }

View File

@@ -1,41 +1,16 @@
// @vitest-environment jsdom // @vitest-environment jsdom
import "@testing-library/jest-dom/vitest"; import "@testing-library/jest-dom/vitest";
import { cleanup, render, screen } from "@testing-library/react"; import { playerCharacterId } from "@initiative/domain";
import { cleanup, render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event"; import userEvent from "@testing-library/user-event";
import type { ReactNode } from "react";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest"; import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import { createTestAdapters } from "../../__tests__/adapters/in-memory-adapters.js";
import { polyfillDialog } from "../../__tests__/polyfill-dialog.js"; import { polyfillDialog } from "../../__tests__/polyfill-dialog.js";
import { AllProviders } from "../../__tests__/test-providers.js"; import { AllProviders } from "../../__tests__/test-providers.js";
import { ActionBar } from "../action-bar.js"; import { ActionBar } from "../action-bar.js";
// Mock persistence — no localStorage interaction
vi.mock("../../persistence/encounter-storage.js", () => ({
loadEncounter: () => null,
saveEncounter: () => {},
}));
vi.mock("../../persistence/player-character-storage.js", () => ({
loadPlayerCharacters: () => [],
savePlayerCharacters: () => {},
}));
// Mock bestiary — no IndexedDB or JSON index
vi.mock("../../adapters/bestiary-cache.js", () => ({
loadAllCachedCreatures: () => Promise.resolve(new Map()),
isSourceCached: () => Promise.resolve(false),
cacheSource: () => Promise.resolve(),
getCachedSources: () => Promise.resolve([]),
clearSource: () => Promise.resolve(),
clearAll: () => Promise.resolve(),
}));
vi.mock("../../adapters/bestiary-index-adapter.js", () => ({
loadBestiaryIndex: () => ({ sources: {}, creatures: [] }),
getAllSourceCodes: () => [],
getDefaultFetchUrl: () => "",
getSourceDisplayName: (code: string) => code,
}));
// DOM API stubs — jsdom doesn't implement these // DOM API stubs — jsdom doesn't implement these
beforeAll(() => { beforeAll(() => {
Object.defineProperty(globalThis, "matchMedia", { Object.defineProperty(globalThis, "matchMedia", {
@@ -60,10 +35,97 @@ function renderBar(props: Partial<Parameters<typeof ActionBar>[0]> = {}) {
return render(<ActionBar {...props} />, { wrapper: AllProviders }); return render(<ActionBar {...props} />, { wrapper: AllProviders });
} }
function renderBarWithBestiary(
props: Partial<Parameters<typeof ActionBar>[0]> = {},
) {
const adapters = createTestAdapters();
adapters.bestiaryIndex = {
...adapters.bestiaryIndex,
loadIndex: () => ({
sources: { MM: "Monster Manual" },
creatures: [
{
name: "Goblin",
source: "MM",
ac: 15,
hp: 7,
dex: 14,
cr: "1/4",
initiativeProficiency: 0,
size: "Small",
type: "humanoid",
},
{
name: "Golem, Iron",
source: "MM",
ac: 20,
hp: 210,
dex: 9,
cr: "16",
initiativeProficiency: 0,
size: "Large",
type: "construct",
},
],
}),
getSourceDisplayName: (code: string) =>
code === "MM" ? "Monster Manual" : code,
};
return render(<ActionBar {...props} />, {
wrapper: ({ children }: { children: ReactNode }) => (
<AllProviders adapters={adapters}>{children}</AllProviders>
),
});
}
function renderBarWithPCs(
props: Partial<Parameters<typeof ActionBar>[0]> = {},
) {
const adapters = createTestAdapters({
playerCharacters: [
{
id: playerCharacterId("pc-1"),
name: "Gandalf",
ac: 15,
maxHp: 40,
},
],
});
adapters.bestiaryIndex = {
...adapters.bestiaryIndex,
loadIndex: () => ({
sources: { MM: "Monster Manual" },
creatures: [
{
name: "Goblin",
source: "MM",
ac: 15,
hp: 7,
dex: 14,
cr: "1/4",
initiativeProficiency: 0,
size: "Small",
type: "humanoid",
},
],
}),
getSourceDisplayName: (code: string) =>
code === "MM" ? "Monster Manual" : code,
};
return render(<ActionBar {...props} />, {
wrapper: ({ children }: { children: ReactNode }) => (
<AllProviders adapters={adapters}>{children}</AllProviders>
),
});
}
describe("ActionBar", () => { describe("ActionBar", () => {
describe("basic rendering and custom add", () => {
it("renders input with placeholder '+ Add combatants'", () => { it("renders input with placeholder '+ Add combatants'", () => {
renderBar(); renderBar();
expect(screen.getByPlaceholderText("+ Add combatants")).toBeInTheDocument(); expect(
screen.getByPlaceholderText("+ Add combatants"),
).toBeInTheDocument();
}); });
it("submitting with a name adds a combatant", async () => { it("submitting with a name adds a combatant", async () => {
@@ -71,20 +133,16 @@ describe("ActionBar", () => {
renderBar(); renderBar();
const input = screen.getByPlaceholderText("+ Add combatants"); const input = screen.getByPlaceholderText("+ Add combatants");
await user.type(input, "Goblin"); await user.type(input, "Goblin");
// The Add button appears when name >= 2 chars and no suggestions
const addButton = screen.getByRole("button", { name: "Add" }); const addButton = screen.getByRole("button", { name: "Add" });
await user.click(addButton); await user.click(addButton);
// Input is cleared after adding (context handles the state)
expect(input).toHaveValue(""); expect(input).toHaveValue("");
}); });
it("submitting with empty name does nothing", async () => { it("submitting with empty name does nothing", async () => {
const user = userEvent.setup(); const user = userEvent.setup();
renderBar(); renderBar();
// Submit the form directly (Enter on empty input)
const input = screen.getByPlaceholderText("+ Add combatants"); const input = screen.getByPlaceholderText("+ Add combatants");
await user.type(input, "{Enter}"); await user.type(input, "{Enter}");
// Input stays empty, no error
expect(input).toHaveValue(""); expect(input).toHaveValue("");
}); });
@@ -106,6 +164,160 @@ describe("ActionBar", () => {
expect(screen.getByRole("button", { name: "Add" })).toBeInTheDocument(); expect(screen.getByRole("button", { name: "Add" })).toBeInTheDocument();
}); });
it("submits custom stats with combatant", async () => {
const user = userEvent.setup();
renderBar();
const input = screen.getByPlaceholderText("+ Add combatants");
await user.type(input, "Fighter");
await user.type(screen.getByPlaceholderText("Init"), "15");
await user.type(screen.getByPlaceholderText("AC"), "18");
await user.type(screen.getByPlaceholderText("MaxHP"), "45");
await user.click(screen.getByRole("button", { name: "Add" }));
expect(input).toHaveValue("");
});
});
describe("bestiary suggestions and queuing", () => {
it("shows bestiary suggestions when typing a matching name", async () => {
const user = userEvent.setup();
renderBarWithBestiary();
const input = screen.getByPlaceholderText("+ Add combatants");
await user.type(input, "Go");
await waitFor(() => {
expect(screen.getByText("Goblin")).toBeInTheDocument();
});
expect(screen.getByText("Golem, Iron")).toBeInTheDocument();
});
it("clicking a suggestion queues it with count badge", async () => {
const user = userEvent.setup();
renderBarWithBestiary();
const input = screen.getByPlaceholderText("+ Add combatants");
await user.type(input, "Go");
await waitFor(() => {
expect(screen.getByText("Goblin")).toBeInTheDocument();
});
// Click the Goblin suggestion
await user.click(screen.getByText("Goblin"));
// Should show count badge "1"
expect(screen.getByText("1")).toBeInTheDocument();
});
it("clicking same suggestion again increments count", async () => {
const user = userEvent.setup();
renderBarWithBestiary();
const input = screen.getByPlaceholderText("+ Add combatants");
await user.type(input, "Go");
await waitFor(() => {
expect(screen.getByText("Goblin")).toBeInTheDocument();
});
await user.click(screen.getByText("Goblin"));
await user.click(screen.getByText("Goblin"));
expect(screen.getByText("2")).toBeInTheDocument();
});
it("confirming queued creatures adds them to the encounter", async () => {
const user = userEvent.setup();
renderBarWithBestiary();
const input = screen.getByPlaceholderText("+ Add combatants");
await user.type(input, "Go");
await waitFor(() => {
expect(screen.getByText("Goblin")).toBeInTheDocument();
});
// Queue 1 Goblin
await user.click(screen.getByText("Goblin"));
// Press Enter to confirm the queued creature
await user.keyboard("{Enter}");
// Input should be cleared after confirming
expect(input).toHaveValue("");
});
it("clears queued when search text no longer matches", async () => {
const user = userEvent.setup();
renderBarWithBestiary();
const input = screen.getByPlaceholderText("+ Add combatants");
await user.type(input, "Go");
await waitFor(() => {
expect(screen.getByText("Goblin")).toBeInTheDocument();
});
await user.click(screen.getByText("Goblin"));
expect(screen.getByText("1")).toBeInTheDocument();
// Change search to something with no matches
await user.clear(input);
await user.type(input, "xyz");
// Count badge should be gone
expect(screen.queryByText("1")).not.toBeInTheDocument();
});
});
describe("player character matching", () => {
it("shows matching player characters in suggestions", async () => {
const user = userEvent.setup();
renderBarWithPCs();
const input = screen.getByPlaceholderText("+ Add combatants");
await user.type(input, "Gan");
await waitFor(() => {
expect(screen.getByText("Gandalf")).toBeInTheDocument();
});
expect(screen.getByText("Player")).toBeInTheDocument();
});
});
describe("browse mode", () => {
it("toggles browse mode via eye icon button", async () => {
const user = userEvent.setup();
renderBarWithBestiary();
const browseButton = screen.getByRole("button", {
name: "Browse stat blocks",
});
await user.click(browseButton);
expect(
screen.getByPlaceholderText("Search stat blocks..."),
).toBeInTheDocument();
expect(
screen.getByRole("button", { name: "Switch to add mode" }),
).toBeInTheDocument();
});
it("browse mode shows suggestions without add UI", async () => {
const user = userEvent.setup();
renderBarWithBestiary();
await user.click(
screen.getByRole("button", { name: "Browse stat blocks" }),
);
const input = screen.getByPlaceholderText("Search stat blocks...");
await user.type(input, "Go");
await waitFor(() => {
expect(screen.getByText("Goblin")).toBeInTheDocument();
});
// No Add button in browse mode
expect(
screen.queryByRole("button", { name: "Add" }),
).not.toBeInTheDocument();
});
});
describe("overflow menu", () => {
it("does not show roll all initiative button when no creature combatants", () => { it("does not show roll all initiative button when no creature combatants", () => {
renderBar(); renderBar();
expect( expect(
@@ -115,7 +327,6 @@ describe("ActionBar", () => {
it("shows overflow menu items", () => { it("shows overflow menu items", () => {
renderBar({ onManagePlayers: vi.fn() }); renderBar({ onManagePlayers: vi.fn() });
// The overflow menu should be present (it contains Player Characters etc.)
expect( expect(
screen.getByRole("button", { name: "More actions" }), screen.getByRole("button", { name: "More actions" }),
).toBeInTheDocument(); ).toBeInTheDocument();
@@ -125,10 +336,8 @@ describe("ActionBar", () => {
const user = userEvent.setup(); const user = userEvent.setup();
renderBar(); renderBar();
await user.click(screen.getByRole("button", { name: "More actions" })); await user.click(screen.getByRole("button", { name: "More actions" }));
// Click the menu item
const items = screen.getAllByText("Export Encounter"); const items = screen.getAllByText("Export Encounter");
await user.click(items[0]); await user.click(items[0]);
// Dialog should now be open — it renders a second "Export Encounter" as heading
expect( expect(
screen.getAllByText("Export Encounter").length, screen.getAllByText("Export Encounter").length,
).toBeGreaterThanOrEqual(1); ).toBeGreaterThanOrEqual(1);
@@ -162,19 +371,5 @@ describe("ActionBar", () => {
await user.click(screen.getByText("Settings")); await user.click(screen.getByText("Settings"));
expect(onOpenSettings).toHaveBeenCalledOnce(); expect(onOpenSettings).toHaveBeenCalledOnce();
}); });
it("submits custom stats with combatant", async () => {
const user = userEvent.setup();
renderBar();
const input = screen.getByPlaceholderText("+ Add combatants");
await user.type(input, "Fighter");
const initInput = screen.getByPlaceholderText("Init");
const acInput = screen.getByPlaceholderText("AC");
const hpInput = screen.getByPlaceholderText("MaxHP");
await user.type(initInput, "15");
await user.type(acInput, "18");
await user.type(hpInput, "45");
await user.click(screen.getByRole("button", { name: "Add" }));
expect(input).toHaveValue("");
}); });
}); });

View File

@@ -4,6 +4,9 @@ import "@testing-library/jest-dom/vitest";
import { cleanup, render, screen } from "@testing-library/react"; import { cleanup, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event"; import userEvent from "@testing-library/user-event";
import { afterEach, describe, expect, it, vi } from "vitest"; import { afterEach, describe, expect, it, vi } from "vitest";
import { createTestAdapters } from "../../__tests__/adapters/in-memory-adapters.js";
import { AdapterProvider } from "../../contexts/adapter-context.js";
import { RulesEditionProvider } from "../../contexts/rules-edition-context.js";
import { BulkImportPrompt } from "../bulk-import-prompt.js"; import { BulkImportPrompt } from "../bulk-import-prompt.js";
const THREE_SOURCES_REGEX = /3 sources/; const THREE_SOURCES_REGEX = /3 sources/;
@@ -28,6 +31,10 @@ let mockImportState = {
failed: 0, failed: 0,
}; };
// Uses context mocks because the bulk import state machine (idle → loading →
// complete → partial-failure) is impractical to drive through user interactions
// without real network calls. Consider migrating if adapter injection expands
// to cover these state transitions.
vi.mock("../../contexts/bestiary-context.js", () => ({ vi.mock("../../contexts/bestiary-context.js", () => ({
useBestiaryContext: () => ({ useBestiaryContext: () => ({
fetchAndCacheSource: mockFetchAndCacheSource, fetchAndCacheSource: mockFetchAndCacheSource,
@@ -50,12 +57,25 @@ vi.mock("../../contexts/side-panel-context.js", () => ({
}), }),
})); }));
vi.mock("../../adapters/bestiary-index-adapter.js", () => ({ function createAdaptersWithSources() {
const adapters = createTestAdapters();
adapters.bestiaryIndex = {
...adapters.bestiaryIndex,
getAllSourceCodes: () => ["MM", "VGM", "XGE"], getAllSourceCodes: () => ["MM", "VGM", "XGE"],
getDefaultFetchUrl: () => "", };
getSourceDisplayName: (code: string) => code, return adapters;
loadBestiaryIndex: () => ({ sources: {}, creatures: [] }), }
}));
function renderWithAdapters() {
const adapters = createAdaptersWithSources();
return render(
<RulesEditionProvider>
<AdapterProvider adapters={adapters}>
<BulkImportPrompt />
</AdapterProvider>
</RulesEditionProvider>,
);
}
describe("BulkImportPrompt", () => { describe("BulkImportPrompt", () => {
afterEach(() => { afterEach(() => {
@@ -64,7 +84,7 @@ describe("BulkImportPrompt", () => {
}); });
it("idle: shows base URL input, source count, Load All button", () => { it("idle: shows base URL input, source count, Load All button", () => {
render(<BulkImportPrompt />); renderWithAdapters();
expect(screen.getByText(THREE_SOURCES_REGEX)).toBeInTheDocument(); expect(screen.getByText(THREE_SOURCES_REGEX)).toBeInTheDocument();
expect(screen.getByDisplayValue(GITHUB_URL_REGEX)).toBeInTheDocument(); expect(screen.getByDisplayValue(GITHUB_URL_REGEX)).toBeInTheDocument();
expect( expect(
@@ -74,7 +94,7 @@ describe("BulkImportPrompt", () => {
it("idle: clearing URL disables the button", async () => { it("idle: clearing URL disables the button", async () => {
const user = userEvent.setup(); const user = userEvent.setup();
render(<BulkImportPrompt />); renderWithAdapters();
const input = screen.getByDisplayValue(GITHUB_URL_REGEX); const input = screen.getByDisplayValue(GITHUB_URL_REGEX);
await user.clear(input); await user.clear(input);
@@ -83,7 +103,7 @@ describe("BulkImportPrompt", () => {
it("idle: clicking Load All calls startImport with URL", async () => { it("idle: clicking Load All calls startImport with URL", async () => {
const user = userEvent.setup(); const user = userEvent.setup();
render(<BulkImportPrompt />); renderWithAdapters();
await user.click(screen.getByRole("button", { name: "Load All" })); await user.click(screen.getByRole("button", { name: "Load All" }));
expect(mockStartImport).toHaveBeenCalledWith( expect(mockStartImport).toHaveBeenCalledWith(
@@ -101,7 +121,7 @@ describe("BulkImportPrompt", () => {
completed: 3, completed: 3,
failed: 1, failed: 1,
}; };
render(<BulkImportPrompt />); renderWithAdapters();
expect(screen.getByText(LOADING_PROGRESS_REGEX)).toBeInTheDocument(); expect(screen.getByText(LOADING_PROGRESS_REGEX)).toBeInTheDocument();
}); });
@@ -112,7 +132,7 @@ describe("BulkImportPrompt", () => {
completed: 10, completed: 10,
failed: 0, failed: 0,
}; };
render(<BulkImportPrompt />); renderWithAdapters();
expect(screen.getByText("All sources loaded")).toBeInTheDocument(); expect(screen.getByText("All sources loaded")).toBeInTheDocument();
expect(screen.getByRole("button", { name: "Done" })).toBeInTheDocument(); expect(screen.getByRole("button", { name: "Done" })).toBeInTheDocument();
}); });
@@ -125,7 +145,7 @@ describe("BulkImportPrompt", () => {
failed: 0, failed: 0,
}; };
const user = userEvent.setup(); const user = userEvent.setup();
render(<BulkImportPrompt />); renderWithAdapters();
await user.click(screen.getByRole("button", { name: "Done" })); await user.click(screen.getByRole("button", { name: "Done" }));
expect(mockDismissPanel).toHaveBeenCalled(); expect(mockDismissPanel).toHaveBeenCalled();
@@ -139,7 +159,7 @@ describe("BulkImportPrompt", () => {
completed: 7, completed: 7,
failed: 3, failed: 3,
}; };
render(<BulkImportPrompt />); renderWithAdapters();
expect(screen.getByText(SEVEN_OF_TEN_REGEX)).toBeInTheDocument(); expect(screen.getByText(SEVEN_OF_TEN_REGEX)).toBeInTheDocument();
expect(screen.getByText(THREE_FAILED_REGEX)).toBeInTheDocument(); expect(screen.getByText(THREE_FAILED_REGEX)).toBeInTheDocument();
}); });

View File

@@ -13,34 +13,6 @@ const TEMP_HP_REGEX = /^\+\d/;
const CURRENT_HP_7_REGEX = /Current HP: 7/; const CURRENT_HP_7_REGEX = /Current HP: 7/;
const CURRENT_HP_REGEX = /Current HP/; const CURRENT_HP_REGEX = /Current HP/;
// Mock persistence — no localStorage interaction
vi.mock("../../persistence/encounter-storage.js", () => ({
loadEncounter: () => null,
saveEncounter: () => {},
}));
vi.mock("../../persistence/player-character-storage.js", () => ({
loadPlayerCharacters: () => [],
savePlayerCharacters: () => {},
}));
// Mock bestiary — no IndexedDB or JSON index
vi.mock("../../adapters/bestiary-cache.js", () => ({
loadAllCachedCreatures: () => Promise.resolve(new Map()),
isSourceCached: () => Promise.resolve(false),
cacheSource: () => Promise.resolve(),
getCachedSources: () => Promise.resolve([]),
clearSource: () => Promise.resolve(),
clearAll: () => Promise.resolve(),
}));
vi.mock("../../adapters/bestiary-index-adapter.js", () => ({
loadBestiaryIndex: () => ({ sources: {}, creatures: [] }),
getAllSourceCodes: () => [],
getDefaultFetchUrl: () => "",
getSourceDisplayName: (code: string) => code,
}));
// DOM API stubs // DOM API stubs
beforeAll(() => { beforeAll(() => {
Object.defineProperty(globalThis, "matchMedia", { Object.defineProperty(globalThis, "matchMedia", {

View File

@@ -1,7 +1,11 @@
// @vitest-environment jsdom // @vitest-environment jsdom
import "@testing-library/jest-dom/vitest"; import "@testing-library/jest-dom/vitest";
import { CONDITION_DEFINITIONS, type ConditionId } from "@initiative/domain"; import {
type ConditionEntry,
type ConditionId,
getConditionsForEdition,
} from "@initiative/domain";
import { cleanup, render, screen } from "@testing-library/react"; import { cleanup, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event"; import userEvent from "@testing-library/user-event";
import { createRef, type RefObject } from "react"; import { createRef, type RefObject } from "react";
@@ -13,12 +17,14 @@ afterEach(cleanup);
function renderPicker( function renderPicker(
overrides: Partial<{ overrides: Partial<{
activeConditions: readonly ConditionId[]; activeConditions: readonly ConditionEntry[];
onToggle: (conditionId: ConditionId) => void; onToggle: (conditionId: ConditionId) => void;
onSetValue: (conditionId: ConditionId, value: number) => void;
onClose: () => void; onClose: () => void;
}> = {}, }> = {},
) { ) {
const onToggle = overrides.onToggle ?? vi.fn(); const onToggle = overrides.onToggle ?? vi.fn();
const onSetValue = overrides.onSetValue ?? vi.fn();
const onClose = overrides.onClose ?? vi.fn(); const onClose = overrides.onClose ?? vi.fn();
const anchorRef = createRef<HTMLElement>() as RefObject<HTMLElement>; const anchorRef = createRef<HTMLElement>() as RefObject<HTMLElement>;
const anchor = document.createElement("div"); const anchor = document.createElement("div");
@@ -30,25 +36,27 @@ function renderPicker(
anchorRef={anchorRef} anchorRef={anchorRef}
activeConditions={overrides.activeConditions ?? []} activeConditions={overrides.activeConditions ?? []}
onToggle={onToggle} onToggle={onToggle}
onSetValue={onSetValue}
onClose={onClose} onClose={onClose}
/> />
</RulesEditionProvider>, </RulesEditionProvider>,
); );
return { ...result, onToggle, onClose }; return { ...result, onToggle, onSetValue, onClose };
} }
describe("ConditionPicker", () => { describe("ConditionPicker", () => {
it("renders all condition definitions from domain", () => { it("renders edition-specific conditions from domain", () => {
renderPicker(); renderPicker();
for (const def of CONDITION_DEFINITIONS) { const editionConditions = getConditionsForEdition("5.5e");
for (const def of editionConditions) {
expect(screen.getByText(def.label)).toBeInTheDocument(); expect(screen.getByText(def.label)).toBeInTheDocument();
} }
}); });
it("active conditions are visually distinguished", () => { it("active conditions are visually distinguished", () => {
renderPicker({ activeConditions: ["blinded"] }); renderPicker({ activeConditions: [{ id: "blinded" }] });
const blindedButton = screen.getByText("Blinded").closest("button"); const row = screen.getByText("Blinded").closest("div[class]");
expect(blindedButton?.className).toContain("bg-card/50"); expect(row?.className).toContain("bg-card/50");
}); });
it("clicking a condition calls onToggle with that condition's ID", async () => { it("clicking a condition calls onToggle with that condition's ID", async () => {
@@ -65,7 +73,7 @@ describe("ConditionPicker", () => {
}); });
it("active condition labels use foreground color", () => { it("active condition labels use foreground color", () => {
renderPicker({ activeConditions: ["charmed"] }); renderPicker({ activeConditions: [{ id: "charmed" }] });
const label = screen.getByText("Charmed"); const label = screen.getByText("Charmed");
expect(label.className).toContain("text-foreground"); expect(label.className).toContain("text-foreground");
}); });

View File

@@ -1,38 +1,36 @@
// @vitest-environment jsdom // @vitest-environment jsdom
import type { ConditionId } from "@initiative/domain"; import type { ConditionEntry } from "@initiative/domain";
import { cleanup, render, screen } from "@testing-library/react"; import { cleanup, render, screen } from "@testing-library/react";
import { userEvent } from "@testing-library/user-event"; import { userEvent } from "@testing-library/user-event";
import { afterEach, describe, expect, it, vi } from "vitest"; import { afterEach, describe, expect, it, vi } from "vitest";
import { RulesEditionProvider } from "../../contexts/rules-edition-context.js";
import { ConditionTags } from "../condition-tags.js"; import { ConditionTags } from "../condition-tags.js";
vi.mock("../../contexts/rules-edition-context.js", () => ({
useRulesEditionContext: () => ({ edition: "5.5e" }),
}));
afterEach(cleanup); afterEach(cleanup);
function renderTags(props: Partial<Parameters<typeof ConditionTags>[0]> = {}) {
return render(
<RulesEditionProvider>
<ConditionTags
conditions={props.conditions}
onRemove={props.onRemove ?? (() => {})}
onDecrement={props.onDecrement ?? (() => {})}
onOpenPicker={props.onOpenPicker ?? (() => {})}
/>
</RulesEditionProvider>,
);
}
describe("ConditionTags", () => { describe("ConditionTags", () => {
it("renders nothing when conditions is undefined", () => { it("renders nothing when conditions is undefined", () => {
const { container } = render( const { container } = renderTags();
<ConditionTags
conditions={undefined}
onRemove={() => {}}
onOpenPicker={() => {}}
/>,
);
// Only the add button should be present // Only the add button should be present
expect(container.querySelectorAll("button")).toHaveLength(1); expect(container.querySelectorAll("button")).toHaveLength(1);
}); });
it("renders a button per condition", () => { it("renders a button per condition", () => {
const conditions: ConditionId[] = ["blinded", "prone"]; const conditions: ConditionEntry[] = [{ id: "blinded" }, { id: "prone" }];
render( renderTags({ conditions });
<ConditionTags
conditions={conditions}
onRemove={() => {}}
onOpenPicker={() => {}}
/>,
);
expect( expect(
screen.getByRole("button", { name: "Remove Blinded" }), screen.getByRole("button", { name: "Remove Blinded" }),
).toBeDefined(); ).toBeDefined();
@@ -41,13 +39,10 @@ describe("ConditionTags", () => {
it("calls onRemove with condition id when clicked", async () => { it("calls onRemove with condition id when clicked", async () => {
const onRemove = vi.fn(); const onRemove = vi.fn();
render( renderTags({
<ConditionTags conditions: [{ id: "blinded" }] as ConditionEntry[],
conditions={["blinded"] as ConditionId[]} onRemove,
onRemove={onRemove} });
onOpenPicker={() => {}}
/>,
);
await userEvent.click( await userEvent.click(
screen.getByRole("button", { name: "Remove Blinded" }), screen.getByRole("button", { name: "Remove Blinded" }),
@@ -58,13 +53,7 @@ describe("ConditionTags", () => {
it("calls onOpenPicker when add button is clicked", async () => { it("calls onOpenPicker when add button is clicked", async () => {
const onOpenPicker = vi.fn(); const onOpenPicker = vi.fn();
render( renderTags({ conditions: [], onOpenPicker });
<ConditionTags
conditions={[]}
onRemove={() => {}}
onOpenPicker={onOpenPicker}
/>,
);
await userEvent.click( await userEvent.click(
screen.getByRole("button", { name: "Add condition" }), screen.getByRole("button", { name: "Add condition" }),
@@ -74,14 +63,41 @@ describe("ConditionTags", () => {
}); });
it("renders empty conditions array without errors", () => { it("renders empty conditions array without errors", () => {
render( renderTags({ conditions: [] });
<ConditionTags
conditions={[]}
onRemove={() => {}}
onOpenPicker={() => {}}
/>,
);
// Only add button // Only add button
expect(screen.getByRole("button", { name: "Add condition" })).toBeDefined(); expect(screen.getByRole("button", { name: "Add condition" })).toBeDefined();
}); });
it("displays value badge for valued conditions", () => {
renderTags({ conditions: [{ id: "frightened", value: 3 }] });
expect(screen.getByText("3")).toBeDefined();
});
it("calls onDecrement for valued condition click", async () => {
const onDecrement = vi.fn();
renderTags({
conditions: [{ id: "frightened", value: 2 }],
onDecrement,
});
await userEvent.click(
screen.getByRole("button", { name: "Remove Frightened" }),
);
expect(onDecrement).toHaveBeenCalledWith("frightened");
});
it("calls onRemove for non-valued condition click", async () => {
const onRemove = vi.fn();
renderTags({
conditions: [{ id: "blinded" }],
onRemove,
});
await userEvent.click(
screen.getByRole("button", { name: "Remove Blinded" }),
);
expect(onRemove).toHaveBeenCalledWith("blinded");
});
}); });

View File

@@ -0,0 +1,360 @@
// @vitest-environment jsdom
import "@testing-library/jest-dom/vitest";
import type { Creature, CreatureId, PlayerCharacter } from "@initiative/domain";
import { combatantId, creatureId, playerCharacterId } from "@initiative/domain";
import {
cleanup,
render,
renderHook,
screen,
waitFor,
} from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import { createTestAdapters } from "../../__tests__/adapters/in-memory-adapters.js";
import {
buildCombatant,
buildCreature,
buildEncounter,
} from "../../__tests__/factories/index.js";
import { AllProviders } from "../../__tests__/test-providers.js";
import { useRulesEdition } from "../../hooks/use-rules-edition.js";
import { DifficultyBreakdownPanel } from "../difficulty-breakdown-panel.js";
beforeAll(() => {
Object.defineProperty(globalThis, "matchMedia", {
writable: true,
value: vi.fn().mockImplementation((query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
});
afterEach(cleanup);
const pcId1 = playerCharacterId("pc-1");
const goblinCreature = buildCreature({
id: creatureId("srd:goblin"),
name: "Goblin",
cr: "1/4",
source: "srd",
sourceDisplayName: "SRD",
});
function renderPanel(options: {
encounter: ReturnType<typeof buildEncounter>;
playerCharacters?: PlayerCharacter[];
creatures?: Map<CreatureId, Creature>;
onClose?: () => void;
}) {
const adapters = createTestAdapters({
encounter: options.encounter,
playerCharacters: options.playerCharacters ?? [],
creatures: options.creatures,
});
return render(
<AllProviders adapters={adapters}>
<DifficultyBreakdownPanel onClose={options.onClose ?? (() => {})} />
</AllProviders>,
);
}
function defaultEncounter() {
return buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c-1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c-2"),
name: "Goblin",
creatureId: goblinCreature.id,
}),
buildCombatant({
id: combatantId("c-3"),
name: "Custom Thug",
cr: "2",
}),
buildCombatant({
id: combatantId("c-4"),
name: "Bandit",
}),
],
});
}
const defaultPCs: PlayerCharacter[] = [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 5 },
];
describe("DifficultyBreakdownPanel", () => {
it("renders party budget section", async () => {
renderPanel({
encounter: defaultEncounter(),
playerCharacters: defaultPCs,
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
await waitFor(() => {
expect(
screen.getByText("Party Budget", { exact: false }),
).toBeInTheDocument();
expect(screen.getByText("1 PC", { exact: false })).toBeInTheDocument();
expect(screen.getByText("Low:", { exact: false })).toBeInTheDocument();
});
});
it("renders tier label", async () => {
renderPanel({
encounter: defaultEncounter(),
playerCharacters: defaultPCs,
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
await waitFor(() => {
expect(
screen.getByText("Encounter Difficulty:", { exact: false }),
).toBeInTheDocument();
});
});
it("shows PC in party column with level", async () => {
renderPanel({
encounter: defaultEncounter(),
playerCharacters: defaultPCs,
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
await waitFor(() => {
expect(screen.getByText("Hero")).toBeInTheDocument();
expect(screen.getByText("Lv 5")).toBeInTheDocument();
});
});
it("shows monsters in enemy column", async () => {
renderPanel({
encounter: defaultEncounter(),
playerCharacters: defaultPCs,
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
await waitFor(() => {
expect(screen.getByText("Goblin")).toBeInTheDocument();
expect(screen.getAllByText("CR 1/4").length).toBeGreaterThanOrEqual(1);
});
});
it("renders explanation text", async () => {
renderPanel({
encounter: defaultEncounter(),
playerCharacters: defaultPCs,
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
await waitFor(() => {
expect(
screen.getByText(
"Allied NPC XP is subtracted from encounter difficulty",
),
).toBeInTheDocument();
});
});
it("renders Net Monster XP footer", async () => {
renderPanel({
encounter: defaultEncounter(),
playerCharacters: defaultPCs,
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
await waitFor(() => {
expect(screen.getByText("Net Monster XP")).toBeInTheDocument();
});
});
it("renders custom combatant with CR picker in enemy column", async () => {
renderPanel({
encounter: defaultEncounter(),
playerCharacters: defaultPCs,
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
await waitFor(() => {
const pickers = screen.getAllByLabelText("Challenge rating");
expect(pickers).toHaveLength(2);
expect(pickers[0]).toHaveValue("2");
});
});
it("selecting a CR updates the visible XP value", async () => {
const user = userEvent.setup();
renderPanel({
encounter: defaultEncounter(),
playerCharacters: defaultPCs,
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
await waitFor(() => {
expect(screen.getAllByLabelText("Challenge rating")).toHaveLength(2);
});
const pickers = screen.getAllByLabelText("Challenge rating");
await user.selectOptions(pickers[1], "5");
await waitFor(() => {
expect(screen.getByText("1,800")).toBeInTheDocument();
});
});
it("non-PC combatants show toggle button", async () => {
renderPanel({
encounter: defaultEncounter(),
playerCharacters: defaultPCs,
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
await waitFor(() => {
// Each non-PC enemy combatant has a toggle button
expect(
screen.getByLabelText("Move Goblin to party side"),
).toBeInTheDocument();
expect(
screen.getByLabelText("Move Custom Thug to party side"),
).toBeInTheDocument();
});
});
it("PC combatants do not show side toggle", async () => {
renderPanel({
encounter: defaultEncounter(),
playerCharacters: defaultPCs,
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
await waitFor(() => {
expect(screen.getByText("Hero")).toBeInTheDocument();
});
expect(
screen.queryByLabelText("Move Hero to enemy side"),
).not.toBeInTheDocument();
});
it("side toggle moves combatant between sections", async () => {
const user = userEvent.setup();
renderPanel({
encounter: defaultEncounter(),
playerCharacters: defaultPCs,
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
await waitFor(() => {
expect(screen.getByText("Goblin")).toBeInTheDocument();
});
// Toggle goblin to party side
const toggleBtn = screen.getByLabelText("Move Goblin to party side");
await user.click(toggleBtn);
// After toggle, the aria-label should change to "Move Goblin to enemy side"
await waitFor(() => {
expect(
screen.getByLabelText("Move Goblin to enemy side"),
).toBeInTheDocument();
});
});
it("renders nothing when breakdown data is insufficient", () => {
const { container } = renderPanel({
encounter: buildEncounter({
combatants: [
buildCombatant({ id: combatantId("c-1"), name: "Custom" }),
],
}),
});
expect(container.innerHTML).toBe("");
});
it("shows 4 threshold columns for 2014 edition", async () => {
const { result: editionResult } = renderHook(() => useRulesEdition());
editionResult.current.setEdition("5e");
try {
renderPanel({
encounter: defaultEncounter(),
playerCharacters: defaultPCs,
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
await waitFor(() => {
expect(screen.getByText("Easy:", { exact: false })).toBeInTheDocument();
expect(screen.getByText("Med:", { exact: false })).toBeInTheDocument();
expect(screen.getByText("Hard:", { exact: false })).toBeInTheDocument();
expect(
screen.getByText("Deadly:", { exact: false }),
).toBeInTheDocument();
});
} finally {
editionResult.current.setEdition("5.5e");
}
});
it("shows multiplier and adjusted XP for 2014 edition", async () => {
const { result: editionResult } = renderHook(() => useRulesEdition());
editionResult.current.setEdition("5e");
try {
renderPanel({
encounter: defaultEncounter(),
playerCharacters: defaultPCs,
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
await waitFor(() => {
expect(screen.getByText("Monster XP")).toBeInTheDocument();
// 1 PC (<3) triggers party size adjustment
expect(screen.getByText("Adjusted for 1 PC")).toBeInTheDocument();
});
} finally {
editionResult.current.setEdition("5.5e");
}
});
it("shows Net Monster XP for 5.5e edition (no multiplier)", async () => {
renderPanel({
encounter: defaultEncounter(),
playerCharacters: defaultPCs,
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
await waitFor(() => {
expect(screen.getByText("Net Monster XP")).toBeInTheDocument();
});
});
it("calls onClose when Escape is pressed", async () => {
const user = userEvent.setup();
const onClose = vi.fn();
renderPanel({
encounter: defaultEncounter(),
playerCharacters: defaultPCs,
creatures: new Map([[goblinCreature.id, goblinCreature]]),
onClose,
});
await user.keyboard("{Escape}");
expect(onClose).toHaveBeenCalledOnce();
});
});

View File

@@ -1,8 +1,13 @@
// @vitest-environment jsdom // @vitest-environment jsdom
import type { DifficultyResult } from "@initiative/domain"; import type { DifficultyResult } from "@initiative/domain";
import { cleanup, render, screen } from "@testing-library/react"; import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it } from "vitest"; import userEvent from "@testing-library/user-event";
import { DifficultyIndicator } from "../difficulty-indicator.js"; import { afterEach, describe, expect, it, vi } from "vitest";
import {
DifficultyIndicator,
TIER_LABELS_5_5E,
TIER_LABELS_2014,
} from "../difficulty-indicator.js";
afterEach(cleanup); afterEach(cleanup);
@@ -10,50 +15,114 @@ function makeResult(tier: DifficultyResult["tier"]): DifficultyResult {
return { return {
tier, tier,
totalMonsterXp: 100, totalMonsterXp: 100,
partyBudget: { low: 50, moderate: 100, high: 200 }, thresholds: [
{ label: "Low", value: 50 },
{ label: "Moderate", value: 100 },
{ label: "High", value: 200 },
],
encounterMultiplier: undefined,
adjustedXp: undefined,
partySizeAdjusted: undefined,
}; };
} }
describe("DifficultyIndicator", () => { describe("DifficultyIndicator", () => {
it("renders 3 bars", () => { it("renders 3 bars", () => {
const { container } = render( const { container } = render(
<DifficultyIndicator result={makeResult("moderate")} />, <DifficultyIndicator result={makeResult(2)} labels={TIER_LABELS_5_5E} />,
); );
const bars = container.querySelectorAll("[class*='rounded-sm']"); const bars = container.querySelectorAll("[class*='rounded-sm']");
expect(bars).toHaveLength(3); expect(bars).toHaveLength(3);
}); });
it("shows 'Trivial encounter difficulty' label for trivial tier", () => { it("shows 'Trivial encounter difficulty' for 5.5e tier 0", () => {
render(<DifficultyIndicator result={makeResult("trivial")} />); render(
<DifficultyIndicator result={makeResult(0)} labels={TIER_LABELS_5_5E} />,
);
expect( expect(
screen.getByRole("img", { screen.getByRole("img", { name: "Trivial encounter difficulty" }),
name: "Trivial encounter difficulty",
}),
).toBeDefined(); ).toBeDefined();
}); });
it("shows 'Low encounter difficulty' label for low tier", () => { it("shows 'Low encounter difficulty' for 5.5e tier 1", () => {
render(<DifficultyIndicator result={makeResult("low")} />); render(
<DifficultyIndicator result={makeResult(1)} labels={TIER_LABELS_5_5E} />,
);
expect( expect(
screen.getByRole("img", { name: "Low encounter difficulty" }), screen.getByRole("img", { name: "Low encounter difficulty" }),
).toBeDefined(); ).toBeDefined();
}); });
it("shows 'Moderate encounter difficulty' label for moderate tier", () => { it("shows 'Moderate encounter difficulty' for 5.5e tier 2", () => {
render(<DifficultyIndicator result={makeResult("moderate")} />); render(
<DifficultyIndicator result={makeResult(2)} labels={TIER_LABELS_5_5E} />,
);
expect( expect(
screen.getByRole("img", { screen.getByRole("img", { name: "Moderate encounter difficulty" }),
name: "Moderate encounter difficulty",
}),
).toBeDefined(); ).toBeDefined();
}); });
it("shows 'High encounter difficulty' label for high tier", () => { it("shows 'High encounter difficulty' for 5.5e tier 3", () => {
render(<DifficultyIndicator result={makeResult("high")} />); render(
<DifficultyIndicator result={makeResult(3)} labels={TIER_LABELS_5_5E} />,
);
expect( expect(
screen.getByRole("img", { screen.getByRole("img", { name: "High encounter difficulty" }),
name: "High encounter difficulty",
}),
).toBeDefined(); ).toBeDefined();
}); });
it("shows 'Easy encounter difficulty' for 2014 tier 0", () => {
render(
<DifficultyIndicator result={makeResult(0)} labels={TIER_LABELS_2014} />,
);
expect(
screen.getByRole("img", { name: "Easy encounter difficulty" }),
).toBeDefined();
});
it("shows 'Deadly encounter difficulty' for 2014 tier 3", () => {
render(
<DifficultyIndicator result={makeResult(3)} labels={TIER_LABELS_2014} />,
);
expect(
screen.getByRole("img", { name: "Deadly encounter difficulty" }),
).toBeDefined();
});
it("calls onClick when clicked", async () => {
const user = userEvent.setup();
const handleClick = vi.fn();
render(
<DifficultyIndicator
result={makeResult(2)}
labels={TIER_LABELS_5_5E}
onClick={handleClick}
/>,
);
await user.click(
screen.getByRole("img", { name: "Moderate encounter difficulty" }),
);
expect(handleClick).toHaveBeenCalledOnce();
});
it("renders as div when onClick not provided", () => {
const { container } = render(
<DifficultyIndicator result={makeResult(2)} labels={TIER_LABELS_5_5E} />,
);
const element = container.querySelector("[role='img']");
expect(element?.tagName).toBe("DIV");
});
it("renders as button when onClick provided", () => {
const { container } = render(
<DifficultyIndicator
result={makeResult(2)}
labels={TIER_LABELS_5_5E}
onClick={() => {}}
/>,
);
const element = container.querySelector("[role='img']");
expect(element?.tagName).toBe("BUTTON");
});
}); });

View File

@@ -0,0 +1,280 @@
// @vitest-environment jsdom
import "@testing-library/jest-dom/vitest";
import type { Pf2eCreature } from "@initiative/domain";
import { creatureId } from "@initiative/domain";
import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it } from "vitest";
import { Pf2eStatBlock } from "../pf2e-stat-block.js";
afterEach(cleanup);
const PERCEPTION_SENSES_REGEX = /\+2.*Darkvision/;
const SKILLS_REGEX = /Acrobatics \+5.*Stealth \+5/;
const SAVE_CONDITIONAL_REGEX = /\+12.*\+1 status to all saves vs\. magic/;
const SAVE_CONDITIONAL_ABSENT_REGEX = /status to all saves/;
const HP_DETAILS_REGEX = /115.*regeneration 20/;
const REGEN_REGEX = /regeneration/;
const ATTACK_NAME_REGEX = /Dogslicer/;
const ATTACK_DAMAGE_REGEX = /1d6 slashing/;
const SPELLCASTING_ENTRY_REGEX = /Divine Innate Spells\./;
const ABILITY_MID_NAME_REGEX = /Goblin Scuttle/;
const ABILITY_MID_DESC_REGEX = /The goblin Steps\./;
const CANTRIPS_REGEX = /Cantrips:/;
const AC_REGEX = /16/;
const GOBLIN_WARRIOR: Pf2eCreature = {
system: "pf2e",
id: creatureId("pathfinder-monster-core:goblin-warrior"),
name: "Goblin Warrior",
source: "pathfinder-monster-core",
sourceDisplayName: "Monster Core",
level: -1,
traits: ["small", "goblin", "humanoid"],
perception: 2,
senses: "Darkvision",
languages: "Common, Goblin",
skills: "Acrobatics +5, Athletics +2, Nature +1, Stealth +5",
abilityMods: { str: 0, dex: 3, con: 1, int: 0, wis: -1, cha: 1 },
ac: 16,
saveFort: 5,
saveRef: 7,
saveWill: 3,
hp: 6,
speed: "25 feet",
attacks: [
{
name: "Dogslicer",
activity: { number: 1, unit: "action" },
segments: [
{
type: "text",
value: "+7 (agile, backstabber, finesse), 1d6 slashing",
},
],
},
],
abilitiesMid: [
{
name: "Goblin Scuttle",
activity: { number: 1, unit: "reaction" },
segments: [{ type: "text", value: "The goblin Steps." }],
},
],
};
const NAUNET: Pf2eCreature = {
system: "pf2e",
id: creatureId("pathfinder-monster-core-2:naunet"),
name: "Naunet",
source: "pathfinder-monster-core-2",
sourceDisplayName: "Monster Core 2",
level: 7,
traits: ["large", "monitor", "protean"],
perception: 14,
senses: "Darkvision",
languages: "Chthonian, Empyrean, Protean",
skills:
"Acrobatics +14, Athletics +16, Intimidation +16, Stealth +14, Survival +12",
abilityMods: { str: 5, dex: 3, con: 5, int: 0, wis: 3, cha: 3 },
ac: 24,
saveFort: 18,
saveRef: 14,
saveWill: 12,
saveConditional: "+1 status to all saves vs. magic",
hp: 120,
resistances: "Precision 5, Protean anatomy 10",
speed: "25 feet, Fly 30 feet, Swim 25 feet (unfettered movement)",
spellcasting: [
{
name: "Divine Innate Spells",
headerText: "DC 25, attack +17",
daily: [
{ uses: 4, each: true, spells: ["Unfettered Movement (Constant)"] },
],
atWill: ["Detect Magic"],
},
],
};
const TROLL: Pf2eCreature = {
system: "pf2e",
id: creatureId("pathfinder-monster-core:forest-troll"),
name: "Forest Troll",
source: "pathfinder-monster-core",
sourceDisplayName: "Monster Core",
level: 5,
traits: ["large", "giant", "troll"],
perception: 11,
senses: "Darkvision",
languages: "Jotun",
skills: "Athletics +12, Intimidation +12",
abilityMods: { str: 5, dex: 2, con: 6, int: -2, wis: 0, cha: -2 },
ac: 20,
saveFort: 17,
saveRef: 11,
saveWill: 7,
hp: 115,
hpDetails: "regeneration 20 (deactivated by acid or fire)",
weaknesses: "Fire 10",
speed: "30 feet",
};
function renderStatBlock(creature: Pf2eCreature) {
return render(<Pf2eStatBlock creature={creature} />);
}
describe("Pf2eStatBlock", () => {
describe("header", () => {
it("renders creature name and level", () => {
renderStatBlock(GOBLIN_WARRIOR);
expect(
screen.getByRole("heading", { name: "Goblin Warrior" }),
).toBeInTheDocument();
expect(screen.getByText("Level -1")).toBeInTheDocument();
});
it("renders traits as tags", () => {
renderStatBlock(GOBLIN_WARRIOR);
expect(screen.getByText("Small")).toBeInTheDocument();
expect(screen.getByText("Goblin")).toBeInTheDocument();
expect(screen.getByText("Humanoid")).toBeInTheDocument();
});
it("renders source display name", () => {
renderStatBlock(GOBLIN_WARRIOR);
expect(screen.getByText("Monster Core")).toBeInTheDocument();
});
});
describe("perception and senses", () => {
it("renders perception modifier and senses", () => {
renderStatBlock(GOBLIN_WARRIOR);
expect(screen.getByText("Perception")).toBeInTheDocument();
expect(screen.getByText(PERCEPTION_SENSES_REGEX)).toBeInTheDocument();
});
it("renders languages", () => {
renderStatBlock(GOBLIN_WARRIOR);
expect(screen.getByText("Languages")).toBeInTheDocument();
expect(screen.getByText("Common, Goblin")).toBeInTheDocument();
});
it("renders skills", () => {
renderStatBlock(GOBLIN_WARRIOR);
expect(screen.getByText("Skills")).toBeInTheDocument();
expect(screen.getByText(SKILLS_REGEX)).toBeInTheDocument();
});
});
describe("ability modifiers", () => {
it("renders all six ability labels", () => {
renderStatBlock(GOBLIN_WARRIOR);
for (const label of ["Str", "Dex", "Con", "Int", "Wis", "Cha"]) {
expect(screen.getByText(label)).toBeInTheDocument();
}
});
it("renders positive and negative modifiers", () => {
renderStatBlock(GOBLIN_WARRIOR);
expect(screen.getByText("+3")).toBeInTheDocument();
expect(screen.getByText("-1")).toBeInTheDocument();
});
});
describe("defenses", () => {
it("renders AC and saves", () => {
renderStatBlock(GOBLIN_WARRIOR);
expect(screen.getByText("AC")).toBeInTheDocument();
expect(screen.getByText(AC_REGEX)).toBeInTheDocument();
expect(screen.getByText("Fort")).toBeInTheDocument();
expect(screen.getByText("Ref")).toBeInTheDocument();
expect(screen.getByText("Will")).toBeInTheDocument();
});
it("renders HP", () => {
renderStatBlock(GOBLIN_WARRIOR);
expect(screen.getByText("HP")).toBeInTheDocument();
expect(screen.getByText("6")).toBeInTheDocument();
});
it("renders saveConditional inline with saves", () => {
renderStatBlock(NAUNET);
expect(screen.getByText(SAVE_CONDITIONAL_REGEX)).toBeInTheDocument();
});
it("omits saveConditional when absent", () => {
renderStatBlock(GOBLIN_WARRIOR);
expect(
screen.queryByText(SAVE_CONDITIONAL_ABSENT_REGEX),
).not.toBeInTheDocument();
});
it("renders hpDetails in parentheses after HP", () => {
renderStatBlock(TROLL);
expect(screen.getByText(HP_DETAILS_REGEX)).toBeInTheDocument();
});
it("omits hpDetails when absent", () => {
renderStatBlock(GOBLIN_WARRIOR);
expect(screen.queryByText(REGEN_REGEX)).not.toBeInTheDocument();
});
it("renders resistances and weaknesses", () => {
renderStatBlock(NAUNET);
expect(screen.getByText("Resistances")).toBeInTheDocument();
expect(
screen.getByText("Precision 5, Protean anatomy 10"),
).toBeInTheDocument();
});
});
describe("abilities", () => {
it("renders mid (defensive) abilities", () => {
renderStatBlock(GOBLIN_WARRIOR);
expect(screen.getByText(ABILITY_MID_NAME_REGEX)).toBeInTheDocument();
expect(screen.getByText(ABILITY_MID_DESC_REGEX)).toBeInTheDocument();
});
});
describe("speed and attacks", () => {
it("renders speed", () => {
renderStatBlock(GOBLIN_WARRIOR);
expect(screen.getByText("Speed")).toBeInTheDocument();
expect(screen.getByText("25 feet")).toBeInTheDocument();
});
it("renders attacks", () => {
renderStatBlock(GOBLIN_WARRIOR);
expect(screen.getByText(ATTACK_NAME_REGEX)).toBeInTheDocument();
expect(screen.getByText(ATTACK_DAMAGE_REGEX)).toBeInTheDocument();
});
});
describe("spellcasting", () => {
it("renders spellcasting entry with header", () => {
renderStatBlock(NAUNET);
expect(screen.getByText(SPELLCASTING_ENTRY_REGEX)).toBeInTheDocument();
expect(screen.getByText("DC 25, attack +17")).toBeInTheDocument();
});
it("renders ranked spells", () => {
renderStatBlock(NAUNET);
expect(screen.getByText("Rank 4:")).toBeInTheDocument();
expect(
screen.getByText("Unfettered Movement (Constant)"),
).toBeInTheDocument();
});
it("renders cantrips", () => {
renderStatBlock(NAUNET);
expect(screen.getByText("Cantrips:")).toBeInTheDocument();
expect(screen.getByText("Detect Magic")).toBeInTheDocument();
});
it("omits spellcasting when absent", () => {
renderStatBlock(GOBLIN_WARRIOR);
expect(screen.queryByText(CANTRIPS_REGEX)).not.toBeInTheDocument();
});
});
});

View File

@@ -33,32 +33,6 @@ beforeAll(() => {
afterEach(cleanup); afterEach(cleanup);
vi.mock("../../persistence/encounter-storage.js", () => ({
loadEncounter: () => null,
saveEncounter: () => {},
}));
vi.mock("../../persistence/player-character-storage.js", () => ({
loadPlayerCharacters: () => [],
savePlayerCharacters: () => {},
}));
vi.mock("../../adapters/bestiary-cache.js", () => ({
loadAllCachedCreatures: () => Promise.resolve(new Map()),
isSourceCached: () => Promise.resolve(false),
cacheSource: () => Promise.resolve(),
getCachedSources: () => Promise.resolve([]),
clearSource: () => Promise.resolve(),
clearAll: () => Promise.resolve(),
}));
vi.mock("../../adapters/bestiary-index-adapter.js", () => ({
loadBestiaryIndex: () => ({ sources: {}, creatures: [] }),
getAllSourceCodes: () => [],
getDefaultFetchUrl: () => "",
getSourceDisplayName: (code: string) => code,
}));
function renderSection() { function renderSection() {
const ref = createRef<PlayerCharacterSectionHandle>(); const ref = createRef<PlayerCharacterSectionHandle>();
const result = render(<PlayerCharacterSection ref={ref} />, { const result = render(<PlayerCharacterSection ref={ref} />, {

View File

@@ -28,32 +28,6 @@ beforeAll(() => {
}); });
}); });
vi.mock("../../persistence/encounter-storage.js", () => ({
loadEncounter: () => null,
saveEncounter: () => {},
}));
vi.mock("../../persistence/player-character-storage.js", () => ({
loadPlayerCharacters: () => [],
savePlayerCharacters: () => {},
}));
vi.mock("../../adapters/bestiary-cache.js", () => ({
loadAllCachedCreatures: () => Promise.resolve(new Map()),
isSourceCached: () => Promise.resolve(false),
cacheSource: () => Promise.resolve(),
getCachedSources: () => Promise.resolve([]),
clearSource: () => Promise.resolve(),
clearAll: () => Promise.resolve(),
}));
vi.mock("../../adapters/bestiary-index-adapter.js", () => ({
loadBestiaryIndex: () => ({ sources: {}, creatures: [] }),
getAllSourceCodes: () => [],
getDefaultFetchUrl: () => "",
getSourceDisplayName: (code: string) => code,
}));
function renderModal(open = true) { function renderModal(open = true) {
const onClose = vi.fn(); const onClose = vi.fn();
const result = render(<SettingsModal open={open} onClose={onClose} />, { const result = render(<SettingsModal open={open} onClose={onClose} />, {
@@ -63,14 +37,18 @@ function renderModal(open = true) {
} }
describe("SettingsModal", () => { describe("SettingsModal", () => {
it("renders edition toggle buttons", () => { it("renders game system section with all three options", () => {
renderModal(); renderModal();
expect(screen.getByText("Game System")).toBeInTheDocument();
expect( expect(
screen.getByRole("button", { name: "5e (2014)" }), screen.getByRole("button", { name: "5e (2014)" }),
).toBeInTheDocument(); ).toBeInTheDocument();
expect( expect(
screen.getByRole("button", { name: "5.5e (2024)" }), screen.getByRole("button", { name: "5.5e (2024)" }),
).toBeInTheDocument(); ).toBeInTheDocument();
expect(
screen.getByRole("button", { name: "Pathfinder 2e" }),
).toBeInTheDocument();
}); });
it("renders theme toggle buttons", () => { it("renders theme toggle buttons", () => {

View File

@@ -4,6 +4,9 @@ import "@testing-library/jest-dom/vitest";
import { cleanup, render, screen, waitFor } from "@testing-library/react"; import { cleanup, render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event"; import userEvent from "@testing-library/user-event";
import { afterEach, describe, expect, it, vi } from "vitest"; import { afterEach, describe, expect, it, vi } from "vitest";
import { createTestAdapters } from "../../__tests__/adapters/in-memory-adapters.js";
import { AdapterProvider } from "../../contexts/adapter-context.js";
import { RulesEditionProvider } from "../../contexts/rules-edition-context.js";
import { SourceFetchPrompt } from "../source-fetch-prompt.js"; import { SourceFetchPrompt } from "../source-fetch-prompt.js";
const MONSTER_MANUAL_REGEX = /Monster Manual/; const MONSTER_MANUAL_REGEX = /Monster Manual/;
@@ -13,6 +16,9 @@ afterEach(cleanup);
const mockFetchAndCacheSource = vi.fn(); const mockFetchAndCacheSource = vi.fn();
const mockUploadAndCacheSource = vi.fn(); const mockUploadAndCacheSource = vi.fn();
// Uses context mock because fetchAndCacheSource/uploadAndCacheSource involve
// real fetch() calls. The test controls success/failure to verify the
// component's loading and error UI, not the fetching logic itself.
vi.mock("../../contexts/bestiary-context.js", () => ({ vi.mock("../../contexts/bestiary-context.js", () => ({
useBestiaryContext: () => ({ useBestiaryContext: () => ({
fetchAndCacheSource: mockFetchAndCacheSource, fetchAndCacheSource: mockFetchAndCacheSource,
@@ -20,22 +26,25 @@ vi.mock("../../contexts/bestiary-context.js", () => ({
}), }),
})); }));
vi.mock("../../adapters/bestiary-index-adapter.js", () => ({ function renderPrompt(sourceCode = "MM") {
const onSourceLoaded = vi.fn();
const adapters = createTestAdapters();
adapters.bestiaryIndex = {
...adapters.bestiaryIndex,
getDefaultFetchUrl: (code: string) => getDefaultFetchUrl: (code: string) =>
`https://example.com/bestiary/${code}.json`, `https://example.com/bestiary/${code}.json`,
getSourceDisplayName: (code: string) => getSourceDisplayName: (code: string) =>
code === "MM" ? "Monster Manual" : code, code === "MM" ? "Monster Manual" : code,
loadBestiaryIndex: () => ({ sources: {}, creatures: [] }), };
getAllSourceCodes: () => [],
}));
function renderPrompt(sourceCode = "MM") {
const onSourceLoaded = vi.fn();
const result = render( const result = render(
<RulesEditionProvider>
<AdapterProvider adapters={adapters}>
<SourceFetchPrompt <SourceFetchPrompt
sourceCode={sourceCode} sourceCode={sourceCode}
onSourceLoaded={onSourceLoaded} onSourceLoaded={onSourceLoaded}
/>, />
</AdapterProvider>
</RulesEditionProvider>,
); );
return { ...result, onSourceLoaded }; return { ...result, onSourceLoaded };
} }

View File

@@ -3,60 +3,68 @@ import "@testing-library/jest-dom/vitest";
import { cleanup, render, screen, waitFor } from "@testing-library/react"; import { cleanup, render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event"; import userEvent from "@testing-library/user-event";
import { afterEach, describe, expect, it, vi } from "vitest"; import type { ReactNode } from "react";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
vi.mock("../../adapters/bestiary-cache.js", () => ({ import { createTestAdapters } from "../../__tests__/adapters/in-memory-adapters.js";
getCachedSources: vi.fn(), import { AllProviders } from "../../__tests__/test-providers.js";
clearSource: vi.fn(), import type { CachedSourceInfo } from "../../adapters/ports.js";
clearAll: vi.fn(),
}));
// Mock the context module
vi.mock("../../contexts/bestiary-context.js", () => ({
useBestiaryContext: vi.fn(),
}));
import * as bestiaryCache from "../../adapters/bestiary-cache.js";
import { useBestiaryContext } from "../../contexts/bestiary-context.js";
import { SourceManager } from "../source-manager.js"; import { SourceManager } from "../source-manager.js";
const mockGetCachedSources = vi.mocked(bestiaryCache.getCachedSources); beforeAll(() => {
const mockClearSource = vi.mocked(bestiaryCache.clearSource); Object.defineProperty(globalThis, "matchMedia", {
const mockClearAll = vi.mocked(bestiaryCache.clearAll); writable: true,
const mockUseBestiaryContext = vi.mocked(useBestiaryContext); value: vi.fn().mockImplementation((query: string) => ({
matches: false,
afterEach(() => { media: query,
cleanup(); onchange: null,
vi.clearAllMocks(); addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
}); });
function setupMockContext() { afterEach(cleanup);
const refreshCache = vi.fn().mockResolvedValue(undefined);
mockUseBestiaryContext.mockReturnValue({ function renderWithSources(sources: CachedSourceInfo[] = []) {
refreshCache, const adapters = createTestAdapters();
search: vi.fn().mockReturnValue([]), // Wire getCachedSources to return the provided sources initially,
getCreature: vi.fn(), // then empty after clear operations
isLoaded: true, let currentSources = [...sources];
isSourceCached: vi.fn().mockResolvedValue(false), adapters.bestiaryCache = {
fetchAndCacheSource: vi.fn(), ...adapters.bestiaryCache,
uploadAndCacheSource: vi.fn(), getCachedSources: () => Promise.resolve(currentSources),
} as ReturnType<typeof useBestiaryContext>); clearSource(_system, sourceCode) {
return { refreshCache }; currentSources = currentSources.filter(
(s) => s.sourceCode !== sourceCode,
);
return Promise.resolve();
},
clearAll() {
currentSources = [];
return Promise.resolve();
},
};
render(<SourceManager />, {
wrapper: ({ children }: { children: ReactNode }) => (
<AllProviders adapters={adapters}>{children}</AllProviders>
),
});
} }
describe("SourceManager", () => { describe("SourceManager", () => {
it("shows 'No cached sources' empty state when no sources", async () => { it("shows 'No cached sources' empty state when no sources", async () => {
setupMockContext(); void renderWithSources([]);
mockGetCachedSources.mockResolvedValue([]);
render(<SourceManager />);
await waitFor(() => { await waitFor(() => {
expect(screen.getByText("No cached sources")).toBeInTheDocument(); expect(screen.getByText("No cached sources")).toBeInTheDocument();
}); });
}); });
it("lists cached sources with display name and creature count", async () => { it("lists cached sources with display name and creature count", async () => {
setupMockContext(); void renderWithSources([
mockGetCachedSources.mockResolvedValue([
{ {
sourceCode: "mm", sourceCode: "mm",
displayName: "Monster Manual", displayName: "Monster Manual",
@@ -70,7 +78,6 @@ describe("SourceManager", () => {
cachedAt: Date.now(), cachedAt: Date.now(),
}, },
]); ]);
render(<SourceManager />);
await waitFor(() => { await waitFor(() => {
expect(screen.getByText("Monster Manual")).toBeInTheDocument(); expect(screen.getByText("Monster Manual")).toBeInTheDocument();
}); });
@@ -79,38 +86,31 @@ describe("SourceManager", () => {
expect(screen.getByText("100 creatures")).toBeInTheDocument(); expect(screen.getByText("100 creatures")).toBeInTheDocument();
}); });
it("Clear All button calls cache clear and refreshCache", async () => { it("Clear All button removes all sources", async () => {
const user = userEvent.setup(); const user = userEvent.setup();
const { refreshCache } = setupMockContext(); void renderWithSources([
mockGetCachedSources
.mockResolvedValueOnce([
{ {
sourceCode: "mm", sourceCode: "mm",
displayName: "Monster Manual", displayName: "Monster Manual",
creatureCount: 300, creatureCount: 300,
cachedAt: Date.now(), cachedAt: Date.now(),
}, },
]) ]);
.mockResolvedValue([]);
mockClearAll.mockResolvedValue(undefined);
render(<SourceManager />);
await waitFor(() => { await waitFor(() => {
expect(screen.getByText("Monster Manual")).toBeInTheDocument(); expect(screen.getByText("Monster Manual")).toBeInTheDocument();
}); });
await user.click(screen.getByRole("button", { name: "Clear All" })); await user.click(screen.getByRole("button", { name: "Clear All" }));
await waitFor(() => { await waitFor(() => {
expect(mockClearAll).toHaveBeenCalled(); expect(screen.getByText("No cached sources")).toBeInTheDocument();
}); });
expect(refreshCache).toHaveBeenCalled();
}); });
it("individual source delete button calls clear for that source", async () => { it("individual source delete button removes that source", async () => {
const user = userEvent.setup(); const user = userEvent.setup();
const { refreshCache } = setupMockContext(); void renderWithSources([
mockGetCachedSources
.mockResolvedValueOnce([
{ {
sourceCode: "mm", sourceCode: "mm",
displayName: "Monster Manual", displayName: "Monster Manual",
@@ -123,18 +123,8 @@ describe("SourceManager", () => {
creatureCount: 100, creatureCount: 100,
cachedAt: Date.now(), cachedAt: Date.now(),
}, },
])
.mockResolvedValue([
{
sourceCode: "vgm",
displayName: "Volo's Guide",
creatureCount: 100,
cachedAt: Date.now(),
},
]); ]);
mockClearSource.mockResolvedValue(undefined);
render(<SourceManager />);
await waitFor(() => { await waitFor(() => {
expect(screen.getByText("Monster Manual")).toBeInTheDocument(); expect(screen.getByText("Monster Manual")).toBeInTheDocument();
}); });
@@ -142,9 +132,10 @@ describe("SourceManager", () => {
await user.click( await user.click(
screen.getByRole("button", { name: "Remove Monster Manual" }), screen.getByRole("button", { name: "Remove Monster Manual" }),
); );
await waitFor(() => { await waitFor(() => {
expect(mockClearSource).toHaveBeenCalledWith("mm"); expect(screen.queryByText("Monster Manual")).not.toBeInTheDocument();
}); });
expect(refreshCache).toHaveBeenCalled(); expect(screen.getByText("Volo's Guide")).toBeInTheDocument();
}); });
}); });

View File

@@ -5,7 +5,7 @@ import type { Creature } from "@initiative/domain";
import { creatureId } from "@initiative/domain"; import { creatureId } from "@initiative/domain";
import { cleanup, render, screen } from "@testing-library/react"; import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it } from "vitest"; import { afterEach, describe, expect, it } from "vitest";
import { StatBlock } from "../stat-block.js"; import { DndStatBlock as StatBlock } from "../dnd-stat-block.js";
afterEach(cleanup); afterEach(cleanup);
@@ -46,10 +46,30 @@ const GOBLIN: Creature = {
skills: "Stealth +6", skills: "Stealth +6",
senses: "darkvision 60 ft., passive Perception 9", senses: "darkvision 60 ft., passive Perception 9",
languages: "Common, Goblin", languages: "Common, Goblin",
traits: [{ name: "Nimble Escape", text: "Disengage or Hide as bonus." }], traits: [
actions: [{ name: "Scimitar", text: "Melee: +4 to hit, 5 slashing." }], {
bonusActions: [{ name: "Nimble", text: "Disengage or Hide." }], name: "Nimble Escape",
reactions: [{ name: "Redirect", text: "Redirect attack to ally." }], segments: [{ type: "text", value: "Disengage or Hide as bonus." }],
},
],
actions: [
{
name: "Scimitar",
segments: [{ type: "text", value: "Melee: +4 to hit, 5 slashing." }],
},
],
bonusActions: [
{
name: "Nimble",
segments: [{ type: "text", value: "Disengage or Hide." }],
},
],
reactions: [
{
name: "Redirect",
segments: [{ type: "text", value: "Redirect attack to ally." }],
},
],
}; };
const DRAGON: Creature = { const DRAGON: Creature = {
@@ -75,8 +95,16 @@ const DRAGON: Creature = {
legendaryActions: { legendaryActions: {
preamble: "The dragon can take 3 legendary actions.", preamble: "The dragon can take 3 legendary actions.",
entries: [ entries: [
{ name: "Detect", text: "Wisdom (Perception) check." }, {
{ name: "Tail Attack", text: "Tail attack." }, name: "Detect",
segments: [
{ type: "text" as const, value: "Wisdom (Perception) check." },
],
},
{
name: "Tail Attack",
segments: [{ type: "text" as const, value: "Tail attack." }],
},
], ],
}, },
spellcasting: [ spellcasting: [

View File

@@ -1,100 +1,68 @@
// @vitest-environment jsdom // @vitest-environment jsdom
import "@testing-library/jest-dom/vitest"; import "@testing-library/jest-dom/vitest";
import type { Encounter } from "@initiative/domain";
import { combatantId } from "@initiative/domain"; import { combatantId } from "@initiative/domain";
import { cleanup, render, screen } from "@testing-library/react"; import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest"; import type { ReactNode } from "react";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
// Mock the context modules import { createTestAdapters } from "../../__tests__/adapters/in-memory-adapters.js";
vi.mock("../../contexts/encounter-context.js", () => ({ import {
useEncounterContext: vi.fn(), buildCombatant,
})); buildEncounter,
} from "../../__tests__/factories/index.js";
vi.mock("../../contexts/player-characters-context.js", () => ({ import { AllProviders } from "../../__tests__/test-providers.js";
usePlayerCharactersContext: vi.fn().mockReturnValue({ characters: [] }),
}));
vi.mock("../../contexts/bestiary-context.js", () => ({
useBestiaryContext: vi.fn().mockReturnValue({ getCreature: () => undefined }),
}));
import { useEncounterContext } from "../../contexts/encounter-context.js";
import { TurnNavigation } from "../turn-navigation.js"; import { TurnNavigation } from "../turn-navigation.js";
const mockUseEncounterContext = vi.mocked(useEncounterContext); beforeAll(() => {
Object.defineProperty(globalThis, "matchMedia", {
afterEach(() => { writable: true,
cleanup(); value: vi.fn().mockImplementation((query: string) => ({
vi.clearAllMocks(); matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
}); });
function mockContext(overrides: Partial<Encounter> = {}) { afterEach(cleanup);
const encounter: Encounter = {
combatants: [
{ id: combatantId("1"), name: "Goblin" },
{ id: combatantId("2"), name: "Conjurer" },
],
activeIndex: 0,
roundNumber: 1,
...overrides,
};
const value = { function renderNav(encounter = buildEncounter()) {
encounter, const adapters = createTestAdapters({ encounter });
advanceTurn: vi.fn(), return render(<TurnNavigation />, {
retreatTurn: vi.fn(), wrapper: ({ children }: { children: ReactNode }) => (
clearEncounter: vi.fn(), <AllProviders adapters={adapters}>{children}</AllProviders>
isEmpty: encounter.combatants.length === 0, ),
hasCreatureCombatants: false, });
canRollAllInitiative: false,
addCombatant: vi.fn(),
removeCombatant: vi.fn(),
editCombatant: vi.fn(),
setInitiative: vi.fn(),
setHp: vi.fn(),
adjustHp: vi.fn(),
setTempHp: vi.fn(),
hasTempHp: false,
setAc: vi.fn(),
toggleCondition: vi.fn(),
toggleConcentration: vi.fn(),
addFromBestiary: vi.fn(),
addMultipleFromBestiary: vi.fn(),
addFromPlayerCharacter: vi.fn(),
makeStore: vi.fn(),
withUndo: vi.fn((action: () => unknown) => action()),
undo: vi.fn(),
redo: vi.fn(),
canUndo: false,
canRedo: false,
undoRedoState: { undoStack: [], redoStack: [] },
setEncounter: vi.fn(),
setUndoRedoState: vi.fn(),
events: [],
lastCreatureId: null,
};
mockUseEncounterContext.mockReturnValue(
value as ReturnType<typeof useEncounterContext>,
);
return value;
}
function renderNav(overrides: Partial<Encounter> = {}) {
mockContext(overrides);
return render(<TurnNavigation />);
} }
describe("TurnNavigation", () => { describe("TurnNavigation", () => {
describe("US1: Round badge and combatant name", () => { describe("US1: Round badge and combatant name", () => {
it("renders the round badge with correct round number", () => { it("renders the round badge with correct round number", () => {
renderNav({ roundNumber: 3 }); renderNav(
buildEncounter({
combatants: [buildCombatant({ name: "Goblin" })],
roundNumber: 3,
}),
);
expect(screen.getByText("R3")).toBeInTheDocument(); expect(screen.getByText("R3")).toBeInTheDocument();
}); });
it("renders the combatant name separately from the round badge", () => { it("renders the combatant name separately from the round badge", () => {
renderNav(); renderNav(
buildEncounter({
combatants: [
buildCombatant({ id: combatantId("c-1"), name: "Goblin" }),
buildCombatant({ id: combatantId("c-2"), name: "Conjurer" }),
],
activeIndex: 0,
roundNumber: 1,
}),
);
const badge = screen.getByText("R1"); const badge = screen.getByText("R1");
const name = screen.getByText("Goblin"); const name = screen.getByText("Goblin");
expect(badge).toBeInTheDocument(); expect(badge).toBeInTheDocument();
@@ -104,41 +72,24 @@ describe("TurnNavigation", () => {
}); });
it("does not render an em dash between round and name", () => { it("does not render an em dash between round and name", () => {
const { container } = renderNav(); const { container } = renderNav(
buildEncounter({
combatants: [buildCombatant({ name: "Goblin" })],
}),
);
expect(container.textContent).not.toContain("\u2014"); expect(container.textContent).not.toContain("\u2014");
}); });
it("round badge and combatant name are siblings in the center area", () => { it("round badge is in the left zone and name is in the center zone", () => {
renderNav(); renderNav(
buildEncounter({
combatants: [buildCombatant({ name: "Goblin" })],
}),
);
const badge = screen.getByText("R1"); const badge = screen.getByText("R1");
const name = screen.getByText("Goblin"); const name = screen.getByText("Goblin");
// badge text is inside inner span > outer span, name is a direct child // Badge and name are in separate grid cells to prevent layout shifts
expect(badge.closest(".flex")).toBe(name.parentElement); expect(badge.parentElement).not.toBe(name.parentElement);
});
it("updates the round badge when round changes", () => {
mockContext({ roundNumber: 2 });
const { rerender } = render(<TurnNavigation />);
expect(screen.getByText("R2")).toBeInTheDocument();
mockContext({ roundNumber: 3 });
rerender(<TurnNavigation />);
expect(screen.getByText("R3")).toBeInTheDocument();
expect(screen.queryByText("R2")).not.toBeInTheDocument();
});
it("renders the next combatant name when turn advances", () => {
const combatants = [
{ id: combatantId("1"), name: "Goblin" },
{ id: combatantId("2"), name: "Conjurer" },
];
mockContext({ combatants, activeIndex: 0 });
const { rerender } = render(<TurnNavigation />);
expect(screen.getByText("Goblin")).toBeInTheDocument();
mockContext({ combatants, activeIndex: 1 });
rerender(<TurnNavigation />);
expect(screen.getByText("Conjurer")).toBeInTheDocument();
}); });
}); });
@@ -146,17 +97,21 @@ describe("TurnNavigation", () => {
it("applies truncation styles to long combatant names", () => { it("applies truncation styles to long combatant names", () => {
const longName = const longName =
"Ancient Red Dragon Wyrm of the Northern Wastes and Beyond"; "Ancient Red Dragon Wyrm of the Northern Wastes and Beyond";
renderNav({ renderNav(
combatants: [{ id: combatantId("1"), name: longName }], buildEncounter({
}); combatants: [buildCombatant({ name: longName })],
}),
);
const nameEl = screen.getByText(longName); const nameEl = screen.getByText(longName);
expect(nameEl.className).toContain("truncate"); expect(nameEl.className).toContain("truncate");
}); });
it("renders three-zone layout with a single-character name", () => { it("renders three-zone layout with a single-character name", () => {
renderNav({ renderNav(
combatants: [{ id: combatantId("1"), name: "O" }], buildEncounter({
}); combatants: [buildCombatant({ name: "O" })],
}),
);
expect(screen.getByText("R1")).toBeInTheDocument(); expect(screen.getByText("R1")).toBeInTheDocument();
expect(screen.getByText("O")).toBeInTheDocument(); expect(screen.getByText("O")).toBeInTheDocument();
expect( expect(
@@ -169,9 +124,11 @@ describe("TurnNavigation", () => {
it("keeps all action buttons accessible regardless of name length", () => { it("keeps all action buttons accessible regardless of name length", () => {
const longName = "A".repeat(60); const longName = "A".repeat(60);
renderNav({ renderNav(
combatants: [{ id: combatantId("1"), name: longName }], buildEncounter({
}); combatants: [buildCombatant({ name: longName })],
}),
);
expect( expect(
screen.getByRole("button", { name: "Previous turn" }), screen.getByRole("button", { name: "Previous turn" }),
).toBeInTheDocument(); ).toBeInTheDocument();
@@ -182,29 +139,30 @@ describe("TurnNavigation", () => {
it("renders a 40-character name without truncation class issues", () => { it("renders a 40-character name without truncation class issues", () => {
const name40 = "A".repeat(40); const name40 = "A".repeat(40);
renderNav({ renderNav(
combatants: [{ id: combatantId("1"), name: name40 }], buildEncounter({
}); combatants: [buildCombatant({ name: name40 })],
}),
);
const nameEl = screen.getByText(name40); const nameEl = screen.getByText(name40);
expect(nameEl).toBeInTheDocument(); expect(nameEl).toBeInTheDocument();
// The truncate class is applied but CSS only visually truncates if content overflows
expect(nameEl.className).toContain("truncate"); expect(nameEl.className).toContain("truncate");
}); });
}); });
describe("US3: No combatants state", () => { describe("US3: No combatants state", () => {
it("shows the round badge when there are no combatants", () => { it("shows the round badge when there are no combatants", () => {
renderNav({ combatants: [], roundNumber: 1 }); renderNav(buildEncounter({ combatants: [], roundNumber: 1 }));
expect(screen.getByText("R1")).toBeInTheDocument(); expect(screen.getByText("R1")).toBeInTheDocument();
}); });
it("shows 'No combatants' placeholder text", () => { it("shows 'No combatants' placeholder text", () => {
renderNav({ combatants: [] }); renderNav(buildEncounter({ combatants: [] }));
expect(screen.getByText("No combatants")).toBeInTheDocument(); expect(screen.getByText("No combatants")).toBeInTheDocument();
}); });
it("disables navigation buttons when there are no combatants", () => { it("disables navigation buttons when there are no combatants", () => {
renderNav({ combatants: [] }); renderNav(buildEncounter({ combatants: [] }));
expect( expect(
screen.getByRole("button", { name: "Previous turn" }), screen.getByRole("button", { name: "Previous turn" }),
).toBeDisabled(); ).toBeDisabled();

View File

@@ -12,27 +12,20 @@ import {
Upload, Upload,
Users, Users,
} from "lucide-react"; } from "lucide-react";
import React, { type RefObject, useCallback, useRef, useState } from "react"; import React, { type RefObject, useCallback, useState } from "react";
import type { SearchResult } from "../contexts/bestiary-context.js"; import type { SearchResult } from "../contexts/bestiary-context.js";
import { useBulkImportContext } from "../contexts/bulk-import-context.js"; import { useBulkImportContext } from "../contexts/bulk-import-context.js";
import { useEncounterContext } from "../contexts/encounter-context.js"; import { useEncounterContext } from "../contexts/encounter-context.js";
import { useInitiativeRollsContext } from "../contexts/initiative-rolls-context.js"; import { useInitiativeRollsContext } from "../contexts/initiative-rolls-context.js";
import { usePlayerCharactersContext } from "../contexts/player-characters-context.js";
import { import {
creatureKey, creatureKey,
type QueuedCreature, type QueuedCreature,
type SuggestionActions, type SuggestionActions,
useActionBarState, useActionBarState,
} from "../hooks/use-action-bar-state.js"; } from "../hooks/use-action-bar-state.js";
import { useEncounterExportImport } from "../hooks/use-encounter-export-import.js";
import { useLongPress } from "../hooks/use-long-press.js"; import { useLongPress } from "../hooks/use-long-press.js";
import { cn } from "../lib/utils.js"; import { cn } from "../lib/utils.js";
import {
assembleExportBundle,
bundleToJson,
readImportFile,
triggerDownload,
validateImportBundle,
} from "../persistence/export-import.js";
import { D20Icon } from "./d20-icon.js"; import { D20Icon } from "./d20-icon.js";
import { ExportMethodDialog } from "./export-method-dialog.js"; import { ExportMethodDialog } from "./export-method-dialog.js";
import { ImportConfirmDialog } from "./import-confirm-prompt.js"; import { ImportConfirmDialog } from "./import-confirm-prompt.js";
@@ -439,116 +432,23 @@ export function ActionBar({
} = useActionBarState(); } = useActionBarState();
const { state: bulkImportState } = useBulkImportContext(); const { state: bulkImportState } = useBulkImportContext();
const { const {
encounter, importError,
undoRedoState, showExportMethod,
isEmpty: encounterIsEmpty, showImportMethod,
setEncounter, showImportConfirm,
setUndoRedoState, importFileRef,
} = useEncounterContext(); setImportError,
const { characters: playerCharacters, replacePlayerCharacters } = setShowExportMethod,
usePlayerCharactersContext(); setShowImportMethod,
handleExportDownload,
const importFileRef = useRef<HTMLInputElement>(null); handleExportClipboard,
const [importError, setImportError] = useState<string | null>(null); handleImportFile,
const [showExportMethod, setShowExportMethod] = useState(false); handleImportClipboard,
const [showImportMethod, setShowImportMethod] = useState(false); handleImportConfirm,
const [showImportConfirm, setShowImportConfirm] = useState(false); handleImportCancel,
const pendingBundleRef = useRef< } = useEncounterExportImport();
import("@initiative/domain").ExportBundle | null
>(null);
const handleExportDownload = useCallback(
(includeHistory: boolean, filename: string) => {
const bundle = assembleExportBundle(
encounter,
undoRedoState,
playerCharacters,
includeHistory,
);
triggerDownload(bundle, filename);
},
[encounter, undoRedoState, playerCharacters],
);
const handleExportClipboard = useCallback(
(includeHistory: boolean) => {
const bundle = assembleExportBundle(
encounter,
undoRedoState,
playerCharacters,
includeHistory,
);
void navigator.clipboard.writeText(bundleToJson(bundle));
},
[encounter, undoRedoState, playerCharacters],
);
const applyImport = useCallback(
(bundle: import("@initiative/domain").ExportBundle) => {
setEncounter(bundle.encounter);
setUndoRedoState({
undoStack: bundle.undoStack,
redoStack: bundle.redoStack,
});
replacePlayerCharacters([...bundle.playerCharacters]);
},
[setEncounter, setUndoRedoState, replacePlayerCharacters],
);
const handleValidatedBundle = useCallback(
(result: import("@initiative/domain").ExportBundle | string) => {
if (typeof result === "string") {
setImportError(result);
return;
}
if (encounterIsEmpty) {
applyImport(result);
} else {
pendingBundleRef.current = result;
setShowImportConfirm(true);
}
},
[encounterIsEmpty, applyImport],
);
const handleImportFile = useCallback(
async (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
if (!file) return;
if (importFileRef.current) importFileRef.current.value = "";
setImportError(null);
handleValidatedBundle(await readImportFile(file));
},
[handleValidatedBundle],
);
const handleImportClipboard = useCallback(
(text: string) => {
setImportError(null);
try {
const parsed: unknown = JSON.parse(text);
handleValidatedBundle(validateImportBundle(parsed));
} catch {
setImportError("Invalid file format");
}
},
[handleValidatedBundle],
);
const handleImportConfirm = useCallback(() => {
if (pendingBundleRef.current) {
applyImport(pendingBundleRef.current);
pendingBundleRef.current = null;
}
setShowImportConfirm(false);
}, [applyImport]);
const handleImportCancel = useCallback(() => {
pendingBundleRef.current = null;
setShowImportConfirm(false);
}, []);
const overflowItems = buildOverflowItems({ const overflowItems = buildOverflowItems({
onManagePlayers, onManagePlayers,

View File

@@ -1,24 +1,32 @@
import { Loader2 } from "lucide-react"; import { Loader2 } from "lucide-react";
import { useId, useState } from "react"; import { useId, useState } from "react";
import { getAllSourceCodes } from "../adapters/bestiary-index-adapter.js"; import { useAdapters } from "../contexts/adapter-context.js";
import { useBestiaryContext } from "../contexts/bestiary-context.js"; import { useBestiaryContext } from "../contexts/bestiary-context.js";
import { useBulkImportContext } from "../contexts/bulk-import-context.js"; import { useBulkImportContext } from "../contexts/bulk-import-context.js";
import { useRulesEditionContext } from "../contexts/rules-edition-context.js";
import { useSidePanelContext } from "../contexts/side-panel-context.js"; import { useSidePanelContext } from "../contexts/side-panel-context.js";
import { Button } from "./ui/button.js"; import { Button } from "./ui/button.js";
import { Input } from "./ui/input.js"; import { Input } from "./ui/input.js";
const DEFAULT_BASE_URL = const DND_BASE_URL =
"https://raw.githubusercontent.com/5etools-mirror-3/5etools-src/main/data/bestiary/"; "https://raw.githubusercontent.com/5etools-mirror-3/5etools-src/main/data/bestiary/";
const PF2E_BASE_URL =
"https://raw.githubusercontent.com/foundryvtt/pf2e/v13-dev/packs/pf2e/";
export function BulkImportPrompt() { export function BulkImportPrompt() {
const { bestiaryIndex, pf2eBestiaryIndex } = useAdapters();
const { fetchAndCacheSource, isSourceCached, refreshCache } = const { fetchAndCacheSource, isSourceCached, refreshCache } =
useBestiaryContext(); useBestiaryContext();
const { state: importState, startImport, reset } = useBulkImportContext(); const { state: importState, startImport, reset } = useBulkImportContext();
const { dismissPanel } = useSidePanelContext(); const { dismissPanel } = useSidePanelContext();
const { edition } = useRulesEditionContext();
const [baseUrl, setBaseUrl] = useState(DEFAULT_BASE_URL); const indexPort = edition === "pf2e" ? pf2eBestiaryIndex : bestiaryIndex;
const defaultUrl = edition === "pf2e" ? PF2E_BASE_URL : DND_BASE_URL;
const [baseUrl, setBaseUrl] = useState(defaultUrl);
const baseUrlId = useId(); const baseUrlId = useId();
const totalSources = getAllSourceCodes().length; const totalSources = indexPort.getAllSourceCodes().length;
const handleStart = (url: string) => { const handleStart = (url: string) => {
startImport(url, fetchAndCacheSource, isSourceCached, refreshCache); startImport(url, fetchAndCacheSource, isSourceCached, refreshCache);

View File

@@ -1,6 +1,6 @@
import { import {
type CombatantId, type CombatantId,
type ConditionId, type ConditionEntry,
type CreatureId, type CreatureId,
deriveHpStatus, deriveHpStatus,
type PlayerIcon, type PlayerIcon,
@@ -10,6 +10,7 @@ import { Brain, Pencil, X } from "lucide-react";
import { type Ref, useCallback, useEffect, useRef, useState } from "react"; import { type Ref, useCallback, useEffect, useRef, useState } from "react";
import { useEncounterContext } from "../contexts/encounter-context.js"; import { useEncounterContext } from "../contexts/encounter-context.js";
import { useInitiativeRollsContext } from "../contexts/initiative-rolls-context.js"; import { useInitiativeRollsContext } from "../contexts/initiative-rolls-context.js";
import { useRulesEditionContext } from "../contexts/rules-edition-context.js";
import { useSidePanelContext } from "../contexts/side-panel-context.js"; import { useSidePanelContext } from "../contexts/side-panel-context.js";
import { useLongPress } from "../hooks/use-long-press.js"; import { useLongPress } from "../hooks/use-long-press.js";
import { cn } from "../lib/utils.js"; import { cn } from "../lib/utils.js";
@@ -31,7 +32,7 @@ interface Combatant {
readonly currentHp?: number; readonly currentHp?: number;
readonly tempHp?: number; readonly tempHp?: number;
readonly ac?: number; readonly ac?: number;
readonly conditions?: readonly ConditionId[]; readonly conditions?: readonly ConditionEntry[];
readonly isConcentrating?: boolean; readonly isConcentrating?: boolean;
readonly color?: string; readonly color?: string;
readonly icon?: string; readonly icon?: string;
@@ -415,12 +416,14 @@ function InitiativeDisplay({
function rowBorderClass( function rowBorderClass(
isActive: boolean, isActive: boolean,
isConcentrating: boolean | undefined, isConcentrating: boolean | undefined,
isPf2e: boolean,
): string { ): string {
if (isActive && isConcentrating) const showConcentration = isConcentrating && !isPf2e;
if (isActive && showConcentration)
return "border border-l-2 border-active-row-border border-l-purple-400 bg-active-row-bg card-glow"; return "border border-l-2 border-active-row-border border-l-purple-400 bg-active-row-bg card-glow";
if (isActive) if (isActive)
return "border border-l-2 border-active-row-border bg-active-row-bg card-glow"; return "border border-l-2 border-active-row-border bg-active-row-bg card-glow";
if (isConcentrating) if (showConcentration)
return "border border-l-2 border-transparent border-l-purple-400"; return "border border-l-2 border-transparent border-l-purple-400";
return "border border-l-2 border-transparent"; return "border border-l-2 border-transparent";
} }
@@ -430,7 +433,7 @@ function concentrationIconClass(
dimmed: boolean, dimmed: boolean,
): string { ): string {
if (!isConcentrating) if (!isConcentrating)
return "opacity-0 group-hover:opacity-50 text-muted-foreground"; return "opacity-0 pointer-coarse:opacity-50 group-hover:opacity-50 text-muted-foreground";
return dimmed ? "opacity-50 text-purple-400" : "opacity-100 text-purple-400"; return dimmed ? "opacity-50 text-purple-400" : "opacity-100 text-purple-400";
} }
@@ -448,11 +451,15 @@ export function CombatantRow({
setTempHp, setTempHp,
setAc, setAc,
toggleCondition, toggleCondition,
setConditionValue,
decrementCondition,
toggleConcentration, toggleConcentration,
} = useEncounterContext(); } = useEncounterContext();
const { selectedCreatureId, showCreature, toggleCollapse } = const { selectedCreatureId, showCreature, toggleCollapse } =
useSidePanelContext(); useSidePanelContext();
const { handleRollInitiative } = useInitiativeRollsContext(); const { handleRollInitiative } = useInitiativeRollsContext();
const { edition } = useRulesEditionContext();
const isPf2e = edition === "pf2e";
// Derive what was previously conditional props // Derive what was previously conditional props
const isStatBlockOpen = combatant.creatureId === selectedCreatureId; const isStatBlockOpen = combatant.creatureId === selectedCreatureId;
@@ -493,12 +500,16 @@ export function CombatantRow({
const tempHpDropped = const tempHpDropped =
prevTempHp !== undefined && (combatant.tempHp ?? 0) < prevTempHp; prevTempHp !== undefined && (combatant.tempHp ?? 0) < prevTempHp;
if ((realHpDropped || tempHpDropped) && combatant.isConcentrating) { if (
(realHpDropped || tempHpDropped) &&
combatant.isConcentrating &&
!isPf2e
) {
setIsPulsing(true); setIsPulsing(true);
clearTimeout(pulseTimerRef.current); clearTimeout(pulseTimerRef.current);
pulseTimerRef.current = setTimeout(() => setIsPulsing(false), 1200); pulseTimerRef.current = setTimeout(() => setIsPulsing(false), 1200);
} }
}, [currentHp, combatant.tempHp, combatant.isConcentrating]); }, [currentHp, combatant.tempHp, combatant.isConcentrating, isPf2e]);
useEffect(() => { useEffect(() => {
if (!combatant.isConcentrating) { if (!combatant.isConcentrating) {
@@ -516,12 +527,20 @@ export function CombatantRow({
ref={ref} ref={ref}
className={cn( className={cn(
"group rounded-lg pr-3 transition-colors", "group rounded-lg pr-3 transition-colors",
rowBorderClass(isActive, combatant.isConcentrating), rowBorderClass(isActive, combatant.isConcentrating, isPf2e),
isPulsing && "animate-concentration-pulse", isPulsing && "animate-concentration-pulse",
)} )}
> >
<div className="grid grid-cols-[2rem_3rem_auto_1fr_auto_2rem] items-center gap-1.5 py-3 sm:grid-cols-[2rem_3.5rem_auto_1fr_auto_2rem] sm:gap-3 sm:py-2"> <div
{/* Concentration */} className={cn(
"grid items-center gap-1.5 py-3 sm:gap-3 sm:py-2",
isPf2e
? "grid-cols-[3rem_auto_1fr_auto_2rem] pl-3 sm:grid-cols-[3.5rem_auto_1fr_auto_2rem]"
: "grid-cols-[2rem_3rem_auto_1fr_auto_2rem] sm:grid-cols-[2rem_3.5rem_auto_1fr_auto_2rem]",
)}
>
{/* Concentration — hidden in PF2e mode */}
{!isPf2e && (
<button <button
type="button" type="button"
onClick={() => toggleConcentration(id)} onClick={() => toggleConcentration(id)}
@@ -534,6 +553,7 @@ export function CombatantRow({
> >
<Brain size={16} /> <Brain size={16} />
</button> </button>
)}
{/* Initiative */} {/* Initiative */}
<div className="rounded-md bg-muted/30 px-1"> <div className="rounded-md bg-muted/30 px-1">
@@ -585,6 +605,7 @@ export function CombatantRow({
<ConditionTags <ConditionTags
conditions={combatant.conditions} conditions={combatant.conditions}
onRemove={(conditionId) => toggleCondition(id, conditionId)} onRemove={(conditionId) => toggleCondition(id, conditionId)}
onDecrement={(conditionId) => decrementCondition(id, conditionId)}
onOpenPicker={() => setPickerOpen((prev) => !prev)} onOpenPicker={() => setPickerOpen((prev) => !prev)}
/> />
</div> </div>
@@ -593,6 +614,9 @@ export function CombatantRow({
anchorRef={conditionAnchorRef} anchorRef={conditionAnchorRef}
activeConditions={combatant.conditions} activeConditions={combatant.conditions}
onToggle={(conditionId) => toggleCondition(id, conditionId)} onToggle={(conditionId) => toggleCondition(id, conditionId)}
onSetValue={(conditionId, value) =>
setConditionValue(id, conditionId, value)
}
onClose={() => setPickerOpen(false)} onClose={() => setPickerOpen(false)}
/> />
)} )}

View File

@@ -1,8 +1,10 @@
import { import {
type ConditionEntry,
type ConditionId, type ConditionId,
getConditionDescription, getConditionDescription,
getConditionsForEdition, getConditionsForEdition,
} from "@initiative/domain"; } from "@initiative/domain";
import { Check, Minus, Plus } from "lucide-react";
import { useLayoutEffect, useRef, useState } from "react"; import { useLayoutEffect, useRef, useState } from "react";
import { createPortal } from "react-dom"; import { createPortal } from "react-dom";
import { useRulesEditionContext } from "../contexts/rules-edition-context.js"; import { useRulesEditionContext } from "../contexts/rules-edition-context.js";
@@ -16,8 +18,9 @@ import { Tooltip } from "./ui/tooltip.js";
interface ConditionPickerProps { interface ConditionPickerProps {
anchorRef: React.RefObject<HTMLElement | null>; anchorRef: React.RefObject<HTMLElement | null>;
activeConditions: readonly ConditionId[] | undefined; activeConditions: readonly ConditionEntry[] | undefined;
onToggle: (conditionId: ConditionId) => void; onToggle: (conditionId: ConditionId) => void;
onSetValue: (conditionId: ConditionId, value: number) => void;
onClose: () => void; onClose: () => void;
} }
@@ -25,6 +28,7 @@ export function ConditionPicker({
anchorRef, anchorRef,
activeConditions, activeConditions,
onToggle, onToggle,
onSetValue,
onClose, onClose,
}: Readonly<ConditionPickerProps>) { }: Readonly<ConditionPickerProps>) {
const ref = useRef<HTMLDivElement>(null); const ref = useRef<HTMLDivElement>(null);
@@ -34,6 +38,11 @@ export function ConditionPicker({
maxHeight: number; maxHeight: number;
} | null>(null); } | null>(null);
const [editing, setEditing] = useState<{
id: ConditionId;
value: number;
} | null>(null);
useLayoutEffect(() => { useLayoutEffect(() => {
const anchor = anchorRef.current; const anchor = anchorRef.current;
const el = ref.current; const el = ref.current;
@@ -59,7 +68,9 @@ export function ConditionPicker({
const { edition } = useRulesEditionContext(); const { edition } = useRulesEditionContext();
const conditions = getConditionsForEdition(edition); const conditions = getConditionsForEdition(edition);
const active = new Set(activeConditions ?? []); const activeMap = new Map(
(activeConditions ?? []).map((e) => [e.id, e.value]),
);
return createPortal( return createPortal(
<div <div
@@ -74,35 +85,127 @@ export function ConditionPicker({
{conditions.map((def) => { {conditions.map((def) => {
const Icon = CONDITION_ICON_MAP[def.iconName]; const Icon = CONDITION_ICON_MAP[def.iconName];
if (!Icon) return null; if (!Icon) return null;
const isActive = active.has(def.id); const isActive = activeMap.has(def.id);
const activeValue = activeMap.get(def.id);
const isEditing = editing?.id === def.id;
const colorClass = const colorClass =
CONDITION_COLOR_CLASSES[def.color] ?? "text-muted-foreground"; CONDITION_COLOR_CLASSES[def.color] ?? "text-muted-foreground";
const handleClick = () => {
if (def.valued && edition === "pf2e") {
const current = activeMap.get(def.id);
setEditing({
id: def.id,
value: current ?? 1,
});
} else {
onToggle(def.id);
}
};
return ( return (
<Tooltip <Tooltip
key={def.id} key={def.id}
content={getConditionDescription(def, edition)} content={getConditionDescription(def, edition)}
className="block" className="block"
>
<div
className={cn(
"flex w-full items-center gap-2 rounded px-2 py-1 text-sm transition-colors",
(isActive || isEditing) && "bg-card/50",
)}
> >
<button <button
type="button" type="button"
className={cn( className="flex flex-1 items-center gap-2"
"flex w-full items-center gap-2 rounded px-2 py-1 text-sm transition-colors hover:bg-hover-neutral-bg", onClick={handleClick}
isActive && "bg-card/50",
)}
onClick={() => onToggle(def.id)}
> >
<Icon <Icon
size={14} size={14}
className={isActive ? colorClass : "text-muted-foreground"} className={
isActive || isEditing ? colorClass : "text-muted-foreground"
}
/> />
<span <span
className={ className={
isActive ? "text-foreground" : "text-muted-foreground" isActive || isEditing
? "text-foreground"
: "text-muted-foreground"
} }
> >
{def.label} {def.label}
</span> </span>
</button> </button>
{isActive && def.valued && edition === "pf2e" && !isEditing && (
<span className="min-w-5 rounded-full bg-accent px-1.5 py-0.5 text-center font-medium text-primary-foreground text-xs">
{activeValue}
</span>
)}
{isEditing && (
<div className="flex items-center gap-0.5">
<button
type="button"
className="rounded p-0.5 text-foreground hover:bg-accent/40"
onMouseDown={(e) => e.preventDefault()}
onClick={(e) => {
e.stopPropagation();
if (editing.value > 1) {
setEditing({
...editing,
value: editing.value - 1,
});
}
}}
>
<Minus className="h-3 w-3" />
</button>
<span className="min-w-5 rounded-full bg-accent px-1.5 py-0.5 text-center font-medium text-primary-foreground text-xs">
{editing.value}
</span>
{(() => {
const atMax =
def.maxValue !== undefined &&
editing.value >= def.maxValue;
return (
<button
type="button"
className={cn(
"rounded p-0.5",
atMax
? "cursor-not-allowed text-muted-foreground opacity-50"
: "text-foreground hover:bg-accent/40",
)}
disabled={atMax}
onMouseDown={(e) => e.preventDefault()}
onClick={(e) => {
e.stopPropagation();
if (!atMax) {
setEditing({
...editing,
value: editing.value + 1,
});
}
}}
>
<Plus className="h-3 w-3" />
</button>
);
})()}
<button
type="button"
className="ml-0.5 rounded p-0.5 text-foreground hover:bg-accent/40"
onMouseDown={(e) => e.preventDefault()}
onClick={(e) => {
e.stopPropagation();
onSetValue(editing.id, editing.value);
setEditing(null);
}}
>
<Check className="h-3.5 w-3.5" />
</button>
</div>
)}
</div>
</Tooltip> </Tooltip>
); );
})} })}

View File

@@ -1,42 +1,74 @@
import type { LucideIcon } from "lucide-react"; import type { LucideIcon } from "lucide-react";
import { import {
Anchor,
ArrowDown, ArrowDown,
Ban, Ban,
BatteryLow, BatteryLow,
BrainCog,
CircleHelp,
CloudFog,
Drama,
Droplet, Droplet,
Droplets,
EarOff, EarOff,
Eye,
EyeOff, EyeOff,
Footprints,
Gem, Gem,
Ghost, Ghost,
Hand, Hand,
Heart, Heart,
HeartCrack,
HeartPulse,
Link, Link,
Moon, Moon,
PersonStanding,
ShieldMinus, ShieldMinus,
ShieldOff,
Siren, Siren,
Skull,
Snail, Snail,
Sparkles, Sparkles,
Sun,
TrendingDown,
Zap,
ZapOff, ZapOff,
} from "lucide-react"; } from "lucide-react";
export const CONDITION_ICON_MAP: Record<string, LucideIcon> = { export const CONDITION_ICON_MAP: Record<string, LucideIcon> = {
EyeOff, Anchor,
Heart,
EarOff,
BatteryLow,
Siren,
Hand,
Ban,
Ghost,
ZapOff,
Gem,
Droplet,
ArrowDown, ArrowDown,
Ban,
BatteryLow,
BrainCog,
CircleHelp,
CloudFog,
Drama,
Droplet,
Droplets,
EarOff,
Eye,
EyeOff,
Footprints,
Gem,
Ghost,
Hand,
Heart,
HeartCrack,
HeartPulse,
Link, Link,
Moon,
PersonStanding,
ShieldMinus, ShieldMinus,
ShieldOff,
Siren,
Skull,
Snail, Snail,
Sparkles, Sparkles,
Moon, Sun,
TrendingDown,
Zap,
ZapOff,
}; };
export const CONDITION_COLOR_CLASSES: Record<string, string> = { export const CONDITION_COLOR_CLASSES: Record<string, string> = {
@@ -51,4 +83,5 @@ export const CONDITION_COLOR_CLASSES: Record<string, string> = {
green: "text-green-400", green: "text-green-400",
indigo: "text-indigo-400", indigo: "text-indigo-400",
sky: "text-sky-400", sky: "text-sky-400",
red: "text-red-400",
}; };

View File

@@ -1,5 +1,6 @@
import { import {
CONDITION_DEFINITIONS, CONDITION_DEFINITIONS,
type ConditionEntry,
type ConditionId, type ConditionId,
getConditionDescription, getConditionDescription,
} from "@initiative/domain"; } from "@initiative/domain";
@@ -13,44 +14,57 @@ import {
import { Tooltip } from "./ui/tooltip.js"; import { Tooltip } from "./ui/tooltip.js";
interface ConditionTagsProps { interface ConditionTagsProps {
conditions: readonly ConditionId[] | undefined; conditions: readonly ConditionEntry[] | undefined;
onRemove: (conditionId: ConditionId) => void; onRemove: (conditionId: ConditionId) => void;
onDecrement: (conditionId: ConditionId) => void;
onOpenPicker: () => void; onOpenPicker: () => void;
} }
export function ConditionTags({ export function ConditionTags({
conditions, conditions,
onRemove, onRemove,
onDecrement,
onOpenPicker, onOpenPicker,
}: Readonly<ConditionTagsProps>) { }: Readonly<ConditionTagsProps>) {
const { edition } = useRulesEditionContext(); const { edition } = useRulesEditionContext();
return ( return (
<div className="flex flex-wrap items-center gap-0.5"> <div className="flex flex-wrap items-center gap-0.5">
{conditions?.map((condId) => { {conditions?.map((entry) => {
const def = CONDITION_DEFINITIONS.find((d) => d.id === condId); const def = CONDITION_DEFINITIONS.find((d) => d.id === entry.id);
if (!def) return null; if (!def) return null;
const Icon = CONDITION_ICON_MAP[def.iconName]; const Icon = CONDITION_ICON_MAP[def.iconName];
if (!Icon) return null; if (!Icon) return null;
const colorClass = const colorClass =
CONDITION_COLOR_CLASSES[def.color] ?? "text-muted-foreground"; CONDITION_COLOR_CLASSES[def.color] ?? "text-muted-foreground";
const tooltipLabel =
entry.value === undefined ? def.label : `${def.label} ${entry.value}`;
return ( return (
<Tooltip <Tooltip
key={condId} key={entry.id}
content={`${def.label}:\n${getConditionDescription(def, edition)}`} content={`${tooltipLabel}:\n${getConditionDescription(def, edition)}`}
> >
<button <button
type="button" type="button"
aria-label={`Remove ${def.label}`} aria-label={`Remove ${def.label}`}
className={cn( className={cn(
"inline-flex items-center rounded p-0.5 transition-colors hover:bg-hover-neutral-bg", "inline-flex items-center gap-0.5 rounded p-0.5 transition-colors hover:bg-hover-neutral-bg",
colorClass, colorClass,
)} )}
onClick={(e) => { onClick={(e) => {
e.stopPropagation(); e.stopPropagation();
onRemove(condId); if (entry.value === undefined) {
onRemove(entry.id);
} else {
onDecrement(entry.id);
}
}} }}
> >
<Icon size={14} /> <Icon size={14} />
{entry.value !== undefined && (
<span className="font-medium text-xs leading-none">
{entry.value}
</span>
)}
</button> </button>
</Tooltip> </Tooltip>
); );

View File

@@ -0,0 +1,36 @@
import { VALID_CR_VALUES } from "@initiative/domain";
const CR_LABELS: Record<string, string> = {
"0": "CR 0",
"1/8": "CR 1/8",
"1/4": "CR 1/4",
"1/2": "CR 1/2",
};
function formatCr(cr: string): string {
return CR_LABELS[cr] ?? `CR ${cr}`;
}
export function CrPicker({
value,
onChange,
}: {
value: string | null;
onChange: (cr: string | undefined) => void;
}) {
return (
<select
className="rounded border border-border bg-card px-1.5 py-0.5 text-xs"
value={value ?? ""}
onChange={(e) => onChange(e.target.value || undefined)}
aria-label="Challenge rating"
>
<option value="">Assign</option>
{VALID_CR_VALUES.map((cr) => (
<option key={cr} value={cr}>
{formatCr(cr)}
</option>
))}
</select>
);
}

View File

@@ -0,0 +1,229 @@
import type { DifficultyTier, RulesEdition } from "@initiative/domain";
import { ArrowLeftRight } from "lucide-react";
import { useRef } from "react";
import { useEncounterContext } from "../contexts/encounter-context.js";
import { useRulesEditionContext } from "../contexts/rules-edition-context.js";
import { useClickOutside } from "../hooks/use-click-outside.js";
import {
type BreakdownCombatant,
useDifficultyBreakdown,
} from "../hooks/use-difficulty-breakdown.js";
import { CrPicker } from "./cr-picker.js";
import { Button } from "./ui/button.js";
const TIER_LABEL_MAP: Partial<
Record<RulesEdition, Record<DifficultyTier, { label: string; color: string }>>
> = {
"5.5e": {
0: { label: "Trivial", color: "text-muted-foreground" },
1: { label: "Low", color: "text-green-500" },
2: { label: "Moderate", color: "text-yellow-500" },
3: { label: "High", color: "text-red-500" },
},
"5e": {
0: { label: "Easy", color: "text-muted-foreground" },
1: { label: "Medium", color: "text-green-500" },
2: { label: "Hard", color: "text-yellow-500" },
3: { label: "Deadly", color: "text-red-500" },
},
};
/** Short labels for threshold display where horizontal space is limited. */
const SHORT_LABELS: Readonly<Record<string, string>> = {
Moderate: "Mod",
Medium: "Med",
};
function shortLabel(label: string): string {
return SHORT_LABELS[label] ?? label;
}
function formatXp(xp: number): string {
return xp.toLocaleString();
}
function PcRow({ entry }: { entry: BreakdownCombatant }) {
return (
<div className="col-span-4 grid grid-cols-subgrid items-center text-xs">
<span className="min-w-0 truncate" title={entry.combatant.name}>
{entry.combatant.name}
</span>
<span />
<span className="text-muted-foreground">
{entry.level === undefined ? "\u2014" : `Lv ${entry.level}`}
</span>
<span className="text-right tabular-nums">{"\u2014"}</span>
</div>
);
}
function NpcRow({
entry,
onToggleSide,
}: {
entry: BreakdownCombatant;
onToggleSide: () => void;
}) {
const { setCr } = useEncounterContext();
const isParty = entry.side === "party";
const targetSide = isParty ? "enemy" : "party";
let xpDisplay: string;
if (entry.xp == null) {
xpDisplay = "\u2014";
} else if (isParty && entry.cr) {
xpDisplay = `\u2212${formatXp(entry.xp)}`;
} else {
xpDisplay = formatXp(entry.xp);
}
return (
<div className="col-span-4 grid grid-cols-subgrid items-center text-xs">
<span className="min-w-0 truncate" title={entry.combatant.name}>
{entry.combatant.name}
</span>
<Button
variant="ghost"
size="icon-sm"
onClick={onToggleSide}
aria-label={`Move ${entry.combatant.name} to ${targetSide} side`}
>
<ArrowLeftRight className="h-3 w-3" />
</Button>
<span>
{entry.editable ? (
<CrPicker
value={entry.cr}
onChange={(cr) => setCr(entry.combatant.id, cr)}
/>
) : (
<span className="text-muted-foreground">
{entry.cr ? `CR ${entry.cr}` : "\u2014"}
</span>
)}
</span>
<span className="text-right tabular-nums">{xpDisplay}</span>
</div>
);
}
export function DifficultyBreakdownPanel({ onClose }: { onClose: () => void }) {
const ref = useRef<HTMLDivElement>(null);
useClickOutside(ref, onClose);
const { setSide } = useEncounterContext();
const { edition } = useRulesEditionContext();
const breakdown = useDifficultyBreakdown();
if (!breakdown) return null;
const tierLabels = TIER_LABEL_MAP[edition];
if (!tierLabels) return null;
const tierConfig = tierLabels[breakdown.tier];
const handleToggle = (entry: BreakdownCombatant) => {
const newSide = entry.side === "party" ? "enemy" : "party";
setSide(entry.combatant.id, newSide);
};
const isPC = (entry: BreakdownCombatant) =>
entry.combatant.playerCharacterId != null;
return (
<div
ref={ref}
className="absolute top-full right-0 z-50 mt-1 w-80 rounded-lg border border-border bg-card p-3 shadow-lg max-sm:fixed max-sm:top-12 max-sm:right-3 max-sm:left-3 max-sm:w-auto"
>
<div className="mb-2 font-medium text-sm">
Encounter Difficulty:{" "}
<span className={tierConfig.color}>{tierConfig.label}</span>
</div>
<div className="mb-2 border-border border-t pt-2">
<div className="mb-1 text-muted-foreground text-xs">
Party Budget ({breakdown.pcCount}{" "}
{breakdown.pcCount === 1 ? "PC" : "PCs"})
</div>
<div className="flex gap-3 text-xs">
{breakdown.thresholds.map((t) => (
<span key={t.label}>
{shortLabel(t.label)}: <strong>{formatXp(t.value)}</strong>
</span>
))}
</div>
</div>
<div className="border-border border-t pt-2 pb-2 text-muted-foreground text-xs italic">
Allied NPC XP is subtracted from encounter difficulty
</div>
<div className="border-border border-t pt-2">
<div className="mb-1 flex justify-between text-muted-foreground text-xs">
<span>Party</span>
<span>XP</span>
</div>
<div className="grid grid-cols-[1fr_auto_auto_3.5rem] gap-x-2 gap-y-1">
{breakdown.partyCombatants.map((entry) =>
isPC(entry) ? (
<PcRow key={entry.combatant.id} entry={entry} />
) : (
<NpcRow
key={entry.combatant.id}
entry={entry}
onToggleSide={() => handleToggle(entry)}
/>
),
)}
</div>
</div>
<div className="mt-2 border-border border-t pt-2">
<div className="mb-1 flex justify-between text-muted-foreground text-xs">
<span>Enemy</span>
<span>XP</span>
</div>
<div className="grid grid-cols-[1fr_auto_auto_3.5rem] gap-x-2 gap-y-1">
{breakdown.enemyCombatants.map((entry) =>
isPC(entry) ? (
<PcRow key={entry.combatant.id} entry={entry} />
) : (
<NpcRow
key={entry.combatant.id}
entry={entry}
onToggleSide={() => handleToggle(entry)}
/>
),
)}
</div>
</div>
{breakdown.encounterMultiplier !== undefined &&
breakdown.adjustedXp !== undefined ? (
<div className="mt-2 border-border border-t pt-2">
<div className="flex justify-between font-medium text-xs">
<span>Monster XP</span>
<span className="tabular-nums">
{formatXp(breakdown.totalMonsterXp)}{" "}
<span className="text-muted-foreground">
&times;{breakdown.encounterMultiplier}
</span>{" "}
= {formatXp(breakdown.adjustedXp)}
</span>
</div>
{breakdown.partySizeAdjusted === true ? (
<div className="mt-0.5 text-muted-foreground text-xs italic">
Adjusted for {breakdown.pcCount}{" "}
{breakdown.pcCount === 1 ? "PC" : "PCs"}
</div>
) : null}
</div>
) : (
<div className="mt-2 flex justify-between border-border border-t pt-2 font-medium text-xs">
<span>Net Monster XP</span>
<span className="tabular-nums">
{formatXp(breakdown.totalMonsterXp)}
</span>
</div>
)}
</div>
);
}

View File

@@ -1,28 +1,58 @@
import type { DifficultyResult, DifficultyTier } from "@initiative/domain"; import type { DifficultyResult, DifficultyTier } from "@initiative/domain";
import { cn } from "../lib/utils.js"; import { cn } from "../lib/utils.js";
const TIER_CONFIG: Record< export const TIER_LABELS_5_5E: Record<DifficultyTier, string> = {
0: "Trivial",
1: "Low",
2: "Moderate",
3: "High",
};
export const TIER_LABELS_2014: Record<DifficultyTier, string> = {
0: "Easy",
1: "Medium",
2: "Hard",
3: "Deadly",
};
const TIER_COLORS: Record<
DifficultyTier, DifficultyTier,
{ filledBars: number; color: string; label: string } { filledBars: number; color: string }
> = { > = {
trivial: { filledBars: 0, color: "", label: "Trivial" }, 0: { filledBars: 0, color: "" },
low: { filledBars: 1, color: "bg-green-500", label: "Low" }, 1: { filledBars: 1, color: "bg-green-500" },
moderate: { filledBars: 2, color: "bg-yellow-500", label: "Moderate" }, 2: { filledBars: 2, color: "bg-yellow-500" },
high: { filledBars: 3, color: "bg-red-500", label: "High" }, 3: { filledBars: 3, color: "bg-red-500" },
}; };
const BAR_HEIGHTS = ["h-2", "h-3", "h-4"] as const; const BAR_HEIGHTS = ["h-2", "h-3", "h-4"] as const;
export function DifficultyIndicator({ result }: { result: DifficultyResult }) { export function DifficultyIndicator({
const config = TIER_CONFIG[result.tier]; result,
const tooltip = `${config.label} encounter difficulty`; labels,
onClick,
}: {
result: DifficultyResult;
labels: Record<DifficultyTier, string>;
onClick?: () => void;
}) {
const config = TIER_COLORS[result.tier];
const label = labels[result.tier];
const tooltip = `${label} encounter difficulty`;
const Element = onClick ? "button" : "div";
return ( return (
<div <Element
className="flex items-end gap-0.5" className={cn(
"flex items-end gap-0.5",
onClick && "cursor-pointer rounded p-1 hover:bg-muted/50",
)}
title={tooltip} title={tooltip}
role="img" role="img"
aria-label={tooltip} aria-label={tooltip}
onClick={onClick}
type={onClick ? "button" : undefined}
> >
{BAR_HEIGHTS.map((height, i) => ( {BAR_HEIGHTS.map((height, i) => (
<div <div
@@ -34,6 +64,6 @@ export function DifficultyIndicator({ result }: { result: DifficultyResult }) {
)} )}
/> />
))} ))}
</div> </Element>
); );
} }

View File

@@ -1,10 +1,16 @@
import type { Creature } from "@initiative/domain";
import { import {
type Creature,
calculateInitiative, calculateInitiative,
formatInitiativeModifier, formatInitiativeModifier,
} from "@initiative/domain"; } from "@initiative/domain";
import {
PropertyLine,
SectionDivider,
TraitEntry,
TraitSection,
} from "./stat-block-parts.js";
interface StatBlockProps { interface DndStatBlockProps {
creature: Creature; creature: Creature;
} }
@@ -13,53 +19,7 @@ function abilityMod(score: number): string {
return mod >= 0 ? `+${mod}` : `${mod}`; return mod >= 0 ? `+${mod}` : `${mod}`;
} }
function PropertyLine({ export function DndStatBlock({ creature }: Readonly<DndStatBlockProps>) {
label,
value,
}: Readonly<{
label: string;
value: string | undefined;
}>) {
if (!value) return null;
return (
<div className="text-sm">
<span className="font-semibold">{label}</span> {value}
</div>
);
}
function SectionDivider() {
return (
<div className="my-2 h-px bg-gradient-to-r from-stat-divider-from via-stat-divider-via to-transparent" />
);
}
function TraitSection({
entries,
heading,
}: Readonly<{
entries: readonly { name: string; text: string }[] | undefined;
heading?: string;
}>) {
if (!entries || entries.length === 0) return null;
return (
<>
<SectionDivider />
{heading ? (
<h3 className="font-bold text-base text-stat-heading">{heading}</h3>
) : null}
<div className="space-y-2">
{entries.map((e) => (
<div key={e.name} className="text-sm">
<span className="font-semibold italic">{e.name}.</span> {e.text}
</div>
))}
</div>
</>
);
}
export function StatBlock({ creature }: Readonly<StatBlockProps>) {
const abilities = [ const abilities = [
{ label: "STR", score: creature.abilities.str }, { label: "STR", score: creature.abilities.str },
{ label: "DEX", score: creature.abilities.dex }, { label: "DEX", score: creature.abilities.dex },
@@ -219,9 +179,7 @@ export function StatBlock({ creature }: Readonly<StatBlockProps>) {
</p> </p>
<div className="space-y-2"> <div className="space-y-2">
{creature.legendaryActions.entries.map((a) => ( {creature.legendaryActions.entries.map((a) => (
<div key={a.name} className="text-sm"> <TraitEntry key={a.name} trait={a} />
<span className="font-semibold italic">{a.name}.</span> {a.text}
</div>
))} ))}
</div> </div>
</> </>

View File

@@ -0,0 +1,174 @@
import type { Pf2eCreature } from "@initiative/domain";
import { formatInitiativeModifier } from "@initiative/domain";
import {
PropertyLine,
SectionDivider,
TraitSection,
} from "./stat-block-parts.js";
interface Pf2eStatBlockProps {
creature: Pf2eCreature;
}
const ALIGNMENTS = new Set([
"lg",
"ng",
"cg",
"ln",
"n",
"cn",
"le",
"ne",
"ce",
]);
function capitalize(s: string): string {
return s.charAt(0).toUpperCase() + s.slice(1);
}
function displayTraits(traits: readonly string[]): string[] {
return traits.filter((t) => !ALIGNMENTS.has(t)).map(capitalize);
}
function formatMod(mod: number): string {
return mod >= 0 ? `+${mod}` : `${mod}`;
}
export function Pf2eStatBlock({ creature }: Readonly<Pf2eStatBlockProps>) {
const abilityEntries = [
{ label: "Str", mod: creature.abilityMods.str },
{ label: "Dex", mod: creature.abilityMods.dex },
{ label: "Con", mod: creature.abilityMods.con },
{ label: "Int", mod: creature.abilityMods.int },
{ label: "Wis", mod: creature.abilityMods.wis },
{ label: "Cha", mod: creature.abilityMods.cha },
];
return (
<div className="space-y-1 text-foreground">
{/* Header */}
<div>
<div className="flex items-baseline justify-between gap-2">
<h2 className="font-bold text-stat-heading text-xl">
{creature.name}
</h2>
<span className="shrink-0 font-semibold text-sm">
Level {creature.level}
</span>
</div>
<div className="mt-1 flex flex-wrap gap-1">
{displayTraits(creature.traits).map((trait) => (
<span
key={trait}
className="rounded border border-border bg-card px-1.5 py-0.5 text-foreground text-xs"
>
{trait}
</span>
))}
</div>
<p className="mt-1 text-muted-foreground text-xs">
{creature.sourceDisplayName}
</p>
</div>
<SectionDivider />
{/* Perception, Languages, Skills */}
<div className="space-y-0.5 text-sm">
<div>
<span className="font-semibold">Perception</span>{" "}
{formatInitiativeModifier(creature.perception)}
{creature.senses ? `; ${creature.senses}` : ""}
</div>
<PropertyLine label="Languages" value={creature.languages} />
<PropertyLine label="Skills" value={creature.skills} />
</div>
{/* Ability Modifiers */}
<div className="grid grid-cols-6 gap-1 text-center text-sm">
{abilityEntries.map((a) => (
<div key={a.label}>
<div className="font-semibold text-muted-foreground text-xs">
{a.label}
</div>
<div>{formatMod(a.mod)}</div>
</div>
))}
</div>
<PropertyLine label="Items" value={creature.items} />
{/* Top abilities (before defenses) */}
<TraitSection entries={creature.abilitiesTop} />
<SectionDivider />
{/* Defenses */}
<div className="space-y-0.5 text-sm">
<div>
<span className="font-semibold">AC</span> {creature.ac}
{creature.acConditional ? ` (${creature.acConditional})` : ""};{" "}
<span className="font-semibold">Fort</span>{" "}
{formatMod(creature.saveFort)},{" "}
<span className="font-semibold">Ref</span>{" "}
{formatMod(creature.saveRef)},{" "}
<span className="font-semibold">Will</span>{" "}
{formatMod(creature.saveWill)}
{creature.saveConditional ? `; ${creature.saveConditional}` : ""}
</div>
<div>
<span className="font-semibold">HP</span> {creature.hp}
{creature.hpDetails ? ` (${creature.hpDetails})` : ""}
</div>
<PropertyLine label="Immunities" value={creature.immunities} />
<PropertyLine label="Resistances" value={creature.resistances} />
<PropertyLine label="Weaknesses" value={creature.weaknesses} />
</div>
{/* Mid abilities (reactions, auras) */}
<TraitSection entries={creature.abilitiesMid} />
<SectionDivider />
{/* Speed */}
<div className="text-sm">
<span className="font-semibold">Speed</span> {creature.speed}
</div>
{/* Attacks */}
<TraitSection entries={creature.attacks} />
{/* Bottom abilities (active abilities) */}
<TraitSection entries={creature.abilitiesBot} />
{/* Spellcasting */}
{creature.spellcasting && creature.spellcasting.length > 0 && (
<>
<SectionDivider />
{creature.spellcasting.map((sc) => (
<div key={sc.name} className="space-y-1 text-sm">
<div>
<span className="font-semibold italic">{sc.name}.</span>{" "}
{sc.headerText}
</div>
{sc.daily?.map((d) => (
<div key={d.uses} className="pl-2">
<span className="font-semibold">
{d.uses === 0 ? "Cantrips" : `Rank ${d.uses}`}:
</span>{" "}
{d.spells.join(", ")}
</div>
))}
{sc.atWill && sc.atWill.length > 0 && (
<div className="pl-2">
<span className="font-semibold">Cantrips:</span>{" "}
{sc.atWill.join(", ")}
</div>
)}
</div>
))}
</>
)}
</div>
);
}

View File

@@ -13,6 +13,7 @@ interface SettingsModalProps {
const EDITION_OPTIONS: { value: RulesEdition; label: string }[] = [ const EDITION_OPTIONS: { value: RulesEdition; label: string }[] = [
{ value: "5e", label: "5e (2014)" }, { value: "5e", label: "5e (2014)" },
{ value: "5.5e", label: "5.5e (2024)" }, { value: "5.5e", label: "5.5e (2024)" },
{ value: "pf2e", label: "Pathfinder 2e" },
]; ];
const THEME_OPTIONS: { const THEME_OPTIONS: {
@@ -36,7 +37,7 @@ export function SettingsModal({ open, onClose }: Readonly<SettingsModalProps>) {
<div className="flex flex-col gap-5"> <div className="flex flex-col gap-5">
<div> <div>
<span className="mb-2 block font-medium text-muted-foreground text-sm"> <span className="mb-2 block font-medium text-muted-foreground text-sm">
Conditions Game System
</span> </span>
<div className="flex gap-1"> <div className="flex gap-1">
{EDITION_OPTIONS.map((opt) => ( {EDITION_OPTIONS.map((opt) => (

View File

@@ -1,10 +1,8 @@
import { Download, Loader2, Upload } from "lucide-react"; import { Download, Loader2, Upload } from "lucide-react";
import { useId, useRef, useState } from "react"; import { useId, useRef, useState } from "react";
import { import { useAdapters } from "../contexts/adapter-context.js";
getDefaultFetchUrl,
getSourceDisplayName,
} from "../adapters/bestiary-index-adapter.js";
import { useBestiaryContext } from "../contexts/bestiary-context.js"; import { useBestiaryContext } from "../contexts/bestiary-context.js";
import { useRulesEditionContext } from "../contexts/rules-edition-context.js";
import { Button } from "./ui/button.js"; import { Button } from "./ui/button.js";
import { Input } from "./ui/input.js"; import { Input } from "./ui/input.js";
@@ -17,9 +15,14 @@ export function SourceFetchPrompt({
sourceCode, sourceCode,
onSourceLoaded, onSourceLoaded,
}: Readonly<SourceFetchPromptProps>) { }: Readonly<SourceFetchPromptProps>) {
const { bestiaryIndex, pf2eBestiaryIndex } = useAdapters();
const { fetchAndCacheSource, uploadAndCacheSource } = useBestiaryContext(); const { fetchAndCacheSource, uploadAndCacheSource } = useBestiaryContext();
const sourceDisplayName = getSourceDisplayName(sourceCode); const { edition } = useRulesEditionContext();
const [url, setUrl] = useState(() => getDefaultFetchUrl(sourceCode)); const indexPort = edition === "pf2e" ? pf2eBestiaryIndex : bestiaryIndex;
const sourceDisplayName = indexPort.getSourceDisplayName(sourceCode);
const [url, setUrl] = useState(() =>
indexPort.getDefaultFetchUrl(sourceCode),
);
const [status, setStatus] = useState<"idle" | "fetching" | "error">("idle"); const [status, setStatus] = useState<"idle" | "fetching" | "error">("idle");
const [error, setError] = useState<string>(""); const [error, setError] = useState<string>("");
const fileInputRef = useRef<HTMLInputElement>(null); const fileInputRef = useRef<HTMLInputElement>(null);

View File

@@ -6,14 +6,18 @@ import {
useOptimistic, useOptimistic,
useState, useState,
} from "react"; } from "react";
import type { CachedSourceInfo } from "../adapters/bestiary-cache.js"; import type { CachedSourceInfo } from "../adapters/ports.js";
import * as bestiaryCache from "../adapters/bestiary-cache.js"; import { useAdapters } from "../contexts/adapter-context.js";
import { useBestiaryContext } from "../contexts/bestiary-context.js"; import { useBestiaryContext } from "../contexts/bestiary-context.js";
import { useRulesEditionContext } from "../contexts/rules-edition-context.js";
import { Button } from "./ui/button.js"; import { Button } from "./ui/button.js";
import { Input } from "./ui/input.js"; import { Input } from "./ui/input.js";
export function SourceManager() { export function SourceManager() {
const { bestiaryCache } = useAdapters();
const { refreshCache } = useBestiaryContext(); const { refreshCache } = useBestiaryContext();
const { edition } = useRulesEditionContext();
const system = edition === "pf2e" ? "pf2e" : "dnd";
const [sources, setSources] = useState<CachedSourceInfo[]>([]); const [sources, setSources] = useState<CachedSourceInfo[]>([]);
const [filter, setFilter] = useState(""); const [filter, setFilter] = useState("");
const [optimisticSources, applyOptimistic] = useOptimistic( const [optimisticSources, applyOptimistic] = useOptimistic(
@@ -28,9 +32,9 @@ export function SourceManager() {
); );
const loadSources = useCallback(async () => { const loadSources = useCallback(async () => {
const cached = await bestiaryCache.getCachedSources(); const cached = await bestiaryCache.getCachedSources(system);
setSources(cached); setSources(cached);
}, []); }, [bestiaryCache, system]);
useEffect(() => { useEffect(() => {
void loadSources(); void loadSources();
@@ -38,7 +42,7 @@ export function SourceManager() {
const handleClearSource = async (sourceCode: string) => { const handleClearSource = async (sourceCode: string) => {
applyOptimistic({ type: "remove", sourceCode }); applyOptimistic({ type: "remove", sourceCode });
await bestiaryCache.clearSource(sourceCode); await bestiaryCache.clearSource(system, sourceCode);
await loadSources(); await loadSources();
void refreshCache(); void refreshCache();
}; };

View File

@@ -1,4 +1,4 @@
import type { CreatureId } from "@initiative/domain"; import type { Creature, CreatureId } from "@initiative/domain";
import { PanelRightClose, Pin, PinOff } from "lucide-react"; import { PanelRightClose, Pin, PinOff } from "lucide-react";
import type { ReactNode } from "react"; import type { ReactNode } from "react";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
@@ -7,9 +7,10 @@ import { useSidePanelContext } from "../contexts/side-panel-context.js";
import { useSwipeToDismiss } from "../hooks/use-swipe-to-dismiss.js"; import { useSwipeToDismiss } from "../hooks/use-swipe-to-dismiss.js";
import { cn } from "../lib/utils.js"; import { cn } from "../lib/utils.js";
import { BulkImportPrompt } from "./bulk-import-prompt.js"; import { BulkImportPrompt } from "./bulk-import-prompt.js";
import { DndStatBlock } from "./dnd-stat-block.js";
import { Pf2eStatBlock } from "./pf2e-stat-block.js";
import { SourceFetchPrompt } from "./source-fetch-prompt.js"; import { SourceFetchPrompt } from "./source-fetch-prompt.js";
import { SourceManager } from "./source-manager.js"; import { SourceManager } from "./source-manager.js";
import { StatBlock } from "./stat-block.js";
import { Button } from "./ui/button.js"; import { Button } from "./ui/button.js";
interface StatBlockPanelProps { interface StatBlockPanelProps {
@@ -20,7 +21,10 @@ interface StatBlockPanelProps {
function extractSourceCode(cId: CreatureId): string { function extractSourceCode(cId: CreatureId): string {
const colonIndex = cId.indexOf(":"); const colonIndex = cId.indexOf(":");
if (colonIndex === -1) return ""; if (colonIndex === -1) return "";
return cId.slice(0, colonIndex).toUpperCase(); const prefix = cId.slice(0, colonIndex);
// D&D source codes are short uppercase (e.g. "mm" from "MM").
// PF2e source codes use hyphens (e.g. "pathfinder-monster-core").
return prefix.includes("-") ? prefix : prefix.toUpperCase();
} }
function CollapsedTab({ function CollapsedTab({
@@ -307,7 +311,10 @@ export function StatBlockPanel({
} }
if (creature) { if (creature) {
return <StatBlock creature={creature} />; if ("system" in creature && creature.system === "pf2e") {
return <Pf2eStatBlock creature={creature} />;
}
return <DndStatBlock creature={creature as Creature} />;
} }
if (needsFetch && sourceCode) { if (needsFetch && sourceCode) {

View File

@@ -0,0 +1,175 @@
import type {
ActivityCost,
TraitBlock,
TraitSegment,
} from "@initiative/domain";
export function PropertyLine({
label,
value,
}: Readonly<{
label: string;
value: string | undefined;
}>) {
if (!value) return null;
return (
<div className="text-sm">
<span className="font-semibold">{label}</span> {value}
</div>
);
}
export function SectionDivider() {
return (
<div className="my-2 h-px bg-gradient-to-r from-stat-divider-from via-stat-divider-via to-transparent" />
);
}
function segmentKey(seg: TraitSegment): string {
return seg.type === "text"
? seg.value.slice(0, 40)
: seg.items.map((i) => i.label ?? i.text.slice(0, 20)).join(",");
}
function TraitSegments({
segments,
}: Readonly<{ segments: readonly TraitSegment[] }>) {
return (
<>
{segments.map((seg, i) => {
if (seg.type === "text") {
return (
<span key={segmentKey(seg)}>
{i === 0 ? ` ${seg.value}` : seg.value}
</span>
);
}
return (
<div key={segmentKey(seg)} className="mt-1 space-y-0.5">
{seg.items.map((item) => (
<p key={item.label ?? item.text}>
{item.label != null && (
<span className="font-semibold">{item.label}. </span>
)}
{item.text}
</p>
))}
</div>
);
})}
</>
);
}
const ACTION_DIAMOND = "M50 2 L96 50 L50 98 L4 50 Z M48 28 L78 50 L48 72 Z";
const ACTION_DIAMOND_SOLID = "M50 2 L96 50 L50 98 L4 50 Z";
const FREE_ACTION_DIAMOND =
"M50 2 L96 50 L50 98 L4 50 Z M50 12 L12 50 L50 88 L88 50 Z";
const FREE_ACTION_CHEVRON = "M48 28 L78 50 L48 72 Z";
const REACTION_ARROW =
"M75 15 A42 42 0 1 0 85 55 L72 55 A30 30 0 1 1 65 25 L65 40 L92 20 L65 0 L65 15 Z";
function ActivityIcon({ activity }: Readonly<{ activity: ActivityCost }>) {
const cls = "inline-block h-[1em] align-[-0.1em]";
if (activity.unit === "free") {
return (
<svg aria-hidden="true" className={cls} viewBox="0 0 100 100">
<path d={FREE_ACTION_DIAMOND} fill="currentColor" fillRule="evenodd" />
<path d={FREE_ACTION_CHEVRON} fill="currentColor" />
</svg>
);
}
if (activity.unit === "reaction") {
return (
<svg aria-hidden="true" className={cls} viewBox="0 0 100 100">
<g transform="translate(100,100) rotate(180)">
<path d={REACTION_ARROW} fill="currentColor" />
</g>
</svg>
);
}
const count = activity.number;
if (count === 1) {
return (
<svg aria-hidden="true" className={cls} viewBox="0 0 100 100">
<path d={ACTION_DIAMOND} fill="currentColor" fillRule="evenodd" />
</svg>
);
}
if (count === 2) {
return (
<svg aria-hidden="true" className={cls} viewBox="0 0 140 100">
<path d={ACTION_DIAMOND_SOLID} fill="currentColor" />
<path
d="M90 2 L136 50 L90 98 L44 50 Z M88 28 L118 50 L88 72 Z"
fill="currentColor"
fillRule="evenodd"
/>
</svg>
);
}
return (
<svg aria-hidden="true" className={cls} viewBox="0 0 180 100">
<path d={ACTION_DIAMOND_SOLID} fill="currentColor" />
<path d="M90 2 L136 50 L90 98 L44 50 Z" fill="currentColor" />
<path
d="M130 2 L176 50 L130 98 L84 50 Z M128 28 L158 50 L128 72 Z"
fill="currentColor"
fillRule="evenodd"
/>
</svg>
);
}
export function TraitEntry({ trait }: Readonly<{ trait: TraitBlock }>) {
return (
<div className="text-sm">
<span className="font-semibold italic">
{trait.name}
{trait.activity ? null : "."}
{trait.activity ? (
<>
{" "}
<ActivityIcon activity={trait.activity} />
</>
) : null}
</span>
{trait.trigger ? (
<>
{" "}
<span className="font-semibold">Trigger</span> {trait.trigger}
{trait.segments.length > 0 ? (
<>
{" "}
<span className="font-semibold">Effect</span>
</>
) : null}
</>
) : null}
<TraitSegments segments={trait.segments} />
</div>
);
}
export function TraitSection({
entries,
heading,
}: Readonly<{
entries: readonly TraitBlock[] | undefined;
heading?: string;
}>) {
if (!entries || entries.length === 0) return null;
return (
<>
<SectionDivider />
{heading ? (
<h3 className="font-bold text-base text-stat-heading">{heading}</h3>
) : null}
<div className="space-y-2">
{entries.map((e) => (
<TraitEntry key={e.name} trait={e} />
))}
</div>
</>
);
}

View File

@@ -1,7 +1,14 @@
import { Redo2, StepBack, StepForward, Trash2, Undo2 } from "lucide-react"; import { Redo2, StepBack, StepForward, Trash2, Undo2 } from "lucide-react";
import { useState } from "react";
import { useEncounterContext } from "../contexts/encounter-context.js"; import { useEncounterContext } from "../contexts/encounter-context.js";
import { useRulesEditionContext } from "../contexts/rules-edition-context.js";
import { useDifficulty } from "../hooks/use-difficulty.js"; import { useDifficulty } from "../hooks/use-difficulty.js";
import { DifficultyIndicator } from "./difficulty-indicator.js"; import { DifficultyBreakdownPanel } from "./difficulty-breakdown-panel.js";
import {
DifficultyIndicator,
TIER_LABELS_5_5E,
TIER_LABELS_2014,
} from "./difficulty-indicator.js";
import { Button } from "./ui/button.js"; import { Button } from "./ui/button.js";
import { ConfirmButton } from "./ui/confirm-button.js"; import { ConfirmButton } from "./ui/confirm-button.js";
@@ -18,12 +25,17 @@ export function TurnNavigation() {
} = useEncounterContext(); } = useEncounterContext();
const difficulty = useDifficulty(); const difficulty = useDifficulty();
const { edition } = useRulesEditionContext();
const tierLabels = edition === "5e" ? TIER_LABELS_2014 : TIER_LABELS_5_5E;
const [showBreakdown, setShowBreakdown] = useState(false);
const hasCombatants = encounter.combatants.length > 0; const hasCombatants = encounter.combatants.length > 0;
const isAtStart = encounter.roundNumber === 1 && encounter.activeIndex === 0; const isAtStart = encounter.roundNumber === 1 && encounter.activeIndex === 0;
const activeCombatant = encounter.combatants[encounter.activeIndex]; const activeCombatant = encounter.combatants[encounter.activeIndex];
return ( return (
<div className="card-glow flex items-center gap-3 border-border border-b bg-card px-4 py-3 sm:rounded-lg sm:border"> <div className="card-glow grid grid-cols-[1fr_minmax(0,auto)_1fr] items-center border-border border-b bg-card px-2 py-3 sm:rounded-lg sm:border sm:px-4">
{/* Left zone: navigation + history + round */}
<div className="flex items-center gap-1">
<Button <Button
variant="ghost" variant="ghost"
size="icon" size="icon"
@@ -34,8 +46,6 @@ export function TurnNavigation() {
> >
<StepBack className="h-5 w-5" /> <StepBack className="h-5 w-5" />
</Button> </Button>
<div className="flex items-center gap-1">
<Button <Button
variant="ghost" variant="ghost"
size="icon" size="icon"
@@ -56,23 +66,36 @@ export function TurnNavigation() {
> >
<Redo2 className="h-4 w-4" /> <Redo2 className="h-4 w-4" />
</Button> </Button>
</div> <span className="ml-1 rounded-md bg-muted px-2 py-0.5 font-semibold text-foreground text-sm tabular-nums">
<div className="flex min-w-0 flex-1 items-center justify-center gap-2 text-sm">
<span className="shrink-0 rounded-md bg-muted px-2 py-0.5 font-semibold text-foreground text-sm">
<span className="-mt-[3px] inline-block">
R{encounter.roundNumber} R{encounter.roundNumber}
</span> </span>
</span> </div>
{/* Center zone: active combatant name */}
<div className="min-w-0 px-2 text-center text-sm">
{activeCombatant ? ( {activeCombatant ? (
<span className="truncate font-medium">{activeCombatant.name}</span> <span className="truncate font-medium">{activeCombatant.name}</span>
) : ( ) : (
<span className="text-muted-foreground">No combatants</span> <span className="text-muted-foreground">No combatants</span>
)} )}
{difficulty && <DifficultyIndicator result={difficulty} />}
</div> </div>
<div className="flex flex-shrink-0 items-center gap-3"> {/* Right zone: difficulty + destructive + forward */}
<div className="flex items-center justify-end gap-1">
{difficulty && (
<div className="relative mr-1">
<DifficultyIndicator
result={difficulty}
labels={tierLabels}
onClick={() => setShowBreakdown((prev) => !prev)}
/>
{showBreakdown ? (
<DifficultyBreakdownPanel
onClose={() => setShowBreakdown(false)}
/>
) : null}
</div>
)}
<ConfirmButton <ConfirmButton
icon={<Trash2 className="h-5 w-5" />} icon={<Trash2 className="h-5 w-5" />}
label="Clear encounter" label="Clear encounter"

View File

@@ -0,0 +1,40 @@
import { createContext, type ReactNode, useContext } from "react";
import type {
BestiaryCachePort,
BestiaryIndexPort,
EncounterPersistence,
Pf2eBestiaryIndexPort,
PlayerCharacterPersistence,
UndoRedoPersistence,
} from "../adapters/ports.js";
export interface Adapters {
encounterPersistence: EncounterPersistence;
undoRedoPersistence: UndoRedoPersistence;
playerCharacterPersistence: PlayerCharacterPersistence;
bestiaryCache: BestiaryCachePort;
bestiaryIndex: BestiaryIndexPort;
pf2eBestiaryIndex: Pf2eBestiaryIndexPort;
}
const AdapterContext = createContext<Adapters | null>(null);
export function AdapterProvider({
adapters,
children,
}: {
adapters: Adapters;
children: ReactNode;
}) {
return (
<AdapterContext.Provider value={adapters}>
{children}
</AdapterContext.Provider>
);
}
export function useAdapters(): Adapters {
const ctx = useContext(AdapterContext);
if (!ctx) throw new Error("useAdapters requires AdapterProvider");
return ctx;
}

View File

@@ -1,8 +1,4 @@
import type { import type { ConditionId, PlayerCharacter } from "@initiative/domain";
BestiaryIndexEntry,
ConditionId,
PlayerCharacter,
} from "@initiative/domain";
import { import {
combatantId, combatantId,
createEncounter, createEncounter,
@@ -10,19 +6,10 @@ import {
isDomainError, isDomainError,
playerCharacterId, playerCharacterId,
} from "@initiative/domain"; } from "@initiative/domain";
import { describe, expect, it, vi } from "vitest"; import { describe, expect, it } from "vitest";
import type { SearchResult } from "../use-bestiary.js";
import { type EncounterState, encounterReducer } from "../use-encounter.js"; import { type EncounterState, encounterReducer } from "../use-encounter.js";
vi.mock("../../persistence/encounter-storage.js", () => ({
loadEncounter: vi.fn().mockReturnValue(null),
saveEncounter: vi.fn(),
}));
vi.mock("../../persistence/undo-redo-storage.js", () => ({
loadUndoRedoStacks: vi.fn().mockReturnValue(EMPTY_UNDO_REDO_STATE),
saveUndoRedoStacks: vi.fn(),
}));
function emptyState(): EncounterState { function emptyState(): EncounterState {
return { return {
encounter: { encounter: {
@@ -55,9 +42,11 @@ function stateWithHp(name: string, maxHp: number): EncounterState {
}); });
} }
const BESTIARY_ENTRY: BestiaryIndexEntry = { const BESTIARY_ENTRY: SearchResult = {
system: "dnd",
name: "Goblin", name: "Goblin",
source: "MM", source: "MM",
sourceDisplayName: "Monster Manual",
ac: 15, ac: 15,
hp: 7, hp: 7,
dex: 14, dex: 14,
@@ -67,6 +56,19 @@ const BESTIARY_ENTRY: BestiaryIndexEntry = {
type: "humanoid", type: "humanoid",
}; };
const PF2E_BESTIARY_ENTRY: SearchResult = {
system: "pf2e",
name: "Goblin Warrior",
source: "B1",
sourceDisplayName: "Bestiary",
level: -1,
ac: 16,
hp: 6,
perception: 5,
size: "small",
type: "humanoid",
};
describe("encounterReducer", () => { describe("encounterReducer", () => {
describe("add-combatant", () => { describe("add-combatant", () => {
it("adds a combatant and pushes undo", () => { it("adds a combatant and pushes undo", () => {
@@ -246,7 +248,9 @@ describe("encounterReducer", () => {
conditionId: "blinded" as ConditionId, conditionId: "blinded" as ConditionId,
}); });
expect(next.encounter.combatants[0].conditions).toContain("blinded"); expect(next.encounter.combatants[0].conditions).toContainEqual({
id: "blinded",
});
}); });
it("toggles concentration", () => { it("toggles concentration", () => {
@@ -337,6 +341,19 @@ describe("encounterReducer", () => {
expect(names).toContain("Goblin 1"); expect(names).toContain("Goblin 1");
expect(names).toContain("Goblin 2"); expect(names).toContain("Goblin 2");
}); });
it("adds PF2e creature with HP, AC, and creatureId", () => {
const next = encounterReducer(emptyState(), {
type: "add-from-bestiary",
entry: PF2E_BESTIARY_ENTRY,
});
const c = next.encounter.combatants[0];
expect(c.name).toBe("Goblin Warrior");
expect(c.maxHp).toBe(6);
expect(c.ac).toBe(16);
expect(c.creatureId).toBe("b1:goblin-warrior");
});
}); });
describe("add-multiple-from-bestiary", () => { describe("add-multiple-from-bestiary", () => {

View File

@@ -1,328 +0,0 @@
// @vitest-environment jsdom
import type { PlayerCharacter } from "@initiative/domain";
import { playerCharacterId } from "@initiative/domain";
import { act, renderHook } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { SearchResult } from "../../contexts/bestiary-context.js";
import { useActionBarState } from "../use-action-bar-state.js";
const mockAddCombatant = vi.fn();
const mockAddFromBestiary = vi.fn();
const mockAddMultipleFromBestiary = vi.fn();
const mockAddFromPlayerCharacter = vi.fn();
const mockBestiarySearch = vi.fn<(q: string) => SearchResult[]>();
const mockShowCreature = vi.fn();
const mockShowBulkImport = vi.fn();
const mockShowSourceManager = vi.fn();
vi.mock("../../contexts/encounter-context.js", () => ({
useEncounterContext: () => ({
addCombatant: mockAddCombatant,
addFromBestiary: mockAddFromBestiary,
addMultipleFromBestiary: mockAddMultipleFromBestiary,
addFromPlayerCharacter: mockAddFromPlayerCharacter,
lastCreatureId: null,
}),
}));
vi.mock("../../contexts/bestiary-context.js", () => ({
useBestiaryContext: () => ({
search: mockBestiarySearch,
isLoaded: true,
}),
}));
vi.mock("../../contexts/player-characters-context.js", () => ({
usePlayerCharactersContext: () => ({
characters: mockPlayerCharacters,
}),
}));
vi.mock("../../contexts/side-panel-context.js", () => ({
useSidePanelContext: () => ({
showCreature: mockShowCreature,
showBulkImport: mockShowBulkImport,
showSourceManager: mockShowSourceManager,
panelView: { mode: "closed" },
}),
}));
let mockPlayerCharacters: PlayerCharacter[] = [];
const GOBLIN: SearchResult = {
name: "Goblin",
source: "MM",
sourceDisplayName: "Monster Manual",
ac: 15,
hp: 7,
dex: 14,
cr: "1/4",
initiativeProficiency: 0,
size: "Small",
type: "humanoid",
};
const ORC: SearchResult = {
name: "Orc",
source: "MM",
sourceDisplayName: "Monster Manual",
ac: 13,
hp: 15,
dex: 12,
cr: "1/2",
initiativeProficiency: 0,
size: "Medium",
type: "humanoid",
};
function renderActionBar() {
return renderHook(() => useActionBarState());
}
describe("useActionBarState", () => {
beforeEach(() => {
vi.clearAllMocks();
mockBestiarySearch.mockReturnValue([]);
mockPlayerCharacters = [];
});
describe("search and suggestions", () => {
it("starts with empty state", () => {
const { result } = renderActionBar();
expect(result.current.nameInput).toBe("");
expect(result.current.suggestions).toEqual([]);
expect(result.current.queued).toBeNull();
expect(result.current.browseMode).toBe(false);
});
it("searches bestiary when input >= 2 chars", () => {
mockBestiarySearch.mockReturnValue([GOBLIN]);
const { result } = renderActionBar();
act(() => result.current.handleNameChange("go"));
expect(mockBestiarySearch).toHaveBeenCalledWith("go");
expect(result.current.nameInput).toBe("go");
});
it("does not search when input < 2 chars", () => {
const { result } = renderActionBar();
act(() => result.current.handleNameChange("g"));
expect(mockBestiarySearch).not.toHaveBeenCalled();
});
it("matches player characters by name", () => {
mockPlayerCharacters = [
{
id: playerCharacterId("pc-1"),
name: "Gandalf",
ac: 15,
maxHp: 40,
},
];
mockBestiarySearch.mockReturnValue([]);
const { result } = renderActionBar();
act(() => result.current.handleNameChange("gan"));
expect(result.current.pcMatches).toHaveLength(1);
});
});
describe("queued creatures", () => {
it("queues a creature on click", () => {
const { result } = renderActionBar();
act(() => result.current.suggestionActions.clickSuggestion(GOBLIN));
expect(result.current.queued).toEqual({
result: GOBLIN,
count: 1,
});
});
it("increments count when same creature clicked again", () => {
const { result } = renderActionBar();
act(() => result.current.suggestionActions.clickSuggestion(GOBLIN));
act(() => result.current.suggestionActions.clickSuggestion(GOBLIN));
expect(result.current.queued?.count).toBe(2);
});
it("resets queue when different creature clicked", () => {
const { result } = renderActionBar();
act(() => result.current.suggestionActions.clickSuggestion(GOBLIN));
act(() => result.current.suggestionActions.clickSuggestion(ORC));
expect(result.current.queued).toEqual({
result: ORC,
count: 1,
});
});
it("confirmQueued calls addFromBestiary for count=1", () => {
const { result } = renderActionBar();
act(() => result.current.suggestionActions.clickSuggestion(GOBLIN));
act(() => result.current.suggestionActions.confirmQueued());
expect(mockAddFromBestiary).toHaveBeenCalledWith(GOBLIN);
expect(result.current.queued).toBeNull();
});
it("confirmQueued calls addMultipleFromBestiary for count>1", () => {
const { result } = renderActionBar();
act(() => result.current.suggestionActions.clickSuggestion(GOBLIN));
act(() => result.current.suggestionActions.clickSuggestion(GOBLIN));
act(() => result.current.suggestionActions.clickSuggestion(GOBLIN));
act(() => result.current.suggestionActions.confirmQueued());
expect(mockAddMultipleFromBestiary).toHaveBeenCalledWith(GOBLIN, 3);
});
it("clears queued when search text changes and creature no longer visible", () => {
mockBestiarySearch.mockReturnValue([GOBLIN]);
const { result } = renderActionBar();
act(() => result.current.handleNameChange("go"));
act(() => result.current.suggestionActions.clickSuggestion(GOBLIN));
// Change search to something that won't match
mockBestiarySearch.mockReturnValue([]);
act(() => result.current.handleNameChange("xyz"));
expect(result.current.queued).toBeNull();
});
});
describe("form submission", () => {
it("adds custom combatant on submit", () => {
const { result } = renderActionBar();
act(() => result.current.handleNameChange("Fighter"));
const event = {
preventDefault: vi.fn(),
} as unknown as React.SubmitEvent<HTMLFormElement>;
act(() => result.current.handleAdd(event));
expect(mockAddCombatant).toHaveBeenCalledWith("Fighter", undefined);
expect(result.current.nameInput).toBe("");
});
it("does not add when name is empty", () => {
const { result } = renderActionBar();
const event = {
preventDefault: vi.fn(),
} as unknown as React.SubmitEvent<HTMLFormElement>;
act(() => result.current.handleAdd(event));
expect(mockAddCombatant).not.toHaveBeenCalled();
});
it("passes custom init/ac/maxHp when set", () => {
const { result } = renderActionBar();
act(() => result.current.handleNameChange("Fighter"));
act(() => result.current.setCustomInit("15"));
act(() => result.current.setCustomAc("18"));
act(() => result.current.setCustomMaxHp("45"));
const event = {
preventDefault: vi.fn(),
} as unknown as React.SubmitEvent<HTMLFormElement>;
act(() => result.current.handleAdd(event));
expect(mockAddCombatant).toHaveBeenCalledWith("Fighter", {
initiative: 15,
ac: 18,
maxHp: 45,
});
});
it("does not submit in browse mode", () => {
const { result } = renderActionBar();
act(() => result.current.toggleBrowseMode());
act(() => result.current.handleNameChange("Fighter"));
const event = {
preventDefault: vi.fn(),
} as unknown as React.SubmitEvent<HTMLFormElement>;
act(() => result.current.handleAdd(event));
expect(mockAddCombatant).not.toHaveBeenCalled();
});
it("confirms queued on submit instead of adding by name", () => {
const { result } = renderActionBar();
act(() => result.current.suggestionActions.clickSuggestion(GOBLIN));
const event = {
preventDefault: vi.fn(),
} as unknown as React.SubmitEvent<HTMLFormElement>;
act(() => result.current.handleAdd(event));
expect(mockAddFromBestiary).toHaveBeenCalledWith(GOBLIN);
expect(mockAddCombatant).not.toHaveBeenCalled();
});
});
describe("browse mode", () => {
it("toggles browse mode", () => {
const { result } = renderActionBar();
act(() => result.current.toggleBrowseMode());
expect(result.current.browseMode).toBe(true);
act(() => result.current.toggleBrowseMode());
expect(result.current.browseMode).toBe(false);
});
it("handleBrowseSelect shows creature and exits browse mode", () => {
const { result } = renderActionBar();
act(() => result.current.toggleBrowseMode());
act(() => result.current.handleBrowseSelect(GOBLIN));
expect(mockShowCreature).toHaveBeenCalledWith("mm:goblin");
expect(result.current.browseMode).toBe(false);
expect(result.current.nameInput).toBe("");
});
});
describe("dismiss and clear", () => {
it("dismissSuggestions clears suggestions and queued", () => {
mockBestiarySearch.mockReturnValue([GOBLIN]);
const { result } = renderActionBar();
act(() => result.current.handleNameChange("go"));
act(() => result.current.suggestionActions.clickSuggestion(GOBLIN));
act(() => result.current.suggestionActions.dismiss());
expect(result.current.queued).toBeNull();
expect(result.current.suggestionIndex).toBe(-1);
});
it("clear resets everything", () => {
mockBestiarySearch.mockReturnValue([GOBLIN]);
const { result } = renderActionBar();
act(() => result.current.handleNameChange("go"));
act(() => result.current.suggestionActions.clickSuggestion(GOBLIN));
act(() => result.current.suggestionActions.clear());
expect(result.current.nameInput).toBe("");
expect(result.current.queued).toBeNull();
expect(result.current.suggestionIndex).toBe(-1);
});
});
});

View File

@@ -1,15 +1,38 @@
// @vitest-environment jsdom // @vitest-environment jsdom
import { act, renderHook } from "@testing-library/react"; import { act, renderHook } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest"; import type { ReactNode } from "react";
import { beforeAll, describe, expect, it, vi } from "vitest";
import { createTestAdapters } from "../../__tests__/adapters/in-memory-adapters.js";
import { AllProviders } from "../../__tests__/test-providers.js";
import { useBulkImport } from "../use-bulk-import.js"; import { useBulkImport } from "../use-bulk-import.js";
vi.mock("../../adapters/bestiary-index-adapter.js", () => ({ beforeAll(() => {
Object.defineProperty(globalThis, "matchMedia", {
writable: true,
value: vi.fn().mockImplementation((query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
});
const adapters = createTestAdapters();
adapters.bestiaryIndex = {
...adapters.bestiaryIndex,
getAllSourceCodes: () => ["MM", "VGM", "XGE"], getAllSourceCodes: () => ["MM", "VGM", "XGE"],
getDefaultFetchUrl: (code: string, baseUrl: string) => getDefaultFetchUrl: (code: string, baseUrl?: string) =>
`${baseUrl}${code}.json`, `${baseUrl}${code}.json`,
loadBestiaryIndex: () => ({ sources: {}, creatures: [] }), };
getSourceDisplayName: (code: string) => code,
})); function wrapper({ children }: { children: ReactNode }) {
return <AllProviders adapters={adapters}>{children}</AllProviders>;
}
/** Flush microtasks so the internal async IIFE inside startImport settles. */ /** Flush microtasks so the internal async IIFE inside startImport settles. */
function flushMicrotasks(): Promise<void> { function flushMicrotasks(): Promise<void> {
@@ -20,7 +43,7 @@ function flushMicrotasks(): Promise<void> {
describe("useBulkImport", () => { describe("useBulkImport", () => {
it("starts in idle state with all counters at 0", () => { it("starts in idle state with all counters at 0", () => {
const { result } = renderHook(() => useBulkImport()); const { result } = renderHook(() => useBulkImport(), { wrapper });
expect(result.current.state).toEqual({ expect(result.current.state).toEqual({
status: "idle", status: "idle",
total: 0, total: 0,
@@ -30,7 +53,7 @@ describe("useBulkImport", () => {
}); });
it("reset returns to idle state", async () => { it("reset returns to idle state", async () => {
const { result } = renderHook(() => useBulkImport()); const { result } = renderHook(() => useBulkImport(), { wrapper });
const isSourceCached = vi.fn().mockResolvedValue(true); const isSourceCached = vi.fn().mockResolvedValue(true);
const fetchAndCacheSource = vi.fn(); const fetchAndCacheSource = vi.fn();
@@ -51,7 +74,7 @@ describe("useBulkImport", () => {
}); });
it("goes straight to complete when all sources are cached", async () => { it("goes straight to complete when all sources are cached", async () => {
const { result } = renderHook(() => useBulkImport()); const { result } = renderHook(() => useBulkImport(), { wrapper });
const isSourceCached = vi.fn().mockResolvedValue(true); const isSourceCached = vi.fn().mockResolvedValue(true);
const fetchAndCacheSource = vi.fn(); const fetchAndCacheSource = vi.fn();
@@ -73,7 +96,7 @@ describe("useBulkImport", () => {
}); });
it("fetches uncached sources and completes", async () => { it("fetches uncached sources and completes", async () => {
const { result } = renderHook(() => useBulkImport()); const { result } = renderHook(() => useBulkImport(), { wrapper });
const isSourceCached = vi.fn().mockResolvedValue(false); const isSourceCached = vi.fn().mockResolvedValue(false);
const fetchAndCacheSource = vi.fn().mockResolvedValue(undefined); const fetchAndCacheSource = vi.fn().mockResolvedValue(undefined);
@@ -97,7 +120,7 @@ describe("useBulkImport", () => {
}); });
it("reports partial-failure when some sources fail", async () => { it("reports partial-failure when some sources fail", async () => {
const { result } = renderHook(() => useBulkImport()); const { result } = renderHook(() => useBulkImport(), { wrapper });
const isSourceCached = vi.fn().mockResolvedValue(false); const isSourceCached = vi.fn().mockResolvedValue(false);
const fetchAndCacheSource = vi const fetchAndCacheSource = vi
@@ -124,7 +147,7 @@ describe("useBulkImport", () => {
}); });
it("calls refreshCache after all batches complete", async () => { it("calls refreshCache after all batches complete", async () => {
const { result } = renderHook(() => useBulkImport()); const { result } = renderHook(() => useBulkImport(), { wrapper });
const isSourceCached = vi.fn().mockResolvedValue(false); const isSourceCached = vi.fn().mockResolvedValue(false);
const fetchAndCacheSource = vi.fn().mockResolvedValue(undefined); const fetchAndCacheSource = vi.fn().mockResolvedValue(undefined);

View File

@@ -0,0 +1,348 @@
// @vitest-environment jsdom
import type { Creature, CreatureId, PlayerCharacter } from "@initiative/domain";
import { combatantId, creatureId, playerCharacterId } from "@initiative/domain";
import { renderHook, waitFor } from "@testing-library/react";
import type { ReactNode } from "react";
import { beforeAll, describe, expect, it, vi } from "vitest";
import { createTestAdapters } from "../../__tests__/adapters/in-memory-adapters.js";
import {
buildCombatant,
buildCreature,
buildEncounter,
} from "../../__tests__/factories/index.js";
import { AllProviders } from "../../__tests__/test-providers.js";
import { useDifficultyBreakdown } from "../use-difficulty-breakdown.js";
import { useRulesEdition } from "../use-rules-edition.js";
beforeAll(() => {
Object.defineProperty(globalThis, "matchMedia", {
writable: true,
value: vi.fn().mockImplementation((query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
});
const pcId1 = playerCharacterId("pc-1");
const goblinCreature = buildCreature({
id: creatureId("srd:goblin"),
name: "Goblin",
cr: "1/4",
source: "srd",
sourceDisplayName: "SRD",
});
function makeWrapper(options: {
encounter: ReturnType<typeof buildEncounter>;
playerCharacters?: PlayerCharacter[];
creatures?: Map<CreatureId, Creature>;
}) {
const adapters = createTestAdapters({
encounter: options.encounter,
playerCharacters: options.playerCharacters ?? [],
creatures: options.creatures,
});
return ({ children }: { children: ReactNode }) => (
<AllProviders adapters={adapters}>{children}</AllProviders>
);
}
describe("useDifficultyBreakdown", () => {
it("returns null when no leveled PCs", () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c-1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c-2"),
name: "Goblin",
creatureId: goblinCreature.id,
}),
],
}),
playerCharacters: [{ id: pcId1, name: "Hero", ac: 15, maxHp: 30 }],
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
const { result } = renderHook(() => useDifficultyBreakdown(), {
wrapper,
});
expect(result.current).toBeNull();
});
it("returns null when no monsters with CR", () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c-1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c-2"),
name: "Custom",
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 5 },
],
});
const { result } = renderHook(() => useDifficultyBreakdown(), {
wrapper,
});
expect(result.current).toBeNull();
});
it("returns per-combatant entries split by side", async () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c-1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c-2"),
name: "Goblin",
creatureId: goblinCreature.id,
}),
buildCombatant({
id: combatantId("c-3"),
name: "Custom Thug",
cr: "2",
}),
buildCombatant({
id: combatantId("c-4"),
name: "Bandit",
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 5 },
],
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
const { result } = renderHook(() => useDifficultyBreakdown(), {
wrapper,
});
await waitFor(() => {
const breakdown = result.current;
expect(breakdown).not.toBeNull();
expect(breakdown?.pcCount).toBe(1);
// CR 1/4 = 50 + CR 2 = 450 -> total 500
expect(breakdown?.totalMonsterXp).toBe(500);
// PC in party column
expect(breakdown?.partyCombatants).toHaveLength(1);
expect(breakdown?.partyCombatants[0].combatant.name).toBe("Hero");
expect(breakdown?.partyCombatants[0].side).toBe("party");
expect(breakdown?.partyCombatants[0].level).toBe(5);
// Enemies: goblin, thug, bandit
expect(breakdown?.enemyCombatants).toHaveLength(3);
const goblin = breakdown?.enemyCombatants[0];
expect(goblin?.cr).toBe("1/4");
expect(goblin?.xp).toBe(50);
expect(goblin?.source).toBe("SRD");
expect(goblin?.editable).toBe(false);
expect(goblin?.side).toBe("enemy");
const thug = breakdown?.enemyCombatants[1];
expect(thug?.cr).toBe("2");
expect(thug?.xp).toBe(450);
expect(thug?.source).toBeNull();
expect(thug?.editable).toBe(true);
const bandit = breakdown?.enemyCombatants[2];
expect(bandit?.cr).toBeNull();
expect(bandit?.xp).toBeNull();
expect(bandit?.editable).toBe(true);
});
});
it("bestiary combatant with missing creature is non-editable with null CR", () => {
const missingCreatureId = creatureId("creature-missing");
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c-1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c-2"),
name: "Ghost",
creatureId: missingCreatureId,
}),
buildCombatant({
id: combatantId("c-3"),
name: "Thug",
cr: "1",
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 5 },
],
});
const { result } = renderHook(() => useDifficultyBreakdown(), {
wrapper,
});
const breakdown = result.current;
expect(breakdown).not.toBeNull();
const ghost = breakdown?.enemyCombatants[0];
expect(ghost?.cr).toBeNull();
expect(ghost?.xp).toBeNull();
expect(ghost?.editable).toBe(false);
});
it("PC combatants appear in partyCombatants with level", async () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c-1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c-2"),
name: "Goblin",
creatureId: goblinCreature.id,
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 1 },
],
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
const { result } = renderHook(() => useDifficultyBreakdown(), {
wrapper,
});
await waitFor(() => {
expect(result.current?.partyCombatants).toHaveLength(1);
expect(result.current?.partyCombatants[0].combatant.name).toBe("Hero");
expect(result.current?.partyCombatants[0].level).toBe(1);
expect(result.current?.partyCombatants[0].side).toBe("party");
});
});
it("combatant with explicit side override is placed correctly", () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c-1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c-2"),
name: "Allied Guard",
creatureId: goblinCreature.id,
side: "party",
}),
buildCombatant({
id: combatantId("c-3"),
name: "Thug",
cr: "1",
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 5 },
],
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
const { result } = renderHook(() => useDifficultyBreakdown(), {
wrapper,
});
const breakdown = result.current;
expect(breakdown).not.toBeNull();
// Allied Guard should be in party column
expect(breakdown?.partyCombatants).toHaveLength(2);
expect(breakdown?.partyCombatants[1].combatant.name).toBe("Allied Guard");
expect(breakdown?.partyCombatants[1].side).toBe("party");
// Thug in enemy column
expect(breakdown?.enemyCombatants).toHaveLength(1);
expect(breakdown?.enemyCombatants[0].combatant.name).toBe("Thug");
});
it("exposes encounterMultiplier and adjustedXp for 5e edition", async () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c-1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c-2"),
name: "Goblin",
creatureId: goblinCreature.id,
}),
buildCombatant({
id: combatantId("c-3"),
name: "Thug",
cr: "1",
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 1 },
],
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
const { result: editionResult } = renderHook(() => useRulesEdition(), {
wrapper,
});
editionResult.current.setEdition("5e");
try {
const { result } = renderHook(() => useDifficultyBreakdown(), {
wrapper,
});
await waitFor(() => {
const breakdown = result.current;
expect(breakdown).not.toBeNull();
// 2 enemy monsters, 1 PC (<3) → base x1.5, shift up → x2
expect(breakdown?.encounterMultiplier).toBe(2);
// CR 1/4 (50) + CR 1 (200) = 250, x2 = 500
expect(breakdown?.totalMonsterXp).toBe(250);
expect(breakdown?.adjustedXp).toBe(500);
expect(breakdown?.thresholds).toHaveLength(4);
});
} finally {
editionResult.current.setEdition("5.5e");
}
});
});

View File

@@ -0,0 +1,173 @@
// @vitest-environment jsdom
import type { Creature, CreatureId, PlayerCharacter } from "@initiative/domain";
import { combatantId, creatureId, playerCharacterId } from "@initiative/domain";
import { renderHook, waitFor } from "@testing-library/react";
import type { ReactNode } from "react";
import { beforeAll, describe, expect, it, vi } from "vitest";
import { createTestAdapters } from "../../__tests__/adapters/in-memory-adapters.js";
import {
buildCombatant,
buildCreature,
buildEncounter,
} from "../../__tests__/factories/index.js";
import { AllProviders } from "../../__tests__/test-providers.js";
import { useDifficulty } from "../use-difficulty.js";
beforeAll(() => {
Object.defineProperty(globalThis, "matchMedia", {
writable: true,
value: vi.fn().mockImplementation((query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
});
const pcId1 = playerCharacterId("pc-1");
const goblinCreature = buildCreature({
id: creatureId("srd:goblin"),
name: "Goblin",
cr: "1/4",
});
function makeWrapper(options: {
encounter: ReturnType<typeof buildEncounter>;
playerCharacters?: PlayerCharacter[];
creatures?: Map<CreatureId, Creature>;
}) {
const adapters = createTestAdapters({
encounter: options.encounter,
playerCharacters: options.playerCharacters ?? [],
creatures: options.creatures,
});
return ({ children }: { children: ReactNode }) => (
<AllProviders adapters={adapters}>{children}</AllProviders>
);
}
describe("useDifficulty with custom combatant CRs", () => {
it("includes custom combatant with cr field in monster XP", () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c-1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c-2"),
name: "Custom Thug",
cr: "2",
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 1 },
],
});
const { result } = renderHook(() => useDifficulty(), { wrapper });
expect(result.current).not.toBeNull();
expect(result.current?.totalMonsterXp).toBe(450);
});
it("uses bestiary CR when combatant has both creatureId and cr", async () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c-1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c-2"),
name: "Goblin",
creatureId: goblinCreature.id,
cr: "5",
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 1 },
],
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
const { result } = renderHook(() => useDifficulty(), { wrapper });
await waitFor(() => {
expect(result.current).not.toBeNull();
// Should use bestiary CR 1/4 (50 XP), not the manual cr "5" (1800 XP)
expect(result.current?.totalMonsterXp).toBe(50);
});
});
it("mixes bestiary and custom-with-CR combatants correctly", async () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c-1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c-2"),
name: "Goblin",
creatureId: goblinCreature.id,
}),
buildCombatant({
id: combatantId("c-3"),
name: "Custom",
cr: "1",
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 5 },
],
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
const { result } = renderHook(() => useDifficulty(), { wrapper });
await waitFor(() => {
expect(result.current).not.toBeNull();
// CR 1/4 = 50 XP, CR 1 = 200 XP → total 250
expect(result.current?.totalMonsterXp).toBe(250);
});
});
it("custom combatant without CR is still excluded", () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c-1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c-2"),
name: "Custom Monster",
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 1 },
],
});
const { result } = renderHook(() => useDifficulty(), { wrapper });
expect(result.current).toBeNull();
});
});

View File

@@ -1,220 +0,0 @@
// @vitest-environment jsdom
import type {
Combatant,
CreatureId,
Encounter,
PlayerCharacter,
} from "@initiative/domain";
import { combatantId, creatureId, playerCharacterId } from "@initiative/domain";
import { renderHook } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("../../contexts/encounter-context.js", () => ({
useEncounterContext: vi.fn(),
}));
vi.mock("../../contexts/player-characters-context.js", () => ({
usePlayerCharactersContext: vi.fn(),
}));
vi.mock("../../contexts/bestiary-context.js", () => ({
useBestiaryContext: vi.fn(),
}));
import { useBestiaryContext } from "../../contexts/bestiary-context.js";
import { useEncounterContext } from "../../contexts/encounter-context.js";
import { usePlayerCharactersContext } from "../../contexts/player-characters-context.js";
import { useDifficulty } from "../use-difficulty.js";
const mockEncounterContext = vi.mocked(useEncounterContext);
const mockPlayerCharactersContext = vi.mocked(usePlayerCharactersContext);
const mockBestiaryContext = vi.mocked(useBestiaryContext);
const pcId1 = playerCharacterId("pc-1");
const pcId2 = playerCharacterId("pc-2");
const crId1 = creatureId("creature-1");
const _crId2 = creatureId("creature-2");
function setup(options: {
combatants: Combatant[];
characters: PlayerCharacter[];
creatures: Map<CreatureId, { cr: string }>;
}) {
const encounter = {
combatants: options.combatants,
activeIndex: 0,
roundNumber: 1,
} as Encounter;
mockEncounterContext.mockReturnValue({
encounter,
} as ReturnType<typeof useEncounterContext>);
mockPlayerCharactersContext.mockReturnValue({
characters: options.characters,
} as ReturnType<typeof usePlayerCharactersContext>);
mockBestiaryContext.mockReturnValue({
getCreature: (id: CreatureId) => options.creatures.get(id),
} as ReturnType<typeof useBestiaryContext>);
}
beforeEach(() => {
vi.clearAllMocks();
});
describe("useDifficulty", () => {
it("returns difficulty result for leveled PCs and bestiary monsters", () => {
setup({
combatants: [
{ id: combatantId("c1"), name: "Hero", playerCharacterId: pcId1 },
{ id: combatantId("c2"), name: "Goblin", creatureId: crId1 },
],
characters: [{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 1 }],
creatures: new Map([[crId1, { cr: "1/4" }]]),
});
const { result } = renderHook(() => useDifficulty());
expect(result.current).not.toBeNull();
expect(result.current?.tier).toBe("low");
expect(result.current?.totalMonsterXp).toBe(50);
});
describe("returns null when data is insufficient (ED-2)", () => {
it("returns null when encounter has no combatants", () => {
setup({ combatants: [], characters: [], creatures: new Map() });
const { result } = renderHook(() => useDifficulty());
expect(result.current).toBeNull();
});
it("returns null when only custom combatants (no creatureId)", () => {
setup({
combatants: [
{
id: combatantId("c1"),
name: "Custom",
playerCharacterId: pcId1,
},
],
characters: [{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 5 }],
creatures: new Map(),
});
const { result } = renderHook(() => useDifficulty());
expect(result.current).toBeNull();
});
it("returns null when bestiary monsters present but no PC combatants", () => {
setup({
combatants: [
{ id: combatantId("c1"), name: "Goblin", creatureId: crId1 },
],
characters: [],
creatures: new Map([[crId1, { cr: "1" }]]),
});
const { result } = renderHook(() => useDifficulty());
expect(result.current).toBeNull();
});
it("returns null when PC combatants have no level", () => {
setup({
combatants: [
{
id: combatantId("c1"),
name: "Hero",
playerCharacterId: pcId1,
},
{ id: combatantId("c2"), name: "Goblin", creatureId: crId1 },
],
characters: [{ id: pcId1, name: "Hero", ac: 15, maxHp: 30 }],
creatures: new Map([[crId1, { cr: "1" }]]),
});
const { result } = renderHook(() => useDifficulty());
expect(result.current).toBeNull();
});
it("returns null when PC combatant references unknown player character", () => {
setup({
combatants: [
{
id: combatantId("c1"),
name: "Hero",
playerCharacterId: pcId2,
},
{ id: combatantId("c2"), name: "Goblin", creatureId: crId1 },
],
characters: [{ id: pcId1, name: "Other", ac: 15, maxHp: 30, level: 5 }],
creatures: new Map([[crId1, { cr: "1" }]]),
});
const { result } = renderHook(() => useDifficulty());
expect(result.current).toBeNull();
});
});
it("handles mixed combatants: only leveled PCs and bestiary monsters contribute", () => {
// Party: one leveled PC, one without level (excluded)
// Monsters: one bestiary creature, one custom (excluded)
setup({
combatants: [
{
id: combatantId("c1"),
name: "Leveled",
playerCharacterId: pcId1,
},
{
id: combatantId("c2"),
name: "No Level",
playerCharacterId: pcId2,
},
{ id: combatantId("c3"), name: "Goblin", creatureId: crId1 },
{ id: combatantId("c4"), name: "Custom Monster" },
],
characters: [
{ id: pcId1, name: "Leveled", ac: 15, maxHp: 30, level: 1 },
{ id: pcId2, name: "No Level", ac: 12, maxHp: 20 },
],
creatures: new Map([[crId1, { cr: "1" }]]),
});
const { result } = renderHook(() => useDifficulty());
expect(result.current).not.toBeNull();
// 1 level-1 PC: budget low=50, mod=75, high=100
// 1 CR 1 monster: 200 XP → high (200 >= 100)
expect(result.current?.tier).toBe("high");
expect(result.current?.totalMonsterXp).toBe(200);
expect(result.current?.partyBudget.low).toBe(50);
});
it("includes duplicate PC combatants in budget", () => {
// Same PC added twice → counts twice
setup({
combatants: [
{
id: combatantId("c1"),
name: "Hero 1",
playerCharacterId: pcId1,
},
{
id: combatantId("c2"),
name: "Hero 2",
playerCharacterId: pcId1,
},
{ id: combatantId("c3"), name: "Goblin", creatureId: crId1 },
],
characters: [{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 1 }],
creatures: new Map([[crId1, { cr: "1/4" }]]),
});
const { result } = renderHook(() => useDifficulty());
expect(result.current).not.toBeNull();
// 2x level 1: budget low=100
expect(result.current?.partyBudget.low).toBe(100);
});
});

View File

@@ -0,0 +1,427 @@
// @vitest-environment jsdom
import type { Creature, CreatureId, PlayerCharacter } from "@initiative/domain";
import { combatantId, creatureId, playerCharacterId } from "@initiative/domain";
import { renderHook, waitFor } from "@testing-library/react";
import type { ReactNode } from "react";
import { beforeAll, describe, expect, it, vi } from "vitest";
import { createTestAdapters } from "../../__tests__/adapters/in-memory-adapters.js";
import {
buildCombatant,
buildCreature,
buildEncounter,
} from "../../__tests__/factories/index.js";
import { AllProviders } from "../../__tests__/test-providers.js";
import { useDifficulty } from "../use-difficulty.js";
import { useRulesEdition } from "../use-rules-edition.js";
beforeAll(() => {
Object.defineProperty(globalThis, "matchMedia", {
writable: true,
value: vi.fn().mockImplementation((query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
});
const pcId1 = playerCharacterId("pc-1");
const pcId2 = playerCharacterId("pc-2");
const crId1 = creatureId("srd:goblin");
const goblinCreature = buildCreature({
id: crId1,
name: "Goblin",
cr: "1/4",
});
function makeWrapper(options: {
encounter: ReturnType<typeof buildEncounter>;
playerCharacters?: PlayerCharacter[];
creatures?: Map<CreatureId, Creature>;
}) {
const adapters = createTestAdapters({
encounter: options.encounter,
playerCharacters: options.playerCharacters ?? [],
creatures: options.creatures,
});
return ({ children }: { children: ReactNode }) => (
<AllProviders adapters={adapters}>{children}</AllProviders>
);
}
describe("useDifficulty", () => {
it("returns difficulty result for leveled PCs and bestiary monsters", async () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c2"),
name: "Goblin",
creatureId: crId1,
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 1 },
],
creatures: new Map([[crId1, goblinCreature]]),
});
const { result } = renderHook(() => useDifficulty(), { wrapper });
await waitFor(() => {
expect(result.current).not.toBeNull();
expect(result.current?.tier).toBe(1);
expect(result.current?.totalMonsterXp).toBe(50);
});
});
describe("returns null when data is insufficient (ED-2)", () => {
it("returns null when encounter has no combatants", () => {
const wrapper = makeWrapper({
encounter: buildEncounter({ combatants: [] }),
});
const { result } = renderHook(() => useDifficulty(), { wrapper });
expect(result.current).toBeNull();
});
it("returns null when only custom combatants (no creatureId)", () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c1"),
name: "Custom",
playerCharacterId: pcId1,
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 5 },
],
});
const { result } = renderHook(() => useDifficulty(), { wrapper });
expect(result.current).toBeNull();
});
it("returns null when bestiary monsters present but no PC combatants", () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c1"),
name: "Goblin",
creatureId: crId1,
}),
],
}),
creatures: new Map([[crId1, goblinCreature]]),
});
const { result } = renderHook(() => useDifficulty(), { wrapper });
expect(result.current).toBeNull();
});
it("returns null when PC combatants have no level", () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c2"),
name: "Goblin",
creatureId: crId1,
}),
],
}),
playerCharacters: [{ id: pcId1, name: "Hero", ac: 15, maxHp: 30 }],
creatures: new Map([[crId1, goblinCreature]]),
});
const { result } = renderHook(() => useDifficulty(), { wrapper });
expect(result.current).toBeNull();
});
it("returns null when PC combatant references unknown player character", () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c1"),
name: "Hero",
playerCharacterId: pcId2,
}),
buildCombatant({
id: combatantId("c2"),
name: "Goblin",
creatureId: crId1,
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Other", ac: 15, maxHp: 30, level: 5 },
],
creatures: new Map([[crId1, goblinCreature]]),
});
const { result } = renderHook(() => useDifficulty(), { wrapper });
expect(result.current).toBeNull();
});
});
it("handles mixed combatants: only leveled PCs and CR-bearing monsters contribute", async () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c1"),
name: "Leveled",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c2"),
name: "No Level",
playerCharacterId: pcId2,
}),
buildCombatant({
id: combatantId("c3"),
name: "Goblin",
creatureId: crId1,
}),
buildCombatant({
id: combatantId("c4"),
name: "Custom Monster",
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Leveled", ac: 15, maxHp: 30, level: 1 },
{ id: pcId2, name: "No Level", ac: 12, maxHp: 20 },
],
creatures: new Map([[crId1, goblinCreature]]),
});
const { result } = renderHook(() => useDifficulty(), { wrapper });
await waitFor(() => {
expect(result.current).not.toBeNull();
// 1 level-1 PC: budget low=50, mod=75, high=100
// CR 1/4 = 50 XP -> low (50 >= 50)
expect(result.current?.tier).toBe(1);
expect(result.current?.totalMonsterXp).toBe(50);
expect(result.current?.thresholds[0].value).toBe(50);
});
});
it("includes duplicate PC combatants in budget", async () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c1"),
name: "Hero 1",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c2"),
name: "Hero 2",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c3"),
name: "Goblin",
creatureId: crId1,
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 1 },
],
creatures: new Map([[crId1, goblinCreature]]),
});
const { result } = renderHook(() => useDifficulty(), { wrapper });
await waitFor(() => {
expect(result.current).not.toBeNull();
// 2x level 1: budget low=100
expect(result.current?.thresholds[0].value).toBe(100);
});
});
it("combatant toggled to party side subtracts XP", async () => {
const bugbear = buildCreature({
id: creatureId("srd:bugbear"),
name: "Bugbear",
cr: "1",
});
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c2"),
name: "Allied Guard",
creatureId: bugbear.id,
side: "party",
}),
buildCombatant({
id: combatantId("c3"),
name: "Thug",
cr: "1",
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 1 },
],
creatures: new Map([[bugbear.id, bugbear]]),
});
const { result } = renderHook(() => useDifficulty(), { wrapper });
await waitFor(() => {
expect(result.current).not.toBeNull();
// Thug CR 1 = 200 XP, Allied Guard CR 1 = 200 XP subtracted, net = 0
expect(result.current?.totalMonsterXp).toBe(0);
expect(result.current?.tier).toBe(0);
});
});
it("default side resolution: PC -> party, non-PC -> enemy", async () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c2"),
name: "Goblin",
creatureId: crId1,
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 3 },
],
creatures: new Map([[crId1, goblinCreature]]),
});
const { result } = renderHook(() => useDifficulty(), { wrapper });
await waitFor(() => {
expect(result.current).not.toBeNull();
// Level 3 budget: low=150, mod=225, high=400
// CR 1/4 = 50 XP -> trivial
expect(result.current?.thresholds[0].value).toBe(150);
expect(result.current?.totalMonsterXp).toBe(50);
expect(result.current?.tier).toBe(0);
});
});
it("returns 2014 difficulty when edition is 5e", async () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c2"),
name: "Goblin",
creatureId: crId1,
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 1 },
],
creatures: new Map([[crId1, goblinCreature]]),
});
// Set edition via the hook's external store
const { result: editionResult } = renderHook(() => useRulesEdition(), {
wrapper,
});
editionResult.current.setEdition("5e");
try {
const { result } = renderHook(() => useDifficulty(), { wrapper });
await waitFor(() => {
expect(result.current).not.toBeNull();
// 2014: 4 thresholds with Easy/Medium/Hard/Deadly labels
expect(result.current?.thresholds).toHaveLength(4);
expect(result.current?.thresholds[0].label).toBe("Easy");
// CR 1/4 = 50 XP, 1 PC (<3) shifts x1 → x1.5, adjusted = 75
expect(result.current?.encounterMultiplier).toBe(1.5);
expect(result.current?.adjustedXp).toBe(75);
});
} finally {
editionResult.current.setEdition("5.5e");
}
});
it("custom combatant with CR on party side subtracts XP", async () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c2"),
name: "Ally",
cr: "2",
side: "party",
}),
buildCombatant({
id: combatantId("c3"),
name: "Goblin",
creatureId: crId1,
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 5 },
],
creatures: new Map([[crId1, goblinCreature]]),
});
const { result } = renderHook(() => useDifficulty(), { wrapper });
await waitFor(() => {
expect(result.current).not.toBeNull();
// CR 1/4 = 50 XP enemy, CR 2 = 450 XP ally subtracted, net = 0 (floored)
expect(result.current?.totalMonsterXp).toBe(0);
});
});
});

View File

@@ -0,0 +1,234 @@
// @vitest-environment jsdom
import "@testing-library/jest-dom/vitest";
import type { ExportBundle } from "@initiative/domain";
import { combatantId, playerCharacterId } from "@initiative/domain";
import { act, renderHook } from "@testing-library/react";
import type { ReactNode } from "react";
import { beforeAll, describe, expect, it, vi } from "vitest";
import { createTestAdapters } from "../../__tests__/adapters/in-memory-adapters.js";
import {
buildCombatant,
buildEncounter,
} from "../../__tests__/factories/index.js";
import { AllProviders } from "../../__tests__/test-providers.js";
import { useEncounterContext } from "../../contexts/encounter-context.js";
import { useEncounterExportImport } from "../use-encounter-export-import.js";
beforeAll(() => {
Object.defineProperty(globalThis, "matchMedia", {
writable: true,
value: vi.fn().mockImplementation((query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
});
function wrapper({ children }: { children: ReactNode }) {
return <AllProviders>{children}</AllProviders>;
}
function wrapperWithEncounter(encounter: ReturnType<typeof buildEncounter>) {
const adapters = createTestAdapters({ encounter });
return function Wrapper({ children }: { children: ReactNode }) {
return <AllProviders adapters={adapters}>{children}</AllProviders>;
};
}
const VALID_BUNDLE: ExportBundle = {
version: 1,
exportedAt: "2026-01-01T00:00:00.000Z",
encounter: buildEncounter({
combatants: [buildCombatant({ id: combatantId("c-1"), name: "Imported" })],
}),
undoStack: [],
redoStack: [],
playerCharacters: [
{
id: playerCharacterId("pc-1"),
name: "Hero",
ac: 16,
maxHp: 30,
},
],
};
describe("useEncounterExportImport", () => {
describe("import via clipboard", () => {
it("imports valid JSON into empty encounter without error", () => {
const { result } = renderHook(() => useEncounterExportImport(), {
wrapper,
});
act(() => {
result.current.handleImportClipboard(JSON.stringify(VALID_BUNDLE));
});
// Import should succeed without error and not show confirm
expect(result.current.importError).toBeNull();
expect(result.current.showImportConfirm).toBe(false);
});
it("sets error for invalid JSON", () => {
const { result } = renderHook(() => useEncounterExportImport(), {
wrapper,
});
act(() => {
result.current.handleImportClipboard("not json{{{");
});
expect(result.current.importError).toBe("Invalid file format");
});
it("sets error for valid JSON that fails validation", () => {
const { result } = renderHook(() => useEncounterExportImport(), {
wrapper,
});
act(() => {
result.current.handleImportClipboard(JSON.stringify({ version: 999 }));
});
expect(result.current.importError).toBe("Invalid file format");
});
it("shows confirm dialog when encounter is not empty", () => {
const encounter = buildEncounter({
combatants: [
buildCombatant({ id: combatantId("c-1"), name: "Existing" }),
],
});
const { result } = renderHook(() => useEncounterExportImport(), {
wrapper: wrapperWithEncounter(encounter),
});
act(() => {
result.current.handleImportClipboard(JSON.stringify(VALID_BUNDLE));
});
expect(result.current.showImportConfirm).toBe(true);
expect(result.current.importError).toBeNull();
});
it("handleImportConfirm clears confirm dialog", () => {
const encounter = buildEncounter({
combatants: [
buildCombatant({ id: combatantId("c-1"), name: "Existing" }),
],
});
const { result } = renderHook(() => useEncounterExportImport(), {
wrapper: wrapperWithEncounter(encounter),
});
act(() => {
result.current.handleImportClipboard(JSON.stringify(VALID_BUNDLE));
});
expect(result.current.showImportConfirm).toBe(true);
act(() => {
result.current.handleImportConfirm();
});
expect(result.current.showImportConfirm).toBe(false);
});
it("handleImportCancel clears pending without applying", () => {
const encounter = buildEncounter({
combatants: [
buildCombatant({ id: combatantId("c-1"), name: "Existing" }),
],
});
const { result } = renderHook(
() => ({
exportImport: useEncounterExportImport(),
encounter: useEncounterContext(),
}),
{ wrapper: wrapperWithEncounter(encounter) },
);
act(() => {
result.current.exportImport.handleImportClipboard(
JSON.stringify(VALID_BUNDLE),
);
});
act(() => {
result.current.exportImport.handleImportCancel();
});
expect(result.current.exportImport.showImportConfirm).toBe(false);
expect(result.current.encounter.encounter.combatants[0].name).toBe(
"Existing",
);
});
});
describe("export", () => {
it("handleExportDownload calls triggerDownload", () => {
const encounter = buildEncounter({
combatants: [
buildCombatant({ id: combatantId("c-1"), name: "Fighter" }),
],
});
const { result } = renderHook(() => useEncounterExportImport(), {
wrapper: wrapperWithEncounter(encounter),
});
// triggerDownload creates a blob URL and clicks an anchor — just verify it doesn't throw
expect(() => {
act(() => {
result.current.handleExportDownload(false, "test-export.json");
});
}).not.toThrow();
});
});
describe("dialog state", () => {
it("toggles export method dialog", () => {
const { result } = renderHook(() => useEncounterExportImport(), {
wrapper,
});
expect(result.current.showExportMethod).toBe(false);
act(() => result.current.setShowExportMethod(true));
expect(result.current.showExportMethod).toBe(true);
});
it("toggles import method dialog", () => {
const { result } = renderHook(() => useEncounterExportImport(), {
wrapper,
});
expect(result.current.showImportMethod).toBe(false);
act(() => result.current.setShowImportMethod(true));
expect(result.current.showImportMethod).toBe(true);
});
it("clears import error", () => {
const { result } = renderHook(() => useEncounterExportImport(), {
wrapper,
});
act(() => {
result.current.handleImportClipboard("bad json");
});
expect(result.current.importError).toBe("Invalid file format");
act(() => result.current.setImportError(null));
expect(result.current.importError).toBeNull();
});
});
});

View File

@@ -1,28 +1,37 @@
// @vitest-environment jsdom // @vitest-environment jsdom
import type { BestiaryIndexEntry, PlayerCharacter } from "@initiative/domain"; import type { PlayerCharacter } from "@initiative/domain";
import { combatantId, creatureId, playerCharacterId } from "@initiative/domain"; import { combatantId, creatureId, playerCharacterId } from "@initiative/domain";
import { act, renderHook } from "@testing-library/react"; import { act, renderHook } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest"; import type { ReactNode } from "react";
import { beforeAll, describe, expect, it, vi } from "vitest";
import { createTestAdapters } from "../../__tests__/adapters/in-memory-adapters.js";
import { AllProviders } from "../../__tests__/test-providers.js";
import type { SearchResult } from "../use-bestiary.js";
import { useEncounter } from "../use-encounter.js"; import { useEncounter } from "../use-encounter.js";
vi.mock("../../persistence/encounter-storage.js", () => ({ beforeAll(() => {
loadEncounter: vi.fn().mockReturnValue(null), Object.defineProperty(globalThis, "matchMedia", {
saveEncounter: vi.fn(), writable: true,
})); value: vi.fn().mockImplementation((query: string) => ({
matches: false,
const { loadEncounter: mockLoad, saveEncounter: mockSave } = media: query,
await vi.importMock<typeof import("../../persistence/encounter-storage.js")>( onchange: null,
"../../persistence/encounter-storage.js", addListener: vi.fn(),
); removeListener: vi.fn(),
addEventListener: vi.fn(),
describe("useEncounter", () => { removeEventListener: vi.fn(),
beforeEach(() => { dispatchEvent: vi.fn(),
vi.clearAllMocks(); })),
mockLoad.mockReturnValue(null); });
}); });
function wrapper({ children }: { children: ReactNode }) {
return <AllProviders>{children}</AllProviders>;
}
describe("useEncounter", () => {
it("initializes with empty encounter when persistence returns null", () => { it("initializes with empty encounter when persistence returns null", () => {
const { result } = renderHook(() => useEncounter()); const { result } = renderHook(() => useEncounter(), { wrapper });
expect(result.current.encounter.combatants).toEqual([]); expect(result.current.encounter.combatants).toEqual([]);
expect(result.current.encounter.activeIndex).toBe(0); expect(result.current.encounter.activeIndex).toBe(0);
@@ -32,13 +41,33 @@ describe("useEncounter", () => {
it("initializes from stored encounter", () => { it("initializes from stored encounter", () => {
const stored = { const stored = {
combatants: [{ id: combatantId("c-1"), name: "Goblin" }], combatants: [
{
id: combatantId("c-1"),
name: "Goblin",
initiative: undefined,
maxHp: undefined,
currentHp: undefined,
tempHp: undefined,
ac: undefined,
conditions: [],
concentrating: false,
creatureId: undefined,
playerCharacterId: undefined,
color: undefined,
icon: undefined,
},
],
activeIndex: 0, activeIndex: 0,
roundNumber: 2, roundNumber: 2,
}; };
mockLoad.mockReturnValue(stored); const adapters = createTestAdapters({ encounter: stored });
const { result } = renderHook(() => useEncounter()); const { result } = renderHook(() => useEncounter(), {
wrapper: ({ children }: { children: ReactNode }) => (
<AllProviders adapters={adapters}>{children}</AllProviders>
),
});
expect(result.current.encounter.combatants).toHaveLength(1); expect(result.current.encounter.combatants).toHaveLength(1);
expect(result.current.encounter.roundNumber).toBe(2); expect(result.current.encounter.roundNumber).toBe(2);
@@ -46,7 +75,7 @@ describe("useEncounter", () => {
}); });
it("addCombatant adds a combatant with incremental IDs and persists", () => { it("addCombatant adds a combatant with incremental IDs and persists", () => {
const { result } = renderHook(() => useEncounter()); const { result } = renderHook(() => useEncounter(), { wrapper });
act(() => result.current.addCombatant("Goblin")); act(() => result.current.addCombatant("Goblin"));
act(() => result.current.addCombatant("Orc")); act(() => result.current.addCombatant("Orc"));
@@ -55,11 +84,10 @@ describe("useEncounter", () => {
expect(result.current.encounter.combatants[0].name).toBe("Goblin"); expect(result.current.encounter.combatants[0].name).toBe("Goblin");
expect(result.current.encounter.combatants[1].name).toBe("Orc"); expect(result.current.encounter.combatants[1].name).toBe("Orc");
expect(result.current.isEmpty).toBe(false); expect(result.current.isEmpty).toBe(false);
expect(mockSave).toHaveBeenCalled();
}); });
it("removeCombatant removes a combatant and persists", () => { it("removeCombatant removes a combatant and persists", () => {
const { result } = renderHook(() => useEncounter()); const { result } = renderHook(() => useEncounter(), { wrapper });
act(() => result.current.addCombatant("Goblin")); act(() => result.current.addCombatant("Goblin"));
const id = result.current.encounter.combatants[0].id; const id = result.current.encounter.combatants[0].id;
@@ -71,7 +99,7 @@ describe("useEncounter", () => {
}); });
it("advanceTurn and retreatTurn update encounter state", () => { it("advanceTurn and retreatTurn update encounter state", () => {
const { result } = renderHook(() => useEncounter()); const { result } = renderHook(() => useEncounter(), { wrapper });
act(() => result.current.addCombatant("Goblin")); act(() => result.current.addCombatant("Goblin"));
act(() => result.current.addCombatant("Orc")); act(() => result.current.addCombatant("Orc"));
@@ -86,7 +114,7 @@ describe("useEncounter", () => {
}); });
it("clearEncounter resets to empty and resets ID counter", () => { it("clearEncounter resets to empty and resets ID counter", () => {
const { result } = renderHook(() => useEncounter()); const { result } = renderHook(() => useEncounter(), { wrapper });
act(() => result.current.addCombatant("Goblin")); act(() => result.current.addCombatant("Goblin"));
act(() => result.current.clearEncounter()); act(() => result.current.clearEncounter());
@@ -100,7 +128,7 @@ describe("useEncounter", () => {
}); });
it("addCombatant with opts applies initiative, ac, maxHp", () => { it("addCombatant with opts applies initiative, ac, maxHp", () => {
const { result } = renderHook(() => useEncounter()); const { result } = renderHook(() => useEncounter(), { wrapper });
act(() => act(() =>
result.current.addCombatant("Goblin", { result.current.addCombatant("Goblin", {
@@ -118,16 +146,18 @@ describe("useEncounter", () => {
}); });
it("derived flags: hasCreatureCombatants and canRollAllInitiative", () => { it("derived flags: hasCreatureCombatants and canRollAllInitiative", () => {
const { result } = renderHook(() => useEncounter()); const { result } = renderHook(() => useEncounter(), { wrapper });
// No creatures yet // No creatures yet
expect(result.current.hasCreatureCombatants).toBe(false); expect(result.current.hasCreatureCombatants).toBe(false);
expect(result.current.canRollAllInitiative).toBe(false); expect(result.current.canRollAllInitiative).toBe(false);
// Add from bestiary to get a creature combatant // Add from bestiary to get a creature combatant
const entry: BestiaryIndexEntry = { const entry: SearchResult = {
system: "dnd",
name: "Goblin", name: "Goblin",
source: "MM", source: "MM",
sourceDisplayName: "Monster Manual",
ac: 15, ac: 15,
hp: 7, hp: 7,
dex: 14, dex: 14,
@@ -146,11 +176,13 @@ describe("useEncounter", () => {
}); });
it("addFromBestiary adds combatant with HP, AC, creatureId", () => { it("addFromBestiary adds combatant with HP, AC, creatureId", () => {
const { result } = renderHook(() => useEncounter()); const { result } = renderHook(() => useEncounter(), { wrapper });
const entry: BestiaryIndexEntry = { const entry: SearchResult = {
system: "dnd",
name: "Goblin", name: "Goblin",
source: "MM", source: "MM",
sourceDisplayName: "Monster Manual",
ac: 15, ac: 15,
hp: 7, hp: 7,
dex: 14, dex: 14,
@@ -173,11 +205,13 @@ describe("useEncounter", () => {
}); });
it("addFromBestiary auto-numbers duplicate names", () => { it("addFromBestiary auto-numbers duplicate names", () => {
const { result } = renderHook(() => useEncounter()); const { result } = renderHook(() => useEncounter(), { wrapper });
const entry: BestiaryIndexEntry = { const entry: SearchResult = {
system: "dnd",
name: "Goblin", name: "Goblin",
source: "MM", source: "MM",
sourceDisplayName: "Monster Manual",
ac: 15, ac: 15,
hp: 7, hp: 7,
dex: 14, dex: 14,
@@ -200,7 +234,7 @@ describe("useEncounter", () => {
}); });
it("addFromPlayerCharacter adds combatant with HP, AC, color, icon", () => { it("addFromPlayerCharacter adds combatant with HP, AC, color, icon", () => {
const { result } = renderHook(() => useEncounter()); const { result } = renderHook(() => useEncounter(), { wrapper });
const pc: PlayerCharacter = { const pc: PlayerCharacter = {
id: playerCharacterId("pc-1"), id: playerCharacterId("pc-1"),

View File

@@ -1,25 +1,33 @@
// @vitest-environment jsdom // @vitest-environment jsdom
import { playerCharacterId } from "@initiative/domain"; import { playerCharacterId } from "@initiative/domain";
import { act, renderHook } from "@testing-library/react"; import { act, renderHook } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest"; import type { ReactNode } from "react";
import { beforeAll, describe, expect, it, vi } from "vitest";
import { createTestAdapters } from "../../__tests__/adapters/in-memory-adapters.js";
import { AllProviders } from "../../__tests__/test-providers.js";
import { usePlayerCharacters } from "../use-player-characters.js"; import { usePlayerCharacters } from "../use-player-characters.js";
vi.mock("../../persistence/player-character-storage.js", () => ({ beforeAll(() => {
loadPlayerCharacters: vi.fn().mockReturnValue([]), Object.defineProperty(globalThis, "matchMedia", {
savePlayerCharacters: vi.fn(), writable: true,
})); value: vi.fn().mockImplementation((query: string) => ({
matches: false,
const { loadPlayerCharacters: mockLoad, savePlayerCharacters: mockSave } = media: query,
await vi.importMock< onchange: null,
typeof import("../../persistence/player-character-storage.js") addListener: vi.fn(),
>("../../persistence/player-character-storage.js"); removeListener: vi.fn(),
addEventListener: vi.fn(),
describe("usePlayerCharacters", () => { removeEventListener: vi.fn(),
beforeEach(() => { dispatchEvent: vi.fn(),
vi.clearAllMocks(); })),
mockLoad.mockReturnValue([]); });
}); });
function wrapper({ children }: { children: ReactNode }) {
return <AllProviders>{children}</AllProviders>;
}
describe("usePlayerCharacters", () => {
it("initializes with characters from persistence", () => { it("initializes with characters from persistence", () => {
const stored = [ const stored = [
{ {
@@ -31,15 +39,19 @@ describe("usePlayerCharacters", () => {
icon: undefined, icon: undefined,
}, },
]; ];
mockLoad.mockReturnValue(stored); const adapters = createTestAdapters({ playerCharacters: stored });
const { result } = renderHook(() => usePlayerCharacters()); const { result } = renderHook(() => usePlayerCharacters(), {
wrapper: ({ children }: { children: ReactNode }) => (
<AllProviders adapters={adapters}>{children}</AllProviders>
),
});
expect(result.current.characters).toEqual(stored); expect(result.current.characters).toEqual(stored);
}); });
it("createCharacter adds a character and persists", () => { it("createCharacter adds a character and persists", () => {
const { result } = renderHook(() => usePlayerCharacters()); const { result } = renderHook(() => usePlayerCharacters(), { wrapper });
act(() => { act(() => {
result.current.createCharacter( result.current.createCharacter(
@@ -56,11 +68,10 @@ describe("usePlayerCharacters", () => {
expect(result.current.characters[0].name).toBe("Vex"); expect(result.current.characters[0].name).toBe("Vex");
expect(result.current.characters[0].ac).toBe(15); expect(result.current.characters[0].ac).toBe(15);
expect(result.current.characters[0].maxHp).toBe(28); expect(result.current.characters[0].maxHp).toBe(28);
expect(mockSave).toHaveBeenCalled();
}); });
it("createCharacter returns domain error for empty name", () => { it("createCharacter returns domain error for empty name", () => {
const { result } = renderHook(() => usePlayerCharacters()); const { result } = renderHook(() => usePlayerCharacters(), { wrapper });
let error: unknown; let error: unknown;
act(() => { act(() => {
@@ -79,7 +90,7 @@ describe("usePlayerCharacters", () => {
}); });
it("editCharacter updates character and persists", () => { it("editCharacter updates character and persists", () => {
const { result } = renderHook(() => usePlayerCharacters()); const { result } = renderHook(() => usePlayerCharacters(), { wrapper });
act(() => { act(() => {
result.current.createCharacter( result.current.createCharacter(
@@ -99,11 +110,10 @@ describe("usePlayerCharacters", () => {
}); });
expect(result.current.characters[0].name).toBe("Vex'ahlia"); expect(result.current.characters[0].name).toBe("Vex'ahlia");
expect(mockSave).toHaveBeenCalled();
}); });
it("deleteCharacter removes character and persists", () => { it("deleteCharacter removes character and persists", () => {
const { result } = renderHook(() => usePlayerCharacters()); const { result } = renderHook(() => usePlayerCharacters(), { wrapper });
act(() => { act(() => {
result.current.createCharacter( result.current.createCharacter(
@@ -123,6 +133,5 @@ describe("usePlayerCharacters", () => {
}); });
expect(result.current.characters).toHaveLength(0); expect(result.current.characters).toHaveLength(0);
expect(mockSave).toHaveBeenCalled();
}); });
}); });

View File

@@ -1,9 +1,10 @@
// @vitest-environment jsdom // @vitest-environment jsdom
import { act, renderHook } from "@testing-library/react"; import { act, renderHook } from "@testing-library/react";
import { afterEach, describe, expect, it } from "vitest"; import { afterEach, describe, expect, it, vi } from "vitest";
import { useRulesEdition } from "../use-rules-edition.js"; import { useRulesEdition } from "../use-rules-edition.js";
const STORAGE_KEY = "initiative:rules-edition"; const STORAGE_KEY = "initiative:game-system";
const OLD_STORAGE_KEY = "initiative:rules-edition";
describe("useRulesEdition", () => { describe("useRulesEdition", () => {
afterEach(() => { afterEach(() => {
@@ -11,6 +12,7 @@ describe("useRulesEdition", () => {
const { result } = renderHook(() => useRulesEdition()); const { result } = renderHook(() => useRulesEdition());
act(() => result.current.setEdition("5.5e")); act(() => result.current.setEdition("5.5e"));
localStorage.removeItem(STORAGE_KEY); localStorage.removeItem(STORAGE_KEY);
localStorage.removeItem(OLD_STORAGE_KEY);
}); });
it("defaults to 5.5e", () => { it("defaults to 5.5e", () => {
@@ -42,4 +44,31 @@ describe("useRulesEdition", () => {
expect(r2.current.edition).toBe("5e"); expect(r2.current.edition).toBe("5e");
}); });
it("accepts pf2e as a valid game system", () => {
const { result } = renderHook(() => useRulesEdition());
act(() => result.current.setEdition("pf2e"));
expect(result.current.edition).toBe("pf2e");
expect(localStorage.getItem(STORAGE_KEY)).toBe("pf2e");
});
it("migrates from old storage key on fresh module load", async () => {
// Set up old key before re-importing the module
localStorage.setItem(OLD_STORAGE_KEY, "5e");
localStorage.removeItem(STORAGE_KEY);
// Force a fresh module so loadEdition() re-runs at init time
vi.resetModules();
const { useRulesEdition: freshHook } = await import(
"../use-rules-edition.js"
);
const { result } = renderHook(() => freshHook());
expect(result.current.edition).toBe("5e");
expect(localStorage.getItem(STORAGE_KEY)).toBe("5e");
expect(localStorage.getItem(OLD_STORAGE_KEY)).toBeNull();
});
}); });

View File

@@ -1,26 +1,31 @@
import type { import type {
AnyCreature,
BestiaryIndexEntry, BestiaryIndexEntry,
Creature,
CreatureId, CreatureId,
Pf2eBestiaryIndexEntry,
} from "@initiative/domain"; } from "@initiative/domain";
import { useCallback, useEffect, useState } from "react"; import { useCallback, useEffect, useState } from "react";
import { import {
normalizeBestiary, normalizeBestiary,
setSourceDisplayNames, setSourceDisplayNames,
} from "../adapters/bestiary-adapter.js"; } from "../adapters/bestiary-adapter.js";
import * as bestiaryCache from "../adapters/bestiary-cache.js"; import { normalizeFoundryCreatures } from "../adapters/pf2e-bestiary-adapter.js";
import { import { useAdapters } from "../contexts/adapter-context.js";
getSourceDisplayName, import { useRulesEditionContext } from "../contexts/rules-edition-context.js";
loadBestiaryIndex,
} from "../adapters/bestiary-index-adapter.js";
export interface SearchResult extends BestiaryIndexEntry { export type SearchResult =
| (BestiaryIndexEntry & {
readonly system: "dnd";
readonly sourceDisplayName: string; readonly sourceDisplayName: string;
} })
| (Pf2eBestiaryIndexEntry & {
readonly system: "pf2e";
readonly sourceDisplayName: string;
});
interface BestiaryHook { interface BestiaryHook {
search: (query: string) => SearchResult[]; search: (query: string) => SearchResult[];
getCreature: (id: CreatureId) => Creature | undefined; getCreature: (id: CreatureId) => AnyCreature | undefined;
isLoaded: boolean; isLoaded: boolean;
isSourceCached: (sourceCode: string) => Promise<boolean>; isSourceCached: (sourceCode: string) => Promise<boolean>;
fetchAndCacheSource: (sourceCode: string, url: string) => Promise<void>; fetchAndCacheSource: (sourceCode: string, url: string) => Promise<void>;
@@ -32,53 +37,102 @@ interface BestiaryHook {
} }
export function useBestiary(): BestiaryHook { export function useBestiary(): BestiaryHook {
const { bestiaryCache, bestiaryIndex, pf2eBestiaryIndex } = useAdapters();
const { edition } = useRulesEditionContext();
const [isLoaded, setIsLoaded] = useState(false); const [isLoaded, setIsLoaded] = useState(false);
const [creatureMap, setCreatureMap] = useState( const [creatureMap, setCreatureMap] = useState(
() => new Map<CreatureId, Creature>(), () => new Map<CreatureId, AnyCreature>(),
); );
useEffect(() => { useEffect(() => {
const index = loadBestiaryIndex(); const index = bestiaryIndex.loadIndex();
setSourceDisplayNames(index.sources as Record<string, string>); setSourceDisplayNames(index.sources as Record<string, string>);
if (index.creatures.length > 0) {
const pf2eIndex = pf2eBestiaryIndex.loadIndex();
if (index.creatures.length > 0 || pf2eIndex.creatures.length > 0) {
setIsLoaded(true); setIsLoaded(true);
} }
void bestiaryCache.loadAllCachedCreatures().then((map) => { void bestiaryCache.loadAllCachedCreatures().then((map) => {
setCreatureMap(map); setCreatureMap(map);
}); });
}, []); }, [bestiaryCache, bestiaryIndex, pf2eBestiaryIndex]);
const search = useCallback((query: string): SearchResult[] => { const search = useCallback(
(query: string): SearchResult[] => {
if (query.length < 2) return []; if (query.length < 2) return [];
const lower = query.toLowerCase(); const lower = query.toLowerCase();
const index = loadBestiaryIndex();
if (edition === "pf2e") {
const index = pf2eBestiaryIndex.loadIndex();
return index.creatures return index.creatures
.filter((c) => c.name.toLowerCase().includes(lower)) .filter((c) => c.name.toLowerCase().includes(lower))
.sort((a, b) => a.name.localeCompare(b.name)) .sort((a, b) => a.name.localeCompare(b.name))
.slice(0, 10) .slice(0, 10)
.map((c) => ({ .map((c) => ({
...c, ...c,
sourceDisplayName: getSourceDisplayName(c.source), system: "pf2e" as const,
sourceDisplayName: pf2eBestiaryIndex.getSourceDisplayName(c.source),
})); }));
}, []); }
const index = bestiaryIndex.loadIndex();
return index.creatures
.filter((c) => c.name.toLowerCase().includes(lower))
.sort((a, b) => a.name.localeCompare(b.name))
.slice(0, 10)
.map((c) => ({
...c,
system: "dnd" as const,
sourceDisplayName: bestiaryIndex.getSourceDisplayName(c.source),
}));
},
[bestiaryIndex, pf2eBestiaryIndex, edition],
);
const getCreature = useCallback( const getCreature = useCallback(
(id: CreatureId): Creature | undefined => { (id: CreatureId): AnyCreature | undefined => {
return creatureMap.get(id); return creatureMap.get(id);
}, },
[creatureMap], [creatureMap],
); );
const system = edition === "pf2e" ? "pf2e" : "dnd";
const isSourceCachedFn = useCallback( const isSourceCachedFn = useCallback(
(sourceCode: string): Promise<boolean> => { (sourceCode: string): Promise<boolean> => {
return bestiaryCache.isSourceCached(sourceCode); return bestiaryCache.isSourceCached(system, sourceCode);
}, },
[], [bestiaryCache, system],
); );
const fetchAndCacheSource = useCallback( const fetchAndCacheSource = useCallback(
async (sourceCode: string, url: string): Promise<void> => { async (sourceCode: string, url: string): Promise<void> => {
let creatures: AnyCreature[];
if (edition === "pf2e") {
// PF2e: url is a base URL; fetch each creature file in parallel
const paths = pf2eBestiaryIndex.getCreaturePathsForSource(sourceCode);
const baseUrl = url.endsWith("/") ? url : `${url}/`;
const responses = await Promise.all(
paths.map(async (path) => {
const response = await fetch(`${baseUrl}${path}`);
if (!response.ok) {
throw new Error(
`Failed to fetch ${path}: ${response.status} ${response.statusText}`,
);
}
return response.json();
}),
);
const displayName = pf2eBestiaryIndex.getSourceDisplayName(sourceCode);
creatures = normalizeFoundryCreatures(
responses,
sourceCode,
displayName,
);
} else {
const response = await fetch(url); const response = await fetch(url);
if (!response.ok) { if (!response.ok) {
throw new Error( throw new Error(
@@ -86,9 +140,19 @@ export function useBestiary(): BestiaryHook {
); );
} }
const json = await response.json(); const json = await response.json();
const creatures = normalizeBestiary(json); creatures = normalizeBestiary(json);
const displayName = getSourceDisplayName(sourceCode); }
await bestiaryCache.cacheSource(sourceCode, displayName, creatures);
const displayName =
edition === "pf2e"
? pf2eBestiaryIndex.getSourceDisplayName(sourceCode)
: bestiaryIndex.getSourceDisplayName(sourceCode);
await bestiaryCache.cacheSource(
system,
sourceCode,
displayName,
creatures,
);
setCreatureMap((prev) => { setCreatureMap((prev) => {
const next = new Map(prev); const next = new Map(prev);
for (const c of creatures) { for (const c of creatures) {
@@ -97,15 +161,31 @@ export function useBestiary(): BestiaryHook {
return next; return next;
}); });
}, },
[], [bestiaryCache, bestiaryIndex, pf2eBestiaryIndex, edition, system],
); );
const uploadAndCacheSource = useCallback( const uploadAndCacheSource = useCallback(
async (sourceCode: string, jsonData: unknown): Promise<void> => { async (sourceCode: string, jsonData: unknown): Promise<void> => {
// biome-ignore lint/suspicious/noExplicitAny: raw JSON shape varies const creatures =
const creatures = normalizeBestiary(jsonData as any); edition === "pf2e"
const displayName = getSourceDisplayName(sourceCode); ? normalizeFoundryCreatures(
await bestiaryCache.cacheSource(sourceCode, displayName, creatures); Array.isArray(jsonData) ? jsonData : [jsonData],
sourceCode,
pf2eBestiaryIndex.getSourceDisplayName(sourceCode),
)
: normalizeBestiary(
jsonData as Parameters<typeof normalizeBestiary>[0],
);
const displayName =
edition === "pf2e"
? pf2eBestiaryIndex.getSourceDisplayName(sourceCode)
: bestiaryIndex.getSourceDisplayName(sourceCode);
await bestiaryCache.cacheSource(
system,
sourceCode,
displayName,
creatures,
);
setCreatureMap((prev) => { setCreatureMap((prev) => {
const next = new Map(prev); const next = new Map(prev);
for (const c of creatures) { for (const c of creatures) {
@@ -114,13 +194,13 @@ export function useBestiary(): BestiaryHook {
return next; return next;
}); });
}, },
[], [bestiaryCache, bestiaryIndex, pf2eBestiaryIndex, edition, system],
); );
const refreshCache = useCallback(async (): Promise<void> => { const refreshCache = useCallback(async (): Promise<void> => {
const map = await bestiaryCache.loadAllCachedCreatures(); const map = await bestiaryCache.loadAllCachedCreatures();
setCreatureMap(map); setCreatureMap(map);
}, []); }, [bestiaryCache]);
return { return {
search, search,

View File

@@ -1,8 +1,6 @@
import { useCallback, useRef, useState } from "react"; import { useCallback, useRef, useState } from "react";
import { import { useAdapters } from "../contexts/adapter-context.js";
getAllSourceCodes, import { useRulesEditionContext } from "../contexts/rules-edition-context.js";
getDefaultFetchUrl,
} from "../adapters/bestiary-index-adapter.js";
const BATCH_SIZE = 6; const BATCH_SIZE = 6;
@@ -32,6 +30,9 @@ interface BulkImportHook {
} }
export function useBulkImport(): BulkImportHook { export function useBulkImport(): BulkImportHook {
const { bestiaryIndex, pf2eBestiaryIndex } = useAdapters();
const { edition } = useRulesEditionContext();
const indexPort = edition === "pf2e" ? pf2eBestiaryIndex : bestiaryIndex;
const [state, setState] = useState<BulkImportState>(IDLE_STATE); const [state, setState] = useState<BulkImportState>(IDLE_STATE);
const countersRef = useRef({ completed: 0, failed: 0 }); const countersRef = useRef({ completed: 0, failed: 0 });
@@ -42,7 +43,7 @@ export function useBulkImport(): BulkImportHook {
isSourceCached: (sourceCode: string) => Promise<boolean>, isSourceCached: (sourceCode: string) => Promise<boolean>,
refreshCache: () => Promise<void>, refreshCache: () => Promise<void>,
) => { ) => {
const allCodes = getAllSourceCodes(); const allCodes = indexPort.getAllSourceCodes();
const total = allCodes.length; const total = allCodes.length;
countersRef.current = { completed: 0, failed: 0 }; countersRef.current = { completed: 0, failed: 0 };
@@ -83,7 +84,7 @@ export function useBulkImport(): BulkImportHook {
chain.then(() => chain.then(() =>
Promise.allSettled( Promise.allSettled(
batch.map(async ({ code }) => { batch.map(async ({ code }) => {
const url = getDefaultFetchUrl(code, baseUrl); const url = indexPort.getDefaultFetchUrl(code, baseUrl);
try { try {
await fetchAndCacheSource(code, url); await fetchAndCacheSource(code, url);
countersRef.current.completed++; countersRef.current.completed++;
@@ -117,7 +118,7 @@ export function useBulkImport(): BulkImportHook {
}); });
})(); })();
}, },
[], [indexPort],
); );
const reset = useCallback(() => { const reset = useCallback(() => {

View File

@@ -0,0 +1,171 @@
import type {
Combatant,
CreatureId,
DifficultyThreshold,
DifficultyTier,
PlayerCharacter,
} from "@initiative/domain";
import { calculateEncounterDifficulty, crToXp } from "@initiative/domain";
import { useMemo } from "react";
import { useBestiaryContext } from "../contexts/bestiary-context.js";
import { useEncounterContext } from "../contexts/encounter-context.js";
import { usePlayerCharactersContext } from "../contexts/player-characters-context.js";
import { useRulesEditionContext } from "../contexts/rules-edition-context.js";
import { resolveSide } from "./use-difficulty.js";
export interface BreakdownCombatant {
readonly combatant: Combatant;
readonly cr: string | null;
readonly xp: number | null;
readonly source: string | null;
readonly editable: boolean;
readonly side: "party" | "enemy";
readonly level: number | undefined;
}
interface DifficultyBreakdown {
readonly tier: DifficultyTier;
readonly totalMonsterXp: number;
readonly thresholds: readonly DifficultyThreshold[];
readonly encounterMultiplier: number | undefined;
readonly adjustedXp: number | undefined;
readonly partySizeAdjusted: boolean | undefined;
readonly pcCount: number;
readonly partyCombatants: readonly BreakdownCombatant[];
readonly enemyCombatants: readonly BreakdownCombatant[];
}
export function useDifficultyBreakdown(): DifficultyBreakdown | null {
const { encounter } = useEncounterContext();
const { characters } = usePlayerCharactersContext();
const { getCreature } = useBestiaryContext();
const { edition } = useRulesEditionContext();
return useMemo(() => {
const { partyCombatants, enemyCombatants, descriptors, pcCount } =
classifyCombatants(encounter.combatants, characters, getCreature);
const hasPartyLevel = descriptors.some(
(d) => d.side === "party" && d.level !== undefined,
);
const hasCr = descriptors.some((d) => d.cr !== undefined);
if (!hasPartyLevel || !hasCr) return null;
const result = calculateEncounterDifficulty(descriptors, edition);
return {
...result,
pcCount,
partyCombatants,
enemyCombatants,
};
}, [encounter.combatants, characters, getCreature, edition]);
}
type CreatureInfo = {
cr?: string;
source: string;
sourceDisplayName: string;
};
function buildBreakdownEntry(
c: Combatant,
side: "party" | "enemy",
level: number | undefined,
creature: CreatureInfo | undefined,
): BreakdownCombatant {
if (c.playerCharacterId) {
return {
combatant: c,
cr: null,
xp: null,
source: null,
editable: false,
side,
level,
};
}
if (creature) {
const cr = creature.cr ?? null;
return {
combatant: c,
cr,
xp: cr ? crToXp(cr) : null,
source: creature.sourceDisplayName ?? creature.source,
editable: false,
side,
level: undefined,
};
}
if (c.cr) {
return {
combatant: c,
cr: c.cr,
xp: crToXp(c.cr),
source: null,
editable: true,
side,
level: undefined,
};
}
return {
combatant: c,
cr: null,
xp: null,
source: null,
editable: !c.creatureId,
side,
level: undefined,
};
}
function resolveLevel(
c: Combatant,
characters: readonly PlayerCharacter[],
): number | undefined {
if (!c.playerCharacterId) return undefined;
return characters.find((p) => p.id === c.playerCharacterId)?.level;
}
function resolveCr(
c: Combatant,
getCreature: (id: CreatureId) => CreatureInfo | undefined,
): { cr: string | null; creature: CreatureInfo | undefined } {
const creature = c.creatureId ? getCreature(c.creatureId) : undefined;
const cr = creature?.cr ?? c.cr ?? null;
return { cr, creature };
}
function classifyCombatants(
combatants: readonly Combatant[],
characters: readonly PlayerCharacter[],
getCreature: (id: CreatureId) => CreatureInfo | undefined,
) {
const partyCombatants: BreakdownCombatant[] = [];
const enemyCombatants: BreakdownCombatant[] = [];
const descriptors: {
level?: number;
cr?: string;
side: "party" | "enemy";
}[] = [];
let pcCount = 0;
for (const c of combatants) {
const side = resolveSide(c);
const level = resolveLevel(c, characters);
if (level !== undefined) pcCount++;
const { cr, creature } = resolveCr(c, getCreature);
if (level !== undefined || cr != null) {
descriptors.push({ level, cr: cr ?? undefined, side });
}
const entry = buildBreakdownEntry(c, side, level, creature);
const target = side === "party" ? partyCombatants : enemyCombatants;
target.push(entry);
}
return { partyCombatants, enemyCombatants, descriptors, pcCount };
}

View File

@@ -1,5 +1,7 @@
import type { import type {
AnyCreature,
Combatant, Combatant,
CombatantDescriptor,
CreatureId, CreatureId,
DifficultyResult, DifficultyResult,
PlayerCharacter, PlayerCharacter,
@@ -9,46 +11,58 @@ import { useMemo } from "react";
import { useBestiaryContext } from "../contexts/bestiary-context.js"; import { useBestiaryContext } from "../contexts/bestiary-context.js";
import { useEncounterContext } from "../contexts/encounter-context.js"; import { useEncounterContext } from "../contexts/encounter-context.js";
import { usePlayerCharactersContext } from "../contexts/player-characters-context.js"; import { usePlayerCharactersContext } from "../contexts/player-characters-context.js";
import { useRulesEditionContext } from "../contexts/rules-edition-context.js";
function derivePartyLevels( export function resolveSide(c: Combatant): "party" | "enemy" {
if (c.side) return c.side;
return c.playerCharacterId ? "party" : "enemy";
}
function buildDescriptors(
combatants: readonly Combatant[], combatants: readonly Combatant[],
characters: readonly PlayerCharacter[], characters: readonly PlayerCharacter[],
): number[] { getCreature: (id: CreatureId) => AnyCreature | undefined,
const levels: number[] = []; ): CombatantDescriptor[] {
const descriptors: CombatantDescriptor[] = [];
for (const c of combatants) { for (const c of combatants) {
if (!c.playerCharacterId) continue; const side = resolveSide(c);
const pc = characters.find((p) => p.id === c.playerCharacterId); const level = c.playerCharacterId
if (pc?.level !== undefined) levels.push(pc.level); ? characters.find((p) => p.id === c.playerCharacterId)?.level
} : undefined;
return levels; const creature = c.creatureId ? getCreature(c.creatureId) : undefined;
} const creatureCr =
creature && !("system" in creature) ? creature.cr : undefined;
const cr = creatureCr ?? c.cr ?? undefined;
function deriveMonsterCrs( if (level !== undefined || cr !== undefined) {
combatants: readonly Combatant[], descriptors.push({ level, cr, side });
getCreature: (id: CreatureId) => { cr: string } | undefined,
): string[] {
const crs: string[] = [];
for (const c of combatants) {
if (!c.creatureId) continue;
const creature = getCreature(c.creatureId);
if (creature) crs.push(creature.cr);
} }
return crs; }
return descriptors;
} }
export function useDifficulty(): DifficultyResult | null { export function useDifficulty(): DifficultyResult | null {
const { encounter } = useEncounterContext(); const { encounter } = useEncounterContext();
const { characters } = usePlayerCharactersContext(); const { characters } = usePlayerCharactersContext();
const { getCreature } = useBestiaryContext(); const { getCreature } = useBestiaryContext();
const { edition } = useRulesEditionContext();
return useMemo(() => { return useMemo(() => {
const partyLevels = derivePartyLevels(encounter.combatants, characters); if (edition === "pf2e") return null;
const monsterCrs = deriveMonsterCrs(encounter.combatants, getCreature);
if (partyLevels.length === 0 || monsterCrs.length === 0) { const descriptors = buildDescriptors(
return null; encounter.combatants,
} characters,
getCreature,
);
return calculateEncounterDifficulty(partyLevels, monsterCrs); const hasPartyLevel = descriptors.some(
}, [encounter.combatants, characters, getCreature]); (d) => d.side === "party" && d.level !== undefined,
);
const hasCr = descriptors.some((d) => d.cr !== undefined);
if (!hasPartyLevel || !hasCr) return null;
return calculateEncounterDifficulty(descriptors, edition);
}, [encounter.combatants, characters, getCreature, edition]);
} }

View File

@@ -0,0 +1,139 @@
import type { ExportBundle } from "@initiative/domain";
import { useCallback, useRef, useState } from "react";
import { useEncounterContext } from "../contexts/encounter-context.js";
import { usePlayerCharactersContext } from "../contexts/player-characters-context.js";
import {
assembleExportBundle,
bundleToJson,
readImportFile,
triggerDownload,
validateImportBundle,
} from "../persistence/export-import.js";
export function useEncounterExportImport() {
const {
encounter,
undoRedoState,
isEmpty: encounterIsEmpty,
setEncounter,
setUndoRedoState,
} = useEncounterContext();
const { characters: playerCharacters, replacePlayerCharacters } =
usePlayerCharactersContext();
const [importError, setImportError] = useState<string | null>(null);
const [showExportMethod, setShowExportMethod] = useState(false);
const [showImportMethod, setShowImportMethod] = useState(false);
const [showImportConfirm, setShowImportConfirm] = useState(false);
const pendingBundleRef = useRef<ExportBundle | null>(null);
const importFileRef = useRef<HTMLInputElement>(null);
const handleExportDownload = useCallback(
(includeHistory: boolean, filename: string) => {
const bundle = assembleExportBundle(
encounter,
undoRedoState,
playerCharacters,
includeHistory,
);
triggerDownload(bundle, filename);
},
[encounter, undoRedoState, playerCharacters],
);
const handleExportClipboard = useCallback(
(includeHistory: boolean) => {
const bundle = assembleExportBundle(
encounter,
undoRedoState,
playerCharacters,
includeHistory,
);
void navigator.clipboard.writeText(bundleToJson(bundle));
},
[encounter, undoRedoState, playerCharacters],
);
const applyImport = useCallback(
(bundle: ExportBundle) => {
setEncounter(bundle.encounter);
setUndoRedoState({
undoStack: bundle.undoStack,
redoStack: bundle.redoStack,
});
replacePlayerCharacters([...bundle.playerCharacters]);
},
[setEncounter, setUndoRedoState, replacePlayerCharacters],
);
const handleValidatedBundle = useCallback(
(result: ExportBundle | string) => {
if (typeof result === "string") {
setImportError(result);
return;
}
if (encounterIsEmpty) {
applyImport(result);
} else {
pendingBundleRef.current = result;
setShowImportConfirm(true);
}
},
[encounterIsEmpty, applyImport],
);
const handleImportFile = useCallback(
async (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
if (!file) return;
if (importFileRef.current) importFileRef.current.value = "";
setImportError(null);
handleValidatedBundle(await readImportFile(file));
},
[handleValidatedBundle],
);
const handleImportClipboard = useCallback(
(text: string) => {
setImportError(null);
try {
const parsed: unknown = JSON.parse(text);
handleValidatedBundle(validateImportBundle(parsed));
} catch {
setImportError("Invalid file format");
}
},
[handleValidatedBundle],
);
const handleImportConfirm = useCallback(() => {
if (pendingBundleRef.current) {
applyImport(pendingBundleRef.current);
pendingBundleRef.current = null;
}
setShowImportConfirm(false);
}, [applyImport]);
const handleImportCancel = useCallback(() => {
pendingBundleRef.current = null;
setShowImportConfirm(false);
}, []);
return {
importError,
showExportMethod,
showImportMethod,
showImportConfirm,
importFileRef,
setImportError,
setShowExportMethod,
setShowImportMethod,
handleExportDownload,
handleExportClipboard,
handleImportFile,
handleImportClipboard,
handleImportConfirm,
handleImportCancel,
} as const;
}

View File

@@ -4,20 +4,23 @@ import {
adjustHpUseCase, adjustHpUseCase,
advanceTurnUseCase, advanceTurnUseCase,
clearEncounterUseCase, clearEncounterUseCase,
decrementConditionUseCase,
editCombatantUseCase, editCombatantUseCase,
redoUseCase, redoUseCase,
removeCombatantUseCase, removeCombatantUseCase,
retreatTurnUseCase, retreatTurnUseCase,
setAcUseCase, setAcUseCase,
setConditionValueUseCase,
setCrUseCase,
setHpUseCase, setHpUseCase,
setInitiativeUseCase, setInitiativeUseCase,
setSideUseCase,
setTempHpUseCase, setTempHpUseCase,
toggleConcentrationUseCase, toggleConcentrationUseCase,
toggleConditionUseCase, toggleConditionUseCase,
undoUseCase, undoUseCase,
} from "@initiative/application"; } from "@initiative/application";
import type { import type {
BestiaryIndexEntry,
CombatantId, CombatantId,
CombatantInit, CombatantInit,
ConditionId, ConditionId,
@@ -37,14 +40,8 @@ import {
resolveCreatureName, resolveCreatureName,
} from "@initiative/domain"; } from "@initiative/domain";
import { useCallback, useEffect, useReducer, useRef } from "react"; import { useCallback, useEffect, useReducer, useRef } from "react";
import { import { useAdapters } from "../contexts/adapter-context.js";
loadEncounter, import type { SearchResult } from "./use-bestiary.js";
saveEncounter,
} from "../persistence/encounter-storage.js";
import {
loadUndoRedoStacks,
saveUndoRedoStacks,
} from "../persistence/undo-redo-storage.js";
// -- Types -- // -- Types --
@@ -59,19 +56,32 @@ type EncounterAction =
| { type: "adjust-hp"; id: CombatantId; delta: number } | { type: "adjust-hp"; id: CombatantId; delta: number }
| { type: "set-temp-hp"; id: CombatantId; tempHp: number | undefined } | { type: "set-temp-hp"; id: CombatantId; tempHp: number | undefined }
| { type: "set-ac"; id: CombatantId; value: number | undefined } | { type: "set-ac"; id: CombatantId; value: number | undefined }
| { type: "set-cr"; id: CombatantId; value: string | undefined }
| { type: "set-side"; id: CombatantId; value: "party" | "enemy" }
| { | {
type: "toggle-condition"; type: "toggle-condition";
id: CombatantId; id: CombatantId;
conditionId: ConditionId; conditionId: ConditionId;
} }
| {
type: "set-condition-value";
id: CombatantId;
conditionId: ConditionId;
value: number;
}
| {
type: "decrement-condition";
id: CombatantId;
conditionId: ConditionId;
}
| { type: "toggle-concentration"; id: CombatantId } | { type: "toggle-concentration"; id: CombatantId }
| { type: "clear-encounter" } | { type: "clear-encounter" }
| { type: "undo" } | { type: "undo" }
| { type: "redo" } | { type: "redo" }
| { type: "add-from-bestiary"; entry: BestiaryIndexEntry } | { type: "add-from-bestiary"; entry: SearchResult }
| { | {
type: "add-multiple-from-bestiary"; type: "add-multiple-from-bestiary";
entry: BestiaryIndexEntry; entry: SearchResult;
count: number; count: number;
} }
| { type: "add-from-player-character"; pc: PlayerCharacter } | { type: "add-from-player-character"; pc: PlayerCharacter }
@@ -111,11 +121,14 @@ function deriveNextId(encounter: Encounter): number {
return max; return max;
} }
function initializeState(): EncounterState { function initializeState(
const encounter = loadEncounter() ?? EMPTY_ENCOUNTER; loadEncounterFn: () => Encounter | null,
loadUndoRedoFn: () => UndoRedoState,
): EncounterState {
const encounter = loadEncounterFn() ?? EMPTY_ENCOUNTER;
return { return {
encounter, encounter,
undoRedoState: loadUndoRedoStacks(), undoRedoState: loadUndoRedoFn(),
events: [], events: [],
nextId: deriveNextId(encounter), nextId: deriveNextId(encounter),
lastCreatureId: null, lastCreatureId: null,
@@ -156,7 +169,7 @@ function resolveAndRename(store: EncounterStore, name: string): string {
function addOneFromBestiary( function addOneFromBestiary(
store: EncounterStore, store: EncounterStore,
entry: BestiaryIndexEntry, entry: SearchResult,
nextId: number, nextId: number,
): { ): {
cId: CreatureId; cId: CreatureId;
@@ -215,7 +228,7 @@ function handleUndoRedo(
function handleAddFromBestiary( function handleAddFromBestiary(
state: EncounterState, state: EncounterState,
entry: BestiaryIndexEntry, entry: SearchResult,
count: number, count: number,
): EncounterState { ): EncounterState {
const { store, getEncounter } = makeStoreFromState(state); const { store, getEncounter } = makeStoreFromState(state);
@@ -322,7 +335,11 @@ function dispatchEncounterAction(
| { type: "adjust-hp" } | { type: "adjust-hp" }
| { type: "set-temp-hp" } | { type: "set-temp-hp" }
| { type: "set-ac" } | { type: "set-ac" }
| { type: "set-cr" }
| { type: "set-side" }
| { type: "toggle-condition" } | { type: "toggle-condition" }
| { type: "set-condition-value" }
| { type: "decrement-condition" }
| { type: "toggle-concentration" } | { type: "toggle-concentration" }
>, >,
): EncounterState { ): EncounterState {
@@ -362,9 +379,26 @@ function dispatchEncounterAction(
case "set-ac": case "set-ac":
result = setAcUseCase(store, action.id, action.value); result = setAcUseCase(store, action.id, action.value);
break; break;
case "set-cr":
result = setCrUseCase(store, action.id, action.value);
break;
case "set-side":
result = setSideUseCase(store, action.id, action.value);
break;
case "toggle-condition": case "toggle-condition":
result = toggleConditionUseCase(store, action.id, action.conditionId); result = toggleConditionUseCase(store, action.id, action.conditionId);
break; break;
case "set-condition-value":
result = setConditionValueUseCase(
store,
action.id,
action.conditionId,
action.value,
);
break;
case "decrement-condition":
result = decrementConditionUseCase(store, action.id, action.conditionId);
break;
case "toggle-concentration": case "toggle-concentration":
result = toggleConcentrationUseCase(store, action.id); result = toggleConcentrationUseCase(store, action.id);
break; break;
@@ -385,7 +419,10 @@ function dispatchEncounterAction(
// -- Hook -- // -- Hook --
export function useEncounter() { export function useEncounter() {
const [state, dispatch] = useReducer(encounterReducer, null, initializeState); const { encounterPersistence, undoRedoPersistence } = useAdapters();
const [state, dispatch] = useReducer(encounterReducer, null, () =>
initializeState(encounterPersistence.load, undoRedoPersistence.load),
);
const { encounter, undoRedoState, events } = state; const { encounter, undoRedoState, events } = state;
const encounterRef = useRef(encounter); const encounterRef = useRef(encounter);
@@ -394,12 +431,12 @@ export function useEncounter() {
undoRedoRef.current = undoRedoState; undoRedoRef.current = undoRedoState;
useEffect(() => { useEffect(() => {
saveEncounter(encounter); encounterPersistence.save(encounter);
}, [encounter]); }, [encounter, encounterPersistence]);
useEffect(() => { useEffect(() => {
saveUndoRedoStacks(undoRedoState); undoRedoPersistence.save(undoRedoState);
}, [undoRedoState]); }, [undoRedoState, undoRedoPersistence]);
// Escape hatches for useInitiativeRolls (needs raw port access) // Escape hatches for useInitiativeRolls (needs raw port access)
const makeStore = useCallback((): EncounterStore => { const makeStore = useCallback((): EncounterStore => {
@@ -496,11 +533,31 @@ export function useEncounter() {
dispatch({ type: "set-ac", id, value }), dispatch({ type: "set-ac", id, value }),
[], [],
), ),
setCr: useCallback(
(id: CombatantId, value: string | undefined) =>
dispatch({ type: "set-cr", id, value }),
[],
),
setSide: useCallback(
(id: CombatantId, value: "party" | "enemy") =>
dispatch({ type: "set-side", id, value }),
[],
),
toggleCondition: useCallback( toggleCondition: useCallback(
(id: CombatantId, conditionId: ConditionId) => (id: CombatantId, conditionId: ConditionId) =>
dispatch({ type: "toggle-condition", id, conditionId }), dispatch({ type: "toggle-condition", id, conditionId }),
[], [],
), ),
setConditionValue: useCallback(
(id: CombatantId, conditionId: ConditionId, value: number) =>
dispatch({ type: "set-condition-value", id, conditionId, value }),
[],
),
decrementCondition: useCallback(
(id: CombatantId, conditionId: ConditionId) =>
dispatch({ type: "decrement-condition", id, conditionId }),
[],
),
toggleConcentration: useCallback( toggleConcentration: useCallback(
(id: CombatantId) => dispatch({ type: "toggle-concentration", id }), (id: CombatantId) => dispatch({ type: "toggle-concentration", id }),
[], [],
@@ -509,15 +566,12 @@ export function useEncounter() {
() => dispatch({ type: "clear-encounter" }), () => dispatch({ type: "clear-encounter" }),
[], [],
), ),
addFromBestiary: useCallback( addFromBestiary: useCallback((entry: SearchResult): CreatureId | null => {
(entry: BestiaryIndexEntry): CreatureId | null => {
dispatch({ type: "add-from-bestiary", entry }); dispatch({ type: "add-from-bestiary", entry });
return null; return null;
}, }, []),
[],
),
addMultipleFromBestiary: useCallback( addMultipleFromBestiary: useCallback(
(entry: BestiaryIndexEntry, count: number): CreatureId | null => { (entry: SearchResult, count: number): CreatureId | null => {
dispatch({ dispatch({
type: "add-multiple-from-bestiary", type: "add-multiple-from-bestiary",
entry, entry,

View File

@@ -7,14 +7,7 @@ import {
import type { PlayerCharacter, PlayerCharacterId } from "@initiative/domain"; import type { PlayerCharacter, PlayerCharacterId } from "@initiative/domain";
import { isDomainError, playerCharacterId } from "@initiative/domain"; import { isDomainError, playerCharacterId } from "@initiative/domain";
import { useCallback, useEffect, useRef, useState } from "react"; import { useCallback, useEffect, useRef, useState } from "react";
import { import { useAdapters } from "../contexts/adapter-context.js";
loadPlayerCharacters,
savePlayerCharacters,
} from "../persistence/player-character-storage.js";
function initializeCharacters(): PlayerCharacter[] {
return loadPlayerCharacters();
}
let nextPcId = 0; let nextPcId = 0;
@@ -32,14 +25,16 @@ interface EditFields {
} }
export function usePlayerCharacters() { export function usePlayerCharacters() {
const [characters, setCharacters] = const { playerCharacterPersistence } = useAdapters();
useState<PlayerCharacter[]>(initializeCharacters); const [characters, setCharacters] = useState<PlayerCharacter[]>(() =>
playerCharacterPersistence.load(),
);
const charactersRef = useRef(characters); const charactersRef = useRef(characters);
charactersRef.current = characters; charactersRef.current = characters;
useEffect(() => { useEffect(() => {
savePlayerCharacters(characters); playerCharacterPersistence.save(characters);
}, [characters]); }, [characters, playerCharacterPersistence]);
const makeStore = useCallback((): PlayerCharacterStore => { const makeStore = useCallback((): PlayerCharacterStore => {
return { return {

View File

@@ -1,7 +1,8 @@
import type { RulesEdition } from "@initiative/domain"; import type { RulesEdition } from "@initiative/domain";
import { useCallback, useSyncExternalStore } from "react"; import { useCallback, useSyncExternalStore } from "react";
const STORAGE_KEY = "initiative:rules-edition"; const STORAGE_KEY = "initiative:game-system";
const OLD_STORAGE_KEY = "initiative:rules-edition";
const listeners = new Set<() => void>(); const listeners = new Set<() => void>();
let currentEdition: RulesEdition = loadEdition(); let currentEdition: RulesEdition = loadEdition();
@@ -9,7 +10,14 @@ let currentEdition: RulesEdition = loadEdition();
function loadEdition(): RulesEdition { function loadEdition(): RulesEdition {
try { try {
const raw = localStorage.getItem(STORAGE_KEY); const raw = localStorage.getItem(STORAGE_KEY);
if (raw === "5e" || raw === "5.5e") return raw; if (raw === "5e" || raw === "5.5e" || raw === "pf2e") return raw;
// Migrate from old key
const old = localStorage.getItem(OLD_STORAGE_KEY);
if (old === "5e" || old === "5.5e") {
localStorage.setItem(STORAGE_KEY, old);
localStorage.removeItem(OLD_STORAGE_KEY);
return old;
}
} catch { } catch {
// storage unavailable // storage unavailable
} }

View File

@@ -1,6 +1,8 @@
import { StrictMode } from "react"; import { StrictMode } from "react";
import { createRoot } from "react-dom/client"; import { createRoot } from "react-dom/client";
import { App } from "./App.js"; import { App } from "./App.js";
import { productionAdapters } from "./adapters/production-adapters.js";
import { AdapterProvider } from "./contexts/adapter-context.js";
import { import {
BestiaryProvider, BestiaryProvider,
BulkImportProvider, BulkImportProvider,
@@ -17,6 +19,7 @@ const root = document.getElementById("root");
if (root) { if (root) {
createRoot(root).render( createRoot(root).render(
<StrictMode> <StrictMode>
<AdapterProvider adapters={productionAdapters}>
<ThemeProvider> <ThemeProvider>
<RulesEditionProvider> <RulesEditionProvider>
<EncounterProvider> <EncounterProvider>
@@ -34,6 +37,7 @@ if (root) {
</EncounterProvider> </EncounterProvider>
</RulesEditionProvider> </RulesEditionProvider>
</ThemeProvider> </ThemeProvider>
</AdapterProvider>
</StrictMode>, </StrictMode>,
); );
} }

View File

@@ -134,6 +134,67 @@ describe("loadEncounter", () => {
expect(loadEncounter()).toBeNull(); expect(loadEncounter()).toBeNull();
}); });
it("round-trip preserves combatant cr field", () => {
const result = createEncounter(
[
{
id: combatantId("c-1"),
name: "Custom Thug",
cr: "2",
},
],
0,
1,
);
if (isDomainError(result)) throw new Error("unreachable");
saveEncounter(result);
const loaded = loadEncounter();
expect(loaded).not.toBeNull();
expect(loaded?.combatants[0].cr).toBe("2");
});
it("round-trip preserves combatant side field", () => {
const result = createEncounter(
[
{
id: combatantId("c-1"),
name: "Allied Guard",
cr: "2",
side: "party",
},
{
id: combatantId("c-2"),
name: "Goblin",
side: "enemy",
},
],
0,
1,
);
if (isDomainError(result)) throw new Error("unreachable");
saveEncounter(result);
const loaded = loadEncounter();
expect(loaded).not.toBeNull();
expect(loaded?.combatants[0].side).toBe("party");
expect(loaded?.combatants[1].side).toBe("enemy");
});
it("round-trip preserves combatant without side field as undefined", () => {
const result = createEncounter(
[{ id: combatantId("c-1"), name: "Custom" }],
0,
1,
);
if (isDomainError(result)) throw new Error("unreachable");
saveEncounter(result);
const loaded = loadEncounter();
expect(loaded).not.toBeNull();
expect(loaded?.combatants[0].side).toBeUndefined();
});
it("saving after modifications persists the latest state", () => { it("saving after modifications persists the latest state", () => {
const encounter = makeEncounter(); const encounter = makeEncounter();
saveEncounter(encounter); saveEncounter(encounter);

View File

@@ -10,7 +10,8 @@
"!coverage", "!coverage",
"!.pnpm-store", "!.pnpm-store",
"!.rodney", "!.rodney",
"!.agent-tests" "!.agent-tests",
"!data"
] ]
}, },
"assist": { "assist": {

File diff suppressed because one or more lines are too long

View File

@@ -292,9 +292,9 @@ describe("toggleConditionUseCase", () => {
); );
expect(isDomainError(result)).toBe(false); expect(isDomainError(result)).toBe(false);
expect(requireSaved(store.saved).combatants[0].conditions).toContain( expect(requireSaved(store.saved).combatants[0].conditions).toContainEqual({
"blinded", id: "blinded",
); });
}); });
it("returns domain error for unknown combatant", () => { it("returns domain error for unknown combatant", () => {

View File

@@ -0,0 +1,21 @@
import {
type AnyCreature,
calculateInitiative,
calculatePf2eInitiative,
} from "@initiative/domain";
export function creatureInitiativeModifier(creature: AnyCreature): number {
if ("system" in creature && creature.system === "pf2e") {
return calculatePf2eInitiative(creature.perception).modifier;
}
const c = creature as {
abilities: { dex: number };
cr: string;
initiativeProficiency: number;
};
return calculateInitiative({
dexScore: c.abilities.dex,
cr: c.cr,
initiativeProficiency: c.initiativeProficiency,
}).modifier;
}

View File

@@ -0,0 +1,19 @@
import {
type CombatantId,
type ConditionId,
type DomainError,
type DomainEvent,
decrementCondition,
} from "@initiative/domain";
import type { EncounterStore } from "./ports.js";
import { runEncounterAction } from "./run-encounter-action.js";
export function decrementConditionUseCase(
store: EncounterStore,
combatantId: CombatantId,
conditionId: ConditionId,
): DomainEvent[] | DomainError {
return runEncounterAction(store, (encounter) =>
decrementCondition(encounter, combatantId, conditionId),
);
}

View File

@@ -3,6 +3,7 @@ export { adjustHpUseCase } from "./adjust-hp-use-case.js";
export { advanceTurnUseCase } from "./advance-turn-use-case.js"; export { advanceTurnUseCase } from "./advance-turn-use-case.js";
export { clearEncounterUseCase } from "./clear-encounter-use-case.js"; export { clearEncounterUseCase } from "./clear-encounter-use-case.js";
export { createPlayerCharacterUseCase } from "./create-player-character-use-case.js"; export { createPlayerCharacterUseCase } from "./create-player-character-use-case.js";
export { decrementConditionUseCase } from "./decrement-condition-use-case.js";
export { deletePlayerCharacterUseCase } from "./delete-player-character-use-case.js"; export { deletePlayerCharacterUseCase } from "./delete-player-character-use-case.js";
export { editCombatantUseCase } from "./edit-combatant-use-case.js"; export { editCombatantUseCase } from "./edit-combatant-use-case.js";
export { editPlayerCharacterUseCase } from "./edit-player-character-use-case.js"; export { editPlayerCharacterUseCase } from "./edit-player-character-use-case.js";
@@ -21,8 +22,11 @@ export {
} from "./roll-all-initiative-use-case.js"; } from "./roll-all-initiative-use-case.js";
export { rollInitiativeUseCase } from "./roll-initiative-use-case.js"; export { rollInitiativeUseCase } from "./roll-initiative-use-case.js";
export { setAcUseCase } from "./set-ac-use-case.js"; export { setAcUseCase } from "./set-ac-use-case.js";
export { setConditionValueUseCase } from "./set-condition-value-use-case.js";
export { setCrUseCase } from "./set-cr-use-case.js";
export { setHpUseCase } from "./set-hp-use-case.js"; export { setHpUseCase } from "./set-hp-use-case.js";
export { setInitiativeUseCase } from "./set-initiative-use-case.js"; export { setInitiativeUseCase } from "./set-initiative-use-case.js";
export { setSideUseCase } from "./set-side-use-case.js";
export { setTempHpUseCase } from "./set-temp-hp-use-case.js"; export { setTempHpUseCase } from "./set-temp-hp-use-case.js";
export { toggleConcentrationUseCase } from "./toggle-concentration-use-case.js"; export { toggleConcentrationUseCase } from "./toggle-concentration-use-case.js";
export { toggleConditionUseCase } from "./toggle-condition-use-case.js"; export { toggleConditionUseCase } from "./toggle-condition-use-case.js";

View File

@@ -1,5 +1,5 @@
import type { import type {
Creature, AnyCreature,
CreatureId, CreatureId,
Encounter, Encounter,
PlayerCharacter, PlayerCharacter,
@@ -12,7 +12,7 @@ export interface EncounterStore {
} }
export interface BestiarySourceCache { export interface BestiarySourceCache {
getCreature(creatureId: CreatureId): Creature | undefined; getCreature(creatureId: CreatureId): AnyCreature | undefined;
isSourceCached(sourceCode: string): boolean; isSourceCached(sourceCode: string): boolean;
} }

View File

@@ -1,7 +1,6 @@
import { import {
type Creature, type AnyCreature,
type CreatureId, type CreatureId,
calculateInitiative,
type DomainError, type DomainError,
type DomainEvent, type DomainEvent,
isDomainError, isDomainError,
@@ -10,6 +9,7 @@ import {
selectRoll, selectRoll,
setInitiative, setInitiative,
} from "@initiative/domain"; } from "@initiative/domain";
import { creatureInitiativeModifier } from "./creature-initiative-modifier.js";
import type { EncounterStore } from "./ports.js"; import type { EncounterStore } from "./ports.js";
export interface RollAllResult { export interface RollAllResult {
@@ -20,7 +20,7 @@ export interface RollAllResult {
export function rollAllInitiativeUseCase( export function rollAllInitiativeUseCase(
store: EncounterStore, store: EncounterStore,
rollDice: () => number, rollDice: () => number,
getCreature: (id: CreatureId) => Creature | undefined, getCreature: (id: CreatureId) => AnyCreature | undefined,
mode: RollMode = "normal", mode: RollMode = "normal",
): RollAllResult | DomainError { ): RollAllResult | DomainError {
let encounter = store.get(); let encounter = store.get();
@@ -37,11 +37,7 @@ export function rollAllInitiativeUseCase(
continue; continue;
} }
const { modifier } = calculateInitiative({ const modifier = creatureInitiativeModifier(creature);
dexScore: creature.abilities.dex,
cr: creature.cr,
initiativeProficiency: creature.initiativeProficiency,
});
const roll1 = rollDice(); const roll1 = rollDice();
const effectiveRoll = const effectiveRoll =
mode === "normal" ? roll1 : selectRoll(roll1, rollDice(), mode); mode === "normal" ? roll1 : selectRoll(roll1, rollDice(), mode);

View File

@@ -1,8 +1,7 @@
import { import {
type AnyCreature,
type CombatantId, type CombatantId,
type Creature,
type CreatureId, type CreatureId,
calculateInitiative,
type DomainError, type DomainError,
type DomainEvent, type DomainEvent,
isDomainError, isDomainError,
@@ -11,13 +10,14 @@ import {
selectRoll, selectRoll,
setInitiative, setInitiative,
} from "@initiative/domain"; } from "@initiative/domain";
import { creatureInitiativeModifier } from "./creature-initiative-modifier.js";
import type { EncounterStore } from "./ports.js"; import type { EncounterStore } from "./ports.js";
export function rollInitiativeUseCase( export function rollInitiativeUseCase(
store: EncounterStore, store: EncounterStore,
combatantId: CombatantId, combatantId: CombatantId,
diceRolls: readonly [number, ...number[]], diceRolls: readonly [number, ...number[]],
getCreature: (id: CreatureId) => Creature | undefined, getCreature: (id: CreatureId) => AnyCreature | undefined,
mode: RollMode = "normal", mode: RollMode = "normal",
): DomainEvent[] | DomainError { ): DomainEvent[] | DomainError {
const encounter = store.get(); const encounter = store.get();
@@ -48,11 +48,7 @@ export function rollInitiativeUseCase(
}; };
} }
const { modifier } = calculateInitiative({ const modifier = creatureInitiativeModifier(creature);
dexScore: creature.abilities.dex,
cr: creature.cr,
initiativeProficiency: creature.initiativeProficiency,
});
const effectiveRoll = const effectiveRoll =
mode === "normal" mode === "normal"
? diceRolls[0] ? diceRolls[0]

View File

@@ -0,0 +1,20 @@
import {
type CombatantId,
type ConditionId,
type DomainError,
type DomainEvent,
setConditionValue,
} from "@initiative/domain";
import type { EncounterStore } from "./ports.js";
import { runEncounterAction } from "./run-encounter-action.js";
export function setConditionValueUseCase(
store: EncounterStore,
combatantId: CombatantId,
conditionId: ConditionId,
value: number,
): DomainEvent[] | DomainError {
return runEncounterAction(store, (encounter) =>
setConditionValue(encounter, combatantId, conditionId, value),
);
}

View File

@@ -0,0 +1,18 @@
import {
type CombatantId,
type DomainError,
type DomainEvent,
setCr,
} from "@initiative/domain";
import type { EncounterStore } from "./ports.js";
import { runEncounterAction } from "./run-encounter-action.js";
export function setCrUseCase(
store: EncounterStore,
combatantId: CombatantId,
value: string | undefined,
): DomainEvent[] | DomainError {
return runEncounterAction(store, (encounter) =>
setCr(encounter, combatantId, value),
);
}

View File

@@ -0,0 +1,18 @@
import {
type CombatantId,
type DomainError,
type DomainEvent,
setSide,
} from "@initiative/domain";
import type { EncounterStore } from "./ports.js";
import { runEncounterAction } from "./run-encounter-action.js";
export function setSideUseCase(
store: EncounterStore,
combatantId: CombatantId,
value: "party" | "enemy",
): DomainEvent[] | DomainError {
return runEncounterAction(store, (encounter) =>
setSide(encounter, combatantId, value),
);
}

View File

@@ -54,7 +54,7 @@ describe("clearEncounter", () => {
maxHp: 50, maxHp: 50,
currentHp: 30, currentHp: 30,
ac: 18, ac: 18,
conditions: ["blinded", "poisoned"], conditions: [{ id: "blinded" }, { id: "poisoned" }],
isConcentrating: true, isConcentrating: true,
}, },
{ {
@@ -63,7 +63,7 @@ describe("clearEncounter", () => {
maxHp: 25, maxHp: 25,
currentHp: 0, currentHp: 0,
ac: 12, ac: 12,
conditions: ["unconscious"], conditions: [{ id: "unconscious" }],
}, },
], ],
activeIndex: 0, activeIndex: 0,

View File

@@ -26,25 +26,40 @@ describe("getConditionDescription", () => {
); );
}); });
it("universal conditions have both descriptions", () => { it("returns pf2e description when edition is pf2e", () => {
const universal = CONDITION_DEFINITIONS.filter( const blinded = findCondition("blinded");
(d) => d.edition === undefined, expect(getConditionDescription(blinded, "pf2e")).toBe(
blinded.descriptionPf2e,
); );
expect(universal.length).toBeGreaterThan(0); });
for (const def of universal) {
expect(def.description).toBeTruthy(); it("falls back to default description for pf2e when no pf2e text", () => {
expect(def.description5e).toBeTruthy(); const paralyzed = findCondition("paralyzed");
expect(getConditionDescription(paralyzed, "pf2e")).toBe(
paralyzed.descriptionPf2e,
);
});
it("shared D&D conditions have both description and description5e", () => {
const sharedDndConditions = CONDITION_DEFINITIONS.filter(
(d) =>
d.systems === undefined ||
(d.systems.includes("5e") && d.systems.includes("5.5e")),
);
for (const def of sharedDndConditions) {
expect(def.description, `${def.id} missing description`).toBeTruthy();
expect(def.description5e, `${def.id} missing description5e`).toBeTruthy();
} }
}); });
it("edition-specific conditions have their edition description", () => { it("system-specific conditions use the systems field", () => {
const sapped = findCondition("sapped"); const sapped = findCondition("sapped");
expect(sapped.description).toBeTruthy(); expect(sapped.description).toBeTruthy();
expect(sapped.edition).toBe("5.5e"); expect(sapped.systems).toContain("5.5e");
const slowed = findCondition("slowed"); const slowed = findCondition("slowed");
expect(slowed.description).toBeTruthy(); expect(slowed.description).toBeTruthy();
expect(slowed.edition).toBe("5.5e"); expect(slowed.systems).toContain("5.5e");
}); });
it("conditions with identical rules share the same text", () => { it("conditions with identical rules share the same text", () => {
@@ -79,4 +94,34 @@ describe("getConditionsForEdition", () => {
expect(ids5e).toContain("blinded"); expect(ids5e).toContain("blinded");
expect(ids55e).toContain("blinded"); expect(ids55e).toContain("blinded");
}); });
it("returns PF2e conditions for pf2e edition", () => {
const conditions = getConditionsForEdition("pf2e");
const ids = conditions.map((d) => d.id);
expect(ids).toContain("clumsy");
expect(ids).toContain("drained");
expect(ids).toContain("off-guard");
expect(ids).toContain("sickened");
expect(ids).not.toContain("charmed");
expect(ids).not.toContain("exhaustion");
expect(ids).not.toContain("grappled");
});
it("returns D&D conditions for 5.5e", () => {
const conditions = getConditionsForEdition("5.5e");
const ids = conditions.map((d) => d.id);
expect(ids).toContain("charmed");
expect(ids).toContain("exhaustion");
expect(ids).not.toContain("clumsy");
expect(ids).not.toContain("off-guard");
});
it("shared conditions appear in both D&D and PF2e", () => {
const dndIds = getConditionsForEdition("5.5e").map((d) => d.id);
const pf2eIds = getConditionsForEdition("pf2e").map((d) => d.id);
expect(dndIds).toContain("blinded");
expect(pf2eIds).toContain("blinded");
expect(dndIds).toContain("prone");
expect(pf2eIds).toContain("prone");
});
}); });

View File

@@ -36,98 +36,353 @@ describe("crToXp", () => {
}); });
}); });
describe("calculateEncounterDifficulty", () => { /** Helper to build party-side descriptors with level. */
it("returns trivial when monster XP is below Low threshold", () => { function party(level: number) {
return { level, side: "party" as const };
}
/** Helper to build enemy-side descriptors with CR. */
function enemy(cr: string) {
return { cr, side: "enemy" as const };
}
describe("calculateEncounterDifficulty — 5.5e edition", () => {
it("returns tier 0 when monster XP is below Low threshold", () => {
// 4x level 1: Low = 200, Moderate = 300, High = 400 // 4x level 1: Low = 200, Moderate = 300, High = 400
// 1x CR 0 = 0 XP → trivial // 1x CR 0 = 0 XP -> tier 0
const result = calculateEncounterDifficulty([1, 1, 1, 1], ["0"]); const result = calculateEncounterDifficulty(
expect(result.tier).toBe("trivial"); [party(1), party(1), party(1), party(1), enemy("0")],
"5.5e",
);
expect(result.tier).toBe(0);
expect(result.totalMonsterXp).toBe(0); expect(result.totalMonsterXp).toBe(0);
expect(result.partyBudget).toEqual({ expect(result.thresholds).toEqual([
low: 200, { label: "Low", value: 200 },
moderate: 300, { label: "Moderate", value: 300 },
high: 400, { label: "High", value: 400 },
}); ]);
expect(result.encounterMultiplier).toBeUndefined();
expect(result.adjustedXp).toBeUndefined();
expect(result.partySizeAdjusted).toBeUndefined();
}); });
it("returns low for 4x level 1 vs Bugbear (CR 1)", () => { it("returns tier 1 for 4x level 1 vs Bugbear (CR 1)", () => {
// DMG example: 4x level 1 PCs vs 1 Bugbear (CR 1, 200 XP) const result = calculateEncounterDifficulty(
// Low = 200, Moderate = 300 → 200 >= 200 but < 300 → Low [party(1), party(1), party(1), party(1), enemy("1")],
const result = calculateEncounterDifficulty([1, 1, 1, 1], ["1"]); "5.5e",
expect(result.tier).toBe("low"); );
expect(result.tier).toBe(1);
expect(result.totalMonsterXp).toBe(200); expect(result.totalMonsterXp).toBe(200);
}); });
it("returns moderate for 5x level 3 vs 1125 XP", () => { it("returns tier 2 for 5x level 3 vs 1150 XP", () => {
// 5x level 3: Low = 750, Moderate = 1125, High = 2000 // 5x level 3: Low = 750, Moderate = 1125, High = 2000
// 1125 XP >= 1125 Moderate but < 2000 High → Moderate // CR 3 (700) + CR 2 (450) = 1150 XP >= 1125 Moderate
// Using CR 3 (700) + CR 2 (450) = 1150 XP ≈ 1125 threshold const result = calculateEncounterDifficulty(
// Let's use exact: 5 * 225 = 1125 moderate budget [
// Need monsters that sum exactly to 1125: CR 3 (700) + CR 2 (450) = 1150 party(3),
const result = calculateEncounterDifficulty([3, 3, 3, 3, 3], ["3", "2"]); party(3),
expect(result.tier).toBe("moderate"); party(3),
party(3),
party(3),
enemy("3"),
enemy("2"),
],
"5.5e",
);
expect(result.tier).toBe(2);
expect(result.totalMonsterXp).toBe(1150); expect(result.totalMonsterXp).toBe(1150);
expect(result.partyBudget.moderate).toBe(1125); expect(result.thresholds[1].value).toBe(1125);
}); });
it("returns high when XP meets High threshold", () => { it("returns tier 3 when XP meets High threshold", () => {
// 4x level 1: High = 400 // 4x level 1: High = 400
// 2x CR 1 = 400 XP → High // 2x CR 1 = 400 XP -> tier 3
const result = calculateEncounterDifficulty([1, 1, 1, 1], ["1", "1"]); const result = calculateEncounterDifficulty(
expect(result.tier).toBe("high"); [party(1), party(1), party(1), party(1), enemy("1"), enemy("1")],
"5.5e",
);
expect(result.tier).toBe(3);
expect(result.totalMonsterXp).toBe(400); expect(result.totalMonsterXp).toBe(400);
}); });
it("caps at high when XP far exceeds threshold", () => { it("caps at tier 3 when XP far exceeds threshold", () => {
// 4x level 1: High = 400 const result = calculateEncounterDifficulty(
// CR 30 = 155000 XP → still High (no tier above) [party(1), party(1), party(1), party(1), enemy("30")],
const result = calculateEncounterDifficulty([1, 1, 1, 1], ["30"]); "5.5e",
expect(result.tier).toBe("high"); );
expect(result.tier).toBe(3);
expect(result.totalMonsterXp).toBe(155000); expect(result.totalMonsterXp).toBe(155000);
}); });
it("handles mixed party levels", () => { it("handles mixed party levels", () => {
// 3x level 3 + 1x level 2 // 3x level 3 + 1x level 2
// level 3: low=150, mod=225, high=400 (x3 = 450, 675, 1200)
// level 2: low=100, mod=150, high=200 (x1 = 100, 150, 200)
// Total: low=550, mod=825, high=1400 // Total: low=550, mod=825, high=1400
const result = calculateEncounterDifficulty([3, 3, 3, 2], ["3"]); const result = calculateEncounterDifficulty(
expect(result.partyBudget).toEqual({ [party(3), party(3), party(3), party(2), enemy("3")],
low: 550, "5.5e",
moderate: 825, );
high: 1400, expect(result.thresholds).toEqual([
}); { label: "Low", value: 550 },
{ label: "Moderate", value: 825 },
{ label: "High", value: 1400 },
]);
expect(result.totalMonsterXp).toBe(700); expect(result.totalMonsterXp).toBe(700);
expect(result.tier).toBe("low"); expect(result.tier).toBe(1);
}); });
it("returns trivial with empty monster array", () => { it("returns tier 0 with no enemies", () => {
const result = calculateEncounterDifficulty([5, 5], []); const result = calculateEncounterDifficulty([party(5), party(5)], "5.5e");
expect(result.tier).toBe("trivial"); expect(result.tier).toBe(0);
expect(result.totalMonsterXp).toBe(0); expect(result.totalMonsterXp).toBe(0);
}); });
it("returns high with empty party array (zero budget thresholds)", () => { it("returns tier 3 with no party levels (zero budget thresholds)", () => {
// Domain function treats empty party as zero budgets — any XP exceeds all thresholds. const result = calculateEncounterDifficulty([enemy("1")], "5.5e");
// The useDifficulty hook guards this path by returning null when no leveled PCs exist. expect(result.tier).toBe(3);
const result = calculateEncounterDifficulty([], ["1"]);
expect(result.tier).toBe("high");
expect(result.totalMonsterXp).toBe(200); expect(result.totalMonsterXp).toBe(200);
expect(result.partyBudget).toEqual({ low: 0, moderate: 0, high: 0 }); expect(result.thresholds).toEqual([
{ label: "Low", value: 0 },
{ label: "Moderate", value: 0 },
{ label: "High", value: 0 },
]);
}); });
it("handles fractional CRs", () => { it("handles fractional CRs", () => {
const result = calculateEncounterDifficulty( const result = calculateEncounterDifficulty(
[1, 1, 1, 1], [
["1/8", "1/4", "1/2"], party(1),
party(1),
party(1),
party(1),
enemy("1/8"),
enemy("1/4"),
enemy("1/2"),
],
"5.5e",
); );
expect(result.totalMonsterXp).toBe(175); // 25 + 50 + 100 expect(result.totalMonsterXp).toBe(175); // 25 + 50 + 100
expect(result.tier).toBe("trivial"); // 175 < 200 Low expect(result.tier).toBe(0); // 175 < 200 Low
}); });
it("ignores unknown CRs (0 XP)", () => { it("ignores unknown CRs (0 XP)", () => {
const result = calculateEncounterDifficulty([1, 1, 1, 1], ["unknown"]); const result = calculateEncounterDifficulty(
[party(1), party(1), party(1), party(1), enemy("unknown")],
"5.5e",
);
expect(result.totalMonsterXp).toBe(0); expect(result.totalMonsterXp).toBe(0);
expect(result.tier).toBe("trivial"); expect(result.tier).toBe(0);
});
it("subtracts XP for party-side combatant with CR", () => {
// 4x level 1 party, 1 enemy CR 2 (450 XP), 1 party CR 1 (200 XP)
// Net = 450 - 200 = 250
const result = calculateEncounterDifficulty(
[
party(1),
party(1),
party(1),
party(1),
enemy("2"),
{ cr: "1", side: "party" },
],
"5.5e",
);
expect(result.totalMonsterXp).toBe(250);
expect(result.tier).toBe(1); // 250 >= 200 Low, < 300 Moderate
});
it("floors net monster XP at 0", () => {
// Party ally has more XP than enemy
const result = calculateEncounterDifficulty(
[
party(1),
{ cr: "5", side: "party" }, // 1800 XP subtracted
enemy("1"), // 200 XP added
],
"5.5e",
);
expect(result.totalMonsterXp).toBe(0);
expect(result.tier).toBe(0);
});
it("dual contribution: combatant with both level and CR on party side", () => {
// Party combatant with level 1 AND CR 1 on party side
// Level contributes to budget, CR subtracts from monster XP
const result = calculateEncounterDifficulty(
[
{ level: 1, cr: "1", side: "party" }, // budget += lv1, monsterXp -= 200
enemy("2"), // monsterXp += 450
],
"5.5e",
);
expect(result.thresholds).toEqual([
{ label: "Low", value: 50 },
{ label: "Moderate", value: 75 },
{ label: "High", value: 100 },
]);
expect(result.totalMonsterXp).toBe(250); // 450 - 200
});
it("enemy-side combatant with level does NOT contribute to budget", () => {
const result = calculateEncounterDifficulty(
[party(1), { level: 5, side: "enemy" }, enemy("1")],
"5.5e",
);
// Only level 1 party contributes to budget
expect(result.thresholds).toEqual([
{ label: "Low", value: 50 },
{ label: "Moderate", value: 75 },
{ label: "High", value: 100 },
]);
expect(result.totalMonsterXp).toBe(200);
});
it("mixed sides calculate correctly", () => {
// 2 party PCs (level 3), 1 party ally (CR 1, 200 XP), 2 enemies (CR 2, 450 each)
// Budget: 2x level 3 = low 300, mod 450, high 800
// Monster XP: 900 - 200 = 700
const result = calculateEncounterDifficulty(
[party(3), party(3), { cr: "1", side: "party" }, enemy("2"), enemy("2")],
"5.5e",
);
expect(result.thresholds).toEqual([
{ label: "Low", value: 300 },
{ label: "Moderate", value: 450 },
{ label: "High", value: 800 },
]);
expect(result.totalMonsterXp).toBe(700);
expect(result.tier).toBe(2); // 700 >= 450 Moderate, < 800 High
});
});
describe("calculateEncounterDifficulty — 2014 edition", () => {
it("uses 2014 XP thresholds table", () => {
// 4x level 1: Easy=100, Medium=200, Hard=300, Deadly=400
// 1 enemy CR 1 = 200 XP, x1 multiplier = 200 adjusted
// 200 >= 200 Medium → tier 1
const result = calculateEncounterDifficulty(
[party(1), party(1), party(1), party(1), enemy("1")],
"5e",
);
expect(result.tier).toBe(1);
expect(result.thresholds).toEqual([
{ label: "Easy", value: 100 },
{ label: "Medium", value: 200 },
{ label: "Hard", value: 300 },
{ label: "Deadly", value: 400 },
]);
});
it("applies encounter multiplier for 3 monsters (x2)", () => {
const result = calculateEncounterDifficulty(
[
party(1),
party(1),
party(1),
party(1),
enemy("1/8"),
enemy("1/8"),
enemy("1/8"),
],
"5e",
);
// Base: 75 XP, 3 monsters → x2 = 150 adjusted
expect(result.totalMonsterXp).toBe(75);
expect(result.encounterMultiplier).toBe(2);
expect(result.adjustedXp).toBe(150);
});
it("shifts multiplier up for fewer than 3 PCs", () => {
const result = calculateEncounterDifficulty(
[party(1), party(1), enemy("1")],
"5e",
);
// 1 monster, 2 PCs → base x1 shifts up to x1.5
expect(result.encounterMultiplier).toBe(1.5);
expect(result.partySizeAdjusted).toBe(true);
});
it("shifts multiplier down for 6+ PCs", () => {
const result = calculateEncounterDifficulty(
[
party(1),
party(1),
party(1),
party(1),
party(1),
party(1),
enemy("1"),
enemy("1"),
enemy("1"),
],
"5e",
);
// 3 monsters, 6 PCs → base x2 shifts down to x1.5
expect(result.encounterMultiplier).toBe(1.5);
expect(result.partySizeAdjusted).toBe(true);
});
it("shifts multiplier to x5 for 15+ monsters with <3 PCs", () => {
const result = calculateEncounterDifficulty(
[party(1), party(1), ...Array.from({ length: 15 }, () => enemy("0"))],
"5e",
);
// 15+ monsters = x4 base, shift up → x5
expect(result.encounterMultiplier).toBe(5);
expect(result.partySizeAdjusted).toBe(true);
});
it("shifts multiplier to x0.5 for 1 monster with 6+ PCs", () => {
const result = calculateEncounterDifficulty(
[party(1), party(1), party(1), party(1), party(1), party(1), enemy("1")],
"5e",
);
expect(result.encounterMultiplier).toBe(0.5);
expect(result.partySizeAdjusted).toBe(true);
});
it("only counts enemy-side combatants for monster count", () => {
const result = calculateEncounterDifficulty(
[
party(1),
party(1),
party(1),
party(1),
{ cr: "1", side: "party" },
enemy("1"),
enemy("1"),
enemy("1"),
],
"5e",
);
// 3 enemy monsters → x2, NOT 4
expect(result.encounterMultiplier).toBe(2);
});
it("returns tier 0 when adjusted XP meets Easy but not Medium", () => {
// 4x level 1: Easy=100, Medium=200
// 1 enemy CR 1/2 = 100 XP, x1 multiplier = 100 adjusted
// 100 >= Easy(100) but < Medium(200) → tier 0
const result = calculateEncounterDifficulty(
[party(1), party(1), party(1), party(1), enemy("1/2")],
"5e",
);
expect(result.tier).toBe(0);
expect(result.adjustedXp).toBe(100);
});
it("returns no party size adjustment for standard party (3-5)", () => {
const result = calculateEncounterDifficulty(
[party(1), party(1), party(1), party(1), enemy("1")],
"5e",
);
expect(result.partySizeAdjusted).toBe(false);
});
it("returns undefined multiplier/adjustedXp for 5.5e", () => {
const result = calculateEncounterDifficulty([party(1), enemy("1")], "5.5e");
expect(result.encounterMultiplier).toBeUndefined();
expect(result.adjustedXp).toBeUndefined();
}); });
}); });

View File

@@ -1,6 +1,7 @@
import { describe, expect, it } from "vitest"; import { describe, expect, it } from "vitest";
import { import {
calculateInitiative, calculateInitiative,
calculatePf2eInitiative,
formatInitiativeModifier, formatInitiativeModifier,
} from "../initiative.js"; } from "../initiative.js";
@@ -93,6 +94,26 @@ describe("calculateInitiative", () => {
}); });
}); });
describe("calculatePf2eInitiative", () => {
it("returns perception as both modifier and passive", () => {
const result = calculatePf2eInitiative(11);
expect(result.modifier).toBe(11);
expect(result.passive).toBe(11);
});
it("handles zero perception", () => {
const result = calculatePf2eInitiative(0);
expect(result.modifier).toBe(0);
expect(result.passive).toBe(0);
});
it("handles negative perception", () => {
const result = calculatePf2eInitiative(-2);
expect(result.modifier).toBe(-2);
expect(result.passive).toBe(-2);
});
});
describe("formatInitiativeModifier", () => { describe("formatInitiativeModifier", () => {
it("formats positive modifier with plus sign", () => { it("formats positive modifier with plus sign", () => {
expect(formatInitiativeModifier(7)).toBe("+7"); expect(formatInitiativeModifier(7)).toBe("+7");

View File

@@ -35,7 +35,7 @@ describe("rehydrateCombatant", () => {
expect(result?.maxHp).toBe(7); expect(result?.maxHp).toBe(7);
expect(result?.currentHp).toBe(5); expect(result?.currentHp).toBe(5);
expect(result?.tempHp).toBe(3); expect(result?.tempHp).toBe(3);
expect(result?.conditions).toEqual(["poisoned"]); expect(result?.conditions).toEqual([{ id: "poisoned" }]);
expect(result?.isConcentrating).toBe(true); expect(result?.isConcentrating).toBe(true);
expect(result?.creatureId).toBe("creature-goblin"); expect(result?.creatureId).toBe("creature-goblin");
expect(result?.color).toBe("red"); expect(result?.color).toBe("red");
@@ -165,7 +165,45 @@ describe("rehydrateCombatant", () => {
...minimalCombatant(), ...minimalCombatant(),
conditions: ["poisoned", "fake", "blinded"], conditions: ["poisoned", "fake", "blinded"],
}); });
expect(result?.conditions).toEqual(["poisoned", "blinded"]); expect(result?.conditions).toEqual([
{ id: "poisoned" },
{ id: "blinded" },
]);
});
it("converts old bare string format to ConditionEntry", () => {
const result = rehydrateCombatant({
...minimalCombatant(),
conditions: ["blinded", "prone"],
});
expect(result?.conditions).toEqual([{ id: "blinded" }, { id: "prone" }]);
});
it("passes through new ConditionEntry format with values", () => {
const result = rehydrateCombatant({
...minimalCombatant(),
conditions: [{ id: "blinded" }, { id: "frightened", value: 2 }],
});
expect(result?.conditions).toEqual([
{ id: "blinded" },
{ id: "frightened", value: 2 },
]);
});
it("handles mixed old and new format entries", () => {
const result = rehydrateCombatant({
...minimalCombatant(),
conditions: ["blinded", { id: "prone" }],
});
expect(result?.conditions).toEqual([{ id: "blinded" }, { id: "prone" }]);
});
it("drops ConditionEntry with invalid value", () => {
const result = rehydrateCombatant({
...minimalCombatant(),
conditions: [{ id: "blinded", value: -1 }],
});
expect(result?.conditions).toEqual([{ id: "blinded" }]);
}); });
it("drops invalid color — keeps combatant", () => { it("drops invalid color — keeps combatant", () => {
@@ -219,6 +257,50 @@ describe("rehydrateCombatant", () => {
} }
}); });
it("preserves valid cr field", () => {
for (const cr of ["5", "1/4", "0", "30"]) {
const result = rehydrateCombatant({ ...minimalCombatant(), cr });
expect(result).not.toBeNull();
expect(result?.cr).toBe(cr);
}
});
it("drops invalid cr field", () => {
for (const cr of ["99", "", 42, null, "abc"]) {
const result = rehydrateCombatant({ ...minimalCombatant(), cr });
expect(result).not.toBeNull();
expect(result?.cr).toBeUndefined();
}
});
it("combatant without cr rehydrates as before", () => {
const result = rehydrateCombatant(minimalCombatant());
expect(result).not.toBeNull();
expect(result?.cr).toBeUndefined();
});
it("preserves valid side field", () => {
for (const side of ["party", "enemy"]) {
const result = rehydrateCombatant({ ...minimalCombatant(), side });
expect(result).not.toBeNull();
expect(result?.side).toBe(side);
}
});
it("drops invalid side field", () => {
for (const side of ["ally", "", 42, null, true]) {
const result = rehydrateCombatant({ ...minimalCombatant(), side });
expect(result).not.toBeNull();
expect(result?.side).toBeUndefined();
}
});
it("combatant without side rehydrates as before", () => {
const result = rehydrateCombatant(minimalCombatant());
expect(result).not.toBeNull();
expect(result?.side).toBeUndefined();
});
it("drops invalid tempHp — keeps combatant", () => { it("drops invalid tempHp — keeps combatant", () => {
for (const tempHp of [-1, 1.5, "3"]) { for (const tempHp of [-1, 1.5, "3"]) {
const result = rehydrateCombatant({ const result = rehydrateCombatant({

Some files were not shown because too many files have changed in this diff Show More