Files
PotentialStorage/test/kv-storage/kv-storage-http-watch.test.ts
T
Claude 631c75428b
CI / Node 22 (pull_request) Successful in 12s
CI / Node 26 (pull_request) Successful in 13s
CI / Node 24 (pull_request) Successful in 14s
Plain operations: last write wins, hash-keyed events, simpler composite
- 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
2026-10-23 13:33:07 +00:00

60 lines
2.4 KiB
TypeScript

import { afterEach, describe, expect, it } from 'vitest'
import type { KVChangeEvent } from '../../src/kv-storage/base-kv-storage.js'
import { StorageUnavailableError } from '../../src/utils/errors.js'
import { TestNetwork } from './sync-helpers.js'
let network: TestNetwork
afterEach(() => network?.close())
describe('KVStorageHttp.subscribe (SSE)', () => {
it('relays every change under the path as remote change events, its own writes included', async () => {
network = new TestNetwork()
const watcher = network.client()
await watcher.child('users').subscribe()
const received: KVChangeEvent[] = []
watcher.on('change', (e) => e.origin === 'remote' && received.push(e))
const other = watcher.waitFor('change', (e) => e.origin === 'remote')
await network.client().child('users').set('1', { name: 'Ada', n: 1n })
await other
const own = watcher.waitFor('change', (e) => e.origin === 'remote')
await network.client().set('elsewhere', 1) // not subscribed
await watcher.child('users').delete('1')
await own
expect(received.map((e) => e.changes)).toEqual([[{ key: 'users/1', value: { name: 'Ada', n: 1n } }], [{ key: 'users/1' }]])
})
it('emits resync when a dropped stream reconnects', async () => {
network = new TestNetwork()
const watcher = network.client()
await watcher.subscribe('')
const resync = watcher.waitFor('resync', () => true)
network.dropStreams()
await resync
const after = watcher.waitFor('change', () => true)
await network.client().set('a', 1)
expect((await after).changes).toEqual([{ key: 'a', value: 1 }])
})
it('stops relaying after unsubscribe', async () => {
network = new TestNetwork()
const watcher = network.client()
await watcher.subscribe('a')
await watcher.subscribe('b')
await watcher.unsubscribe('a')
const received: KVChangeEvent[] = []
watcher.on('change', (e) => received.push(e))
const arrived = watcher.waitFor('change', () => true)
await network.client().set('a', 1)
await network.client().set('b', 2)
await arrived
expect(received.flatMap((e) => e.changes)).toEqual([{ key: 'b', value: 2 }])
})
it('reports an unreachable server as StorageUnavailableError', async () => {
network = new TestNetwork()
const client = network.client()
network.online = false
await expect(client.readMany(['x'])).rejects.toBeInstanceOf(StorageUnavailableError)
})
})