| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "regexp" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/specialist" |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // TestContractDesignNamedAsItself is the production shape Paul found |
| 15 | // (27d50e7f): Design added for Patrix, its task running as an |
| 16 | // Engineering-kind step bound to design-v1. Everything that names the |
| 17 | // step's specialist names Design: the step and the task carry their maker |
| 18 | // in the person API; the sidebar has a Design row (its name, icon and |
| 19 | // drawer) working on the task while Engineering's stays idle; Design's |
| 20 | // drawer, the task page's step lines and chip, and the conversation's |
| 21 | // report header say Design, never Engineering, for this work. |
| 22 | func TestContractDesignNamedAsItself(t *testing.T) { |
| 23 | r := newToolRigSaying(t, wire.ProtocolCurrent, overhaulWords) |
| 24 | r.designOn(rendererHealth(t, nil)) |
| 25 | sp, id := r.designAdded(r.proposeDesign("d-propose")) |
| 26 | d := r.designStarted(sp, id) |
| 27 | |
| 28 | k := r.task(id) |
| 29 | eng := stepsOf(k, wire.SpecialistEngineering)[0] |
| 30 | if eng.Maker == nil || eng.Maker.ID != sp.ID || eng.Maker.Profile != specialist.ProfileDesign { |
| 31 | t.Fatalf("the step's maker %+v", eng.Maker) |
| 32 | } |
| 33 | if k.Maker == nil || k.Maker.ID != sp.ID || k.Maker.Profile != specialist.ProfileDesign { |
| 34 | t.Fatalf("the task's maker %+v", k.Maker) |
| 35 | } |
| 36 | |
| 37 | // The step's events (what the stream and the apps' snapshots read) |
| 38 | // carry the maker too. |
| 39 | rows, err := r.inst.DB.Writer().Query("SELECT body FROM chat_events WHERE type = ? ORDER BY id", wire.EventStep) |
| 40 | if err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | events := 0 |
| 44 | for rows.Next() { |
| 45 | var body string |
| 46 | var e wire.Event |
| 47 | var st wire.Step |
| 48 | if err := rows.Scan(&body); err != nil || json.Unmarshal([]byte(body), &e) != nil || json.Unmarshal(e.Data, &st) != nil { |
| 49 | t.Fatalf("a step event %q: %v", body, err) |
| 50 | } |
| 51 | if st.ID != d.StepID { |
| 52 | continue |
| 53 | } |
| 54 | events++ |
| 55 | if st.Maker == nil || st.Maker.ID != sp.ID || st.Maker.Profile != specialist.ProfileDesign { |
| 56 | t.Fatalf("a step event without its maker: %s", body) |
| 57 | } |
| 58 | } |
| 59 | rows.Close() |
| 60 | if events == 0 { |
| 61 | t.Fatal("no events for Design's step") |
| 62 | } |
| 63 | |
| 64 | owner := r.inst.SessionFor(t, Owner) |
| 65 | rail := railOf(t, r.get(owner, "/tasks", 200)) |
| 66 | row := regexp.MustCompile(`(?s)<a equipo-spec id="nav-` + regexp.QuoteMeta(sp.ID) + `".*?</a>`).FindString(rail) |
| 67 | for _, want := range []string{`href="/team/` + sp.ID + `"`, `data-state="working"`, `<span equipo-spec-name>Design</span>`, `<span equipo-nav-task>` + k.Title + `</span>`} { |
| 68 | if !strings.Contains(row, want) { |
| 69 | t.Fatalf("Design's row lacks %q: %q\nrail: %s", want, row, rail) |
| 70 | } |
| 71 | } |
| 72 | engRow := regexp.MustCompile(`(?s)<a equipo-spec id="nav-engineering".*?</a>`).FindString(rail) |
| 73 | if !strings.Contains(engRow, `data-state="idle"`) || strings.Contains(engRow, k.Title) { |
| 74 | t.Fatalf("Engineering's row shows Design's work: %s", engRow) |
| 75 | } |
| 76 | |
| 77 | drawer := r.get(owner, "/team/"+sp.ID, 200) |
| 78 | for _, want := range []string{`data-team="` + sp.ID + `" data-state="working"`, `<h1 id="team-title">Design</h1>`, `href="/tasks/` + id + `"`} { |
| 79 | if !strings.Contains(drawer, want) { |
| 80 | t.Fatalf("Design's drawer lacks %q:\n%s", want, drawer) |
| 81 | } |
| 82 | } |
| 83 | if p := r.get(owner, "/team/engineering", 200); strings.Contains(p, `href="/tasks/`+id+`"`) { |
| 84 | t.Fatal("Engineering's drawer lists Design's task") |
| 85 | } |
| 86 | |
| 87 | page := r.get(owner, "/tasks/"+id, 200) |
| 88 | for _, want := range []string{fmt.Sprintf("Design, attempt %d", eng.Attempt), "Design is on it now"} { |
| 89 | if !strings.Contains(page, want) { |
| 90 | t.Fatalf("the task page lacks %q", want) |
| 91 | } |
| 92 | } |
| 93 | for _, not := range []string{fmt.Sprintf("Engineering, attempt %d", eng.Attempt), "Engineering is on it now"} { |
| 94 | if strings.Contains(page, not) { |
| 95 | t.Fatalf("the task page says %q", not) |
| 96 | } |
| 97 | } |
| 98 | if list := r.get(owner, "/tasks", 200); !strings.Contains(list, "Design working") || strings.Contains(list, "Engineering working") { |
| 99 | t.Fatal("the task list's chip does not say Design is working") |
| 100 | } |
| 101 | // The task's report in the conversation is Design's. |
| 102 | conv := r.get(owner, "/conversation", 200) |
| 103 | authors := regexp.MustCompile(`<span equipo-roster-author><b>([^<]*)</b>`).FindAllStringSubmatch(conv, -1) |
| 104 | design := false |
| 105 | for _, a := range authors { |
| 106 | design = design || a[1] == "Design" |
| 107 | if a[1] == "Engineering" { |
| 108 | t.Fatalf("a report by Engineering in a conversation where only Design works: %v", authors) |
| 109 | } |
| 110 | } |
| 111 | if !design { |
| 112 | t.Fatalf("no report by Design: %v", authors) |
| 113 | } |
| 114 | |
| 115 | // Once the step ends with a summary, its line in the task's steps is |
| 116 | // Design's too. |
| 117 | tokens := int64(1000) |
| 118 | r.step("ds-failed", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepFailed, Reason: "provider_failed", BaseSHA: baseX, Termination: true, Family: "claude", Account: "claude-a", Summary: "Stopped early.", LogTail: "provider failed", Usage: []wire.Usage{{Provider: wire.ProviderClaude, Connection: "claude-a", Model: "claude-opus-5-5", Attempt: d.Attempt, Outcome: wire.OutcomeFailed, Complete: true, InputTokens: &tokens, OutputTokens: &tokens}}}) |
| 119 | page = r.get(owner, "/tasks/"+id, 200) |
| 120 | if !strings.Contains(page, fmt.Sprintf("Design, attempt %d: ", d.Attempt)) || strings.Contains(page, fmt.Sprintf("Engineering, attempt %d: ", d.Attempt)) { |
| 121 | t.Fatal("the ended step's line is not Design's") |
| 122 | } |
| 123 | |
| 124 | // Its spend is Design's on Usage by role, not Engineering's. |
| 125 | raw, _ := r.fetch(r.client.Token, "/api/v1/usage?period=week&by=role", 200) |
| 126 | var totals wire.UsageTotals |
| 127 | if err := wire.Strict(raw, &totals); err != nil { |
| 128 | t.Fatal(err) |
| 129 | } |
| 130 | roles := map[string]int64{} |
| 131 | for _, row := range totals.Rows { |
| 132 | roles[row.ID] += row.Attempts |
| 133 | } |
| 134 | if roles[specialist.ProfileDesign] != 1 || roles[wire.SpecialistEngineering] != 0 { |
| 135 | t.Fatalf("usage by role %s", raw) |
| 136 | } |
| 137 | if usage := r.get(owner, "/usage?period=week&by=role", 200); !regexp.MustCompile(`<th[^>]*>Design</th>`).MatchString(usage) { |
| 138 | t.Fatal("Usage by role does not name Design") |
| 139 | } |
| 140 | } |
| 141 | |