import { describe, expect, it } from 'vitest' import { DocumentStorageInMemory } from '../../src/document-storage/document-storage-in-memory.js' import { DocumentStorageKV } from '../../src/document-storage/document-storage-kv.js' import { EventSourcedDocumentStorage } from '../../src/document-storage/event-sourced-document-storage.js' import { EventStorageKV } from '../../src/event-storage/event-storage-kv.js' import { KVStorageInMemory } from '../../src/kv-storage/kv-storage-in-memory.js' import { collect, KVStorageCounting } from '../helpers.js' import { runDocumentStorageSuite } from './document-storage.suite.js' const indexes = { users: ['email', 'age'] } runDocumentStorageSuite('DocumentStorageInMemory', () => new DocumentStorageInMemory()) runDocumentStorageSuite('DocumentStorageInMemory (indexed)', () => new DocumentStorageInMemory({ indexes })) runDocumentStorageSuite('DocumentStorageKV', () => new DocumentStorageKV(new KVStorageInMemory())) runDocumentStorageSuite('EventSourced (in-memory projections)', () => { const kv = new KVStorageInMemory() return new EventSourcedDocumentStorage(new EventStorageKV(kv), new DocumentStorageInMemory({ indexes })) }) runDocumentStorageSuite('EventSourced (KV projections)', () => { const kv = new KVStorageInMemory() return new EventSourcedDocumentStorage(new EventStorageKV(kv), new DocumentStorageKV(kv.child('projections'))) }) describe('DocumentStorageInMemory indexes', () => { it('keeps the equality index in sync with updates and deletes', async () => { const users = new DocumentStorageInMemory({ indexes }).child('users') await users.set('1', { email: 'a', age: 1 }) await users.set('2', { email: 'b', age: 1 }) await users.update('1', { $set: { email: 'c' } }) await users.delete('2') expect(await collect(users.find({ email: 'a' }))).toEqual([]) expect(await collect(users.find({ email: 'c' }))).toEqual([{ key: '1', data: { email: 'c', age: 1 } }]) expect(await collect(users.find({ age: { $eq: 1 } }))).toHaveLength(1) }) }) describe('EventSourcedDocumentStorage', () => { function setup() { const kv = new KVStorageCounting() const events = new EventStorageKV(kv) const projections = new DocumentStorageKV(kv.child('projections')) return { kv, events, projections, docs: new EventSourcedDocumentStorage(events, projections) } } it('records resolved changes as history and replays any event', async () => { const users = setup().docs.child('users') await users.set('1', { n: 5 }) await users.update('1', { $inc: { n: 1 } }) await users.delete('1') await users.set('1', { n: 10 }) const history = await collect(users.history('1')) expect(history.map((e) => e.data)).toEqual([ { op: 'set', doc: { n: 5 } }, { op: 'patch', set: { n: 6 }, unset: [] }, { op: 'delete' }, { op: 'set', doc: { n: 10 } }, ]) expect(await users.getAt('1', history[1]!.id)).toEqual({ n: 6 }) expect(await users.child('1').getAt(history[2]!.id)).toBeUndefined() expect(await users.get('1')).toEqual({ n: 10 }) }) it('catchUp folds in events written elsewhere', async () => { const { docs, events } = setup() const users = docs.child('users') await users.set('1', { n: 1 }) await events.child('users').append('1', [{ op: 'patch', set: { m: 2 }, unset: [] }]) // e.g. synced from another device expect(await users.get('1')).toEqual({ n: 1 }) expect(await users.catchUp('1')).toEqual({ n: 1, m: 2 }) expect(await users.get('1')).toEqual({ n: 1, m: 2 }) await events.child('users').append('2', [{ op: 'set', doc: { n: 7 } }, { op: 'delete' }]) expect(await users.catchUp('2')).toBeUndefined() expect(await users.get('2')).toBeUndefined() }) it('two instances editing different fields both keep their edits; the same field ends with the later value', async () => { const kv = new KVStorageInMemory() let t = 1000 const make = () => new EventSourcedDocumentStorage(new EventStorageKV(kv, { now: () => t++ }), new DocumentStorageKV(kv.child('projections'))).child('users') const a = make() const b = make() await a.set('1', { name: 'x', age: 1 }) await Promise.all([a.update('1', { $set: { name: 'Ada' } }), b.update('1', { $set: { age: 2 } })]) await b.update('1', { $set: { name: 'Bob' } }) expect(await a.catchUp('1')).toEqual({ name: 'Bob', age: 2 }) }) it('a write is a projection read, an append and a projection write', async () => { const { docs, kv } = setup() await docs.child('users').set('1', { n: 1 }) kv.reset() await docs.child('users').update('1', { $inc: { n: 1 } }) expect(kv.calls).toEqual({ readMany: 2, writeMany: 2 }) }) })