model.spec.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { describe, expect, it } from 'vitest'
  2. import Schema from 'schemastery'
  3. import {
  4. deletePath, getPath, hasPath, nodeAtPath, rehydrateSchema, setPath, validateDraft,
  5. } from '../src/model.ts'
  6. const Wire = (schema: Schema): unknown => JSON.parse(JSON.stringify(schema.toJSON()))
  7. describe('rehydration and validation', () => {
  8. it('rehydrates a serialized envelope into a working validator', () => {
  9. const root = rehydrateSchema(Wire(Schema.object({ name: Schema.string().required() })))
  10. expect(validateDraft(root, { name: 'ok' })).toBeUndefined()
  11. expect(validateDraft(root, { name: 42 })).toContain('name')
  12. })
  13. it('stringifies non-Error validation throws', () => {
  14. const hostile = (() => {
  15. throw 'plain-string failure'
  16. }) as unknown as Parameters<typeof validateDraft>[0]
  17. expect(validateDraft(hostile, {})).toBe('plain-string failure')
  18. })
  19. })
  20. describe('path helpers', () => {
  21. const root = { providers: { openai: { baseURL: 'https://x' } }, models: [{ id: 'a' }] }
  22. it('reads nested object and array paths', () => {
  23. expect(getPath(root, [])).toBe(root)
  24. expect(getPath(root, ['providers', 'openai', 'baseURL'])).toBe('https://x')
  25. expect(getPath(root, ['models', '0', 'id'])).toBe('a')
  26. expect(getPath(root, ['providers', 'missing', 'x'])).toBeUndefined()
  27. expect(getPath(root, ['providers', 'openai', 'baseURL', 'deep'])).toBeUndefined()
  28. })
  29. it('reports draft presence by key existence, not value truthiness', () => {
  30. expect(hasPath({ flag: false }, ['flag'])).toBe(true)
  31. expect(hasPath({ nested: { key: undefined } }, ['nested', 'key'])).toBe(true)
  32. expect(hasPath({}, ['missing'])).toBe(false)
  33. expect(hasPath({ leaf: 'x' }, ['leaf', 'deeper'])).toBe(false)
  34. expect(hasPath({ models: ['a'] }, ['models', '0'])).toBe(true)
  35. expect(hasPath({ models: ['a'] }, ['models', '1'])).toBe(false)
  36. expect(hasPath({ root: true }, [])).toBe(true)
  37. expect(hasPath(undefined, [])).toBe(false)
  38. })
  39. it('sets nested paths immutably, materializing containers by key shape', () => {
  40. const draft = {}
  41. const next = setPath(draft, ['providers', 'openai', 'baseURL'], 'https://y')
  42. expect(draft).toEqual({})
  43. expect(next).toEqual({ providers: { openai: { baseURL: 'https://y' } } })
  44. const withArray = setPath(next, ['models', '0'], { id: 'a' })
  45. expect(withArray).toEqual({ providers: { openai: { baseURL: 'https://y' } }, models: [{ id: 'a' }] })
  46. const replaced = setPath(withArray, ['models', '0', 'id'], 'b')
  47. expect(replaced.models).toEqual([{ id: 'b' }])
  48. expect((withArray as { models: unknown[] }).models).toEqual([{ id: 'a' }])
  49. expect(() => setPath({}, [], 'x')).toThrow(/non-empty path/)
  50. })
  51. it('deletes nested paths immutably and splices array indexes', () => {
  52. const draft = { providers: { openai: { baseURL: 'https://x', apiKey: 'k' } }, models: ['a', 'b'] }
  53. const withoutKey = deletePath(draft, ['providers', 'openai', 'apiKey'])
  54. expect(withoutKey).toEqual({ providers: { openai: { baseURL: 'https://x' } }, models: ['a', 'b'] })
  55. expect(draft.providers.openai.apiKey).toBe('k')
  56. const withoutModel = deletePath(withoutKey, ['models', '0'])
  57. expect(withoutModel.models).toEqual(['b'])
  58. expect(deletePath(draft, ['providers', 'missing', 'x'])).toBe(draft)
  59. expect(() => deletePath({}, [])).toThrow(/non-empty path/)
  60. })
  61. it('deletes keys through array intermediates immutably', () => {
  62. const draft = { models: [{ id: 'a', contextWindow: 1 }] }
  63. const next = deletePath(draft, ['models', '0', 'contextWindow'])
  64. expect(next).toEqual({ models: [{ id: 'a' }] })
  65. expect(draft.models[0]).toEqual({ id: 'a', contextWindow: 1 })
  66. })
  67. })
  68. describe('nodeAtPath', () => {
  69. const Root = Schema.object({
  70. providers: Schema.dict(Schema.object({ baseURL: Schema.string() })),
  71. models: Schema.array(Schema.object({ id: Schema.string() })),
  72. leaf: Schema.string(),
  73. })
  74. it('resolves object, dict, and array positions', () => {
  75. const root = rehydrateSchema(Wire(Root))
  76. expect(nodeAtPath(root, [])).toBe(root)
  77. expect(nodeAtPath(root, ['providers', 'openai'])?.type).toBe('object')
  78. expect(nodeAtPath(root, ['providers', 'openai', 'baseURL'])?.type).toBe('string')
  79. expect(nodeAtPath(root, ['models', '0', 'id'])?.type).toBe('string')
  80. expect(nodeAtPath(root, ['missing'])).toBeUndefined()
  81. expect(nodeAtPath(root, ['missing', 'deeper'])).toBeUndefined()
  82. expect(nodeAtPath(root, ['leaf', 'below'])).toBeUndefined()
  83. })
  84. it('tolerates structural nodes missing their relation maps', () => {
  85. expect(nodeAtPath({ type: 'object' } as never, ['x'])).toBeUndefined()
  86. expect(nodeAtPath({ type: 'dict' } as never, ['x'])).toBeUndefined()
  87. })
  88. })