package server import ( "context" "sync" "testing" "time" "git.a-wan.net/alan/pipelines/internal/protocol" ) // mappedShownRuns is a run store that also reads the card's graph map. type mappedShownRuns struct { mu sync.Mutex runs fakeShownRuns maps map[int64]*protocol.ChatShownRunMap } func (m *mappedShownRuns) RunSummaryByID(ctx context.Context, id int64) (protocol.RunSummary, bool, error) { m.mu.Lock() defer m.mu.Unlock() return m.runs.RunSummaryByID(ctx, id) } func (m *mappedShownRuns) RunShownMap(_ context.Context, id int64) (*protocol.ChatShownRunMap, error) { m.mu.Lock() defer m.mu.Unlock() return m.maps[id], nil } func TestShowRunCarriesTheGraphMapAndRefreshesItsProgress(t *testing.T) { previous := shownRunRefreshDelay shownRunRefreshDelay = 10 * time.Millisecond t.Cleanup(func() { shownRunRefreshDelay = previous }) f := newChatToolFixture(t) stages := func(implement string) []protocol.ChatShownRunStage { return []protocol.ChatShownRunStage{ {Key: "plan", Edges: map[string]string{"success": "implement"}, Visits: 1, Status: "completed", Seconds: 30}, {Key: "implement", Edges: map[string]string{"success": "#"}, Visits: 1, Status: implement}, } } store := &mappedShownRuns{ runs: fakeShownRuns{7: {ID: 7, Name: "feature", Status: "running", CurrentStage: "implement", CreatedAt: "2026-12-01T10:00:00Z"}}, maps: map[int64]*protocol.ChatShownRunMap{7: {Stages: stages("running"), Visits: []protocol.RunGraphVisit{ {Sequence: 1, Stage: "plan", Status: "completed", Outcome: "success", Target: "implement"}, {Sequence: 2, Stage: "implement", Status: "running"}, }}}, } f.service.runs = store token := f.token(t, chatToolSessionScope+"ses_m") if r := f.call(token, "show_run", `{"run_id":7}`); r.IsError { t.Fatalf("show_run: %v", r) } item := f.items(t, "ses_m")[0] if item.Run.Map == nil || len(item.Run.Map.Stages) != 2 || item.Run.Map.Stages[1].Status != "running" { t.Fatalf("run map = %+v", item.Run.Map) } // Only the stage progress changes: the summary is the same, the card is not. store.mu.Lock() store.maps[7] = &protocol.ChatShownRunMap{Stages: stages("completed"), Visits: store.maps[7].Visits} store.mu.Unlock() f.service.observeRun(protocol.RunEventFrame{RunID: 7, Status: "running"}) deadline := time.Now().Add(5 * time.Second) for { item = f.items(t, "ses_m")[0] if item.Run.Map != nil && item.Run.Map.Stages[1].Status == "completed" { break } if time.Now().After(deadline) { t.Fatalf("the run map was not refreshed: %+v", item.Run.Map) } time.Sleep(10 * time.Millisecond) } }