package app

import (
	"strings"
	"testing"
)

// The collapsed tool row for a write/edit must summarise the diff by its row count (what
// ^x shows), not the raw diff byte length that used to read as a meaningless "N chars".
func TestToolRowSummary_DiffReportsRows(t *testing.T) {
	diff := "--- a/f\n+++ b/f\n@@ -1,2 +1,3 @@\n context\n-old line\n+new line\n+added line\n"
	got := toolRowSummary(diff)
	wantRows := previewRowCount(diff)
	if !strings.Contains(got, "▾ diff") || !strings.Contains(got, itoa(wantRows)+" rows") {
		t.Fatalf("diff summary = %q, want a diff row count of %d", got, wantRows)
	}
	if strings.Contains(got, "output") {
		t.Errorf("a diff should not be labelled 'output': %q", got)
	}
}

// Non-diff output reports a true character (rune) count, not a byte count.
func TestToolRowSummary_NonDiffCountsRunes(t *testing.T) {
	content := "café — déjà" // 11 runes, but 14 bytes (é, —, à are multi-byte)
	got := toolRowSummary(content)
	if !strings.Contains(got, "11 chars") {
		t.Fatalf("non-diff summary = %q, want 11 chars (runes, not bytes)", got)
	}
}

// A changed line whose content starts with "++"/"--" must still be counted (and rendered):
// requiring the trailing space on the file-header guard prevents mistaking it for a header.
func TestDiffSummary_CountsDoublePrefixContentLines(t *testing.T) {
	// The added line's content is "++flag"; in the diff that's "+" + "++flag" = "+++flag".
	diff := "--- a/f\n+++ b/f\n@@ -0,0 +1,2 @@\n+++flag\n+normal\n"
	_, added, removed := diffSummary(diff)
	if added != 2 {
		t.Errorf("added = %d, want 2 (the '+++flag' content line must count)", added)
	}
	if removed != 0 {
		t.Errorf("removed = %d, want 0", removed)
	}
	// The renderer must keep the line too (2 add rows), not drop it as a header.
	if n := previewRowCount(diff); n != 2 {
		t.Errorf("rendered rows = %d, want 2", n)
	}
}
