| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/wire" |
| 9 | ) |
| 10 | |
| 11 | // TestProjectProposalRefusedBelow36: a propose_specialist call for one |
| 12 | // project is answered here on a link below protocol 38, where Encode |
| 13 | // would refuse it (an older instance has no scope); at 38, on an unknown |
| 14 | // protocol, for the team ("" or no project) and for another tool it is |
| 15 | // not touched. |
| 16 | func TestProjectProposalRefusedBelow36(t *testing.T) { |
| 17 | call := func(args string) wire.ToolCall { |
| 18 | return wire.ToolCall{Name: wire.ToolProposeSpecialist, Args: json.RawMessage(args)} |
| 19 | } |
| 20 | scoped := call(`{"profile":"design-v1","project":"p_xyz567"}`) |
| 21 | for name, c := range map[string]struct { |
| 22 | call wire.ToolCall |
| 23 | spoken int |
| 24 | refused bool |
| 25 | }{ |
| 26 | "one project at 37": {scoped, wire.ProtocolMacEngineering, true}, |
| 27 | "one project at 13": {scoped, wire.ProtocolResearch, true}, |
| 28 | "a null project at 37": {call(`{"project":null}`), wire.ProtocolMacEngineering, true}, |
| 29 | "one project at 38": {scoped, wire.ProtocolDesign, false}, |
| 30 | "protocol unknown": {scoped, 0, false}, |
| 31 | "the team at 37": {call(`{"profile":"research-v1","project":""}`), wire.ProtocolMacEngineering, false}, |
| 32 | "no project at 37": {call(`{"profile":"research-v1"}`), wire.ProtocolMacEngineering, false}, |
| 33 | "another tool at 37": {wire.ToolCall{Name: wire.ToolAskPerson, Args: json.RawMessage(`{"project":"p_1"}`)}, wire.ProtocolMacEngineering, false}, |
| 34 | } { |
| 35 | res, refused := designRefused(c.call, c.spoken) |
| 36 | if refused != c.refused || refused && (res.Code != "project_unsupported" || !res.IsError || res.Text != toolProjectOld) { |
| 37 | t.Errorf("%s: %+v %t", name, res, refused) |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // TestBridgeRefusesProjectProposalOnAnOlderLink: the bridge never sends a |
| 43 | // proposal for one project on a link below 38; at 38 it goes to the |
| 44 | // instance. |
| 45 | func TestBridgeRefusesProjectProposalOnAnOlderLink(t *testing.T) { |
| 46 | sent := 0 |
| 47 | trip := func(context.Context, wire.ToolCall) wire.ToolResult { |
| 48 | sent++ |
| 49 | return wire.ToolResult{Code: "proposed", Text: "asked"} |
| 50 | } |
| 51 | call := wire.ToolCall{CallID: "t/1/u", TurnID: "t", Attempt: 1, Name: wire.ToolProposeSpecialist, Args: json.RawMessage(`{"profile":"design-v1","project":"p_xyz567"}`)} |
| 52 | old := &bridge{speaks: wire.ProtocolMacEngineering, trip: trip} |
| 53 | if res := old.roundTrip(context.Background(), call); !res.IsError || res.Code != "project_unsupported" || sent != 0 { |
| 54 | t.Fatalf("at 37: %+v, %d sent", res, sent) |
| 55 | } |
| 56 | current := &bridge{speaks: wire.ProtocolDesign, trip: trip} |
| 57 | if res := current.roundTrip(context.Background(), call); res.IsError || sent != 1 { |
| 58 | t.Fatalf("at 38: %+v, %d sent", res, sent) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // TestProposeSpecialistOffersProject: the coordinator's propose_specialist |
| 63 | // takes an optional project, a string. |
| 64 | func TestProposeSpecialistOffersProject(t *testing.T) { |
| 65 | for _, tool := range converseTools { |
| 66 | m := tool.(map[string]any) |
| 67 | if m["name"] != wire.ToolProposeSpecialist { |
| 68 | continue |
| 69 | } |
| 70 | schema := m["inputSchema"].(map[string]any) |
| 71 | if p, ok := schema["properties"].(map[string]any)["project"]; !ok || p.(map[string]string)["type"] != "string" { |
| 72 | t.Fatalf("project: %v", schema["properties"]) |
| 73 | } |
| 74 | for _, r := range schema["required"].([]string) { |
| 75 | if r == "project" { |
| 76 | t.Fatal("project is required") |
| 77 | } |
| 78 | } |
| 79 | return |
| 80 | } |
| 81 | t.Fatal("no propose_specialist") |
| 82 | } |
| 83 | |
| 84 | // TestProjectProposalUnencodableCallIsRetired: a proposal for one project |
| 85 | // queued for a link that turns out to speak 37 is answered at once and |
| 86 | // leaves the outbox, so it never replays after an upgrade. |
| 87 | func TestProjectProposalUnencodableCallIsRetired(t *testing.T) { |
| 88 | ctx := context.Background() |
| 89 | s, err := OpenState(ctx, t.TempDir()) |
| 90 | if err != nil { |
| 91 | t.Fatal(err) |
| 92 | } |
| 93 | defer s.Close() |
| 94 | call := wire.ToolCall{CallID: "call", TurnID: "turn", Name: wire.ToolProposeSpecialist, Args: json.RawMessage(`{"profile":"design-v1","project":"p_xyz567"}`)} |
| 95 | if err := s.SendToolCall(ctx, call); err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | frames, _, err := s.Unacked(ctx, 0) |
| 99 | if err != nil || len(frames) != 1 { |
| 100 | t.Fatal(err, frames) |
| 101 | } |
| 102 | l := &Link{State: s, proto: wire.ProtocolMacEngineering} |
| 103 | wait := l.waitTool(call.CallID) |
| 104 | if err := l.send(ctx, nil, frames[0]); err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | select { |
| 108 | case res := <-wait: |
| 109 | if res.Code != "project_unsupported" { |
| 110 | t.Fatal(res) |
| 111 | } |
| 112 | default: |
| 113 | t.Fatal("no immediate unsupported result") |
| 114 | } |
| 115 | if frames, _, err = s.Unacked(ctx, 0); err != nil || len(frames) != 0 { |
| 116 | t.Fatal("an old call can replay after an upgrade", frames, err) |
| 117 | } |
| 118 | } |
| 119 | |