@allbluecn/collab-server 0.0.0-stage → 0.1.7
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/LICENSE +661 -0
- package/README.md +126 -2
- package/package.json +64 -3
- package/src/__tests__/clients.test.ts +93 -0
- package/src/__tests__/embed.test.ts +227 -0
- package/src/__tests__/env-setup.ts +33 -0
- package/src/__tests__/exporters-bug.test.ts +552 -0
- package/src/__tests__/exporters-case.test.ts +356 -0
- package/src/__tests__/exporters-design.test.ts +356 -0
- package/src/__tests__/exporters-knowledge.test.ts +366 -0
- package/src/__tests__/exporters-stats.test.ts +105 -0
- package/src/__tests__/exporters-story.test.ts +519 -0
- package/src/__tests__/internal-apply-channel.test.ts +233 -0
- package/src/__tests__/internal-apply-race.test.ts +165 -0
- package/src/__tests__/internal-apply.test.ts +418 -0
- package/src/__tests__/multiplex.test.ts +166 -0
- package/src/__tests__/persistence-retry.test.ts +262 -0
- package/src/__tests__/persistence.test.ts +302 -0
- package/src/__tests__/server.test.ts +145 -0
- package/src/__tests__/share-room-limit.test.ts +183 -0
- package/src/__tests__/shutdown.test.ts +130 -0
- package/src/__tests__/test-utils.ts +176 -0
- package/src/auth.ts +84 -0
- package/src/clients.ts +133 -0
- package/src/embed.ts +156 -0
- package/src/exporters/bug.ts +454 -0
- package/src/exporters/case.ts +317 -0
- package/src/exporters/design.ts +268 -0
- package/src/exporters/entity-shared.ts +75 -0
- package/src/exporters/knowledge.ts +259 -0
- package/src/exporters/story.ts +483 -0
- package/src/exporters.ts +173 -0
- package/src/index.ts +354 -0
- package/src/internal-apply-limit.ts +99 -0
- package/src/internal-apply.ts +888 -0
- package/src/metrics.ts +82 -0
- package/src/persistence.ts +409 -0
- package/src/rooms.ts +32 -0
- package/src/share-room-limit.ts +73 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// AllBlue - 理想之海
|
|
3
|
+
// Copyright (C) 2026 AllBlue Contributors
|
|
4
|
+
//
|
|
5
|
+
// This program is free software: you can redistribute it and/or modify
|
|
6
|
+
// it under the terms of the GNU Affero General Public License as published by
|
|
7
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
// (at your option) any later version.
|
|
9
|
+
//
|
|
10
|
+
// This program is distributed in the hope that it will be useful,
|
|
11
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
// GNU Affero General Public License for more details.
|
|
14
|
+
//
|
|
15
|
+
// You should have received a copy of the GNU Affero General Public License
|
|
16
|
+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
// /internal/apply HTTP 通道单元测试(2026-10-25 审计 A07 修复):
|
|
19
|
+
// 不经 Hocuspocus、不触真库——attachInternalApplyChannel 挂在裸 node:http
|
|
20
|
+
// 上(hocuspocus 面传空 documents Map,本文件的用例全部在验签/读体/准入层
|
|
21
|
+
// 终结,不进入 handleInternalApply 的业务路径)。覆盖:
|
|
22
|
+
// 1. 客户端中断上传(body 流 error)→ 400 + 服务进程存活(此前为
|
|
23
|
+
// unhandledRejection → Node ≥15 默认进程崩溃,公网可打的可用性缺陷)
|
|
24
|
+
// 2. 非法 JSON body(签名合法 over 坏 JSON)→ 401 bad-json(fail-closed 不崩溃)
|
|
25
|
+
// 3. 准入限流:全局并发读体上限 → 503;per-IP 验签失败滑动窗 → 429
|
|
26
|
+
// 4. 流式超限 413 后服务端断开(防半开滞留)
|
|
27
|
+
|
|
28
|
+
import { afterEach, describe, expect, test } from "vitest";
|
|
29
|
+
import * as http from "node:http";
|
|
30
|
+
import { createHmac } from "node:crypto";
|
|
31
|
+
import type { AddressInfo } from "node:net";
|
|
32
|
+
import type { Hocuspocus, Server } from "@hocuspocus/server";
|
|
33
|
+
import { INTERNAL_DOMAIN_APPLY } from "@allbluecn/collab-core/internal";
|
|
34
|
+
import { attachInternalApplyChannel } from "../internal-apply";
|
|
35
|
+
import { createApplyAdmission } from "../internal-apply-limit";
|
|
36
|
+
import { INTERNAL_APPLY_MAX_BODY_BYTES } from "../internal-apply";
|
|
37
|
+
|
|
38
|
+
const SECRET = "audit-fix-channel-secret";
|
|
39
|
+
|
|
40
|
+
/** 裸 http 服务 + 空 hocuspocus 面的通道挂载(用例均在业务层之前终结) */
|
|
41
|
+
function startChannel(options: { admission?: ReturnType<typeof createApplyAdmission> } = {}): Promise<number> {
|
|
42
|
+
return new Promise((resolve) => {
|
|
43
|
+
const httpServer = http.createServer();
|
|
44
|
+
const stubServer = {
|
|
45
|
+
httpServer,
|
|
46
|
+
hocuspocus: {
|
|
47
|
+
documents: new Map(),
|
|
48
|
+
loadingDocuments: new Map(),
|
|
49
|
+
} as unknown as Hocuspocus,
|
|
50
|
+
} as unknown as Server;
|
|
51
|
+
httpServer.listen(0, "127.0.0.1", () => {
|
|
52
|
+
attachInternalApplyChannel(stubServer, SECRET, options);
|
|
53
|
+
resolve((httpServer.address() as AddressInfo).port);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface RawResult {
|
|
59
|
+
status: number;
|
|
60
|
+
body: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** 原生 http 客户端(可控制 body 分片与中途断开) */
|
|
64
|
+
function rawRequest(
|
|
65
|
+
port: number,
|
|
66
|
+
opts: {
|
|
67
|
+
headers?: Record<string, string | number>;
|
|
68
|
+
body?: Buffer;
|
|
69
|
+
/** 写出 body 后强制断开(模拟客户端中断上传) */
|
|
70
|
+
abortAfterWrite?: boolean;
|
|
71
|
+
} = {},
|
|
72
|
+
): Promise<RawResult> {
|
|
73
|
+
return new Promise((resolve) => {
|
|
74
|
+
const req = http.request(
|
|
75
|
+
{ host: "127.0.0.1", port, path: "/internal/apply", method: "POST", headers: opts.headers },
|
|
76
|
+
(res) => {
|
|
77
|
+
const chunks: Buffer[] = [];
|
|
78
|
+
res.on("data", (c: Buffer) => chunks.push(c));
|
|
79
|
+
res.on("end", () =>
|
|
80
|
+
resolve({ status: res.statusCode ?? 0, body: Buffer.concat(chunks).toString("utf8") }),
|
|
81
|
+
);
|
|
82
|
+
res.on("error", () =>
|
|
83
|
+
resolve({ status: res.statusCode ?? 0, body: Buffer.concat(chunks).toString("utf8") }),
|
|
84
|
+
);
|
|
85
|
+
},
|
|
86
|
+
);
|
|
87
|
+
req.on("error", () => {
|
|
88
|
+
// 客户端主动断开/服务端 reset 时响应可能收不到——空结果即未崩溃证据链的一部分
|
|
89
|
+
resolve({ status: 0, body: "" });
|
|
90
|
+
});
|
|
91
|
+
if (opts.body) req.write(opts.body);
|
|
92
|
+
if (opts.abortAfterWrite) {
|
|
93
|
+
req.end();
|
|
94
|
+
req.destroy();
|
|
95
|
+
resolve({ status: 0, body: "" });
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
req.end();
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** 与 collab-core internalSignature 同构的最小签名(hex,`${domain}\n${body}`) */
|
|
103
|
+
function signRaw(body: string, secret: string = SECRET): string {
|
|
104
|
+
return createHmac("sha256", secret).update(`${INTERNAL_DOMAIN_APPLY}\n${body}`).digest("hex");
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** 服务存活探针:POST /internal/apply 能得到任何响应(401/429)即进程与通道存活 */
|
|
108
|
+
async function alive(port: number): Promise<boolean> {
|
|
109
|
+
return new Promise((resolve) => {
|
|
110
|
+
const req = http.request(
|
|
111
|
+
{ host: "127.0.0.1", port, path: "/internal/apply", method: "POST" },
|
|
112
|
+
(res) => {
|
|
113
|
+
res.resume();
|
|
114
|
+
res.on("end", () => resolve(true));
|
|
115
|
+
},
|
|
116
|
+
);
|
|
117
|
+
req.on("error", () => resolve(false));
|
|
118
|
+
req.end("{}");
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const startedPorts: number[] = [];
|
|
123
|
+
async function start(): Promise<number> {
|
|
124
|
+
const port = await startChannel();
|
|
125
|
+
startedPorts.push(port);
|
|
126
|
+
return port;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
afterEach(() => {
|
|
130
|
+
// 裸 server 无句柄表——进程级用例互不依赖端口复用,交由 vitest 收尾
|
|
131
|
+
startedPorts.length = 0;
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
describe("/internal/apply HTTP 通道(审计修复:中断/非法 JSON/准入)", () => {
|
|
135
|
+
test("客户端中断上传 → 读体 rejection 被捕获(400 + destroy),服务进程存活", async () => {
|
|
136
|
+
const port = await start();
|
|
137
|
+
|
|
138
|
+
// 中断上传:发出部分 body 后 socket 级断开——服务端 readBody 以
|
|
139
|
+
// ECONNRESET reject。修复前该 rejection 逃逸为 unhandledRejection
|
|
140
|
+
// (Node ≥15 默认崩进程);修复后回 400 并 destroy。
|
|
141
|
+
await rawRequest(port, {
|
|
142
|
+
headers: { "content-length": "1000" },
|
|
143
|
+
body: Buffer.alloc(100),
|
|
144
|
+
abortAfterWrite: true,
|
|
145
|
+
});
|
|
146
|
+
// 给服务端事件循环处理中断的时间
|
|
147
|
+
await new Promise((r) => setTimeout(r, 200));
|
|
148
|
+
|
|
149
|
+
// 进程存活 + 通道可用:后续请求照常进入正常处理路径(401 = 验签层可达)
|
|
150
|
+
expect(await alive(port)).toBe(true);
|
|
151
|
+
const next = await rawRequest(port, { headers: { "content-length": "2" }, body: Buffer.from("{}") });
|
|
152
|
+
expect(next.status).toBe(401);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test("非法 JSON body(签名合法)→ 401 bad-json,不崩溃", async () => {
|
|
156
|
+
const port = await start();
|
|
157
|
+
const badBody = '{"domain":"collab-internal-apply:v1","ts":123'; // 截断 JSON
|
|
158
|
+
const res = await rawRequest(port, {
|
|
159
|
+
headers: { "content-length": String(Buffer.byteLength(badBody)), "x-collab-signature": signRaw(badBody) },
|
|
160
|
+
body: Buffer.from(badBody),
|
|
161
|
+
});
|
|
162
|
+
expect(res.status).toBe(401);
|
|
163
|
+
expect(JSON.parse(res.body)).toMatchObject({ error: "unauthorized", reason: "bad-json" });
|
|
164
|
+
expect(await alive(port)).toBe(true);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test("per-IP 验签失败超滑动窗上限 → 429(读体前拒绝)", async () => {
|
|
168
|
+
const admission = createApplyAdmission();
|
|
169
|
+
const port = await startChannel({ admission });
|
|
170
|
+
startedPorts.push(port);
|
|
171
|
+
for (let i = 0; i < 30; i += 1) admission.noteVerifyFailure("127.0.0.1");
|
|
172
|
+
const res = await rawRequest(port, { headers: { "content-length": "2" }, body: Buffer.from("{}") });
|
|
173
|
+
expect(res.status).toBe(429);
|
|
174
|
+
expect(JSON.parse(res.body)).toEqual({ error: "too many attempts" });
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test("全局并发读体达上限 → 503 busy,占用释放后恢复", async () => {
|
|
178
|
+
const admission = createApplyAdmission({ maxInFlight: 1 });
|
|
179
|
+
const port = await startChannel({ admission });
|
|
180
|
+
startedPorts.push(port);
|
|
181
|
+
|
|
182
|
+
// 占用唯一并发名额:发起一个不结束的读体(声明 2 字节但只发 1 字节后挂起)
|
|
183
|
+
let holdReq: http.ClientRequest | null = null;
|
|
184
|
+
const hold = new Promise<RawResult>((resolve) => {
|
|
185
|
+
const req = http.request(
|
|
186
|
+
{ host: "127.0.0.1", port, path: "/internal/apply", method: "POST", headers: { "content-length": "2" } },
|
|
187
|
+
(res) => {
|
|
188
|
+
const chunks: Buffer[] = [];
|
|
189
|
+
res.on("data", (c: Buffer) => chunks.push(c));
|
|
190
|
+
res.on("end", () => resolve({ status: res.statusCode ?? 0, body: Buffer.concat(chunks).toString("utf8") }));
|
|
191
|
+
},
|
|
192
|
+
);
|
|
193
|
+
req.on("error", () => resolve({ status: 0, body: "" }));
|
|
194
|
+
req.write(Buffer.from([0x7b]));
|
|
195
|
+
// 不 end——读体挂起,名额被占
|
|
196
|
+
holdReq = req;
|
|
197
|
+
});
|
|
198
|
+
await new Promise((r) => setTimeout(r, 100)); // 等服务端进入读体段
|
|
199
|
+
|
|
200
|
+
const busy = await rawRequest(port, { headers: { "content-length": "2" }, body: Buffer.from("{}") });
|
|
201
|
+
expect(busy.status).toBe(503);
|
|
202
|
+
expect(JSON.parse(busy.body)).toEqual({ error: "busy" });
|
|
203
|
+
|
|
204
|
+
// 释放:挂起请求补完 body → 401(验签层)→ 名额归还
|
|
205
|
+
holdReq!.end(Buffer.from("}"));
|
|
206
|
+
const held = await hold;
|
|
207
|
+
expect(held.status).toBe(401);
|
|
208
|
+
expect(admission.inFlight()).toBe(0);
|
|
209
|
+
|
|
210
|
+
const next = await rawRequest(port, { headers: { "content-length": "2" }, body: Buffer.from("{}") });
|
|
211
|
+
expect(next.status).toBe(401);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test("流式超限 → 413 且服务端主动断开(不滞留半开连接)", async () => {
|
|
215
|
+
const port = await start();
|
|
216
|
+
const oversized = Buffer.alloc(INTERNAL_APPLY_MAX_BODY_BYTES + 1024, 0x61);
|
|
217
|
+
const res = await rawRequest(port, { body: oversized });
|
|
218
|
+
expect(res.status).toBe(413);
|
|
219
|
+
expect(JSON.parse(res.body)).toEqual({ error: "payload too large" });
|
|
220
|
+
// 断开的是超限连接本身,服务存活。⚠ 紧随其后建连有极小概率被 RST
|
|
221
|
+
// (客户端 ephemeral 端口被立即复用、SYN 撞上旧连接残态——仅影响
|
|
222
|
+
// 「谎报体积被服务端断开」的同一客户端瞬间重连,合法流量不受影响),
|
|
223
|
+
// 稍候重试探活。
|
|
224
|
+
const aliveAfter = async (): Promise<boolean> => {
|
|
225
|
+
for (let attempt = 0; attempt < 5; attempt += 1) {
|
|
226
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
227
|
+
if (await alive(port)) return true;
|
|
228
|
+
}
|
|
229
|
+
return false;
|
|
230
|
+
};
|
|
231
|
+
expect(await aliveAfter()).toBe(true);
|
|
232
|
+
});
|
|
233
|
+
});
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// AllBlue - 理想之海
|
|
3
|
+
// Copyright (C) 2026 AllBlue Contributors
|
|
4
|
+
//
|
|
5
|
+
// This program is free software: you can redistribute it and/or modify
|
|
6
|
+
// it under the terms of the GNU Affero General Public License as published by
|
|
7
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
// (at your option) any later version.
|
|
9
|
+
//
|
|
10
|
+
// This program is distributed in the hope that it will be useful,
|
|
11
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
// GNU Affero General Public License for more details.
|
|
14
|
+
//
|
|
15
|
+
// You should have received a copy of the GNU Affero General Public License
|
|
16
|
+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
// 离线判定与在途连接竞态测试(I3,任务 17 复审修复)——prisma 桩件直驱
|
|
19
|
+
// handleInternalApply(不经 WS、不触真库,模式同 persistence-retry.test):
|
|
20
|
+
//
|
|
21
|
+
// 判离线 → 直写 DB 期间连接到达(文档装载完成/装载中)→ 复查发现 live
|
|
22
|
+
// room → 进程内补一次同内容 apply(幂等,与 HTTP 在线分支同一 applyToLiveDoc
|
|
23
|
+
// 路径)→ 内存态与 DB 直写收敛,旧 Y 态会话不再覆盖刚直写的 content。
|
|
24
|
+
//
|
|
25
|
+
// 时序用 prisma 桩件的 onWrite 钩子确定性注入(真实 WS 下毫秒级窗口无法
|
|
26
|
+
// 稳定复现);装载中场景经 loadingDocuments Promise 模拟。
|
|
27
|
+
|
|
28
|
+
import { beforeEach, describe, expect, test, vi } from "vitest";
|
|
29
|
+
import * as Y from "yjs";
|
|
30
|
+
import type { Document, Hocuspocus } from "@hocuspocus/server";
|
|
31
|
+
import type { Document as CollabDocument } from "@hocuspocus/server";
|
|
32
|
+
import {
|
|
33
|
+
INTERNAL_DOMAIN_APPLY,
|
|
34
|
+
INTERNAL_SIGNATURE_HEADER,
|
|
35
|
+
signInternalRequest,
|
|
36
|
+
} from "@allbluecn/collab-core/internal";
|
|
37
|
+
import { yDocToProsemirrorJSON } from "@tiptap/y-tiptap";
|
|
38
|
+
|
|
39
|
+
const state = vi.hoisted(() => ({
|
|
40
|
+
updateCalls: [] as Array<{ where: { id: string }; data: { content: unknown } }>,
|
|
41
|
+
/** 直写落库瞬间注入的副作用(模拟连接到达) */
|
|
42
|
+
onWrite: null as null | (() => void),
|
|
43
|
+
}));
|
|
44
|
+
|
|
45
|
+
vi.mock("@allbluecn/database", () => ({
|
|
46
|
+
prisma: {
|
|
47
|
+
knowledgeDocument: {
|
|
48
|
+
update: async (args: { where: { id: string }; data: { content: unknown } }) => {
|
|
49
|
+
state.updateCalls.push(args);
|
|
50
|
+
state.onWrite?.();
|
|
51
|
+
return {};
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
}));
|
|
56
|
+
|
|
57
|
+
import { handleInternalApply } from "../internal-apply";
|
|
58
|
+
import { setKnowledgeReindexNotifier } from "../exporters/knowledge";
|
|
59
|
+
|
|
60
|
+
const SECRET = "test-internal-race-secret";
|
|
61
|
+
const DOC_ID = "race-doc-1";
|
|
62
|
+
const ROOM = `doc:knowledge:${DOC_ID}`;
|
|
63
|
+
|
|
64
|
+
/** Y.Doc + Document 名面(internal-apply 只消费 fragment/transact/连接计数) */
|
|
65
|
+
function makeLiveDoc(name: string, connections: number): CollabDocument {
|
|
66
|
+
const doc = new Y.Doc() as unknown as CollabDocument;
|
|
67
|
+
Object.assign(doc, {
|
|
68
|
+
name,
|
|
69
|
+
getConnectionsCount: () => connections,
|
|
70
|
+
});
|
|
71
|
+
return doc;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function makeHocuspocus(opts: {
|
|
75
|
+
documents?: Map<string, CollabDocument>;
|
|
76
|
+
loading?: Map<string, Promise<Document>>;
|
|
77
|
+
}): Hocuspocus {
|
|
78
|
+
return {
|
|
79
|
+
documents: opts.documents ?? new Map(),
|
|
80
|
+
loadingDocuments: opts.loading ?? new Map<string, Promise<Document>>(),
|
|
81
|
+
storeDocumentHooks: async () => undefined,
|
|
82
|
+
} as unknown as Hocuspocus;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function post(
|
|
86
|
+
hocuspocus: Hocuspocus,
|
|
87
|
+
html: string,
|
|
88
|
+
): Promise<{ status: number; body: Record<string, unknown> }> {
|
|
89
|
+
const signed = signInternalRequest({
|
|
90
|
+
secret: SECRET,
|
|
91
|
+
domain: INTERNAL_DOMAIN_APPLY,
|
|
92
|
+
payload: {
|
|
93
|
+
roomName: ROOM,
|
|
94
|
+
docId: DOC_ID,
|
|
95
|
+
actor: "user-actor",
|
|
96
|
+
patch: { content: { html, markdown: html } },
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
const result = await handleInternalApply({
|
|
100
|
+
hocuspocus,
|
|
101
|
+
secret: SECRET,
|
|
102
|
+
body: signed.body,
|
|
103
|
+
signature: signed.headers[INTERNAL_SIGNATURE_HEADER]!,
|
|
104
|
+
});
|
|
105
|
+
return { status: result.status, body: result.body };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
beforeEach(() => {
|
|
109
|
+
state.updateCalls = [];
|
|
110
|
+
state.onWrite = null;
|
|
111
|
+
setKnowledgeReindexNotifier(async () => {});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe("/internal/apply 离线判定与在途连接竞态(I3)", () => {
|
|
115
|
+
test("直写期间连接到达 → 复查发现 live room,补一次同内容 apply(幂等收敛)", async () => {
|
|
116
|
+
// 「判离线 → 直写 → 连接到达」:落库瞬间把装载完成的 room 塞进 documents
|
|
117
|
+
const arriving = makeLiveDoc(ROOM, 1);
|
|
118
|
+
const documents = new Map<string, CollabDocument>();
|
|
119
|
+
state.onWrite = () => {
|
|
120
|
+
documents.set(ROOM, arriving);
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const res = await post(makeHocuspocus({ documents }), "<p>竞态补写</p>");
|
|
124
|
+
|
|
125
|
+
expect(res.status).toBe(202);
|
|
126
|
+
expect(res.body).toMatchObject({ applied: "db", reapplied: "ydoc" });
|
|
127
|
+
// 直写发生且 content 原样落库
|
|
128
|
+
expect(state.updateCalls).toHaveLength(1);
|
|
129
|
+
expect(state.updateCalls[0]!.data.content).toEqual({
|
|
130
|
+
html: "<p>竞态补写</p>",
|
|
131
|
+
markdown: "<p>竞态补写</p>",
|
|
132
|
+
});
|
|
133
|
+
// 补应用生效:到达会话的 Y 态与 DB 直写内容一致(不再以旧态覆盖直写)
|
|
134
|
+
const json = yDocToProsemirrorJSON(arriving as unknown as Y.Doc, "default");
|
|
135
|
+
expect(JSON.stringify(json)).toContain("竞态补写");
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test("装载中的 room(loadingDocuments 在途)→ 等装载完成后补应用", async () => {
|
|
139
|
+
const loading = makeLiveDoc(ROOM, 1);
|
|
140
|
+
|
|
141
|
+
const res = await post(
|
|
142
|
+
makeHocuspocus({
|
|
143
|
+
loading: new Map([[ROOM, Promise.resolve(loading)]]),
|
|
144
|
+
}),
|
|
145
|
+
"<p>装载中补写</p>",
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
expect(res.status).toBe(202);
|
|
149
|
+
expect(res.body).toMatchObject({ applied: "db", reapplied: "ydoc" });
|
|
150
|
+
const json = yDocToProsemirrorJSON(loading as unknown as Y.Doc, "default");
|
|
151
|
+
expect(JSON.stringify(json)).toContain("装载中补写");
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test("无连接到达 → 纯直写(applied:db,无补应用语义)", async () => {
|
|
155
|
+
const res = await post(makeHocuspocus({}), "<p>纯直写</p>");
|
|
156
|
+
|
|
157
|
+
expect(res.status).toBe(202);
|
|
158
|
+
expect(res.body).toEqual({ applied: "db" });
|
|
159
|
+
expect(state.updateCalls).toHaveLength(1);
|
|
160
|
+
expect(state.updateCalls[0]!.data.content).toEqual({
|
|
161
|
+
html: "<p>纯直写</p>",
|
|
162
|
+
markdown: "<p>纯直写</p>",
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
});
|