1
0

schema.spec.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { describe, expect, it } from 'vitest'
  2. import {
  3. MACHINE_SCHEMA_VERSION,
  4. parseMachineJson,
  5. serializeMachineJson,
  6. } from '../src/repo/schema'
  7. describe('机器态 JSON(B8 schemaVersion)', () => {
  8. it('接受已知版本', () => {
  9. const r = parseMachineJson(JSON.stringify({ schemaVersion: 1, 状态: '可写' }), MACHINE_SCHEMA_VERSION)
  10. expect(r.ok).toBe(true)
  11. if (r.ok) {
  12. expect(r.data.schemaVersion).toBe(1)
  13. expect(r.data['状态']).toBe('可写')
  14. }
  15. })
  16. it('拒绝缺 schemaVersion', () => {
  17. const r = parseMachineJson(JSON.stringify({ 状态: '可写' }), MACHINE_SCHEMA_VERSION)
  18. expect(r.ok).toBe(false)
  19. if (!r.ok) {
  20. expect(r.reason).toBe('parse-error')
  21. expect(r.detail).toContain('schemaVersion')
  22. }
  23. })
  24. it('拒绝未知版本', () => {
  25. const r = parseMachineJson(JSON.stringify({ schemaVersion: 99 }), MACHINE_SCHEMA_VERSION)
  26. expect(r.ok).toBe(false)
  27. if (!r.ok) {
  28. expect(r.reason).toBe('parse-error')
  29. expect(r.detail).toMatch(/未知|不匹配/)
  30. }
  31. })
  32. it('拒绝非法 JSON', () => {
  33. const r = parseMachineJson('{坏的 json', MACHINE_SCHEMA_VERSION)
  34. expect(r.ok).toBe(false)
  35. if (!r.ok) expect(r.reason).toBe('parse-error')
  36. })
  37. it('往返:serialize 必写 schemaVersion,parse 还原字段', () => {
  38. const text = serializeMachineJson({ 完成: true, 问题: [] }, MACHINE_SCHEMA_VERSION)
  39. expect(JSON.parse(text).schemaVersion).toBe(1)
  40. const r = parseMachineJson(text, MACHINE_SCHEMA_VERSION)
  41. expect(r.ok).toBe(true)
  42. if (r.ok) {
  43. expect(r.data['完成']).toBe(true)
  44. expect(r.data.schemaVersion).toBe(1)
  45. }
  46. })
  47. })