.github/9
ISSUE_TEMPLATE/3bug_report.md config.yml feature_request.md
workflows/3ci.yml clean-clone.yml release.yml
CODEOWNERS dependabot.yml pull_request_template.md
apps/630
api/250
src/243
common/30
__tests__/3authorize.test.ts bigint-serialization.interceptor.test.ts files-limits.test.ts
filters/1all-exceptions.filter.ts
guards/3auth.guard.ts internal-service.guard.ts rate-limit.guard.ts
interceptors/2actor-context.interceptor.ts bigint-serialization.interceptor.ts
middleware/2idempotency.test.ts idempotency.ts
pipes/1zod-validation.pipe.ts
actor-context.ts audit.service.ts cache.test.ts cache.ts cookies.ts core.module.ts deprecation.ts email.service.ts errors.ts event-bus.ts log-scrubber.test.ts log-scrubber.ts logger.ts permission-cache-bridge.ts prisma.service.ts redis.service.ts trace-metadata.ts types.ts
config/1env.ts
lib/11
__tests__/5crypto.test.ts filter-builder.test.ts jwt.test.ts pagination.test.ts positions.test.ts
authorize.ts crypto.ts filter-builder.ts jwt.ts pagination.ts positions.ts
modules/197
ai/28
__tests__/4ai-writing.test.ts database-ai.test.ts generate-executor.test.ts prompts.test.ts
generator/4executor.ts generate.controller.ts generate.dto.ts generate.service.ts
ai-chat.service.ts ai-rate-limit.ts ai-writing.controller.ts ai-writing.dto.ts ai-writing.service.ts ai.controller.ts ai.module.ts ai.service.ts budget-guard.service.ts database-ai.controller.ts database-ai.dto.ts database-ai.service.ts db-prompts.ts dto.ts gateway-provider.ts ledger.service.ts prompts.ts rag.controller.ts rag.dto.ts rag.service.ts
api-keys/3api-keys.controller.ts api-keys.module.ts api-keys.service.ts
audit/2audit-log.controller.ts audit.module.ts
auth/4auth.controller.ts auth.module.ts auth.service.ts dto.ts
automations/5automations.controller.ts automations.module.ts automations.service.ts dto.ts webhook-incoming.controller.ts
blocks/5
__tests__/1batch-version-cas.test.ts
block.service.ts blocks.controller.ts blocks.module.ts dto.ts
comments/4comments.controller.ts comments.module.ts comments.service.ts dto.ts
databases/9
__tests__/3normalize-select-options.test.ts option-id-canonicalization.test.ts record-open-query-shape.test.ts
database.service.ts databases.controller.ts databases.module.ts dto.ts property-cache-schema.ts query.service.ts
docs/2docs.controller.ts docs.module.ts
files/10
__tests__/1quota.test.ts
dto.ts files.controller.ts files.module.ts files.service.ts quota.ts record.ts s3.ts uploads.controller.ts uploads.service.ts
forms/18debug.controller.ts form.dto.ts forms.controller.ts forms.module.ts forms.service.ts markdown.test.ts markdown.ts optional-auth.guard.ts public-forms.controller.ts public-forms.service.ts question.dto.ts questions.service.ts rate-limit.ts recaptcha.ts slug.ts submissions.controller.ts submissions.service.ts submit-schema.ts
health/3health.controller.ts health.module.ts health.service.ts
integrations/3integrations.controller.ts integrations.module.ts integrations.service.ts
internal/12
__tests__/2flush-query-shape.test.ts internal.endpoints.test.ts
ai-test-seam.controller.ts ai-test-seam.dto.ts ai-test-seam.service.ts internal.controller.ts internal.dto.ts internal.module.ts internal.service.ts test-seam.controller.ts test-seam.dto.ts test-seam.service.ts
notifications/3notifications.controller.ts notifications.module.ts notifications.service.ts
oauth/6
__tests__/1oauth.scopes.test.ts
oauth.controller.ts oauth.module.ts oauth.service.ts scopes.ts token-keys.ts
org/15audit.service.ts embed-preview.controller.ts guards.ts members.service.ts org-admin.controller.ts org-policy.service.ts org.module.ts overview.service.ts policy-effective.controller.ts policy.schema.ts policy.test.ts retention.service.ts totp.test.ts totp.ts two-factor.service.ts
orgs/5dto.ts orgs.controller.ts orgs.module.ts orgs.service.ts util.ts
pages/4dto.ts page.service.ts pages.controller.ts pages.module.ts
scim/15
__tests__/3email-mapping.test.ts etag.test.ts filters.test.ts
etag.ts filters.ts groups.service.ts schemas.ts scim-admin.controller.ts scim-admin.service.ts scim-auth.guard.ts scim-auth.service.ts scim-errors.ts scim.controller.ts scim.module.ts users.service.ts
search/4
__tests__/1search.service.test.ts
search.controller.ts search.module.ts search.service.ts
sessions/3sessions.controller.ts sessions.module.ts sessions.service.ts
share/3share.controller.ts share.module.ts share.service.ts
sso/16
__tests__/4enforcement.test.ts oidc.verify.test.ts provisioning.test.ts saml.validate.test.ts
enforcement.ts oidc.controller.ts oidc.service.ts oidc.ts provisioning.service.ts saml.controller.ts saml.ts schemas.ts sso-admin.controller.ts sso-admin.service.ts sso-secrets.ts sso.module.ts
teams/4dto.ts teams.controller.ts teams.module.ts teams.service.ts
users/4dto.ts users.controller.ts users.module.ts users.service.ts
webhooks/3webhooks.controller.ts webhooks.module.ts webhooks.service.ts
workspaces/4dto.ts workspaces.controller.ts workspaces.module.ts workspaces.service.ts
types/1xmldom.d.ts
app.module.ts constants.ts main.ts
_probe_rawbody.cjs .env.example Dockerfile package.json README.md tsconfig.json vitest.config.ts
realtime/33
src/17auth.ts comments.ts config.ts event-source.ts http-store.ts index.ts metrics.ts notifications.ts page-updated-source.ts presence.ts prisma-store.ts protocol.ts rate-limit.ts room-key.ts rooms.ts server.ts update-log.ts
test/11auth.test.ts config.test.ts event-source.test.ts http-store.test.ts metrics-merge.test.ts page-updated-source.test.ts presence.test.ts protocol.test.ts rate-limit.test.ts rooms.test.ts task-0094-builders.test.ts
.env.example Dockerfile package.json README.md tsconfig.json
web/255
src/244
app/45
__tests__/1theme-tokens.test.ts
(app)/26
w/25
[workspace]/25
ask/1page.tsx
databases/1
[id]/1page.tsx
favorites/1page.tsx
files/1page.tsx
notifications/1page.tsx
page/4
[id]/4
[block]/1page.tsx
share/1page.tsx
layout.tsx page.tsx
search/1page.tsx
settings/10
appearance/1page.tsx
automations/3
[id]/1page.tsx
new/1page.tsx
page.tsx
billing/1page.tsx
developers/1page.tsx
members/1page.tsx
permissions/1page.tsx
teams/1page.tsx
page.tsx
shared/1page.tsx
templates/1page.tsx
trash/1page.tsx
layout.tsx page.tsx
layout.tsx
(auth)/4
login/1page.tsx
register/1page.tsx
reset-password/1page.tsx
layout.tsx
api/2
metrics/1route.ts
v1/1
health/1route.ts
f/1
[slug]/1page.tsx
health/1
ready/1route.ts
org/2
[orgId]/2
admin/2
integrations/1page.tsx
page.tsx
share/1
[id]/1page.tsx
error.tsx global-error.tsx globals.css layout.tsx not-found.tsx page.tsx providers.tsx
components/118
admin/10
org-admin/8ApiKeysTab.tsx AuditTab.tsx MembersTab.tsx OverviewTab.tsx PoliciesTab.tsx RetentionTab.tsx WebhooksTab.tsx WorkspacePicker.tsx
ScimCard.tsx SsoTabs.tsx
ai/6ai-panel.tsx ask-ai-submenu.tsx db-ai-dialog.tsx generate-page-dialog.tsx writing-actions-menu.tsx writing-diff.tsx
automations/5action-editor.tsx condition-editor.tsx lib.ts run-history.tsx trigger-editor.tsx
collaboration/6CursorLayer.tsx OfflineBanner.tsx PresenceBar.tsx README.md ReviewFailedDialog.tsx StatusChip.tsx
comments/3comment-panel.tsx comment-thread.tsx mention-input.tsx
database/27
__tests__/2cell-editor.test.tsx cell-renderer.test.tsx
AiExtractDialog.tsx AiFormulaDialog.tsx AiGenerateDialog.tsx board-view.tsx buttons-panel.tsx calendar-view.tsx cell-editor.tsx cell-renderer.tsx chart-view.tsx database-page-view.tsx database-toolbar.tsx database-view-renderer.tsx db-embedded.tsx filter-builder.tsx form-builder-view.tsx formula-editor.tsx gallery-view.tsx group-builder.tsx list-view.tsx property-editor.tsx record-header.tsx record-panel.tsx sort-builder.tsx table-view.tsx timeline-view.tsx
dialogs/7
__tests__/1move-to-dialog.test.tsx
create-dialog.tsx dialog.tsx file-permissions-dialog.tsx invite-dialog.tsx move-to-dialog.tsx share-dialog.tsx
editor/19
__tests__/3code-block-text.test.ts code-block.test.tsx expandable.test.tsx
block-card.tsx block-renderer.tsx block-save-queue.ts block-toolbar.tsx context-menu.tsx expandable.tsx history-popover.tsx inline-editor.tsx insert-dialog.tsx page-canvas.tsx page-spinner.tsx page-view.tsx selection-toolbar.tsx slash-menu.tsx template-dialog.tsx type-menu.tsx
files/4FileBlock.tsx FileBrowser.tsx FilePicker.tsx PreviewModal.tsx
form/4form-field.tsx form-renderer.tsx form-responses-view.tsx form-view.tsx
layout/8
__tests__/1page-tree-icon.test.tsx
command-palette.tsx factory-logo.tsx page-tree-node.tsx sidebar.tsx theme-provider.tsx topbar.tsx workspace-shell.tsx
mentions/3
__tests__/1mention-model.test.ts
mention-model.ts MentionPopup.tsx
search/3ask-ai-search.tsx results-list.tsx search-dialog.tsx
settings/10
api-keys/4ApiKeyCreateDialog.tsx ApiKeyRevealDialog.tsx ApiKeyTable.tsx ScopesBadge.tsx
webhooks/5CurlExamples.tsx DeliveryInspector.tsx EventExplorer.tsx OverviewTab.tsx WebhooksTab.tsx
settings-shell.tsx
ui/3confirm-dialog.tsx form.tsx toaster.tsx
hooks/21
__tests__/4realtime-frame-handler.test.ts use-blocks.test.ts use-database-options.test.ts use-workspace.test.tsx
realtime-frame-handler.ts use-ai-stream.ts use-auth.ts use-blocks.ts use-database.ts use-db-ai-stream.ts use-forms.ts use-members.ts use-mention-sources.ts use-notifications.ts use-offline-status.ts use-pages.ts use-presence-announcer.ts use-presence-toasts.ts use-presence.ts use-realtime.ts use-workspace.ts
lib/50
__tests__/21ai-database.test.ts ai-generate.test.ts ai-sse.test.ts ai-writing.test.ts api-keys.test.ts api-members.test.ts auth-refresh.test.ts auth-store.test.ts batch-queue-drop.test.ts block-content.test.ts clipboard-import.test.ts code-highlight.test.ts offline-status.test.ts presence-announcer.test.ts presence-color.test.ts presence-cursor.test.ts presence-emitter.test.ts realtime-url.test.ts search.test.ts utils.test.ts writing-diff.test.ts
files/7
__tests__/2picker.test.ts upload.test.ts
api.ts picker.ts upload-store.ts upload.ts use-file-uploads.ts
ai-database.ts ai.ts api-keys.ts api.ts auth-store.ts batch-queue.ts block-content.ts clipboard-import.ts code-highlight.ts constants.ts offline-status.ts org-admin.ts presence-announcer.ts presence-color.ts presence-cursor.ts presence-editor-views.ts presence-emitter.ts query-client.ts realtime.ts search.ts types.ts utils.ts
stores/8
__tests__/1block-patch-store.test.ts
block-patch-store.ts comment-pill-store.ts database-store.ts editor-store.ts offlineQueue.ts presence-store.ts ui-store.ts
instrumentation.ts middleware.ts
.env.example Dockerfile next-env.d.ts next.config.mjs package.json postcss.config.mjs README.md tailwind.config.ts tsconfig.json tsconfig.tsbuildinfo vitest.config.ts
worker/92
src/87
__tests__/13ai-embedding.test.ts ai-request.test.ts ai-usage-budget.test.ts audit-export.test.ts email-decision.test.ts encrypt.test.ts health-search-index-lag.test.ts mime.test.ts retention.test.ts s3-sign.test.ts search-index.test.ts smtp.test.ts webhook-sign.test.ts
health/1server.ts
lib/12
__tests__/1clamav.test.ts
clamav.ts encrypt.ts job-otel.ts mime.ts notify.ts prisma.ts redis-stream.ts redis.ts s3.ts sharp.ts smtp.ts
processors/56
automations/28
__tests__/3condition-evaluator.spec.ts executors.spec.ts run-pipeline.spec.ts
executors/17assign-user.ts call-integration.ts change-status.ts classify.ts create-notification.ts create-page.ts email-cap.ts helpers.ts integration-registry.ts permission.ts record-write.ts registry.ts run-ai-action.ts send-email.ts send-webhook.ts types.ts update-record.ts
condition-evaluator.ts dead-letter.ts log.ts matcher.ts pipeline.ts run-state.ts schedule.ts watchdog.ts
files/15
__tests__/4errors.test.ts scan-gate.test.ts status.test.ts temp.test.ts
steps/4finalize.ts metadata.ts scan.ts thumbnails.ts
cleanup.ts errors.ts generateThumbs.ts processor.ts status.ts temp.ts types.ts
search-index/4builders.ts grants.ts index.ts processor.ts
ai-embedding.ts ai.processor.ts audit-export.ts automations.processor.ts email-decision.processor.ts email.processor.ts maintenance.processor.ts retention.ts webhooks.processor.ts
config.ts events.ts index.ts logger.ts queues.ts
.env.example Dockerfile package.json README.md tsconfig.json
docs/73
api/17ai.md authentication.md automations.md blocks.md comments.md databases.md files.md forms.md openapi.yaml org-admin.md pages.md scim.md search.md sso.md versioning.md webhooks.md workspaces.md
architecture/10ai-architecture.md automation.md backend-architecture.md database-architecture.md frontend-architecture.md realtime-architecture.md search-architecture.md security-architecture.md storage.md system-architecture.md
database/2database-architecture.md redis-keys.md
development/8admin-guide.md coding-standards.md deployment.md development-guide.md files-pipeline.md perf-report.md performance.md testing.md
product/4functional-requirements.md non-functional-requirements.md product-requirements.md ux-requirements.md
requirements/1requirements-matrix.md
security/5audit-report.md files.md forms.md license-allowlist.md security-architecture.md
tasks/16phase-00-foundation.md phase-01-authentication.md phase-02-workspace.md phase-03-pages.md phase-04-editor.md phase-05-databases.md phase-06-relations.md phase-07-collaboration.md phase-08-search.md phase-09-files.md phase-10-forms.md phase-11-automation.md phase-12-ai.md phase-13-api.md phase-14-enterprise.md phase-15-production.md
ui/9ai.md automations.md design-system.md developers.md files.md forms.md org-admin.md scim.md sso.md
support-matrix.md
infrastructure/71
caddy/1Caddyfile
docker/6
init/101-extensions.sql
opensearch/1init-indices.json
.env.docker docker-compose.yml opensearch-init.sh README.md
kubernetes/40
observability/20
alertmanager/1alertmanager.yaml
grafana/8
dashboards/6ai.json api-overview.json infra.json realtime.json search.json workers-queues.json
provisioning/2
dashboards/1dashboards.yaml
datasources/1datasource.yaml
prometheus/3alerts.yaml recording-rules.yaml scrape.yaml
scripts/1sync-sources.mjs
alertmanager.yaml grafana.yaml kustomization.yaml namespace.yaml node-exporter.yaml otel-collector.yaml prometheus.yaml
postgres/7configmap.yaml cronjob-backup.yaml cronjob-opensearch-snapshot.yaml pgbouncer.yaml replica.yaml statefulset.yaml walg-iam-policy.yaml
api.yaml configmap.yaml ingress.yaml kustomization.yaml namespace.yaml networkpolicy.yaml pdb.yaml README.md realtime.yaml redis.yaml secret.example.yaml web.yaml worker.yaml
nginx/2nginx.conf README.md
observability/14
alertmanager/1alertmanager.yaml
grafana/8
dashboards/6ai.json api-overview.json infra.json realtime.json search.json workers-queues.json
provisioning/2
dashboards/1dashboards.yaml
datasources/1datasource.yaml
prometheus/3alerts.yaml recording-rules.yaml scrape.yaml
scripts/1dashboard-completeness.mjs
README.md
terraform/8compute.tf data.tf main.tf network.tf outputs.tf README.md security.tf variables.tf
packages/289
ai/24
src/20
providers/7anthropic.ts google.ts http.ts index.ts openai-compatible.ts rag.test.ts writing.test.ts
schemas/2database.ts generate.ts
adapters.test.ts config.test.ts config.ts gateway.test.ts gateway.ts index.ts tools.test.ts tools.ts types.ts usage.test.ts usage.ts
package.json README.md tsconfig.json vitest.config.ts
auth/11
src/7index.ts passwords.test.ts passwords.ts sessions.test.ts sessions.ts tokens.test.ts tokens.ts
package.json README.md tsconfig.json vitest.config.ts
collaboration/17
src/13awareness.ts codec.test.ts codec.ts index.ts lww.test.ts lww.ts mapping.ts offline.ts outbox.test.ts outbox.ts provider.test.ts provider.ts types.ts
package.json README.md tsconfig.json vitest.config.ts
config/7
src/4eslint.base.js index.ts prettier.base.json tsconfig.base.json
package.json README.md tsconfig.json
database-client/5
src/2index.test.ts index.ts
package.json README.md tsconfig.json
database-engine/33
src/29
filter/2filter.test.ts index.ts
formula/8evaluator.ts formula.test.ts functions.ts index.ts lexer.ts parser.ts types.ts validate.ts
query/6group.ts index.ts paginate.ts pivot.ts query.test.ts sort.ts
relations/2index.ts relations.test.ts
rollups/2index.ts rollups.test.ts
values/5dates.ts index.ts registry.ts types.ts values.test.ts
views/3buttons.ts index.ts views.test.ts
index.ts
package.json README.md tsconfig.json vitest.config.ts
editor/25
src/21
marks/1highlight.ts
nodes/2equation.ts mention.ts
autosave.test.ts autosave.ts clipboard.test.ts clipboard.ts codec.test.ts codec.ts context-menu.test.ts context-menu.ts diff.test.ts diff.ts factory.ts history.test.ts history.ts index.ts positions.ts slash.test.ts slash.ts types.ts
package.json README.md tsconfig.json vitest.config.ts
observability/20
src/16
__tests__/8db-redis-metrics.test.ts metrics.test.ts otel.test.ts pino-otel.test.ts prometheus.test.ts resource.test.ts scrubber.test.ts traceparent.test.ts
index.ts metrics.ts otel.ts pino-otel.ts prometheus.ts resource.ts scrubber.ts traceparent.ts
package.json README.md tsconfig.json vitest.config.ts
permissions/11
src/7index.ts levels.test.ts levels.ts resolve.test.ts resolve.ts rules.test.ts rules.ts
package.json README.md tsconfig.json vitest.config.ts
search/22
src/18chunking.test.ts chunking.ts client.test.ts client.ts embeddings.test.ts embeddings.ts hybrid.test.ts hybrid.ts index.ts indexer.test.ts indexer.ts mapping.test.ts mapping.ts queries.test.ts queries.ts rrf.test.ts rrf.ts types.ts
package.json README.md tsconfig.json vitest.config.ts
shared/61
src/57
dto/17ai.dto.ts auth.dto.test.ts auth.dto.ts block.dto.ts browser-safety.test.ts comment.dto.ts database.dto.test.ts database.dto.ts file.dto.ts formula-ast.ts index.ts notification.dto.ts page.dto.ts search.dto.ts share.dto.ts webhook.dto.ts workspace.dto.ts
events/8audit-catalog.test.ts audit-catalog.ts audit-csv.test.ts audit-csv.ts audit-scope.test.ts audit-scope.ts catalog.test.ts catalog.ts
realtime/6comment-events.ts frames.test.ts frames.ts notification-policy.ts notification-prefs.ts task-0094.test.ts
automation.test.ts automation.ts constants.ts crypto.test.ts crypto.ts env.ts errors.ts events.test.ts events.ts forms.test.ts forms.ts index.ts markdown.test.ts markdown.ts mentions.test.ts mentions.ts net.test.ts net.ts positions.test.ts positions.ts presence.test.ts presence.ts security-headers.ts webhook-delivery.test.ts webhook-delivery.ts webhook-events.ts
package.json README.md tsconfig.json vitest.config.ts
storage/16
src/12client.test.ts client.ts index.ts keys.test.ts keys.ts mime.test.ts mime.ts presign.test.ts presign.ts thumbnails.test.ts thumbnails.ts types.ts
package.json README.md tsconfig.json vitest.config.ts
ui/37
src/33
components/19avatar.tsx badge.tsx button.tsx checkbox.tsx command.tsx context-menu.tsx dialog.tsx dropdown-menu.tsx empty-state.tsx index.ts popover.tsx separator.tsx skeleton.tsx slider.tsx spinner.tsx switch.tsx tabs.tsx toast.tsx tooltip.tsx
color.ts command-filter.test.ts command-filter.ts focus.test.ts focus.ts formatters.test.ts formatters.ts index.ts theme.test.ts theme.ts toast-store.test.ts toast-store.ts tokens.test.ts tokens.ts
package.json README.md tsconfig.json vitest.config.ts
prisma/16
migrations/13
20260927000000_init/1migration.sql
20260928000000_search_documents_content_hash/1migration.sql
20260929000000_workspace_usage_storage_bytes/1migration.sql
20260930000000_form_submitted_notification_type/1migration.sql
20261001000000_ai_phase12/1migration.sql
20261002000000_webhooks_secret_encrypted_dlq/1migration.sql
20261002111436_webhook_delivery_latency/1migration.sql
20261002120000_org_admin_2fa_retention_unique/1migration.sql
20261002215124_audit_export/1migration.sql
20261003210000_perf_indexes_hot_paths/1migration.sql
20261004004500_perf_indexes_hot_paths_2/1migration.sql
migration_lock.toml README.md
schema.prisma seed.ts verify.ts
scripts/7backup-health.ts docs-completeness.mjs generate-openapi.mjs minio-init.ts opensearch-reindex.ts storage-orphans.ts verify-clean-clone.sh
tests/169
e2e/63
.auth/1user.json
fixtures/7
files/6clip.mp4 clip.wav clip.webm hero-1400.png note.docx note.txt
realtime.ts
mocks/2oidc-issuer.ts saml-idp.ts
playwright-report/3
data/2a6557bae9c2714a658968d4eb8534195c08ebda8.png e3092d07d00a16063e46385c02a559a9054fa847.webm
index.html
reports/1files-security.json
specs/34ai-database.spec.ts ai-generate.spec.ts ai-rag.spec.ts ai-security.spec.ts ai-writing.spec.ts ai.spec.ts api-keys.spec.ts appearance-theme.spec.ts auth.spec.ts automation.spec.ts collab-two-tab.spec.ts comments.spec.ts databases.spec.ts default-workspace-alias.spec.ts developers.spec.ts editor.spec.ts files.spec.ts final-flow.spec.ts forms.spec.ts oauth.spec.ts org-admin.spec.ts pages.spec.ts performance.spec.ts presence-cursors.spec.ts qa-paste-toggle.spec.ts realtime.spec.ts scim.spec.ts search-index-lag.spec.ts search.spec.ts sharing.spec.ts sso.spec.ts task-0094-comments-notifications.spec.ts webhooks.spec.ts workspace.spec.ts
test-results-final-flow/3
final-flow-Release-v1-0-fi-6c06b-w-state-carries-sequential--chromium-desktop/2test-failed-1.png trace.zip
.last-run.json
utils/4api.ts dnd.ts search-lag.ts wait.ts
.gitignore fixtures.ts global-setup.ts package.json playwright.config.ts playwright.final-flow.config.ts README.md tsconfig.json
fixtures/1
files/1make-sparse.ts
integration/60
ai-capability/2ai-app.capability.test.ts gateway.capability.test.ts
drill/2seed-drill.mjs verify-restore.mjs
fixtures/5
files/5clip.mp4 clip.wav doc.pdf hero-1400.png note.txt
helpers/3api.ts containers.ts seed.ts
realtime/11control-channel.test.ts convergence.test.ts flush-rebase-http.test.ts handshake.test.ts heartbeat-limits.test.ts lifecycle.test.ts presence.test.ts rebase.test.ts resume.test.ts rt-helpers.ts task-0094-fanout.test.ts
ai-chat.integration.test.ts ai-database.integration.test.ts ai-generate.integration.test.ts ai-rag.integration.test.ts ai-writing.integration.test.ts api-keys.integration.test.ts audit-completeness.integration.test.ts audit-export-s3.integration.test.ts auth.integration.test.ts automation-pipeline.integration.test.ts automations.integration.test.ts blocks.integration.test.ts cache-invalidation.integration.test.ts comments.integration.test.ts databases.integration.test.ts files-lifecycle.integration.test.ts files-pipeline.integration.test.ts files-security.integration.test.ts files.integration.test.ts forms-responses.integration.test.ts forms.integration.test.ts oauth.integration.test.ts openapi.contract.test.ts org-admin.integration.test.ts package.json pages.integration.test.ts permissions.integration.test.ts README.md replica-routing.integration.test.ts retention-engine.integration.test.ts scim.integration.test.ts search.integration.test.ts sso.integration.test.ts tsconfig.json vitest.config.ts webhooks.integration.test.ts workspace.integration.test.ts
performance/28
fixtures/1search-100k.ts
helpers/2seed-100k.mjs seed-1k-load.mjs
reports/7api-load-smoke.json database-load-smoke.json editor-local.json realtime-load.json search-full.json search-last.json search-reduced.json
api-load.js api.js archive.tar database-load.js database.js editor-local.mjs extract-100k-env.mjs package.json README.md realtime-1k.js realtime-load.js realtime.js run-realtime-load.sh search-search.k6.js search-summary.json seed-100k.mjs threshold-breach-demo.js verify-zero-op-loss.mjs
security/16
helpers/1app.ts
audit-report.test.ts auth-paths.test.ts deps.test.ts formula-fuzz.test.ts gitleaks-config.test.ts headers.test.ts images.test.ts oracle.ts package.json permission-fuzz.test.ts rate-limits.test.ts README.md tsconfig.json upload-abuse.test.ts vitest.config.ts
README.md
.dockerignore .env .env.bak-task0125 .env.example .eslintrc.json .gitignore .npmrc .prettierignore .prettierrc.json AGENTS.md ARCHITECTURE.md CHANGELOG.md CONTRIBUTING.md gitleaks.toml LICENSE package.json pnpm-lock.yaml pnpm-workspace.yaml PRODUCT-SPEC.md README.md ROADMAP.md SECURITY.md tsconfig.json turbo.json
webhooks.integration.test.tstests/integration/webhooks.integration.test.tsraw← integration46,806 B← Back to the app
1/**
2 * AI Software Factory — Integration: Webhooks production (TASK-0130).
3 *
4 * The full outbound-delivery contract through the REAL API + in-process
5 * worker (same source-import convention as automation-pipeline):
6 *
7 *   1.  delivery happy path — create page → `page.created` fan-out →
8 *       DELIVERED; envelope + ALL documented headers; in-test HMAC
9 *       recompute `v1 == HMAC-SHA256(secret, "<t>.<rawBody>")`;
10 *   2.  secret storage — `secretEncrypted` = AES-GCM ciphertext (NOT
11 *       plaintext), `secretHash` = sha256, list/get expose no raw secret
12 *       (schema test), 409 at the 20-per-workspace cap;
13 *   3.  4xx target → REJECTED (terminal, no retry);
14 *   4.  5xx target → FAILED ×8 → DEAD_LETTER + admin notification (24h
15 *       throttled); nextRetryAt gaps match `min(30s·2^(a-1),15min)` ±20%
16 *       (env-shortened: 300ms base / 2400ms cap);
17 *   5.  resubmit `all_dead` → new rows linked by originDeliveryId,
18 *       attempts reset, re-delivered;
19 *   6.  delivery retry endpoint — FAILED → PENDING → re-delivered;
20 *       DELIVERED → 409;
21 *   7.  SSRF — private/loopback delivery URLs rejected (400), allowed with
22 *       WEBHOOK_ALLOW_PRIVATE=true;
23 *   8.  test send — synchronous `webhook.test` delivery (no retry queue);
24 *   9.  secret rotation (PATCH rotateSecret) — new secret signs, old
25 *       signature invalid;
26 *  10.  catalog completeness — EVERY catalog event fires on its operation
27 *       (or the canonical stream entry) and is delivered + signed;
28 *  11.  incoming webhooks — valid → 202 + run row; bad sig → 401
29 *       signature_invalid; 6-min-old ts → 401 timestamp_expired;
30 *       replay → 401 replay_detected.
31 *
32 * Backoff env is shortened for this suite (WEBHOOK_BACKOFF_BASE_MS=300,
33 * MAX=2400) so the 8-attempt run completes in ~6 s.
34 */
35import { afterAll, beforeAll, describe, expect, it } from "vitest";
36import * as http from "node:http";
37import { createHash, createHmac } from "node:crypto";
38import { PrismaClient } from "@prisma/client";
39import { Redis } from "ioredis";
40import { WEBHOOK_EVENT_CATALOG, WEBHOOK_EVENT_NAMES, type WebhookCatalogEvent } from "@factory/shared";
41import { getDatabaseUrl, getRedisUrl } from "./helpers/containers";
42import { deploySchema } from "./helpers/api";
43import { createTestApp, type TestAppHandle } from "./helpers/api";
44import { authHeaders, createWorkspace, createUser } from "./helpers/seed";
45 
46// ── worker source (no build on the monorepo test path) ─────────────────────
47import { pathToFileURL } from "node:url";
48import path from "node:path";
49import { fileURLToPath } from "node:url";
50const HERE = path.dirname(fileURLToPath(import.meta.url));
51const WORKER_SRC = path.resolve(HERE, "..", "..", "apps", "worker", "src");
52function wurl(rel: string): string {
53  return pathToFileURL(path.join(WORKER_SRC, rel)).href;
54}
55const { createWebhooksWorker, createWebhooksConsumer } = await import(
56  /* webpackIgnore: true */ wurl("processors/webhooks.processor.ts")
57);
58 
59// ── infra gate ─────────────────────────────────────────────────────────────
60let dbUrl = "";
61let redisUrl = "";
62let infraReady = false;
63try {
64  [dbUrl, redisUrl] = await Promise.all([getDatabaseUrl(), getRedisUrl()]);
65  infraReady = true;
66} catch {
67  infraReady = false; // no Docker daemon
68}
69 
70describe.runIf(infraReady)("Webhooks (TASK-0130)", () => {
71  let app: TestAppHandle;
72  let prisma: PrismaClient;
73  let redis: Redis;
74  let env: import("../../apps/worker/src/config").WorkerEnv;
75  const startedAt = new Date();
76 
77  function baseEnv() {
78    return {
79      NODE_ENV: "test",
80      REDIS_URL: redisUrl,
81      DATABASE_URL: dbUrl,
82      S3_ENDPOINT: "http://localhost:9000",
83      S3_REGION: "us-east-1",
84      S3_BUCKET_FILES: "factory-files",
85      S3_BUCKET_THUMBS: "factory-thumbs",
86      S3_BUCKET: "factory-files",
87      S3_ACCESS_KEY: "minioadmin",
88      S3_SECRET_KEY: "minioadmin",
89      S3_PUBLIC_BASE: "http://localhost:9001",
90      SEARCH_URL: "http://localhost:9200",
91      SEARCH_USERNAME: "admin",
92      SEARCH_PASSWORD: "changeme",
93      SEARCH_VECTOR_DIMENSION: 1536,
94      SEARCH_KNN_M: 16,
95      SEARCH_KNN_EF_CONSTRUCTION: 100,
96      MASTER_ENCRYPTION_KEY: process.env.MASTER_ENCRYPTION_KEY ?? "",
97      WEBHOOK_ALLOW_PRIVATE: "true",
98      // Short backoff: 300ms base ×2 → 2.4s cap (tests the SCHEDULE math;
99      // the production 30s/15min defaults live in worker config + shared
100      // unit tests).
101      WEBHOOK_BACKOFF_BASE_MS: "300",
102      WEBHOOK_BACKOFF_MAX_MS: "2400",
103      AI_API_KEY: "",
104      AI_PROVIDER: "openai",
105      AI_BASE_URL: "",
106      AI_MODEL: "gpt-4o-mini",
107      AI_EMBEDDING_PROVIDER: "",
108      AI_EMBEDDING_MODEL: "text-embedding-3-small",
109      SMTP_HOST: "", // no mail — the Notification row is the assertion
110      SMTP_PORT: 587,
111      SMTP_USER: "",
112      SMTP_PASS: "",
113      SMTP_FROM: "noreply@factory.local",
114      SMTP_STARTTLS: true,
115      CLAMAV_HOST: "",
116      CLAMAV_PORT: 3310,
117      CLAMD_SOCKET: "",
118      FILES_WORKER_CONCURRENCY: 4,
119      CLAMAV_TIMEOUT_MS: 30_000,
120      FILES_TEMP_DIR: "",
121      FILES_TEMP_MAX_AGE_MS: 600_000,
122      FILES_TEMP_FREE_MIN_BYTES: 1,
123      PUBLIC_URL: "http://localhost:3000",
124      WORKER_CONCURRENCY: 10,
125      WORKER_ID: "webhooks-it",
126      WORKER_RETENTION_EXECUTE: "false",
127      HEALTH_PORT: 4003,
128      AUTO_RUN_MAX_RETRIES: 8,
129      AUTOMATIONS_WORKER_CONCURRENCY: 1
130    } as import("../../apps/worker/src/config").WorkerEnv;
131  }
132 
133  beforeAll(async () => {
134    process.env.WEBHOOK_ALLOW_PRIVATE = "true"; // the API's create() SSRF guard
135    app = await createTestApp();
136    env = baseEnv();
137    prisma = new PrismaClient({ datasources: { db: { url: dbUrl } } });
138    await prisma.$connect();
139    redis = new Redis(redisUrl, { maxRetriesPerRequest: null });
140  }, 180_000);
141 
142  afterAll(async () => {
143    // Wipe the webhooks queue (delayed retry jobs parked with real delays).
144    try {
145      const { Queue } = await import("bullmq");
146      const q = new Queue("webhooks", { connection: redis });
147      await q.obliterate({ force: true });
148      await q.close();
149    } catch {
150      /* best-effort */
151    }
152    await redis?.quit().catch(() => {});
153    await prisma?.$disconnect().catch(() => {});
154    await app?.close();
155  });
156 
157  // ── fixtures ────────────────────────────────────────────────────────────
158 
159  interface Captured {
160    rawBody: Buffer;
161    headers: http.IncomingHttpHeaders;
162    /** Epoch ms the receiver captured the request (backoff-schedule proof). */
163    at: number;
164  }
165  function startReceiver(statusCode: number, requests?: Captured[]): Promise<{ url: string; close: () => Promise<void> }> {
166    const list = requests ?? [];
167    const server = http.createServer((req, res) => {
168      const chunks: Buffer[] = [];
169      req.on("data", (c) => chunks.push(c));
170      req.on("end", () => {
171        list.push({ rawBody: Buffer.concat(chunks), headers: req.headers, at: Date.now() });
172        res.writeHead(statusCode, { "Content-Type": "application/json" });
173        res.end("{}");
174      });
175    });
176    return new Promise((resolve) => {
177      server.listen(0, "127.0.0.1", () => {
178        const addr = server.address();
179        const port = typeof addr === "object" && addr ? addr.port : 0;
180        resolve({
181          url: `http://127.0.0.1:${port}/hook`,
182          close: () =>
183            new Promise<void>((r) => {
184              server.closeAllConnections?.();
185              server.close(() => r());
186            })
187        });
188      });
189    });
190  }
191 
192  interface Hook {
193    headers: Record<string, string>;
194    workspaceId: string;
195    webhookId: string;
196    secret: string;
197  }
198  async function makeHook(events: string[], url: string, opts?: { secret?: string; active?: boolean; eventsWildcard?: boolean }): Promise<Hook> {
199    const user = await createUser(app.request, { name: `Hook ${Math.random().toString(36).slice(2, 6)}` });
200    const { workspaceId } = await createWorkspace(app.request, authHeaders(user.accessToken!), `WS ${Math.random().toString(36).slice(2, 6)}`);
201    const res = await app.request("POST", `/api/v1/workspaces/${workspaceId}/webhooks`, {
202      headers: authHeaders(user.accessToken!),
203      body: {
204        url,
205        events: opts?.eventsWildcard ? ["*"] : events,
206        ...(opts?.secret ? { secret: opts.secret } : {}),
207        ...(opts?.active !== undefined ? { active: opts.active } : {})
208      }
209    });
210    expect(res.status).toBe(201);
211    const body = await res.json();
212    const hook = body.data ?? body;
213    return {
214      headers: authHeaders(user.accessToken!),
215      workspaceId,
216      webhookId: hook.id,
217      secret: opts?.secret ?? hook.secret
218    };
219  }
220 
221  /** The worker: in-process consumer (stream) + BullMQ worker (queue). */
222  async function withWorker<T>(fn: () => Promise<T>): Promise<T> {
223    const consumerRedis = new Redis(redisUrl, { maxRetriesPerRequest: null });
224    const workerRedis = new Redis(redisUrl, { maxRetriesPerRequest: null });
225    const consumer = createWebhooksConsumer({ redis: consumerRedis, prisma, consumerId: `it-${Date.now()}` });
226    const worker = createWebhooksWorker({ connection: workerRedis, prisma, env, concurrency: 2 });
227    try {
228      await consumer.start();
229      await worker.waitUntilReady();
230      return await fn();
231    } finally {
232      await worker.close().catch(() => {});
233      await consumer.stop().catch(() => {});
234      await consumerRedis.quit().catch(() => {});
235      await workerRedis.quit().catch(() => {});
236    }
237  }
238 
239  async function waitFor<T>(
240    probe: () => Promise<T | null | undefined>,
241    pred: (v: T) => boolean,
242    timeoutMs: number,
243    what: string
244  ): Promise<T> {
245    const deadline = Date.now() + timeoutMs;
246    for (;;) {
247      const v = await probe();
248      if (v !== null && v !== undefined && pred(v as T)) return v as T;
249      if (Date.now() > deadline) throw new Error(`timed out waiting for ${what}`);
250      await new Promise((r) => setTimeout(r, 250));
251    }
252  }
253 
254  /** All deliveries of a webhook newer than `since`. */
255  async function deliveriesOf(webhookId: string, since?: Date) {
256    return app.prisma.webhookDelivery.findMany({
257      where: { webhookId, ...(since ? { createdAt: { gt: since } } : {}) },
258      orderBy: { createdAt: "asc" }
259    });
260  }
261 
262  /** Recompute + verify the signature of a captured request. */
263  function expectSigned(req: Captured, secret: string, event: string) {
264    const header = req.headers["x-factory-signature"];
265    expect(typeof header, "X-Factory-Signature header present").toBe("string");
266    const m = String(header).match(/^t=(\d+),v1=([0-9a-f]{64})$/);
267    expect(m, `canonical header shape (got ${header})`).not.toBeNull();
268    const t = Number(m![1]);
269    const v1 = m![2];
270    const expected = createHmac("sha256", secret).update(`${t}.` + req.rawBody.toString("utf8")).digest("hex");
271    expect(v1, "v1 == HMAC-SHA256(secret, `<t>.<rawBody>`)").toBe(expected);
272    expect(req.headers["x-factory-event"]).toBe(event);
273    expect(String(req.headers["user-agent"])).toContain("factory-webhooks/1.0");
274  }
275 
276  // ── 1. delivery happy path ───────────────────────────────────────────────
277 
278  it("page.created → DELIVERED: envelope, headers, in-test HMAC", async () => {
279    const reqs: Captured[] = [];
280    const receiver = await startReceiver(200, reqs);
281    try {
282      await withWorker(async () => {
283        const hook = await makeHook(["page.created"], receiver.url, { secret: "whsec_it_happy_0123456789abcdef" });
284        const pg = await app.request("POST", `/api/v1/workspaces/${hook.workspaceId}/pages`, {
285          headers: hook.headers,
286          body: { title: "Hook Trigger Page" }
287        });
288        expect(pg.status).toBe(201);
289        const pageId = ((await pg.json()) as { id: string }).id;
290 
291        const d = await waitFor(
292          () => app.prisma.webhookDelivery.findFirst({ where: { webhookId: hook.webhookId }, orderBy: { createdAt: "desc" } }),
293          (row) => row.status === "DELIVERED",
294          30_000,
295          "DELIVERED row"
296        );
297        expect(d.attempts).toBe(1);
298        expect(d.statusCode).toBe(200);
299        expect(d.latencyMs).toBeGreaterThanOrEqual(0);
300 
301        // The receiver got the envelope.
302        await waitFor(() => (reqs.length ? reqs : null), (r) => r.length >= 1, 10_000, "captured request");
303        const cap = reqs[0]!;
304        const envBody = JSON.parse(cap.rawBody.toString("utf8")) as Record<string, unknown>;
305        expect(envBody.type).toBe("page.created");
306        expect(envBody.id).toMatch(/^evt_/);
307        expect(envBody.workspaceId).toBe(hook.workspaceId);
308        expect(typeof envBody.occurredAt).toBe("string");
309        const ws = envBody.workspace as Record<string, unknown>;
310        expect(ws.id).toBe(hook.workspaceId);
311        const data = envBody.data as Record<string, unknown>;
312        expect(data.pageId).toBe(pageId);
313        expect(data.title).toBe("Hook Trigger Page");
314 
315        // Signature + headers.
316        expectSigned(cap, "whsec_it_happy_0123456789abcdef", "page.created");
317        expect(cap.headers["x-factory-event-id"]).toBe(envBody.id);
318        expect(cap.headers["x-factory-delivery-id"]).toBe(d.id);
319        expect(cap.headers["x-factory-attempt"]).toBe("1");
320 
321        // The stored payload matches the wire bytes (minus re-serialization).
322        const stored = JSON.parse(JSON.stringify(d.payload)) as Record<string, unknown>;
323        expect(stored.id).toBe(envBody.id);
324        expect(stored.data).toEqual(envBody.data);
325      });
326    } finally {
327      await receiver.close();
328    }
329  }, 90_000);
330 
331  // ── 2. secret storage + schema (no raw secret on the wire) ──────────────
332 
333  it("secret: AES-GCM at rest, sha256 bookkeeping, list/get expose prefix only, 409 at 20", async () => {
334    const receiver = await startReceiver(200);
335    try {
336      const secret = "whsec_storage_test_00000000000000";
337      const hook = await makeHook(["*"], receiver.url, { secret });
338      const row = await app.prisma.webhook.findUniqueOrThrow({ where: { id: hook.webhookId } });
339 
340      // At rest: ciphertext (never plaintext), sha256 bookkeeping.
341      expect(row.secretEncrypted).toBeInstanceOf(Uint8Array);
342      expect(row.secretEncrypted!.length).toBeGreaterThan(27); // iv+tag+ct
343      expect(Buffer.from(row.secretEncrypted).toString("utf8")).not.toContain(secret);
344      expect(row.secretHash).toBe(createHash("sha256").update(secret).digest("hex"));
345      expect(row.secretHash).not.toContain(secret);
346 
347      // Wire: list + get never leak the raw secret (schema test).
348      const listRes = await app.request("GET", `/api/v1/workspaces/${hook.workspaceId}/webhooks`, { headers: hook.headers });
349      expect(listRes.status).toBe(200);
350      const listBody = await listRes.json();
351      const listText = JSON.stringify(listBody);
352      expect(listText).not.toContain(secret);
353      const items = (listBody.data ?? listBody) as Array<Record<string, unknown>>;
354      const me = items.find((i) => i.id === hook.webhookId)!;
355      expect(me.secretPrefix).toMatch(/^whsec_/);
356      expect(me).not.toHaveProperty("secret");
357      expect(me).not.toHaveProperty("secretEncrypted");
358      expect(me).not.toHaveProperty("secretHash");
359 
360      const getRes = await app.request("GET", `/api/v1/workspaces/${hook.workspaceId}/webhooks/${hook.webhookId}`, {
361        headers: hook.headers
362      });
363      const gotBody = await getRes.json();
364      const got = gotBody.data ?? gotBody;
365      expect(JSON.stringify(got)).not.toContain(secret);
366      expect(got).not.toHaveProperty("secret");
367      expect(got.secretPrefix).toBe(me.secretPrefix);
368 
369      // 409 at the 20-per-workspace cap.
370      for (let i = 0; i < 19; i++) {
371        const r = await app.request("POST", `/api/v1/workspaces/${hook.workspaceId}/webhooks`, {
372          headers: hook.headers,
373          body: { url: receiver.url, events: ["*"] }
374        });
375        expect(r.status).toBe(201);
376      }
377      const over = await app.request("POST", `/api/v1/workspaces/${hook.workspaceId}/webhooks`, {
378        headers: hook.headers,
379        body: { url: receiver.url, events: ["*"] }
380      });
381      expect(over.status).toBe(409);
382      const overBody = await over.json();
383      expect((overBody.error as { code: string }).code).toBe("CONFLICT");
384    } finally {
385      await receiver.close();
386    }
387  }, 120_000);
388 
389  // ── 3. 4xx → REJECTED (terminal) ─────────────────────────────────────────
390 
391  it("4xx target → REJECTED, no retry", async () => {
392    const reqs: Captured[] = [];
393    const receiver = await startReceiver(422, reqs);
394    try {
395      await withWorker(async () => {
396        const hook = await makeHook(["page.created"], receiver.url, { secret: "whsec_reject_0000000000000000" });
397        const t0 = new Date();
398        await app.request("POST", `/api/v1/workspaces/${hook.workspaceId}/pages`, {
399          headers: hook.headers,
400          body: { title: "422 page" }
401        });
402        const d = await waitFor(
403          () => app.prisma.webhookDelivery.findFirst({ where: { webhookId: hook.webhookId }, orderBy: { createdAt: "desc" } }),
404          (row) => row.status === "REJECTED",
405          30_000,
406          "REJECTED row"
407        );
408        expect(d.attempts).toBe(1);
409        expect(d.statusCode).toBe(422);
410        expect(String(d.error)).toContain("422");
411        expect(d.nextRetryAt).toBeNull();
412        // Exactly ONE attempt — a 4xx is terminal (the endpoint's contract bug).
413        await new Promise((r) => setTimeout(r, 2_000));
414        expect(reqs.length).toBe(1);
415        expect((await deliveriesOf(hook.webhookId, t0)).length).toBe(1);
416        expectSigned(reqs[0]!, "whsec_reject_0000000000000000", "page.created");
417      });
418    } finally {
419      await receiver.close();
420    }
421  }, 90_000);
422 
423  // ── 4. 5xx ×8 → DEAD_LETTER + notification + backoff gaps ───────────────
424 
425  it("5xx ×8 → DEAD_LETTER + admin notification; nextRetryAt gaps match backoff ±20%", async () => {
426    const reqs: Captured[] = [];
427    const receiver = await startReceiver(500, reqs);
428    try {
429      await withWorker(async () => {
430        const hook = await makeHook(["page.created"], receiver.url, { secret: "whsec_dlq_00000000000000000000" });
431        const t0 = new Date();
432        await app.request("POST", `/api/v1/workspaces/${hook.workspaceId}/pages`, {
433          headers: hook.headers,
434          body: { title: "DLQ page" }
435        });
436        const d = await waitFor(
437          () => app.prisma.webhookDelivery.findFirst({ where: { webhookId: hook.webhookId }, orderBy: { createdAt: "desc" } }),
438          (row) => row.status === "DEAD_LETTER",
439          60_000,
440          "DEAD_LETTER row"
441        );
442        expect(d.attempts).toBe(8);
443        expect(d.statusCode).toBe(500);
444        expect(d.nextRetryAt).toBeNull();
445        // One row per logical delivery (attempts increment 1→8); the retries
446        // are PROVEN visible by the 8 captured attempts + their timing.
447        expect(reqs.length).toBe(8);
448 
449        // Admin notification (in-app row; no SMTP in the test env) — the
450        // workspace owner gets exactly one (24h throttle).
451        const note = await waitFor(
452          () =>
453            app.prisma.notification.findFirst({
454              where: { workspaceId: hook.workspaceId, title: "Webhook delivery failed" },
455              orderBy: { createdAt: "desc" }
456            }),
457          (n) => n !== null,
458          10_000,
459          "admin notification"
460        );
461        expect(note.type).toBe("INTEGRATION");
462        expect(String(note.body)).toContain(hook.webhookId);
463 
464        // The backoff SCHEDULE, proven from the 8 captured attempts: the gap
465        // between attempt a and a+1 is ≈ base·2^(a-1) (±20% jitter + a few
466        // seconds of BullMQ/queue/DB slack). ≥3 retries are visibly spaced.
467        const base = 300;
468        const cap = 2400;
469        for (let a = 1; a < reqs.length; a++) {
470          const gap = reqs[a]!.at - reqs[a - 1]!.at;
471          const expected = Math.min(base * 2 ** (a - 1), cap);
472          // Lower bound: never faster than 60% of the nominal delay (jitter
473          // floor −20% − slack). Upper bound: nominal + jitter + queue slack.
474          expect(gap, `gap after attempt ${a}`).toBeGreaterThanOrEqual(expected * 0.6 - 500);
475          expect(gap, `gap after attempt ${a}`).toBeLessThanOrEqual(expected * 1.2 + 5000);
476        }
477        // The final row is terminal; earlier FAILED states are subsumed into
478        // the single row's attempt counter (worker-owned retry, C-13.8d).
479        const rows = (await deliveriesOf(hook.webhookId, t0)).filter((r) => r.status === "FAILED");
480        for (const row of rows) {
481          expect(row.nextRetryAt).toBeNull(); // only the live row carries it
482        }
483      });
484    } finally {
485      await receiver.close();
486    }
487  }, 120_000);
488 
489  // ── 5. resubmit all_dead ─────────────────────────────────────────────────
490 
491  it("resubmit all_dead → new rows (originDeliveryId), attempts reset, re-delivered", async () => {
492    const reqs: Captured[] = [];
493    const receiver = await startReceiver(200, reqs);
494    try {
495      await withWorker(async () => {
496        // A dead-lettered webhook (from a previous failing receiver is gone —
497        // craft the state directly: webhook + a DEAD_LETTER delivery row).
498        const user = await createUser(app.request, { name: "Resubmit owner" });
499        const { workspaceId } = await createWorkspace(app.request, authHeaders(user.accessToken!), "Resubmit WS");
500        const hookRes = await app.request("POST", `/api/v1/workspaces/${workspaceId}/webhooks`, {
501          headers: authHeaders(user.accessToken!),
502          body: { url: receiver.url, events: ["*"], secret: "whsec_resubmit_000000000000" }
503        });
504        const hookBody = await hookRes.json();
505        const hook = (hookBody.data ?? hookBody) as { id: string };
506        const webhookId = hook.id;
507 
508        const deadId = `dlv_dead_${Math.random().toString(36).slice(2, 10)}`;
509        const envelope = {
510          id: `evt_${Math.random().toString(36).slice(2, 10)}`,
511          type: "page.created",
512          workspaceId,
513          occurredAt: new Date().toISOString(),
514          data: { pageId: "pg_resubmit", title: "resubmit me" }
515        };
516        await app.prisma.webhookDelivery.create({
517          data: { id: deadId, webhookId, event: "page.created", payload: envelope as object, status: "DEAD_LETTER", attempts: 8, statusCode: 503, error: "non-2xx: HTTP 503" }
518        });
519 
520        const res = await app.request("POST", `/api/v1/workspaces/${workspaceId}/webhooks/${webhookId}/resubmit`, {
521          headers: authHeaders(user.accessToken!),
522          body: { all_dead: true }
523        });
524        expect(res.status).toBe(200);
525        const body = await res.json();
526        const out = body.data ?? body;
527        expect(out.resubmitted).toBe(1);
528        const newId = out.deliveryIds[0] as string;
529        expect(out.originDeliveryIds[0]).toBe(deadId);
530 
531        const newRow = await waitFor(
532          () => app.prisma.webhookDelivery.findUnique({ where: { id: newId } }),
533          (row) => row.status === "DELIVERED",
534          30_000,
535          "resubmitted delivery"
536        );
537        expect(newRow.originDeliveryId).toBe(deadId);
538        expect(newRow.attempts).toBe(1);
539        // The ORIGINAL stays put (the log keeps the history).
540        const original = await app.prisma.webhookDelivery.findUnique({ where: { id: deadId } });
541        expect(original.status).toBe("DEAD_LETTER");
542        expect(reqs.length).toBe(1);
543        expectSigned(reqs[0]!, "whsec_resubmit_000000000000", "page.created");
544        // Retry of an already-DELIVERED row → 409.
545        const dup = await app.request("POST", `/api/v1/workspaces/${workspaceId}/webhooks/${webhookId}/deliveries/${newId}/retry`, {
546          headers: authHeaders(user.accessToken!)
547        });
548        expect(dup.status).toBe(409);
549      });
550    } finally {
551      await receiver.close();
552    }
553  }, 90_000);
554 
555  // ── 6. per-delivery retry ─────────────────────────────────────────────────
556 
557  it("delivery retry: FAILED → PENDING → re-delivered (attempts reset)", async () => {
558    const reqs: Captured[] = [];
559    const receiver = await startReceiver(500, reqs);
560    const okReceiver = await startReceiver(200, reqs);
561    try {
562      await withWorker(async () => {
563        const hook = await makeHook(["page.created"], receiver.url, { secret: "whsec_retry_0000000000000000" });
564        await app.request("POST", `/api/v1/workspaces/${hook.workspaceId}/pages`, {
565          headers: hook.headers,
566          body: { title: "retry page" }
567        });
568        const failed = await waitFor(
569          () => app.prisma.webhookDelivery.findFirst({ where: { webhookId: hook.webhookId }, orderBy: { createdAt: "desc" } }),
570          (row) => row.status === "FAILED",
571          30_000,
572          "FAILED row"
573        );
574        // Point the webhook at a healthy receiver, then retry.
575        const patch = await app.request("PATCH", `/api/v1/workspaces/${hook.workspaceId}/webhooks/${hook.webhookId}`, {
576          headers: hook.headers,
577          body: { url: okReceiver.url }
578        });
579        expect(patch.status).toBe(200);
580        const retry = await app.request(
581          "POST",
582          `/api/v1/workspaces/${hook.workspaceId}/webhooks/${hook.webhookId}/deliveries/${failed.id}/retry`,
583          { headers: hook.headers }
584        );
585        expect(retry.status).toBe(200);
586        const retried = await waitFor(
587          () => app.prisma.webhookDelivery.findUnique({ where: { id: failed.id } }),
588          (row) => row.status === "DELIVERED",
589          30_000,
590          "retried delivery"
591        );
592        // The suite runs a 300 ms backoff, so the worker's own auto-retry
593        // may race the manual one — the row is delivered either way; only
594        // the final attempt count is non-deterministic.
595        expect(retried.attempts).toBeGreaterThanOrEqual(1);
596        expect(reqs.length).toBeGreaterThanOrEqual(2); // 500 attempt + retry
597        expectSigned(reqs[reqs.length - 1]!, "whsec_retry_0000000000000000", "page.created");
598      });
599    } finally {
600      await receiver.close();
601      await okReceiver.close();
602    }
603  }, 90_000);
604 
605  // ── 7. SSRF ──────────────────────────────────────────────────────────────
606 
607  it("SSRF: private/loopback/non-http rejected; WEBHOOK_ALLOW_PRIVATE override works", async () => {
608    const user = await createUser(app.request, { name: "SSRF owner" });
609    const { workspaceId } = await createWorkspace(app.request, authHeaders(user.accessToken!), "SSRF WS");
610    const H = authHeaders(user.accessToken!);
611 
612    // Flip the override OFF (the suite runs with it ON for the receivers)
613    // and confirm every private/loopback/non-http target is rejected. The
614    // API's loadEnv is cached — reset it after flipping.
615    const { resetEnvCache } = await import("@factory/api/src/config/env");
616    process.env.WEBHOOK_ALLOW_PRIVATE = "false";
617    resetEnvCache();
618    for (const url of [
619      "http://127.0.0.1:1/dead",
620      "http://10.1.2.3/x",
621      "http://192.168.0.1/x",
622      "http://169.254.169.254/latest/meta-data",
623      "http://localhost/hook",
624      "ftp://example.com/hook",
625      "not-a-url"
626    ]) {
627      const res = await app.request("POST", `/api/v1/workspaces/${workspaceId}/webhooks`, {
628        headers: H,
629        body: { url, events: ["*"] }
630      });
631      expect(res.status, url).toBe(400);
632      const body = await res.json();
633      expect((body.error as { code: string }).code, url).toBe("VALIDATION_ERROR");
634    }
635 
636    // Restore the override: the SAME loopback url is now accepted (that is
637    // what every receiver-based scenario in this suite relies on).
638    process.env.WEBHOOK_ALLOW_PRIVATE = "true";
639    resetEnvCache();
640    const allowed = await app.request("POST", `/api/v1/workspaces/${workspaceId}/webhooks`, {
641      headers: H,
642      body: { url: "http://127.0.0.1:1/dead", events: ["*"] }
643    });
644    expect(allowed.status).toBe(201);
645  }, 60_000);
646 
647  // ── 8. test send ─────────────────────────────────────────────────────────
648 
649  it("test send: synchronous webhook.test delivery (no retry queue)", async () => {
650    const reqs: Captured[] = [];
651    const receiver = await startReceiver(200, reqs);
652    try {
653      const hook = await makeHook(["page.created"], receiver.url, { secret: "whsec_testsend_0000000000000000" });
654      const res = await app.request("POST", `/api/v1/workspaces/${hook.workspaceId}/webhooks/${hook.webhookId}/test`, {
655        headers: hook.headers
656      });
657      expect(res.status).toBe(200);
658      const body = await res.json();
659      const out = body.data ?? body;
660      expect(out.status).toBe("DELIVERED");
661      expect(out.statusCode).toBe(200);
662      expect(out.attempts).toBe(1);
663      expect(out.latencyMs).toBeGreaterThanOrEqual(0);
664      const cap = reqs[0]!;
665      const envBody = JSON.parse(cap.rawBody.toString("utf8")) as Record<string, unknown>;
666      expect(envBody.type).toBe("webhook.test");
667      expect(envBody.test).toBe(true);
668      expectSigned(cap, "whsec_testsend_0000000000000000", "webhook.test");
669      // The test delivery appears in the log (the inspector tab asserts it).
670      const logRes = await app.request("GET", `/api/v1/workspaces/${hook.workspaceId}/webhooks/${hook.webhookId}/deliveries?event=webhook.test`, {
671        headers: hook.headers
672      });
673      const logBody = await logRes.json();
674      const items = (logBody.data ?? []) as Array<Record<string, unknown>>;
675      expect(items.some((i) => i.id === out.deliveryId)).toBe(true);
676    } finally {
677      await receiver.close();
678    }
679  }, 60_000);
680 
681  // ── 9. rotation ───────────────────────────────────────────────────────────
682 
683  it("rotateSecret (PATCH): new secret signs deliveries, old signature invalid", async () => {
684    const reqs: Captured[] = [];
685    const receiver = await startReceiver(200, reqs);
686    try {
687      await withWorker(async () => {
688        const hook = await makeHook(["page.created"], receiver.url, { secret: "whsec_rotate_OLD_000000000000000000" });
689        const rot = await app.request("PATCH", `/api/v1/workspaces/${hook.workspaceId}/webhooks/${hook.webhookId}`, {
690          headers: hook.headers,
691          body: { rotateSecret: true }
692        });
693        expect(rot.status).toBe(200);
694        const rotBody = await rot.json();
695        const rotOut = rotBody.data ?? rotBody;
696        expect(typeof rotOut.secret).toBe("string");
697        expect(rotOut.secret.length).toBeGreaterThanOrEqual(40);
698        const newSecret = rotOut.secret as string;
699        expect(newSecret).not.toBe(hook.secret);
700        expect(String(rotOut.warning)).toContain("cannot be retrieved again");
701 
702        // The old secret no longer decrypts — the row now carries the new one.
703        const row = await app.prisma.webhook.findUniqueOrThrow({ where: { id: hook.webhookId } });
704        expect(row.secretHash).toBe(createHash("sha256").update(newSecret).digest("hex"));
705 
706        // The next delivery signs with the NEW secret.
707        await app.request("POST", `/api/v1/workspaces/${hook.workspaceId}/pages`, {
708          headers: hook.headers,
709          body: { title: "rotate page" }
710        });
711        await waitFor(
712          () => app.prisma.webhookDelivery.findFirst({ where: { webhookId: hook.webhookId }, orderBy: { createdAt: "desc" } }),
713          (r) => r.status === "DELIVERED",
714          30_000,
715          "post-rotation delivery"
716        );
717        expectSigned(reqs[0]!, newSecret, "page.created");
718        // The same bytes do NOT verify under the old secret.
719        const m = String(reqs[0]!.headers["x-factory-signature"])!.match(/^t=(\d+),v1=([0-9a-f]{64})$/)!;
720        const withOld = createHmac("sha256", "whsec_rotate_OLD_000000000000000000").update(`${m[1]}.` + reqs[0]!.rawBody.toString("utf8")).digest("hex");
721        expect(m[2]).not.toBe(withOld);
722      });
723    } finally {
724      await receiver.close();
725    }
726  }, 90_000);
727 
728  // ── 10. catalog completeness ─────────────────────────────────────────────
729 
730  it("catalog completeness: every catalog event fires on its operation and delivers signed", async () => {
731    const reqs: Captured[] = [];
732    const receiver = await startReceiver(200, reqs);
733    const t0 = new Date();
734    try {
735      await withWorker(async () => {
736        // The hook must live in the SAME workspace the operations fire in
737        // (the fan-out is workspace-scoped), so create the workspace first
738        // and the webhook inside it (not via makeHook's own workspace).
739        const user = await createUser(app.request, { name: "Catalog owner" });
740        const { workspaceId } = await createWorkspace(app.request, authHeaders(user.accessToken!), "Catalog WS");
741        const H = authHeaders(user.accessToken!);
742        const hookRes = await app.request("POST", `/api/v1/workspaces/${workspaceId}/webhooks`, {
743          headers: H,
744          body: { url: receiver.url, events: ["*"], secret: "whsec_catalog_000000000000000000" }
745        });
746        expect(hookRes.status).toBe(201);
747        // Create returns a BARE object ({…shape, secret, warning}) — no wrapper.
748        const hookBody = (await hookRes.json()) as { id: string; data?: { id: string } };
749        const hook = { webhookId: hookBody.data?.id ?? hookBody.id };
750        expect(hook.webhookId).toBeTruthy();
751 
752        const seen = new Set<string>();
753        const observe = (timeoutMs: number, what: string) =>
754          waitFor(
755            async () => {
756              const rows = await app.prisma.webhookDelivery.findMany({ where: { webhookId: hook.webhookId, createdAt: { gt: t0 } } });
757              rows.forEach((r) => seen.add(r.event));
758              return seen.has(what) ? true : null;
759            },
760            (v) => v === true,
761            timeoutMs,
762            `delivery of ${what}`
763          );
764 
765        // page.created / database.created — a page with a database fires both.
766        const pg = await app.request("POST", `/api/v1/workspaces/${workspaceId}/pages`, {
767          headers: H,
768          body: { title: "Catalog page", type: "database" }
769        });
770        expect(pg.status).toBe(201);
771        const page = (await pg.json()) as { id: string; databaseId?: string };
772        await observe(20_000, "page.created");
773        await observe(20_000, "database.created");
774        const dbId = page.databaseId as string;
775 
776        // page.updated (PATCH title) — record ops come next on the db.
777        await app.request("PATCH", `/api/v1/pages/${page.id}`, { headers: H, body: { title: "Catalog page v2" } });
778        await observe(20_000, "page.updated");
779 
780        // page.deleted — trash a fresh page (page.deleted fires on trash).
781        const pgDel = await app.request("POST", `/api/v1/workspaces/${workspaceId}/pages`, {
782          headers: H,
783          body: { title: "Catalog delete me" }
784        });
785        const delPage = (await pgDel.json()) as { id: string };
786        await app.request("DELETE", `/api/v1/pages/${delPage.id}`, { headers: H });
787        await observe(20_000, "page.deleted");
788 
789        // record.created / record.updated (db seeded with Title + Status;
790        // the title is a first-class field — the event fires on any change).
791        const rec = await app.request("POST", `/api/v1/databases/${dbId}/records`, {
792          headers: H,
793          body: { title: "Catalog record" }
794        });
795        expect([200, 201]).toContain(rec.status);
796        const recDto = (await rec.json()) as { id: string };
797        expect(recDto.id).toBeTruthy();
798        await observe(20_000, "record.created");
799        const up = await app.request("PATCH", `/api/v1/records/${recDto.id}`, {
800          headers: H,
801          body: { title: "Catalog record v2" }
802        });
803        expect(up.status).toBe(200);
804        await observe(20_000, "record.updated");
805 
806        // database.updated (rename).
807        await app.request("PATCH", `/api/v1/databases/${dbId}`, { headers: H, body: { name: "Catalog db v2" } });
808        await observe(20_000, "database.updated");
809 
810        // page.shared (link share).
811        await app.request("POST", `/api/v1/shares`, {
812          headers: H,
813          body: { workspaceId, targetType: "page", targetId: page.id, kind: "link", level: "VIEW" }
814        });
815        await observe(20_000, "page.shared");
816 
817        // comment.created (page comment).
818        await app.request("POST", `/api/v1/comments`, {
819          headers: H,
820          body: { targetType: "page", targetId: page.id, body: "Catalog comment" }
821        });
822        await observe(20_000, "comment.created");
823 
824        // api_key.created (workspace key).
825        await app.request("POST", `/api/v1/workspaces/${workspaceId}/api-keys`, {
826          headers: H,
827          body: { name: "Catalog key", scopes: ["READ"] }
828        });
829        await observe(20_000, "api_key.created");
830 
831        // form.submitted (public form on the db).
832        const formRes = await app.request("POST", `/api/v1/databases/${dbId}/forms`, {
833          headers: H,
834          body: {
835            name: "Catalog form",
836            slug: `catalog-${Math.random().toString(36).slice(2, 8)}`,
837            questions: [{ type: "SHORT_TEXT", label: "Feedback" }]
838          }
839        });
840        expect(formRes.status).toBe(201);
841        const form = (await formRes.json()) as { id: string; slug: string };
842        // Public submit requires a PUBLISHED form (DRAFT → 404).
843        const pub = await app.request("PATCH", `/api/v1/forms/${form.id}`, {
844          headers: H,
845          body: { status: "PUBLISHED" }
846        });
847        expect(pub.status).toBe(200);
848        const sub = await app.request("POST", `/api/v1/public/forms/${form.slug}/submit`, { body: { answers: {} } });
849        expect(sub.status).toBeGreaterThanOrEqual(200);
850        expect(sub.status).toBeLessThan(300);
851        await observe(20_000, "form.submitted");
852 
853        // file.uploaded + automation.executed — their emitters live in
854        // pipelines this suite doesn't run (MinIO / a full automation run);
855        // drive them through the SAME stream entry shape the emitters
856        // publish (the catalog contract is about the event, not the upload).
857        for (const [event, payload] of [
858          ["file.uploaded", { workspaceId, fileId: "file_catalog", name: "catalog.bin", size: 10, mimeType: "application/octet-stream" }],
859          ["automation.executed", { workspaceId, automationId: "auto_catalog", runId: "run_catalog", status: "SUCCEEDED" }]
860        ]) {
861          await redis.xadd(
862            "factory:events",
863            "*",
864            "event",
865            event,
866            "payload",
867            JSON.stringify(payload),
868            "ts",
869            new Date().toISOString()
870          );
871          await observe(20_000, event);
872        }
873 
874        // Every catalog event observed AND delivered (the AC: a new catalog
875        // event with no emitter fails this build — the table is the catalog).
876        expect(WEBHOOK_EVENT_NAMES.length).toBe(WEBHOOK_EVENT_CATALOG.length);
877        for (const name of WEBHOOK_EVENT_NAMES) {
878          expect(seen.has(name), `catalog event ${name} fired + delivered`).toBe(true);
879        }
880 
881        // Every delivered envelope carries a valid signature (in-test
882        // recompute) — spot-check all captured requests.
883        for (const cap of reqs) {
884          const m = String(cap.headers["x-factory-signature"])!.match(/^t=(\d+),v1=([0-9a-f]{64})$/);
885          expect(m, "header shape").not.toBeNull();
886          const expected = createHmac("sha256", "whsec_catalog_000000000000000000").update(`${m![1]}.` + cap.rawBody.toString("utf8")).digest("hex");
887          expect(m![2], "v1 matches").toBe(expected);
888        }
889      });
890    } finally {
891      await receiver.close();
892    }
893  }, 240_000);
894 
895  // ── 11. incoming webhooks ─────────────────────────────────────────────────
896 
897  describe("incoming webhooks (signature + replay)", () => {
898    let incoming: { url: string; secret: string; workspaceId: string; automationId: string; headers: Record<string, string> };
899 
900    async function signedBody(secret: string, body: unknown, ts = Math.floor(Date.now() / 1000)): Promise<{ raw: string; header: string }> {
901      const raw = JSON.stringify(body);
902      const v1 = createHmac("sha256", secret).update(`${ts}.` + raw).digest("hex");
903      return { raw, header: `t=${ts},v1=${v1}` };
904    }
905    async function postIncoming(raw: string, header: string | undefined): Promise<{ status: number; body: Record<string, unknown> }> {
906      const res = await fetch(`${app.baseUrl}/api/v1${incoming.url}`, {
907        method: "POST",
908        headers: {
909          "Content-Type": "application/json",
910          ...(header !== undefined ? { "X-Factory-Signature": header } : {})
911        },
912        body: raw
913      });
914      return { status: res.status, body: (await res.json().catch(() => ({}))) as Record<string, unknown> };
915    }
916 
917    beforeAll(async () => {
918      const user = await createUser(app.request, { name: "Incoming owner" });
919      const { workspaceId } = await createWorkspace(app.request, authHeaders(user.accessToken!), "Incoming WS");
920      const H = authHeaders(user.accessToken!);
921      const res = await app.request("POST", `/api/v1/workspaces/${workspaceId}/automations`, {
922        headers: H,
923        body: { name: "Incoming automation", triggers: [{ type: "WEBHOOK", config: {} }], actions: [] }
924      });
925      expect([200, 201]).toContain(res.status);
926      const body = await res.json();
927      const autom = body.data ?? body;
928      const automationId = autom.id ?? autom.automationId;
929      // Activate: a created automation starts ACTIVE (Phase 11 default) —
930      // findActiveWebhook requires ACTIVE.
931      const st = await app.request("POST", `/api/v1/automations/${automationId}/status`, {
932        headers: H,
933        body: { status: "ACTIVE" }
934      });
935      expect(st.status).toBe(200);
936      // The trigger PUT returns the generated webhook pair (shown once).
937      const trig = await app.request("PUT", `/api/v1/automations/${automationId}/triggers`, {
938        headers: H,
939        body: { triggers: [{ type: "WEBHOOK", config: {} }] }
940      });
941      expect(trig.status).toBe(200);
942      const trigBody = await trig.json();
943      const trigOut = trigBody.data ?? trigBody;
944      const wh = (trigOut.webhooks as Array<{ webhookId: string; secret: string }>)?.[0] ??
945        (trigOut.triggers as Array<{ config?: { secret?: string } }>)?.[0]?.config ?? null;
946      expect(wh, "incoming webhook pair generated").not.toBeNull();
947      const url = `/webhooks/incoming/${(wh as { webhookId: string }).webhookId}`;
948      incoming = {
949        url,
950        secret: (wh as { secret: string }).secret,
951        workspaceId,
952        automationId: String(automationId),
953        headers: H
954      };
955    });
956 
957    it("valid signature → 202 {data:{runId}} + run row + INTEGRATION audit", async () => {
958      const { raw, header } = await signedBody(incoming.secret, { hello: "factory", n: 1 });
959      const { status, body } = await postIncoming(raw, header);
960      expect(status).toBe(202);
961      expect((body.data as { runId: string }).runId).toMatch(/^[a-z0-9]+$/);
962      const runId = (body.data as { runId: string }).runId;
963      const run = await waitFor(
964        () => app.prisma.automationRun.findUnique({ where: { id: runId } }),
965        (r) => r !== null,
966        10_000,
967        "run row"
968      );
969      expect(run.workspaceId).toBe(incoming.workspaceId);
970      const audit = await app.prisma.auditLog.findFirst({
971        where: { workspaceId: incoming.workspaceId, action: "webhook.received", actorType: "INTEGRATION" }
972      });
973      expect(audit).not.toBeNull();
974    }, 60_000);
975 
976    it("bad signature → 401 signature_invalid", async () => {
977      const { raw } = await signedBody("whsec_WRONG_SECRET_00000000000000000", { hello: "x" });
978      const { status, body } = await postIncoming(raw, `t=${Math.floor(Date.now() / 1000)},v1=${"0".repeat(64)}`);
979      expect(status).toBe(401);
980      const err = body.error as { code: string; details?: Array<{ reason?: string }> };
981      expect(err.code).toBe("WEBHOOK_SIGNATURE_INVALID");
982      expect(err.details?.[0]?.reason).toBe("signature_invalid");
983    }, 30_000);
984 
985    it("6-min-old timestamp → 401 timestamp_expired", async () => {
986      const staleTs = Math.floor(Date.now() / 1000) - 6 * 60;
987      const raw = JSON.stringify({ hello: "stale" });
988      const v1 = createHmac("sha256", incoming.secret).update(`${staleTs}.` + raw).digest("hex");
989      const { status, body } = await postIncoming(raw, `t=${staleTs},v1=${v1}`);
990      expect(status).toBe(401);
991      const err = body.error as { code: string; details?: Array<{ reason?: string }> };
992      expect(err.code).toBe("WEBHOOK_TIMESTAMP_STALE");
993      expect(err.details?.[0]?.reason).toBe("timestamp_expired");
994    }, 30_000);
995 
996    it("replay (same t+body twice) → second is 401 replay_detected", async () => {
997      const { raw, header } = await signedBody(incoming.secret, { replay: "me" });
998      const first = await postIncoming(raw, header);
999      expect(first.status).toBe(202);
1000      const second = await postIncoming(raw, header);
1001      expect(second.status).toBe(401);
1002      const err = second.body.error as { code: string; details?: Array<{ reason?: string }> };
1003      expect(err.code).toBe("WEBHOOK_REPLAY_DETECTED");
1004      expect(err.details?.[0]?.reason).toBe("replay_detected");
1005    }, 30_000);
1006  });
1007});
1008 
1009describe.skipIf(!infraReady)("Webhooks infra gate", () => {
1010  it("skips when Docker/Testcontainers are unavailable", () => {});
1011});
1012