- KV: one write primitive, write(changes) with { key, value? }; writeIfAbsent removed.
The server executes exactly what clients send and never reads values, so they can be
encrypted end to end. Adds setFields/getFields on the base class.
- HTTP: no server history or event ids; SSE relays every change (own writes included) and
the client emits resync when a dropped stream reconnects.
- Composite: writes apply locally, then go to the source (undone locally if refused);
pushed changes are applied and announced only where they differ from local values;
offline writes go to a queue store (any KV storage); sync() = replay, diff, replay.
Removes KVReplicaSet, KVOutbox, KeyCounter, SerialQueue and RetryTimer.
- Events: id = <time>_<sha256(time + canonical data)>, so concurrent appends never
collide and retries can't duplicate; no versions or ConcurrencyError.
- Event-sourced documents: read, resolve, append, apply; catchUp folds in foreign events;
getAt takes an event id.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017Qxbkp4tA7G5SCWjzWuxmE
105 lines
4.9 KiB
TypeScript
105 lines
4.9 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import fc from 'fast-check'
|
|
import { EventStorageKV } from '../../src/event-storage/event-storage-kv.js'
|
|
import type { BaseKVStorage } from '../../src/kv-storage/base-kv-storage.js'
|
|
import { KVStorageInMemory } from '../../src/kv-storage/kv-storage-in-memory.js'
|
|
import { collect, KVStorageCounting } from '../helpers.js'
|
|
|
|
function setup(kv: BaseKVStorage = new KVStorageCounting(), start = 1000) {
|
|
let t = start
|
|
return { kv, events: new EventStorageKV(kv, { now: () => t++ }) }
|
|
}
|
|
|
|
describe('EventStorageKV', () => {
|
|
it('appends and reads in self and child forms', async () => {
|
|
const orders = setup().events.child('orders')
|
|
const [created] = await orders.append('o1', [{ type: 'created' }])
|
|
const ids = await orders.child('o1').append([{ type: 'paid' }, { type: 'shipped' }])
|
|
expect(await collect(orders.read('o1'))).toEqual([
|
|
{ id: created, at: 1000, data: { type: 'created' } },
|
|
{ id: ids[0], at: 1001, data: { type: 'paid' } },
|
|
{ id: ids[1], at: 1002, data: { type: 'shipped' } },
|
|
])
|
|
expect((await collect(orders.child('o1').read({ after: ids[0] }))).map((e) => e.data)).toEqual([{ type: 'shipped' }])
|
|
expect((await collect(orders.read('o1', { reverse: true, limit: 2 }))).map((e) => e.id)).toEqual([ids[1], ids[0]])
|
|
expect((await collect(orders.read('o1', { reverse: true, after: ids[0] }))).map((e) => e.id)).toEqual([created])
|
|
})
|
|
|
|
it('ids are the time and a hash of the time and content', async () => {
|
|
const { kv, events } = setup()
|
|
const [id] = await events.child('s').append([{ b: 1, a: 2 }])
|
|
expect(id).toMatch(/^000000000001000_[0-9a-f]{64}$/)
|
|
expect(id).toBe(await EventStorageKV.id(1000, { a: 2, b: 1 }))
|
|
expect((await collect(kv.scan())).map(([k]) => k)).toEqual([`s/~e/${id}`])
|
|
})
|
|
|
|
it('one append is one write; one instance never reuses a time', async () => {
|
|
const kv = new KVStorageCounting()
|
|
const events = new EventStorageKV(kv, { now: () => 5 })
|
|
const ids = await events.child('s').append(['a', 'a'])
|
|
expect(kv.calls).toEqual({ writeMany: 1 })
|
|
expect(ids.map((id) => id.slice(0, 15))).toEqual(['000000000000005', '000000000000006'])
|
|
expect((await collect(events.child('s').read())).map((e) => e.data)).toEqual(['a', 'a'])
|
|
})
|
|
|
|
it('keeps concurrent appends from different writers, ordered by time', async () => {
|
|
const kv = new KVStorageInMemory()
|
|
const a = setup(kv, 1000).events.child('s')
|
|
const b = setup(kv, 1000).events.child('s')
|
|
await Promise.all([a.append(['a1', 'a2']), b.append(['b1', 'b2'])])
|
|
const read = await collect(a.read())
|
|
expect(read).toHaveLength(4)
|
|
expect(read.map((e) => e.at)).toEqual([1000, 1000, 1001, 1001])
|
|
expect(read.map((e) => e.id)).toEqual([...read.map((e) => e.id)].sort())
|
|
expect(await collect(b.read())).toEqual(read)
|
|
})
|
|
|
|
it('writing the same event again does not duplicate it', async () => {
|
|
const kv = new KVStorageInMemory()
|
|
await setup(kv).events.child('s').append(['x'])
|
|
await setup(kv).events.child('s').append(['x'])
|
|
expect(await collect(new EventStorageKV(kv).child('s').read())).toHaveLength(1)
|
|
})
|
|
|
|
it('checks the arity rules', async () => {
|
|
const { events } = setup()
|
|
await expect(events.append(['x'])).rejects.toThrow(/root/)
|
|
await expect(events.child('o').append(undefined as never, ['x'] as never)).rejects.toThrow(TypeError)
|
|
})
|
|
|
|
it('lists child streams', async () => {
|
|
const orders = setup().events.child('orders')
|
|
await orders.append('o1', ['a'])
|
|
await orders.append('o2', ['b'])
|
|
await orders.child('o1').append('notes', ['n'])
|
|
expect(await collect(orders.keys())).toEqual(['o1', 'o2'])
|
|
expect(await collect(orders.child('o1').keys())).toEqual(['notes'])
|
|
})
|
|
|
|
it('reads any window correctly (property)', async () => {
|
|
await fc.assert(
|
|
fc.asyncProperty(
|
|
fc.array(fc.integer({ min: 1, max: 4 }), { maxLength: 20 }),
|
|
fc.record({
|
|
after: fc.option(fc.nat(60), { nil: undefined }),
|
|
limit: fc.option(fc.nat(10), { nil: undefined }),
|
|
reverse: fc.boolean(),
|
|
}),
|
|
async (sizes, { after, limit, reverse }) => {
|
|
const s = setup().events.child('s')
|
|
const ids: string[] = []
|
|
for (const size of sizes) ids.push(...(await s.append(Array.from({ length: size }, () => ids.length))))
|
|
const all = await collect(s.read())
|
|
expect(all.map((e) => e.id)).toEqual(ids)
|
|
const ordered = reverse ? [...ids].reverse() : ids
|
|
const afterId = after === undefined ? undefined : ids[after]
|
|
let expected = afterId === undefined ? ordered : ordered.slice(ordered.indexOf(afterId) + 1)
|
|
if (limit !== undefined) expected = expected.slice(0, limit)
|
|
expect((await collect(s.read({ after: afterId, limit, reverse }))).map((e) => e.id)).toEqual(expected)
|
|
},
|
|
),
|
|
{ numRuns: 200 },
|
|
)
|
|
})
|
|
})
|