| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/base64" |
| 6 | "regexp" |
| 7 | "slices" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/equipo/chat" |
| 13 | "amadan.net/oficina/equipo/internal/wire" |
| 14 | ) |
| 15 | |
| 16 | // connectionsHTML is the Connections page's content for v. |
| 17 | func connectionsHTML(t *testing.T, v modelsView) string { |
| 18 | t.Helper() |
| 19 | var buf bytes.Buffer |
| 20 | if err := pages["models"].ExecuteTemplate(&buf, "content", pageData{Models: v, Address: "me@example.com"}); err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | return buf.String() |
| 24 | } |
| 25 | |
| 26 | // TestConnectionsPageGroupsByMachine (Mac runners slice F, task 11): |
| 27 | // the Connections page groups the person's connections by machine, the |
| 28 | // main runner first, each under its name. A Mac worker that can take |
| 29 | // Claude (protocol 37, a seal key, not removing, not tainted) offers |
| 30 | // "Add Claude to <Mac>", which starts on that Mac; a worker below 37, a |
| 31 | // tainted one and the main runner offer none. A worker's connection with |
| 32 | // the same local id as one on the main runner keeps its own usage and is |
| 33 | // never the coordinator's. |
| 34 | func TestConnectionsPageGroupsByMachine(t *testing.T) { |
| 35 | key := base64.StdEncoding.EncodeToString(bytes.Repeat([]byte{7}, 32)) |
| 36 | snap := screenshotSnapshot() |
| 37 | snap.Connections = append(snap.Connections, wire.Connection{ID: "c-mac", RunnerID: "w-studio", LocalID: "claude-1", Provider: wire.ProviderClaude, Kind: wire.KindToken, Label: "studio@tito.io", State: wire.ConnHealthy}) |
| 38 | // The coordinator answered last on the main runner's claude-1; a card |
| 39 | // connecting on the Mac seals to the Mac's own key. |
| 40 | snap.Turns = append(snap.Turns, wire.Turn{ID: "t3", State: wire.TurnReplied, Connection: "claude-1", UpdatedAt: screenshotNow.Add(-time.Minute)}) |
| 41 | macKey := base64.StdEncoding.EncodeToString(bytes.Repeat([]byte{9}, 32)) |
| 42 | snap.Runner.SealKey = base64.StdEncoding.EncodeToString(bytes.Repeat([]byte{8}, 32)) |
| 43 | snap.Runners = []wire.Runner{snap.Runner, {ID: "w-studio", Role: wire.RoleWorker, SealKey: macKey}} |
| 44 | snap.Connections = append(snap.Connections, wire.Connection{ID: "c-mac-2", RunnerID: "w-studio", Provider: wire.ProviderClaude, Kind: wire.KindToken, State: wire.ConnConnecting, Prompt: &wire.ConnectPrompt{Kind: wire.PromptToken, Code: "claude setup-token"}}) |
| 45 | machines := []chat.ConnectionMachine{ |
| 46 | {ID: "w-old", Label: "Old Mac", Role: wire.RoleWorker, Platform: "darwin", Protocol: wire.ProtocolChangeDiffs, Online: true, SealKey: key}, |
| 47 | {ID: "r1", Label: "docs-t1", Role: wire.RolePrimary, Platform: "linux", Protocol: wire.ProtocolMacEngineering, Online: true, SealKey: key}, |
| 48 | {ID: "w-studio", Label: "Tito Studio", Role: wire.RoleWorker, Platform: "darwin", Protocol: wire.ProtocolMacEngineering, Online: true, SealKey: key}, |
| 49 | {ID: "w-taint", Label: "Shared Mac", Role: wire.RoleWorker, Platform: "darwin", Protocol: wire.ProtocolMacEngineering, Online: true, SealKey: key, Tainted: true}, |
| 50 | {ID: "w-new", Label: "Paul's Mac mini", Role: wire.RoleWorker, Platform: "darwin", Protocol: wire.ProtocolMacEngineering, Online: true, SealKey: key}, |
| 51 | } |
| 52 | today := map[string]int64{chat.UsageKey("r1", "", "claude-1"): 840, chat.UsageKey("r1", "w-studio", "claude-1"): 12_400} |
| 53 | v := connectionsPage(snap, today, wire.Preferences{}, "docs-t1", nil, screenshotNow) |
| 54 | groupByMachine(&v, machines, snap.Runner.ID) |
| 55 | var names []string |
| 56 | for _, m := range v.Machines { |
| 57 | names = append(names, m.Name) |
| 58 | } |
| 59 | if strings.Join(names, "|") != "docs-t1|Tito Studio|Paul's Mac mini" { |
| 60 | t.Fatalf("groups %v", names) |
| 61 | } |
| 62 | main, studio, mini := v.Machines[0], v.Machines[1], v.Machines[2] |
| 63 | if !main.Primary || len(main.Rows) != 3 || main.AddClaude != "" { |
| 64 | t.Fatalf("the main runner's group %+v", main) |
| 65 | } |
| 66 | if len(studio.Rows) != 1 || studio.Rows[0].ID != "c-mac" || studio.AddClaude != "Add Claude to Tito Studio" || studio.ID != "w-studio" { |
| 67 | t.Fatalf("the Mac's group %+v", studio) |
| 68 | } |
| 69 | if len(mini.Rows) != 0 || mini.AddClaude != "Add Claude to Paul's Mac mini" { |
| 70 | t.Fatalf("a Mac with nothing yet %+v", mini) |
| 71 | } |
| 72 | if rowOf(t, v, "c-claude").Usage != "840" || rowOf(t, v, "c-mac").Usage != "12k" { |
| 73 | t.Fatalf("usage: main %q, Mac %q", rowOf(t, v, "c-claude").Usage, rowOf(t, v, "c-mac").Usage) |
| 74 | } |
| 75 | if len(v.Attempts) != 1 || v.Attempts[0].SealKey != macKey || v.Attempts[0].Machine != "w-studio" { |
| 76 | t.Fatalf("the Mac's card %+v", v.Attempts) |
| 77 | } |
| 78 | if !slices.ContainsFunc(rowOf(t, v, "c-claude").Roles, func(r connRole) bool { return r.Name == copyText("conversation.coordinator") }) { |
| 79 | t.Fatalf("the main runner's claude-1 is not the coordinator's: %+v", rowOf(t, v, "c-claude").Roles) |
| 80 | } |
| 81 | for _, r := range rowOf(t, v, "c-mac").Roles { |
| 82 | if r.Name == copyText("conversation.coordinator") { |
| 83 | t.Fatal("a Mac's connection shown as the coordinator's") |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | html := connectionsHTML(t, v) |
| 88 | group := func(id string) string { |
| 89 | t.Helper() |
| 90 | m := regexp.MustCompile(`(?s)<section equipo-conn-group data-machine="` + id + `".*?</section>`).FindString(html) |
| 91 | if m == "" { |
| 92 | t.Fatalf("no group %s:\n%s", id, html) |
| 93 | } |
| 94 | return m |
| 95 | } |
| 96 | if !strings.Contains(group("r1"), "<h2>docs-t1 ") || strings.Contains(group("r1"), "runner_id") { |
| 97 | t.Fatalf("the main runner's group:\n%s", group("r1")) |
| 98 | } |
| 99 | studioHTML := group("w-studio") |
| 100 | for _, want := range []string{"<h2>Tito Studio ", `data-connection="c-mac"`, `<input type="hidden" name="runner_id" value="w-studio">`, `<input type="hidden" name="provider" value="claude">`, ">Add Claude to Tito Studio<", `id="machine-w-studio"`} { |
| 101 | if !strings.Contains(studioHTML, want) { |
| 102 | t.Fatalf("the Mac's group lacks %q:\n%s", want, studioHTML) |
| 103 | } |
| 104 | } |
| 105 | if strings.Index(html, `data-machine="r1"`) > strings.Index(html, `data-machine="w-studio"`) { |
| 106 | t.Fatal("the main runner is not first") |
| 107 | } |
| 108 | for _, id := range []string{"w-old", "w-taint"} { |
| 109 | if strings.Contains(html, `value="`+id+`"`) || strings.Contains(html, `data-machine="`+id+`"`) { |
| 110 | t.Fatalf("%s offers Claude:\n%s", id, html) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // One machine, as before: no machine heading. |
| 115 | plain := connectionsPage(screenshotSnapshot(), nil, wire.Preferences{}, "docs-t1", nil, screenshotNow) |
| 116 | groupByMachine(&plain, machines[1:2], "r1") |
| 117 | if strings.Contains(connectionsHTML(t, plain), "data-machine=") { |
| 118 | t.Fatal("a page with only the main runner grouped by machine") |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // TestTakesClaude: the one rule for offering Claude on a machine. |
| 123 | func TestTakesClaude(t *testing.T) { |
| 124 | key := base64.StdEncoding.EncodeToString(bytes.Repeat([]byte{7}, 32)) |
| 125 | ok := chat.ConnectionMachine{ID: "w", Role: wire.RoleWorker, Platform: "darwin", Protocol: wire.ProtocolMacEngineering, Online: true, SealKey: key} |
| 126 | if !chat.TakesClaude(ok) { |
| 127 | t.Fatal("a Mac at 37 does not take Claude") |
| 128 | } |
| 129 | for name, change := range map[string]func(*chat.ConnectionMachine){ |
| 130 | "below 37": func(m *chat.ConnectionMachine) { m.Protocol = wire.ProtocolChangeDiffs }, |
| 131 | "no key": func(m *chat.ConnectionMachine) { m.SealKey = "" }, |
| 132 | "tainted": func(m *chat.ConnectionMachine) { m.Tainted = true }, |
| 133 | "removing": func(m *chat.ConnectionMachine) { m.Removing = true }, |
| 134 | "the main": func(m *chat.ConnectionMachine) { m.Role = wire.RolePrimary }, |
| 135 | "not a Mac": func(m *chat.ConnectionMachine) { m.Platform = "linux" }, |
| 136 | } { |
| 137 | m := ok |
| 138 | change(&m) |
| 139 | if chat.TakesClaude(m) { |
| 140 | t.Fatalf("%s takes Claude", name) |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // TestMacConnectionNeedsLinksToAdd: Needs you for a Mac waiting for a |
| 146 | // Claude connection links straight to adding Claude to that Mac. |
| 147 | func TestMacConnectionNeedsLinksToAdd(t *testing.T) { |
| 148 | task := wire.Task{ID: "t1", NeedsYou: wire.NeedsMacConnection, Steps: []wire.Step{{ID: "s1", Specialist: wire.SpecialistEngineering, WaitingFor: "w-studio", RunnerLabel: "Tito Studio", Placement: wire.PlacementMacConnection, EngineeringStatus: &wire.EngineeringStatus{State: wire.EngineeringNoConnection}}}} |
| 149 | d := decisionOf(task, taskLine{}) |
| 150 | if d.AddClaude != "/conversation/settings/connections#machine-w-studio" || d.AddClaudeLabel != "Add Claude to Tito Studio" { |
| 151 | t.Fatalf("decision %+v", d) |
| 152 | } |
| 153 | task.Steps[0].EngineeringStatus = &wire.EngineeringStatus{State: wire.EngineeringHomeOpen} |
| 154 | if d := decisionOf(task, taskLine{}); d.AddClaude != "" { |
| 155 | t.Fatalf("a Mac whose home is open offered Claude: %+v", d) |
| 156 | } |
| 157 | } |
| 158 | |