Skip to content

feat(tests): add @chat-adapter/tests test kit#470

Merged
bensabic merged 7 commits intomainfrom
feat/tests-package
May 9, 2026
Merged

feat(tests): add @chat-adapter/tests test kit#470
bensabic merged 7 commits intomainfrom
feat/tests-package

Conversation

@bensabic
Copy link
Copy Markdown
Contributor

@bensabic bensabic commented May 9, 2026

Adds a new published package @chat-adapter/tests with Vitest factories, custom matchers, and a setup file for people building Chat SDK bots and custom adapters.

Factories (@chat-adapter/tests):

  • createMockAdapter(name?, overrides?) — every method is vi.fn() with sensible defaults; overrides merges on top
  • createMockChatInstance(options?) — every process* handler is vi.fn(); getState/getUserName/getLogger wired up
  • createMockState() — in-memory MockStateAdapter with working subscriptions, locks, KV, lists, and queues; cache exposes the underlying map
  • createTestMessage(id, text, overrides?) — markdown text is parsed into the formatted AST
  • mockLogger / createMockLogger() — shared default vs fresh-per-call

Matchers (@chat-adapter/tests/matchers):

  • expect(adapter).toHavePosted(threadId, textPattern?)
  • expect(chat).toHaveDispatched(handler)
  • await expect(state).toBeSubscribedTo(threadId)

Setup file (@chat-adapter/tests/setup) auto-registers all matchers via expect.extend — drop into vitest.config.ts setupFiles.

chat and vitest are peer dependencies. Adapter-specific helpers (e.g. signed Slack webhook builders, Teams claim builders) belong in each adapter's own /testing subpath, not in this kit, so adopting @chat-adapter/tests doesn't pull in adapter dependencies you don't use.

Docs:

  • New page at /docs/testing walks through factories, matchers, and the two main flows (bot authors testing handlers, adapter authors testing webhook → dispatch).
  • Cross-link added from /docs/contributing/testing clarifying that the hand-rolled patterns there are for repo contributors building first-party adapters; consumers should use the kit instead.

Existing internal packages/chat/src/mock-adapter.ts is left untouched — migrating the chat package's own tests onto the kit (and dropping vitest from chat's deps) is a follow-up.

bensabic added 4 commits May 9, 2026 11:53
New package providing Vitest factories, custom matchers, and a setup file for
people building Chat SDK adapters and bots.

Factories: createMockAdapter, createMockChatInstance, createMockState (with
working in-memory subscriptions/locks/KV/queues), createTestMessage,
mockLogger / createMockLogger.

Matchers: toHavePosted(threadId, textPattern?), toHaveDispatched(handler),
toBeSubscribedTo(threadId). Auto-register via the
'@chat-adapter/tests/setup' subpath in vitest setupFiles.

chat and vitest are peer dependencies. Adapter-specific helpers (e.g. signed
Slack webhook builders) belong in each adapter's own /testing subpath, not
in this kit.
New content/docs/testing.mdx walks bot authors and custom-adapter authors
through the kit's factories, custom matchers, and setup file. Added under
the Usage section in the sidebar, after error-handling.

Cross-link from contributing/testing.mdx clarifying that the hand-rolled
patterns there are for repo contributors building first-party adapters,
while consumers of Chat SDK should use @chat-adapter/tests.
@bensabic bensabic requested a review from a team as a code owner May 9, 2026 02:01
@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented May 9, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
chat Ready Ready Preview, Comment, Open in v0 May 9, 2026 4:12am
chat-sdk-nextjs-chat Ready Ready Preview, Comment, Open in v0 May 9, 2026 4:12am

Comment thread packages/tests/src/matchers.ts Outdated
Adapter.postMessage is (threadId: string, message: AdapterPostableMessage)
— previously the matcher read args[0] as { id: string } and args[1] as
{ text: string }, neither of which match the actual SDK shape. The matcher's
own tests fed the same wrong shape into the mock so they passed locally
while the matcher silently failed against any real bot or adapter.

Now compares args[0] as a string threadId, and extracts a comparable string
from AdapterPostableMessage's union — strings directly, PostableMarkdown
.markdown, PostableRaw.raw, and PostableCard.fallbackText. PostableAst and
fallback-less cards aren't text-matchable; documented in the JSDoc.

Tests updated to call postMessage with the real signature and to cover
each comparable AdapterPostableMessage shape.
bensabic added 2 commits May 9, 2026 14:01
Construct a real `Chat` with a `createMockAdapter` + `createMockState` and
exercise `Chat.thread().post()` and `.subscribe()` end-to-end. The matchers
(toHavePosted, toBeSubscribedTo) then assert against the actual call shape
the SDK uses, so a future signature drift breaks here instead of silently
agreeing with whatever wrong shape lives in the unit tests.

This is the regression guard for the postMessage-shape bug fixed in the
prior commit: each new matcher in subsequent PRs should be paired with a
smoke case here.
Adds toHaveEdited, toHaveDeleted, toHaveReactedWith, toHaveStartedTyping,
and toHavePostedToChannel — covering the common Adapter mutation surface
that bot authors assert on. Each matcher's signature was checked against
packages/chat/src/types.ts, and each is paired with a smoke case that
drives a real Chat through the corresponding Thread/Channel API so
signature drift breaks the smoke test instead of silently agreeing with
the unit tests.

Emoji matching accepts both plain strings and EmojiValue ({ name }).
Text matching reuses the same extraction rules as toHavePosted —
strings, PostableMarkdown.markdown, PostableRaw.raw, and
PostableCard.fallbackText. Documented in matcher JSDoc, README, and
the Testing docs page.
@bensabic bensabic merged commit 0adf3ad into main May 9, 2026
13 checks passed
@bensabic bensabic deleted the feat/tests-package branch May 9, 2026 04:17
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.

2 participants