package thin import ( "context" "encoding/json" "strings" "testing" "amadan.net/oficina/equipo/internal/wire" ) // TestResearchRefusedBelow12: propose_specialist is answered here on a // connection below protocol 13, before sending and in place of an older // instance's refusal of a call queued at 13 (it does not know the tool); // protocol 13, an unknown protocol, another tool and a stored success are // never touched. func TestResearchRefusedBelow12(t *testing.T) { propose := wire.ToolCall{Name: wire.ToolProposeSpecialist, Args: json.RawMessage(`{"profile":"research-v1"}`)} other := wire.ToolCall{Name: wire.ToolAskPerson, Args: json.RawMessage(`{"question":"q"}`)} unknown := wire.ToolResult{IsError: true, Code: "unknown_tool", Text: "Unknown tool."} invalid := wire.ToolResult{IsError: true, Code: "invalid", Text: "bad input"} ok := wire.ToolResult{Code: "proposed", Text: "asked"} for name, c := range map[string]struct { call wire.ToolCall spoken int res *wire.ToolResult refused bool }{ "before sending at 11": {propose, wire.ProtocolPreviewHost, nil, true}, "before sending at 2": {propose, wire.Protocol2, nil, true}, "an older unknown tool at 11": {propose, wire.ProtocolPreviewHost, &unknown, true}, "an older refusal at 11": {propose, wire.ProtocolPreviewHost, &invalid, true}, "a stored success at 11": {propose, wire.ProtocolPreviewHost, &ok, false}, "at 13": {propose, wire.ProtocolResearch, nil, false}, "an answer at 13": {propose, wire.ProtocolResearch, &unknown, false}, "protocol unknown": {propose, 0, nil, false}, "another tool at 11": {other, wire.ProtocolPreviewHost, &unknown, false}, } { res, refused := researchRefused(c.call, c.spoken, c.res) if refused != c.refused || refused && (res.Code != "research_unsupported" || !res.IsError || res.Text != toolResearchOld) { t.Errorf("%s: %+v %t", name, res, refused) } } if toolResearchOld != "Equipo needs an update before it can research." { t.Fatalf("the update message: %q", toolResearchOld) } } // TestBridgeRefusesResearchOnAnOlderLink: the bridge never sends // propose_specialist on a link below 13; at 13 it goes to the instance. func TestBridgeRefusesResearchOnAnOlderLink(t *testing.T) { sent := 0 trip := func(context.Context, wire.ToolCall) wire.ToolResult { sent++ return wire.ToolResult{Code: "proposed", Text: "asked"} } call := wire.ToolCall{CallID: "t/1/u", TurnID: "t", Attempt: 1, Name: wire.ToolProposeSpecialist, Args: json.RawMessage(`{}`)} old := &bridge{speaks: wire.ProtocolPreviewHost, trip: trip} if res := old.roundTrip(context.Background(), call); !res.IsError || res.Text != toolResearchOld || sent != 0 { t.Fatalf("at 11: %+v, %d sent", res, sent) } current := &bridge{speaks: wire.ProtocolResearch, trip: trip} if res := current.roundTrip(context.Background(), call); res.IsError || sent != 1 { t.Fatalf("at 13: %+v, %d sent", res, sent) } } // TestResearchToolRefusedBelow12 (slice R2): the research tool is one of // wire.ResearchTools, so an older link answers it here and never sends // it; at 13 it is offered to the model and goes to the instance. func TestResearchToolRefusedBelow12(t *testing.T) { call := wire.ToolCall{CallID: "t/1/r", TurnID: "t", Attempt: 1, Name: wire.ToolResearch, Args: json.RawMessage(`{"task":"tk1","question":"q","urls":[]}`)} sent := 0 trip := func(context.Context, wire.ToolCall) wire.ToolResult { sent++ return wire.ToolResult{Code: "requested", Text: "ok"} } old := &bridge{speaks: wire.ProtocolPreviewHost, trip: trip} if res := old.roundTrip(context.Background(), call); !res.IsError || res.Code != "research_unsupported" || res.Text != toolResearchOld || sent != 0 { t.Fatalf("at 11: %+v, %d sent", res, sent) } current := &bridge{speaks: wire.ProtocolResearch, trip: trip} if res := current.roundTrip(context.Background(), call); res.IsError || sent != 1 { t.Fatalf("at 13: %+v, %d sent", res, sent) } var offered map[string]any for _, tool := range converseTools { if m := tool.(map[string]any); m["name"] == wire.ToolResearch { offered = m } } if offered == nil { t.Fatal("research is not offered") } d := offered["description"].(string) for _, want := range []string{"names an outside site", "starts it only at the task's start (call it before start_task) or after a review round", "never retries"} { if !strings.Contains(d, want) { t.Fatalf("the description lacks %q: %s", want, d) } } }