40 lines
1.4 KiB
TypeScript
40 lines
1.4 KiB
TypeScript
|
|
import { mkdtempSync } from 'node:fs'
|
||
|
|
import { tmpdir } from 'node:os'
|
||
|
|
import { join } from 'node:path'
|
||
|
|
import { describe, expect, it } from 'vitest'
|
||
|
|
import { FsSnapshotStore, MemorySnapshotStore, canonicalJson, contentRef } from '../src/snapshot.js'
|
||
|
|
|
||
|
|
describe.each([
|
||
|
|
['memory', () => new MemorySnapshotStore()],
|
||
|
|
['fs', () => new FsSnapshotStore(mkdtempSync(join(tmpdir(), 'vpp-snap-')))],
|
||
|
|
])('snapshot store (%s)', (_label, make) => {
|
||
|
|
it('is content-addressed: same content → same ref, regardless of key order', () => {
|
||
|
|
const store = make()
|
||
|
|
const ref1 = store.put({ b: '2', a: '1' })
|
||
|
|
const ref2 = store.put({ a: '1', b: '2' })
|
||
|
|
expect(ref1).toBe(ref2)
|
||
|
|
expect(ref1).toMatch(/^[0-9a-f]{64}$/)
|
||
|
|
})
|
||
|
|
|
||
|
|
it('round-trips content and reports presence', () => {
|
||
|
|
const store = make()
|
||
|
|
const value = { curve: ['1.0', '2.5'], meta: { unit: 'mw' } }
|
||
|
|
const ref = store.put(value)
|
||
|
|
expect(store.has(ref)).toBe(true)
|
||
|
|
expect(store.get(ref)).toEqual(value)
|
||
|
|
expect(store.get('f'.repeat(64))).toBeUndefined()
|
||
|
|
})
|
||
|
|
|
||
|
|
it('ref verifies against canonical content (checksummed by construction)', () => {
|
||
|
|
const store = make()
|
||
|
|
const value = { x: '42.0' }
|
||
|
|
const ref = store.put(value)
|
||
|
|
expect(contentRef(canonicalJson(store.get(ref)))).toBe(ref)
|
||
|
|
})
|
||
|
|
|
||
|
|
it('rejects float content (decimal-string rule, docs/11 §3.3)', () => {
|
||
|
|
const store = make()
|
||
|
|
expect(() => store.put({ price: 425.5 })).toThrow(/decimal strings/)
|
||
|
|
})
|
||
|
|
})
|