persistence-schema-model.spec.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { describe, expect, it } from 'vitest'
  2. import { canonicalizeSchema, isArbitraryJsonSchema, schemaDigest, type SchemaNode } from './persistence-schema-model.ts'
  3. const string: SchemaNode = { kind: 'primitive', type: 'string' }
  4. const number: SchemaNode = { kind: 'primitive', type: 'number' }
  5. describe('canonical persisted type graphs', () => {
  6. it('ignores source ordering and shared versus duplicated subtypes', () => {
  7. const left: SchemaNode[] = [
  8. { kind: 'object', properties: [{ name: 'b', type: 1, optional: false }, { name: 'a', type: 1, optional: true }], indices: [] }, string,
  9. ]
  10. const right: SchemaNode[] = [
  11. string, string,
  12. { kind: 'object', properties: [{ name: 'a', type: 0, optional: true }, { name: 'b', type: 1, optional: false }], indices: [] },
  13. ]
  14. expect(canonicalizeSchema(left, 0)).toEqual(canonicalizeSchema(right, 2))
  15. })
  16. it('minimizes differently factored recursive aliases to the same graph', () => {
  17. const self: SchemaNode[] = [
  18. { kind: 'object', properties: [{ name: 'next', type: 0, optional: true }, { name: 'value', type: 1, optional: false }], indices: [] }, string,
  19. ]
  20. const mutual: SchemaNode[] = [
  21. { kind: 'object', properties: [{ name: 'value', type: 2, optional: false }, { name: 'next', type: 1, optional: true }], indices: [] },
  22. { kind: 'object', properties: [{ name: 'next', type: 0, optional: true }, { name: 'value', type: 3, optional: false }], indices: [] }, string, string,
  23. ]
  24. const normalized = canonicalizeSchema(self, 0)
  25. expect(canonicalizeSchema(mutual, 0)).toEqual(normalized)
  26. expect(canonicalizeSchema(normalized.nodes, 0)).toEqual(normalized)
  27. mutual[3] = number
  28. expect(schemaDigest(canonicalizeSchema(mutual, 0))).not.toBe(schemaDigest(normalized))
  29. })
  30. it('flattens reordered unions and removes structurally equal alternatives', () => {
  31. const flat: SchemaNode[] = [{ kind: 'union', types: [1, 2] }, string, number]
  32. const factored: SchemaNode[] = [string, number, { kind: 'union', types: [0, 3] }, { kind: 'union', types: [1, 0] }]
  33. expect(canonicalizeSchema(flat, 0)).toEqual(canonicalizeSchema(factored, 2))
  34. expect(canonicalizeSchema([{ kind: 'union', types: [1, 2] }, string, string], 0)).toEqual(canonicalizeSchema([string], 0))
  35. expect(canonicalizeSchema([{ kind: 'union', types: [1, 2] }, { kind: 'literal', value: true }, { kind: 'literal', value: false }], 0))
  36. .toEqual(canonicalizeSchema([{ kind: 'primitive', type: 'boolean' }], 0))
  37. })
  38. it('preserves tuple positions, element absence, and rest elements', () => {
  39. const baseline: SchemaNode[] = [{ kind: 'tuple', elements: [{ type: 1, optional: false, rest: false }, { type: 2, optional: true, rest: false }] }, string, number]
  40. const reversed: SchemaNode[] = [{ kind: 'tuple', elements: [{ type: 2, optional: false, rest: false }, { type: 1, optional: true, rest: false }] }, string, number]
  41. const rest: SchemaNode[] = [{ kind: 'tuple', elements: [{ type: 1, optional: false, rest: false }, { type: 2, optional: false, rest: true }] }, string, number]
  42. const digest = schemaDigest(canonicalizeSchema(baseline, 0))
  43. expect(schemaDigest(canonicalizeSchema(reversed, 0))).not.toBe(digest)
  44. expect(schemaDigest(canonicalizeSchema(rest, 0))).not.toBe(digest)
  45. })
  46. it('recognizes arbitrary recursive JSON structurally and retains its definition', () => {
  47. const json: SchemaNode[] = [
  48. { kind: 'union', types: [1, 2, 3, 4, 5, 6] },
  49. { kind: 'primitive', type: 'null' }, { kind: 'primitive', type: 'boolean' }, number, string,
  50. { kind: 'array', element: 0 }, { kind: 'object', properties: [], indices: [{ key: 4, value: 0 }] },
  51. ]
  52. const schema = canonicalizeSchema(json, 0)
  53. expect(isArbitraryJsonSchema(schema)).toBe(true)
  54. expect(schema.nodes.some(node => node.kind === 'array' && node.element === 0)).toBe(true)
  55. json[0] = { kind: 'union', types: [1, 2, 3, 4, 5] }
  56. expect(isArbitraryJsonSchema(canonicalizeSchema(json, 0))).toBe(false)
  57. })
  58. it('rejects missing references and unproductive union cycles', () => {
  59. expect(() => canonicalizeSchema([{ kind: 'array', element: 5 }], 0)).toThrow('missing node 5')
  60. expect(() => canonicalizeSchema([{ kind: 'union', types: [0] }], 0)).toThrow('union cycle')
  61. })
  62. })