json.spec.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { describe, expect, it } from 'vitest'
  2. import {
  3. inspectSessionFormatVersion,
  4. sessionFormatCount,
  5. sessionFormatSafeInteger,
  6. snapshotSessionFormatArtifact,
  7. snapshotSessionFormatHeader,
  8. snapshotSessionFormatJson,
  9. } from '../src/index.ts'
  10. describe('lossless Session format JSON snapshots', () => {
  11. it.each([
  12. ['negative zero', -0],
  13. ['non-finite number', Number.POSITIVE_INFINITY],
  14. ['undefined member', { value: undefined }],
  15. ['sparse array', Array(1)],
  16. ['symbol member', { [Symbol('hidden')]: true }],
  17. ['non-enumerable member', Object.defineProperty({}, 'hidden', { value: true })],
  18. ['array property', Object.assign([], { extra: true })],
  19. ])('refuses %s that JSON cannot preserve', (_name, value) => {
  20. expect(() => snapshotSessionFormatJson(value, 'payload')).toThrow('payload is not lossless JSON')
  21. })
  22. it('detaches, freezes, and retains repeated non-cyclic values and __proto__ keys', () => {
  23. const shared = { value: 1 }
  24. const source = JSON.parse('{"__proto__":{"safe":true}}') as Record<string, unknown>
  25. source['values'] = [shared, shared]
  26. const snapshot = snapshotSessionFormatJson(source) as Record<string, unknown>
  27. expect(snapshot).toEqual(source)
  28. expect(snapshot).not.toBe(source)
  29. expect(Object.isFrozen(snapshot)).toBe(true)
  30. expect(Object.isFrozen(snapshot['values'])).toBe(true)
  31. expect(Object.getPrototypeOf(snapshot)).toBe(Object.prototype)
  32. })
  33. it('refuses invalid scalar coordinates, cycles, and custom prototypes', () => {
  34. const cyclic: { self?: unknown } = {}
  35. cyclic.self = cyclic
  36. class RecordValue { value = 1 }
  37. class ArrayValue extends Array<number> {}
  38. expect(() => sessionFormatCount(-1, 'count')).toThrow(/non-negative/)
  39. expect(() => sessionFormatSafeInteger(1.5, 'integer')).toThrow(/safe integer/)
  40. expect(() => inspectSessionFormatVersion([])).toThrow(/header/)
  41. expect(() => snapshotSessionFormatJson(cyclic)).toThrow(/not lossless JSON/)
  42. expect(() => snapshotSessionFormatJson(new RecordValue())).toThrow(/not lossless JSON/)
  43. expect(() => snapshotSessionFormatJson(new ArrayValue(1))).toThrow(/not lossless JSON/)
  44. })
  45. it.each([
  46. ['non-object header', { header: null, inheritedEventCount: 0, events: [] }],
  47. ['non-array events', { header: { version: 1 }, inheritedEventCount: 0, events: null }],
  48. ['non-object event', { header: { version: 1 }, inheritedEventCount: 0, events: [null] }],
  49. ['non-dense seq', { header: { version: 1 }, inheritedEventCount: 0, events: [{ type: 'x', seq: 1, time: 1, data: {} }] }],
  50. ['empty type', { header: { version: 1 }, inheritedEventCount: 0, events: [{ type: '', seq: 0, time: 1, data: {} }] }],
  51. ['invalid time', { header: { version: 1 }, inheritedEventCount: 0, events: [{ type: 'x', seq: 0, time: 1.5, data: {} }] }],
  52. ['missing data', { header: { version: 1 }, inheritedEventCount: 0, events: [{ type: 'x', seq: 0, time: 1 }] }],
  53. ['oversized cut', { header: { version: 1 }, inheritedEventCount: 1, events: [] }],
  54. ])('refuses an artifact with %s', (_name, artifact) => {
  55. expect(() => snapshotSessionFormatArtifact(artifact as never)).toThrow()
  56. })
  57. it('refuses a non-object header snapshot', () => {
  58. expect(() => snapshotSessionFormatHeader(null as never)).toThrow(/header|object/)
  59. expect(() => snapshotSessionFormatHeader({
  60. version: 1, id: 'missing-seeded', createdAt: 1, delegationDepth: 0,
  61. } as never)).toThrow(/isSeeded/)
  62. })
  63. })