@dxos/feed 0.8.4-main.fbb7a13 → 0.8.4-main.fcfe5033a5
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 +6 -2
- package/dist/lib/neutral/chunk-53CMKQL5.mjs +734 -0
- package/dist/lib/neutral/chunk-53CMKQL5.mjs.map +7 -0
- package/dist/lib/neutral/index.mjs +13 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/lib/neutral/testing/index.mjs +180 -0
- package/dist/lib/neutral/testing/index.mjs.map +7 -0
- package/dist/types/src/errors.d.ts +39 -0
- package/dist/types/src/errors.d.ts.map +1 -0
- package/dist/types/src/feed-store.d.ts +103 -9
- package/dist/types/src/feed-store.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +3 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/sync-client.d.ts +54 -0
- package/dist/types/src/sync-client.d.ts.map +1 -0
- package/dist/types/src/sync-server.d.ts +28 -0
- package/dist/types/src/sync-server.d.ts.map +1 -0
- package/dist/types/src/testing/index.d.ts +1 -1
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/test-builder.d.ts +78 -0
- package/dist/types/src/testing/test-builder.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +19 -14
- package/src/errors.ts +20 -0
- package/src/feed-store.test.ts +394 -39
- package/src/feed-store.ts +493 -140
- package/src/index.ts +3 -1
- package/src/sync-client.ts +195 -0
- package/src/sync-server.ts +96 -0
- package/src/sync.test.ts +151 -91
- package/src/testing/index.ts +1 -1
- package/src/testing/test-builder.ts +223 -0
- package/dist/lib/browser/index.mjs +0 -342
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/browser/testing/index.mjs +0 -1
- package/dist/lib/node-esm/index.mjs +0 -344
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/lib/node-esm/testing/index.mjs +0 -2
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- package/dist/types/src/protocol.d.ts +0 -120
- package/dist/types/src/protocol.d.ts.map +0 -1
- package/src/protocol.ts +0 -110
- /package/dist/lib/{browser/testing → neutral}/index.mjs.map +0 -0
package/src/feed-store.test.ts
CHANGED
|
@@ -5,20 +5,29 @@
|
|
|
5
5
|
import * as SqliteClient from '@effect/sql-sqlite-node/SqliteClient';
|
|
6
6
|
import { describe, expect, it } from '@effect/vitest';
|
|
7
7
|
import * as Effect from 'effect/Effect';
|
|
8
|
+
import * as Layer from 'effect/Layer';
|
|
8
9
|
|
|
9
10
|
import { ObjectId, SpaceId } from '@dxos/keys';
|
|
10
11
|
import { log } from '@dxos/log';
|
|
12
|
+
import { FeedProtocol } from '@dxos/protocols';
|
|
13
|
+
import { SqlTransaction } from '@dxos/sql-sqlite';
|
|
11
14
|
|
|
12
15
|
import { FeedStore } from './feed-store';
|
|
13
|
-
import { Block } from './protocol';
|
|
14
16
|
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
const Block = FeedProtocol.Block;
|
|
18
|
+
type Block = FeedProtocol.Block;
|
|
19
|
+
const WellKnownNamespaces = FeedProtocol.WellKnownNamespaces;
|
|
20
|
+
|
|
21
|
+
const TestLayer = SqlTransaction.layer.pipe(
|
|
22
|
+
Layer.provideMerge(
|
|
23
|
+
SqliteClient.layer({
|
|
24
|
+
filename: ':memory:',
|
|
25
|
+
}),
|
|
26
|
+
),
|
|
27
|
+
);
|
|
18
28
|
|
|
19
29
|
// ActorIds.
|
|
20
30
|
const ALICE = 'alice';
|
|
21
|
-
const BOB = 'bob';
|
|
22
31
|
|
|
23
32
|
describe('Feed V2', () => {
|
|
24
33
|
it.effect('should append and query blocks via RPC', () =>
|
|
@@ -31,23 +40,34 @@ describe('Feed V2', () => {
|
|
|
31
40
|
|
|
32
41
|
// Append
|
|
33
42
|
const block: Block = {
|
|
34
|
-
feedId
|
|
43
|
+
feedId,
|
|
35
44
|
actorId: feedId,
|
|
36
45
|
sequence: 123, // Author sequence provided by peer
|
|
37
|
-
|
|
38
|
-
|
|
46
|
+
prevActorId: null,
|
|
47
|
+
prevSequence: null,
|
|
39
48
|
position: null, // Input doesn't have position
|
|
40
49
|
timestamp: Date.now(),
|
|
41
50
|
data: new Uint8Array([1, 2, 3]),
|
|
42
51
|
};
|
|
43
52
|
|
|
44
|
-
const appendRes = yield* feed.append({
|
|
53
|
+
const appendRes = yield* feed.append({
|
|
54
|
+
requestId: 'req-1',
|
|
55
|
+
blocks: [block],
|
|
56
|
+
spaceId,
|
|
57
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
58
|
+
});
|
|
45
59
|
expect(appendRes.positions.length).toBe(1);
|
|
46
60
|
expect(appendRes.positions[0]).toBeDefined();
|
|
47
61
|
expect(appendRes.requestId).toBe('req-1');
|
|
48
62
|
|
|
49
63
|
// Query by feedId
|
|
50
|
-
const queryRes = yield* feed.query({
|
|
64
|
+
const queryRes = yield* feed.query({
|
|
65
|
+
requestId: 'req-2',
|
|
66
|
+
query: { feedIds: [feedId] },
|
|
67
|
+
position: -1,
|
|
68
|
+
spaceId,
|
|
69
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
70
|
+
}); // Use position -1 to get everything
|
|
51
71
|
expect(queryRes.blocks.length).toBe(1);
|
|
52
72
|
expect(queryRes.blocks[0].position).toBe(appendRes.positions[0]);
|
|
53
73
|
expect(queryRes.blocks[0].sequence).toBe(123); // Verify Author Sequence is preserved
|
|
@@ -59,24 +79,24 @@ describe('Feed V2', () => {
|
|
|
59
79
|
Effect.gen(function* () {
|
|
60
80
|
const spaceId = SpaceId.random();
|
|
61
81
|
const feedId = ObjectId.random();
|
|
62
|
-
const
|
|
82
|
+
const feedNamespace = WellKnownNamespaces.data;
|
|
63
83
|
|
|
64
84
|
const feed = new FeedStore({ localActorId: ALICE, assignPositions: true });
|
|
65
85
|
yield* feed.migrate();
|
|
66
86
|
|
|
67
87
|
// Append with namespace
|
|
68
88
|
const block = Block.make({
|
|
69
|
-
feedId
|
|
89
|
+
feedId,
|
|
70
90
|
actorId: ALICE,
|
|
71
91
|
sequence: 1,
|
|
72
|
-
|
|
73
|
-
|
|
92
|
+
prevActorId: null,
|
|
93
|
+
prevSequence: null,
|
|
74
94
|
position: null,
|
|
75
95
|
timestamp: Date.now(),
|
|
76
96
|
data: new Uint8Array([1]),
|
|
77
97
|
});
|
|
78
98
|
|
|
79
|
-
yield* feed.append({ requestId: 'req-ns', blocks: [block],
|
|
99
|
+
yield* feed.append({ requestId: 'req-ns', blocks: [block], spaceId, feedNamespace });
|
|
80
100
|
|
|
81
101
|
// Verify directly from DB (white-box test) to ensure schema is correct
|
|
82
102
|
const sql = yield* SqliteClient.SqliteClient;
|
|
@@ -84,7 +104,7 @@ describe('Feed V2', () => {
|
|
|
84
104
|
SELECT feedNamespace FROM feeds WHERE spaceId = ${spaceId} AND feedId = ${feedId}
|
|
85
105
|
`;
|
|
86
106
|
expect(rows.length).toBeGreaterThan(0);
|
|
87
|
-
expect(rows[0].feedNamespace).toBe(
|
|
107
|
+
expect(rows[0].feedNamespace).toBe(feedNamespace);
|
|
88
108
|
}).pipe(Effect.provide(TestLayer)),
|
|
89
109
|
);
|
|
90
110
|
|
|
@@ -101,19 +121,18 @@ describe('Feed V2', () => {
|
|
|
101
121
|
requestId: 'req-1',
|
|
102
122
|
blocks: [
|
|
103
123
|
Block.make({
|
|
104
|
-
feedId
|
|
124
|
+
feedId,
|
|
105
125
|
actorId: feedId,
|
|
106
126
|
sequence: 1,
|
|
107
|
-
|
|
108
|
-
|
|
127
|
+
prevActorId: null,
|
|
128
|
+
prevSequence: null,
|
|
109
129
|
position: null,
|
|
110
130
|
timestamp: Date.now(),
|
|
111
131
|
data: new Uint8Array([1]),
|
|
112
132
|
}),
|
|
113
133
|
],
|
|
114
134
|
spaceId,
|
|
115
|
-
|
|
116
|
-
namespace: 'data',
|
|
135
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
117
136
|
});
|
|
118
137
|
|
|
119
138
|
// Subscribe
|
|
@@ -125,6 +144,7 @@ describe('Feed V2', () => {
|
|
|
125
144
|
const queryRes = yield* feed.query({
|
|
126
145
|
requestId: 'req-3',
|
|
127
146
|
spaceId,
|
|
147
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
128
148
|
query: { subscriptionId: subRes.subscriptionId },
|
|
129
149
|
position: 0,
|
|
130
150
|
});
|
|
@@ -133,6 +153,37 @@ describe('Feed V2', () => {
|
|
|
133
153
|
}).pipe(Effect.provide(TestLayer)),
|
|
134
154
|
);
|
|
135
155
|
|
|
156
|
+
it.effect('should allow position query with unpositionedOnly false', () =>
|
|
157
|
+
Effect.gen(function* () {
|
|
158
|
+
const spaceId = SpaceId.random();
|
|
159
|
+
const feedId = ObjectId.random();
|
|
160
|
+
|
|
161
|
+
const feed = new FeedStore({ localActorId: ALICE, assignPositions: true });
|
|
162
|
+
yield* feed.migrate();
|
|
163
|
+
|
|
164
|
+
yield* feed.appendLocal([
|
|
165
|
+
{
|
|
166
|
+
spaceId,
|
|
167
|
+
feedId,
|
|
168
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
169
|
+
data: new Uint8Array([1]),
|
|
170
|
+
},
|
|
171
|
+
]);
|
|
172
|
+
|
|
173
|
+
const queryRes = yield* feed.query({
|
|
174
|
+
requestId: 'req-position-false',
|
|
175
|
+
spaceId,
|
|
176
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
177
|
+
query: { feedIds: [feedId] },
|
|
178
|
+
position: -1,
|
|
179
|
+
unpositionedOnly: false,
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
expect(queryRes.blocks.length).toBe(1);
|
|
183
|
+
expect(queryRes.blocks[0].feedId).toBe(feedId);
|
|
184
|
+
}).pipe(Effect.provide(TestLayer)),
|
|
185
|
+
);
|
|
186
|
+
|
|
136
187
|
it.effect('should assign monotonic insertionId', () =>
|
|
137
188
|
Effect.gen(function* () {
|
|
138
189
|
const feedStore = new FeedStore({ localActorId: ALICE, assignPositions: true });
|
|
@@ -144,7 +195,7 @@ describe('Feed V2', () => {
|
|
|
144
195
|
{
|
|
145
196
|
spaceId,
|
|
146
197
|
feedId: 'feed-1',
|
|
147
|
-
feedNamespace:
|
|
198
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
148
199
|
data: new Uint8Array([1]),
|
|
149
200
|
},
|
|
150
201
|
]);
|
|
@@ -153,7 +204,7 @@ describe('Feed V2', () => {
|
|
|
153
204
|
{
|
|
154
205
|
spaceId,
|
|
155
206
|
feedId: 'feed-2',
|
|
156
|
-
feedNamespace:
|
|
207
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
157
208
|
data: new Uint8Array([2]),
|
|
158
209
|
},
|
|
159
210
|
]);
|
|
@@ -161,6 +212,7 @@ describe('Feed V2', () => {
|
|
|
161
212
|
const result1 = yield* feedStore.query({
|
|
162
213
|
requestId: 'req1',
|
|
163
214
|
spaceId,
|
|
215
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
164
216
|
query: { feedIds: ['feed-1'] },
|
|
165
217
|
position: -1,
|
|
166
218
|
});
|
|
@@ -168,6 +220,7 @@ describe('Feed V2', () => {
|
|
|
168
220
|
const result2 = yield* feedStore.query({
|
|
169
221
|
requestId: 'req2',
|
|
170
222
|
spaceId,
|
|
223
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
171
224
|
query: { feedIds: ['feed-2'] },
|
|
172
225
|
position: -1,
|
|
173
226
|
});
|
|
@@ -190,7 +243,7 @@ describe('Feed V2', () => {
|
|
|
190
243
|
{
|
|
191
244
|
spaceId,
|
|
192
245
|
feedId: 'feed-1',
|
|
193
|
-
feedNamespace:
|
|
246
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
194
247
|
data: new Uint8Array([1]),
|
|
195
248
|
},
|
|
196
249
|
]);
|
|
@@ -198,7 +251,7 @@ describe('Feed V2', () => {
|
|
|
198
251
|
{
|
|
199
252
|
spaceId,
|
|
200
253
|
feedId: 'feed-2',
|
|
201
|
-
feedNamespace:
|
|
254
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
202
255
|
data: new Uint8Array([2]),
|
|
203
256
|
},
|
|
204
257
|
]);
|
|
@@ -206,7 +259,7 @@ describe('Feed V2', () => {
|
|
|
206
259
|
{
|
|
207
260
|
spaceId,
|
|
208
261
|
feedId: 'feed-1',
|
|
209
|
-
feedNamespace:
|
|
262
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
210
263
|
data: new Uint8Array([3]),
|
|
211
264
|
},
|
|
212
265
|
]);
|
|
@@ -215,12 +268,14 @@ describe('Feed V2', () => {
|
|
|
215
268
|
const feed1Res = yield* feedStore.query({
|
|
216
269
|
requestId: 'req1',
|
|
217
270
|
spaceId,
|
|
271
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
218
272
|
query: { feedIds: ['feed-1'] },
|
|
219
273
|
cursor: undefined,
|
|
220
274
|
});
|
|
221
275
|
const feed2Res = yield* feedStore.query({
|
|
222
276
|
requestId: 'req2',
|
|
223
277
|
spaceId,
|
|
278
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
224
279
|
query: { feedIds: ['feed-2'] },
|
|
225
280
|
cursor: undefined,
|
|
226
281
|
});
|
|
@@ -243,6 +298,7 @@ describe('Feed V2', () => {
|
|
|
243
298
|
.query({
|
|
244
299
|
requestId: 'req-bad',
|
|
245
300
|
spaceId,
|
|
301
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
246
302
|
query: { feedIds: ['feed-1'] },
|
|
247
303
|
cursor: invalidCursor as any,
|
|
248
304
|
})
|
|
@@ -275,6 +331,7 @@ describe('Feed V2', () => {
|
|
|
275
331
|
const nextRes = yield* feedStore.query({
|
|
276
332
|
requestId: 'req-next',
|
|
277
333
|
spaceId,
|
|
334
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
278
335
|
query: { feedIds: ['feed-1'] },
|
|
279
336
|
cursor: validCursor as any,
|
|
280
337
|
});
|
|
@@ -296,24 +353,118 @@ describe('Feed V2', () => {
|
|
|
296
353
|
{
|
|
297
354
|
spaceId,
|
|
298
355
|
feedId,
|
|
299
|
-
feedNamespace:
|
|
356
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
300
357
|
data: new Uint8Array([1]),
|
|
301
358
|
},
|
|
302
359
|
]);
|
|
303
360
|
expect(blocks.length).toBe(1);
|
|
304
|
-
expect(blocks[0].position).
|
|
361
|
+
expect(blocks[0].position).toBeDefined();
|
|
362
|
+
expect(blocks[0].position).toBeGreaterThanOrEqual(0);
|
|
305
363
|
expect(blocks[0].sequence).toBe(0);
|
|
306
364
|
expect(blocks[0].actorId).toBe(ALICE);
|
|
307
|
-
expect(blocks[0].
|
|
308
|
-
expect(blocks[0].
|
|
365
|
+
expect(blocks[0].prevActorId).toBeNull();
|
|
366
|
+
expect(blocks[0].prevSequence).toBeNull();
|
|
309
367
|
expect(blocks[0].timestamp).toBeGreaterThan(0);
|
|
310
368
|
expect(blocks[0].data).toEqual(new Uint8Array([1]));
|
|
311
369
|
|
|
312
|
-
// Query by feedId
|
|
313
|
-
const queryRes = yield* feed.query({
|
|
314
|
-
|
|
370
|
+
// Query by feedId: persisted position matches returned block position.
|
|
371
|
+
const queryRes = yield* feed.query({
|
|
372
|
+
query: { feedIds: [feedId] },
|
|
373
|
+
position: -1,
|
|
374
|
+
spaceId,
|
|
375
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
376
|
+
});
|
|
315
377
|
expect(queryRes.blocks.length).toBe(1);
|
|
316
|
-
expect(queryRes.blocks[0]).
|
|
378
|
+
expect(queryRes.blocks[0].position).toBe(blocks[0].position);
|
|
379
|
+
expect(queryRes.blocks[0]).toMatchObject({ ...blocks[0], feedId });
|
|
380
|
+
}).pipe(Effect.provide(TestLayer)),
|
|
381
|
+
);
|
|
382
|
+
|
|
383
|
+
it.effect('appendLocal returns blocks with positions from append when assignPositions is true', () =>
|
|
384
|
+
Effect.gen(function* () {
|
|
385
|
+
const spaceId = SpaceId.random();
|
|
386
|
+
const feed = new FeedStore({ localActorId: ALICE, assignPositions: true });
|
|
387
|
+
yield* feed.migrate();
|
|
388
|
+
|
|
389
|
+
const blocks = yield* feed.appendLocal([
|
|
390
|
+
{ spaceId, feedId: 'feed-a', feedNamespace: WellKnownNamespaces.data, data: new Uint8Array([1]) },
|
|
391
|
+
{ spaceId, feedId: 'feed-b', feedNamespace: WellKnownNamespaces.data, data: new Uint8Array([2]) },
|
|
392
|
+
{ spaceId, feedId: 'feed-a', feedNamespace: WellKnownNamespaces.data, data: new Uint8Array([3]) },
|
|
393
|
+
]);
|
|
394
|
+
|
|
395
|
+
expect(blocks.length).toBe(3);
|
|
396
|
+
expect(blocks.every((block) => block.position != null && block.position >= 0)).toBe(true);
|
|
397
|
+
expect(blocks[0].position).toBeLessThan(blocks[1].position!);
|
|
398
|
+
expect(blocks[1].position).toBeLessThan(blocks[2].position!);
|
|
399
|
+
}).pipe(Effect.provide(TestLayer)),
|
|
400
|
+
);
|
|
401
|
+
|
|
402
|
+
it.effect('assigns positions independently per feed and namespace pair', () =>
|
|
403
|
+
Effect.gen(function* () {
|
|
404
|
+
const feed = new FeedStore({ localActorId: ALICE, assignPositions: true });
|
|
405
|
+
yield* feed.migrate();
|
|
406
|
+
|
|
407
|
+
const spaceA = SpaceId.random();
|
|
408
|
+
const spaceB = SpaceId.random();
|
|
409
|
+
|
|
410
|
+
const nsData = WellKnownNamespaces.data;
|
|
411
|
+
const nsTrace = WellKnownNamespaces.trace;
|
|
412
|
+
|
|
413
|
+
// First append in each (spaceId, feedNamespace) pair starts at 0.
|
|
414
|
+
const feedAData = ObjectId.random();
|
|
415
|
+
const firstAData = yield* feed.appendLocal([
|
|
416
|
+
{
|
|
417
|
+
spaceId: spaceA,
|
|
418
|
+
feedId: feedAData,
|
|
419
|
+
feedNamespace: nsData,
|
|
420
|
+
data: new Uint8Array([1]),
|
|
421
|
+
},
|
|
422
|
+
]);
|
|
423
|
+
const feedATrace = ObjectId.random();
|
|
424
|
+
const firstATrace = yield* feed.appendLocal([
|
|
425
|
+
{
|
|
426
|
+
spaceId: spaceA,
|
|
427
|
+
feedId: feedATrace,
|
|
428
|
+
feedNamespace: nsTrace,
|
|
429
|
+
data: new Uint8Array([2]),
|
|
430
|
+
},
|
|
431
|
+
]);
|
|
432
|
+
const feedBData = ObjectId.random();
|
|
433
|
+
const firstBData = yield* feed.appendLocal([
|
|
434
|
+
{
|
|
435
|
+
spaceId: spaceB,
|
|
436
|
+
feedId: feedBData,
|
|
437
|
+
feedNamespace: nsData,
|
|
438
|
+
data: new Uint8Array([3]),
|
|
439
|
+
},
|
|
440
|
+
]);
|
|
441
|
+
const feedBTrace = ObjectId.random();
|
|
442
|
+
const firstBHalo = yield* feed.appendLocal([
|
|
443
|
+
{
|
|
444
|
+
spaceId: spaceB,
|
|
445
|
+
feedId: feedBTrace,
|
|
446
|
+
feedNamespace: nsTrace,
|
|
447
|
+
data: new Uint8Array([4]),
|
|
448
|
+
},
|
|
449
|
+
]);
|
|
450
|
+
|
|
451
|
+
// A second append in one pair should only advance that specific pair.
|
|
452
|
+
const feedAData2 = ObjectId.random();
|
|
453
|
+
const secondAData = yield* feed.appendLocal([
|
|
454
|
+
{
|
|
455
|
+
spaceId: spaceA,
|
|
456
|
+
feedId: feedAData2,
|
|
457
|
+
feedNamespace: nsData,
|
|
458
|
+
data: new Uint8Array([5]),
|
|
459
|
+
},
|
|
460
|
+
]);
|
|
461
|
+
|
|
462
|
+
// appendLocal returns blocks with positions matching persisted values.
|
|
463
|
+
expect(firstAData[0].position).toBe(0);
|
|
464
|
+
expect(firstATrace[0].position).toBe(0);
|
|
465
|
+
expect(firstBData[0].position).toBe(0);
|
|
466
|
+
expect(firstBHalo[0].position).toBe(0);
|
|
467
|
+
expect(secondAData[0].position).toBe(1);
|
|
317
468
|
}).pipe(Effect.provide(TestLayer)),
|
|
318
469
|
);
|
|
319
470
|
|
|
@@ -329,15 +480,24 @@ describe('Feed V2', () => {
|
|
|
329
480
|
{
|
|
330
481
|
spaceId,
|
|
331
482
|
feedId,
|
|
332
|
-
feedNamespace:
|
|
483
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
333
484
|
data: new Uint8Array([1]),
|
|
334
485
|
},
|
|
335
486
|
]);
|
|
336
|
-
const query1 = yield* feed.query({
|
|
487
|
+
const query1 = yield* feed.query({
|
|
488
|
+
spaceId,
|
|
489
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
490
|
+
query: { feedIds: [feedId] },
|
|
491
|
+
}); // Use position '-1' to get everything
|
|
337
492
|
log.info('query 1', { blocks: query1.blocks.length, cursor: query1.nextCursor });
|
|
338
493
|
expect(query1.blocks.length).toBe(1);
|
|
339
494
|
|
|
340
|
-
const query2 = yield* feed.query({
|
|
495
|
+
const query2 = yield* feed.query({
|
|
496
|
+
spaceId,
|
|
497
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
498
|
+
query: { feedIds: [feedId] },
|
|
499
|
+
cursor: query1.nextCursor,
|
|
500
|
+
});
|
|
341
501
|
log.info('query 2', { blocks: query2.blocks.length, cursor: query2.nextCursor });
|
|
342
502
|
expect(query2.blocks.length).toBe(0);
|
|
343
503
|
|
|
@@ -345,13 +505,208 @@ describe('Feed V2', () => {
|
|
|
345
505
|
{
|
|
346
506
|
spaceId,
|
|
347
507
|
feedId,
|
|
348
|
-
feedNamespace:
|
|
508
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
349
509
|
data: new Uint8Array([2]),
|
|
350
510
|
},
|
|
351
511
|
]);
|
|
352
|
-
const query3 = yield* feed.query({
|
|
512
|
+
const query3 = yield* feed.query({
|
|
513
|
+
spaceId,
|
|
514
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
515
|
+
query: { feedIds: [feedId] },
|
|
516
|
+
cursor: query2.nextCursor,
|
|
517
|
+
});
|
|
353
518
|
log.info('query 3', { blocks: query3.blocks.length, cursor: query3.nextCursor });
|
|
354
519
|
expect(query3.blocks.length).toBe(1);
|
|
355
520
|
}).pipe(Effect.provide(TestLayer)),
|
|
356
521
|
);
|
|
522
|
+
|
|
523
|
+
it.effect('countBlocks and deleteOldestBlocks operate per feed within space and namespace', () =>
|
|
524
|
+
Effect.gen(function* () {
|
|
525
|
+
const feed = new FeedStore({ localActorId: ALICE, assignPositions: true });
|
|
526
|
+
yield* feed.migrate();
|
|
527
|
+
|
|
528
|
+
const spaceA = SpaceId.random();
|
|
529
|
+
const spaceB = SpaceId.random();
|
|
530
|
+
const nsData = WellKnownNamespaces.data;
|
|
531
|
+
const nsTrace = WellKnownNamespaces.trace;
|
|
532
|
+
const targetFeedId = 'feed-target';
|
|
533
|
+
|
|
534
|
+
// Target feed in target namespace/space.
|
|
535
|
+
yield* feed.appendLocal([
|
|
536
|
+
{ spaceId: spaceA, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([1]) },
|
|
537
|
+
{ spaceId: spaceA, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([2]) },
|
|
538
|
+
{ spaceId: spaceA, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([3]) },
|
|
539
|
+
{ spaceId: spaceA, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([4]) },
|
|
540
|
+
{ spaceId: spaceA, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([5]) },
|
|
541
|
+
]);
|
|
542
|
+
|
|
543
|
+
// Data that must not be affected by target cleanup.
|
|
544
|
+
yield* feed.appendLocal([
|
|
545
|
+
{ spaceId: spaceA, feedId: 'feed-other', feedNamespace: nsData, data: new Uint8Array([11]) },
|
|
546
|
+
{ spaceId: spaceA, feedId: 'feed-trace', feedNamespace: nsTrace, data: new Uint8Array([12]) },
|
|
547
|
+
{ spaceId: spaceB, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([13]) },
|
|
548
|
+
]);
|
|
549
|
+
|
|
550
|
+
const beforeCount = yield* feed.countBlocks({
|
|
551
|
+
spaceId: spaceA,
|
|
552
|
+
feedNamespace: nsData,
|
|
553
|
+
feedId: targetFeedId,
|
|
554
|
+
});
|
|
555
|
+
expect(beforeCount).toBe(5);
|
|
556
|
+
|
|
557
|
+
const deleted = yield* feed.deleteOldestBlocks({
|
|
558
|
+
spaceId: spaceA,
|
|
559
|
+
feedNamespace: nsData,
|
|
560
|
+
feedId: targetFeedId,
|
|
561
|
+
count: 3,
|
|
562
|
+
});
|
|
563
|
+
expect(deleted).toBe(3);
|
|
564
|
+
|
|
565
|
+
const afterCount = yield* feed.countBlocks({
|
|
566
|
+
spaceId: spaceA,
|
|
567
|
+
feedNamespace: nsData,
|
|
568
|
+
feedId: targetFeedId,
|
|
569
|
+
});
|
|
570
|
+
expect(afterCount).toBe(2);
|
|
571
|
+
|
|
572
|
+
const targetRemaining = yield* feed.query({
|
|
573
|
+
requestId: 'req-target-remaining',
|
|
574
|
+
spaceId: spaceA,
|
|
575
|
+
feedNamespace: nsData,
|
|
576
|
+
query: { feedIds: [targetFeedId] },
|
|
577
|
+
position: -1,
|
|
578
|
+
});
|
|
579
|
+
expect(targetRemaining.blocks.length).toBe(2);
|
|
580
|
+
expect(targetRemaining.blocks.map((block) => block.data[0])).toEqual([4, 5]);
|
|
581
|
+
|
|
582
|
+
const unaffectedFeed = yield* feed.countBlocks({
|
|
583
|
+
spaceId: spaceA,
|
|
584
|
+
feedNamespace: nsData,
|
|
585
|
+
feedId: 'feed-other',
|
|
586
|
+
});
|
|
587
|
+
expect(unaffectedFeed).toBe(1);
|
|
588
|
+
|
|
589
|
+
const unaffectedNamespace = yield* feed.countBlocks({
|
|
590
|
+
spaceId: spaceA,
|
|
591
|
+
feedNamespace: nsTrace,
|
|
592
|
+
feedId: 'feed-trace',
|
|
593
|
+
});
|
|
594
|
+
expect(unaffectedNamespace).toBe(1);
|
|
595
|
+
|
|
596
|
+
const unaffectedSpace = yield* feed.countBlocks({
|
|
597
|
+
spaceId: spaceB,
|
|
598
|
+
feedNamespace: nsData,
|
|
599
|
+
feedId: targetFeedId,
|
|
600
|
+
});
|
|
601
|
+
expect(unaffectedSpace).toBe(1);
|
|
602
|
+
}).pipe(Effect.provide(TestLayer)),
|
|
603
|
+
);
|
|
604
|
+
|
|
605
|
+
it.effect('deleteOldestBlocks handles non-positive and oversized delete counts', () =>
|
|
606
|
+
Effect.gen(function* () {
|
|
607
|
+
const spaceId = SpaceId.random();
|
|
608
|
+
const feedId = 'feed-delete-count';
|
|
609
|
+
const feed = new FeedStore({ localActorId: ALICE, assignPositions: true });
|
|
610
|
+
yield* feed.migrate();
|
|
611
|
+
|
|
612
|
+
yield* feed.appendLocal([
|
|
613
|
+
{ spaceId, feedId, feedNamespace: WellKnownNamespaces.data, data: new Uint8Array([1]) },
|
|
614
|
+
{ spaceId, feedId, feedNamespace: WellKnownNamespaces.data, data: new Uint8Array([2]) },
|
|
615
|
+
]);
|
|
616
|
+
|
|
617
|
+
const deletedZero = yield* feed.deleteOldestBlocks({
|
|
618
|
+
spaceId,
|
|
619
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
620
|
+
feedId,
|
|
621
|
+
count: 0,
|
|
622
|
+
});
|
|
623
|
+
expect(deletedZero).toBe(0);
|
|
624
|
+
|
|
625
|
+
const deletedNegative = yield* feed.deleteOldestBlocks({
|
|
626
|
+
spaceId,
|
|
627
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
628
|
+
feedId,
|
|
629
|
+
count: -5,
|
|
630
|
+
});
|
|
631
|
+
expect(deletedNegative).toBe(0);
|
|
632
|
+
|
|
633
|
+
const deletedOversized = yield* feed.deleteOldestBlocks({
|
|
634
|
+
spaceId,
|
|
635
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
636
|
+
feedId,
|
|
637
|
+
count: 10,
|
|
638
|
+
});
|
|
639
|
+
expect(deletedOversized).toBe(2);
|
|
640
|
+
|
|
641
|
+
const remainingCount = yield* feed.countBlocks({
|
|
642
|
+
spaceId,
|
|
643
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
644
|
+
feedId,
|
|
645
|
+
});
|
|
646
|
+
expect(remainingCount).toBe(0);
|
|
647
|
+
}).pipe(Effect.provide(TestLayer)),
|
|
648
|
+
);
|
|
649
|
+
|
|
650
|
+
it.effect('append ignores duplicate Lamport timestamp and preserves original data', () =>
|
|
651
|
+
Effect.gen(function* () {
|
|
652
|
+
const spaceId = SpaceId.random();
|
|
653
|
+
const feedId = ObjectId.random();
|
|
654
|
+
const feed = new FeedStore({ localActorId: ALICE, assignPositions: false });
|
|
655
|
+
yield* feed.migrate();
|
|
656
|
+
|
|
657
|
+
const actorId = 'actor-1';
|
|
658
|
+
const sequence = 5;
|
|
659
|
+
const timestamp = Date.now();
|
|
660
|
+
|
|
661
|
+
yield* feed.append({
|
|
662
|
+
requestId: 'req-first',
|
|
663
|
+
spaceId,
|
|
664
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
665
|
+
blocks: [
|
|
666
|
+
Block.make({
|
|
667
|
+
feedId,
|
|
668
|
+
actorId,
|
|
669
|
+
sequence,
|
|
670
|
+
prevActorId: null,
|
|
671
|
+
prevSequence: null,
|
|
672
|
+
position: null,
|
|
673
|
+
timestamp,
|
|
674
|
+
data: new Uint8Array([1]),
|
|
675
|
+
}),
|
|
676
|
+
],
|
|
677
|
+
});
|
|
678
|
+
|
|
679
|
+
// Same Lamport tuple with different data and position should be ignored.
|
|
680
|
+
yield* feed.append({
|
|
681
|
+
requestId: 'req-duplicate',
|
|
682
|
+
spaceId,
|
|
683
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
684
|
+
blocks: [
|
|
685
|
+
Block.make({
|
|
686
|
+
feedId,
|
|
687
|
+
actorId,
|
|
688
|
+
sequence,
|
|
689
|
+
prevActorId: null,
|
|
690
|
+
prevSequence: null,
|
|
691
|
+
position: null,
|
|
692
|
+
timestamp: timestamp + 1,
|
|
693
|
+
data: new Uint8Array([2]),
|
|
694
|
+
}),
|
|
695
|
+
],
|
|
696
|
+
});
|
|
697
|
+
|
|
698
|
+
const queryRes = yield* feed.query({
|
|
699
|
+
requestId: 'req-query',
|
|
700
|
+
spaceId,
|
|
701
|
+
feedNamespace: WellKnownNamespaces.data,
|
|
702
|
+
query: { feedIds: [feedId] },
|
|
703
|
+
position: -1,
|
|
704
|
+
});
|
|
705
|
+
|
|
706
|
+
expect(queryRes.blocks.length).toBe(1);
|
|
707
|
+
expect(queryRes.blocks[0].actorId).toBe(actorId);
|
|
708
|
+
expect(queryRes.blocks[0].sequence).toBe(sequence);
|
|
709
|
+
expect(queryRes.blocks[0].data).toEqual(new Uint8Array([1]));
|
|
710
|
+
}).pipe(Effect.provide(TestLayer)),
|
|
711
|
+
);
|
|
357
712
|
});
|