import { describe, expect, it } from 'vitest'; import { GUIDE_END_PREFIX, GUIDE_HEADER_PREFIX, GUIDE_MAX_CHARS, renderGuide } from './guide.js'; describe('renderGuide', () => { it('renderGuide puts the versioned header first and the end line with the line count last', () => { const lines = renderGuide('# Guide\n\nBody text.', '1.2.3').trimEnd().split('\n'); expect(lines[0]).toBe(`${GUIDE_HEADER_PREFIX}1.2.3`); expect(lines.at(-1)).toBe(`${GUIDE_END_PREFIX}1.2.3 (7 lines)`); }); it('renderGuide counts every printed line in the end line', () => { const guide = renderGuide(Array.from({ length: 40 }, (_, i) => `line ${String(i)}`).join('\n'), '0.1.0'); const printed = guide.split('\n').length - 1; expect(guide).toContain(`(${String(printed)} lines)\n`); expect(printed).toBe(44); }); it('renderGuide throws when the output exceeds GUIDE_MAX_CHARS', () => { expect(() => renderGuide('x'.repeat(GUIDE_MAX_CHARS), '0.1.0')).toThrow( /^agent guide is \d+ characters; the cap is 18000\. Move reference material into JSDoc \(npx browserclaw help\) or trim\.$/, ); expect(() => renderGuide('x'.repeat(GUIDE_MAX_CHARS - 200), '0.1.0')).not.toThrow(); }); it('renderGuide trims the body and ends with one newline', () => { const guide = renderGuide('\n\n # Guide\n\nBody.\n\n\n', '0.1.0'); expect(guide).toBe(`${GUIDE_HEADER_PREFIX}0.1.0\n\n# Guide\n\nBody.\n\n${GUIDE_END_PREFIX}0.1.0 (7 lines)\n`); expect(guide.endsWith('\n\n')).toBe(false); }); });