| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "sync" |
| 7 | "sync/atomic" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/equipo/assist" |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // hookProvider is an Oficina-like provider whose List and Invoke run a |
| 15 | // test's hook first: n is the call's number. |
| 16 | type hookProvider struct { |
| 17 | mu sync.Mutex |
| 18 | ops func(n int) []assist.Operation |
| 19 | onList func(n int) |
| 20 | onInvoke func(n int) |
| 21 | lists int |
| 22 | invokes atomic.Int32 |
| 23 | } |
| 24 | |
| 25 | func (p *hookProvider) Name() string { return OficinaProvider } |
| 26 | func (p *hookProvider) List(context.Context, string) ([]assist.Operation, error) { |
| 27 | p.mu.Lock() |
| 28 | p.lists++ |
| 29 | n := p.lists |
| 30 | p.mu.Unlock() |
| 31 | if p.onList != nil { |
| 32 | p.onList(n) |
| 33 | } |
| 34 | return p.ops(n), nil |
| 35 | } |
| 36 | func (p *hookProvider) Invoke(context.Context, string, string, json.RawMessage, string) (json.RawMessage, error) { |
| 37 | n := int(p.invokes.Add(1)) |
| 38 | if p.onInvoke != nil { |
| 39 | p.onInvoke(n) |
| 40 | } |
| 41 | return json.RawMessage(`{"events":[]}`), nil |
| 42 | } |
| 43 | |
| 44 | func calendarList(int) []assist.Operation { |
| 45 | return []assist.Operation{{Name: "calendar_list", ReadOnly: true, Scope: "calendar:read"}} |
| 46 | } |
| 47 | |
| 48 | // turnOff turns reading apps off at Base and has Equipo hear it. |
| 49 | func turnOff(t *testing.T, h *abilitiesHarness, base *fakeBoardBase) { |
| 50 | base.set(func(f *fakeBoardBase) { f.board.Abilities["apps_read"], f.board.Revision = false, f.board.Revision+1 }) |
| 51 | if _, _, err := h.s.RefreshAbilities(context.Background(), abPerson); err != nil { |
| 52 | t.Error(err) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // The switch turned off after use was checked but before the operation |
| 57 | // goes out (while it is described) means it isn't sent; turned off once |
| 58 | // the gate has passed, that request completes, and the next is refused. |
| 59 | func TestSendGate(t *testing.T) { |
| 60 | h, base := boardBase(t) |
| 61 | refresh(t, h) |
| 62 | p := &hookProvider{ops: calendarList} |
| 63 | p.onList = func(n int) { |
| 64 | if n == 1 { |
| 65 | turnOff(t, h, base) |
| 66 | } |
| 67 | } |
| 68 | h.s.SetOperations(assist.NewRegistry(p)) |
| 69 | res, _ := h.s.runRemote(context.Background(), useRow(abPerson, "calendar_list")) |
| 70 | if !res.IsError || res.Code != wire.CodeAbilityOff || p.invokes.Load() != 0 { |
| 71 | t.Errorf("off while described: %+v, sent %d", res, p.invokes.Load()) |
| 72 | } |
| 73 | |
| 74 | h2, base2 := boardBase(t) |
| 75 | refresh(t, h2) |
| 76 | p2 := &hookProvider{ops: calendarList} |
| 77 | p2.onInvoke = func(n int) { |
| 78 | if n == 1 { |
| 79 | turnOff(t, h2, base2) |
| 80 | } |
| 81 | } |
| 82 | h2.s.SetOperations(assist.NewRegistry(p2)) |
| 83 | if res, _ := h2.s.runRemote(context.Background(), useRow(abPerson, "calendar_list")); res.IsError { |
| 84 | t.Errorf("in flight once the gate passed: %+v", res) |
| 85 | } |
| 86 | res, _ = h2.s.runRemote(context.Background(), useRow(abPerson, "calendar_list")) |
| 87 | if !res.IsError || res.Code != wire.CodeAbilityOff || p2.invokes.Load() != 1 { |
| 88 | t.Errorf("the next request: %+v, sent %d", res, p2.invokes.Load()) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // An operation that resolves differently at send from the one checked is |
| 93 | // refused operation_changed, and isn't sent. |
| 94 | func TestGateBindsOperation(t *testing.T) { |
| 95 | h, _ := boardBase(t) |
| 96 | refresh(t, h) |
| 97 | p := &hookProvider{} |
| 98 | p.ops = func(n int) []assist.Operation { |
| 99 | if n == 1 { |
| 100 | return calendarList(n) |
| 101 | } |
| 102 | return []assist.Operation{{Name: "calendar_list", ReadOnly: true, Scope: "calendar:all"}} |
| 103 | } |
| 104 | p.onList = func(n int) { |
| 105 | if n == 1 { |
| 106 | // The list is stale by the time Invoke looks again. |
| 107 | h.s.operations().Forget(abPerson) |
| 108 | } |
| 109 | } |
| 110 | h.s.SetOperations(assist.NewRegistry(p)) |
| 111 | res, _ := h.s.runRemote(context.Background(), useRow(abPerson, "calendar_list")) |
| 112 | if !res.IsError || res.Code != wire.CodeOperationChanged || p.invokes.Load() != 0 { |
| 113 | t.Errorf("changed at send: %+v, sent %d", res, p.invokes.Load()) |
| 114 | } |
| 115 | } |
| 116 | |