Files
pipelines/internal/tui/artifacts_test.go
T
alan 95362134aa session export: write only session.html, retire the markdown export
A stage's session was exported twice, as session.md and session.html, and both
files were presented. It is now one html file; the markdown export is retired.

- internal/stage/session_export.go: ExportSession renders and writes exactly one
  file, session.html, via writeSessionFile; renderSessionMarkdown and its helpers
  are gone; SessionExportResult carries HTML/HTMLBytes only. The source read, the
  8 MiB source / 2 MiB rendered bounds, the truncation banner, the
  never-overwrite rule and the honest empty-session handling are unchanged.
- internal/protocol/runs.go: SessionExport drops markdown/markdown_bytes (a
  record written before the change still decodes; unknown fields are ignored),
  and adds LegacySessionMarkdownArtifact with RunStep.PresentedArtifacts(), the
  one Go rule that hides the retired session.md from every artifact listing.
- internal/server/runner.go: evidence.Markdown/MarkdownBytes are no longer copied.
- internal/tui: the artifacts panel, the recorded count and the per-visit summary
  derive from PresentedArtifacts(), so the terminal never lists session.md.
- web/src/app.js: the spike's renderArtifacts applies the same filter.
- android: the Session section is the html export alone in a WebView — SessionView,
  the Markdown/HTML toggle and the session markdown path are removed;
  presentedArtifacts(step) filters the retired session.md out of the per-visit
  gallery, the run gallery and its count, the verdict/selection lists and the
  timeline indicator; data/protocol SessionExport drops the markdown fields.
- Tests pin the single html file, prove no session.md is written and that a
  pre-existing one is neither overwritten nor presented, and prove a legacy
  export record still loads and its session.md is hidden.

The legacy session.md of an older run stays on disk (dated evidence); it is only
no longer produced and no longer presented.

Checks (macOS, Go 1.24, Android Gradle 8.11.1, 18 Oct 2026): gofmt -l . prints
nothing; go build ./... && go vet ./... clean; go test ./internal/... ./cmd/...
-> 19 packages ok, 0 failures; node --check web/src/app.js ok and node --test
web/test -> 20/20; gradle --offline --no-daemon :app:assembleDebug
:app:testDebugUnitTest -> BUILD SUCCESSFUL.

Packaged references checked: internal/author/skill/references/delivery.md and
operations.md both described the session.md+session.html pair, so both are
updated here to describe the single session.html export and the retired, hidden
session.md. README.md and internal/author/skill/SKILL.md carry no session.md
reference and install.go's managed inventory is unchanged (no reference added or
removed). docs/notes/session-html-only.md records the remaining stale shared
documentation (docs/stage-artifacts.md, docs/app-integration-verification.md) for
the docs-fold writer; shared docs are not edited by this item.
2026-10-18 10:06:49 +01:00

393 lines
14 KiB
Go

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)
}
}
}
// A run recorded before the HTML-only change still lists session.md among its
// stored artifacts. It is dated evidence that must never be presented beside the
// session html, so the panel, its recorded count and its summary all hide it,
// exactly as the app does.
func TestArtifactsViewHidesLegacySessionMarkdown(t *testing.T) {
m := New(context.Background(), "ws://127.0.0.1:8080/connect")
m.width, m.height = 120, 24
m.runDetail = &protocol.RunDetail{
RunSummary: protocol.RunSummary{ID: 344, Status: "succeeded"},
Steps: []protocol.RunStep{{Sequence: 1, Stage: "committer", Status: "completed",
Artifacts: []protocol.StageArtifact{
{Name: "session.md", SizeBytes: 62370, ContentType: "text/markdown", URL: "/artifacts/344/1/session.md"},
{Name: "session.html", SizeBytes: 68813, ContentType: "text/html", URL: "/artifacts/344/1/session.html"},
}}},
}
view := ansi.Strip(m.artifactsView(20))
if strings.Contains(view, "session.md") {
t.Fatalf("legacy session markdown presented beside the html:\n%s", view)
}
for _, want := range []string{"session.html", "Artifacts · run #344 · 1 recorded · 1 visits"} {
if !strings.Contains(view, want) {
t.Fatalf("artifacts view missing %q:\n%s", want, view)
}
}
// The step's own stored record is untouched: only the presentation filters.
if len(m.runDetail.Steps[0].Artifacts) != 2 {
t.Fatalf("presentation mutated the stored artifact record: %d", len(m.runDetail.Steps[0].Artifacts))
}
}
func TestArtifactsSummaryAndNotesHideLegacySessionMarkdown(t *testing.T) {
legacy := protocol.RunStep{Status: "completed", ArtifactsSkipped: 1,
Artifacts: []protocol.StageArtifact{{Name: "session.md", URL: "/artifacts/344/1/session.md"}}}
if got, want := artifactsSummary(legacy), "Artifacts · not recorded for this visit"; got != want {
t.Fatalf("summary = %q, want %q", got, want)
}
if notes := artifactCaptureNotes(legacy); len(notes) != 0 {
t.Fatalf("legacy-only visit still produced capture notes: %v", notes)
}
presented := protocol.RunStep{Status: "completed", Artifacts: []protocol.StageArtifact{
{Name: "session.md"}, {Name: "session.html"},
}}
if got, want := artifactsSummary(presented), "Artifacts · 1 recorded"; got != want {
t.Fatalf("summary = %q, want %q", got, want)
}
}
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)
}
}
}