| 1 | package wire_test |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "slices" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | ) |
| 12 | |
| 13 | // jsonKeys is v marshalled: its keys, sorted, and the object. |
| 14 | func jsonKeys(t *testing.T, v any) ([]string, map[string]json.RawMessage) { |
| 15 | t.Helper() |
| 16 | raw, err := json.Marshal(v) |
| 17 | if err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | var got map[string]json.RawMessage |
| 21 | if err := json.Unmarshal(raw, &got); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | keys := make([]string, 0, len(got)) |
| 25 | for k := range got { |
| 26 | keys = append(keys, k) |
| 27 | } |
| 28 | slices.Sort(keys) |
| 29 | return keys, got |
| 30 | } |
| 31 | |
| 32 | // An Assistant card is the shape Base reads: exactly these keys, and the |
| 33 | // two lists as [] (the store always fills them), never null. A job card's |
| 34 | // fields (job, detail, started_at) and a link's rel each add exactly |
| 35 | // their key when set, so a Base that predates them reads the same eight. |
| 36 | func TestAssistantCardJSON(t *testing.T) { |
| 37 | zero := wire.AssistantCard{Options: []wire.CardOption{}, Links: []wire.CardLink{}} |
| 38 | keys, got := jsonKeys(t, zero) |
| 39 | want := []string{"id", "kind", "links", "options", "question", "result", "revision", "state"} |
| 40 | if !slices.Equal(keys, want) { |
| 41 | t.Fatalf("keys %v, want %v", keys, want) |
| 42 | } |
| 43 | if string(got["options"]) != "[]" || string(got["links"]) != "[]" { |
| 44 | t.Fatalf("options %s, links %s", got["options"], got["links"]) |
| 45 | } |
| 46 | started := time.Date(2026, 11, 13, 9, 30, 0, 0, time.UTC) |
| 47 | for key, card := range map[string]wire.AssistantCard{ |
| 48 | "job": {Options: zero.Options, Links: zero.Links, Job: "c_job"}, |
| 49 | "detail": {Options: zero.Options, Links: zero.Links, Detail: "One sentence more."}, |
| 50 | "started_at": {Options: zero.Options, Links: zero.Links, StartedAt: &started}, |
| 51 | } { |
| 52 | keys, got := jsonKeys(t, card) |
| 53 | if !slices.Equal(keys, slices.Sorted(slices.Values(append(slices.Clone(want), key)))) { |
| 54 | t.Fatalf("with %s set: keys %v", key, keys) |
| 55 | } |
| 56 | if key == "started_at" && string(got[key]) != `"2026-11-13T09:30:00Z"` { |
| 57 | t.Fatalf("started_at %s", got[key]) |
| 58 | } |
| 59 | } |
| 60 | if keys, got := jsonKeys(t, wire.CardLink{Label: "Open", URL: "https://e.example/jobs/h_1", Rel: wire.LinkEquipo}); !slices.Equal(keys, []string{"label", "rel", "url"}) || string(got["rel"]) != `"equipo"` { |
| 61 | t.Fatalf("a link with rel: %v %s", keys, got["rel"]) |
| 62 | } |
| 63 | if keys, _ := jsonKeys(t, wire.CardLink{Label: "Open", URL: "https://e.example/"}); !slices.Equal(keys, []string{"label", "url"}) { |
| 64 | t.Fatalf("a link without rel: %v", keys) |
| 65 | } |
| 66 | if wire.CardConfirm != "confirm" || wire.CardOutcome != "outcome" || wire.CardConnect != "connect" || wire.CardResolved != "resolved" || wire.CardJob != "job" { |
| 67 | t.Fatal("card kinds and states") |
| 68 | } |
| 69 | if wire.JobWorking != "working" || wire.JobAsking != "asking" || wire.JobReady != "ready" || wire.JobDone != "done" || wire.JobStopped != "stopped" || wire.LinkEquipo != "equipo" { |
| 70 | t.Fatal("job states and link rel") |
| 71 | } |
| 72 | if wire.LaneCoordinator != "" || wire.LaneAssistant != "assistant" || wire.RoleAssistant != "assistant" || wire.TurnTeamUpdate != "team_update" { |
| 73 | t.Fatal("lane, role and turn kind") |
| 74 | } |
| 75 | if !slices.Equal(wire.AssistTools, []string{"look", "describe", "use", "ask_person", "write_note", "read_notes", "read_attachment"}) || |
| 76 | !slices.Equal(wire.TeamUpdateTools, []string{"look", "describe", "use", "read_notes"}) { |
| 77 | t.Fatalf("assist %v, team update %v", wire.AssistTools, wire.TeamUpdateTools) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // A runner that speaks 24 or more is spoken to at 24, the warm |
| 82 | // protocol; glances came at 23 and jobs at 22. |
| 83 | func TestSpokenIs24(t *testing.T) { |
| 84 | if wire.Spoken(99) != 38 || wire.ProtocolCurrent != wire.ProtocolDesign || wire.ProtocolWarm != wire.Protocol24 || wire.ProtocolGlance != wire.Protocol23 || wire.ProtocolJobs != wire.Protocol22 || wire.ProtocolCopy != wire.Protocol21 || wire.ProtocolUsageRefresh != wire.Protocol20 || wire.ProtocolAssistant != wire.Protocol19 { |
| 85 | t.Fatalf("spoken %d, current %d", wire.Spoken(99), wire.ProtocolCurrent) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // A job lane is "job:" and a hand-off id: h_ and 1 to 64 of [a-z0-9] |
| 90 | // (J2). Job lanes are Coordinator turns; the Assistant's lane is valid |
| 91 | // and is not (J3). |
| 92 | func TestJobLane(t *testing.T) { |
| 93 | if wire.LaneJobPrefix != "job:" || wire.JobLane("h_abc") != "job:h_abc" { |
| 94 | t.Fatalf("prefix %q, lane %q", wire.LaneJobPrefix, wire.JobLane("h_abc")) |
| 95 | } |
| 96 | longest := "h_" + strings.Repeat("a", 64) |
| 97 | for _, c := range []struct{ lane, handoff string }{{"job:h_abc", "h_abc"}, {"job:h_a", "h_a"}, {"job:h_0z9", "h_0z9"}, {"job:" + longest, longest}} { |
| 98 | if h, ok := wire.JobOf(c.lane); !ok || h != c.handoff { |
| 99 | t.Fatalf("JobOf(%q) = %q, %v", c.lane, h, ok) |
| 100 | } |
| 101 | if !wire.ValidLane(c.lane) || !wire.CoordinatorRole(c.lane) { |
| 102 | t.Fatalf("%q: valid %v, coordinator role %v", c.lane, wire.ValidLane(c.lane), wire.CoordinatorRole(c.lane)) |
| 103 | } |
| 104 | } |
| 105 | for _, lane := range []string{"", "assistant", "job:", "job:../x", "job:H_ABC", "job:h_x/y", "job:h_", "job:h_" + strings.Repeat("a", 65), "job:x_abc", "job:h_abc ", "jobs:h_abc", "Job:h_abc", "job:h_a\nb", "h_abc"} { |
| 106 | if h, ok := wire.JobOf(lane); ok || h != "" { |
| 107 | t.Fatalf("JobOf(%q) = %q, %v", lane, h, ok) |
| 108 | } |
| 109 | } |
| 110 | for _, c := range []struct { |
| 111 | lane string |
| 112 | valid, coordinator bool |
| 113 | }{ |
| 114 | {"", true, true}, |
| 115 | {"assistant", true, false}, |
| 116 | {"job:h_abc", true, true}, |
| 117 | {"job:", false, false}, |
| 118 | {"job:../x", false, false}, |
| 119 | {"job:H_ABC", false, false}, |
| 120 | {"job:h_x/y", false, false}, |
| 121 | {"job:h_" + strings.Repeat("a", 65), false, false}, |
| 122 | {"Assistant", false, false}, |
| 123 | {"coordinator", false, false}, |
| 124 | } { |
| 125 | if wire.ValidLane(c.lane) != c.valid || wire.CoordinatorRole(c.lane) != c.coordinator { |
| 126 | t.Fatalf("%q: valid %v, coordinator role %v", c.lane, wire.ValidLane(c.lane), wire.CoordinatorRole(c.lane)) |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |