package view import ( "bufio" "bytes" "context" "encoding/json" "fmt" "net/http" "net/http/httptest" "strings" "sync" "testing" "time" seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner" ) // fakeRunner answers the seat mesh-pipeline-runner's list and show as the runner's process does, from rows and pipelines // the test sets; it counts the lists asked. type fakeRunner struct { mu sync.Mutex rows map[string][]seat.PipelineRow // by branch shows map[string]seat.ShowAnswer // by pipeline, or pipeline/mover lists int } func (f *fakeRunner) call(key string, body any) (json.RawMessage, error) { f.mu.Lock() defer f.mu.Unlock() switch key { case PipelinesListVerb: in := body.(seat.ListInput) if in.Limit < 1 || in.Full { return nil, fmt.Errorf("list asked with limit %d, full %v", in.Limit, in.Full) } f.lists++ rows := f.rows[in.Branch] return json.Marshal(seat.ListAnswer{Rows: rows[:min(int(in.Limit), len(rows))]}) case PipelinesShowVerb: in := body.(seat.ShowInput) k := in.Pipeline if in.Mover != "" { k += "/" + in.Mover } a, ok := f.shows[k] if !ok { return json.Marshal(map[string]any{"isError": true, "content": []map[string]string{{"type": "text", "text": "no pipeline " + k}}}) } return json.Marshal(a) } return nil, fmt.Errorf("the runner serves no %s", key) } func (f *fakeRunner) setRows(branch string, rows ...seat.PipelineRow) { f.mu.Lock() defer f.mu.Unlock() f.rows[branch] = rows } // changedWithin is p.Changed bounded, so a change never said fails the test instead of hanging it. func changedWithin(ctx context.Context, p *Pipelines, what string, since uint64) bool { ctx, cancel := context.WithTimeout(ctx, 30*time.Second) defer cancel() return p.Changed(ctx, what, since) } // Design 47 H2 H-A-467: the pipelines page reads the runner's list and show, refreshes on its events, never on a timer, // and links each delivery to its pipeline by its source revision. func TestThePipelinesPage(t *testing.T) { started, ended := t0, t0.Add(3*60e9) runner := &fakeRunner{rows: map[string][]seat.PipelineRow{}, shows: map[string]seat.ShowAnswer{ "main.12": {Pipeline: seat.Pipeline{Pipeline: "main.12", Branch: "main", Source: 9, Revision: 12, Outcome: seat.PipelineOutcomeFailed, Started: &started, Ended: &ended, Movers: []seat.Mover{{Module: "alpha", Fingerprint: "src2:aa", Version: "0.1.4"}, {Module: "beta", Fingerprint: "src2:bb"}}}, Movers: []seat.MoverRow{{Module: "alpha", Outcome: seat.MoverRowOutcomePassed}, {Module: "beta", Outcome: seat.MoverRowOutcomeFailed, Failed: &seat.FailedStep{Step: "unit", Artifact: "beta:program", Outcome: "could not run", Why: "no build node"}}}}, "main.12/beta": {Pipeline: seat.Pipeline{Pipeline: "main.12", Branch: "main", Source: 9, Revision: 12, Outcome: seat.PipelineOutcomeFailed, Steps: []seat.StepResult{{Module: "beta", Step: "unit", Artifact: "beta:program", Seat: "node-build-go", Node: "n1", Outcome: "could not run", Started: &started, Ended: &ended, Why: "no build node"}}}}, }} runner.setRows("main", seat.PipelineRow{Pipeline: "main.12", Source: 9, Revision: 12, Started: &started, Outcome: seat.PipelineOutcomeFailed, MoverCount: 2, Steps: map[string]int64{"passed": 5, "failed": 1, "could not run": 1, "waiting for a build node": 2}}) runner.setRows(testBranch, seat.PipelineRow{Pipeline: "release.8", Source: 4, Revision: 8, Outcome: seat.PipelineOutcomePassed}, seat.PipelineRow{Pipeline: "release.7", Source: 4, Revision: 7, Outcome: seat.PipelineOutcomeSuperseded}) p := NewPipelines(runner.call, func() []string { return []string{testBranch} }, 0, nil) m := New(testView) fill(m, sourceE(t, 4)) m.SetPipelines(p) ctx, cancel := context.WithCancel(context.Background()) defer cancel() go p.Run(ctx) if !changedWithin(ctx, p, "", 0) { // the first reading at the start t.Fatal("the lists were never read") } web := func(path string) string { rec := httptest.NewRecorder() Web(m).ServeHTTP(rec, httptest.NewRequest(http.MethodGet, path, nil)) if rec.Code != 200 { t.Fatalf("%s = %d %s", path, rec.Code, rec.Body.String()) } return rec.Body.String() } text := func(f func(*bytes.Buffer)) string { var b bytes.Buffer f(&b) return strings.Join(strings.Fields(b.String()), " ") } rows := []struct { name string got func() string want []string }{ {"the terminal view's list counts the steps per outcome", func() string { return text(func(b *bytes.Buffer) { PipelinesTable(b, p.Page("", 0)) }) }, []string{"PIPELINES OF main", "PASSED FAILED COULD NOT RUN NOT ASKED WAITING WAITING FOR A BUILD NODE BYPASSED NOTHING TO RUN", "main.12 12 9 2026-10-18", "failed 2 5 1 1 0 0 2 0 0"}}, {"the web list links each pipeline and reloads on the view's changes, never a timer", func() string { return web("/pipelines") }, []string{ `main.12`, `new EventSource("/pipelines/changes?since=`}}, {"a pipeline's page is its mover rows, each linking its steps", func() string { return web("/pipelines/show?pipeline=main.12") }, []string{ `alphasrc2:aa0.1.4passed`, "unit beta:program: could not run: no build node", `changes?pipeline=main.12\u0026since=`}}, {"a mover's page is its steps", func() string { return web("/pipelines/show?pipeline=main.12&mover=beta") }, []string{ "unit beta:programnode-build-gon1could not run", "no build node"}}, {"the terminal view's pipeline names each mover's fingerprint, version and failed step", func() string { a, err := p.Show("main.12", "", "") if err != nil { return err.Error() } return text(func(b *bytes.Buffer) { PipelineTable(b, a, "") }) }, []string{"PIPELINE main.12: failed, source 9, revision 12", "alpha src2:aa 0.1.4 passed", "beta src2:bb failed unit beta:program: could not run: no build node"}}, {"the web list asks the newest limit of a branch", func() string { return web("/pipelines?branch=" + testBranch + "&limit=1") }, []string{ "release.8", `name="limit" value="1"`}}, {"the web list is the newest 20 by default", func() string { return web("/pipelines?branch=" + testBranch) }, []string{ "release.8", "release.7", `name="limit" value="20"`}}, {"a pipeline the runner does not hold says so", func() string { return web("/pipelines/show?pipeline=main.99") }, []string{"unreadable: ", "no pipeline main.99"}}, {"a delivery links to the newest pipeline of its source revision", func() string { return text(func(b *bytes.Buffer) { Table(b, m.Snapshot()) }) + web("/") }, []string{testBranch + ": source revision 4: delivered (pipeline release.8)", `(pipeline)`, `Pipelines`}}, } for _, r := range rows { t.Run(r.name, func(t *testing.T) { got := r.got() for _, w := range r.want { if !strings.Contains(got, w) { t.Errorf("got %s\nwant it to hold %q", got, w) } } }) } if got := web("/pipelines?branch=" + testBranch + "&limit=1"); strings.Contains(got, "release.7") { t.Errorf("the newest 1 shows release.7: %s", got) } for _, l := range []string{"0", "x"} { rec := httptest.NewRecorder() Web(m).ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/pipelines?limit="+l, nil)) if rec.Code != http.StatusBadRequest { t.Errorf("limit %s = %d, want 400", l, rec.Code) } } if strings.Contains(web("/pipelines"), "http-equiv=\"refresh\"") || strings.Contains(web("/pipelines/show?pipeline=main.12"), "http-equiv=\"refresh\"") { t.Error("a pipelines page reloads on a timer") } t.Run("an event of the runner reads the lists again and tells the pages", func(t *testing.T) { srv := httptest.NewServer(Web(m)) defer srv.Close() v := p.Version() res, err := http.Get(srv.URL + "/pipelines/changes?since=" + fmt.Sprint(v)) if err != nil { t.Fatal(err) } defer res.Body.Close() stream := bufio.NewReader(res.Body) if line, _ := stream.ReadString('\n'); !strings.HasPrefix(line, ":") { t.Fatalf("the stream starts %q", line) } shown := make(chan bool, 1) go func() { shown <- changedWithin(ctx, p, "main.13", v) }() runner.mu.Lock() before := runner.lists runner.mu.Unlock() runner.setRows("main", seat.PipelineRow{Pipeline: "main.13", Source: 10, Revision: 13, Outcome: seat.PipelineOutcomeRunning, MoverCount: 1}) if err := p.Hear([]byte(`{"pipeline":"main.13","branch":"main","source":10}`)); err != nil { t.Fatal(err) } if !<-shown { t.Fatal("the page of the pipeline heard is not told") } for { line, err := stream.ReadString('\n') if err != nil { t.Fatalf("the stream ended before the change: %v", err) } if line == "data: changed\n" { break } } runner.mu.Lock() after := runner.lists runner.mu.Unlock() if !strings.Contains(web("/pipelines"), "main.13") || after <= before { t.Errorf("the list was not read again (%d lists, %d before): %s", after, before, web("/pipelines")) } if err := p.Hear([]byte(`{"step":"unit"}`)); err == nil { t.Error("an event naming no pipeline is taken") } }) t.Run("the terminal view follows the list on the socket, again at each event", func(t *testing.T) { path, sm := served(t) sm.SetPipelines(p) var seen []string heard := false err := AskPipelines(path, "", 0, "", "", "", true, func(page PipelinesPage, _ *seat.ShowAnswer) bool { seen = append(seen, page.Rows[0].Pipeline) if !heard { heard = true runner.setRows("main", seat.PipelineRow{Pipeline: "main.14", Source: 11, Revision: 14, Outcome: seat.PipelineOutcomeRunning, MoverCount: 1}) if err := p.Hear([]byte(`{"pipeline":"main.14","mover":"alpha","revision":14}`)); err != nil { // a re-pinned names no branch t.Error(err) } return true } return len(seen) < 2 || seen[len(seen)-1] != "main.14" }) if err != nil || seen[len(seen)-1] != "main.14" { t.Fatalf("followed %v, %v", seen, err) } var show *seat.ShowAnswer if err := AskPipelines(path, "", 0, "main.12", "beta", "", false, func(_ PipelinesPage, a *seat.ShowAnswer) bool { show = a; return true }); err != nil { t.Fatal(err) } if len(show.Pipeline.Steps) != 1 || show.Pipeline.Steps[0].Node != "n1" { t.Fatalf("the mover's steps on the socket = %+v", show) } if err := AskPipelines(path, "", 0, "main.99", "", "", false, func(PipelinesPage, *seat.ShowAnswer) bool { return true }); err == nil || !strings.Contains(err.Error(), "no pipeline main.99") { t.Fatalf("a pipeline the runner does not hold = %v", err) } }) t.Run("the pipelines heard are kept up to the setting pipelines-heard", func(t *testing.T) { few := NewPipelines(runner.call, nil, 2, nil) for _, k := range []string{"main.1", "main.2", "main.2", "main.3"} { few.notify(k) } few.mu.Lock() n, last := len(few.heard), few.heard["main.3"] few.mu.Unlock() if n > 2 || last == 0 { t.Errorf("heard holds %d pipelines (main.3 at %d), want at most the setting's 2, the newest kept", n, last) } if d := NewPipelines(runner.call, nil, 0, nil); d.most != DefaultPipelinesHeard { t.Errorf("the default keeps %d, want %d", d.most, DefaultPipelinesHeard) } }) t.Run("a list not read is shown unreadable and followed on, read again at the next event", func(t *testing.T) { var mu sync.Mutex away := true // the runner not answering yet, as the bus says when no holder listens call := func(key string, body any) (json.RawMessage, error) { mu.Lock() defer mu.Unlock() if away { return nil, fmt.Errorf("nats: no responders available for request") } return runner.call(key, body) } up := NewPipelines(call, nil, 0, nil) upCtx, upCancel := context.WithCancel(ctx) defer upCancel() go up.Run(upCtx) if !changedWithin(upCtx, up, "", 0) { t.Fatal("the lists were never read") } path, sm := served(t) sm.SetPipelines(up) var seen []PipelinesPage err := AskPipelines(path, "", 0, "", "", "", true, func(page PipelinesPage, _ *seat.ShowAnswer) bool { seen = append(seen, page) if len(seen) == 1 { mu.Lock() away = false mu.Unlock() if err := up.Hear([]byte(`{"pipeline":"main.14"}`)); err != nil { t.Error(err) } } return page.Error != "" || len(page.Rows) == 0 }) if err != nil || len(seen) < 2 || !strings.Contains(seen[0].Error, "no responders") || seen[len(seen)-1].Rows[0].Pipeline != "main.14" { t.Fatalf("followed %+v, %v; want the unreadable list, then main.14 after the event", seen, err) } var b bytes.Buffer PipelinesTable(&b, seen[0]) if !strings.Contains(b.String(), "unreadable: ") { t.Errorf("the terminal view of an unreadable list = %q", b.String()) } }) }