|
| 1 | +import { describe, expect, it, vi, type MockedFunction } from 'vitest' |
| 2 | +import { initialize } from './context' |
| 3 | +import { writable, type Writable } from 'svelte/store' |
| 4 | +import * as Errors from './errors' |
| 5 | +import type * as Gateway from './gateway' |
| 6 | +import type { Local } from './repositories' |
| 7 | +import { renderer } from './parser' |
| 8 | +import { type Prestore } from './stores' |
| 9 | +import { property, space } from './__test__/factories' |
| 10 | +import { get } from 'svelte/store' |
| 11 | +import type { Result } from './gateway' |
| 12 | + |
| 13 | +type InitializeParams = Parameters<typeof initialize>[0] |
| 14 | + |
| 15 | +type SetupParams = { |
| 16 | + local: Partial<Prestore> |
| 17 | + remote: Result |
| 18 | +} & Pick<InitializeParams, 'showDev' | 'source'> |
| 19 | + |
| 20 | +describe(initialize, () => { |
| 21 | + let db: Local<Prestore> & { |
| 22 | + subscribe: MockedFunction<Local<Prestore>['subscribe']> |
| 23 | + } |
| 24 | + let errorHandler: MockedFunction<Errors.Handler> |
| 25 | + let gateway: MockedFunction<Gateway.Sync> |
| 26 | + let mockRenderer: typeof renderer |
| 27 | + |
| 28 | + function setup(args: Partial<SetupParams> = {}) { |
| 29 | + vi.clearAllMocks() |
| 30 | + |
| 31 | + errorHandler = vi.fn() |
| 32 | + mockRenderer = vi.fn() |
| 33 | + |
| 34 | + const prestore: Prestore = { |
| 35 | + spaces: [], |
| 36 | + properties: [], |
| 37 | + theorems: [], |
| 38 | + traits: [], |
| 39 | + deduction: { checked: new Set(), all: new Set() }, |
| 40 | + source: { host: 'example.com', branch: 'main' }, |
| 41 | + sync: undefined, |
| 42 | + ...args.local, |
| 43 | + } |
| 44 | + |
| 45 | + db = { |
| 46 | + load: () => prestore, |
| 47 | + subscribe: vi.fn(), |
| 48 | + } |
| 49 | + |
| 50 | + gateway = vi.fn(async (_host: string, _branch: string) => args.remote) |
| 51 | + |
| 52 | + return initialize({ |
| 53 | + db, |
| 54 | + errorHandler, |
| 55 | + gateway, |
| 56 | + typesetter: mockRenderer, |
| 57 | + showDev: args.showDev || false, |
| 58 | + source: args.source || {}, |
| 59 | + }) |
| 60 | + } |
| 61 | + |
| 62 | + it('should create a context with default dependencies', () => { |
| 63 | + const { showDev, errorHandler } = setup() |
| 64 | + |
| 65 | + expect(showDev).toBe(false) |
| 66 | + expect(errorHandler).toEqual(errorHandler) |
| 67 | + }) |
| 68 | + |
| 69 | + it('use the provided source', () => { |
| 70 | + const { source } = setup({ |
| 71 | + source: { host: 'default.com', branch: 'master' }, |
| 72 | + }) |
| 73 | + |
| 74 | + expect(get(source)).toEqual({ host: 'default.com', branch: 'master' }) |
| 75 | + }) |
| 76 | + |
| 77 | + it('should trigger sync when local sync state is undefined', () => { |
| 78 | + setup({ |
| 79 | + local: { |
| 80 | + source: { host: 'example.com', branch: 'main' }, |
| 81 | + sync: undefined, |
| 82 | + }, |
| 83 | + }) |
| 84 | + |
| 85 | + expect(gateway).toHaveBeenCalledWith('example.com', 'main', undefined) |
| 86 | + }) |
| 87 | + |
| 88 | + it('persists remote sync state to the local db', async () => { |
| 89 | + const context = setup({ |
| 90 | + local: { |
| 91 | + source: { host: 'example.com', branch: 'main' }, |
| 92 | + sync: undefined, |
| 93 | + }, |
| 94 | + remote: { |
| 95 | + spaces: [space({ id: 123 })], |
| 96 | + properties: [property({ id: 456 })], |
| 97 | + theorems: [], |
| 98 | + traits: [], |
| 99 | + etag: 'etag', |
| 100 | + sha: 'sha', |
| 101 | + }, |
| 102 | + }) |
| 103 | + |
| 104 | + await context.loaded() |
| 105 | + |
| 106 | + const { spaces, properties, sync } = db.subscribe.mock.calls[0][0] |
| 107 | + |
| 108 | + expect(get(spaces).map(s => s.id)).toEqual([123]) |
| 109 | + expect(get(properties).map(p => p.id)).toEqual([456]) |
| 110 | + expect(get(sync)).toEqual( |
| 111 | + expect.objectContaining({ |
| 112 | + kind: 'fetched', |
| 113 | + value: { etag: 'etag', sha: 'sha' }, |
| 114 | + }), |
| 115 | + ) |
| 116 | + }) |
| 117 | + |
| 118 | + it('runs deductions', async () => { |
| 119 | + const context = setup({ |
| 120 | + local: { |
| 121 | + spaces: [space({ id: 1 })], |
| 122 | + properties: [property({ id: 1 }), property({ id: 2 })], |
| 123 | + traits: [ |
| 124 | + { |
| 125 | + asserted: true, |
| 126 | + space: 1, |
| 127 | + property: 1, |
| 128 | + value: true, |
| 129 | + description: '', |
| 130 | + refs: [], |
| 131 | + }, |
| 132 | + ], |
| 133 | + theorems: [ |
| 134 | + { |
| 135 | + id: 1, |
| 136 | + when: { kind: 'atom', property: 1, value: true }, |
| 137 | + then: { kind: 'atom', property: 2, value: true }, |
| 138 | + description: '', |
| 139 | + refs: [], |
| 140 | + }, |
| 141 | + ], |
| 142 | + }, |
| 143 | + }) |
| 144 | + |
| 145 | + await context.checked('S1') |
| 146 | + |
| 147 | + const { trait, proof } = get(context.traits).lookup({ |
| 148 | + spaceId: 'S1', |
| 149 | + propertyId: 'P2', |
| 150 | + theorems: get(context.theorems), |
| 151 | + })! |
| 152 | + |
| 153 | + expect(trait?.value).toEqual(true) |
| 154 | + expect(proof?.theorems.map(t => t.id)).toEqual([1]) |
| 155 | + }) |
| 156 | + |
| 157 | + it('can wait for a space to be checked', async () => { |
| 158 | + const { checked } = setup({ |
| 159 | + local: { |
| 160 | + spaces: [space({ id: 123 })], |
| 161 | + }, |
| 162 | + }) |
| 163 | + |
| 164 | + expect(checked('S123')).resolves.toBeUndefined() |
| 165 | + }) |
| 166 | + |
| 167 | + describe('load', () => { |
| 168 | + let store: Writable<number> |
| 169 | + |
| 170 | + it('resolves when lookup finds value', async () => { |
| 171 | + store = writable(0) |
| 172 | + const context = setup() |
| 173 | + |
| 174 | + const loadPromise = context.load(store, n => (n > 10 ? n : false)) |
| 175 | + |
| 176 | + store.set(5) |
| 177 | + store.set(13) |
| 178 | + |
| 179 | + await expect(loadPromise).resolves.toBe(13) |
| 180 | + }) |
| 181 | + |
| 182 | + it('rejects when until promise resolves first', async () => { |
| 183 | + store = writable(0) |
| 184 | + const context = setup() |
| 185 | + |
| 186 | + const loadPromise = context.load(store, n => n > 0, Promise.resolve()) |
| 187 | + |
| 188 | + await expect(loadPromise).rejects.toBeUndefined() |
| 189 | + }) |
| 190 | + }) |
| 191 | +}) |
0 commit comments