Skip to content

Add @bb/plugin-sdk/testing - #517

Merged
SawyerHood merged 3 commits into
mainfrom
bb/bb-plugin-sdk-testing-module-separate-pr-thr_bff8ubf7dq
Jul 6, 2026
Merged

SawyerHood merged 3 commits into
mainfrom
bb/bb-plugin-sdk-testing-module-separate-pr-thr_bff8ubf7dq

Conversation

@SawyerHood

Copy link
Copy Markdown
Collaborator

The official test harness for bb plugins, as two new (additive) subpath exports of @bb/plugin-sdk. No existing plugin-sdk APIs change.

@bb/plugin-sdk/testing — backend fake host

const { bb, harness } = createFakePluginHost({
  pluginId?: string,                                // default "test-plugin"
  settings?: Record<string, string | boolean>,      // pre-seeded stored values (secrets included, in-memory)
  sdk?: FakeSdkOverrides,                           // per-area/per-method stubs, loose return types
});

bb satisfies BbPluginApi; semantics are ported from apps/server/src/services/plugins/plugin-api.ts wherever a plugin can observe them: registration name validation and error messages, the kv 256KB cap, append-only storage.migrate, settings descriptor validation + effective-value resolution, rpc/realtime JSON round-tripping, cron validation via cron-parser, reserved CLI/tool names, threads.spawn plugin attribution, and the dispose order (services aborted → hooks LIFO → sqlite closed → stale handle throws PluginContextStaleError). Storage is real better-sqlite3 :memory: (one shared handle, like the host's shared file) — never a mocked db.

harness drives and inspects it:

  • setSettings(values) — validates like the settings route, fires onChange with (next, prev)
  • callRpc(method, input?) / runCli(argv, ctx?) / fetchHttp(method, path, init?) — host-faithful invocation (JSON round trips, exit-code normalization, real Hono context; a throwing route yields the host's 500 { ok: false, error: "plugin route failed: …" })
  • runService(name) → { controller: AbortController, done } (synchronous start, no timers; NeedsConfigurationError by name → recorded, not a crash) and runSchedule(name)
  • emitThreadEvent(event, payload) — typed against PluginThreadEventPayloads, returns caught handler errors
  • callAgentTool(name, input, ctx?) — parse (zod) + execute with default context
  • sdk.calls / sdk.callsTo(path) / sdk.stub(path, fn) — every bb.sdk call recorded; unstubbed methods throw naming the exact path to stub
  • logEntries, realtimeSignals, needsConfigurationMessages, registrations (routes, rpc methods, services, schedules, cli, agent tools, thread actions, mention providers), dispose()
  • makeThreadResponse(overrides?) — complete deterministic ThreadResponse fixture (typecheck fails here if the contract grows)

@bb/plugin-sdk/testing/app — frontend harness

  • installTestPluginRuntime() fills globalThis.__bbPluginRuntime.pluginSdkApp with a satisfies PluginSdkApp test implementation (the same seam bb plugin build shims).
  • loadPluginApp(() => import("./app")) installs the runtime before the plugin module evaluates (it binds the runtime at import time), runs setup against a collector ported from apps/app/src/lib/plugin-app-definition.ts (same validation, same messages, chrome default filled), and returns typed registrations.
  • renderSlot(registration, props, { rpc, settings, context }) mounts the component via Testing Library + jsdom with mock hook backends and returns the render result plus rpcCalls, emitRealtime(channel, payload), navigateCalls, and a composer log (quotes/mentions/focusCount).

Proof tests

  • examples/plugins/slack-bot/server.test.ts (7 tests): URL-verification handshake, signature rejection, first mention → signed webhook → kv mapping → recorded threads.spawn with origin: "plugin" attribution, follow-up threads.send, unconfigured 503 + needs-configuration, and thread.idle posting the answer back to the Slack thread (global fetch stubbed).
  • examples/plugins/notes/app.test.tsx (4 tests): slot registrations, nav-panel tree over rpc, realtime-driven refetch, toPluginPanel deep links, and note creation over rpc.
  • packages/plugin-sdk/src/testing/__tests__ (25 tests): kv cap, append-only migrations, settings onChange/no-op/unset, rpc & http & cli semantics, service abort + NeedsConfigurationError, cron validation, thread events, sdk recording/attribution/late stubs, zod agent tools, dispose order + stale-handle poisoning, realtime normalization.

Example packages gain test scripts + vitest configs, and root vitest.config.ts project discovery now includes examples/plugins so the suites aren't silently skipped.

Docs & scope decisions

  • bb-plugin-authoring SKILL.md Testing section rewritten around the harness with copy-pasteable patterns (host-loop guidance kept as a subsection); packages/plugin-sdk/README.md documents the subpaths.
  • Not in the bundled .d.ts for scaffolded plugins (V1 is workspace/in-repo consumers only — the harness runtime-imports better-sqlite3/hono/cron-parser/react, which standalone scaffolds don't carry). Documented in the README, the skill, and the module docblock.
  • Reserved-name lists (RESERVED_BB_CLI_COMMANDS, RESERVED_AGENT_TOOL_NAMES) are duplicated from the server with pointer comments (the contract package cannot import apps/server); both are already hand-maintained lists there.

Validation

  • turbo run typecheck --filter=@bb/plugin-sdk --filter=@bb/server --filter=@bb/app ✓ (includes the bundled-dts staleness check)
  • turbo run test --filter=@bb/plugin-sdk --filter=bb-plugin-slack-bot --filter=bb-plugin-notes ✓ 36/36
  • apps/server plugin-authoring-docs.test.ts (skill durability) ✓ 9/9

🤖 Generated with Claude Code

SawyerHood and others added 3 commits July 6, 2026 13:05
createFakePluginHost() returns a bb satisfying BbPluginApi backed by real
better-sqlite3 :memory: storage, in-memory kv with the host's 256KB cap,
host-ported registration validation/error shapes, and a recordable bb.sdk
stub with threads.spawn plugin attribution. The harness drives rpc/http/cli,
services and schedules (no timers), settings saves (onChange), thread
lifecycle events, agent tools, and dispose (hooks LIFO, stale handles throw).

@bb/plugin-sdk/testing/app tests a plugin's app.tsx without the bb host:
installTestPluginRuntime() fills globalThis.__bbPluginRuntime.pluginSdkApp,
loadPluginApp() captures typed slot registrations with the app's own
validation, and renderSlot() mounts a slot with mock rpc/realtime/settings/
context/navigate/composer backends over Testing Library + jsdom.

Co-Authored-By: Claude Fable 5 <[email protected]>
slack-bot server.test.ts covers the webhook flow end to end against
createFakePluginHost: URL-verification handshake, signature rejection,
first-mention spawn (recorded with plugin attribution) + kv mapping,
follow-up send, unconfigured 503 + needs-configuration, and the thread.idle
reply posted back to Slack. notes app.test.tsx renders the nav panel via
loadPluginApp/renderSlot: rpc-backed tree, realtime-driven refetch,
toPluginPanel deep links, and note creation over rpc.

Example plugin packages gain test scripts and vitest configs; the root
vitest project discovery now includes examples/plugins so the suites are
not silently skipped.

Co-Authored-By: Claude Fable 5 <[email protected]>
Testing section now leads with the official harness (backend
createFakePluginHost patterns, frontend loadPluginApp/renderSlot patterns,
the thunk-import constraint) and points at the example tests; the live
bb-plugin-dev loop keeps its own subsection. Notes that the testing
subpaths are workspace-only for V1 (not in the scaffold's bundled dts).

Co-Authored-By: Claude Fable 5 <[email protected]>
@SawyerHood
SawyerHood merged commit d60c58a into main Jul 6, 2026
6 checks passed
@SawyerHood
SawyerHood deleted the bb/bb-plugin-sdk-testing-module-separate-pr-thr_bff8ubf7dq branch July 6, 2026 20:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant