Files
helder/test/diff-view.test.ts
T
2026-09-21 06:47:46 +02:00

41 lines
1.4 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { buildDiffView } from '../src/renderer/src/editor'
import { makeDiff } from '../src/renderer/src/diff'
/** The current-side view of one text pair: the lines Actual marks. */
function view(original: string, updated: string): ReturnType<typeof buildDiffView> {
return buildDiffView(makeDiff('M', original, updated))
}
describe('buildDiffView', () => {
it('marks a rewritten line in place', () => {
const lines = view('a\nold\nb', 'a\nnew\nb')
expect(lines.map((l) => l.text)).toEqual(['a', 'new', 'b'])
expect(lines[1].row).toBe('add')
expect(lines.every((l) => !l.gap)).toBe(true)
})
it('puts a pure deletion on the line that follows it', () => {
const lines = view('a\ngone\nb', 'a\nb')
expect(lines[1].text).toBe('b')
expect(lines[1].gap).toBe('above')
expect(lines[1].row).toBeNull()
})
it('puts a deletion at end of file under the last line', () => {
const lines = view('a\nb\ntail', 'a\nb')
expect(lines[1].gap).toBe('below')
})
it('marks an insertion and leaves the neighbours alone', () => {
const lines = view('a\nb', 'a\nNEW\nb')
expect(lines[1].row).toBe('add')
expect(lines.every((l) => !l.gap)).toBe(true)
})
it('leaves an unchanged file without marks', () => {
const lines = view('a\nb\n', 'a\nb\n')
expect(lines.every((l) => !l.row && !l.gap)).toBe(true)
})
})