Skip to content

Testing a plugin

@taflor/testkit starts a plugin the way the table page does - the manifest checked, its data registered, then activate(ctx) with the real context, registries and loader - but in Node, without a browser. Every bundled plugin is tested with it, and the checks it runs on yours are the same ones.

Not published yet

The kit is testkit/ in the Taflor repository; the npm package @taflor/testkit follows with the first stable plugin API. Until then, import it by path from a Taflor checkout. It needs Node 22.15 or newer and runs with node:test.

A plugin's tests

These are the tests of the lantern example from Writing a plugin, in its tests/ folder:

// The lantern example's own tests, as a plugin repository has them (docs/how-to/testing.md).
// Run with `node --test tests/`; server/taflor_server/tests/js/test_docs_examples.mjs runs them
// in Taflor's checks.
import assert from "node:assert/strict";
import test from "node:test";
import { fileURLToPath } from "node:url";

// In a plugin repository of its own: from "@taflor/testkit".
import { assertOwnNamespace, assertRegisteredAsDeclared, manifestProblems, mountPlugin, readManifest, runEffect } from "../../../../../testkit/index.js";

const dir = fileURLToPath(new URL("..", import.meta.url));
const manifest = readManifest(dir);

test("the manifest is valid, and every file it names is there", () => {
  assert.deepEqual(manifestProblems(manifest, { dir }), []);
});

test("it starts, registers what it declares and writes only its own names", async () => {
  const mounted = await mountPlugin(manifest, { dir });
  try {
    assertRegisteredAsDeclared(mounted.registry, manifest);
    assertOwnNamespace(mounted, manifest);
  } finally {
    await mounted.unmount();
  }
});

test("the glow draws and stays well within the frame budget", async () => {
  const mounted = await mountPlugin(manifest, { dir });
  try {
    const glow = mounted.registry.registry("tableEffects").get("lantern:glow");
    const run = runEffect(glow, { frames: 120 });
    assert.ok(run.calls.get("fillRect") > 0);
    assert.ok(run.avgMs < 2, `${run.avgMs.toFixed(3)} ms per frame`);
  } finally {
    await mounted.unmount();
  }
});

Run them with node --test tests/. Taflor's own checks run them too (server/taflor_server/tests/js/test_docs_examples.mjs), so this page can't drift from what the kit does.

What the kit gives you

mountPlugin(manifest, options) starts one plugin. With dir (the plugin's folder) it imports the entry and reads labels and data files from there, and @taflor/api resolves to the core's API module. It returns:

field what
ctx the plugin's context, as activate got it
activated what activate returned, awaited (the page's loader drops it)
registry the registries, with the core's entries and the plugin's (registry.registry("tableEffects"))
notices what the GM was told (ctx.ui.notify, failures), [message, level]
logged, reports console lines, and the plugin.error reports the page would send
sync, messages the in-memory sync (memorySync()): what the plugin wrote and what the "server" answered
state, reason "active", or why not (with allowFailure: true it doesn't throw)
unmount() stops it as the page does: deactivate, then the rollback; throws if deactivate failed

Options include host (a fakeHost({...}): role, user, capabilities, providers), settings, timeoutMs, and mounts and tools to inspect what the plugin contributed to menus, the HUD and the tool chain (new Mounts() and new Tools() of the client).

With bundle: true (and dir), the kit starts the plugin from its bundle in <dir>/_bundle/ instead of its source entry (SPEC ยง10.8). Pass a path instead of true to take the bundle from another folder that holds its bundle.json, such as one that bundle-plugins.mjs --out DIR wrote. The kit reads bundle.json first and throws if the bundle is missing, belongs to another plugin, lacks a file or is stale, i.e. built from sources that have changed since. So a test never runs an old bundle by mistake. For Taflor's bundled plugins, scripts/bundle builds the bundles, and its equivalence test mounts every code plugin both ways and compares what each registers. For your own plugin, build with your bundler first and mount the result the same way.

The checks:

  • manifestProblems(manifest, {dir}) - one line per problem, as the server words them: the schema, and every file the manifest names exists inside the folder. assertValidManifest throws instead.
  • assertRegisteredAsDeclared(registries, manifest) - the plugin registered exactly the names its contributes declares, under its namespace, its performance keys included.
  • assertOwnNamespace(mounted, manifest) - nothing written outside the plugin's namespace: no registry entry under another id, no data.ext.<other> in its object writes.
  • runEffect(def, {frames}) - plays a tableEffects def on a fake canvas as the core's engine does; returns {frames, avgMs, maxMs, calls}.
  • perfProbe({budgetMs}) - a frame scheduler on simulated frames that times ctx.frame callbacks for real: run(frames), stat(owner), assertWithinBudget(owner). mountPlugin uses one; it's mounted.probe.
  • fakeTable(), fakeStore(), fakeCamera() - a real Table prototype without the DOM, for bundled kinds that attach to the table.

A mounted plugin has hooks of its own: a test that wants the page's dice tray to ask its dice.summary provider hands the provider on, as test_docs_examples.mjs does for the pbta-moves example of Systems and dice.

The protocol conformance suite

The conformance suite is for servers and hosts, not plugins: it checks that a table authority answers the protocol as specified. The scenarios live in @taflor/protocol (protocol/conformance/v2/<capability>/*.yaml, format in protocol/conformance/FORMAT.md): a fixture with seats and objects, then steps that send, expect, never and expectClose. A server or host claims a capability only if it passes that capability's scenarios.

Two runners exist:

  • Python, in-process, against taflor_server: taflor_server.testing.conformance.run_scenario, which server/taflor_server/tests/test_conformance.py runs for every scenario. A host that replaces Taflor's (PhaseSix, for example) runs the same tests with its own host.
  • JavaScript: runScenario(scenario, target) of protocol/conformance.js, with loadScenarios() and the message validators. The client plays every scenario against its offline authority, OfflineSync (server/taflor_server/tests/js/test_conformance_offline.mjs), which lists the scenarios only a server can pass, with the reason.

A network runner against any running server comes with 1.x.