package tui import ( "context" "fmt" "strings" "testing" "git.a-wan.net/alan/pipelines/internal/protocol" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/x/ansi" ) func artifactsFixtureModel() Model { m := New(context.Background(), "ws://127.0.0.1:8080/connect") m.width, m.height = 100, 30 m.runOpen = true m.keys.runs = true m.runDetailFocus = true m.requestedRunID = 169 m.runDetail = &protocol.RunDetail{ RunSummary: protocol.RunSummary{ID: 169, Name: "demo", Version: 3, Status: "succeeded"}, Steps: []protocol.RunStep{ {Sequence: 1, Stage: "implementer", Status: "completed", Artifacts: []protocol.StageArtifact{ {Name: "build.log", SizeBytes: 2337, ContentType: "text/plain; charset=utf-8", URL: "/artifacts/169/1/build.log"}, }, ArtifactsSkipped: 1, ArtifactsTruncated: 2}, {Sequence: 2, Stage: "ui_reviewer", Status: "completed", Artifacts: []protocol.StageArtifact{ {Name: "01-pill.png", SizeBytes: 80544, ContentType: "image/png", URL: "/artifacts/169/2/01-pill.png"}, {Name: "morph.mp4", SizeBytes: 645782, ContentType: "video/mp4", URL: "/artifacts/169/2/morph.mp4"}, }}, {Sequence: 3, Stage: "committer", Status: "completed"}, {Sequence: 4, Stage: "reviewer", Status: "running"}, }, } return m } func artifactKey(m Model, key rune) Model { updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{key}}) return updated.(Model) } func TestRunArtifactVisitsGroupAndOrder(t *testing.T) { steps := []protocol.RunStep{ {Sequence: 3, Stage: "build"}, {Sequence: 2, Stage: "plan"}, {Sequence: 1, Stage: "plan", Control: &protocol.ControlEvidence{Visit: 1}, Artifacts: []protocol.StageArtifact{{Name: "plan.md"}}}, } visits := runArtifactVisits(steps) want := []struct { stage string visit int }{{"build", 1}, {"plan", 1}, {"plan", 2}} if len(visits) != len(want) { t.Fatalf("visits = %d, want %d", len(visits), len(want)) } for i, w := range want { if visits[i].stage != w.stage || visits[i].visit != w.visit { t.Fatalf("visit %d = %s/%d, want %s/%d", i, visits[i].stage, visits[i].visit, w.stage, w.visit) } } } func TestArtifactsViewGroupsMarkersAndWording(t *testing.T) { m := artifactsFixtureModel() m.width = 120 view := ansi.Strip(m.artifactsView(24)) for _, want := range []string{ "implementer · visit 1", "ui_reviewer · visit 1", "committer · visit 1", "reviewer · visit 1", "Artifacts · 1 recorded · partial capture", "1 files excluded from artifact capture.", "2 files omitted by artifact capture limits.", "Artifacts · 2 recorded", "Artifacts · not recorded for this visit", "Artifacts · capture pending", "[img]", "01-pill.png", "morph.mp4", "video/mp4", "78.7 KiB", "630.6 KiB", "/artifacts/169/2/01-pill.png", } { if !strings.Contains(view, want) { t.Fatalf("artifacts view missing %q:\n%s", want, view) } } for _, line := range strings.Split(view, "\n") { if strings.Contains(line, "morph.mp4") && strings.Contains(line, "[img]") { t.Fatalf("non-image artifact marked as an image: %q", line) } } } func TestArtifactsViewColumnsAlign(t *testing.T) { m := artifactsFixtureModel() m.width = 120 lines := strings.Split(ansi.Strip(m.artifactsView(24)), "\n") var image, video string for _, line := range lines { if strings.Contains(line, "01-pill.png") { image = line } if strings.Contains(line, "morph.mp4") { video = line } } if image == "" || video == "" { t.Fatalf("artifact rows not both rendered:\n%s", strings.Join(lines, "\n")) } name := strings.Index(image, "01-pill.png") typeStart := strings.Index(image, "image/png") sizeStart := strings.Index(image, "78.7 KiB") if name != strings.Index(video, "morph.mp4") || name < 0 { t.Fatalf("name column misaligned:\n%q\n%q", image, video) } if typeStart != strings.Index(video, "video/mp4") || typeStart < 0 { t.Fatalf("type column misaligned:\n%q\n%q", image, video) } if sizeStart+len("78.7 KiB") != strings.Index(video, "630.6 KiB")+len("630.6 KiB") || sizeStart < 0 { t.Fatalf("size column misaligned:\n%q\n%q", image, video) } } func TestArtifactsViewNeverOverflows(t *testing.T) { for _, width := range []int{160, 120, 80, 63, 40, 24, 12} { m := artifactsFixtureModel() m.width = width for _, line := range strings.Split(m.artifactsView(24), "\n") { if got := ansi.StringWidth(line); got > width { t.Fatalf("width %d: line width %d: %q", width, got, line) } } } m := artifactsFixtureModel() m.width = 40 view := ansi.Strip(m.artifactsView(24)) if !strings.Contains(view, "/artifacts/169/1/build.log") { t.Fatalf("narrow view must keep the selected URL available:\n%s", view) } } func TestArtifactsViewEmptyRun(t *testing.T) { m := New(context.Background(), "ws://127.0.0.1:8080/connect") m.width, m.height = 100, 24 m.runDetail = &protocol.RunDetail{RunSummary: protocol.RunSummary{ID: 5}} view := ansi.Strip(m.artifactsView(20)) for _, want := range []string{"Artifacts · run #5", "No recorded visits yet.", "No artifacts recorded"} { if !strings.Contains(view, want) { t.Fatalf("empty artifacts view missing %q:\n%s", want, view) } } } func artifactsScrollFixture() (Model, []string) { m := New(context.Background(), "ws://127.0.0.1:8080/connect") m.width, m.height = 80, 14 m.runOpen = true m.keys.runs = true m.runDetailFocus = true m.requestedRunID = 7 var steps []protocol.RunStep names := make([]string, 0, 12) for i := 0; i < 12; i++ { name := fmt.Sprintf("shot-%02d.png", i) names = append(names, name) steps = append(steps, protocol.RunStep{Sequence: i + 1, Stage: "ui_reviewer", Status: "completed", Artifacts: []protocol.StageArtifact{{Name: name, SizeBytes: 1024, ContentType: "image/png", URL: "/artifacts/7/" + name}}}) } m.runDetail = &protocol.RunDetail{RunSummary: protocol.RunSummary{ID: 7}, Steps: steps} m.artifactsOpen = true return m, names } func TestArtifactsViewKeepsSelectionVisible(t *testing.T) { m, names := artifactsScrollFixture() for i := 0; i < len(names)-1; i++ { m = artifactKey(m, 'j') } if view := ansi.Strip(m.View()); !strings.Contains(view, names[len(names)-1]) { t.Fatalf("last artifact scrolled out of view:\n%s", view) } for i := 0; i < len(names)-1; i++ { m = artifactKey(m, 'k') } if view := ansi.Strip(m.View()); !strings.Contains(view, names[0]) { t.Fatalf("first artifact not visible after scrolling back:\n%s", view) } } func TestArtifactsSelectByNameScrollsIntoView(t *testing.T) { m, names := artifactsScrollFixture() m.selectArtifactByName(names[len(names)-1]) if view := ansi.Strip(m.View()); !strings.Contains(view, names[len(names)-1]) { t.Fatalf("selectArtifactByName did not scroll the match into view:\n%s", view) } } func TestResolveArtifactURL(t *testing.T) { cases := []struct { server, artifact, want string ok bool }{ {"ws://127.0.0.1:8080/connect", "/artifacts/1/2/a.png", "http://127.0.0.1:8080/artifacts/1/2/a.png", true}, {"wss://host/connect", "/artifacts/1/2/a.png", "https://host/artifacts/1/2/a.png", true}, {"ws://host/connect", "artifacts/1/2/a.png", "http://host/artifacts/1/2/a.png", true}, {"ws://host/connect", "https://cdn.example/a.png", "https://cdn.example/a.png", true}, {"", "/artifacts/1/2/a.png", "", false}, {"http://host/connect", "/artifacts/1/2/a.png", "", false}, } for _, tc := range cases { got, ok := resolveArtifactURL(tc.server, tc.artifact) if ok != tc.ok || got != tc.want { t.Fatalf("resolveArtifactURL(%q, %q) = %q,%t want %q,%t", tc.server, tc.artifact, got, ok, tc.want, tc.ok) } } } func TestArtifactsViewOpenAndCopyKeys(t *testing.T) { previousOpen, previousCopy := openExternal, copyText defer func() { openExternal, copyText = previousOpen, previousCopy }() var opened, copied string openExternal = func(target string) error { opened = target; return nil } copyText = func(text string) error { copied = text; return nil } m := artifactsFixtureModel() m = artifactKey(m, 'A') if !m.artifactsOpen { t.Fatal("A did not open the artifacts view") } if m.artifactIndex != 0 { t.Fatalf("selection = %d, want the first artifact", m.artifactIndex) } updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter}) m = updated.(Model) if cmd == nil { t.Fatal("Enter did not produce an open command") } updated, _ = m.Update(cmd()) m = updated.(Model) if opened != "http://127.0.0.1:8080/artifacts/169/1/build.log" { t.Fatalf("opened %q", opened) } m = artifactKey(m, 'j') updated, cmd = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'y'}}) m = updated.(Model) if cmd == nil { t.Fatal("y did not produce a copy command") } updated, _ = m.Update(cmd()) m = updated.(Model) if copied != "http://127.0.0.1:8080/artifacts/169/2/01-pill.png" { t.Fatalf("copied %q", copied) } if !strings.Contains(strings.Join(m.lines, "\n"), "Copied artifact URL") { t.Fatalf("copy outcome not surfaced: %v", m.lines) } } func TestArtifactsViewRenderedInRunDetail(t *testing.T) { m := artifactsFixtureModel() m.artifactsOpen = true view := ansi.Strip(m.View()) if !strings.Contains(view, "Artifacts · run #169") { t.Fatalf("run view did not render the artifacts view:\n%s", view) } if !strings.Contains(m.activeFooter(), "y copy URL") { t.Fatalf("footer does not advertise the artifact controls: %q", m.activeFooter()) } if !strings.Contains(m.activeFooter(), "PgUp/PgDn scroll") { t.Fatalf("footer does not advertise the scroll keys: %q", m.activeFooter()) } } // artifactsTrailingFixture is a run whose last visit is still running and // recorded no artifacts, so its summary sits after every artifact row. func artifactsTrailingFixture() Model { m := New(context.Background(), "ws://127.0.0.1:8080/connect") m.width, m.height = 80, 14 m.runOpen = true m.keys.runs = true m.runDetailFocus = true m.requestedRunID = 7 var steps []protocol.RunStep for i := 0; i < 12; i++ { name := fmt.Sprintf("shot-%02d.png", i) steps = append(steps, protocol.RunStep{Sequence: i + 1, Stage: "ui_reviewer", Status: "completed", Artifacts: []protocol.StageArtifact{{Name: name, SizeBytes: 1024, ContentType: "image/png", URL: "/artifacts/7/" + name}}}) } steps = append(steps, protocol.RunStep{Sequence: 13, Stage: "reviewer", Status: "running"}) m.runDetail = &protocol.RunDetail{RunSummary: protocol.RunSummary{ID: 7}, Steps: steps} m.artifactsOpen = true return m } func TestArtifactsViewEndRevealsTrailingVisit(t *testing.T) { m := artifactsTrailingFixture() if top := ansi.Strip(m.artifactsView(m.artifactsViewHeight())); strings.Contains(top, "capture pending") { t.Fatalf("trailing running visit visible before scrolling:\n%s", top) } m = artifactKey(m, 'G') view := ansi.Strip(m.View()) for _, want := range []string{"reviewer · visit 1", "Artifacts · capture pending"} { if !strings.Contains(view, want) { t.Fatalf("end did not reveal the trailing running visit (%q missing):\n%s", want, view) } } if m.artifactIndex != 11 { t.Fatalf("end did not keep the last artifact selected: %d", m.artifactIndex) } } func TestArtifactsViewPageScrollRevealsTrailingVisit(t *testing.T) { m := artifactsTrailingFixture() for i := 0; i < 100; i++ { before := m.artifactTop updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyCtrlD}) m = updated.(Model) if m.artifactTop == before { break } } if view := ansi.Strip(m.View()); !strings.Contains(view, "capture pending") { t.Fatalf("page scroll did not reveal the trailing running visit:\n%s", view) } } func TestArtifactsViewNarrowKeepsNames(t *testing.T) { m := artifactsFixtureModel() m.width = 12 view := ansi.Strip(m.artifactsView(24)) if !strings.Contains(view, "build") { t.Fatalf("narrow view dropped the artifact name:\n%s", view) } for _, line := range strings.Split(view, "\n") { if got := ansi.StringWidth(line); got > m.width { t.Fatalf("narrow view overflowed: line width %d: %q", got, line) } } }