Files
PotentialStorage/test/utils/building-blocks.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

36 lines
1.5 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import fc from 'fast-check'
import { KeyRange } from '../../src/utils/key-range.js'
import { Keys } from '../../src/utils/keys.js'
describe('KeyRange', () => {
it('contains a key and its subtree, not lookalikes', () => {
const range = new KeyRange('ab')
expect(['ab', 'ab/c', 'ab/c/d'].every((k) => range.contains(k))).toBe(true)
expect(['a', 'abc', 'ab0', 'ab~2D1', 'b'].some((k) => range.contains(k))).toBe(false)
expect(new KeyRange('').contains('anything')).toBe(true)
})
it('agrees with its scan bounds for escaped keys (property)', () => {
const path = fc.array(fc.string({ unit: fc.constantFrom('a', 'b', '-', '/', '~', '0', '.'), minLength: 1, maxLength: 3 }), { minLength: 1, maxLength: 3 })
fc.assert(
fc.property(path, path, (p, q) => {
const range = new KeyRange(Keys.join(p))
const key = Keys.join(q)
expect(range.contains(key)).toBe(key >= range.gte! && key < range.lt!)
}),
)
})
it('clamps and checks scan ranges', () => {
const range = new KeyRange('ab')
expect(range.clamp({ limit: 5 })).toEqual({ limit: 5, gte: 'ab', lt: 'ab0' })
expect(range.clamp({ gte: 'ab/x', lt: 'zz' })).toEqual({ gte: 'ab/x', lt: 'ab0' })
expect(range.clamp({ gte: 'b' })).toBeUndefined()
expect(range.covers('ab/', 'ab0')).toBe(true)
expect(range.covers('a', 'ab0')).toBe(false)
expect(range.covers(undefined, 'ab0')).toBe(false)
expect(new KeyRange('').covers(undefined, undefined)).toBe(true)
})
})