import { describe, expect, it } from 'vitest' import { clampLimit, sliceUtf8, truncateJsonBudget, truncateRows } from '../../src/format.js' describe('clampLimit', () => { it('returns fallback when undefined', () => { expect(clampLimit(undefined)).toBe(100) expect(clampLimit(undefined, 7)).toBe(7) }) it('clamps to [1, max]', () => { expect(clampLimit(0)).toBe(1) expect(clampLimit(99999)).toBe(1000) expect(clampLimit(5000, 100, 2000)).toBe(2000) }) }) describe('truncateRows', () => { it('does not truncate when under limit', () => { expect(truncateRows([1, 2], 5)).toEqual({ rows: [1, 2], truncated: false }) }) it('truncates and flags when over limit', () => { expect(truncateRows([1, 2, 3], 2)).toEqual({ rows: [1, 2], truncated: true }) }) }) describe('truncateJsonBudget', () => { it('passes small payloads through untouched', () => { const v = { a: 1 } expect(truncateJsonBudget(v, 1000)).toEqual({ body: v, truncated: false }) }) it('marks oversized payloads truncated', () => { const big = { s: 'x'.repeat(500) } const out = truncateJsonBudget(big, 100) expect(out.truncated).toBe(true) expect((out.body as { _truncated: boolean })._truncated).toBe(true) }) }) describe('sliceUtf8', () => { it('returns the string unchanged when it fits', () => { expect(sliceUtf8('hello', 100)).toBe('hello') }) it('caps to a real UTF-8 byte budget', () => { const out = sliceUtf8('abcdef', 3) expect(Buffer.byteLength(out, 'utf8')).toBeLessThanOrEqual(3) expect(out).toBe('abc') }) it('does not split a multi-byte code point at the boundary', () => { const out = sliceUtf8('aéé', 3) expect(Buffer.byteLength(out, 'utf8')).toBeLessThanOrEqual(3) expect(out).toBe('aé') }) })