@sleep2agi/commhub-server 0.9.0-preview.13 → 0.9.0-preview.130
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 +130 -10
- package/bin/commhub.ts +99 -11
- package/package.json +7 -7
- package/src/ack-create-request-transport.test.ts +182 -0
- package/src/ack-create-request.test.ts +50 -7
- 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 +137 -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 +659 -0
- package/src/agent-acl-slice3-http.test.ts +267 -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 +323 -0
- package/src/agent-teams.ts +244 -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 +271 -0
- package/src/api-host-supervisors-runtime-readiness.test.ts +195 -0
- package/src/api-nodes-shape.test.ts +26 -10
- package/src/auth-password-change-cli-tokens-http.test.ts +294 -0
- package/src/auth-sessions-http.test.ts +250 -0
- package/src/auth-validate.test.ts +5 -1
- package/src/auth-weak-password-login.test.ts +176 -0
- package/src/auth.ts +598 -49
- package/src/avatar-validate.test.ts +163 -0
- package/src/avatar-validate.ts +103 -0
- package/src/binding-generation-http.test.ts +121 -0
- package/src/blocked-exit-on-reply.test.ts +122 -0
- package/src/boot-side-effect.test.ts +124 -0
- package/src/clone-node.test.ts +248 -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-sec1.test.ts +107 -0
- package/src/config-apply-validate.test.ts +134 -1
- package/src/config-apply-validate.ts +111 -5
- package/src/create-node-blocked-gate.test.ts +356 -0
- package/src/create-node-network-id.test.ts +79 -0
- package/src/create-node-tool-schema.test.ts +48 -0
- package/src/create-node-unicode-name.test.ts +120 -0
- package/src/create-node-validate.test.ts +193 -7
- package/src/create-node-validate.ts +227 -15
- package/src/create-node-workdir.test.ts +289 -0
- package/src/create-node.ts +77 -23
- package/src/daemon-provider-snapshots.test.ts +421 -0
- package/src/daemon-provider-snapshots.ts +327 -0
- package/src/daemon-token-owner-http.test.ts +236 -0
- package/src/dashboard-slash-routing-http.test.ts +107 -0
- package/src/db-adapter-guard.test.ts +148 -0
- package/src/db-adapter-translate.test.ts +128 -0
- package/src/db-adapter.ts +323 -87
- package/src/db-timestamp.test.ts +38 -0
- package/src/db-timestamp.ts +15 -0
- package/src/db.ts +930 -35
- 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 +330 -0
- package/src/department-heads.ts +226 -0
- package/src/departments-http.test.ts +185 -0
- package/src/departments.ts +260 -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/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 +135 -0
- package/src/file-download-authz.test.ts +404 -0
- package/src/file-download-date-bucket.test.ts +436 -0
- 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 +482 -0
- package/src/group-messages.ts +297 -0
- package/src/health-redaction.test.ts +642 -0
- package/src/http-gzip.test.ts +94 -0
- package/src/http-gzip.ts +112 -0
- package/src/hub-provider-config.test.ts +139 -0
- package/src/hub-provider-config.ts +105 -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/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 +15 -2362
- package/src/lifecycle-guard.ts +1 -1
- package/src/mcp-context-cost-http.test.ts +123 -0
- package/src/mcp-projects-events-http.test.ts +162 -0
- package/src/mcp-route-label-http.test.ts +120 -0
- package/src/mcp-write-network-resolution.test.ts +432 -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-scope.ts +276 -0
- package/src/network-token-membership-http.test.ts +139 -0
- package/src/node-alias-control.ts +12 -0
- package/src/node-attrs-validate.ts +88 -0
- package/src/node-attrs.test.ts +217 -0
- package/src/node-caller-identity-http.test.ts +781 -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 +130 -0
- package/src/node-files-transport.test.ts +344 -0
- package/src/node-health-codex-login.test.ts +120 -0
- package/src/node-health-dispatch-http.test.ts +279 -0
- package/src/node-health-guard.ts +105 -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-lifecycle-controllable.test.ts +64 -0
- package/src/node-lifecycle-controllable.ts +53 -0
- package/src/node-lifecycle-read-http.test.ts +245 -0
- package/src/node-lifecycle-read.ts +121 -0
- package/src/node-logs-transport.test.ts +263 -0
- package/src/node-permissions-http.test.ts +276 -0
- package/src/node-permissions.ts +285 -0
- package/src/node-request-retention.test.ts +281 -0
- package/src/node-request-retention.ts +120 -0
- package/src/node-skills-transport.test.ts +209 -0
- package/src/node-token-ownership.ts +51 -0
- package/src/node-token-privilege-http.test.ts +158 -0
- package/src/observer-avatar-http.test.ts +431 -0
- package/src/observer-push.test.ts +252 -0
- package/src/opencode-v2-catalog.test.ts +268 -0
- package/src/opencode-v2-catalog.ts +352 -0
- package/src/package-version-consistency.test.ts +25 -0
- package/src/peer-reply-atomic.test.ts +455 -0
- package/src/peer-reply-capability.test.ts +70 -0
- package/src/pg-worker.ts +87 -0
- package/src/project-dir-warn-clear-http.test.ts +132 -0
- package/src/project-viewer-can-http.test.ts +114 -0
- package/src/push.ts +429 -10
- package/src/redact-tokens.test.ts +65 -0
- package/src/redact-tokens.ts +40 -0
- package/src/rename.ts +22 -2
- 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 +150 -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 +268 -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 +266 -0
- package/src/requirement-tags.ts +89 -0
- package/src/requirements-abandoned-http.test.ts +236 -0
- package/src/requirements-comment-http.test.ts +167 -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 +143 -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 +319 -0
- package/src/requirements-mcp.test.ts +163 -0
- package/src/requirements-migrate.ts +259 -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 +225 -0
- package/src/requirements-people.ts +127 -0
- package/src/requirements-priority-lowest.test.ts +114 -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 +288 -0
- package/src/requirements-stats.ts +167 -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 +1806 -0
- package/src/resolve-port.test.ts +57 -0
- package/src/resolve-port.ts +44 -0
- package/src/response-charset.test.ts +130 -0
- package/src/rest-explicit-columns-http.test.ts +222 -0
- package/src/rest-identity.test.ts +129 -0
- package/src/rest-identity.ts +125 -0
- package/src/rest-projections.ts +68 -0
- package/src/rest-write-network-resolution.test.ts +133 -0
- package/src/restricted-files.ts +76 -0
- package/src/retention.ts +15 -3
- package/src/route-callers-http.test.ts +120 -0
- package/src/route-timing.test.ts +211 -0
- package/src/route-timing.ts +258 -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 +381 -0
- package/src/schedule-agent.ts +192 -0
- package/src/scheduled-doorbell-nodeless-session.test.ts +134 -0
- package/src/scheduled-failures-http.test.ts +233 -0
- package/src/scheduled-failures.ts +179 -0
- package/src/scheduled-offline.test.ts +131 -0
- package/src/scheduled-offline.ts +66 -0
- package/src/scheduled-reply-unread-http.test.ts +212 -0
- package/src/scheduled-run-terminal.test.ts +268 -0
- package/src/scheduled-skip-blocking-task.test.ts +120 -0
- package/src/scheduled-stuck-http.test.ts +238 -0
- package/src/scheduled-stuck.ts +153 -0
- package/src/scheduled-tasks-http.test.ts +638 -0
- package/src/scheduled-tasks.ts +766 -0
- package/src/scoped-secrets.ts +124 -0
- package/src/send-dedup-request-id.test.ts +229 -0
- package/src/send-reply-agent-warning.test.ts +202 -0
- 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 +38 -0
- package/src/serve-error.ts +27 -0
- package/src/server.ts +5257 -0
- 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/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 +108 -0
- package/src/sqlite-close-checkpoint.test.ts +78 -0
- package/src/start-node.test.ts +264 -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 +122 -0
- package/src/stop-delete-node.test.ts +509 -1
- package/src/task-access-http.test.ts +402 -0
- package/src/task-access.ts +420 -0
- package/src/task-auth-origin-http.test.ts +91 -0
- package/src/task-auth-origin.test.ts +24 -0
- package/src/task-auth-origin.ts +7 -0
- package/src/task-consumption.test.ts +482 -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 +315 -0
- package/src/task-idempotency-mcp.test.ts +142 -0
- package/src/task-idempotency.test.ts +60 -0
- package/src/task-idempotency.ts +75 -0
- package/src/task-late-replies.ts +54 -0
- package/src/task-late-reply-http.test.ts +152 -0
- package/src/task-lifecycle-watcher.test.ts +204 -0
- package/src/task-lifecycle-watcher.ts +49 -0
- package/src/task-network-resolution-http.test.ts +141 -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 +49 -0
- package/src/task-stale-open-http.test.ts +290 -0
- package/src/task-stale-open.ts +103 -0
- package/src/tasks-pagination-http.test.ts +141 -0
- package/src/tool-audience-http.test.ts +220 -0
- package/src/tool-audience.ts +118 -0
- package/src/tools-list-json-schema.test.ts +67 -0
- package/src/tools.ts +3927 -323
- package/src/upload-early-reject-guard.test.ts +155 -0
- package/src/uploads-http.test.ts +97 -15
- package/src/uploads.ts +103 -6
- package/src/user-avatar-http.test.ts +74 -0
- package/src/user-avatar-migration.test.ts +25 -0
- package/src/user-inbox-by-agent-http.test.ts +161 -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
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
// Agent 分组(RFC-038 §8)—— HTTP 集成测试(真实 Bun.serve,私有端口,临时库)。
|
|
2
|
+
//
|
|
3
|
+
// 场景:Hub 管理员 admin 的网络 NET 里有 3 个 Agent(X、Y、Z);受限成员 alice 没有任何直接授权。
|
|
4
|
+
// 正向:组授权 ⇒ 看得见、能派活;往组里加节点 ⇒ 下一次请求就可见(动态);直接授权与组授权取并集。
|
|
5
|
+
// 反向:移出组 ⇒ 403 且与不存在的 alias 逐字节相同;删组 ⇒ 访问消失;跨网络 id ⇒ 400;
|
|
6
|
+
// 节点令牌 / 受限成员碰组接口 ⇒ 403。
|
|
7
|
+
// 兼容:旧 app 形状的 PUT(只带 grants)不动组授权;agent_access='all' 的成员不看组。
|
|
8
|
+
|
|
9
|
+
import { describe, expect, test, beforeAll, afterAll } from "bun:test";
|
|
10
|
+
import { mkdtempSync, rmSync } from "fs";
|
|
11
|
+
import { join } from "path";
|
|
12
|
+
import { tmpdir } from "os";
|
|
13
|
+
import { register } from "./auth.js";
|
|
14
|
+
import { db } from "./db.js";
|
|
15
|
+
|
|
16
|
+
const DIR = mkdtempSync(join(tmpdir(), "anet-agent-groups-"));
|
|
17
|
+
let BASE = "";
|
|
18
|
+
let hub: any = null;
|
|
19
|
+
|
|
20
|
+
const PW = "GroupTestPassw0rd!x";
|
|
21
|
+
let adminToken = "";
|
|
22
|
+
let adminId = "";
|
|
23
|
+
let NET = "";
|
|
24
|
+
let OTHER_NET = "";
|
|
25
|
+
let aliceToken = "";
|
|
26
|
+
let aliceId = "";
|
|
27
|
+
let carolToken = "";
|
|
28
|
+
let carolId = "";
|
|
29
|
+
let nodeToken = "";
|
|
30
|
+
|
|
31
|
+
const X = { alias: "grp-agent-x", node: "node_grp_x" };
|
|
32
|
+
const Y = { alias: "grp-agent-y", node: "node_grp_y" };
|
|
33
|
+
const Z = { alias: "grp-agent-z", node: "node_grp_z" };
|
|
34
|
+
const FOREIGN = { alias: "grp-agent-foreign", node: "node_grp_foreign" };
|
|
35
|
+
|
|
36
|
+
const auth = (token: string) => ({ Authorization: `Bearer ${token}` });
|
|
37
|
+
const json = (token: string) => ({ ...auth(token), "Content-Type": "application/json" });
|
|
38
|
+
|
|
39
|
+
function seedAgent(a: { alias: string; node: string }, net: string) {
|
|
40
|
+
db.run(
|
|
41
|
+
`INSERT INTO nodes (node_id, node_name, alias, network_id, owner_user_id, updated_at) VALUES (?1, ?2, ?2, ?3, ?4, datetime('now'))`,
|
|
42
|
+
[a.node, a.alias, net, adminId],
|
|
43
|
+
);
|
|
44
|
+
db.run(
|
|
45
|
+
`INSERT INTO sessions (resume_id, alias, node_id, status, network_id, updated_at, last_seen_at)
|
|
46
|
+
VALUES (?1, ?2, ?3, 'idle', ?4, datetime('now'), datetime('now'))`,
|
|
47
|
+
[`resume_${a.node}`, a.alias, a.node, net],
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function send(token: string, method: string, path: string, payload?: unknown): Promise<{ status: number; body: any; text: string }> {
|
|
52
|
+
const res = await fetch(`${BASE}${path}`, { method, headers: json(token), body: payload === undefined ? undefined : JSON.stringify(payload) });
|
|
53
|
+
const text = await res.text();
|
|
54
|
+
let body: any = null;
|
|
55
|
+
try { body = JSON.parse(text); } catch {}
|
|
56
|
+
return { status: res.status, body, text };
|
|
57
|
+
}
|
|
58
|
+
const get = (token: string, path: string) => send(token, "GET", path);
|
|
59
|
+
const seen = async (token: string) => {
|
|
60
|
+
const r = await get(token, `/api/status?network_id=${NET}`);
|
|
61
|
+
expect(r.status).toBe(200);
|
|
62
|
+
return (r.body.sessions ?? []).map((s: any) => s.alias).filter((a: string) => a.startsWith("grp-agent-")).sort();
|
|
63
|
+
};
|
|
64
|
+
// 每次内容不同:hub 按内容哈希 5 分钟去重(send_dedup),同样的探针第二次会 429。
|
|
65
|
+
let probeSeq = 0;
|
|
66
|
+
const task = (token: string, alias: string) => send(token, "POST", "/api/task", { alias, task: `probe ${alias} #${++probeSeq}`, network_id: NET });
|
|
67
|
+
const grantsPath = (uid: string) => `/api/networks/${NET}/members/${uid}/agent-grants`;
|
|
68
|
+
const groupsPath = `/api/networks/${"__NET__"}/agent-groups`;
|
|
69
|
+
const gp = (suffix = "") => groupsPath.replace("__NET__", NET) + suffix;
|
|
70
|
+
|
|
71
|
+
beforeAll(async () => {
|
|
72
|
+
process.env.COMMHUB_DB ||= join(DIR, "hub.db");
|
|
73
|
+
process.env.COMMHUB_UPLOADS_DIR = join(DIR, "uploads");
|
|
74
|
+
process.env.HOST = "127.0.0.1";
|
|
75
|
+
|
|
76
|
+
const admin = register(`grp_admin_${Date.now()}`, PW, undefined, "Admin");
|
|
77
|
+
expect(admin.ok).toBe(true);
|
|
78
|
+
adminToken = admin.token!;
|
|
79
|
+
adminId = admin.user!.user_id;
|
|
80
|
+
NET = admin.network_id!;
|
|
81
|
+
db.run("UPDATE users SET role = 'admin' WHERE user_id = ?1", [adminId]);
|
|
82
|
+
for (const a of [X, Y, Z]) seedAgent(a, NET);
|
|
83
|
+
|
|
84
|
+
const mod: any = await import("./server.js");
|
|
85
|
+
hub = mod.bootServer({ port: 0, hostname: "127.0.0.1" });
|
|
86
|
+
BASE = `http://127.0.0.1:${hub.port}`;
|
|
87
|
+
|
|
88
|
+
// 另一个网络(admin 也是 owner),放一个外网节点,用来测跨网络 id。
|
|
89
|
+
const other = await send(adminToken, "POST", "/api/networks", { name: `grp-other-${Date.now()}` });
|
|
90
|
+
expect(other.status).toBe(200);
|
|
91
|
+
OTHER_NET = other.body.network_id ?? other.body.network?.network_id;
|
|
92
|
+
expect(typeof OTHER_NET).toBe("string");
|
|
93
|
+
seedAgent(FOREIGN, OTHER_NET);
|
|
94
|
+
|
|
95
|
+
const mk = async (username: string, agentAccess?: "all") => {
|
|
96
|
+
const r = await send(adminToken, "POST", "/api/admin/users", { username, password: PW, network_id: NET, role: "member" });
|
|
97
|
+
expect(r.status).toBe(200);
|
|
98
|
+
const login = await send("", "POST", "/api/auth/login", { username, password: PW });
|
|
99
|
+
expect(login.status).toBe(200);
|
|
100
|
+
if (agentAccess) db.run("UPDATE network_members SET agent_access = 'all' WHERE network_id = ?1 AND user_id = ?2", [NET, login.body.user.user_id]);
|
|
101
|
+
return { token: login.body.token as string, id: login.body.user.user_id as string };
|
|
102
|
+
};
|
|
103
|
+
const alice = await mk(`grp_alice_${Date.now()}`);
|
|
104
|
+
aliceToken = alice.token; aliceId = alice.id;
|
|
105
|
+
const carol = await mk(`grp_carol_${Date.now()}`, "all");
|
|
106
|
+
carolToken = carol.token; carolId = carol.id;
|
|
107
|
+
|
|
108
|
+
const nt = await send(adminToken, "POST", "/api/auth/node-token", { network_id: NET, node_name: X.alias, node_id: X.node });
|
|
109
|
+
expect(nt.status).toBe(200);
|
|
110
|
+
nodeToken = nt.body.token;
|
|
111
|
+
}, 30_000);
|
|
112
|
+
|
|
113
|
+
afterAll(() => {
|
|
114
|
+
try { hub?.stop?.(true); } catch {}
|
|
115
|
+
try { rmSync(DIR, { recursive: true, force: true }); } catch {}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
let groupId = "";
|
|
119
|
+
|
|
120
|
+
describe("Agent 分组:CRUD 与权限", () => {
|
|
121
|
+
test("owner 建组(带成员);重名 409;外网节点 400 且整批不写", async () => {
|
|
122
|
+
const r = await send(adminToken, "POST", gp(), { name: "前端组", node_ids: [X.node] });
|
|
123
|
+
expect(r.status).toBe(200);
|
|
124
|
+
groupId = r.body.group.group_id;
|
|
125
|
+
expect(r.body.group.node_ids).toEqual([X.node]);
|
|
126
|
+
expect((await send(adminToken, "POST", gp(), { name: "前端组" })).status).toBe(409);
|
|
127
|
+
const bad = await send(adminToken, "POST", gp(), { name: "坏组", node_ids: [Y.node, FOREIGN.node] });
|
|
128
|
+
expect(bad.status).toBe(400);
|
|
129
|
+
expect(bad.body.error).toBe("agent_not_in_network");
|
|
130
|
+
const list = await get(adminToken, gp());
|
|
131
|
+
expect(list.body.groups.map((g: any) => g.name)).toEqual(["前端组"]);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test("受限成员、节点令牌碰组接口 ⇒ 403", async () => {
|
|
135
|
+
expect((await get(aliceToken, gp())).status).toBe(403);
|
|
136
|
+
expect((await send(aliceToken, "POST", gp(), { name: "偷偷建" })).status).toBe(403);
|
|
137
|
+
expect((await get(nodeToken, gp())).status).toBe(403);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test("审计:建组写 agent_group_created(带 network_id)", () => {
|
|
141
|
+
const row = db.get<{ network_id: string; detail: string }>("SELECT network_id, detail FROM audit_log WHERE action = 'agent_group_created' AND target_id = ?1", groupId);
|
|
142
|
+
expect(row?.network_id).toBe(NET);
|
|
143
|
+
expect(row?.detail).toContain("前端组");
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
describe("组授权:动态生效", () => {
|
|
148
|
+
test("基线:alice 没有任何授权 ⇒ 看不到、发不了", async () => {
|
|
149
|
+
expect(await seen(aliceToken)).toEqual([]);
|
|
150
|
+
expect((await task(aliceToken, X.alias)).status).toBe(403);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("授权给组 ⇒ 看到组里的 X、能派活;GET 回 group_grants", async () => {
|
|
154
|
+
const put = await send(adminToken, "PUT", grantsPath(aliceId), { group_grants: [{ group_id: groupId, can_message: true }] });
|
|
155
|
+
expect(put.status).toBe(200);
|
|
156
|
+
expect(put.body.group_grants).toEqual([{ group_id: groupId, name: "前端组", can_message: true }]);
|
|
157
|
+
expect(put.body.restricted).toBe(true);
|
|
158
|
+
expect(await seen(aliceToken)).toEqual([X.alias]);
|
|
159
|
+
expect((await task(aliceToken, X.alias)).status).toBe(200);
|
|
160
|
+
expect((await task(aliceToken, Y.alias)).status).toBe(403);
|
|
161
|
+
const g = await get(adminToken, grantsPath(aliceId));
|
|
162
|
+
expect(g.body.grants).toEqual([]);
|
|
163
|
+
expect(g.body.group_grants.length).toBe(1);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("往组里加 Y ⇒ alice 下一次请求就看得见 Y(不用改她的授权)", async () => {
|
|
167
|
+
const r = await send(adminToken, "PUT", gp(`/${groupId}/members`), { node_ids: [X.node, Y.node] });
|
|
168
|
+
expect(r.status).toBe(200);
|
|
169
|
+
expect(r.body.added).toEqual([Y.node]);
|
|
170
|
+
expect(await seen(aliceToken)).toEqual([X.alias, Y.alias]);
|
|
171
|
+
expect((await task(aliceToken, Y.alias)).status).toBe(200);
|
|
172
|
+
const audit = db.get<{ detail: string }>("SELECT detail FROM audit_log WHERE action = 'agent_group_members_changed' AND target_id = ?1 ORDER BY id DESC LIMIT 1", groupId);
|
|
173
|
+
expect(JSON.parse(audit!.detail)).toEqual({ added: [Y.node], removed: [] });
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test("从组里移出 Y ⇒ 看不见,派活 403 且与不存在的 alias 逐字节相同", async () => {
|
|
177
|
+
await send(adminToken, "PUT", gp(`/${groupId}/members`), { node_ids: [X.node] });
|
|
178
|
+
expect(await seen(aliceToken)).toEqual([X.alias]);
|
|
179
|
+
const y = await task(aliceToken, Y.alias);
|
|
180
|
+
const ghost = await task(aliceToken, "grp-agent-does-not-exist");
|
|
181
|
+
expect(y.status).toBe(403);
|
|
182
|
+
expect(y.text).toBe(ghost.text);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test("直接授权与组授权取并集;可对话任一来源给了就算", async () => {
|
|
186
|
+
// 组授权只读,Z 直接授权可对话
|
|
187
|
+
const put = await send(adminToken, "PUT", grantsPath(aliceId), {
|
|
188
|
+
grants: [{ node_id: Z.node, can_message: true }],
|
|
189
|
+
group_grants: [{ group_id: groupId, can_message: false }],
|
|
190
|
+
});
|
|
191
|
+
expect(put.status).toBe(200);
|
|
192
|
+
expect(await seen(aliceToken)).toEqual([X.alias, Z.alias]);
|
|
193
|
+
expect((await task(aliceToken, X.alias)).status).toBe(403); // 组只读
|
|
194
|
+
expect((await task(aliceToken, Z.alias)).status).toBe(200); // 直接可对话
|
|
195
|
+
// X 再给一条直接可对话授权 ⇒ 并集 ⇒ 可以发
|
|
196
|
+
await send(adminToken, "PUT", grantsPath(aliceId), { grants: [{ node_id: Z.node, can_message: true }, { node_id: X.node, can_message: true }] });
|
|
197
|
+
expect((await task(aliceToken, X.alias)).status).toBe(200);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
test("兼容:旧 app 形状的 PUT(只带 grants,不带 group_grants)不动组授权", async () => {
|
|
201
|
+
const before = await get(adminToken, grantsPath(aliceId));
|
|
202
|
+
expect(before.body.group_grants.length).toBe(1);
|
|
203
|
+
const old = await send(adminToken, "PUT", grantsPath(aliceId), { grants: [{ node_id: Z.node, can_message: true }] });
|
|
204
|
+
expect(old.status).toBe(200);
|
|
205
|
+
expect(old.body.group_grants.length).toBe(1);
|
|
206
|
+
expect(await seen(aliceToken)).toEqual([X.alias, Z.alias]);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test("组授权:外网 / 不存在的 group_id ⇒ 400,整批不写", async () => {
|
|
210
|
+
const other = await send(adminToken, "POST", `/api/networks/${OTHER_NET}/agent-groups`, { name: "外网组" });
|
|
211
|
+
expect(other.status).toBe(200);
|
|
212
|
+
const bad = await send(adminToken, "PUT", grantsPath(aliceId), { group_grants: [{ group_id: other.body.group.group_id }] });
|
|
213
|
+
expect(bad.status).toBe(400);
|
|
214
|
+
expect(bad.body.error).toBe("group_not_in_network");
|
|
215
|
+
expect((await get(adminToken, grantsPath(aliceId))).body.group_grants.map((g: any) => g.group_id)).toEqual([groupId]);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test("agent_access='all' 的成员不看组:照旧全部可见", async () => {
|
|
219
|
+
expect(await seen(carolToken)).toEqual([X.alias, Y.alias, Z.alias]);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
test("成员列表带 agent_group_count", async () => {
|
|
223
|
+
const r = await get(adminToken, `/api/networks/${NET}/members`);
|
|
224
|
+
const alice = r.body.members.find((m: any) => m.user_id === aliceId);
|
|
225
|
+
expect(alice.agent_group_count).toBe(1);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
test("删组 ⇒ 组授权随之消失,只剩直接授权;响应带受影响成员;审计", async () => {
|
|
229
|
+
const del = await send(adminToken, "DELETE", gp(`/${groupId}`));
|
|
230
|
+
expect(del.status).toBe(200);
|
|
231
|
+
expect(del.body.affected_user_ids).toEqual([aliceId]);
|
|
232
|
+
expect(await seen(aliceToken)).toEqual([Z.alias]);
|
|
233
|
+
expect((await task(aliceToken, X.alias)).status).toBe(403);
|
|
234
|
+
expect((await get(adminToken, grantsPath(aliceId))).body.group_grants).toEqual([]);
|
|
235
|
+
expect(db.get("SELECT 1 FROM audit_log WHERE action = 'agent_group_deleted' AND target_id = ?1", groupId)).toBeTruthy();
|
|
236
|
+
expect((await send(adminToken, "DELETE", gp(`/${groupId}`))).status).toBe(404);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
test("移出网络 ⇒ 组授权一并清掉", async () => {
|
|
240
|
+
const g2 = await send(adminToken, "POST", gp(), { name: "第二组", node_ids: [Y.node] });
|
|
241
|
+
await send(adminToken, "PUT", grantsPath(aliceId), { group_grants: [g2.body.group.group_id] });
|
|
242
|
+
expect(db.get("SELECT 1 FROM network_member_group_grants WHERE user_id = ?1", aliceId)).toBeTruthy();
|
|
243
|
+
const rm = await send(adminToken, "DELETE", `/api/networks/${NET}/members/${aliceId}`);
|
|
244
|
+
expect(rm.status).toBe(200);
|
|
245
|
+
expect(db.get("SELECT 1 FROM network_member_group_grants WHERE user_id = ?1", aliceId)).toBeFalsy();
|
|
246
|
+
});
|
|
247
|
+
});
|