| 1 | package wire_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "slices" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // researchDispatch is a Research step's dispatch as the instance sends |
| 15 | // it at protocol 13: no repository, branch or SHA. |
| 16 | func researchDispatch() wire.DispatchCommand { |
| 17 | return wire.DispatchCommand{Person: "owner@example.com", TaskID: "tk1", StepID: "s_1", Attempt: 1, Specialist: wire.SpecialistResearch, Title: "Shop mockups", Brief: "Match the shop.", Findings: []wire.Finding{}, RunnerID: "r1", |
| 18 | Research: &wire.ResearchDispatch{Question: "Colours and logo?", PersonWords: []string{"See https://shop.example.com/"}, Sources: []wire.ResearchSource{{ID: "s1", URL: "https://shop.example.com/", Origin: wire.ResearchOriginPerson}}, Inventory: []string{}, SpecialistBrief: "Look for colours and logo.", SpecialistRevision: 2}} |
| 19 | } |
| 20 | |
| 21 | // TestEncodeBelowProtocol13RefusesResearchDispatch (slice R2): a Research |
| 22 | // dispatch is never sent below 13 (ErrProtocol, like a preview below 9); |
| 23 | // the research tool is refused there too; any other dispatch loses the |
| 24 | // research field and is otherwise as it was. The approved specialist's |
| 25 | // brief rides inside it, so it never reaches a runner below 13. |
| 26 | func TestEncodeBelowProtocol13RefusesResearchDispatch(t *testing.T) { |
| 27 | f, _ := wire.NewFrame("d", wire.FrameDispatch, researchDispatch()) |
| 28 | if same, err := wire.Encode(f, wire.ProtocolResearch); err != nil || !bytes.Equal(same.Data, f.Data) { |
| 29 | t.Fatalf("changed at 13: %v", err) |
| 30 | } |
| 31 | if !strings.Contains(string(f.Data), `"research":{"question":"Colours and logo?","person_words":["See https://shop.example.com/"],"sources":[{"id":"s1","url":"https://shop.example.com/","origin":"person"}],"inventory":[],"specialist_brief":"Look for colours and logo.","specialist_revision":2}`) { |
| 32 | t.Fatalf("the research dispatch on the wire: %s", f.Data) |
| 33 | } |
| 34 | for _, p := range []int{wire.ProtocolMetered, wire.ProtocolPreviewHost, wire.ProtocolExplore, wire.ProtocolPreview, wire.Protocol2} { |
| 35 | if _, err := wire.Encode(f, p); !errors.Is(err, wire.ErrProtocol) { |
| 36 | t.Fatalf("a research dispatch at %d: %v", p, err) |
| 37 | } |
| 38 | } |
| 39 | // The specialist alone makes it a research dispatch. |
| 40 | bare := researchDispatch() |
| 41 | bare.Research = nil |
| 42 | g, _ := wire.NewFrame("b", wire.FrameDispatch, bare) |
| 43 | if _, err := wire.Encode(g, wire.ProtocolPreviewHost); !errors.Is(err, wire.ErrProtocol) { |
| 44 | t.Fatalf("a research step without its field at 11: %v", err) |
| 45 | } |
| 46 | // Another dispatch carrying the field loses it below 13 and nothing else. |
| 47 | eng := wire.DispatchCommand{Person: "owner@example.com", TaskID: "tk1", StepID: "s_2", Attempt: 1, Specialist: wire.SpecialistEngineering, Repo: "amadan.net/oficina/sample", Branch: "equipo/tk1-x", Findings: []wire.Finding{}} |
| 48 | plain, _ := wire.NewFrame("e", wire.FrameDispatch, eng) |
| 49 | eng.Research = researchDispatch().Research |
| 50 | stray, _ := wire.NewFrame("e", wire.FrameDispatch, eng) |
| 51 | got, err := wire.Encode(stray, wire.ProtocolPreviewHost) |
| 52 | if err != nil || strings.Contains(string(got.Data), `"research"`) { |
| 53 | t.Fatalf("a stray research field at 11: %s %v", got.Data, err) |
| 54 | } |
| 55 | if !bytes.Equal(got.Data, plain.Data) { |
| 56 | t.Fatalf("more than the field changed:\n%s\n%s", got.Data, plain.Data) |
| 57 | } |
| 58 | // An engineering dispatch without it is unchanged at 11. |
| 59 | if same, err := wire.Encode(plain, wire.ProtocolPreviewHost); err != nil || !bytes.Equal(same.Data, plain.Data) { |
| 60 | t.Fatalf("an engineering dispatch changed at 11: %v", err) |
| 61 | } |
| 62 | // The research tool is refused below 13 like propose_specialist. |
| 63 | call, _ := wire.NewFrame("c", wire.FrameToolCall, wire.ToolCall{CallID: "c", TurnID: "t", Attempt: 1, Name: wire.ToolResearch, Args: json.RawMessage(`{"task":"tk1","question":"q","urls":[]}`)}) |
| 64 | if _, err := wire.Encode(call, wire.ProtocolPreviewHost); !errors.Is(err, wire.ErrProtocol) { |
| 65 | t.Fatalf("the research tool at 11: %v", err) |
| 66 | } |
| 67 | if same, err := wire.Encode(call, wire.ProtocolResearch); err != nil || !bytes.Equal(same.Data, call.Data) { |
| 68 | t.Fatalf("the research tool changed at 13: %v", err) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // TestResearchToolIsACoordinatorWrite: research is one of the |
| 73 | // coordinator's write tools, refused on an older link, in the mutation |
| 74 | // ledger, and never offered to a move-in turn. |
| 75 | func TestResearchToolIsACoordinatorWrite(t *testing.T) { |
| 76 | if !slices.Contains(wire.ResearchTools, wire.ToolResearch) || !slices.Contains(wire.ConverseTools, wire.ToolResearch) || !slices.Contains(wire.WriteTools, wire.ToolResearch) || wire.MutationKinds[wire.ToolResearch] == "" { |
| 77 | t.Fatal("research is not a coordinator write tool") |
| 78 | } |
| 79 | if slices.Contains(wire.MoveInTools, wire.ToolResearch) { |
| 80 | t.Fatal("a move-in turn may request research") |
| 81 | } |
| 82 | if wire.SpecialistResearch != "research" || wire.ToolResearch != "research" { |
| 83 | t.Fatal("names") |
| 84 | } |
| 85 | for _, r := range []string{wire.ReasonResearchNoSources, wire.ReasonResearchNoGrant, wire.ReasonResearchUnsupported} { |
| 86 | if !wire.StepReasons[r] { |
| 87 | t.Fatalf("%s is not a step reason", r) |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |