Skip to content

Commit 748088c

Browse files
committed
[tests] Rationalize comparison
1 parent c49716f commit 748088c

6 files changed

Lines changed: 275 additions & 359 deletions

File tree

test/unit/common.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import {Connector, ConnectorOptions, createSynclet, Synclet} from 'synclets';
33
import {createMemoryTransport} from 'synclets/transport/memory';
44
import {getUniqueId} from 'synclets/utils';
55

6+
export interface TestConnector extends Connector {
7+
getDataForTest(): any;
8+
getMetaForTest(): any;
9+
}
10+
611
export const pause = async (ms = 2) =>
712
new Promise((resolve) => setTimeout(resolve, ms));
813

@@ -42,3 +47,25 @@ export const getTestSyncletAndConnector = async <
4247
const synclet = await createSynclet(connector, transport, {}, {id, logger});
4348
return [synclet, connector];
4449
};
50+
51+
export const expectEquivalentConnectors = (
52+
connectors: TestConnector[],
53+
data: any,
54+
) => {
55+
const timestamp = connectors[0].getMetaForTest();
56+
connectors.forEach((connector) => {
57+
expect(connector.getDataForTest()).toEqual(data);
58+
expect(connector.getMetaForTest()).toEqual(timestamp);
59+
});
60+
};
61+
62+
export const expectDifferingConnectors = (
63+
connector1: TestConnector,
64+
connector2: TestConnector,
65+
data1: any,
66+
data2: any,
67+
) => {
68+
expect(connector1.getDataForTest()).toEqual(data1);
69+
expect(connector2.getDataForTest()).toEqual(data2);
70+
expect(connector1.getMetaForTest()).not.toEqual(connector2.getMetaForTest());
71+
};

test/unit/connectors/base/table.test.ts

Lines changed: 42 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@ import {
33
BaseTableConnector,
44
createBaseTableConnector,
55
} from 'synclets/connector/base';
6-
import {getTestSyncletsAndConnectors, pause} from '../../common.ts';
7-
8-
type TestTableConnector = BaseTableConnector & {
9-
setCellForTest: (rowId: string, cellId: string, cell: Atom) => Promise<void>;
10-
delCellForTest: (rowId: string, cellId: string) => Promise<void>;
11-
getTableForTest: () => {[rowId: string]: {[cellId: string]: Atom}};
12-
getTimestampsForTest: () => {[rowId: string]: {[cellId: string]: Timestamp}};
13-
getTableHashForTest: () => Hash | undefined;
14-
getRowHashesForTest: () => {[rowId: string]: Hash};
15-
};
6+
import {
7+
expectDifferingConnectors,
8+
expectEquivalentConnectors,
9+
getTestSyncletsAndConnectors,
10+
pause,
11+
} from '../../common.ts';
12+
13+
interface TestTableConnector extends BaseTableConnector {
14+
setCellForTest(rowId: string, cellId: string, cell: Atom): Promise<void>;
15+
delCellForTest(rowId: string, cellId: string): Promise<void>;
16+
getDataForTest(): {[rowId: string]: {[cellId: string]: Atom}};
17+
getMetaForTest(): [
18+
{[rowId: string]: {[cellId: string]: Timestamp}},
19+
Hash | undefined,
20+
{[rowId: string]: Hash},
21+
];
22+
}
1623

1724
const createTestTableConnector = async (
1825
options?: ConnectorOptions,
@@ -76,62 +83,21 @@ const createTestTableConnector = async (
7683
delCellForTest: (rowId: string, cellId: string) =>
7784
connector.delCell(rowId, cellId),
7885

79-
getTableForTest: () => table,
80-
81-
getTimestampsForTest: () => timestamps,
82-
83-
getRowHashesForTest: () => rowHashes,
86+
getDataForTest: () => table,
8487

85-
getTableHashForTest: () => tableHash,
88+
getMetaForTest: () => [timestamps, tableHash, rowHashes],
8689
};
8790
};
8891

89-
const expectEquivalentConnectors = (
90-
connector1: TestTableConnector,
91-
connector2: TestTableConnector,
92-
table: {[rowId: string]: {[cellId: string]: Atom}} = {},
93-
) => {
94-
expect(connector1.getTableForTest()).toEqual(table);
95-
expect(connector2.getTableForTest()).toEqual(table);
96-
expect(connector1.getTimestampsForTest()).toEqual(
97-
connector2.getTimestampsForTest(),
98-
);
99-
expect(connector1.getRowHashesForTest()).toEqual(
100-
connector2.getRowHashesForTest(),
101-
);
102-
expect(connector1.getTableHashForTest()).toEqual(
103-
connector2.getTableHashForTest(),
104-
);
105-
};
106-
107-
const expectDifferingConnectors = (
108-
connector1: TestTableConnector,
109-
connector2: TestTableConnector,
110-
table1: {[rowId: string]: {[cellId: string]: Atom}},
111-
table2: {[rowId: string]: {[cellId: string]: Atom}} = {},
112-
) => {
113-
expect(connector1.getTableForTest()).toEqual(table1);
114-
expect(connector2.getTableForTest()).toEqual(table2);
115-
expect(connector1.getTimestampsForTest()).not.toEqual(
116-
connector2.getTimestampsForTest(),
117-
);
118-
expect(connector1.getRowHashesForTest()).not.toEqual(
119-
connector2.getRowHashesForTest(),
120-
);
121-
expect(connector1.getTableHashForTest()).not.toEqual(
122-
connector2.getTableHashForTest(),
123-
);
124-
};
125-
126-
describe('table sync, basics', () => {
92+
describe('2-way', () => {
12793
test('connected, initial', async () => {
12894
const [[synclet1, connector1], [synclet2, connector2]] =
12995
await getTestSyncletsAndConnectors(createTestTableConnector, 2);
13096

13197
await synclet1.start();
13298
await synclet2.start();
13399

134-
expectEquivalentConnectors(connector1, connector2);
100+
expectEquivalentConnectors([connector1, connector2], {});
135101
});
136102

137103
test('connected', async () => {
@@ -142,10 +108,10 @@ describe('table sync, basics', () => {
142108
await synclet2.start();
143109

144110
await connector1.setCellForTest('r1', 'c1', 'C1');
145-
expectEquivalentConnectors(connector1, connector2, {r1: {c1: 'C1'}});
111+
expectEquivalentConnectors([connector1, connector2], {r1: {c1: 'C1'}});
146112

147113
await connector2.setCellForTest('r1', 'c1', 'C2');
148-
expectEquivalentConnectors(connector1, connector2, {r1: {c1: 'C2'}});
114+
expectEquivalentConnectors([connector1, connector2], {r1: {c1: 'C2'}});
149115
});
150116

151117
test('connected, deletion', async () => {
@@ -156,12 +122,12 @@ describe('table sync, basics', () => {
156122
await synclet2.start();
157123

158124
await connector1.setCellForTest('r1', 'c1', 'C1');
159-
expectEquivalentConnectors(connector1, connector2, {r1: {c1: 'C1'}});
125+
expectEquivalentConnectors([connector1, connector2], {r1: {c1: 'C1'}});
160126

161-
const timestamp = connector1.getTimestampsForTest().r1.c1;
127+
const timestamp = connector1.getMetaForTest()[0].r1.c1;
162128
await connector1.delCellForTest('r1', 'c1');
163-
expectEquivalentConnectors(connector1, connector2, {r1: {}});
164-
expect(timestamp).not.toEqual(connector1.getTimestampsForTest().r1.c1);
129+
expectEquivalentConnectors([connector1, connector2], {r1: {}});
130+
expect(timestamp).not.toEqual(connector1.getMetaForTest()[0].r1.c1);
165131
});
166132

167133
test('start 1, set 1, start 2', async () => {
@@ -171,10 +137,10 @@ describe('table sync, basics', () => {
171137
await synclet1.start();
172138

173139
await connector1.setCellForTest('r1', 'c1', 'C1');
174-
expectDifferingConnectors(connector1, connector2, {r1: {c1: 'C1'}});
140+
expectDifferingConnectors(connector1, connector2, {r1: {c1: 'C1'}}, {});
175141

176142
await synclet2.start();
177-
expectEquivalentConnectors(connector1, connector2, {r1: {c1: 'C1'}});
143+
expectEquivalentConnectors([connector1, connector2], {r1: {c1: 'C1'}});
178144
});
179145

180146
test('start 2, set 1, start 1', async () => {
@@ -184,10 +150,10 @@ describe('table sync, basics', () => {
184150
await synclet2.start();
185151
await connector1.connect();
186152
await connector1.setCellForTest('r1', 'c1', 'C1');
187-
expectDifferingConnectors(connector1, connector2, {r1: {c1: 'C1'}});
153+
expectDifferingConnectors(connector1, connector2, {r1: {c1: 'C1'}}, {});
188154

189155
await synclet1.start();
190-
expectEquivalentConnectors(connector1, connector2, {r1: {c1: 'C1'}});
156+
expectEquivalentConnectors([connector1, connector2], {r1: {c1: 'C1'}});
191157
});
192158

193159
test('stop 1, set 1, start 1', async () => {
@@ -198,7 +164,7 @@ describe('table sync, basics', () => {
198164
await synclet2.start();
199165

200166
await connector1.setCellForTest('r1', 'c1', 'C1');
201-
expectEquivalentConnectors(connector1, connector2, {r1: {c1: 'C1'}});
167+
expectEquivalentConnectors([connector1, connector2], {r1: {c1: 'C1'}});
202168

203169
await synclet1.stop();
204170
await connector1.connect();
@@ -211,7 +177,7 @@ describe('table sync, basics', () => {
211177
);
212178

213179
await synclet1.start();
214-
expectEquivalentConnectors(connector1, connector2, {r1: {c1: 'C2'}});
180+
expectEquivalentConnectors([connector1, connector2], {r1: {c1: 'C2'}});
215181
});
216182

217183
test('stop 1, set 2, start 1', async () => {
@@ -222,7 +188,7 @@ describe('table sync, basics', () => {
222188
await synclet2.start();
223189

224190
await connector1.setCellForTest('r1', 'c1', 'C1');
225-
expectEquivalentConnectors(connector1, connector2, {r1: {c1: 'C1'}});
191+
expectEquivalentConnectors([connector1, connector2], {r1: {c1: 'C1'}});
226192

227193
await synclet1.stop();
228194
await connector2.setCellForTest('r1', 'c1', 'C2');
@@ -234,7 +200,7 @@ describe('table sync, basics', () => {
234200
);
235201

236202
await synclet1.start();
237-
expectEquivalentConnectors(connector1, connector2, {r1: {c1: 'C2'}});
203+
expectEquivalentConnectors([connector1, connector2], {r1: {c1: 'C2'}});
238204
});
239205

240206
test('set 1, set 2, start 2, start 1', async () => {
@@ -243,7 +209,7 @@ describe('table sync, basics', () => {
243209

244210
await connector1.connect();
245211
await connector1.setCellForTest('r1', 'c1', 'C1');
246-
expectDifferingConnectors(connector1, connector2, {r1: {c1: 'C1'}});
212+
expectDifferingConnectors(connector1, connector2, {r1: {c1: 'C1'}}, {});
247213

248214
await pause();
249215

@@ -258,19 +224,17 @@ describe('table sync, basics', () => {
258224

259225
await synclet2.start();
260226
await synclet1.start();
261-
expectEquivalentConnectors(connector1, connector2, {r1: {c1: 'C2'}});
227+
expectEquivalentConnectors([connector1, connector2], {r1: {c1: 'C2'}});
262228
});
263-
});
264229

265-
describe('table sync, multiple values', () => {
266230
test('connected, different values 1', async () => {
267231
const [[synclet1, connector1], [synclet2, connector2]] =
268232
await getTestSyncletsAndConnectors(createTestTableConnector, 2);
269233
await synclet1.start();
270234
await synclet2.start();
271235
await connector1.setCellForTest('r1', 'c1', 'C1');
272236
await connector2.setCellForTest('r1', 'c2', 'C2');
273-
expectEquivalentConnectors(connector1, connector2, {
237+
expectEquivalentConnectors([connector1, connector2], {
274238
r1: {c1: 'C1', c2: 'C2'},
275239
});
276240
});
@@ -281,7 +245,7 @@ describe('table sync, multiple values', () => {
281245
await synclet2.start();
282246
await connector1.setCellForTest('r1', 'c1', 'C1');
283247
await connector2.setCellForTest('r2', 'c2', 'C2');
284-
expectEquivalentConnectors(connector1, connector2, {
248+
expectEquivalentConnectors([connector1, connector2], {
285249
r1: {c1: 'C1'},
286250
r2: {c2: 'C2'},
287251
});
@@ -295,7 +259,7 @@ describe('table sync, multiple values', () => {
295259
await connector2.setCellForTest('r1', 'c2', 'C2');
296260
await synclet1.start();
297261
await synclet2.start();
298-
expectEquivalentConnectors(connector1, connector2, {
262+
expectEquivalentConnectors([connector1, connector2], {
299263
r1: {c1: 'C1', c2: 'C2'},
300264
});
301265
});
@@ -308,7 +272,7 @@ describe('table sync, multiple values', () => {
308272
await connector2.setCellForTest('r2', 'c2', 'C2');
309273
await synclet1.start();
310274
await synclet2.start();
311-
expectEquivalentConnectors(connector1, connector2, {
275+
expectEquivalentConnectors([connector1, connector2], {
312276
r1: {c1: 'C1'},
313277
r2: {c2: 'C2'},
314278
});
@@ -325,7 +289,7 @@ describe('table sync, multiple values', () => {
325289
await connector2.setCellForTest('r1', 'c3', 'C3');
326290
await synclet1.start();
327291
await synclet2.start();
328-
expectEquivalentConnectors(connector1, connector2, {
292+
expectEquivalentConnectors([connector1, connector2], {
329293
r1: {c1: 'C1', c2: 'C3', c3: 'C3'},
330294
});
331295
});

0 commit comments

Comments
 (0)