package wire_test import ( "encoding/json" "errors" "strings" "testing" "amadan.net/oficina/equipo/internal/wire" "amadan.net/oficina/equipo/internal/wire/wiretest" ) func TestProtocolLookupIs36(t *testing.T) { // Lookups stay at 36; Mac Engineering is 37 and Design (38) is the // current protocol. if wire.ProtocolLookup != 36 || wire.ProtocolMacEngineering != 37 || wire.ProtocolCurrent != wire.ProtocolDesign || wire.Spoken(99) != 38 || wire.Spoken(36) != 36 { t.Fatalf("lookup %d current %d", wire.ProtocolLookup, wire.ProtocolCurrent) } } func TestLookupFramesRoundTrip(t *testing.T) { for _, name := range []string{"frames/lookup-step.json", "frames/lookup-result.json"} { f, body, err := wire.ParseFrame(wiretest.Fixture(name)) if err != nil { t.Fatalf("%s: %v", name, err) } switch v := body.(type) { case *wire.LookupStep: if v.Stage != wire.LookupStageAnswer || len(v.Pages) != 1 || v.Pack.City != "Dublin" { t.Errorf("step %+v", v) } case *wire.LookupResult: if err := wire.ValidAnswerResult(*v, 1); err != nil { t.Errorf("result %+v: %v", v, err) } default: t.Errorf("%s decoded as %T (%s)", name, body, f.Type) } } } // Neither lookup frame is acked or resent. func TestLookupFramesNotDurable(t *testing.T) { for _, kind := range []string{wire.FrameLookupStep, wire.FrameLookupResult} { if (wire.Frame{Type: kind}).Durable() { t.Errorf("%s is durable", kind) } } } func TestEncodeRefusesLookupBelow36(t *testing.T) { f, _, err := wire.ParseFrame(wiretest.Fixture("frames/lookup-step.json")) if err != nil { t.Fatal(err) } if _, err := wire.Encode(f, wire.Protocol35); !errors.Is(err, wire.ErrProtocol) { t.Errorf("at 35: %v", err) } if _, err := wire.Encode(f, wire.ProtocolLookup); err != nil { t.Errorf("at 36: %v", err) } } // Results are checked strictly: three queries, a 201-character query, an // answer with an extra field, an answer too long or a used index out of // range each fail. func TestLookupStepsStrictJSON(t *testing.T) { q := func(qs ...string) wire.LookupResult { return wire.LookupResult{Stage: wire.LookupStageQuery, Queries: qs} } if err := wire.ValidQueryResult(q("dublin weather", "dublin forecast tomorrow")); err != nil { t.Errorf("two queries: %v", err) } for name, r := range map[string]wire.LookupResult{ "three": q("a", "b", "c"), "long": q(strings.Repeat("x", 201)), "empty": q(""), "none": q(), "answer": {Queries: []string{"a"}, Answer: "no"}, "a code": {Queries: []string{"a"}, Code: "lookup_failed"}, } { if wire.ValidQueryResult(r) == nil { t.Errorf("query %s accepted", name) } } for name, r := range map[string]wire.LookupResult{ "too long": {Answer: strings.Repeat("é", 1201)}, "empty": {}, "used out": {Answer: "ok", Used: []int{3}}, "used twice": {Answer: "ok", Used: []int{0, 0}}, "queries": {Answer: "ok", Queries: []string{"a"}}, } { if wire.ValidAnswerResult(r, 2) == nil { t.Errorf("answer %s accepted", name) } } if err := wire.ValidAnswerResult(wire.LookupResult{Answer: strings.Repeat("é", 1200), Used: []int{1}}, 2); err != nil { t.Errorf("1200 characters: %v", err) } extra := []byte(`{"id":"x","type":"lookup_result","data":{"lookup":"l","attempt":1,"nonce":"n","stage":"answer","answer":"ok","extra":true}}`) if _, _, err := wire.ParseFrame(extra); err == nil { t.Error("an extra field was accepted") } var m wire.Message if err := json.Unmarshal([]byte(`{"id":"m","revision":1,"role":"assistant","text":"","turn_id":"t","created_at":"2026-11-28T10:00:00Z","switched":null,"lookups":[{"answer":"Rain.","sources":[{"url":"https://www.met.ie/","title":"Met"}]}]}`), &m); err != nil || len(m.Lookups) != 1 { t.Errorf("a message's lookups: %+v %v", m, err) } }