Files
hooks-lib/README.md
Kaysser Kayyali a6a1e7cd75 v0.3.0: add Playwright test (30/30), mark §F release gate done
Implements tests/PLAN.md § Playwright run (Section F's release
gate). 30 assertions in tests/verify-hooks-lib-foundry.mjs,
runs in ~10s against a live Foundry v14 instance.

What the Playwright test verifies that the no-Foundry smoke
test CAN'T:
- The library's init hook runs in Foundry's real lifecycle
  (not a stubbed manual init call).
- mod.api is set on game.modules.get('foundry-hooks-lib') with
  the documented surface.
- install() at init calls Hooks.on for every raw hook in the
  registered set.
- A real Foundry-fired hook (combatStart, combatRound) delivers
  an envelope to a consumer that subscribed AFTER init.
- A synthetic Hooks.callAll fire delivers envelopes to
  subscribers (sync-mode hooks via direct dispatch,
  async-mode hooks via microtask).
- subscribeAll receives envelopes from every registered hook.
- subscribe with an unknown hook name throws TypeError.
- A throwing consumer does NOT break the dispatch chain; the
  second subscriber still fires; the error is logged via
  console.error with the [foundry-hooks-lib] prefix.
- subscribe + unsubscribe correctly gate delivery.

**Bumps package.json to 0.3.0** (was 0.2.0 — version lagged
behind module.json's 0.3.0 from the rename commit).

**Adds test:foundry and test:all npm scripts.**

**Marks tests/PLAN.md status as Implemented** (was Proposed).
The Definition of done gate (npm run test:foundry exits 0 in
<30s) is now met.

**Adds playwright-core as a devDependency** (1 package, ~no
runtime impact since this repo's module doesn't depend on it at
runtime — it's a test-only dep).

**Final tallies:**
- npm test: 554/554 in ~0.4s (no-Foundry smoke)
- npm run test:foundry: 30/30 in ~10s (Playwright)
- npm run test:perf: 6/6 in ~5s (median 0.0003ms/fire)
- npm run test:all: all of the above
2026-06-20 17:27:44 -04:00

5.5 KiB
Raw Blame History

Foundry Hooks Lib (foundry-hooks-lib)

Foundry VTT module (id: foundry-hooks-lib) that turns Foundry's hook soup (dnd5e, combat, token updates, canvas/UI) into a clean, normalized event stream. Library-only — no UI, no settings, no chat output. Designed to be consumed by any module that wants Foundry events in a stable shape.

Part of the Foundry module split (battle-focus + its-achievable + this lib). Consumers today: battle-focus (encounter + journal + summary) and its-achievable (achievements, rewards, wall, HUD). System-specific knowledge (dnd5e rolls, PF2e, etc.) lives in separate adapter repos that declare Foundry + system version ranges they support.

v0.3.0 — module id renamed (hax-hooks-lib → foundry-hooks-lib)

v0.2.0 is a complete rewrite. v0.1.0 shipped as a curated-event catalog (a list of hand-written handlers for 18 specific Foundry events). v0.2.0 replaces that with a generic facade:

  • Subscribes to every relevant Foundry hook (combat lifecycle, all document CRUD, canvas/UI, dnd5e v2 roll hooks, etc.).
  • Emits a uniform envelope — {ts, hook, args} — with no domain interpretation.
  • Consumers write queries against the envelope. "When bob takes damage" is a consumer-side query, not a hook name the library knows about.
  • System-specific derived events (e.g. "dnd5e attack roll") live in separate adapter repos. Adapters register a manifest with Foundry + system version ranges; the library evaluates at ready and loads matching adapters.

Why this shape: Foundry's hook names and arities change between versions. The library absorbs that churn (§9 + §10 of the contract) so consumers don't have to rewrite every Foundry upgrade.

Status

v0.2.0 — generic facade per docs/HOOK_CONTRACT.md. Smoke test: 554/554 assertions. Perf test: median 0.0003ms/fire (333× under the 0.1ms budget).

Envelope shape

Every fire produces exactly one envelope:

{
  ts:   1719000000000,    // epoch ms when Foundry fired
  hook: "updateActor",    // the Foundry hook name (normalized; see §9)
  args: [doc, change, options, userId]  // positional args, verbatim
}

That's it. No kind, no normalized fields, no consumer metadata. Consumers that want {kind, actorId, delta} build that themselves from args. This is intentional: the library is the boundary that absorbs Foundry version churn.

Public API (on game.modules.get("foundry-hooks-lib").api)

import { subscribe, subscribeMany, subscribeAll } from
  game.modules.get("foundry-hooks-lib").api;

// Single hook:
const unsub = subscribe("updateActor", (envelope) => {
  const [actor, change] = envelope.args;
  // ...
});

// Batch subscribe (atomic):
subscribeMany({
  updateActor: handleActorUpdate,
  createToken: handleTokenCreate,
});

// Every hook (for audit logs):
subscribeAll((envelope) => log(envelope));

// One-shot cleanup:
unsub();   // or unsubscribeAll() to purge everything

System adapters

A system adapter is a separate Foundry module. At its init, it calls:

hooksLib.api.registerSystemAdapter({
  id: "foundry-hooks-dnd5e",
  moduleId: "foundry-hooks-dnd5e",
  system: { id: "dnd5e", versions: ">=5.2.0 <5.3.0" },
  foundryVersions: ">=13 <15",
  factory: () => [ /* derived-event registrations */ ],
});

The library evaluates the manifest against game.system.id + version and game.version at ready. Matching adapter factories are called once. Non-matching adapters log a warning naming the version mismatch (or silently skip for non-matching system).

Subscribed hook set (v0.2.0)

Lifecycle, document CRUD (Actor/Token/Item/Scene/JournalEntry/ ActiveEffect/Combat/Combatant), combat lifecycle, chat & rolls (incl. dnd5e v2 roll hooks), canvas/scene/UI (canvasInit/Ready/Pan, controlToken, hoverToken, targetToken, lighting/sightRefresh, collapseSidebar, changeSidebarTab, getSceneControlButtons, renderChatMessage, renderChatInput, renderJournalPageSheet, rtcSettingsChanged), and more. Full list: scripts/internal/registered-hooks.js.

Error containment

If a consumer callback throws, the library catches it, logs via console.error with the [foundry-hooks-lib] prefix and the hook name, and continues dispatching to subsequent callbacks. Errors never propagate to Foundry's hook chain.

Tests

npm test                # 554 assertions in <2s, no Foundry needed (smoke)
npm run test:foundry    # 30 assertions in <15s, Playwright + live Foundry
npm run test:perf       # median 0.0003ms/fire, heap delta check
npm run test:all        # all three

See tests/PLAN.md for what we test and what we don't. The test:foundry runner connects to FOUNDRY_URL (defaults to http://localhost:30000), signs in as Gamemaster, waits for the library's mod.api.isReady() === true, and exercises the live envelope dispatch chain end-to-end.

Architecture notes

  • One envelope shape, one dispatcher. Adding a new Foundry hook is one entry in scripts/internal/registered-hooks.js. No new file.
  • System adapters are repos, not files. Each system's derived knowledge is its own module with its own version cadence.
  • Anti-corruption (§9-§10): library subscribes to BOTH v13 and v14 hook names where applicable; consumers see stable envelope names.
  • Async dispatch (microtask) by default. pre-* + combat* + applyActiveEffect + get*Context dispatch sync because their return values matter.

Dependencies

None. Library-only; system adapters depend on this, not the other way around.

Maintained by Kaysser Taylor + Hermes