@dxos/feed 0.0.0 → 0.8.4-main.03d5cd7b56

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.
Files changed (38) hide show
  1. package/README.md +6 -2
  2. package/dist/lib/neutral/chunk-YYASRWLE.mjs +880 -0
  3. package/dist/lib/neutral/chunk-YYASRWLE.mjs.map +7 -0
  4. package/dist/lib/neutral/index.mjs +13 -0
  5. package/dist/lib/neutral/index.mjs.map +7 -0
  6. package/dist/lib/neutral/meta.json +1 -0
  7. package/dist/lib/neutral/testing/index.mjs +174 -0
  8. package/dist/lib/neutral/testing/index.mjs.map +7 -0
  9. package/dist/types/src/errors.d.ts +39 -0
  10. package/dist/types/src/errors.d.ts.map +1 -0
  11. package/dist/types/src/feed-store.d.ts +121 -0
  12. package/dist/types/src/feed-store.d.ts.map +1 -0
  13. package/dist/types/src/feed-store.test.d.ts +2 -0
  14. package/dist/types/src/feed-store.test.d.ts.map +1 -0
  15. package/dist/types/src/index.d.ts +5 -0
  16. package/dist/types/src/index.d.ts.map +1 -0
  17. package/dist/types/src/sync-client.d.ts +54 -0
  18. package/dist/types/src/sync-client.d.ts.map +1 -0
  19. package/dist/types/src/sync-server.d.ts +28 -0
  20. package/dist/types/src/sync-server.d.ts.map +1 -0
  21. package/dist/types/src/sync.test.d.ts +2 -0
  22. package/dist/types/src/sync.test.d.ts.map +1 -0
  23. package/dist/types/src/testing/index.d.ts +2 -0
  24. package/dist/types/src/testing/index.d.ts.map +1 -0
  25. package/dist/types/src/testing/test-builder.d.ts +78 -0
  26. package/dist/types/src/testing/test-builder.d.ts.map +1 -0
  27. package/dist/types/tsconfig.tsbuildinfo +1 -0
  28. package/package.json +23 -21
  29. package/src/errors.ts +20 -0
  30. package/src/feed-store.test.ts +397 -40
  31. package/src/feed-store.ts +508 -142
  32. package/src/index.ts +3 -1
  33. package/src/sync-client.ts +280 -0
  34. package/src/sync-server.ts +150 -0
  35. package/src/sync.test.ts +151 -91
  36. package/src/testing/index.ts +1 -1
  37. package/src/testing/test-builder.ts +223 -0
  38. package/src/protocol.ts +0 -110
@@ -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 TestLayer = SqliteClient.layer({
16
- filename: ':memory:',
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: undefined,
43
+ feedId,
35
44
  actorId: feedId,
36
45
  sequence: 123, // Author sequence provided by peer
37
- predActorId: null,
38
- predSequence: null,
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({ requestId: 'req-1', blocks: [block], spaceId, feedId, namespace: 'data' });
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({ requestId: 'req-2', query: { feedIds: [feedId] }, position: -1, spaceId }); // Use position -1 to get everything
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 namespace = 'data';
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: undefined,
89
+ feedId,
70
90
  actorId: ALICE,
71
91
  sequence: 1,
72
- predActorId: null,
73
- predSequence: null,
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], namespace, spaceId, feedId });
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(namespace);
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: undefined,
124
+ feedId,
105
125
  actorId: feedId,
106
126
  sequence: 1,
107
- predActorId: null,
108
- predSequence: null,
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
- feedId,
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: 'default',
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: 'default',
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: 'default',
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: 'default',
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: 'default',
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
  })
@@ -256,7 +312,9 @@ describe('Feed V2', () => {
256
312
  // Ideally we use Cause.isDie(cause) -> error
257
313
  // But for quick check:
258
314
  let error = cause.value || cause.defect || cause;
259
- if (cause._tag === 'Die') error = cause.value || cause.defect;
315
+ if (cause._tag === 'Die') {
316
+ error = cause.value || cause.defect;
317
+ }
260
318
 
261
319
  expect(error).toBeDefined();
262
320
  expect(error.message).toBe('Cursor token mismatch');
@@ -275,6 +333,7 @@ describe('Feed V2', () => {
275
333
  const nextRes = yield* feedStore.query({
276
334
  requestId: 'req-next',
277
335
  spaceId,
336
+ feedNamespace: WellKnownNamespaces.data,
278
337
  query: { feedIds: ['feed-1'] },
279
338
  cursor: validCursor as any,
280
339
  });
@@ -296,24 +355,118 @@ describe('Feed V2', () => {
296
355
  {
297
356
  spaceId,
298
357
  feedId,
299
- feedNamespace: 'data',
358
+ feedNamespace: WellKnownNamespaces.data,
300
359
  data: new Uint8Array([1]),
301
360
  },
302
361
  ]);
303
362
  expect(blocks.length).toBe(1);
304
- expect(blocks[0].position).toBeNull();
363
+ expect(blocks[0].position).toBeDefined();
364
+ expect(blocks[0].position).toBeGreaterThanOrEqual(0);
305
365
  expect(blocks[0].sequence).toBe(0);
306
366
  expect(blocks[0].actorId).toBe(ALICE);
307
- expect(blocks[0].predActorId).toBeNull();
308
- expect(blocks[0].predSequence).toBeNull();
367
+ expect(blocks[0].prevActorId).toBeNull();
368
+ expect(blocks[0].prevSequence).toBeNull();
309
369
  expect(blocks[0].timestamp).toBeGreaterThan(0);
310
370
  expect(blocks[0].data).toEqual(new Uint8Array([1]));
311
371
 
312
- // Query by feedId
313
- const queryRes = yield* feed.query({ query: { feedIds: [feedId] }, position: -1, spaceId }); // Use position '-1' to get everything
314
- expect(queryRes.blocks.length).toBe(1);
372
+ // Query by feedId: persisted position matches returned block position.
373
+ const queryRes = yield* feed.query({
374
+ query: { feedIds: [feedId] },
375
+ position: -1,
376
+ spaceId,
377
+ feedNamespace: WellKnownNamespaces.data,
378
+ });
315
379
  expect(queryRes.blocks.length).toBe(1);
316
- expect(queryRes.blocks[0]).toMatchObject({ ...blocks[0], feedId, position: expect.any(Number) });
380
+ expect(queryRes.blocks[0].position).toBe(blocks[0].position);
381
+ expect(queryRes.blocks[0]).toMatchObject({ ...blocks[0], feedId });
382
+ }).pipe(Effect.provide(TestLayer)),
383
+ );
384
+
385
+ it.effect('appendLocal returns blocks with positions from append when assignPositions is true', () =>
386
+ Effect.gen(function* () {
387
+ const spaceId = SpaceId.random();
388
+ const feed = new FeedStore({ localActorId: ALICE, assignPositions: true });
389
+ yield* feed.migrate();
390
+
391
+ const blocks = yield* feed.appendLocal([
392
+ { spaceId, feedId: 'feed-a', feedNamespace: WellKnownNamespaces.data, data: new Uint8Array([1]) },
393
+ { spaceId, feedId: 'feed-b', feedNamespace: WellKnownNamespaces.data, data: new Uint8Array([2]) },
394
+ { spaceId, feedId: 'feed-a', feedNamespace: WellKnownNamespaces.data, data: new Uint8Array([3]) },
395
+ ]);
396
+
397
+ expect(blocks.length).toBe(3);
398
+ expect(blocks.every((block) => block.position != null && block.position >= 0)).toBe(true);
399
+ expect(blocks[0].position).toBeLessThan(blocks[1].position!);
400
+ expect(blocks[1].position).toBeLessThan(blocks[2].position!);
401
+ }).pipe(Effect.provide(TestLayer)),
402
+ );
403
+
404
+ it.effect('assigns positions independently per feed and namespace pair', () =>
405
+ Effect.gen(function* () {
406
+ const feed = new FeedStore({ localActorId: ALICE, assignPositions: true });
407
+ yield* feed.migrate();
408
+
409
+ const spaceA = SpaceId.random();
410
+ const spaceB = SpaceId.random();
411
+
412
+ const nsData = WellKnownNamespaces.data;
413
+ const nsTrace = WellKnownNamespaces.trace;
414
+
415
+ // First append in each (spaceId, feedNamespace) pair starts at 0.
416
+ const feedAData = ObjectId.random();
417
+ const firstAData = yield* feed.appendLocal([
418
+ {
419
+ spaceId: spaceA,
420
+ feedId: feedAData,
421
+ feedNamespace: nsData,
422
+ data: new Uint8Array([1]),
423
+ },
424
+ ]);
425
+ const feedATrace = ObjectId.random();
426
+ const firstATrace = yield* feed.appendLocal([
427
+ {
428
+ spaceId: spaceA,
429
+ feedId: feedATrace,
430
+ feedNamespace: nsTrace,
431
+ data: new Uint8Array([2]),
432
+ },
433
+ ]);
434
+ const feedBData = ObjectId.random();
435
+ const firstBData = yield* feed.appendLocal([
436
+ {
437
+ spaceId: spaceB,
438
+ feedId: feedBData,
439
+ feedNamespace: nsData,
440
+ data: new Uint8Array([3]),
441
+ },
442
+ ]);
443
+ const feedBTrace = ObjectId.random();
444
+ const firstBHalo = yield* feed.appendLocal([
445
+ {
446
+ spaceId: spaceB,
447
+ feedId: feedBTrace,
448
+ feedNamespace: nsTrace,
449
+ data: new Uint8Array([4]),
450
+ },
451
+ ]);
452
+
453
+ // A second append in one pair should only advance that specific pair.
454
+ const feedAData2 = ObjectId.random();
455
+ const secondAData = yield* feed.appendLocal([
456
+ {
457
+ spaceId: spaceA,
458
+ feedId: feedAData2,
459
+ feedNamespace: nsData,
460
+ data: new Uint8Array([5]),
461
+ },
462
+ ]);
463
+
464
+ // appendLocal returns blocks with positions matching persisted values.
465
+ expect(firstAData[0].position).toBe(0);
466
+ expect(firstATrace[0].position).toBe(0);
467
+ expect(firstBData[0].position).toBe(0);
468
+ expect(firstBHalo[0].position).toBe(0);
469
+ expect(secondAData[0].position).toBe(1);
317
470
  }).pipe(Effect.provide(TestLayer)),
318
471
  );
319
472
 
@@ -329,15 +482,24 @@ describe('Feed V2', () => {
329
482
  {
330
483
  spaceId,
331
484
  feedId,
332
- feedNamespace: 'data',
485
+ feedNamespace: WellKnownNamespaces.data,
333
486
  data: new Uint8Array([1]),
334
487
  },
335
488
  ]);
336
- const query1 = yield* feed.query({ spaceId, query: { feedIds: [feedId] } }); // Use position '-1' to get everything
489
+ const query1 = yield* feed.query({
490
+ spaceId,
491
+ feedNamespace: WellKnownNamespaces.data,
492
+ query: { feedIds: [feedId] },
493
+ }); // Use position '-1' to get everything
337
494
  log.info('query 1', { blocks: query1.blocks.length, cursor: query1.nextCursor });
338
495
  expect(query1.blocks.length).toBe(1);
339
496
 
340
- const query2 = yield* feed.query({ spaceId, query: { feedIds: [feedId] }, cursor: query1.nextCursor });
497
+ const query2 = yield* feed.query({
498
+ spaceId,
499
+ feedNamespace: WellKnownNamespaces.data,
500
+ query: { feedIds: [feedId] },
501
+ cursor: query1.nextCursor,
502
+ });
341
503
  log.info('query 2', { blocks: query2.blocks.length, cursor: query2.nextCursor });
342
504
  expect(query2.blocks.length).toBe(0);
343
505
 
@@ -345,13 +507,208 @@ describe('Feed V2', () => {
345
507
  {
346
508
  spaceId,
347
509
  feedId,
348
- feedNamespace: 'data',
510
+ feedNamespace: WellKnownNamespaces.data,
349
511
  data: new Uint8Array([2]),
350
512
  },
351
513
  ]);
352
- const query3 = yield* feed.query({ spaceId, query: { feedIds: [feedId] }, cursor: query2.nextCursor });
514
+ const query3 = yield* feed.query({
515
+ spaceId,
516
+ feedNamespace: WellKnownNamespaces.data,
517
+ query: { feedIds: [feedId] },
518
+ cursor: query2.nextCursor,
519
+ });
353
520
  log.info('query 3', { blocks: query3.blocks.length, cursor: query3.nextCursor });
354
521
  expect(query3.blocks.length).toBe(1);
355
522
  }).pipe(Effect.provide(TestLayer)),
356
523
  );
524
+
525
+ it.effect('countBlocks and deleteOldestBlocks operate per feed within space and namespace', () =>
526
+ Effect.gen(function* () {
527
+ const feed = new FeedStore({ localActorId: ALICE, assignPositions: true });
528
+ yield* feed.migrate();
529
+
530
+ const spaceA = SpaceId.random();
531
+ const spaceB = SpaceId.random();
532
+ const nsData = WellKnownNamespaces.data;
533
+ const nsTrace = WellKnownNamespaces.trace;
534
+ const targetFeedId = 'feed-target';
535
+
536
+ // Target feed in target namespace/space.
537
+ yield* feed.appendLocal([
538
+ { spaceId: spaceA, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([1]) },
539
+ { spaceId: spaceA, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([2]) },
540
+ { spaceId: spaceA, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([3]) },
541
+ { spaceId: spaceA, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([4]) },
542
+ { spaceId: spaceA, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([5]) },
543
+ ]);
544
+
545
+ // Data that must not be affected by target cleanup.
546
+ yield* feed.appendLocal([
547
+ { spaceId: spaceA, feedId: 'feed-other', feedNamespace: nsData, data: new Uint8Array([11]) },
548
+ { spaceId: spaceA, feedId: 'feed-trace', feedNamespace: nsTrace, data: new Uint8Array([12]) },
549
+ { spaceId: spaceB, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([13]) },
550
+ ]);
551
+
552
+ const beforeCount = yield* feed.countBlocks({
553
+ spaceId: spaceA,
554
+ feedNamespace: nsData,
555
+ feedId: targetFeedId,
556
+ });
557
+ expect(beforeCount).toBe(5);
558
+
559
+ const deleted = yield* feed.deleteOldestBlocks({
560
+ spaceId: spaceA,
561
+ feedNamespace: nsData,
562
+ feedId: targetFeedId,
563
+ count: 3,
564
+ });
565
+ expect(deleted).toBe(3);
566
+
567
+ const afterCount = yield* feed.countBlocks({
568
+ spaceId: spaceA,
569
+ feedNamespace: nsData,
570
+ feedId: targetFeedId,
571
+ });
572
+ expect(afterCount).toBe(2);
573
+
574
+ const targetRemaining = yield* feed.query({
575
+ requestId: 'req-target-remaining',
576
+ spaceId: spaceA,
577
+ feedNamespace: nsData,
578
+ query: { feedIds: [targetFeedId] },
579
+ position: -1,
580
+ });
581
+ expect(targetRemaining.blocks.length).toBe(2);
582
+ expect(targetRemaining.blocks.map((block) => block.data[0])).toEqual([4, 5]);
583
+
584
+ const unaffectedFeed = yield* feed.countBlocks({
585
+ spaceId: spaceA,
586
+ feedNamespace: nsData,
587
+ feedId: 'feed-other',
588
+ });
589
+ expect(unaffectedFeed).toBe(1);
590
+
591
+ const unaffectedNamespace = yield* feed.countBlocks({
592
+ spaceId: spaceA,
593
+ feedNamespace: nsTrace,
594
+ feedId: 'feed-trace',
595
+ });
596
+ expect(unaffectedNamespace).toBe(1);
597
+
598
+ const unaffectedSpace = yield* feed.countBlocks({
599
+ spaceId: spaceB,
600
+ feedNamespace: nsData,
601
+ feedId: targetFeedId,
602
+ });
603
+ expect(unaffectedSpace).toBe(1);
604
+ }).pipe(Effect.provide(TestLayer)),
605
+ );
606
+
607
+ it.effect('deleteOldestBlocks handles non-positive and oversized delete counts', () =>
608
+ Effect.gen(function* () {
609
+ const spaceId = SpaceId.random();
610
+ const feedId = 'feed-delete-count';
611
+ const feed = new FeedStore({ localActorId: ALICE, assignPositions: true });
612
+ yield* feed.migrate();
613
+
614
+ yield* feed.appendLocal([
615
+ { spaceId, feedId, feedNamespace: WellKnownNamespaces.data, data: new Uint8Array([1]) },
616
+ { spaceId, feedId, feedNamespace: WellKnownNamespaces.data, data: new Uint8Array([2]) },
617
+ ]);
618
+
619
+ const deletedZero = yield* feed.deleteOldestBlocks({
620
+ spaceId,
621
+ feedNamespace: WellKnownNamespaces.data,
622
+ feedId,
623
+ count: 0,
624
+ });
625
+ expect(deletedZero).toBe(0);
626
+
627
+ const deletedNegative = yield* feed.deleteOldestBlocks({
628
+ spaceId,
629
+ feedNamespace: WellKnownNamespaces.data,
630
+ feedId,
631
+ count: -5,
632
+ });
633
+ expect(deletedNegative).toBe(0);
634
+
635
+ const deletedOversized = yield* feed.deleteOldestBlocks({
636
+ spaceId,
637
+ feedNamespace: WellKnownNamespaces.data,
638
+ feedId,
639
+ count: 10,
640
+ });
641
+ expect(deletedOversized).toBe(2);
642
+
643
+ const remainingCount = yield* feed.countBlocks({
644
+ spaceId,
645
+ feedNamespace: WellKnownNamespaces.data,
646
+ feedId,
647
+ });
648
+ expect(remainingCount).toBe(0);
649
+ }).pipe(Effect.provide(TestLayer)),
650
+ );
651
+
652
+ it.effect('append ignores duplicate Lamport timestamp and preserves original data', () =>
653
+ Effect.gen(function* () {
654
+ const spaceId = SpaceId.random();
655
+ const feedId = ObjectId.random();
656
+ const feed = new FeedStore({ localActorId: ALICE, assignPositions: false });
657
+ yield* feed.migrate();
658
+
659
+ const actorId = 'actor-1';
660
+ const sequence = 5;
661
+ const timestamp = Date.now();
662
+
663
+ yield* feed.append({
664
+ requestId: 'req-first',
665
+ spaceId,
666
+ feedNamespace: WellKnownNamespaces.data,
667
+ blocks: [
668
+ Block.make({
669
+ feedId,
670
+ actorId,
671
+ sequence,
672
+ prevActorId: null,
673
+ prevSequence: null,
674
+ position: null,
675
+ timestamp,
676
+ data: new Uint8Array([1]),
677
+ }),
678
+ ],
679
+ });
680
+
681
+ // Same Lamport tuple with different data and position should be ignored.
682
+ yield* feed.append({
683
+ requestId: 'req-duplicate',
684
+ spaceId,
685
+ feedNamespace: WellKnownNamespaces.data,
686
+ blocks: [
687
+ Block.make({
688
+ feedId,
689
+ actorId,
690
+ sequence,
691
+ prevActorId: null,
692
+ prevSequence: null,
693
+ position: null,
694
+ timestamp: timestamp + 1,
695
+ data: new Uint8Array([2]),
696
+ }),
697
+ ],
698
+ });
699
+
700
+ const queryRes = yield* feed.query({
701
+ requestId: 'req-query',
702
+ spaceId,
703
+ feedNamespace: WellKnownNamespaces.data,
704
+ query: { feedIds: [feedId] },
705
+ position: -1,
706
+ });
707
+
708
+ expect(queryRes.blocks.length).toBe(1);
709
+ expect(queryRes.blocks[0].actorId).toBe(actorId);
710
+ expect(queryRes.blocks[0].sequence).toBe(sequence);
711
+ expect(queryRes.blocks[0].data).toEqual(new Uint8Array([1]));
712
+ }).pipe(Effect.provide(TestLayer)),
713
+ );
357
714
  });