import { describe, expect, test } from 'bun:test' import { baseLinePlace, inRemoved, lineMarks } from './hunks' describe('lineMarks', () => { test('classifies added, modified and deleted hunks', () => { const marks = lineMarks( [ { oldStart: 2, oldLines: 0, newStart: 3, newLines: 2 }, // added 3-4 { oldStart: 5, oldLines: 1, newStart: 7, newLines: 1 }, // modified 7 { oldStart: 9, oldLines: 3, newStart: 9, newLines: 0 }, // deleted after 9 { oldStart: 1, oldLines: 1, newStart: 0, newLines: 0 } // deleted at top ], 20 ) expect(Object.fromEntries(marks)).toEqual({ 1: 'deleted', 3: 'added', 4: 'added', 7: 'modified', 9: 'deleted' }) }) }) describe('base lines in the head view', () => { // Base 1-10: two lines added after base 2, base 5-6 removed, base 9 replaced. const hunks = [ { oldStart: 9, oldLines: 1, newStart: 9, newLines: 1 }, { oldStart: 2, oldLines: 0, newStart: 3, newLines: 2 }, { oldStart: 5, oldLines: 2, newStart: 6, newLines: 0 } ] test('a removed or replaced line is in its hunk; a kept line maps to its head line', () => { expect(baseLinePlace(hunks, 5)).toEqual({ removed: hunks[2]! }) expect(baseLinePlace(hunks, 6)).toEqual({ removed: hunks[2]! }) expect(baseLinePlace(hunks, 9)).toEqual({ removed: hunks[0]! }) expect([1, 2, 3, 4, 7, 8, 10].map((l) => baseLinePlace(hunks, l))).toEqual([1, 2, 5, 6, 7, 8, 10].map((headLine) => ({ headLine }))) // Removed at the top of the file: kept lines move up; nothing goes above line 0. expect(baseLinePlace([{ oldStart: 1, oldLines: 2, newStart: 0, newLines: 0 }], 3)).toEqual({ headLine: 1 }) }) test('inRemoved: overlaps with removed or replaced base lines only', () => { expect(inRemoved(hunks, [5, 5])).toBe(true) expect(inRemoved(hunks, [4, 5])).toBe(true) expect(inRemoved(hunks, [9, 9])).toBe(true) expect(inRemoved(hunks, [2, 3])).toBe(false) expect(inRemoved(hunks, [7, 8])).toBe(false) }) })