|
1 | | -import { afterEach, describe, expect, it } from "vitest"; |
| 1 | +import { HocuspocusProvider } from "@hocuspocus/provider"; |
| 2 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
2 | 3 | import * as Y from "yjs"; |
3 | 4 | import { createWorkspaceEditor } from "@/lib/workspace/editor-schema"; |
| 5 | +import { |
| 6 | + createCollaborationServer, |
| 7 | + type DurableUpdate, |
| 8 | + type DurableUpdateStore, |
| 9 | +} from "../../../services/collaboration/src/server"; |
4 | 10 |
|
5 | | -function connectDocuments(left: Y.Doc, right: Y.Doc) { |
6 | | - const leftOrigin = Symbol("left"); |
7 | | - const rightOrigin = Symbol("right"); |
8 | | - const leftUpdates: Uint8Array[] = []; |
9 | | - const rightUpdates: Uint8Array[] = []; |
| 11 | +class MemoryStore implements DurableUpdateStore { |
| 12 | + readonly updates: DurableUpdate[] = []; |
10 | 13 |
|
11 | | - left.on("update", (update, origin) => { |
12 | | - if (origin !== rightOrigin) leftUpdates.push(update); |
13 | | - }); |
14 | | - right.on("update", (update, origin) => { |
15 | | - if (origin !== leftOrigin) rightUpdates.push(update); |
16 | | - }); |
| 14 | + async load(documentId: string, epoch: number) { |
| 15 | + return this.updates.filter( |
| 16 | + (update) => |
| 17 | + update.documentId === documentId && update.epoch === epoch, |
| 18 | + ); |
| 19 | + } |
| 20 | + |
| 21 | + async find(documentId: string, epoch: number, mutationId: string) { |
| 22 | + return this.updates.find( |
| 23 | + (update) => |
| 24 | + update.documentId === documentId && |
| 25 | + update.epoch === epoch && |
| 26 | + update.mutationId === mutationId, |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + async append(update: Omit<DurableUpdate, "sequence">) { |
| 31 | + const existing = await this.find( |
| 32 | + update.documentId, |
| 33 | + update.epoch, |
| 34 | + update.mutationId, |
| 35 | + ); |
| 36 | + if (existing) return existing; |
17 | 37 |
|
18 | | - return () => { |
19 | | - if (leftUpdates.length) { |
20 | | - Y.applyUpdate(right, Y.mergeUpdates(leftUpdates.splice(0)), leftOrigin); |
21 | | - } |
22 | | - if (rightUpdates.length) { |
23 | | - Y.applyUpdate(left, Y.mergeUpdates(rightUpdates.splice(0)), rightOrigin); |
24 | | - } |
25 | | - }; |
| 38 | + const durable = { ...update, sequence: this.updates.length + 1 }; |
| 39 | + this.updates.push(durable); |
| 40 | + return durable; |
| 41 | + } |
26 | 42 | } |
27 | 43 |
|
28 | 44 | describe("workspace editor collaboration", () => { |
29 | | - const cleanup: Array<() => void> = []; |
| 45 | + const cleanup: Array<() => void | Promise<void>> = []; |
30 | 46 |
|
31 | | - afterEach(() => { |
32 | | - cleanup.splice(0).forEach((dispose) => dispose()); |
| 47 | + afterEach(async () => { |
| 48 | + await Promise.all(cleanup.splice(0).map((dispose) => dispose())); |
33 | 49 | }); |
34 | 50 |
|
35 | | - it("converges structured paragraph, list, and link edits after reconnect", () => { |
| 51 | + async function createSession() { |
| 52 | + const server = (() => { |
| 53 | + const deno = Object.getOwnPropertyDescriptor(globalThis, "Deno"); |
| 54 | + try { |
| 55 | + Reflect.deleteProperty(globalThis, "Deno"); |
| 56 | + return createCollaborationServer({ |
| 57 | + authenticate: async ({ documentName, token }) => ({ |
| 58 | + authorId: token, |
| 59 | + documentId: documentName.split("@")[0], |
| 60 | + epoch: Number(documentName.split("@")[1]), |
| 61 | + }), |
| 62 | + store: new MemoryStore(), |
| 63 | + }); |
| 64 | + } finally { |
| 65 | + if (deno) Object.defineProperty(globalThis, "Deno", deno); |
| 66 | + } |
| 67 | + })(); |
| 68 | + await server.listen(); |
| 69 | + |
36 | 70 | const leftDocument = new Y.Doc(); |
37 | 71 | const rightDocument = new Y.Doc(); |
38 | 72 | const left = createWorkspaceEditor(leftDocument); |
39 | 73 | const right = createWorkspaceEditor(rightDocument); |
40 | | - const exchange = connectDocuments(leftDocument, rightDocument); |
| 74 | + const url = `ws://127.0.0.1:${server.address.port}`; |
| 75 | + const leftProvider = new HocuspocusProvider({ |
| 76 | + document: leftDocument, |
| 77 | + name: "structured-editor@1", |
| 78 | + token: "left", |
| 79 | + url, |
| 80 | + }); |
| 81 | + const rightProvider = new HocuspocusProvider({ |
| 82 | + document: rightDocument, |
| 83 | + name: "structured-editor@1", |
| 84 | + token: "right", |
| 85 | + url, |
| 86 | + }); |
41 | 87 | cleanup.push( |
| 88 | + () => leftProvider.destroy(), |
| 89 | + () => rightProvider.destroy(), |
42 | 90 | () => left.destroy(), |
43 | 91 | () => right.destroy(), |
44 | 92 | () => leftDocument.destroy(), |
45 | 93 | () => rightDocument.destroy(), |
| 94 | + () => server.destroy(), |
46 | 95 | ); |
47 | 96 |
|
48 | | - left.commands.setContent("<p>Opening claim</p>"); |
49 | | - exchange(); |
| 97 | + await vi.waitFor(() => { |
| 98 | + expect(leftProvider.isSynced).toBe(true); |
| 99 | + expect(rightProvider.isSynced).toBe(true); |
| 100 | + }); |
50 | 101 |
|
51 | | - left.commands.insertContentAt(1, "For "); |
52 | | - right.commands.insertContentAt(1, "Against "); |
53 | | - exchange(); |
| 102 | + return { left, leftProvider, right, rightProvider }; |
| 103 | + } |
54 | 104 |
|
55 | | - left.commands.selectAll(); |
56 | | - expect(left.commands.toggleBulletList()).toBe(true); |
57 | | - exchange(); |
| 105 | + it("converges structured paragraph, list, and link edits after reconnect", async () => { |
| 106 | + const { left, leftProvider, right, rightProvider } = await createSession(); |
58 | 107 |
|
59 | | - right.commands.selectAll(); |
60 | | - expect(right.commands.setLink({ href: "https://example.com/evidence" })).toBe( |
61 | | - true, |
62 | | - ); |
63 | | - exchange(); |
| 108 | + left.commands.setContent("<p>Opening claim</p><p>Evidence</p>"); |
| 109 | + await vi.waitFor(() => expect(right.getJSON()).toEqual(left.getJSON())); |
64 | 110 |
|
65 | | - expect(left.getJSON()).toEqual(right.getJSON()); |
66 | | - expect(left.getJSON().content?.[0]?.type).toBe("bulletList"); |
67 | | - expect(JSON.stringify(left.getJSON())).toContain( |
68 | | - "https://example.com/evidence", |
| 111 | + rightProvider.disconnect(); |
| 112 | + await vi.waitFor(() => expect(rightProvider.isSynced).toBe(false)); |
| 113 | + |
| 114 | + left.commands.insertContentAt( |
| 115 | + left.state.doc.content.size, |
| 116 | + "<p>Concurrent rebuttal</p>", |
69 | 117 | ); |
| 118 | + right.commands.insertContentAt(1, "Offline "); |
| 119 | + right.commands.selectAll(); |
| 120 | + expect(right.commands.toggleBulletList()).toBe(true); |
| 121 | + right.commands.selectAll(); |
| 122 | + expect( |
| 123 | + right.commands.setLink({ href: "https://example.com/evidence" }), |
| 124 | + ).toBe(true); |
| 125 | + |
| 126 | + rightProvider.connect(); |
| 127 | + await vi.waitFor(() => { |
| 128 | + expect(leftProvider.isSynced).toBe(true); |
| 129 | + expect(rightProvider.isSynced).toBe(true); |
| 130 | + expect(right.getJSON()).toEqual(left.getJSON()); |
| 131 | + }); |
| 132 | + |
| 133 | + const json = JSON.stringify(left.getJSON()); |
| 134 | + expect(json).toContain('"type":"paragraph"'); |
| 135 | + expect(json).toContain('"type":"bulletList"'); |
| 136 | + expect(json).toContain('"type":"link"'); |
| 137 | + expect(json).toContain("Offline Opening claim"); |
| 138 | + expect(json).toContain("Concurrent rebuttal"); |
70 | 139 | }); |
71 | 140 |
|
72 | | - it("undoes only the current editor's last change", () => { |
73 | | - const leftDocument = new Y.Doc(); |
74 | | - const rightDocument = new Y.Doc(); |
75 | | - const left = createWorkspaceEditor(leftDocument); |
76 | | - const right = createWorkspaceEditor(rightDocument); |
77 | | - const exchange = connectDocuments(leftDocument, rightDocument); |
78 | | - cleanup.push( |
79 | | - () => left.destroy(), |
80 | | - () => right.destroy(), |
81 | | - () => leftDocument.destroy(), |
82 | | - () => rightDocument.destroy(), |
83 | | - ); |
| 141 | + it("undoes only the current editor's last change through the provider", async () => { |
| 142 | + const { left, right } = await createSession(); |
84 | 143 |
|
85 | 144 | left.commands.setContent("<p>Base</p>"); |
86 | | - exchange(); |
| 145 | + await vi.waitFor(() => expect(right.getJSON()).toEqual(left.getJSON())); |
87 | 146 | left.commands.insertContentAt(5, " Alice"); |
88 | | - exchange(); |
| 147 | + await vi.waitFor(() => expect(right.getText()).toContain("Alice")); |
89 | 148 | right.commands.insertContentAt(11, " Bob"); |
90 | | - exchange(); |
| 149 | + await vi.waitFor(() => expect(left.getText()).toContain("Bob")); |
91 | 150 |
|
92 | 151 | expect(left.commands.undo()).toBe(true); |
93 | | - exchange(); |
| 152 | + await vi.waitFor(() => expect(right.getJSON()).toEqual(left.getJSON())); |
94 | 153 |
|
95 | 154 | expect(left.getText()).toContain("Bob"); |
96 | 155 | expect(left.getText()).not.toContain("Alice"); |
97 | | - expect(left.getJSON()).toEqual(right.getJSON()); |
98 | 156 | }); |
99 | 157 | }); |
0 commit comments