1
0

output-ledger.spec.ts 1.4 KB

1234567891011121314151617181920212223242526
  1. import { expect, it } from 'vitest'
  2. import { OutputLedger } from '../src/output-ledger.ts'
  3. it('accounts for exact JSON escaping, separators and optional completion values', () => {
  4. const ledger = new OutputLedger(20)
  5. const logs: string[] = []
  6. expect(ledger.admit('first', logs)).toBe(true)
  7. expect(ledger.admit('second', logs)).toBe(true)
  8. expect(ledger.admit('third', logs)).toBe(false)
  9. expect(ledger.success(logs)).toEqual({ logs: ['first', 'second'] })
  10. expect(ledger.success(logs, 1)).toEqual({ logs: ['first', 'second'], value: 1 })
  11. expect(ledger.success(logs, 'too long').error?.kind).toBe('output-limit')
  12. })
  13. it('retains a bounded prefix when logs or a failure diagnostic exceed the limit', () => {
  14. for (const maxBytes of [4, 8, 40, 80]) {
  15. const ledger = new OutputLedger(maxBytes)
  16. const result = ledger.limit(['a', 'b', '你好🙂'.repeat(50)])
  17. expect(result.error?.kind).toBe('output-limit')
  18. const bytes = Buffer.byteLength(JSON.stringify(result.logs)) + Buffer.byteLength(JSON.stringify(result.error?.message))
  19. expect(bytes).toBeLessThanOrEqual(maxBytes)
  20. }
  21. const ledger = new OutputLedger(80)
  22. expect(ledger.failure([], { kind: 'exception', message: 'short' })).toEqual({ logs: [], error: { kind: 'exception', message: 'short' } })
  23. expect(ledger.failure([], { kind: 'exception', message: 'x'.repeat(100) }).error?.kind).toBe('output-limit')
  24. })