package thintest
import (
"encoding/json"
"fmt"
"regexp"
"strings"
"testing"
"amadan.net/oficina/equipo/internal/specialist"
"amadan.net/oficina/equipo/internal/wire"
)
// TestContractDesignNamedAsItself is the production shape Paul found
// (27d50e7f): Design added for Patrix, its task running as an
// Engineering-kind step bound to design-v1. Everything that names the
// step's specialist names Design: the step and the task carry their maker
// in the person API; the sidebar has a Design row (its name, icon and
// drawer) working on the task while Engineering's stays idle; Design's
// drawer, the task page's step lines and chip, and the conversation's
// report header say Design, never Engineering, for this work.
func TestContractDesignNamedAsItself(t *testing.T) {
r := newToolRigSaying(t, wire.ProtocolCurrent, overhaulWords)
r.designOn(rendererHealth(t, nil))
sp, id := r.designAdded(r.proposeDesign("d-propose"))
d := r.designStarted(sp, id)
k := r.task(id)
eng := stepsOf(k, wire.SpecialistEngineering)[0]
if eng.Maker == nil || eng.Maker.ID != sp.ID || eng.Maker.Profile != specialist.ProfileDesign {
t.Fatalf("the step's maker %+v", eng.Maker)
}
if k.Maker == nil || k.Maker.ID != sp.ID || k.Maker.Profile != specialist.ProfileDesign {
t.Fatalf("the task's maker %+v", k.Maker)
}
// The step's events (what the stream and the apps' snapshots read)
// carry the maker too.
rows, err := r.inst.DB.Writer().Query("SELECT body FROM chat_events WHERE type = ? ORDER BY id", wire.EventStep)
if err != nil {
t.Fatal(err)
}
events := 0
for rows.Next() {
var body string
var e wire.Event
var st wire.Step
if err := rows.Scan(&body); err != nil || json.Unmarshal([]byte(body), &e) != nil || json.Unmarshal(e.Data, &st) != nil {
t.Fatalf("a step event %q: %v", body, err)
}
if st.ID != d.StepID {
continue
}
events++
if st.Maker == nil || st.Maker.ID != sp.ID || st.Maker.Profile != specialist.ProfileDesign {
t.Fatalf("a step event without its maker: %s", body)
}
}
rows.Close()
if events == 0 {
t.Fatal("no events for Design's step")
}
owner := r.inst.SessionFor(t, Owner)
rail := railOf(t, r.get(owner, "/tasks", 200))
row := regexp.MustCompile(`(?s)`).FindString(rail)
for _, want := range []string{`href="/team/` + sp.ID + `"`, `data-state="working"`, `Design`, `` + k.Title + ``} {
if !strings.Contains(row, want) {
t.Fatalf("Design's row lacks %q: %q\nrail: %s", want, row, rail)
}
}
engRow := regexp.MustCompile(`(?s)`).FindString(rail)
if !strings.Contains(engRow, `data-state="idle"`) || strings.Contains(engRow, k.Title) {
t.Fatalf("Engineering's row shows Design's work: %s", engRow)
}
drawer := r.get(owner, "/team/"+sp.ID, 200)
for _, want := range []string{`data-team="` + sp.ID + `" data-state="working"`, `Design
`, `href="/tasks/` + id + `"`} {
if !strings.Contains(drawer, want) {
t.Fatalf("Design's drawer lacks %q:\n%s", want, drawer)
}
}
if p := r.get(owner, "/team/engineering", 200); strings.Contains(p, `href="/tasks/`+id+`"`) {
t.Fatal("Engineering's drawer lists Design's task")
}
page := r.get(owner, "/tasks/"+id, 200)
for _, want := range []string{fmt.Sprintf("Design, attempt %d", eng.Attempt), "Design is on it now"} {
if !strings.Contains(page, want) {
t.Fatalf("the task page lacks %q", want)
}
}
for _, not := range []string{fmt.Sprintf("Engineering, attempt %d", eng.Attempt), "Engineering is on it now"} {
if strings.Contains(page, not) {
t.Fatalf("the task page says %q", not)
}
}
if list := r.get(owner, "/tasks", 200); !strings.Contains(list, "Design working") || strings.Contains(list, "Engineering working") {
t.Fatal("the task list's chip does not say Design is working")
}
// The task's report in the conversation is Design's.
conv := r.get(owner, "/conversation", 200)
authors := regexp.MustCompile(`([^<]*)`).FindAllStringSubmatch(conv, -1)
design := false
for _, a := range authors {
design = design || a[1] == "Design"
if a[1] == "Engineering" {
t.Fatalf("a report by Engineering in a conversation where only Design works: %v", authors)
}
}
if !design {
t.Fatalf("no report by Design: %v", authors)
}
// Once the step ends with a summary, its line in the task's steps is
// Design's too.
tokens := int64(1000)
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}}})
page = r.get(owner, "/tasks/"+id, 200)
if !strings.Contains(page, fmt.Sprintf("Design, attempt %d: ", d.Attempt)) || strings.Contains(page, fmt.Sprintf("Engineering, attempt %d: ", d.Attempt)) {
t.Fatal("the ended step's line is not Design's")
}
// Its spend is Design's on Usage by role, not Engineering's.
raw, _ := r.fetch(r.client.Token, "/api/v1/usage?period=week&by=role", 200)
var totals wire.UsageTotals
if err := wire.Strict(raw, &totals); err != nil {
t.Fatal(err)
}
roles := map[string]int64{}
for _, row := range totals.Rows {
roles[row.ID] += row.Attempts
}
if roles[specialist.ProfileDesign] != 1 || roles[wire.SpecialistEngineering] != 0 {
t.Fatalf("usage by role %s", raw)
}
if usage := r.get(owner, "/usage?period=week&by=role", 200); !regexp.MustCompile(`