render-persistence-schema.spec.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /** Linked catalog regressions for transitive changes and shared definitions. */
  2. import { describe, expect, it } from 'vitest'
  3. import { canonicalizeSchema, schemaDigest, type PersistenceSchemaInventory, type PersistenceType, type SchemaNode } from './persistence-schema-model.ts'
  4. import { renderPersistenceSchemaDefinitions, renderPersistenceSchemaIndex } from './render-persistence-schema.ts'
  5. function fixture(optional = true): PersistenceSchemaInventory {
  6. const types = new Map<string, PersistenceType>()
  7. const roots = ['first/event', 'second/event'].map((event) => {
  8. const nodes: SchemaNode[] = [
  9. { kind: 'object', properties: [{ name: 'type', type: 1, optional: false }, { name: 'data', type: 2, optional: false }], indices: [] },
  10. { kind: 'literal', value: event },
  11. { kind: 'object', properties: [{ name: 'error', type: 3, optional: true }], indices: [] },
  12. { kind: 'object', properties: [{ name: 'reason', type: 4, optional }], indices: [] },
  13. { kind: 'primitive', type: 'string' },
  14. ]
  15. nodes.forEach((_, index) => {
  16. const schema = canonicalizeSchema(nodes, index)
  17. const digest = schemaDigest(schema)
  18. if (!types.has(digest)) types.set(digest, { digest, schema, names: index === 3 ? ['ErrorInfo'] : [], sources: index === 3 ? ['packages/core/example/src/types.ts:8'] : [] })
  19. })
  20. const schema = canonicalizeSchema(nodes, 0)
  21. return { key: `event:${event}`, kind: 'event' as const, event, surface: false, digest: schemaDigest(schema), schema }
  22. })
  23. return { formatVersion: 1, roots, types: [...types.values()] }
  24. }
  25. describe('persistence schema catalog', () => {
  26. it('shows a shared nested definition once and updates both affected root fingerprints', () => {
  27. const before = fixture()
  28. const after = fixture(false)
  29. const definitions = renderPersistenceSchemaDefinitions(after)
  30. expect(definitions.match(/### `ErrorInfo`/gu)).toHaveLength(1)
  31. expect(definitions).toContain('| `reason` | required | `string` |')
  32. expect(definitions).toContain('[`ErrorInfo`](#persistence-type-errorinfo)')
  33. const index = renderPersistenceSchemaIndex(after)
  34. for (const [position, root] of after.roots.entries()) {
  35. expect(root.digest).not.toBe(before.roots[position]?.digest)
  36. expect(index).toContain(root.digest)
  37. }
  38. })
  39. it('keeps named definition anchors when their digest changes', () => {
  40. const before = renderPersistenceSchemaDefinitions(fixture())
  41. const after = renderPersistenceSchemaDefinitions(fixture(false))
  42. expect(before).toContain('<a id="persistence-type-errorinfo"></a>')
  43. expect(after).toContain('<a id="persistence-type-errorinfo"></a>')
  44. expect(before).not.toBe(after)
  45. })
  46. it('renders historical source paths and nested headings within their reference section', () => {
  47. const current = fixture()
  48. const inventory = { ...current, types: current.types.map(type => ({ ...type, sources: type.sources.map(source => source.replace(/:\d+$/u, '')) })) }
  49. const definitions = renderPersistenceSchemaDefinitions(inventory, 'en', () => undefined, 3)
  50. expect(definitions).toContain('Sources: `packages/core/example/src/types.ts`')
  51. expect(definitions).not.toContain('types.ts:8')
  52. expect(definitions).not.toContain('(../packages/')
  53. expect(definitions).toContain('### Resolved persistence types')
  54. expect(definitions).toContain('#### `ErrorInfo`')
  55. const index = renderPersistenceSchemaIndex(inventory, 'en', ['[Historical schema](v0.schema.json)'], 3)
  56. expect(index).toContain('[Historical schema](v0.schema.json)')
  57. expect(index).toContain('### Persistence type fingerprints')
  58. expect(index).not.toContain('(persistence-schema.json)')
  59. expect(renderPersistenceSchemaDefinitions(current)).toContain('[`packages/core/example/src/types.ts:8`](../packages/core/example/src/types.ts)')
  60. })
  61. it('refuses a current inventory that omits a referenced definition', () => {
  62. const complete = fixture()
  63. const missing: PersistenceSchemaInventory = { ...complete, types: [] }
  64. expect(() => renderPersistenceSchemaIndex(missing)).toThrow(/absent from the inventory/u)
  65. })
  66. it('renders a recursive definition through a finite self-reference', () => {
  67. const schema = canonicalizeSchema([{ kind: 'object', properties: [{ name: 'next', type: 0, optional: true }], indices: [] }], 0)
  68. const digest = schemaDigest(schema)
  69. const inventory: PersistenceSchemaInventory = {
  70. formatVersion: 1,
  71. roots: [{ key: 'SessionHeader', kind: 'header', digest, schema }],
  72. types: [{ digest, schema, names: ['Recursive'], sources: [] }],
  73. }
  74. expect(renderPersistenceSchemaDefinitions(inventory)).toContain('| `next` | optional | [`Recursive`](#persistence-type-recursive) |')
  75. })
  76. it('uses intrinsic scalar labels even when an alias or reference supplied a name', () => {
  77. const schema = canonicalizeSchema([{ kind: 'primitive', type: 'string' }], 0)
  78. const digest = schemaDigest(schema)
  79. const inventory: PersistenceSchemaInventory = {
  80. formatVersion: 1,
  81. roots: [],
  82. types: [{ digest, schema, names: ['SessionHeader.agentPreset'], sources: [] }],
  83. }
  84. const rendered = renderPersistenceSchemaDefinitions(inventory)
  85. expect(rendered).toContain('### `string`')
  86. expect(rendered).not.toContain('### `SessionHeader.agentPreset`')
  87. })
  88. })