Skip to content

Commit 35626a9

Browse files
committed
got tests working
1 parent c3c1c97 commit 35626a9

7 files changed

Lines changed: 81 additions & 91 deletions

File tree

packages/collab-client/src/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ export class CollabClient {
7979
* Send local editor state changes to the remote server.
8080
*/
8181
async send(editorState: EditorState) {
82-
console.error("SENDING STATE");
8382
const commit = sendableCommit(editorState);
8483
if (!commit) return;
8584
// Avoid unnecessary network traffic by skipping commits
@@ -88,9 +87,7 @@ export class CollabClient {
8887

8988
this.sending = commit.ref;
9089
try {
91-
console.error("CALLING sendCommit");
9290
await this.sendCommit(commit);
93-
console.error("SENT COMMIT");
9491
} catch {
9592
// If the send fails, then unset the
9693
// sending ref so that it's possible

packages/collab-server/src/index.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ export class CollabAuthority<Transaction> {
176176
});
177177

178178
if (!appliedCommitJSON) return;
179-
console.error("AUTHORITY IS BROADCASTING COMMIT");
180179
await this.broadcastManager.broadcastCommit(docId, appliedCommitJSON);
181180
}
182181

@@ -185,7 +184,6 @@ export class CollabAuthority<Transaction> {
185184
* are found or after a timeout specified in the CollabAuthority's `broadcastManager`.
186185
*/
187186
async listenForCommit(docId: string, version: number) {
188-
console.error("AUTHORITY IS LISTENING FOR COMMIT");
189187
// Create listner to notify if commits are made. After this await, the listener is registered with
190188
// the notification service and will be notified if a commit is made.
191189
const { listen, abort } = await this.broadcastManager.createCommitListener(docId, version);
@@ -194,23 +192,17 @@ export class CollabAuthority<Transaction> {
194192
// new commit listener being registered
195193
const preCommits = await this.getCommits(null, docId, version);
196194
if (preCommits.length) {
197-
console.error("RETURNING EARLY WITH COMMITS");
198195
await abort();
199196
return preCommits;
200197
}
201198

202-
console.error("LISTENING FOR COMMMITS");
203199
// Await and return any incoming commits
204200
let commitsFound = await listen();
205201
if (commitsFound) {
206-
console.error("FOUND COMMMITS");
207-
208202
const postCommits = await this.getCommits(null, docId, version);
209203
return postCommits;
210204
}
211205

212-
console.error("FOUND NO COMMMITS");
213-
214206
return [];
215207
}
216208
}

packages/demo/src/server-base.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ export function createDeployment(config: DemoDeploymentConfig): DemoDeployment {
171171
});
172172

173173
app.get("/api/docs/:docId/commits", async (req, res) => {
174-
console.error("SERVER LISTENING FOR COMMIT");
175174
const commits = await collabAuthority.listenForCommit(
176175
req.params.docId,
177176
parseInt(req.query["version"] as string, 10),
@@ -199,7 +198,6 @@ export function createDeployment(config: DemoDeploymentConfig): DemoDeployment {
199198

200199
app.post("/api/docs/:docId/commits", async (req, res) => {
201200
try {
202-
console.log("SERVER HAS RECEIVED A COMMIT");
203201
await collabAuthority.receiveCommit(req.params.docId, req.body);
204202
} catch (e) {
205203
if (e instanceof TooMuchContentionError) {

packages/demo/src/test/collab.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { assert, expect, test } from "vitest";
44
import { getDoc } from "../database/docs";
55
import { createDeployment, startServer } from "../server-base";
66

7-
import { createCollabClient,
8-
generateTestDeploymentConfig, sleep } from "./utils";
7+
import { createCollabClient, generateTestDeploymentConfig, sleep } from "./utils";
98

109
const TEST_PORT = 10001;
1110

@@ -33,13 +32,11 @@ test("Test collab", async () => {
3332
serverUrl,
3433
docId,
3534
doc1,
36-
"client1",
3735
);
3836
const { client: _client2, stateBox: stateBox2 } = await createCollabClient(
3937
serverUrl,
4038
docId,
4139
doc2,
42-
"client2",
4340
);
4441

4542
const tr = stateBox1.state.tr.insertText("hello");
@@ -51,4 +48,3 @@ test("Test collab", async () => {
5148

5249
expect(stateBox1.state.doc.content).toStrictEqual(stateBox2.state.doc.content);
5350
});
54-
Lines changed: 33 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,18 @@
1-
import { Node } from "prosemirror-model";
2-
import { schema } from "prosemirror-schema-basic";
3-
import { EditorState } from "prosemirror-state";
1+
import { TextSelection } from "prosemirror-state";
42
// Todo: should the test files be named differently?
53
import { assert, expect, test } from "vitest";
64

75
import {
8-
NodeJSON,
9-
collab,
10-
} from "@pitter-patter/collab-client";
11-
import {
12-
PresenceClient,
13-
PresenceClientConfig,
14-
LongPollListener as PresenceLongPollListener,
15-
receivePresenceTransaction,
6+
presenceKey,
167
} from "@pitter-patter/presence-client";
178

189
import { getDoc } from "../database/docs";
1910
import { randomRef } from "../editor/Editor";
2011
import { createDeployment, startServer } from "../server-base";
2112

22-
import { createCollabClient, generateTestDeploymentConfig, sleep, StateBox } from "./utils";
23-
24-
const TEST_PORT = 10001;
13+
import { createCollabClient, createPresenceClient, generateTestDeploymentConfig, sleep } from "./utils";
2514

26-
interface Doc {
27-
content: NodeJSON;
28-
createdAt: string;
29-
id: string;
30-
updatedAt: string;
31-
version: number;
32-
}
15+
const TEST_PORT = 10002;
3316

3417
test("Test collab", async () => {
3518
const config = generateTestDeploymentConfig(1);
@@ -51,72 +34,53 @@ test("Test collab", async () => {
5134

5235
const userId1 = randomRef();
5336
const doc1 = await getDoc(await deployment.db.getDb(), docId);
54-
const { client: _presenceClient1, stateBox: presenceStateBox1 } = await createPresenceClient(
37+
const { client: collabClient1, stateBox: stateBox1 } = await createCollabClient(
5538
serverUrl,
56-
userId1,
39+
docId,
5740
doc1,
5841
);
59-
const { client: collabClient1, stateBox: collabStateBox1 } = await createCollabClient(
42+
const { client: presenceClient1 } = await createPresenceClient(
6043
serverUrl,
61-
docId,
44+
userId1,
6245
doc1,
63-
"client1",
46+
stateBox1,
6447
);
6548

6649
const userId2 = randomRef();
6750
const doc2 = await getDoc(await deployment.db.getDb(), docId);
68-
const { client: _presenceClient2, stateBox: presenceStateBox2 } = await createPresenceClient(
51+
const { client: _collabClient2, stateBox: stateBox2 } = await createCollabClient(
52+
serverUrl,
53+
docId,
54+
doc2,
55+
);
56+
const { client: _presenceClient2 } = await createPresenceClient(
6957
serverUrl,
7058
userId2,
7159
doc2,
60+
stateBox2,
7261
);
7362

74-
const tr = collabStateBox1.state.tr..insertText("hello");
75-
collabStateBox1.state = collabStateBox1.state.apply(tr);
76-
await collabClient1.send(collabStateBox1.state);
63+
const collabTr = stateBox1.state.tr.insertText("hello");
64+
stateBox1.state = stateBox1.state.apply(collabTr);
65+
await collabClient1.send(stateBox1.state);
7766

7867
// We could pass a channel into the client's receive function and await that instead
7968
// of using an arbitrary delay
8069
await sleep(500);
8170

82-
expect(presenceStateBox1.state.doc.content).toStrictEqual(presenceStateBox2.state.doc.content);
83-
});
71+
const presenceTr = stateBox1.state.tr.setSelection(TextSelection.create(stateBox1.state.doc, 2));
72+
stateBox1.state = stateBox1.state.apply(presenceTr);
73+
await presenceClient1.send(stateBox1.state);
8474

85-
async function createPresenceClient(
86-
serverUrl: string,
87-
userId: string,
88-
doc: Doc,
89-
): Promise<{ client: PresenceClient; stateBox: StateBox }> {
90-
const presenceListener = new PresenceLongPollListener(
91-
new URL(`${serverUrl}/api/docs/${doc.id}/presence`),
92-
);
75+
await sleep(500);
76+
77+
const presenceState = presenceKey.getState(stateBox2.state);
78+
let indicatorArr = presenceState?.indicators[presenceClient1.getClientId()];
79+
if (!indicatorArr || !indicatorArr[0]) {
80+
throw "Indicators not found";
81+
}
82+
const indicator = indicatorArr[0];
83+
expect(indicator.anchor).toBe(2);
84+
expect(indicator.head).toBe(2);
85+
});
9386

94-
const stateBox = {
95-
state: EditorState.create({
96-
doc: Node.fromJSON(schema, doc.content),
97-
plugins: [collab({ version: doc.version })],
98-
}),
99-
};
100-
101-
const presenceConfig: PresenceClientConfig = {
102-
userId,
103-
sendIndicator: async (clientId, indicator) => {
104-
await fetch(`${serverUrl}/api/docs/${doc.id}/presence/${clientId}`, {
105-
method: "POST",
106-
headers: { "Content-Type": "application/json" },
107-
body: JSON.stringify(indicator),
108-
});
109-
},
110-
receiveIndicators: (indicators) => {
111-
// Todo: Something seems redundant about this line in presence and collab. Why
112-
// do we have to call apply on state and pass it to the receive transaction function?
113-
stateBox.state = stateBox.state.apply(receivePresenceTransaction(stateBox.state, indicators));
114-
},
115-
listener: presenceListener,
116-
};
117-
118-
const client = new PresenceClient(presenceConfig);
119-
client.listen();
120-
121-
return { client, stateBox };
122-
}

packages/demo/src/test/utils.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ import {
1111
receiveCommitTransaction,
1212
collab,
1313
} from "@pitter-patter/collab-client";
14+
import {
15+
PresenceClient,
16+
PresenceClientConfig,
17+
LongPollListener as PresenceLongPollListener,
18+
receivePresenceTransaction,
19+
} from "@pitter-patter/presence-client";
20+
import { presence } from "@pitter-patter/presence-client/react";
1421

1522
import { DemoDeploymentConfig } from "../server-base";
1623

17-
1824
// function randomRedisDatabaseIndex(): number {
1925
// return Math.round(Math.random() * 2147483647);
2026
// }
@@ -50,12 +56,11 @@ export async function createCollabClient(
5056
serverUrl: string,
5157
docId: string,
5258
doc: Doc,
53-
tag: string,
5459
): Promise<{ client: CollabClient; stateBox: StateBox }> {
5560
const stateBox = {
5661
state: EditorState.create({
5762
doc: Node.fromJSON(schema, doc.content),
58-
plugins: [collab({ version: doc.version })],
63+
plugins: [collab({ version: doc.version }), presence()],
5964
}),
6065
};
6166

@@ -69,7 +74,6 @@ export async function createCollabClient(
6974
},
7075
listener: new CollabLongPollListener(new URL(`${serverUrl}/api/docs/${docId}/commits`)),
7176
receiveCommits: (commits) => {
72-
console.log(`${tag}: RECEIVING COMMITS`);
7377
for (const commit of commits) {
7478
stateBox.state = stateBox.state.apply(receiveCommitTransaction(stateBox.state, commit));
7579
}
@@ -80,3 +84,34 @@ export async function createCollabClient(
8084

8185
return { client, stateBox };
8286
}
87+
88+
export async function createPresenceClient(
89+
serverUrl: string,
90+
userId: string,
91+
doc: Doc,
92+
stateBox: StateBox,
93+
): Promise<{ client: PresenceClient; stateBox: StateBox }> {
94+
const presenceListener = new PresenceLongPollListener(
95+
new URL(`${serverUrl}/api/docs/${doc.id}/presence`),
96+
);
97+
98+
const presenceConfig: PresenceClientConfig = {
99+
userId,
100+
sendIndicator: async (clientId, indicator) => {
101+
await fetch(`${serverUrl}/api/docs/${doc.id}/presence/${clientId}`, {
102+
method: "POST",
103+
headers: { "Content-Type": "application/json" },
104+
body: JSON.stringify(indicator),
105+
});
106+
},
107+
receiveIndicators: (indicators) => {
108+
stateBox.state = stateBox.state.apply(receivePresenceTransaction(stateBox.state, indicators));
109+
},
110+
listener: presenceListener,
111+
};
112+
113+
const client = new PresenceClient(presenceConfig);
114+
client.listen();
115+
116+
return { client, stateBox };
117+
}

packages/presence-client/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ export class PresenceClient {
9393
this.receiveIndicators(indicators);
9494
}
9595
}
96+
97+
getUserId(): string {
98+
return this.userId;
99+
}
100+
101+
getClientId(): string {
102+
return this.clientId;
103+
}
96104
}
97105

98106
export interface LongPollListenerOptions {

0 commit comments

Comments
 (0)