package tui import ( "strings" "testing" "git.a-wan.net/alan/pipelines/internal/protocol" "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/ansi" "github.com/muesli/termenv" ) func TestDiffHighlightAndTerminalSafety(t *testing.T) { old := lipgloss.ColorProfile() lipgloss.SetColorProfile(termenv.ANSI256) defer lipgloss.SetColorProfile(old) for _, line := range []string{"+added", "-removed", "@@ -1 +1 @@", "--- a/file", "+++ b/file"} { rendered := diffHighlight(line) if rendered == ansi.Strip(rendered) { t.Fatal("diff not highlighted", line) } if ansi.Strip(rendered) != line { t.Fatal("diff text changed", rendered) } } unsafe := diffHighlight("+\x1b[2J\x07evil") if strings.Contains(unsafe, "\x1b[2J") || strings.Contains(unsafe, "\x07") { t.Fatal("terminal control retained", unsafe) } } func TestStageDiffResultsAndHistoricalState(t *testing.T) { step := protocol.RunStep{Status: "completed", FileChanges: &protocol.StageFileChanges{Status: "partial", Skipped: 1, Files: []protocol.FileChange{{Path: "main.go", Kind: "modified", Patch: "--- a/main.go\n+++ b/main.go\n@@ -1 +1 @@\n-old\n+new\n"}, {Path: "image.png", Kind: "added", Binary: true}, {Path: "big", Kind: "modified", Truncated: true}}}} text := ansi.Strip(strings.Join(stageDiffLines(step), "\n")) for _, want := range []string{"main.go", "-old", "+new", "Binary file", "truncated", "partial capture", "not proof of exclusive authorship"} { if !strings.Contains(text, want) { t.Fatal(want, text) } } if !strings.Contains(fileChangesSummary(protocol.RunStep{Status: "completed"}), "not recorded") { t.Fatal("historical stage implies no edits") } if !strings.Contains(fileChangesSummary(protocol.RunStep{Status: "running"}), "pending") { t.Fatal("running stage implies no edits") } m := Model{width: 80, runDetail: &protocol.RunDetail{Steps: []protocol.RunStep{step}}, runStepIndex: 1} text = ansi.Strip(strings.Join(m.runSectionLines(74), "\n")) if !strings.Contains(text, "[d] ▾ File changes") || !strings.Contains(text, "+new") { t.Fatal("diff missing from stage details", text) } m.toggleRunSection(5) text = ansi.Strip(strings.Join(m.runSectionLines(74), "\n")) if strings.Contains(text, "+new") { t.Fatal("collapsed diff still visible") } } func TestDiffShortcutPerVisitAndNarrowLayout(t *testing.T) { m := runInspectionModel() m = runKey(m, "n") c := &protocol.StageFileChanges{Status: "complete", Files: []protocol.FileChange{{Path: "long/path/main.go", Kind: "modified", Patch: "--- a/main.go\n+++ b/main.go\n@@ -1 +1 @@\n-old line\n+new line\n"}}} m.runDetail.Steps[0].FileChanges = c m = runKey(m, "g") m = runKey(m, "d") if m.runGraphFocus || !m.runDetailFocus || m.runSectionClosed(5) { t.Fatal("diff shortcut did not focus open section") } _, title := runFocusedSection(m.runSectionLines(m.runDetailWidth()), m.runTop) if title != "File changes" { t.Fatal(title) } m = runKey(m, "d") if !m.runSectionClosed(5) { t.Fatal("diff shortcut failed to collapse") } m = runKey(m, "n") if m.runSectionClosed(5) { t.Fatal("collapse leaked to next visit") } m = runKey(m, "p") m = runKey(m, "d") for _, width := range []int{120, 63, 35, 10} { m.width = width for _, line := range strings.Split(m.runsView(24), "\n") { if ansi.StringWidth(line) > width { t.Fatal("diff exceeds viewport", width, line) } } } }