package app

import "testing"

func TestParseUnifiedDiff_Create(t *testing.T) {
	diff := "--- /dev/null\n+++ b/hello.sh\n@@ -0,0 +1,2 @@\n+#!/usr/bin/env bash\n+echo hi\n"
	rows := parseUnifiedDiff(diff)
	if len(rows) != 2 {
		t.Fatalf("want 2 rows, got %d", len(rows))
	}
	for i, r := range rows {
		if r.oldKind != diffBlank || r.oldNum != 0 || r.oldText != "" {
			t.Errorf("row %d: create should have a blank left cell, got %+v", i, r)
		}
		if r.newKind != diffAdd {
			t.Errorf("row %d: right cell should be an addition, got %+v", i, r)
		}
	}
	if rows[0].newNum != 1 || rows[1].newNum != 2 {
		t.Errorf("new line numbers should start at 1: %d, %d", rows[0].newNum, rows[1].newNum)
	}
}

func TestParseUnifiedDiff_EditAlignsAndNumbers(t *testing.T) {
	diff := "--- a/x\n+++ b/x\n@@ -1,3 +1,3 @@\n ctx\n-old\n+new\n more\n"
	rows := parseUnifiedDiff(diff)
	if len(rows) != 3 {
		t.Fatalf("want 3 rows (context, change, context), got %d", len(rows))
	}
	// context row: both sides present, same text, line 1
	if rows[0].oldNum != 1 || rows[0].newNum != 1 || rows[0].oldText != "ctx" || rows[0].newText != "ctx" {
		t.Errorf("context row mis-numbered/aligned: %+v", rows[0])
	}
	// change row: old "old" on left, new "new" on right, both line 2
	c := rows[1]
	if c.oldKind != diffDel || c.newKind != diffAdd || c.oldText != "old" || c.newText != "new" {
		t.Errorf("change row should pair del|add: %+v", c)
	}
	if c.oldNum != 2 || c.newNum != 2 {
		t.Errorf("change row line numbers should be 2/2: %d/%d", c.oldNum, c.newNum)
	}
	// trailing context advances to line 3 on both sides
	if rows[2].oldNum != 3 || rows[2].newNum != 3 {
		t.Errorf("trailing context should be 3/3: %d/%d", rows[2].oldNum, rows[2].newNum)
	}
}

func TestDiffTarget(t *testing.T) {
	got := diffTarget("--- a/old.sh\n+++ b/dir/new.sh\n@@ -0,0 +1 @@\n+x\n")
	if got != "dir/new.sh" {
		t.Errorf("diffTarget = %q, want dir/new.sh", got)
	}
}
