oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package thin
2
3import (
4 "bytes"
5 "context"
6 "log/slog"
7 "os"
8 "path/filepath"
9 "strings"
10 "testing"
11 "time"
12
13 "amadan.net/oficina/equipo/internal/runner"
14 "amadan.net/oficina/equipo/internal/runner/provider"
15 "amadan.net/oficina/equipo/internal/wire"
16 "github.com/coder/websocket"
17)
18
19// fakeLookup is a lookup harness answering reply (or blocking until its
20// context ends when block), recording the prompt.
21type fakeLookup struct {
22 reply string
23 block bool
24 prompt string
25 closed bool
26}
27
28func (f *fakeLookup) Glance(ctx context.Context, prompt string) (string, error) {
29 f.prompt = prompt
30 if f.block {
31 <-ctx.Done()
32 return "", ctx.Err()
33 }
34 return f.reply, nil
35}
36func (f *fakeLookup) Warm(context.Context) error { return nil }
37func (f *fakeLookup) Up() bool { return true }
38func (f *fakeLookup) Close() { f.closed = true }
39
40// lookupEngine is an engine whose lookup harness is h, each in a new
41// directory under root; dirs are the directories made.
42func lookupEngine(t *testing.T, h *fakeLookup, logs *bytes.Buffer) (*Engine, *[]string) {
43 t.Helper()
44 root := t.TempDir()
45 var dirs []string
46 e := &Engine{Logger: slog.New(slog.NewTextHandler(logs, &slog.HandlerOptions{Level: slog.LevelDebug})),
47 OpenLookup: func(person string, _ runner.AccountBinding) (Glancer, string, error) {
48 dir, err := os.MkdirTemp(root, "lookup-")
49 if err == nil {
50 err = os.WriteFile(filepath.Join(dir, "session.jsonl"), []byte("a transcript"), 0o600)
51 }
52 dirs = append(dirs, dir)
53 return h, dir, err
54 }}
55 return e, &dirs
56}
57
58func answerStep(text string) wire.LookupStep {
59 return wire.LookupStep{Person: "paul@example.com", Lookup: "lk_1", Attempt: 1, Nonce: "n1", Stage: wire.LookupStageAnswer,
60 Pack: wire.LookupPack{Question: "Weather in Dublin?", City: "Dublin", Date: "2026-11-28, Saturday"},
61 Results: []wire.SearchResult{{Title: "Met", URL: "https://www.met.ie/"}}, Pages: []wire.LookupPage{{URL: "https://www.met.ie/", Text: text}}}
62}
63
64// A lookup step has the glance's harness (no tools, never resumed) in a
65// directory of its own, gone when it returns; its result is its
66// stage's JSON, for its lookup, attempt and nonce.
67func TestLookupStepsIsolated(t *testing.T) {
68 if lookupProfile.action != provider.GlanceAction || !lookupProfile.ephemeral || lookupProfile.set(true).permission == nil {
69 t.Fatalf("profile %+v", lookupProfile)
70 }
71 h := &fakeLookup{reply: `{"answer": "Rain, 11°C.", "used": [0]}`}
72 var logs bytes.Buffer
73 e, dirs := lookupEngine(t, h, &logs)
74 r := e.Lookup(context.Background(), answerStep("Rain at first."))
75 if r.Code != "" || r.Answer != "Rain, 11°C." || r.Lookup != "lk_1" || r.Nonce != "n1" || r.Attempt != 1 || r.Stage != wire.LookupStageAnswer {
76 t.Fatalf("result %+v", r)
77 }
78 if len(*dirs) != 1 || !h.closed {
79 t.Fatalf("dirs %v closed %v", *dirs, h.closed)
80 }
81 if _, err := os.Stat((*dirs)[0]); !os.IsNotExist(err) {
82 t.Errorf("the step's directory is still there: %v", err)
83 }
84 if !strings.Contains(h.prompt, wire.LookupWebHeader) || !strings.HasPrefix(h.prompt, wire.LookupAnswerPrompt) {
85 t.Errorf("prompt %.200q", h.prompt)
86 }
87}
88
89// What a page says goes nowhere on the runner but the model: not its
90// log, not its directory, which is gone.
91func TestLookupStepEphemeralOnRunner(t *testing.T) {
92 const sentinel = "SENTINEL-PAGE-TEXT-42"
93 h := &fakeLookup{reply: `{"answer": "` + sentinel + `", "used": [0]}`}
94 var logs bytes.Buffer
95 e, dirs := lookupEngine(t, h, &logs)
96 _ = e.Lookup(context.Background(), answerStep(sentinel))
97 if strings.Contains(logs.String(), sentinel) || !strings.Contains(logs.String(), "assistant lookup") {
98 t.Errorf("logs %q", logs.String())
99 }
100 if _, err := os.Stat((*dirs)[0]); !os.IsNotExist(err) {
101 t.Error("the directory survived")
102 }
103}
104
105func TestLookupStepTimeout(t *testing.T) {
106 h := &fakeLookup{block: true}
107 var logs bytes.Buffer
108 e, _ := lookupEngine(t, h, &logs)
109 e.LookupTimeout = 50 * time.Millisecond
110 start := time.Now()
111 if r := e.Lookup(context.Background(), answerStep("x")); r.Code != wire.CodeLookupFailed || time.Since(start) > 5*time.Second {
112 t.Errorf("result %+v after %s", r, time.Since(start))
113 }
114 if LookupTimeout != 20*time.Second {
115 t.Errorf("the timeout is %s", LookupTimeout)
116 }
117}
118
119func TestLookupStepBadJSON(t *testing.T) {
120 for _, reply := range []string{"Rain, 11°C.", `{"answer": "x", "used": [0], "note": "y"}`, `{"answer": "x", "used": [5]}`} {
121 h := &fakeLookup{reply: reply}
122 var logs bytes.Buffer
123 e, _ := lookupEngine(t, h, &logs)
124 if r := e.Lookup(context.Background(), answerStep("x")); r.Code != wire.CodeLookupFailed || r.Answer != "" {
125 t.Errorf("%q: %+v", reply, r)
126 }
127 }
128}
129
130// A result goes on the connection its step came on, or nowhere (Astra
131// phase 3 round 1, P2).
132func TestLookupResultStaysOnItsConnection(t *testing.T) {
133 l := &Link{}
134 l.init()
135 first, second := &websocket.Conn{}, &websocket.Conn{}
136 l.conn = first
137 l.lookupResult(first, wire.LookupResult{Lookup: "a"})
138 l.conn = second
139 l.lookupResult(first, wire.LookupResult{Lookup: "late"})
140 if got := l.takeLookups(second); len(got) != 0 {
141 t.Errorf("sent on the next connection: %+v", got)
142 }
143 l.lookupResult(second, wire.LookupResult{Lookup: "b"})
144 if got := l.takeLookups(second); len(got) != 1 || got[0].Lookup != "b" {
145 t.Errorf("got %+v", got)
146 }
147}
148