oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package wire_test
2
3import (
4 "encoding/json"
5 "errors"
6 "strings"
7 "testing"
8
9 "amadan.net/oficina/equipo/internal/wire"
10 "amadan.net/oficina/equipo/internal/wire/wiretest"
11)
12
13func TestProtocolLookupIs36(t *testing.T) {
14 // Lookups stay at 36; Mac Engineering is 37 and Design (38) is the
15 // current protocol.
16 if wire.ProtocolLookup != 36 || wire.ProtocolMacEngineering != 37 || wire.ProtocolCurrent != wire.ProtocolDesign || wire.Spoken(99) != 38 || wire.Spoken(36) != 36 {
17 t.Fatalf("lookup %d current %d", wire.ProtocolLookup, wire.ProtocolCurrent)
18 }
19}
20
21func TestLookupFramesRoundTrip(t *testing.T) {
22 for _, name := range []string{"frames/lookup-step.json", "frames/lookup-result.json"} {
23 f, body, err := wire.ParseFrame(wiretest.Fixture(name))
24 if err != nil {
25 t.Fatalf("%s: %v", name, err)
26 }
27 switch v := body.(type) {
28 case *wire.LookupStep:
29 if v.Stage != wire.LookupStageAnswer || len(v.Pages) != 1 || v.Pack.City != "Dublin" {
30 t.Errorf("step %+v", v)
31 }
32 case *wire.LookupResult:
33 if err := wire.ValidAnswerResult(*v, 1); err != nil {
34 t.Errorf("result %+v: %v", v, err)
35 }
36 default:
37 t.Errorf("%s decoded as %T (%s)", name, body, f.Type)
38 }
39 }
40}
41
42// Neither lookup frame is acked or resent.
43func TestLookupFramesNotDurable(t *testing.T) {
44 for _, kind := range []string{wire.FrameLookupStep, wire.FrameLookupResult} {
45 if (wire.Frame{Type: kind}).Durable() {
46 t.Errorf("%s is durable", kind)
47 }
48 }
49}
50
51func TestEncodeRefusesLookupBelow36(t *testing.T) {
52 f, _, err := wire.ParseFrame(wiretest.Fixture("frames/lookup-step.json"))
53 if err != nil {
54 t.Fatal(err)
55 }
56 if _, err := wire.Encode(f, wire.Protocol35); !errors.Is(err, wire.ErrProtocol) {
57 t.Errorf("at 35: %v", err)
58 }
59 if _, err := wire.Encode(f, wire.ProtocolLookup); err != nil {
60 t.Errorf("at 36: %v", err)
61 }
62}
63
64// Results are checked strictly: three queries, a 201-character query, an
65// answer with an extra field, an answer too long or a used index out of
66// range each fail.
67func TestLookupStepsStrictJSON(t *testing.T) {
68 q := func(qs ...string) wire.LookupResult {
69 return wire.LookupResult{Stage: wire.LookupStageQuery, Queries: qs}
70 }
71 if err := wire.ValidQueryResult(q("dublin weather", "dublin forecast tomorrow")); err != nil {
72 t.Errorf("two queries: %v", err)
73 }
74 for name, r := range map[string]wire.LookupResult{
75 "three": q("a", "b", "c"),
76 "long": q(strings.Repeat("x", 201)),
77 "empty": q(""),
78 "none": q(),
79 "answer": {Queries: []string{"a"}, Answer: "no"},
80 "a code": {Queries: []string{"a"}, Code: "lookup_failed"},
81 } {
82 if wire.ValidQueryResult(r) == nil {
83 t.Errorf("query %s accepted", name)
84 }
85 }
86 for name, r := range map[string]wire.LookupResult{
87 "too long": {Answer: strings.Repeat("é", 1201)},
88 "empty": {},
89 "used out": {Answer: "ok", Used: []int{3}},
90 "used twice": {Answer: "ok", Used: []int{0, 0}},
91 "queries": {Answer: "ok", Queries: []string{"a"}},
92 } {
93 if wire.ValidAnswerResult(r, 2) == nil {
94 t.Errorf("answer %s accepted", name)
95 }
96 }
97 if err := wire.ValidAnswerResult(wire.LookupResult{Answer: strings.Repeat("é", 1200), Used: []int{1}}, 2); err != nil {
98 t.Errorf("1200 characters: %v", err)
99 }
100 extra := []byte(`{"id":"x","type":"lookup_result","data":{"lookup":"l","attempt":1,"nonce":"n","stage":"answer","answer":"ok","extra":true}}`)
101 if _, _, err := wire.ParseFrame(extra); err == nil {
102 t.Error("an extra field was accepted")
103 }
104 var m wire.Message
105 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 {
106 t.Errorf("a message's lookups: %+v %v", m, err)
107 }
108}
109