| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "strings" |
| 7 | "sync/atomic" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/equipo/assist" |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // opsProvider is an in-test provider: its operations, and how often it |
| 15 | // was asked to list and to run one. |
| 16 | type opsProvider struct { |
| 17 | name string |
| 18 | ops []assist.Operation |
| 19 | lists, invokes atomic.Int32 |
| 20 | } |
| 21 | |
| 22 | func (p *opsProvider) Name() string { return p.name } |
| 23 | func (p *opsProvider) List(context.Context, string) ([]assist.Operation, error) { |
| 24 | p.lists.Add(1) |
| 25 | return p.ops, nil |
| 26 | } |
| 27 | func (p *opsProvider) Invoke(context.Context, string, string, json.RawMessage, string) (json.RawMessage, error) { |
| 28 | p.invokes.Add(1) |
| 29 | return json.RawMessage(`{"ok":true}`), nil |
| 30 | } |
| 31 | |
| 32 | // useRig is a person with a board from their Base and a registry of |
| 33 | // in-test providers, one per row of abilityFor's table. |
| 34 | func useRig(t *testing.T, off ...string) (*abilitiesHarness, map[string]*opsProvider) { |
| 35 | t.Helper() |
| 36 | h, base := boardBase(t) |
| 37 | base.set(func(f *fakeBoardBase) { |
| 38 | for _, k := range off { |
| 39 | f.board.Abilities[k] = false |
| 40 | } |
| 41 | }) |
| 42 | refresh(t, h) |
| 43 | ps := map[string]*opsProvider{ |
| 44 | "base": {name: "base", ops: []assist.Operation{{Name: "base_apps", ReadOnly: true}}}, |
| 45 | "oficina": {name: "oficina", ops: []assist.Operation{{Name: "calendar_list", ReadOnly: true}, {Name: "calendar_add", Idempotent: true, KeyField: "client_id"}}}, |
| 46 | "web": {name: "web", ops: []assist.Operation{{Name: "web_lookup", ReadOnly: true}}}, |
| 47 | "svc": {name: "svc:ledger", ops: []assist.Operation{{Name: "ledger_read", ReadOnly: true}}}, |
| 48 | "mystery": {name: "mystery", ops: []assist.Operation{{Name: "mystery_read", ReadOnly: true}}}, |
| 49 | } |
| 50 | var list []assist.Provider |
| 51 | for _, k := range []string{"base", "oficina", "web", "svc", "mystery"} { |
| 52 | list = append(list, ps[k]) |
| 53 | } |
| 54 | h.s.SetOperations(assist.NewRegistry(list...)) |
| 55 | return h, ps |
| 56 | } |
| 57 | |
| 58 | func useRow(person, op string) toolCallRecord { |
| 59 | args, _ := json.Marshal(map[string]any{"operation": op, "input": map[string]any{}}) |
| 60 | return toolCallRecord{CallID: "call-" + op, Person: person, Name: wire.ToolUse, Operation: op, Args: string(args), TurnID: "t1", Attempt: 1} |
| 61 | } |
| 62 | |
| 63 | func lookRow(person, query string) toolCallRecord { |
| 64 | args, _ := json.Marshal(map[string]any{"query": query}) |
| 65 | return toolCallRecord{CallID: "look", Person: person, Name: wire.ToolLook, Args: string(args), TurnID: "t1", Attempt: 1} |
| 66 | } |
| 67 | |
| 68 | // look leaves out what the person's switches turn off. |
| 69 | func TestLookHidesOffOperationsForPerson(t *testing.T) { |
| 70 | h, ps := useRig(t, wire.AbilityAppsWrite, wire.AbilityWeb) |
| 71 | res, _ := h.s.runRemote(context.Background(), lookRow(abPerson, "")) |
| 72 | for _, want := range []string{"calendar_list", "base_apps", "ledger_read"} { |
| 73 | if !strings.Contains(res.Text, want) { |
| 74 | t.Errorf("look lacks %s:\n%s", want, res.Text) |
| 75 | } |
| 76 | } |
| 77 | for _, gone := range []string{"calendar_add", "web_lookup", "mystery_read"} { |
| 78 | if strings.Contains(res.Text, gone) { |
| 79 | t.Errorf("look offers %s:\n%s", gone, res.Text) |
| 80 | } |
| 81 | } |
| 82 | if ps["web"].lists.Load() != 0 { |
| 83 | t.Error("a provider whose ability is off was asked to list") |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // use refuses every operation whose ability is off, with the plain name |
| 88 | // and who to ask, and runs nothing; with it on, it runs. |
| 89 | func TestUseRefusesAbilityOff(t *testing.T) { |
| 90 | for _, c := range []struct { |
| 91 | op, provider, key string |
| 92 | }{ |
| 93 | {"calendar_list", "oficina", wire.AbilityAppsRead}, |
| 94 | {"calendar_add", "oficina", wire.AbilityAppsWrite}, |
| 95 | {"web_lookup", "web", wire.AbilityWeb}, |
| 96 | {"ledger_read", "svc", wire.AbilityServices}, |
| 97 | } { |
| 98 | h, ps := useRig(t, c.key) |
| 99 | res, _ := h.s.runRemote(context.Background(), useRow(abPerson, c.op)) |
| 100 | if !res.IsError || res.Code != wire.CodeAbilityOff || ps[c.provider].invokes.Load() != 0 { |
| 101 | t.Errorf("%s with %s off: %+v, invoked %d", c.op, c.key, res, ps[c.provider].invokes.Load()) |
| 102 | } |
| 103 | if !strings.Contains(res.Text, abilityNames["assistant.ability."+c.key+".name"]) || !strings.Contains(res.Text, "Paul") { |
| 104 | t.Errorf("%s refusal text: %q", c.op, res.Text) |
| 105 | } |
| 106 | h2, ps2 := useRig(t) |
| 107 | if res, _ := h2.s.runRemote(context.Background(), useRow(abPerson, c.op)); c.op != "calendar_add" && (res.IsError || ps2[c.provider].invokes.Load() != 1) { |
| 108 | t.Errorf("%s with everything on: %+v", c.op, res) |
| 109 | } |
| 110 | } |
| 111 | // Base's reads are always on. |
| 112 | h, ps := useRig(t, wire.Abilities...) |
| 113 | if res, _ := h.s.runRemote(context.Background(), useRow(abPerson, "base_apps")); res.IsError || ps["base"].invokes.Load() != 1 { |
| 114 | t.Errorf("base_apps with everything off: %+v", res) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // An operation no ability covers is never run. |
| 119 | func TestUnmappedOperationRefused(t *testing.T) { |
| 120 | h, ps := useRig(t) |
| 121 | res, _ := h.s.runRemote(context.Background(), useRow(abPerson, "mystery_read")) |
| 122 | if !res.IsError || res.Code != wire.CodeAbilityUnmapped || ps["mystery"].invokes.Load() != 0 { |
| 123 | t.Errorf("unmapped: %+v", res) |
| 124 | } |
| 125 | for _, c := range []struct { |
| 126 | op assist.Operation |
| 127 | key string |
| 128 | okay bool |
| 129 | }{ |
| 130 | {assist.Operation{Provider: "team", Name: "team_request"}, wire.AbilityBuild, true}, |
| 131 | {assist.Operation{Provider: "team", Name: "team_add"}, wire.AbilityBuild, true}, |
| 132 | {assist.Operation{Provider: "team", Name: "team_reply"}, wire.AbilityBuild, true}, |
| 133 | {assist.Operation{Provider: "team", Name: "team_ask"}, wire.AbilityBuild, true}, |
| 134 | {assist.Operation{Provider: "team", Name: "team_nudge"}, wire.AbilityBuild, true}, |
| 135 | {assist.Operation{Provider: "team", Name: "team_status", ReadOnly: true}, "", true}, |
| 136 | {assist.Operation{Provider: "team", Name: "team_cancel"}, "", true}, |
| 137 | {assist.Operation{Provider: "team", Name: "connect_service"}, wire.AbilityServices, true}, |
| 138 | {assist.Operation{Provider: "team", Name: "team_unknown"}, "", false}, |
| 139 | {assist.Operation{Provider: "base", Name: "base_write"}, "", false}, |
| 140 | {assist.Operation{Provider: "svc:x", Name: "anything"}, wire.AbilityServices, true}, |
| 141 | {assist.Operation{Provider: "web", Name: "web_other"}, "", false}, |
| 142 | } { |
| 143 | key, ok := abilityFor(c.op) |
| 144 | if key != c.key || ok != c.okay { |
| 145 | t.Errorf("%s/%s: %q %v, want %q %v", c.op.Provider, c.op.Name, key, ok, c.key, c.okay) |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // The turn's note says what is on and off, and follows the switches. |
| 151 | func TestTurnNoteFollowsSwitches(t *testing.T) { |
| 152 | h, base := boardBase(t) |
| 153 | refresh(t, h) |
| 154 | note := func(listed bool) string { |
| 155 | var out string |
| 156 | if err := h.s.tx(context.Background(), func(tx *txn) error { |
| 157 | var err error |
| 158 | out, err = tx.abilitiesNote(abPerson, listed) |
| 159 | return err |
| 160 | }); err != nil { |
| 161 | t.Fatal(err) |
| 162 | } |
| 163 | return out |
| 164 | } |
| 165 | if got := note(false); got != "" { |
| 166 | t.Errorf("everything on, nothing listed: %q", got) |
| 167 | } |
| 168 | if got := note(true); !strings.Contains(got, "web_lookup") || strings.Contains(got, "Off for this person") { |
| 169 | t.Errorf("listed: %q", got) |
| 170 | } |
| 171 | base.set(func(f *fakeBoardBase) { |
| 172 | f.board.Abilities["build"], f.board.Abilities["go_live"], f.board.Revision = false, false, 2 |
| 173 | }) |
| 174 | refresh(t, h) |
| 175 | want := "Off for this person, set by Paul: Build sites and apps, Put things live. If they ask for one of these, say in one line that you can't here and that Paul can turn it on." |
| 176 | if got := note(false); got != want { |
| 177 | t.Errorf("note %q\nwant %q", got, want) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // assistant.use records the refusal's code. |
| 182 | func TestUseAuditsCode(t *testing.T) { |
| 183 | h, _ := useRig(t, wire.AbilityAppsRead) |
| 184 | row := useRow(abPerson, "calendar_list") |
| 185 | res, _ := h.s.runRemote(context.Background(), row) |
| 186 | if err := h.s.tx(context.Background(), func(tx *txn) error { return tx.auditUse(row, res) }); err != nil { |
| 187 | t.Fatal(err) |
| 188 | } |
| 189 | if got := h.audited(auditAssistantUse); len(got) != 1 || !strings.HasSuffix(got[0], "calendar_list ability_off") { |
| 190 | t.Errorf("audits %v", got) |
| 191 | } |
| 192 | } |
| 193 | |