package thintest import ( "context" "encoding/json" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/equipo/assist" "amadan.net/oficina/equipo/internal/wire" ) // webStub is a web provider with web_lookup, so the switch in front of it // can be seen before lookups are built. type webStub struct{} func (webStub) Name() string { return "web" } func (webStub) List(context.Context, string) ([]assist.Operation, error) { return []assist.Operation{{Name: "web_lookup", App: "Web", ReadOnly: true, Description: "Look something up on the public web for the person.", Input: json.RawMessage(`{"type":"object","properties":{},"additionalProperties":false}`)}}, nil } func (webStub) Invoke(context.Context, string, string, json.RawMessage, string) (json.RawMessage, error) { return json.RawMessage(`{"shown":true}`), nil } // endTurn ends the rig's Assistant turn with a reply. func (o *opsRig) endTurn() { o.t.Helper() if err := o.runner.Send(o.ctx, "end-"+o.turn, wire.FrameReply, wire.Reply{TurnID: o.turn, Attempt: 1, Text: "Done."}); err != nil { o.t.Fatal(err) } o.until("turn "+o.turn+" replied", func() bool { return o.rows("SELECT count(*) FROM chat_turns WHERE id = ? AND state = ?", o.turn, wire.TurnReplied) == 1 }) } // moveClock sets the instance's clock d ahead of now. func (o *opsRig) moveClock(d time.Duration) { o.t.Helper() at := time.Now().UTC().Add(d) o.inst.Store.Chat().SetClock(func() time.Time { return at }) o.t.Cleanup(func() { o.inst.Store.Chat().SetClock(func() time.Time { return time.Now().UTC() }) }) } // Equipo at step 2 with a Base from before the board (step 0 or 1): PUT // host succeeds, every key is on as legacy, renewed through the Base's // operations route across 30 minutes, and the Assistant does what it did. func TestOldBaseKeepsTodaysBehaviour(t *testing.T) { base := StartFakeBase(t) base.SetLegacy(true) r := newRigWith(t, func(i *Instance) { i.AssistantHosts = []string{base.Origin} }) o := &opsRig{toolRig: &toolRig{rig: r, runner: r.dialAt(wire.ProtocolAssistant)}, base: base, a: r.client.Assistant(), protocol: wire.ProtocolAssistant} if out := o.connect(base.Token); out.Revision != 1 { t.Fatalf("PUT host: %+v", out) } for i := 1; i <= 3; i++ { o.moveClock(time.Duration(i) * 11 * time.Minute) o.begin("What are my apps?") if res := o.use("u"+itoa(i), "base_apps", map[string]any{}); res.IsError { t.Fatalf("after %d minutes: %+v", i*11, res) } o.endTurn() } got, err := o.inst.Store.Chat().OwnAbilities(o.ctx, Owner) if err != nil { t.Fatal(err) } for _, k := range wire.Abilities { if !got.Abilities[k] { t.Errorf("%s off for an old Base's member", k) } } } // With web off, the Assistant is told in one line who can turn it on, // with no machinery in what it is told; the lookup isn't offered. func TestWebOffReplyNamesOwner(t *testing.T) { o := newOpsRig(t, webStub{}) o.connect(o.base.Token) o.setBoard(wire.AbilityWeb) o.begin("What's the weather in Dublin?") if res := o.tool("l1", wire.ToolLook, map[string]any{"query": "weather"}); strings.Contains(res.Text, "web_lookup") { t.Errorf("look offers web_lookup with web off: %s", res.Text) } res := o.use("u1", "web_lookup", map[string]any{}) if !res.IsError || res.Code != wire.CodeAbilityOff || !strings.Contains(res.Text, "Paul can turn it on") || !strings.Contains(res.Text, "Look things up on the web") { t.Fatalf("web_lookup with web off: %+v", res) } if bad := MachineryIn(res.Text); bad != "" { t.Errorf("the refusal names %q: %s", bad, res.Text) } } // Staging acceptance 4, headless: the member disconnects Ask in Base, // which forgets their host token, and uses the Assistant with their own // Equipo bearer: nothing switched runs. func TestStagingAcceptance4Headless(t *testing.T) { o := newOpsRig(t, webStub{}) o.connect(o.base.Token) o.begin("What's the weather?") if res := o.use("u1", "web_lookup", map[string]any{}); res.IsError { t.Fatalf("before: %+v", res) } o.endTurn() o.base.Forget(o.base.Token) o.moveClock(2 * time.Minute) o.begin("What's the weather now?") if res := o.use("u2", "web_lookup", map[string]any{}); !res.IsError || res.Code == "used" { t.Errorf("after disconnecting in Base: %+v", res) } if res := o.use("u3", "base_apps", map[string]any{}); !res.IsError { t.Errorf("Base's reads with a forgotten token: %+v", res) } if n := o.rows("SELECT count(*) FROM chat_assistant_abilities WHERE person = ? AND revoked_at IS NOT NULL", Owner); n != 1 { t.Errorf("the copy wasn't revoked: %d", n) } }