@sleep2agi/commhub-server 0.9.0-preview.25 → 0.9.0-preview.250
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +129 -9
- package/bin/commhub.ts +93 -9
- package/build-info.json +7 -0
- package/package.json +10 -8
- package/src/ack-create-request-transport.test.ts +182 -0
- package/src/ack-create-request.test.ts +52 -8
- package/src/admin-delete-network-http.test.ts +228 -0
- package/src/admin-networks-http.test.ts +88 -0
- package/src/adoption-candidates-http.test.ts +140 -0
- package/src/adoption-candidates.ts +158 -0
- package/src/agent-access-batch.test.ts +210 -0
- package/src/agent-access.ts +487 -0
- package/src/agent-acl-groups-http.test.ts +247 -0
- package/src/agent-acl-http.test.ts +675 -0
- package/src/agent-acl-slice3-http.test.ts +717 -0
- package/src/agent-department-http.test.ts +215 -0
- package/src/agent-groups-perf.test.ts +223 -0
- package/src/agent-notice.ts +48 -0
- package/src/agent-team-whoami-http.test.ts +98 -0
- package/src/agent-teams-http.test.ts +330 -0
- package/src/agent-teams.ts +244 -0
- package/src/alias-baseline-2571.test.ts +162 -0
- package/src/alias-baseline.ts +113 -0
- package/src/alias-filter.test.ts +72 -0
- package/src/alias-filter.ts +42 -0
- package/src/api-host-supervisors-can-create-nodes.test.ts +350 -0
- package/src/api-host-supervisors-fallback.test.ts +6 -7
- package/src/api-host-supervisors-runtime-readiness.test.ts +195 -0
- package/src/api-nodes-shape.test.ts +95 -4
- package/src/auth-password-change-cli-tokens-http.test.ts +423 -0
- package/src/auth-sessions-http.test.ts +260 -0
- package/src/auth-validate.test.ts +5 -1
- package/src/auth-weak-password-login.test.ts +176 -0
- package/src/auth.ts +609 -51
- package/src/avatar-validate.test.ts +52 -2
- package/src/avatar-validate.ts +35 -1
- package/src/backfill-node-ids-2664.test.ts +128 -0
- package/src/binding-generation-http.test.ts +132 -0
- package/src/blocked-exit-on-reply.test.ts +122 -0
- package/src/broadcast-fanout-2721.test.ts +149 -0
- package/src/broadcast-fanout.ts +117 -0
- package/src/build-info.test.ts +60 -0
- package/src/build-info.ts +85 -0
- package/src/chain-attempt-dedup.test.ts +447 -0
- package/src/clip.test.ts +36 -0
- package/src/clip.ts +48 -0
- package/src/clone-node.test.ts +262 -0
- package/src/clone-node.ts +289 -0
- package/src/codex-config-flags.ts +77 -0
- package/src/codex-fork-contract.ts +23 -0
- package/src/codex-fork-migration.test.ts +38 -0
- package/src/commhub-bin-args.test.ts +85 -0
- package/src/config-apply-validate.test.ts +188 -1
- package/src/config-apply-validate.ts +222 -3
- package/src/cors-origins.test.ts +73 -0
- package/src/create-node-blocked-gate.test.ts +356 -0
- package/src/create-node-network-id.test.ts +91 -0
- package/src/create-node-tool-schema.test.ts +49 -0
- package/src/create-node-unicode-name.test.ts +126 -0
- package/src/create-node-validate.test.ts +199 -7
- package/src/create-node-validate.ts +232 -15
- package/src/create-node-workdir.test.ts +298 -0
- package/src/create-node.ts +88 -23
- package/src/daemon-provider-snapshots.test.ts +445 -0
- package/src/daemon-provider-snapshots.ts +327 -0
- package/src/daemon-token-owner-http.test.ts +292 -0
- package/src/dashboard-slash-routing-http.test.ts +109 -0
- package/src/db-adapter-guard.test.ts +148 -0
- package/src/db-adapter-translate.test.ts +128 -0
- package/src/db-adapter.ts +377 -91
- package/src/db-timestamp.test.ts +38 -0
- package/src/db-timestamp.ts +15 -0
- package/src/db.ts +1264 -153
- package/src/delivered-without-push.test.ts +140 -0
- package/src/department-groups-http.test.ts +209 -0
- package/src/department-groups-sync-http.test.ts +328 -0
- package/src/department-groups.ts +434 -0
- package/src/department-heads-http.test.ts +350 -0
- package/src/department-heads.ts +226 -0
- package/src/departments-agent-count.test.ts +132 -0
- package/src/departments-http.test.ts +185 -0
- package/src/departments-mcp.test.ts +128 -0
- package/src/departments.ts +318 -0
- package/src/desktop-message-seam.test.ts +150 -0
- package/src/desktop-message.test.ts +368 -0
- package/src/dm-file-scope-http.test.ts +178 -0
- package/src/empty-node-sweeper.test.ts +104 -0
- package/src/empty-node-sweeper.ts +88 -0
- package/src/env-transport-gate-http.test.ts +240 -0
- package/src/env-transport-gate.ts +80 -0
- package/src/env-transport-policy.ts +56 -0
- package/src/etag-of.test.ts +57 -0
- package/src/event-loop-lag.test.ts +180 -0
- package/src/event-loop-lag.ts +201 -0
- package/src/expiry-counters.test.ts +111 -0
- package/src/expiry-counters.ts +93 -0
- package/src/ext-token-shared.test.ts +60 -0
- package/src/ext-validation-regression.test.ts +131 -0
- package/src/external-schedule-edits-http.test.ts +321 -0
- package/src/external-schedule-edits-timestamps.test.ts +64 -0
- package/src/external-schedule-edits.ts +327 -0
- package/src/file-download-authz-dev-open.test.ts +1 -1
- package/src/file-download-authz.test.ts +73 -18
- package/src/file-download-date-bucket.test.ts +8 -26
- package/src/file-network-scope-dev-open.test.ts +97 -0
- package/src/file-network-scope.test.ts +732 -0
- package/src/group-membership-events-http.test.ts +264 -0
- package/src/group-messages-http.test.ts +483 -0
- package/src/group-messages.ts +297 -0
- package/src/health-anon-readers.ts +60 -0
- package/src/health-host-disk-2756.test.ts +71 -0
- package/src/health-redaction.test.ts +1541 -7
- package/src/host-supervisor-identity-http.test.ts +152 -0
- package/src/host-supervisor-identity.ts +92 -0
- package/src/host-supervisors-daemon-version.test.ts +137 -0
- package/src/http-gzip.test.ts +94 -0
- package/src/http-gzip.ts +112 -0
- package/src/hub-catalog-read-http.test.ts +641 -0
- package/src/hub-provider-config.test.ts +166 -0
- package/src/hub-provider-config.ts +202 -0
- package/src/hub-timestamp.test.ts +38 -0
- package/src/hub-timestamp.ts +27 -0
- package/src/human-dm.ts +215 -0
- package/src/human-inbox.ts +42 -0
- package/src/inbox-caller.ts +49 -0
- package/src/inbox-count-sse.test.ts +310 -0
- package/src/inbox-count.ts +29 -0
- package/src/index.ts +6 -2
- package/src/license-state.test.ts +56 -0
- package/src/license-state.ts +48 -0
- package/src/list-host-supervisors.test.ts +15 -4
- package/src/mcp-context-cost-http.test.ts +127 -0
- package/src/mcp-inflight.test.ts +92 -0
- package/src/mcp-inflight.ts +38 -0
- package/src/mcp-projects-events-http.test.ts +165 -0
- package/src/mcp-route-label-http.test.ts +120 -0
- package/src/mcp-write-network-resolution.test.ts +435 -0
- package/src/member-audit-network-id-http.test.ts +93 -0
- package/src/member-presence-http.test.ts +210 -0
- package/src/messages-ack-by-agent-http.test.ts +142 -0
- package/src/migrate-sqlite-to-pg.test.ts +47 -0
- package/src/migrate-sqlite-to-pg.ts +263 -0
- package/src/model-auth-notify-http.test.ts +181 -0
- package/src/model-auth-notify.ts +116 -0
- package/src/network-name-http.test.ts +75 -0
- package/src/network-rules-http.test.ts +169 -0
- package/src/network-rules.ts +129 -0
- package/src/network-scope.ts +276 -0
- package/src/network-token-membership-http.test.ts +186 -0
- package/src/node-alias-control.ts +12 -0
- package/src/node-attrs.test.ts +47 -2
- package/src/node-caller-identity-http.test.ts +1108 -0
- package/src/node-create-request-status-http.test.ts +105 -0
- package/src/node-daemon-bindings.test.ts +132 -0
- package/src/node-daemon-bindings.ts +135 -0
- package/src/node-failure-alert.test.ts +150 -0
- package/src/node-failure-alert.ts +177 -0
- package/src/node-files-transport.test.ts +347 -0
- package/src/node-health-codex-login.test.ts +120 -0
- package/src/node-health-dispatch-http.test.ts +373 -0
- package/src/node-health-guard.ts +121 -0
- package/src/node-health-passthrough.test.ts +115 -0
- package/src/node-health-store.ts +101 -0
- package/src/node-id-alias.test.ts +190 -0
- package/src/node-identity-conflict-http.test.ts +269 -0
- package/src/node-identity-conflict.ts +142 -0
- package/src/node-identity-mismatch.test.ts +74 -0
- package/src/node-in-flight-tasks.ts +31 -0
- package/src/node-lifecycle-controllable.test.ts +64 -0
- package/src/node-lifecycle-controllable.ts +53 -0
- package/src/node-lifecycle-read-http.test.ts +257 -0
- package/src/node-lifecycle-read.ts +124 -0
- package/src/node-logs-transport.test.ts +263 -0
- package/src/node-peer-allowlist.test.ts +114 -0
- package/src/node-permissions-http.test.ts +728 -0
- package/src/node-permissions.ts +494 -0
- package/src/node-request-retention.test.ts +281 -0
- package/src/node-request-retention.ts +120 -0
- package/src/node-secret-rest-http.test.ts +190 -0
- package/src/node-serviceability.test.ts +86 -0
- package/src/node-serviceability.ts +124 -0
- package/src/node-session-control-http.test.ts +310 -0
- package/src/node-session-control.ts +258 -0
- package/src/node-skills-transport.test.ts +212 -0
- package/src/node-token-identity.ts +35 -0
- package/src/node-token-ownership.ts +51 -0
- package/src/node-token-privilege-http.test.ts +158 -0
- package/src/not-found-api-http.test.ts +67 -0
- package/src/observer-avatar-http.test.ts +102 -9
- package/src/observer-push.test.ts +76 -0
- package/src/opencode-catalog-report-http.test.ts +139 -0
- package/src/opencode-v2-catalog.test.ts +268 -0
- package/src/opencode-v2-catalog.ts +356 -0
- package/src/package-version-consistency.test.ts +25 -0
- package/src/parent-source-marker.test.ts +50 -0
- package/src/password-network-tokens.ts +25 -0
- package/src/peer-reply-atomic.test.ts +462 -0
- package/src/peer-reply-capability.test.ts +70 -0
- package/src/pg-worker.ts +87 -0
- package/src/poll-pacing.test.ts +74 -0
- package/src/poll-pacing.ts +123 -0
- package/src/project-dir-warn-clear-http.test.ts +132 -0
- package/src/project-viewer-can-http.test.ts +116 -0
- package/src/provider-tools-shape.test.ts +150 -0
- package/src/push.test.ts +20 -10
- package/src/push.ts +373 -9
- package/src/redact-tokens.test.ts +86 -0
- package/src/redact-tokens.ts +43 -0
- package/src/rejection-counters.test.ts +72 -0
- package/src/rejection-counters.ts +173 -0
- package/src/rename.ts +38 -10
- package/src/reply-target-derivation.test.ts +96 -0
- package/src/report-status-handover-capabilities-http.test.ts +124 -0
- package/src/report-status-host-schema.test.ts +235 -0
- package/src/report-status-resume-id-handover.test.ts +184 -0
- package/src/report-status-secret-keys.test.ts +140 -0
- package/src/require-explicit-test-db.ts +9 -0
- package/src/requirement-due-reminders-http.test.ts +620 -0
- package/src/requirement-due-reminders.ts +416 -0
- package/src/requirement-events-http.test.ts +318 -0
- package/src/requirement-events.test.ts +45 -0
- package/src/requirement-events.ts +144 -0
- package/src/requirement-notify.ts +165 -0
- package/src/requirement-participant-notify-http.test.ts +309 -0
- package/src/requirement-tag-ops-http.test.ts +454 -0
- package/src/requirement-tag-tree-http.test.ts +275 -0
- package/src/requirement-tag-tree.ts +127 -0
- package/src/requirement-tags.ts +102 -0
- package/src/requirements-abandoned-http.test.ts +236 -0
- package/src/requirements-assignee-drift.test.ts +154 -0
- package/src/requirements-comment-http.test.ts +174 -0
- package/src/requirements-completed-at-migration.test.ts +70 -0
- package/src/requirements-errors-mcp-http.test.ts +164 -0
- package/src/requirements-errors.ts +158 -0
- package/src/requirements-etag-http.test.ts +91 -0
- package/src/requirements-http.test.ts +709 -0
- package/src/requirements-last-event-http.test.ts +349 -0
- package/src/requirements-list-mcp-slim-http.test.ts +155 -0
- package/src/requirements-list-slim-http.test.ts +324 -0
- package/src/requirements-mcp.test.ts +164 -0
- package/src/requirements-migrate.ts +283 -0
- package/src/requirements-owner-strict-mcp-http.test.ts +145 -0
- package/src/requirements-people-http.test.ts +114 -0
- package/src/requirements-people-mcp-http.test.ts +231 -0
- package/src/requirements-people.ts +196 -0
- package/src/requirements-priority-lowest.test.ts +114 -0
- package/src/requirements-projects-network-echo.test.ts +57 -0
- package/src/requirements-projects-parent-http.test.ts +123 -0
- package/src/requirements-search-http.test.ts +182 -0
- package/src/requirements-search.test.ts +32 -0
- package/src/requirements-search.ts +145 -0
- package/src/requirements-seq-http.test.ts +193 -0
- package/src/requirements-seq-migration.test.ts +63 -0
- package/src/requirements-start-date.test.ts +125 -0
- package/src/requirements-stats-http.test.ts +357 -0
- package/src/requirements-stats.ts +195 -0
- package/src/requirements-status-participants-http.test.ts +222 -0
- package/src/requirements-upgrade-e2e.test.ts +126 -0
- package/src/requirements-upgrade-migration.test.ts +98 -0
- package/src/requirements.ts +2205 -0
- package/src/resolve-port.test.ts +57 -0
- package/src/resolve-port.ts +44 -0
- package/src/rest-alias-network-fallback-http.test.ts +123 -0
- package/src/rest-alias-network-fallback.test.ts +60 -0
- package/src/rest-alias-network-fallback.ts +65 -0
- package/src/rest-explicit-columns-http.test.ts +266 -0
- package/src/rest-identity.test.ts +130 -0
- package/src/rest-identity.ts +141 -0
- package/src/rest-projections.ts +72 -0
- package/src/rest-read-network-real-chain.test.ts +97 -0
- package/src/rest-write-network-resolution.test.ts +133 -0
- package/src/restart-readiness-http.test.ts +121 -0
- package/src/restricted-files.ts +76 -0
- package/src/retention-first-sweep-quiet.test.ts +31 -0
- package/src/retention.test.ts +100 -40
- package/src/retention.ts +423 -104
- package/src/route-callers-http.test.ts +120 -0
- package/src/route-timing.test.ts +218 -0
- package/src/route-timing.ts +262 -0
- package/src/rules-file-duplicate-alias.test.ts +234 -0
- package/src/rules-file-session-target.test.ts +207 -0
- package/src/rules-file-transport.test.ts +237 -0
- package/src/runtime-label.test.ts +22 -0
- package/src/runtime-label.ts +21 -0
- package/src/runtime-readiness.ts +90 -0
- package/src/schedule-agent-mcp-http.test.ts +475 -0
- package/src/schedule-agent.ts +252 -0
- package/src/schedule-starved.test.ts +92 -0
- package/src/schedule-starved.ts +114 -0
- package/src/scheduled-backoff-http.test.ts +203 -0
- package/src/scheduled-backoff.ts +64 -0
- package/src/scheduled-doorbell-nodeless-session.test.ts +134 -0
- package/src/scheduled-failure-backoff-2622.test.ts +145 -0
- package/src/scheduled-failure-code.test.ts +73 -0
- package/src/scheduled-failure-code.ts +105 -0
- package/src/scheduled-failures-http.test.ts +279 -0
- package/src/scheduled-failures.ts +425 -0
- package/src/scheduled-noprogress-backoff-2622.test.ts +163 -0
- package/src/scheduled-offline-backoff-probe.test.ts +56 -0
- package/src/scheduled-offline.test.ts +131 -0
- package/src/scheduled-offline.ts +66 -0
- package/src/scheduled-outcome-guidance-http.test.ts +139 -0
- package/src/scheduled-outcome-guidance.ts +9 -0
- package/src/scheduled-reply-unread-http.test.ts +212 -0
- package/src/scheduled-requirement-wake-http.test.ts +285 -0
- package/src/scheduled-requirement-wake.ts +125 -0
- package/src/scheduled-run-terminal.test.ts +290 -0
- package/src/scheduled-skip-blocking-task.test.ts +120 -0
- package/src/scheduled-skip-path-backoff-2622.test.ts +98 -0
- package/src/scheduled-stuck-http.test.ts +316 -0
- package/src/scheduled-stuck-notice-cap.test.ts +80 -0
- package/src/scheduled-stuck.ts +219 -0
- package/src/scheduled-tasks-http.test.ts +679 -0
- package/src/scheduled-tasks.ts +959 -0
- package/src/scheduled-work-outcome-http.test.ts +154 -0
- package/src/scheduled-work-outcome.ts +50 -0
- package/src/scoped-secrets.ts +141 -0
- package/src/secret-echo-negative-control.test.ts +118 -0
- package/src/send-dedup-request-id.test.ts +229 -0
- package/src/send-reply-agent-warning.test.ts +38 -38
- package/src/send-reply-alias-optional.test.ts +131 -0
- package/src/send-reply-attachments.test.ts +458 -0
- package/src/send_dedup.test.ts +54 -0
- package/src/send_dedup.ts +52 -6
- package/src/serve-error.test.ts +73 -0
- package/src/serve-error.ts +56 -0
- package/src/server.ts +5134 -503
- package/src/session-task-on-dispatch.ts +14 -0
- package/src/session-token-ownership-http.test.ts +122 -0
- package/src/shared/codex-inventory.ts +41 -0
- package/src/shared/daemon-provider-login.ts +31 -0
- package/src/shared/daemon-provider-snapshot.ts +52 -0
- package/src/shared/external-schedule-contract.ts +60 -0
- package/src/shared/node-name.test.ts +62 -0
- package/src/shared/node-name.ts +161 -0
- package/src/shared/probe-host-allowlist-drift.test.ts +45 -0
- package/src/shared/provider-application.ts +29 -0
- package/src/shared/provider-collection.ts +14 -0
- package/src/shared/provider-initialization.ts +8 -0
- package/src/shared/reserved-env-drift.test.ts +10 -0
- package/src/shared/scoped-secret-contract.ts +5 -0
- package/src/shared/task-content-limit-drift.test.ts +28 -0
- package/src/shared/task-content-limit.ts +25 -0
- package/src/side-thread-command-transport.test.ts +312 -0
- package/src/side-thread-command-transport.ts +318 -0
- package/src/side-thread-http-integration.test.ts +233 -0
- package/src/side-thread-http.ts +504 -0
- package/src/side-thread-production.test.ts +72 -0
- package/src/side-thread-production.ts +118 -0
- package/src/side-thread.test.ts +1067 -0
- package/src/side-thread.ts +2499 -0
- package/src/skillhub-http.test.ts +142 -0
- package/src/sqlite-close-checkpoint.test.ts +78 -0
- package/src/start-node.test.ts +265 -0
- package/src/stats-versions-http.test.ts +65 -0
- package/src/status-alias-filter-http.test.ts +106 -0
- package/src/status-alias-resolver-http.test.ts +163 -0
- package/src/status-alias-resolver.ts +19 -0
- package/src/status-node-id-filter-http.test.ts +161 -0
- package/src/status-read-cache-http.test.ts +229 -0
- package/src/status-read-cache.ts +129 -0
- package/src/stop-delete-node.test.ts +567 -1
- package/src/task-access-http.test.ts +402 -0
- package/src/task-access.ts +420 -0
- package/src/task-activity.ts +60 -0
- package/src/task-auth-origin-http.test.ts +93 -0
- package/src/task-auth-origin.test.ts +24 -0
- package/src/task-auth-origin.ts +7 -0
- package/src/task-consumption.test.ts +498 -0
- package/src/task-diagnostic-http.test.ts +149 -0
- package/src/task-diagnostic.test.ts +115 -0
- package/src/task-diagnostic.ts +118 -0
- package/src/task-expiry-notice-http.test.ts +287 -0
- package/src/task-expiry-notice.ts +318 -0
- package/src/task-idempotency-mcp.test.ts +142 -0
- package/src/task-idempotency.test.ts +76 -0
- package/src/task-idempotency.ts +81 -0
- package/src/task-late-replies.ts +54 -0
- package/src/task-late-reply-http.test.ts +227 -0
- package/src/task-lifecycle-watcher-busy.test.ts +114 -0
- package/src/task-lifecycle-watcher.test.ts +209 -0
- package/src/task-lifecycle-watcher.ts +109 -0
- package/src/task-network-resolution-http.test.ts +140 -0
- package/src/task-offline-delivery-2668.test.ts +119 -0
- package/src/task-offline-delivery.ts +81 -0
- package/src/task-orphan-http.test.ts +287 -0
- package/src/task-orphan.ts +119 -0
- package/src/task-queue-ahead-http.test.ts +309 -0
- package/src/task-queue-ahead-scale.test.ts +236 -0
- package/src/task-queue-ahead.ts +186 -0
- package/src/task-rejection-log-http.test.ts +160 -0
- package/src/task-rejection-log.ts +61 -0
- package/src/task-sender-http.test.ts +143 -0
- package/src/task-stale-open-http.test.ts +292 -0
- package/src/task-stale-open.ts +103 -0
- package/src/tasks-pagination-http.test.ts +141 -0
- package/src/team-rules-status-http.test.ts +90 -0
- package/src/team-rules-status.test.ts +59 -0
- package/src/team-rules-status.ts +72 -0
- package/src/tmux-allowlist.test.ts +48 -0
- package/src/tool-audience-http.test.ts +230 -0
- package/src/tool-audience.ts +127 -0
- package/src/tools-list-json-schema.test.ts +94 -0
- package/src/tools.ts +4326 -420
- package/src/upload-early-reject-guard.test.ts +155 -0
- package/src/uploads-http.test.ts +86 -7
- package/src/uploads.ts +38 -4
- package/src/user-avatar-http.test.ts +74 -0
- package/src/user-avatar-image.ts +160 -0
- package/src/user-avatar-migration.test.ts +25 -0
- package/src/user-avatar-upload-http.test.ts +348 -0
- package/src/user-inbox-by-agent-http.test.ts +165 -0
- package/src/user-inbox-netid-notnull.test.ts +122 -0
- package/src/user-inbox-read-http.test.ts +223 -0
- package/src/user-inbox-schema.test.ts +54 -0
- package/src/version-stats.test.ts +66 -0
- package/src/version-stats.ts +109 -0
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
// 任务动态(#429)—— HTTP 集成测试(真实 Bun.serve,临时库;PG 梯子上用 COMMHUB_TEST_PG_URL 原样再跑一遍)。
|
|
2
|
+
// 写路径 —— 新建 / PATCH 的每个字段 / 勾子任务 / 标签改名 / 删项目 / 删卡,各记字段级流水:谁、旧值 → 新值
|
|
3
|
+
// 同一事务 —— 流水写不进去,那次改动也不生效
|
|
4
|
+
// 可见范围 —— 与列表相同:scoped 成员只看到看得见的卡(删掉的按墓碑);Agent 受限成员看不见的节点被隐去
|
|
5
|
+
// 翻页 —— limit / next_cursor / since / requirement_id;参数不合法 400;capability `events`
|
|
6
|
+
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
7
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
8
|
+
import { tmpdir } from "node:os";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
|
|
11
|
+
const DIR = mkdtempSync(join(tmpdir(), "anet-requirement-events-"));
|
|
12
|
+
process.env.COMMHUB_DB ||= join(DIR, "hub.db");
|
|
13
|
+
process.env.COMMHUB_UPLOADS_DIR = join(DIR, "uploads");
|
|
14
|
+
|
|
15
|
+
const PW = "EventsPassw0rd!x";
|
|
16
|
+
let BASE = "";
|
|
17
|
+
let hub: any = null;
|
|
18
|
+
let NET = "";
|
|
19
|
+
let admin = { token: "", id: "" };
|
|
20
|
+
let member = { token: "", id: "" };
|
|
21
|
+
let scoped = { token: "", id: "" };
|
|
22
|
+
let restricted = { token: "", id: "" };
|
|
23
|
+
let nodeToken = "";
|
|
24
|
+
let NODE = "";
|
|
25
|
+
|
|
26
|
+
type R = { status: number; body: any };
|
|
27
|
+
async function send(token: string, method: string, path: string, payload?: unknown): Promise<R> {
|
|
28
|
+
const res = await fetch(`${BASE}${path}`, {
|
|
29
|
+
method,
|
|
30
|
+
headers: { ...(token ? { Authorization: `Bearer ${token}` } : {}), "Content-Type": "application/json" },
|
|
31
|
+
body: payload === undefined ? undefined : JSON.stringify(payload),
|
|
32
|
+
});
|
|
33
|
+
const text = await res.text();
|
|
34
|
+
let body: any = null;
|
|
35
|
+
try { body = JSON.parse(text); } catch {}
|
|
36
|
+
return { status: res.status, body };
|
|
37
|
+
}
|
|
38
|
+
const create = async (t: string, card: Record<string, unknown>) => {
|
|
39
|
+
const r = await send(t, "POST", "/api/requirements", { network_id: NET, ...card });
|
|
40
|
+
expect(r.status).toBe(201);
|
|
41
|
+
return r.body.requirement as { id: string; seq: number };
|
|
42
|
+
};
|
|
43
|
+
const patch = (t: string, id: string, p: Record<string, unknown>) => send(t, "PATCH", `/api/requirements/${id}?network_id=${NET}`, p);
|
|
44
|
+
const events = (t: string, qs = "") => send(t, "GET", `/api/requirements/events?network_id=${NET}${qs}`);
|
|
45
|
+
const of = (list: any[], id: string) => list.filter(e => e.requirement_id === id);
|
|
46
|
+
const user = (id: string) => ({ kind: "user", id });
|
|
47
|
+
|
|
48
|
+
beforeAll(async () => {
|
|
49
|
+
process.env.HOST = "127.0.0.1";
|
|
50
|
+
const { register, createNetworkTokenForNode } = await import("./auth.js");
|
|
51
|
+
const { db } = await import("./db.js");
|
|
52
|
+
const a = register(`ev_admin_${Date.now()}`, PW, undefined, "Admin");
|
|
53
|
+
expect(a.ok).toBe(true);
|
|
54
|
+
admin = { token: a.token!, id: a.user!.user_id };
|
|
55
|
+
NET = a.network_id!;
|
|
56
|
+
db.run("UPDATE users SET role = 'admin' WHERE user_id = ?1", [admin.id]);
|
|
57
|
+
const mod: any = await import("./server.js");
|
|
58
|
+
hub = mod.bootServer({ port: 0, hostname: "127.0.0.1" });
|
|
59
|
+
BASE = `http://127.0.0.1:${hub.port}`;
|
|
60
|
+
const mk = async (username: string) => {
|
|
61
|
+
const r = await send(admin.token, "POST", "/api/admin/users", { username, password: PW, network_id: NET, role: "member" });
|
|
62
|
+
expect(r.status).toBe(200);
|
|
63
|
+
const login = await send("", "POST", "/api/auth/login", { username, password: PW });
|
|
64
|
+
expect(login.status).toBe(200);
|
|
65
|
+
return { token: login.body.token as string, id: login.body.user.user_id as string };
|
|
66
|
+
};
|
|
67
|
+
const stamp = Date.now();
|
|
68
|
+
member = await mk(`ev_member_${stamp}`);
|
|
69
|
+
scoped = await mk(`ev_scoped_${stamp}`);
|
|
70
|
+
restricted = await mk(`ev_restricted_${stamp}`);
|
|
71
|
+
db.run("UPDATE network_members SET task_access = 'all', agent_access = 'all' WHERE network_id = ?1 AND user_id = ?2", [NET, member.id]);
|
|
72
|
+
db.run("UPDATE network_members SET task_access = 'scoped' WHERE network_id = ?1 AND user_id = ?2", [NET, scoped.id]);
|
|
73
|
+
// 看全部任务,但一个 Agent 都没授权:所有节点对他隐去
|
|
74
|
+
db.run("UPDATE network_members SET task_access = 'all', agent_access = 'granted' WHERE network_id = ?1 AND user_id = ?2", [NET, restricted.id]);
|
|
75
|
+
NODE = `n_ev_${stamp}`;
|
|
76
|
+
const ntok = createNetworkTokenForNode(admin.id, NET, "ev-node");
|
|
77
|
+
expect(ntok.ok).toBe(true);
|
|
78
|
+
nodeToken = ntok.token!;
|
|
79
|
+
// Real legacy-client order: mint by name, then register; never fill owner.
|
|
80
|
+
const registration = await fetch(`${BASE}/mcp`, {
|
|
81
|
+
method: "POST",
|
|
82
|
+
headers: { Authorization: `Bearer ${nodeToken}`, "Content-Type": "application/json", Accept: "application/json, text/event-stream" },
|
|
83
|
+
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "report_status", arguments: {
|
|
84
|
+
resume_id: `ev-session-${stamp}`, alias: "ev-node", status: "idle", node_id: NODE,
|
|
85
|
+
} } }),
|
|
86
|
+
});
|
|
87
|
+
expect(registration.status).toBe(200);
|
|
88
|
+
await registration.text();
|
|
89
|
+
expect(db.get<{owner_user_id: string | null}>("SELECT owner_user_id FROM nodes WHERE node_id=?1", NODE)?.owner_user_id).toBeNull();
|
|
90
|
+
expect(db.get<{bound_node_id: string | null}>("SELECT bound_node_id FROM api_tokens WHERE token_id=?1", ntok.token_id!)?.bound_node_id).toBe(NODE);
|
|
91
|
+
}, 30_000);
|
|
92
|
+
|
|
93
|
+
afterAll(() => {
|
|
94
|
+
try { hub?.stop?.(true); } catch {}
|
|
95
|
+
try { rmSync(DIR, { recursive: true, force: true }); } catch {}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe("requirement events", () => {
|
|
99
|
+
test("capability `events` is advertised", async () => {
|
|
100
|
+
const r = await send(admin.token, "GET", `/api/requirements?network_id=${NET}`);
|
|
101
|
+
expect(r.body.capabilities).toContain("events");
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("create records one `created` event with the actor, title and seq", async () => {
|
|
105
|
+
const card = await create(member.token, { name: "动态示例一" });
|
|
106
|
+
const r = await events(admin.token, `&requirement_id=${card.id}`);
|
|
107
|
+
expect(r.status).toBe(200);
|
|
108
|
+
expect(r.body.events.length).toBe(1);
|
|
109
|
+
const [e] = r.body.events;
|
|
110
|
+
expect(e).toMatchObject({ kind: "created", field: null, actor: user(member.id), title: "动态示例一", seq: card.seq, new: { title: "动态示例一", column: "pool" } });
|
|
111
|
+
expect(typeof e.id).toBe("string");
|
|
112
|
+
expect(Date.parse(e.at)).toBeGreaterThan(0);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("a PATCH records one event per changed field, old → new; unchanged fields and no-op PATCHes record nothing", async () => {
|
|
116
|
+
const card = await create(admin.token, { name: "动态示例二", priority: "normal", tags: ["UI"] });
|
|
117
|
+
const r1 = await patch(member.token, card.id, {
|
|
118
|
+
column: "doing", priority: "high", due: "2026-10-03", name: "动态示例二(改)", tags: ["UI", "后端"],
|
|
119
|
+
agent_owner: { kind: "node", id: NODE }, participants: [user(admin.id)], assignee: "",
|
|
120
|
+
});
|
|
121
|
+
expect(r1.status).toBe(200);
|
|
122
|
+
const list = of((await events(admin.token)).body.events, card.id);
|
|
123
|
+
const byField = Object.fromEntries(list.filter((e: any) => e.kind === "changed").map((e: any) => [e.field, e]));
|
|
124
|
+
expect(Object.keys(byField).sort()).toEqual(["agent_owner", "column", "due", "participants", "priority", "tags", "title"]);
|
|
125
|
+
expect(byField.column).toMatchObject({ old: "pool", new: "doing", actor: user(member.id) });
|
|
126
|
+
expect(byField.priority).toMatchObject({ old: "normal", new: "high" });
|
|
127
|
+
expect(byField.due).toMatchObject({ old: null, new: "2026-10-03" });
|
|
128
|
+
expect(byField.title).toMatchObject({ old: "动态示例二", new: "动态示例二(改)" });
|
|
129
|
+
expect(byField.tags).toMatchObject({ old: ["UI"], new: ["UI", "后端"] });
|
|
130
|
+
expect(byField.agent_owner).toMatchObject({ old: null, new: { kind: "node", id: NODE } });
|
|
131
|
+
expect(byField.participants).toMatchObject({ old: [], new: [user(admin.id)] });
|
|
132
|
+
// every event of that PATCH carries the card's title after the change
|
|
133
|
+
expect(new Set(list.filter((e: any) => e.kind === "changed").map((e: any) => e.title))).toEqual(new Set(["动态示例二(改)"]));
|
|
134
|
+
const before = list.length;
|
|
135
|
+
expect((await patch(member.token, card.id, { priority: "high", column: "doing" })).status).toBe(200);
|
|
136
|
+
expect(of((await events(admin.token)).body.events, card.id).length).toBe(before);
|
|
137
|
+
// archive / unarchive, description (length only, never the text), move to done
|
|
138
|
+
await patch(member.token, card.id, { description: "一段描述" });
|
|
139
|
+
await patch(member.token, card.id, { column: "done" });
|
|
140
|
+
await patch(member.token, card.id, { archived: true });
|
|
141
|
+
const later = of((await events(admin.token)).body.events, card.id).slice(0, 3);
|
|
142
|
+
expect(later.map((e: any) => e.field)).toEqual(["archived", "column", "description"]);
|
|
143
|
+
expect(later[0]).toMatchObject({ old: false, new: true });
|
|
144
|
+
expect(later[1]).toMatchObject({ old: "doing", new: "done" });
|
|
145
|
+
expect(later[2]).toMatchObject({ old: { chars: 0 }, new: { chars: 4 } });
|
|
146
|
+
expect(JSON.stringify(later[2])).not.toContain("一段描述");
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
// 2026-10-31 —— **变化字段集合 == 实际被写库的字段集合**(外部复核那条的对账形态)。
|
|
150
|
+
// 上一节断言的是「事件字段集合」;这里把它与「读回来的行真的变了哪些字段」逐项比对 ——
|
|
151
|
+
// 只有两边相等,才能说「流水没有漏记,也没有凭空多记」。
|
|
152
|
+
test("changed-field set equals the set of columns that actually changed in the stored row", async () => {
|
|
153
|
+
const card = await create(admin.token, { name: "对账示例", priority: "normal" });
|
|
154
|
+
const read = async () => (await send(admin.token, "GET", `/api/requirements/${card.id}?network_id=${NET}`)).body.requirement;
|
|
155
|
+
const before = await read();
|
|
156
|
+
// 2026-10-31 —— **同一个 PATCH 里混一个「赋相同值」的字段**(外部复核 (a) 的原话:
|
|
157
|
+
// 「同一次 update 里加一个赋相同值的字段,断言它不在 changes 里」)。
|
|
158
|
+
// 这是「变化集 vs 赋值集」两种实现唯一分叉的输入形态:
|
|
159
|
+
// 只测「整单都是空操作」时,两种实现都绿;混一个不变字段,只有按值比较的那种才绿。
|
|
160
|
+
const r = await patch(member.token, card.id, { column: "doing", priority: "high", due: "2026-10-09", assignee: "" });
|
|
161
|
+
expect(r.status).toBe(200);
|
|
162
|
+
const after = await read();
|
|
163
|
+
// 只看这次 PATCH 涉及的四个字段(三个改了、一个赋的是原值):读回来的行里**真的不同**的那些
|
|
164
|
+
const keys = ["column", "priority", "due", "assignee"] as const;
|
|
165
|
+
const actuallyChanged = keys.filter((k) => JSON.stringify(before[k] ?? null) !== JSON.stringify(after[k] ?? null)).sort();
|
|
166
|
+
const eventFields = of((await events(admin.token)).body.events, card.id)
|
|
167
|
+
.filter((e: any) => e.kind === "changed")
|
|
168
|
+
.map((e: any) => e.field).sort();
|
|
169
|
+
expect(eventFields).toEqual(actuallyChanged);
|
|
170
|
+
expect(actuallyChanged).toEqual(["column", "due", "priority"]);
|
|
171
|
+
// 赋相同值的那个字段:既不在实际变化里,也不在流水里(否则「赋值集」实现会多记一条)
|
|
172
|
+
expect(actuallyChanged).not.toContain("assignee");
|
|
173
|
+
expect(eventFields).not.toContain("assignee");
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test("checklist: ticking an item is `checklist_item` (repeat is a no-op); adding items is `checklist`", async () => {
|
|
177
|
+
const card = await create(admin.token, { name: "动态示例三", checklist: [{ id: "a", text: "第一项" }, { id: "b", text: "第二项" }] });
|
|
178
|
+
const tick = (done: boolean) => send(member.token, "PATCH", `/api/requirements/${card.id}/checklist/a?network_id=${NET}`, { done });
|
|
179
|
+
expect((await tick(true)).status).toBe(200);
|
|
180
|
+
expect((await tick(true)).status).toBe(200);
|
|
181
|
+
let list = of((await events(admin.token)).body.events, card.id);
|
|
182
|
+
const ticks = list.filter((e: any) => e.field === "checklist_item");
|
|
183
|
+
expect(ticks.length).toBe(1);
|
|
184
|
+
expect(ticks[0]).toMatchObject({ old: { id: "a", text: "第一项", done: false }, new: { id: "a", text: "第一项", done: true }, actor: user(member.id) });
|
|
185
|
+
// a PATCH that only flips one item's done is also `checklist_item`
|
|
186
|
+
await patch(member.token, card.id, { checklist: [{ id: "a", text: "第一项", done: true }, { id: "b", text: "第二项", done: true }] });
|
|
187
|
+
await patch(member.token, card.id, { checklist: [{ id: "a", text: "第一项", done: true }, { id: "b", text: "第二项", done: true }, { id: "c", text: "第三项" }] });
|
|
188
|
+
list = of((await events(admin.token)).body.events, card.id);
|
|
189
|
+
expect(list[0]).toMatchObject({ field: "checklist", old: { total: 2, done: 2 }, new: { total: 3, done: 2 } });
|
|
190
|
+
expect(list[1]).toMatchObject({ field: "checklist_item", new: { id: "b", done: true } });
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test("the actor of a node token is the node", async () => {
|
|
194
|
+
const card = await create(admin.token, { name: "动态示例四" });
|
|
195
|
+
expect((await patch(nodeToken, card.id, { column: "doing" })).status).toBe(200);
|
|
196
|
+
const [e] = of((await events(nodeToken)).body.events, card.id);
|
|
197
|
+
expect(e).toMatchObject({ field: "column", actor: { kind: "node", id: NODE } });
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
test("tag rename, project delete and card delete each record per-card events", async () => {
|
|
201
|
+
const p = await send(admin.token, "POST", `/api/requirements/projects?network_id=${NET}`, { name: `动态项目-${Date.now()}` });
|
|
202
|
+
expect(p.status).toBe(201);
|
|
203
|
+
const card = await create(admin.token, { name: "动态示例五", tags: ["旧标签"], project_id: p.body.project.id });
|
|
204
|
+
expect((await send(admin.token, "POST", `/api/requirements/tags/ops?network_id=${NET}`, { op: "rename", from: "旧标签", to: "新标签" })).status).toBe(200);
|
|
205
|
+
expect((await send(admin.token, "DELETE", `/api/requirements/projects/${p.body.project.id}?network_id=${NET}`)).status).toBe(200);
|
|
206
|
+
expect((await send(admin.token, "DELETE", `/api/requirements/${card.id}?network_id=${NET}`)).status).toBe(200);
|
|
207
|
+
const list = of((await events(admin.token)).body.events, card.id);
|
|
208
|
+
expect(list.slice(0, 3).map((e: any) => [e.kind, e.field])).toEqual([["deleted", null], ["changed", "project"], ["changed", "tags"]]);
|
|
209
|
+
expect(list[0]).toMatchObject({ title: "动态示例五", seq: expect.any(Number), actor: user(admin.id) });
|
|
210
|
+
expect(list[1]).toMatchObject({ old: p.body.project.id, new: null });
|
|
211
|
+
expect(list[2]).toMatchObject({ old: ["旧标签"], new: ["新标签"] });
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test("the write and its events are one transaction: no events table → the PATCH fails and the card is unchanged", async () => {
|
|
215
|
+
const { db } = await import("./db.js");
|
|
216
|
+
const card = await create(admin.token, { name: "动态示例六", priority: "low" });
|
|
217
|
+
db.exec("ALTER TABLE requirement_events RENAME TO requirement_events_away");
|
|
218
|
+
try {
|
|
219
|
+
const r = await patch(admin.token, card.id, { priority: "high" });
|
|
220
|
+
expect(r.status).toBeGreaterThanOrEqual(500);
|
|
221
|
+
} finally {
|
|
222
|
+
db.exec("ALTER TABLE requirement_events_away RENAME TO requirement_events");
|
|
223
|
+
}
|
|
224
|
+
const now = await send(admin.token, "GET", `/api/requirements/${card.id}?network_id=${NET}`);
|
|
225
|
+
expect(now.body.requirement.priority).toBe("low");
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
test("a scoped member sees events only for cards they can see — deleted ones by their tombstone", async () => {
|
|
229
|
+
const mine = await create(scoped.token, { name: "动态示例七(scoped 自己建的)" });
|
|
230
|
+
const theirs = await create(admin.token, { name: "动态示例八(scoped 看不见)" });
|
|
231
|
+
const joined = await create(admin.token, { name: "动态示例九(scoped 是参与人)", participants: [user(scoped.id)] });
|
|
232
|
+
await patch(admin.token, mine.id, { priority: "high" });
|
|
233
|
+
await patch(admin.token, theirs.id, { priority: "high" });
|
|
234
|
+
await send(admin.token, "DELETE", `/api/requirements/${joined.id}?network_id=${NET}`);
|
|
235
|
+
const seen = (await events(scoped.token, "&limit=500")).body.events;
|
|
236
|
+
expect(of(seen, mine.id).length).toBe(2);
|
|
237
|
+
expect(of(seen, theirs.id).length).toBe(0);
|
|
238
|
+
expect(of(seen, joined.id).map((e: any) => e.kind)).toEqual(["deleted", "created"]);
|
|
239
|
+
// a filter on a card they cannot see answers like a card with no events
|
|
240
|
+
expect((await events(scoped.token, `&requirement_id=${theirs.id}`)).body.events).toEqual([]);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
test("the CARD payload hides a hidden node's alias too (assignee is a string, not a ref)", async () => {
|
|
244
|
+
// 2026-10-31 —— #3285 的另一半,比事件列表**常用得多**的那一面。
|
|
245
|
+
// assignee 是派生展示字段(自由文本),写侧在设 agent_owner 时回填别名;
|
|
246
|
+
// 它没有 {kind,id} 形状 ⇒ isHiddenRef 判不了 ⇒ 只抹引用的话,卡列表上照样看得到别名。
|
|
247
|
+
const card = await create(admin.token, { name: "动态示例十之半" });
|
|
248
|
+
await patch(admin.token, card.id, { agent_owner: { kind: "node", id: NODE } });
|
|
249
|
+
const mine = (await send(admin.token, "GET", `/api/requirements?network_id=${NET}&limit=500`)).body.requirements
|
|
250
|
+
.find((c: any) => c.id === card.id);
|
|
251
|
+
expect(mine.assignee).toBe("ev-node"); // 无限制的读者:看得到(这是如实回显)
|
|
252
|
+
const theirs = (await send(restricted.token, "GET", `/api/requirements?network_id=${NET}&limit=500`)).body.requirements
|
|
253
|
+
.find((c: any) => c.id === card.id);
|
|
254
|
+
expect(theirs).toBeDefined(); // 卡本身他看得见(不是「看不见的卡」那条规则)
|
|
255
|
+
expect(theirs.assignee).toBeNull(); // 但执行人那一栏不能借别名漏出去
|
|
256
|
+
expect(JSON.stringify(theirs)).not.toContain("ev-node");
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
test("an agent-restricted member: hidden nodes are masked, and an event that only differs by a hidden node is dropped", async () => {
|
|
260
|
+
const card = await create(admin.token, { name: "动态示例十" });
|
|
261
|
+
await patch(nodeToken, card.id, { priority: "high" });
|
|
262
|
+
await patch(admin.token, card.id, { agent_owner: { kind: "node", id: NODE } });
|
|
263
|
+
await patch(admin.token, card.id, { participants: [user(admin.id), { kind: "node", id: NODE }] });
|
|
264
|
+
const seen = of((await events(restricted.token)).body.events, card.id);
|
|
265
|
+
const all = of((await events(admin.token)).body.events, card.id);
|
|
266
|
+
// 2026-10-31 —— 加了 `assignee` 这一条:它是**派生展示字段**(写侧在设 agent_owner 时
|
|
267
|
+
// 同步回填),存的是**别名字符串**而不是 {kind,id} 引用。列表顺序按事件 id 倒序 ⇒
|
|
268
|
+
// 它排在 agent_owner 之后、priority 之前。
|
|
269
|
+
expect(all.map((e: any) => e.field)).toEqual(["participants", "agent_owner", "assignee", "priority", null]);
|
|
270
|
+
expect(all[2]).toMatchObject({ field: "assignee", old: null, new: "ev-node" });
|
|
271
|
+
// 受限成员那边:agent_owner 与**由它派生的 assignee** 都必须消失(后者是别名字符串,
|
|
272
|
+
// 不走 isHiddenRef,2026-10-31 实测会漏;见 requirements.ts 的 hiddenAlias)。
|
|
273
|
+
expect(seen.map((e: any) => e.field)).toEqual(["participants", "priority", null]);
|
|
274
|
+
expect(seen[0]).toMatchObject({ old: [], new: [user(admin.id)] });
|
|
275
|
+
expect(seen[1]).toMatchObject({ field: "priority", actor: null });
|
|
276
|
+
expect(JSON.stringify(seen)).not.toContain(NODE);
|
|
277
|
+
expect(JSON.stringify(seen)).not.toContain("ev-node"); // ← 别名也不能漏
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
test("paging: newest first, limit + next_cursor without overlap; since; bad params 400", async () => {
|
|
281
|
+
const card = await create(admin.token, { name: "动态示例十一" });
|
|
282
|
+
for (const priority of ["high", "low", "lowest", "normal"]) await patch(admin.token, card.id, { priority });
|
|
283
|
+
const q = `&requirement_id=${card.id}`;
|
|
284
|
+
const p1 = await events(admin.token, `${q}&limit=2`);
|
|
285
|
+
expect(p1.body.events.length).toBe(2);
|
|
286
|
+
expect(p1.body.has_more).toBe(true);
|
|
287
|
+
expect(p1.body.events[0].new).toBe("normal");
|
|
288
|
+
const p2 = await events(admin.token, `${q}&limit=2&cursor=${p1.body.next_cursor}`);
|
|
289
|
+
const p3 = await events(admin.token, `${q}&limit=2&cursor=${p2.body.next_cursor}`);
|
|
290
|
+
const ids = [...p1.body.events, ...p2.body.events, ...p3.body.events].map((e: any) => e.id);
|
|
291
|
+
expect(new Set(ids).size).toBe(5);
|
|
292
|
+
expect(p3.body.has_more).toBe(false);
|
|
293
|
+
expect(p3.body.next_cursor).toBe(null);
|
|
294
|
+
expect(ids.map(Number)).toEqual([...ids.map(Number)].sort((a, b) => b - a));
|
|
295
|
+
// since = server_time of an earlier read → only what came after
|
|
296
|
+
const t = p1.body.server_time;
|
|
297
|
+
await patch(admin.token, card.id, { priority: "high" });
|
|
298
|
+
const after = await events(admin.token, `${q}&since=${encodeURIComponent(t)}`);
|
|
299
|
+
// since is inclusive (>=): an event written in the same millisecond as server_time comes back again,
|
|
300
|
+
// and clients dedupe by id. Only the events not already seen must be exactly the new one.
|
|
301
|
+
expect(after.body.events.every((e: any) => Date.parse(e.at) >= Date.parse(t))).toBe(true);
|
|
302
|
+
const seen = new Set(ids);
|
|
303
|
+
expect(after.body.events.filter((e: any) => !seen.has(e.id)).map((e: any) => e.new)).toEqual(["high"]);
|
|
304
|
+
for (const bad of ["&limit=0", "&limit=501", "&limit=x", "&since=nope", "&cursor=abc", "&cursor=0"]) {
|
|
305
|
+
expect((await events(admin.token, bad)).status).toBe(400);
|
|
306
|
+
}
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
test("migration is idempotent and leaves the rows alone", async () => {
|
|
310
|
+
const { db } = await import("./db.js");
|
|
311
|
+
const { ensureRequirementEvents } = await import("./requirement-events.js");
|
|
312
|
+
const n = Number(db.get<{ n: number }>("SELECT COUNT(*) AS n FROM requirement_events")!.n);
|
|
313
|
+
ensureRequirementEvents(db);
|
|
314
|
+
ensureRequirementEvents(db);
|
|
315
|
+
expect(Number(db.get<{ n: number }>("SELECT COUNT(*) AS n FROM requirement_events")!.n)).toBe(n);
|
|
316
|
+
expect(n).toBeGreaterThan(0);
|
|
317
|
+
});
|
|
318
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// diffRequirement 的边角(HTTP 层的写路径在 requirement-events-http.test.ts)。
|
|
2
|
+
import { describe, expect, test } from "bun:test";
|
|
3
|
+
import { diffRequirement, eventPublic, type EventCard } from "./requirement-events.js";
|
|
4
|
+
|
|
5
|
+
const base: EventCard = {
|
|
6
|
+
requirement_id: "req_1", network_id: "net_1", seq: 7, title: "示例", column_name: "pool", priority: "normal", due_on: null, start_on: null,
|
|
7
|
+
assignee: null, owner_json: null, agent_owner_json: null, participants_json: "[]", tags_json: "[]", checklist_json: "[]", description: null,
|
|
8
|
+
project_id: null, parent_id: null, archived: 0,
|
|
9
|
+
};
|
|
10
|
+
const ck = (items: { id: string; text: string; done?: boolean }[]) => JSON.stringify(items.map(i => ({ done: false, ...i })));
|
|
11
|
+
|
|
12
|
+
describe("diffRequirement", () => {
|
|
13
|
+
test("no change → no events; null vs empty string are the same", () => {
|
|
14
|
+
expect(diffRequirement(base, { ...base })).toEqual([]);
|
|
15
|
+
expect(diffRequirement(base, { ...base, due_on: "", assignee: "", description: "" })).toEqual([]);
|
|
16
|
+
});
|
|
17
|
+
test("created carries title and column only", () => {
|
|
18
|
+
expect(diffRequirement(null, base)).toEqual([{ kind: "created", field: null, old: null, new: { title: "示例", column: "pool" } }]);
|
|
19
|
+
});
|
|
20
|
+
test("checklist: reorder, rename, add or remove is one `checklist` event with counts", () => {
|
|
21
|
+
const a = { ...base, checklist_json: ck([{ id: "a", text: "一" }, { id: "b", text: "二", done: true }]) };
|
|
22
|
+
for (const next of [ck([{ id: "b", text: "二", done: true }, { id: "a", text: "一" }]), ck([{ id: "a", text: "一改" }, { id: "b", text: "二", done: true }]), ck([{ id: "a", text: "一" }])]) {
|
|
23
|
+
const out = diffRequirement(a, { ...a, checklist_json: next });
|
|
24
|
+
expect(out.length).toBe(1);
|
|
25
|
+
expect(out[0].field).toBe("checklist");
|
|
26
|
+
}
|
|
27
|
+
expect(diffRequirement(a, { ...a, checklist_json: ck([{ id: "a", text: "一" }]) })[0]).toMatchObject({ old: { total: 2, done: 1 }, new: { total: 1, done: 0 } });
|
|
28
|
+
});
|
|
29
|
+
test("checklist: two items ticked in one write are two `checklist_item` events", () => {
|
|
30
|
+
const a = { ...base, checklist_json: ck([{ id: "a", text: "一" }, { id: "b", text: "二" }]) };
|
|
31
|
+
const out = diffRequirement(a, { ...a, checklist_json: ck([{ id: "a", text: "一", done: true }, { id: "b", text: "二", done: true }]) });
|
|
32
|
+
expect(out.map(e => [e.field, (e.new as { id: string }).id])).toEqual([["checklist_item", "a"], ["checklist_item", "b"]]);
|
|
33
|
+
});
|
|
34
|
+
test("malformed stored JSON reads as empty rather than throwing", () => {
|
|
35
|
+
expect(() => diffRequirement({ ...base, tags_json: "{nope", participants_json: "x" }, base)).not.toThrow();
|
|
36
|
+
});
|
|
37
|
+
test("description records lengths, never text", () => {
|
|
38
|
+
const [e] = diffRequirement(base, { ...base, description: "secret text" });
|
|
39
|
+
expect(e).toEqual({ kind: "changed", field: "description", old: { chars: 0 }, new: { chars: 11 } });
|
|
40
|
+
});
|
|
41
|
+
test("eventPublic: bigint / string ids from PostgreSQL come out as strings, seq as a number", () => {
|
|
42
|
+
const row = { id: 12n as unknown as string, network_id: "n", requirement_id: "r", seq: "3", title: "t", actor_json: null, kind: "changed", field: "priority", old_json: "\"low\"", new_json: "\"high\"", created_at: "2026-10-01T00:00:00.000Z" };
|
|
43
|
+
expect(eventPublic(row)).toMatchObject({ id: "12", seq: 3, old: "low", new: "high", actor: null });
|
|
44
|
+
});
|
|
45
|
+
});
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// 任务动态(#429,Vincent 2026-09-30「任务有新的状态…这完成了,这是有这种动态」)。
|
|
2
|
+
//
|
|
3
|
+
// requirements 表只存「现在是什么」和「最后是谁改的」(updated_by_json),改之前是什么没有任何地方留着 ——
|
|
4
|
+
// 「优先级 P2 → P0」「状态 需求池 → 进行中」画不出来。这里给每一次写入记字段级的流水:谁(用户 / 节点)、
|
|
5
|
+
// 什么时候、哪个字段、旧值 → 新值。写入点在 requirements.ts 的各条写路径里,和那次 UPDATE 在同一个事务里。
|
|
6
|
+
//
|
|
7
|
+
// 表只加不改:旧 Hub(回滚之后)不认识这张表,也不会碰它;再升回来接着记(回滚期间的改动没有流水)。
|
|
8
|
+
// 读:GET /api/requirements/events(requirements.ts,可见范围与列表相同),capability `events`。
|
|
9
|
+
import type { DbAdapter } from "./db-adapter.js";
|
|
10
|
+
|
|
11
|
+
/** 流水保留多久。更早的在写入时顺手清掉(每 PRUNE_EVERY 次写一次)。 */
|
|
12
|
+
export const EVENTS_RETENTION_MS = 180 * 86_400_000;
|
|
13
|
+
const PRUNE_EVERY = 500;
|
|
14
|
+
|
|
15
|
+
/** IF NOT EXISTS:重复执行无副作用;SQLite / PostgreSQL 同一段(适配层把 AUTOINCREMENT 译成 BIGSERIAL)。 */
|
|
16
|
+
export function ensureRequirementEvents(database: DbAdapter): void {
|
|
17
|
+
database.exec(`
|
|
18
|
+
CREATE TABLE IF NOT EXISTS requirement_events (
|
|
19
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
20
|
+
network_id TEXT NOT NULL,
|
|
21
|
+
requirement_id TEXT NOT NULL,
|
|
22
|
+
seq INTEGER,
|
|
23
|
+
title TEXT,
|
|
24
|
+
actor_json TEXT,
|
|
25
|
+
kind TEXT NOT NULL,
|
|
26
|
+
field TEXT,
|
|
27
|
+
old_json TEXT,
|
|
28
|
+
new_json TEXT,
|
|
29
|
+
created_at TEXT NOT NULL
|
|
30
|
+
);
|
|
31
|
+
`);
|
|
32
|
+
database.exec("CREATE INDEX IF NOT EXISTS idx_requirement_events_network ON requirement_events(network_id, id)");
|
|
33
|
+
database.exec("CREATE INDEX IF NOT EXISTS idx_requirement_events_card ON requirement_events(requirement_id, id)");
|
|
34
|
+
database.exec("CREATE INDEX IF NOT EXISTS idx_requirement_events_created ON requirement_events(created_at)");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** 流水要比较的列(requirements.ts 的 Row 的子集)。 */
|
|
38
|
+
export type EventCard = {
|
|
39
|
+
requirement_id: string;
|
|
40
|
+
network_id: string;
|
|
41
|
+
seq: number | null;
|
|
42
|
+
title: string;
|
|
43
|
+
column_name: string;
|
|
44
|
+
priority: string;
|
|
45
|
+
due_on: string | null;
|
|
46
|
+
start_on: string | null;
|
|
47
|
+
assignee: string | null;
|
|
48
|
+
owner_json: string | null;
|
|
49
|
+
agent_owner_json: string | null;
|
|
50
|
+
participants_json: string | null;
|
|
51
|
+
tags_json: string | null;
|
|
52
|
+
checklist_json: string | null;
|
|
53
|
+
description: string | null;
|
|
54
|
+
project_id: string | null;
|
|
55
|
+
parent_id: string | null;
|
|
56
|
+
archived: number | null;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export type EventKind = "created" | "changed" | "deleted" | "comment";
|
|
60
|
+
/**
|
|
61
|
+
* field(kind = changed):column / title / priority / due / start / assignee / owner / agent_owner / participants /
|
|
62
|
+
* tags / checklist_item(勾选或取消一项:{id,text,done})/ checklist(增删改条目:{total,done})/
|
|
63
|
+
* description(只记字数,不存正文)/ project / parent / archived。
|
|
64
|
+
* kind = comment(#474):field 为 null,new = { text }(评论正文,≤ 4000 字),只追加,不对应任何字段的改动。
|
|
65
|
+
*/
|
|
66
|
+
export type EventDraft = { kind: EventKind; field: string | null; old: unknown; new: unknown };
|
|
67
|
+
|
|
68
|
+
const parse = (json: string | null | undefined): unknown => {
|
|
69
|
+
if (!json) return null;
|
|
70
|
+
try { return JSON.parse(json); } catch { return null; }
|
|
71
|
+
};
|
|
72
|
+
const list = (json: string | null | undefined): unknown[] => { const v = parse(json); return Array.isArray(v) ? v : []; };
|
|
73
|
+
const same = (a: unknown, b: unknown) => JSON.stringify(a) === JSON.stringify(b);
|
|
74
|
+
type Item = { id: string; text: string; done: boolean };
|
|
75
|
+
const items = (json: string | null | undefined): Item[] =>
|
|
76
|
+
list(json).filter((x): x is Item => !!x && typeof (x as Item).id === "string" && typeof (x as Item).text === "string").map(x => ({ id: x.id, text: x.text, done: !!x.done }));
|
|
77
|
+
const counts = (xs: Item[]) => ({ total: xs.length, done: xs.filter(x => x.done).length });
|
|
78
|
+
|
|
79
|
+
/** 一次写入前后的差异。before = null:新建(只记一条 created)。 */
|
|
80
|
+
export function diffRequirement(before: EventCard | null, after: EventCard): EventDraft[] {
|
|
81
|
+
if (!before) return [{ kind: "created", field: null, old: null, new: { title: after.title, column: after.column_name } }];
|
|
82
|
+
const out: EventDraft[] = [];
|
|
83
|
+
const scalar = (field: string, a: unknown, b: unknown) => { if (!same(a, b)) out.push({ kind: "changed", field, old: a, new: b }); };
|
|
84
|
+
scalar("column", before.column_name, after.column_name);
|
|
85
|
+
scalar("title", before.title, after.title);
|
|
86
|
+
scalar("priority", before.priority, after.priority);
|
|
87
|
+
scalar("due", before.due_on || null, after.due_on || null);
|
|
88
|
+
scalar("start", before.start_on || null, after.start_on || null);
|
|
89
|
+
scalar("assignee", before.assignee || null, after.assignee || null);
|
|
90
|
+
scalar("owner", parse(before.owner_json), parse(after.owner_json));
|
|
91
|
+
scalar("agent_owner", parse(before.agent_owner_json), parse(after.agent_owner_json));
|
|
92
|
+
scalar("participants", list(before.participants_json), list(after.participants_json));
|
|
93
|
+
scalar("tags", list(before.tags_json), list(after.tags_json));
|
|
94
|
+
// 子任务:同一条(同 id、同文字)只是勾选变了 = 一条 checklist_item;条目增删 / 改字 / 换顺序 = 一条 checklist。
|
|
95
|
+
const a = items(before.checklist_json), b = items(after.checklist_json);
|
|
96
|
+
const shape = (xs: Item[]) => xs.map(x => `${x.id}\u0000${x.text}`).join("\u0001");
|
|
97
|
+
if (shape(a) === shape(b)) {
|
|
98
|
+
for (let i = 0; i < b.length; i++) if (a[i].done !== b[i].done) out.push({ kind: "changed", field: "checklist_item", old: a[i], new: b[i] });
|
|
99
|
+
} else {
|
|
100
|
+
out.push({ kind: "changed", field: "checklist", old: counts(a), new: counts(b) });
|
|
101
|
+
}
|
|
102
|
+
if ((before.description || "") !== (after.description || "")) {
|
|
103
|
+
out.push({ kind: "changed", field: "description", old: { chars: (before.description || "").length }, new: { chars: (after.description || "").length } });
|
|
104
|
+
}
|
|
105
|
+
scalar("project", before.project_id || null, after.project_id || null);
|
|
106
|
+
scalar("parent", before.parent_id || null, after.parent_id || null);
|
|
107
|
+
scalar("archived", !!before.archived, !!after.archived);
|
|
108
|
+
return out;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
let writes = 0;
|
|
112
|
+
|
|
113
|
+
/** 记下一张卡的一组流水(调用方负责放进同一个事务)。actorJson = updated_by_json 的那个值。 */
|
|
114
|
+
export function recordRequirementEvents(database: DbAdapter, card: Pick<EventCard, "requirement_id" | "network_id" | "seq" | "title">, actorJson: string | null, drafts: readonly EventDraft[], at: string): void {
|
|
115
|
+
for (const d of drafts) {
|
|
116
|
+
database.run(
|
|
117
|
+
`INSERT INTO requirement_events (network_id, requirement_id, seq, title, actor_json, kind, field, old_json, new_json, created_at)
|
|
118
|
+
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)`,
|
|
119
|
+
[card.network_id, card.requirement_id, card.seq ?? null, card.title, actorJson && actorJson !== "null" ? actorJson : null, d.kind, d.field,
|
|
120
|
+
d.old === null || d.old === undefined ? null : JSON.stringify(d.old), d.new === null || d.new === undefined ? null : JSON.stringify(d.new), at],
|
|
121
|
+
);
|
|
122
|
+
writes++;
|
|
123
|
+
}
|
|
124
|
+
if (drafts.length && writes >= PRUNE_EVERY) {
|
|
125
|
+
writes = 0;
|
|
126
|
+
database.run("DELETE FROM requirement_events WHERE created_at < ?1", [new Date(Date.parse(at) - EVENTS_RETENTION_MS).toISOString()]);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export type EventRow = { id: number | string; network_id: string; requirement_id: string; seq: number | string | null; title: string | null; actor_json: string | null; kind: string; field: string | null; old_json: string | null; new_json: string | null; created_at: string };
|
|
131
|
+
|
|
132
|
+
/** 对外的一条(id 转成字符串:PostgreSQL 的 BIGSERIAL 可能回成字符串或 bigint,客户端一律按字符串比较 / 翻页)。 */
|
|
133
|
+
export const eventPublic = (row: EventRow) => ({
|
|
134
|
+
id: String(row.id),
|
|
135
|
+
requirement_id: row.requirement_id,
|
|
136
|
+
seq: row.seq == null ? null : Number(row.seq),
|
|
137
|
+
title: row.title,
|
|
138
|
+
actor: parse(row.actor_json),
|
|
139
|
+
kind: row.kind,
|
|
140
|
+
field: row.field,
|
|
141
|
+
old: parse(row.old_json),
|
|
142
|
+
new: parse(row.new_json),
|
|
143
|
+
at: row.created_at,
|
|
144
|
+
});
|