| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | // labelCopy is the instance's copy for the label's strings and the |
| 13 | // sentences that carry it; other keys read as themselves. |
| 14 | func labelCopy(k string) string { |
| 15 | if s, ok := map[string]string{ |
| 16 | "chat.connections.named": "%s (%s)", |
| 17 | "chat.connections.numbered": "%s, %d", |
| 18 | "chat.connections.signed_in_by_link": "signed in by link", |
| 19 | "chat.connections.you_said": "(added by you)", |
| 20 | "advice.budget_near.day": "%s: %s of today’s %s budget used.", |
| 21 | "advice.budget_near.month": "%s: %s of this month’s %s budget used.", |
| 22 | "chat.held.wait.reset": "I’d wait: %s resets %s.", |
| 23 | "chat.held.run.no_reset": "I’d run it now: %s has no reported reset in the next three days.", |
| 24 | }[k]; ok { |
| 25 | return s |
| 26 | } |
| 27 | return k |
| 28 | } |
| 29 | |
| 30 | // One label per connection, in order: the provider's email, the email |
| 31 | // the person typed with "(added by you)", "signed in by link" for a |
| 32 | // token with neither (with the add-email link), and a number for the |
| 33 | // later of two that would read alike, in the order they were made; the |
| 34 | // first keeps the plain form, and an attempt still connecting counts |
| 35 | // for nothing. |
| 36 | func TestConnectionLabels(t *testing.T) { |
| 37 | at := time.Date(2026, 11, 12, 9, 0, 0, 0, time.UTC) |
| 38 | conns := []wire.Connection{ |
| 39 | // Listed newest first: numbering follows when each was made. |
| 40 | {ID: "cn_tok3", LocalID: "claude-4", Provider: wire.ProviderClaude, Kind: wire.KindToken, State: wire.ConnHealthy, CreatedAt: at.Add(3 * time.Hour)}, |
| 41 | {ID: "cn_tok2", LocalID: "claude-3", Provider: wire.ProviderClaude, Kind: wire.KindToken, State: wire.ConnHealthy, CreatedAt: at.Add(2 * time.Hour)}, |
| 42 | {ID: "cn_said", LocalID: "claude-2", Provider: wire.ProviderClaude, Kind: wire.KindToken, State: wire.ConnHealthy, CreatedAt: at.Add(time.Hour)}, |
| 43 | {ID: "cn_mail", LocalID: "claude-1", Provider: wire.ProviderClaude, Kind: wire.KindSubscription, Label: "paul@tito.io", State: wire.ConnHealthy, CreatedAt: at}, |
| 44 | {ID: "cn_cx1", LocalID: "codex-1", Provider: wire.ProviderCodex, Kind: wire.KindSubscription, State: wire.ConnHealthy, CreatedAt: at}, |
| 45 | {ID: "cn_cx2", LocalID: "codex-2", Provider: wire.ProviderCodex, Kind: wire.KindSubscription, State: wire.ConnSignedOut, CreatedAt: at.Add(time.Hour)}, |
| 46 | {ID: "cn_try", Provider: wire.ProviderCodex, State: wire.ConnConnecting, CreatedAt: at.Add(-time.Hour)}, |
| 47 | {ID: "cn_tx", LocalID: "tensorx-1", Provider: wire.ProviderTensorX, Kind: wire.KindAPIKey, State: wire.ConnHealthy, CreatedAt: at}, |
| 48 | } |
| 49 | labels := ConnectionLabels(conns, map[string]string{"cn_said": "me@tito.io", "cn_mail": "ignored@tito.io"}, labelCopy) |
| 50 | for id, want := range map[string]string{ |
| 51 | "cn_mail": "Claude (paul@tito.io)", |
| 52 | "cn_said": "Claude (me@tito.io) (added by you)", |
| 53 | "cn_tok2": "Claude (signed in by link)", |
| 54 | "cn_tok3": "Claude (signed in by link, 2)", |
| 55 | "cn_cx1": "Codex", |
| 56 | "cn_cx2": "Codex (2)", |
| 57 | "cn_tx": "TensorX", |
| 58 | } { |
| 59 | if got := labels[id].Text; got != want { |
| 60 | t.Fatalf("%s: %q, want %q", id, got, want) |
| 61 | } |
| 62 | } |
| 63 | if l := labels["cn_said"]; !l.Stated || l.Named != "Claude (me@tito.io)" || l.Account != "me@tito.io" || l.AddEmail { |
| 64 | t.Fatalf("a typed email: %+v", l) |
| 65 | } |
| 66 | if l := labels["cn_tok2"]; !l.AddEmail || l.ID != "cn_tok2" || l.Provider != wire.ProviderClaude || l.Name != "Claude" { |
| 67 | t.Fatalf("a token with no email offers to add one: %+v", l) |
| 68 | } |
| 69 | if labels["cn_mail"].AddEmail || labels["cn_cx2"].AddEmail { |
| 70 | t.Fatal("only a token with no email offers to add one") |
| 71 | } |
| 72 | seen := map[string]string{} |
| 73 | for _, c := range conns { |
| 74 | if c.State == wire.ConnConnecting { |
| 75 | continue |
| 76 | } |
| 77 | if other, ok := seen[labels[c.ID].Text]; ok { |
| 78 | t.Fatalf("%s and %s read alike: %q", c.ID, other, labels[c.ID].Text) |
| 79 | } |
| 80 | seen[labels[c.ID].Text] = c.ID |
| 81 | } |
| 82 | if got := LabelOf(labels, wire.Connection{ID: "cn_other", Provider: wire.ProviderCodex}); got.Text != "Codex" || got.Provider != wire.ProviderCodex { |
| 83 | t.Fatalf("not the person's: %+v", got) |
| 84 | } |
| 85 | if got := HoldLabel(conns, labels, "", "claude-4", wire.ProviderClaude); got.Text != "Claude (signed in by link, 2)" { |
| 86 | t.Fatalf("hold label %+v", got) |
| 87 | } |
| 88 | if got := HoldLabel(conns, labels, "", "claude-4", wire.ProviderCodex); got.Text != "Codex" { |
| 89 | t.Fatalf("a hold on another provider's local id: %+v", got) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // Advice that names a connection, and the held card for a step on it, |
| 94 | // name it by its shared label: with two TensorX keys, the later is |
| 95 | // "TensorX (2)" in both. |
| 96 | func TestAdviceAndHeldCardsUseTheSharedLabel(t *testing.T) { |
| 97 | r := newAdviceRig(t) |
| 98 | r.s.authority.Copy = labelCopy |
| 99 | first := connectionRecord{ID: "cn_first", Person: adviceOwner, RunnerID: "RUNNER1", LocalID: "tensorx-first", Provider: wire.ProviderTensorX, Kind: wire.KindAPIKey, State: wire.ConnHealthy, Billing: wire.BillingMetered, Revision: 1, CreatedAt: r.at.Add(-24 * time.Hour), UpdatedAt: r.at} |
| 100 | if err := r.s.db.G.Create(&first).Error; err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | r.refresh() |
| 104 | rows := r.rows() |
| 105 | if len(rows) != 1 || rows[0].Kind != wire.AdviceBudgetNear || !strings.HasPrefix(rows[0].Text, "TensorX (2): $0.84 of today’s $1 budget used.") { |
| 106 | t.Fatalf("advice %+v", rows) |
| 107 | } |
| 108 | if c := r.card(rows[0].CardID); c.Question != rows[0].Text { |
| 109 | t.Fatalf("advice card %q", c.Question) |
| 110 | } |
| 111 | resets := r.at.Add(3 * time.Hour) |
| 112 | tx := &txn{s: r.s, db: r.s.db.G} |
| 113 | hold := &wire.StepHold{Reason: wire.HoldHeadroom, Connection: "tensorx-abc", Provider: wire.ProviderTensorX, ResetsAt: &resets} |
| 114 | if q := tx.heldQuestion(taskRecord{Creator: adviceOwner}, "RUNNER1", hold, r.at); q != fmt.Sprintf("I’d wait: TensorX (2) resets %s.", resets.In(defaultLocation()).Format("Mon 15:04")) || strings.Contains(q, "UTC") { |
| 115 | t.Fatalf("held card %q", q) |
| 116 | } |
| 117 | hold.Connection = "tensorx-first" |
| 118 | if q := tx.heldQuestion(taskRecord{Creator: adviceOwner}, "", hold, r.at); !strings.Contains(q, ": TensorX resets") { |
| 119 | t.Fatalf("held card on the first %q", q) |
| 120 | } |
| 121 | } |
| 122 | |