import { describe, expect, test } from 'bun:test' import { blobLines } from '../main/reviews/store/anchors' import { clampLines, lineCount, viewText } from './lines' const samples = ['', '\n', 'a', 'a\n', 'a\nb', 'a\nb\n', 'a\n\n', '\n\n', 'a\r\nb\r\n', 'a\nb\n\n\n'] describe('lineCount', () => { test('agrees with the store', () => { for (const s of samples) expect([s, lineCount(s)]).toEqual([s, blobLines(Buffer.from(s)).length]) }) }) describe('viewText', () => { test('drops only the line break ending the last line', () => { expect(viewText('a\nb\n')).toBe('a\nb') expect(viewText('a\nb')).toBe('a\nb') expect(viewText('a\r\nb\r\n')).toBe('a\r\nb') expect(viewText('a\n\n')).toBe('a\n') expect(viewText('\n')).toBe('') }) test('leaves an editor as many lines as the store counts, keeping every position', () => { // An editor's line count: line breaks + 1 (CRLF is one break). const editorLines = (s: string) => s.split(/\r?\n/).length for (const s of samples) { const v = viewText(s) expect(s.startsWith(v)).toBe(true) if (s) expect([s, editorLines(v)]).toEqual([s, lineCount(s)]) } }) }) describe('clampLines', () => { test('cuts a range to the lines there are', () => { expect(clampLines([2, 3], 5)).toEqual([2, 3]) expect(clampLines([4, 6], 5)).toEqual([4, 5]) expect(clampLines([0, 1], 5)).toEqual([1, 1]) expect(clampLines([6, 6], 5)).toBeNull() expect(clampLines([1, 1], 0)).toBeNull() }) })