| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/equipo/assist" |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // webStub is a web provider with web_lookup, so the switch in front of it |
| 15 | // can be seen before lookups are built. |
| 16 | type webStub struct{} |
| 17 | |
| 18 | func (webStub) Name() string { return "web" } |
| 19 | func (webStub) List(context.Context, string) ([]assist.Operation, error) { |
| 20 | 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 |
| 21 | } |
| 22 | func (webStub) Invoke(context.Context, string, string, json.RawMessage, string) (json.RawMessage, error) { |
| 23 | return json.RawMessage(`{"shown":true}`), nil |
| 24 | } |
| 25 | |
| 26 | // endTurn ends the rig's Assistant turn with a reply. |
| 27 | func (o *opsRig) endTurn() { |
| 28 | o.t.Helper() |
| 29 | if err := o.runner.Send(o.ctx, "end-"+o.turn, wire.FrameReply, wire.Reply{TurnID: o.turn, Attempt: 1, Text: "Done."}); err != nil { |
| 30 | o.t.Fatal(err) |
| 31 | } |
| 32 | o.until("turn "+o.turn+" replied", func() bool { |
| 33 | return o.rows("SELECT count(*) FROM chat_turns WHERE id = ? AND state = ?", o.turn, wire.TurnReplied) == 1 |
| 34 | }) |
| 35 | } |
| 36 | |
| 37 | // moveClock sets the instance's clock d ahead of now. |
| 38 | func (o *opsRig) moveClock(d time.Duration) { |
| 39 | o.t.Helper() |
| 40 | at := time.Now().UTC().Add(d) |
| 41 | o.inst.Store.Chat().SetClock(func() time.Time { return at }) |
| 42 | o.t.Cleanup(func() { o.inst.Store.Chat().SetClock(func() time.Time { return time.Now().UTC() }) }) |
| 43 | } |
| 44 | |
| 45 | // Equipo at step 2 with a Base from before the board (step 0 or 1): PUT |
| 46 | // host succeeds, every key is on as legacy, renewed through the Base's |
| 47 | // operations route across 30 minutes, and the Assistant does what it did. |
| 48 | func TestOldBaseKeepsTodaysBehaviour(t *testing.T) { |
| 49 | base := StartFakeBase(t) |
| 50 | base.SetLegacy(true) |
| 51 | r := newRigWith(t, func(i *Instance) { i.AssistantHosts = []string{base.Origin} }) |
| 52 | o := &opsRig{toolRig: &toolRig{rig: r, runner: r.dialAt(wire.ProtocolAssistant)}, base: base, a: r.client.Assistant(), protocol: wire.ProtocolAssistant} |
| 53 | if out := o.connect(base.Token); out.Revision != 1 { |
| 54 | t.Fatalf("PUT host: %+v", out) |
| 55 | } |
| 56 | for i := 1; i <= 3; i++ { |
| 57 | o.moveClock(time.Duration(i) * 11 * time.Minute) |
| 58 | o.begin("What are my apps?") |
| 59 | if res := o.use("u"+itoa(i), "base_apps", map[string]any{}); res.IsError { |
| 60 | t.Fatalf("after %d minutes: %+v", i*11, res) |
| 61 | } |
| 62 | o.endTurn() |
| 63 | } |
| 64 | got, err := o.inst.Store.Chat().OwnAbilities(o.ctx, Owner) |
| 65 | if err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | for _, k := range wire.Abilities { |
| 69 | if !got.Abilities[k] { |
| 70 | t.Errorf("%s off for an old Base's member", k) |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // With web off, the Assistant is told in one line who can turn it on, |
| 76 | // with no machinery in what it is told; the lookup isn't offered. |
| 77 | func TestWebOffReplyNamesOwner(t *testing.T) { |
| 78 | o := newOpsRig(t, webStub{}) |
| 79 | o.connect(o.base.Token) |
| 80 | o.setBoard(wire.AbilityWeb) |
| 81 | o.begin("What's the weather in Dublin?") |
| 82 | if res := o.tool("l1", wire.ToolLook, map[string]any{"query": "weather"}); strings.Contains(res.Text, "web_lookup") { |
| 83 | t.Errorf("look offers web_lookup with web off: %s", res.Text) |
| 84 | } |
| 85 | res := o.use("u1", "web_lookup", map[string]any{}) |
| 86 | 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") { |
| 87 | t.Fatalf("web_lookup with web off: %+v", res) |
| 88 | } |
| 89 | if bad := MachineryIn(res.Text); bad != "" { |
| 90 | t.Errorf("the refusal names %q: %s", bad, res.Text) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // Staging acceptance 4, headless: the member disconnects Ask in Base, |
| 95 | // which forgets their host token, and uses the Assistant with their own |
| 96 | // Equipo bearer: nothing switched runs. |
| 97 | func TestStagingAcceptance4Headless(t *testing.T) { |
| 98 | o := newOpsRig(t, webStub{}) |
| 99 | o.connect(o.base.Token) |
| 100 | o.begin("What's the weather?") |
| 101 | if res := o.use("u1", "web_lookup", map[string]any{}); res.IsError { |
| 102 | t.Fatalf("before: %+v", res) |
| 103 | } |
| 104 | o.endTurn() |
| 105 | o.base.Forget(o.base.Token) |
| 106 | o.moveClock(2 * time.Minute) |
| 107 | o.begin("What's the weather now?") |
| 108 | if res := o.use("u2", "web_lookup", map[string]any{}); !res.IsError || res.Code == "used" { |
| 109 | t.Errorf("after disconnecting in Base: %+v", res) |
| 110 | } |
| 111 | if res := o.use("u3", "base_apps", map[string]any{}); !res.IsError { |
| 112 | t.Errorf("Base's reads with a forgotten token: %+v", res) |
| 113 | } |
| 114 | if n := o.rows("SELECT count(*) FROM chat_assistant_abilities WHERE person = ? AND revoked_at IS NOT NULL", Owner); n != 1 { |
| 115 | t.Errorf("the copy wasn't revoked: %d", n) |
| 116 | } |
| 117 | } |
| 118 | |