| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/wire" |
| 5 | "context" |
| 6 | "errors" |
| 7 | "gorm.io/gorm" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | func TestNotificationAnswerReceipt(t *testing.T) { |
| 14 | r := newPushRig(t) |
| 15 | r.s.SetNotificationActions(true) |
| 16 | c := r.card("nc1", wire.CardOpen) |
| 17 | in := wire.AnswerCard{Revision: c.Revision, Choice: "main", IdempotencyKey: "notification-answer-1"} |
| 18 | a, _, err := r.s.AnswerDevice(r.ctx, hkOwner, "cr_phone", c.ID, in) |
| 19 | r.must(err) |
| 20 | b, _, err := r.s.AnswerDevice(r.ctx, hkOwner, "cr_phone", c.ID, in) |
| 21 | r.must(err) |
| 22 | if a.Message.ID == "" || b.Message.ID != a.Message.ID || b.Turn.ID != a.Turn.ID { |
| 23 | t.Fatal("retry lost original result", a, b) |
| 24 | } |
| 25 | if got := r.count("SELECT count(*) FROM chat_messages WHERE idempotency_key = 'card:nc1'"); got != 1 { |
| 26 | t.Fatal(got) |
| 27 | } |
| 28 | in.Choice = "release" |
| 29 | if _, _, err = r.s.AnswerDevice(r.ctx, hkOwner, "cr_phone", c.ID, in); !errors.Is(err, ErrConflict) { |
| 30 | t.Fatal("changed body", err) |
| 31 | } |
| 32 | in.Choice = " main " |
| 33 | if _, _, err = r.s.AnswerDevice(r.ctx, hkOwner, "cr_phone", c.ID, in); !errors.Is(err, ErrConflict) { |
| 34 | t.Fatal("changed untrimmed body", err) |
| 35 | } |
| 36 | in.Choice = "main" |
| 37 | r.revoked["cr_phone"] = true |
| 38 | if _, _, err = r.s.AnswerDevice(r.ctx, hkOwner, "cr_phone", c.ID, in); !errors.Is(err, ErrForbidden) { |
| 39 | t.Fatal("revoked receipt read", err) |
| 40 | } |
| 41 | r.revoked["cr_phone"] = false |
| 42 | r.now = r.now.Add(8 * 24 * time.Hour) |
| 43 | if _, _, err = r.s.AnswerDevice(r.ctx, hkOwner, "cr_phone", c.ID, in); !errors.Is(err, ErrCardConflict) { |
| 44 | t.Fatal("expired receipt repeated action", err) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestNotificationRetryBinding(t *testing.T) { |
| 49 | for _, mutate := range []string{"task", "step", "revision", "attempt", "creator", "terminated", "none"} { |
| 50 | t.Run(mutate, func(t *testing.T) { |
| 51 | r := newPushRig(t) |
| 52 | r.s.SetNotificationActions(true) |
| 53 | k := r.task("tk-notify", "Fix", hkOwner) |
| 54 | r.step(&k, "st-notify", wire.SpecialistEngineering, wire.StepFailed) |
| 55 | k = r.load(k.ID) |
| 56 | in := wire.RetryTask{Revision: k.Revision, StepID: "st-notify", StepRevision: 1, Attempt: 1, IdempotencyKey: "notification-retry-1"} |
| 57 | person := hkOwner |
| 58 | switch mutate { |
| 59 | case "task": |
| 60 | in.Revision++ |
| 61 | case "step": |
| 62 | in.StepID = "other" |
| 63 | case "revision": |
| 64 | in.StepRevision++ |
| 65 | case "attempt": |
| 66 | in.Attempt++ |
| 67 | case "creator": |
| 68 | r.must(r.s.db.G.Model(&taskRecord{}).Where("id = ?", k.ID).Update("creator", "other@example.com").Error) |
| 69 | case "terminated": |
| 70 | r.must(r.s.db.G.Model(&stepRecord{}).Where("id = ?", in.StepID).Update("terminated", false).Error) |
| 71 | } |
| 72 | first, err := r.s.RetryTaskBound(r.ctx, person, "cr_phone", k.ID, in) |
| 73 | if mutate != "none" { |
| 74 | if err == nil { |
| 75 | t.Fatal("accepted changed binding") |
| 76 | } |
| 77 | return |
| 78 | } |
| 79 | r.must(err) |
| 80 | again, err := r.s.RetryTaskBound(r.ctx, person, "cr_phone", k.ID, in) |
| 81 | r.must(err) |
| 82 | if first != again || first != 2 { |
| 83 | t.Fatal(first, again) |
| 84 | } |
| 85 | in.Attempt++ |
| 86 | if _, err = r.s.RetryTaskBound(r.ctx, person, "cr_phone", k.ID, in); !errors.Is(err, ErrConflict) { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | }) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func TestNotificationReceiptsRequireCurrentAuthority(t *testing.T) { |
| 94 | for _, mode := range []string{"disabled", "credential", "member", "device", "invalid-key", "other-device", "inactive-member"} { |
| 95 | t.Run(mode, func(t *testing.T) { |
| 96 | r := newPushRig(t) |
| 97 | r.s.SetNotificationActions(true) |
| 98 | c := r.card("authority-card", wire.CardOpen) |
| 99 | in := wire.AnswerCard{Revision: c.Revision, Choice: "main", IdempotencyKey: "authority-answer"} |
| 100 | _, _, err := r.s.AnswerDevice(r.ctx, hkOwner, "cr_phone", c.ID, in) |
| 101 | r.must(err) |
| 102 | credential := "cr_phone" |
| 103 | switch mode { |
| 104 | case "disabled": |
| 105 | r.s.SetNotificationActions(false) |
| 106 | case "credential": |
| 107 | credential = "" |
| 108 | case "member": |
| 109 | r.s.authority.CurrentPerson = func(context.Context, *gorm.DB) error { return ErrForbidden } |
| 110 | case "device": |
| 111 | r.revoked[credential] = true |
| 112 | case "invalid-key": |
| 113 | in.IdempotencyKey = strings.Repeat("x", 129) |
| 114 | case "other-device": |
| 115 | credential = "cr_other" |
| 116 | case "inactive-member": |
| 117 | r.must(r.s.db.G.Exec("UPDATE members SET active = 0 WHERE address = ?", hkOwner).Error) |
| 118 | } |
| 119 | if _, _, err = r.s.AnswerDevice(r.ctx, hkOwner, credential, c.ID, in); err == nil { |
| 120 | t.Fatal("receipt bypassed current authority or binding") |
| 121 | } |
| 122 | }) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | func TestNotificationConcurrentReceiptsDoNotRepeatEffects(t *testing.T) { |
| 127 | r := newPushRig(t) |
| 128 | r.s.SetNotificationActions(true) |
| 129 | c := r.card("concurrent-card", wire.CardOpen) |
| 130 | in := wire.AnswerCard{Revision: c.Revision, Choice: "main", IdempotencyKey: "concurrent-answer"} |
| 131 | start := make(chan struct{}) |
| 132 | results := make(chan wire.Answered, 4) |
| 133 | failures := make(chan error, 4) |
| 134 | for range 4 { |
| 135 | go func() { |
| 136 | <-start |
| 137 | result, _, err := r.s.AnswerDevice(r.ctx, hkOwner, "cr_phone", c.ID, in) |
| 138 | results <- result |
| 139 | failures <- err |
| 140 | }() |
| 141 | } |
| 142 | close(start) |
| 143 | var first wire.Answered |
| 144 | for i := 0; i < 4; i++ { |
| 145 | got := <-results |
| 146 | if err := <-failures; err != nil { |
| 147 | t.Fatal(err) |
| 148 | } |
| 149 | if i == 0 { |
| 150 | first = got |
| 151 | } else if got.Message.ID != first.Message.ID || got.Turn.ID != first.Turn.ID { |
| 152 | t.Fatal("duplicate effect") |
| 153 | } |
| 154 | } |
| 155 | if r.count("SELECT count(*) FROM chat_notification_receipts") != 1 || r.count("SELECT count(*) FROM chat_messages WHERE idempotency_key = 'card:concurrent-card'") != 1 { |
| 156 | t.Fatal("duplicate receipt or message") |
| 157 | } |
| 158 | } |
| 159 | |