Files
vercel__chat/packages/integration-tests/package.json
Ben Sabic 14b1434998 test(integration-tests): add Emulate.dev-backed tests for the Slack adapter (#477)
* chore(integration-tests): add Emulate.dev Slack emulator devDeps

Add @emulators/slack, @emulators/core, and @hono/node-server as
devDependencies of the private integration-tests package. These power
the upcoming in-process Slack emulator harness used to drive the Slack
adapter against a stateful, Slack-shaped HTTP server instead of mocks.

* test(integration-tests): add Slack emulator test harness

Introduce slack-emulator-utils.ts, a test-only helper that boots the
@emulators/slack Hono app on an ephemeral 127.0.0.1 port via
@hono/node-server and pre-seeds a deterministic team / channel / bot /
human user / OAuth app. The Slack adapter is wired to it via its
existing apiUrl config; no source changes required.

Also exposes startSlackWebhookForwarder, a tiny in-process Node http
forwarder that re-signs the emulator's outbound event_callback
deliveries with x-slack-signature / x-slack-request-timestamp before
handing them to chat.webhooks.slack(...). The emulator's core
WebhookDispatcher only emits GitHub-style X-Hub-Signature-256 headers,
so this bridge is what makes inbound flows exercise the SDK's real
HMAC verification path.

The handle returns direct access to the emulator's Store and
WebhookDispatcher so tests can assert on persisted state instead of
mock call records.

* test(integration-tests): cover Slack auth, postMessage, and reactions via emulator

Add three test files that drive the SlackAdapter's outbound WebClient
calls through the in-process emulator and assert on its stateful store
rather than on mock call records:

- emulator-slack-auth.test.ts (3 tests): auth.test populates botUserId
  during initialize(); explicit botUserId is respected; multi-workspace
  mode skips the call entirely.
- emulator-slack-post-message.test.ts (5 tests): plain text and threaded
  thread.post round-trip into the messages collection and are visible
  via conversations.replies; editMessage updates via chat.update;
  deleteMessage removes via chat.delete; markdown posts succeed.
- emulator-slack-reactions.test.ts (3 tests): addReaction /
  removeReaction round-trip via reactions.add / reactions.remove and
  show up via reactions.get; multi-user reactions accumulate correctly.

These exercise the full HTTP path against a Slack-shaped server,
catching wire-format and contract issues that pure mocks miss.

* test(integration-tests): cover Slack inbound events and OAuth v2 install via emulator

Add two test files that drive end-to-end flows previously only
verifiable against real Slack:

- emulator-slack-events.test.ts (4 tests): a human posts to the
  emulator, which dispatches an event_callback to the local forwarder,
  which signs the body and hands it to chat.webhooks.slack(...). The
  SDK's onNewMention and onNewMessage handlers run with a live Thread
  and the bot's reply lands back in the emulator. Bot self-messages
  are correctly filtered. This is the only Slack adapter test in the
  repo that covers the full inbound-then-outbound round-trip without
  hand-crafted webhook payloads.
- emulator-slack-oauth.test.ts (4 tests): handleOAuthCallback
  exchanges a real authorization code via oauth.v2.access against the
  emulator's authorize/callback flow; the resulting installation is
  persisted in the state adapter; invalid codes and mismatched
  client_secrets are rejected; the freshly issued bot token works for
  subsequent chat.postMessage calls via withBotToken.

* docs(integration-tests): document emulator-* test category

Add an "Emulator tests" entry to the package README so newcomers can
distinguish the new emulator-backed suite from the existing unit and
replay tests, and find the harness in slack-emulator-utils.ts.

* fix(integration-tests): keep full token scopes after emulator.reset()

`applyTokenSeed` (used during `reset()`) was granting only
["chat:write", "channels:read"] to seeded tokens, while the initial
`createCoreServer({ tokens })` call granted the full bot/human scope
sets. After the first `reset()` the bot token silently lost
`channels:history`, `users:read`, `reactions:read`, and
`reactions:write`, which would surface as flaky behaviour for any test
that relied on those scopes after a reset.

Unify both call sites on a single `buildTokenSeedEntries` helper so
fresh-boot and post-reset state always grant the same scopes. Add a
regression test in emulator-slack-auth.test.ts that triggers a manual
`emulator.reset()` and re-asserts that the bot token still resolves
via auth.test.

* refactor(integration-tests): reorganize Slack emulator tests under emulator/slack/

Address review feedback (visyat) by moving the flat
`emulator-slack-*.test.ts` files into a per-adapter directory:

  packages/integration-tests/src/emulator/slack/
      utils.ts
      auth.test.ts
      events.test.ts
      oauth.test.ts
      post-message.test.ts
      reactions.test.ts

This scales cleanly as more adapter emulator suites land (e.g.
`emulator/github/`), instead of cluttering the top-level src tree with
adapter-prefixed file names.

Also hoist the duplicated `silentLogger` definition from each test
file into the shared `utils.ts`, removing five identical copies.

No behavior changes. All 20 emulator tests still pass.

* test(integration-tests): cover Slack multi-workspace token resolution via emulator

Add `emulator/slack/multi-workspace.test.ts` exercising the path that
was previously only covered by replay/mock-based tests: the adapter
runs without a hardcoded `botToken`, multiple workspaces are persisted
in the state adapter via `adapter.setInstallation(teamId, ...)`, and
inbound `event_callback`s for either team are routed to the correct
per-tenant bot token end-to-end against the shared emulator.

To make this work, the helper grew two small additions:

- `addEmulatorWorkspace(emulator, seed)` — register an additional
  team + bot + channel + token on an already-booted emulator, so two
  tenants live side-by-side without needing two emulator instances.
- The inbound forwarder's `augmentEventEnvelope` now accepts an
  optional `resolveTeamId(envelope)` callback, defaulting to a constant
  teamId for the existing single-workspace tests. The multi-workspace
  test passes a resolver that looks up the dispatched event's channel
  in the emulator store and returns the owning team's id, so the SDK's
  per-team token resolver sees the right `team_id` on the envelope.

Three new tests: team-A routing, team-B routing, and `getInstallation`
returning null for unknown team ids. Follow-up from review feedback on
PR #477.

---------

Co-authored-by: Ben Sabic <bensabic@users.noreply.github.com>
2026-05-12 08:29:02 +10:00

33 lines
929 B
JSON

{
"name": "@chat-adapter/integration-tests",
"version": "4.0.0",
"private": true,
"description": "Integration tests for chat",
"type": "module",
"scripts": {
"test": "vitest run --coverage",
"test:watch": "vitest",
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"@emulators/core": "^0.5.0",
"@emulators/slack": "^0.5.0",
"@hono/node-server": "^2.0.2",
"@types/node": "^25.3.2",
"typescript": "^5.7.2",
"vitest": "^4.0.18"
},
"dependencies": {
"@chat-adapter/discord": "workspace:*",
"@chat-adapter/gchat": "workspace:*",
"@chat-adapter/messenger": "workspace:*",
"@chat-adapter/slack": "workspace:*",
"@chat-adapter/state-memory": "workspace:*",
"@chat-adapter/teams": "workspace:*",
"@chat-adapter/telegram": "workspace:*",
"@chat-adapter/whatsapp": "workspace:*",
"chat": "workspace:*",
"discord-api-types": "^0.37.119"
}
}