import { describe, expectTypeOf, it } from 'vitest' import { KVStorage, type KVNode } from '../src/kv/node.js' import { InMemoryKVDriver } from '../src/kv/memory.js' import { KVEventStorage } from '../src/events/kv.js' import type { StoredEvent } from '../src/events/types.js' import { EventSourcedDocumentStorage } from '../src/documents/event-sourced.js' import type { FindResult } from '../src/documents/types.js' import type { AnySpec, Fields, Many } from '../src/types/schema.js' type User = { username: string; age: number } type Post = { title: string; createdAt: string } type Schema = { children: { users: Many }> profile: { children: { [id: string]: { children: Fields } } } // field-per-key style } } const driver = new InMemoryKVDriver() describe('KV types (section 7)', () => { const db = new KVStorage(driver) it('reads child values at dynamic levels', () => { expectTypeOf(db.child('users').get('123')).toEqualTypeOf>() expectTypeOf(db.child('users').child('123').get()).toEqualTypeOf>() expectTypeOf(db.child('users').getMany(['1', '2'])).toEqualTypeOf>() }) it('navigates named levels', () => { expectTypeOf(db.child('users').child('123').child('posts')).toEqualTypeOf>>() // @ts-expect-error unknown named child db.child('users').child('123').child('postz') // @ts-expect-error unknown top-level child db.child('userz') }) it('reads field-per-key values', () => { expectTypeOf(db.child('profile').child('9').get('age')).toEqualTypeOf>() expectTypeOf(db.child('profile').child('9').get('username')).toEqualTypeOf>() // @ts-expect-error unknown field db.child('profile').child('9').get('agee') }) it('checks written values', () => { db.child('users').set('1', { username: 'a', age: 1 }) db.child('users').child('1').set({ username: 'a', age: 1 }) // @ts-expect-error wrong value type db.child('users').set('1', { username: 'a' }) db.child('profile') .child('9') .setMany([ ['age', 3], ['username', 'x'], ]) db.child('profile') .child('9') .setMany([ // @ts-expect-error value doesn't match its key ['age', 'x'], ]) }) it('types listings', () => { expectTypeOf(db.child('users').keys()).toEqualTypeOf>() expectTypeOf(db.child('profile').child('9').keys()).toEqualTypeOf>() expectTypeOf(db.child('users').entries()).toEqualTypeOf>() }) it('defaults to untyped and supports as()', () => { const raw = new KVStorage(driver) expectTypeOf(raw.child('anything').child(5).get('x')).toEqualTypeOf>() const users = raw.child('users').as>() expectTypeOf(users.get('1')).toEqualTypeOf>() expectTypeOf(raw).toMatchTypeOf>() }) }) type OrderEvent = { type: 'created'; total: number } | { type: 'paid' } type EventSchema = { children: { orders: Many }> } } describe('event types', () => { const events = new KVEventStorage(new KVStorage(driver)) const orders = events.child('orders') it('checks appended events', () => { orders.append('o1', [{ type: 'created', total: 40 }]) orders.child('o1').append([{ type: 'paid' }]) // @ts-expect-error not an OrderEvent orders.append('o1', [{ type: 'refunded' }]) orders .child('o1') .child('shipments') .append('s1', [{ carrier: 'x' }]) }) it('types read results', () => { expectTypeOf(orders.read('o1')).toEqualTypeOf>>() expectTypeOf(orders.version('o1')).toEqualTypeOf>() }) }) type DocSchema = { children: { users: Many }> } } describe('document types', () => { const docs = new EventSourcedDocumentStorage(new KVEventStorage(new KVStorage(driver))) const users = docs.child('users') it('types reads and writes', () => { expectTypeOf(users.get('1')).toEqualTypeOf>() expectTypeOf(users.child('1').get()).toEqualTypeOf>() expectTypeOf(users.insert({ username: 'a', age: 1 })).toEqualTypeOf>() // @ts-expect-error missing field users.insert({ username: 'a' }) expectTypeOf(users.update('1', { $inc: { age: 1 } })).toEqualTypeOf>() expectTypeOf(users.child('1').child('posts').get('p')).toEqualTypeOf>() }) it('types queries', () => { expectTypeOf(users.find({ age: { $gte: 18 } })).toEqualTypeOf>>() expectTypeOf(users.findOne()).toEqualTypeOf | undefined>>() }) })