package wire_test import ( "encoding/json" "slices" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/wire" ) // jsonKeys is v marshalled: its keys, sorted, and the object. func jsonKeys(t *testing.T, v any) ([]string, map[string]json.RawMessage) { t.Helper() raw, err := json.Marshal(v) if err != nil { t.Fatal(err) } var got map[string]json.RawMessage if err := json.Unmarshal(raw, &got); err != nil { t.Fatal(err) } keys := make([]string, 0, len(got)) for k := range got { keys = append(keys, k) } slices.Sort(keys) return keys, got } // An Assistant card is the shape Base reads: exactly these keys, and the // two lists as [] (the store always fills them), never null. A job card's // fields (job, detail, started_at) and a link's rel each add exactly // their key when set, so a Base that predates them reads the same eight. func TestAssistantCardJSON(t *testing.T) { zero := wire.AssistantCard{Options: []wire.CardOption{}, Links: []wire.CardLink{}} keys, got := jsonKeys(t, zero) want := []string{"id", "kind", "links", "options", "question", "result", "revision", "state"} if !slices.Equal(keys, want) { t.Fatalf("keys %v, want %v", keys, want) } if string(got["options"]) != "[]" || string(got["links"]) != "[]" { t.Fatalf("options %s, links %s", got["options"], got["links"]) } started := time.Date(2026, 11, 13, 9, 30, 0, 0, time.UTC) for key, card := range map[string]wire.AssistantCard{ "job": {Options: zero.Options, Links: zero.Links, Job: "c_job"}, "detail": {Options: zero.Options, Links: zero.Links, Detail: "One sentence more."}, "started_at": {Options: zero.Options, Links: zero.Links, StartedAt: &started}, } { keys, got := jsonKeys(t, card) if !slices.Equal(keys, slices.Sorted(slices.Values(append(slices.Clone(want), key)))) { t.Fatalf("with %s set: keys %v", key, keys) } if key == "started_at" && string(got[key]) != `"2026-11-13T09:30:00Z"` { t.Fatalf("started_at %s", got[key]) } } 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"` { t.Fatalf("a link with rel: %v %s", keys, got["rel"]) } if keys, _ := jsonKeys(t, wire.CardLink{Label: "Open", URL: "https://e.example/"}); !slices.Equal(keys, []string{"label", "url"}) { t.Fatalf("a link without rel: %v", keys) } if wire.CardConfirm != "confirm" || wire.CardOutcome != "outcome" || wire.CardConnect != "connect" || wire.CardResolved != "resolved" || wire.CardJob != "job" { t.Fatal("card kinds and states") } if wire.JobWorking != "working" || wire.JobAsking != "asking" || wire.JobReady != "ready" || wire.JobDone != "done" || wire.JobStopped != "stopped" || wire.LinkEquipo != "equipo" { t.Fatal("job states and link rel") } if wire.LaneCoordinator != "" || wire.LaneAssistant != "assistant" || wire.RoleAssistant != "assistant" || wire.TurnTeamUpdate != "team_update" { t.Fatal("lane, role and turn kind") } if !slices.Equal(wire.AssistTools, []string{"look", "describe", "use", "ask_person", "write_note", "read_notes", "read_attachment"}) || !slices.Equal(wire.TeamUpdateTools, []string{"look", "describe", "use", "read_notes"}) { t.Fatalf("assist %v, team update %v", wire.AssistTools, wire.TeamUpdateTools) } } // A runner that speaks 24 or more is spoken to at 24, the warm // protocol; glances came at 23 and jobs at 22. func TestSpokenIs24(t *testing.T) { if wire.Spoken(99) != wire.ProtocolOperator || wire.ProtocolCurrent != wire.ProtocolOperator || wire.ProtocolWarm != wire.Protocol24 || wire.ProtocolGlance != wire.Protocol23 || wire.ProtocolJobs != wire.Protocol22 || wire.ProtocolCopy != wire.Protocol21 || wire.ProtocolUsageRefresh != wire.Protocol20 || wire.ProtocolAssistant != wire.Protocol19 { t.Fatalf("spoken %d, current %d", wire.Spoken(99), wire.ProtocolCurrent) } } // A job lane is "job:" and a hand-off id: h_ and 1 to 64 of [a-z0-9] // (J2). Job lanes are Coordinator turns; the Assistant's lane is valid // and is not (J3). func TestJobLane(t *testing.T) { if wire.LaneJobPrefix != "job:" || wire.JobLane("h_abc") != "job:h_abc" { t.Fatalf("prefix %q, lane %q", wire.LaneJobPrefix, wire.JobLane("h_abc")) } longest := "h_" + strings.Repeat("a", 64) 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}} { if h, ok := wire.JobOf(c.lane); !ok || h != c.handoff { t.Fatalf("JobOf(%q) = %q, %v", c.lane, h, ok) } if !wire.ValidLane(c.lane) || !wire.CoordinatorRole(c.lane) { t.Fatalf("%q: valid %v, coordinator role %v", c.lane, wire.ValidLane(c.lane), wire.CoordinatorRole(c.lane)) } } 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"} { if h, ok := wire.JobOf(lane); ok || h != "" { t.Fatalf("JobOf(%q) = %q, %v", lane, h, ok) } } for _, c := range []struct { lane string valid, coordinator bool }{ {"", true, true}, {"assistant", true, false}, {"job:h_abc", true, true}, {"job:", false, false}, {"job:../x", false, false}, {"job:H_ABC", false, false}, {"job:h_x/y", false, false}, {"job:h_" + strings.Repeat("a", 65), false, false}, {"Assistant", false, false}, {"coordinator", false, false}, } { if wire.ValidLane(c.lane) != c.valid || wire.CoordinatorRole(c.lane) != c.coordinator { t.Fatalf("%q: valid %v, coordinator role %v", c.lane, wire.ValidLane(c.lane), wire.CoordinatorRole(c.lane)) } } }