| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/push" |
| 5 | "amadan.net/oficina/equipo/internal/wire" |
| 6 | "encoding/json" |
| 7 | "gorm.io/gorm" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestNotificationPlacementPushRuleIsOpenOnly(t *testing.T) { |
| 12 | for _, byName := range []bool{false, true} { |
| 13 | t.Run(map[bool]string{false: "options", true: "by-name"}[byName], func(t *testing.T) { |
| 14 | r := newPushRig(t) |
| 15 | r.s.SetNotificationActions(true) |
| 16 | r.register(hkOwner, "cr_phone", wire.RegisterPush{Token: pushDevice, Environment: wire.APNsProduction, Platform: wire.PlatformIOS, ActionsVersion: 1, BindingID: "12345678-1234-4234-8234-123456789abc"}) |
| 17 | r.clear() |
| 18 | placement, _ := json.Marshal(placementData{ByName: byName}) |
| 19 | options := `["Use existing","New project"]` |
| 20 | if byName { |
| 21 | options = `[]` |
| 22 | } |
| 23 | c := cardRecord{ID: "c_placement", Person: hkOwner, Kind: wire.CardQuestion, State: wire.CardOpen, Question: "Which project?", Options: options, Placement: string(placement), Revision: 1, CreatedAt: r.now, UpdatedAt: r.now} |
| 24 | r.must(r.tx(func(tx *txn) error { |
| 25 | if err := tx.db.Create(&c).Error; err != nil { |
| 26 | return err |
| 27 | } |
| 28 | return tx.cardEvent(c) |
| 29 | })) |
| 30 | rows := r.outbox() |
| 31 | if len(rows) != 1 { |
| 32 | t.Fatalf("rows %d", len(rows)) |
| 33 | } |
| 34 | var p struct { |
| 35 | APS struct{ Category string } |
| 36 | Equipo wire.PushData |
| 37 | } |
| 38 | r.must(json.Unmarshal([]byte(rows[0].Payload), &p)) |
| 39 | if p.Equipo.Actions != nil || p.APS.Category != push.CategoryNeedsYou { |
| 40 | t.Fatalf("placement offered action: %+v", p) |
| 41 | } |
| 42 | var view wire.Card |
| 43 | r.must(r.tx(func(tx *txn) (err error) { view, err = tx.cardView(c); return err })) |
| 44 | if !view.PlacementQuestion || view.PlacementByName != byName { |
| 45 | t.Fatalf("placement marker: %+v", view) |
| 46 | } |
| 47 | }) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // TestNotificationRetryPushRuleRefusals offers Try again exactly when the |
| 52 | // person's Retry would accept the failed step. Review and Publish failures |
| 53 | // raise no task alert (a failed Review needs a decision, not a Retry, and a |
| 54 | // publish never drives the task), so their eligibility is read through |
| 55 | // notificationIntent directly; every other case uses the real enqueue path. |
| 56 | func TestNotificationRetryPushRuleRefusals(t *testing.T) { |
| 57 | for _, mode := range []string{"valid", "checks", "review", "member", "merge", "publish", "other-step-person", "no-runner"} { |
| 58 | t.Run(mode, func(t *testing.T) { |
| 59 | r := newPushRig(t) |
| 60 | r.s.SetNotificationActions(true) |
| 61 | device := wire.RegisterPush{Token: pushDevice, Environment: wire.APNsProduction, Platform: wire.PlatformIOS, ActionsVersion: 1, BindingID: "12345678-1234-4234-8234-123456789abc"} |
| 62 | r.register(hkOwner, "cr_phone", device) |
| 63 | k := r.task("tk_notify", "Fix", hkOwner) |
| 64 | r.stage(&k, wire.StageInProgress) |
| 65 | if mode == "member" { |
| 66 | r.s.authority.Me = func(*gorm.DB, string) (wire.Me, error) { return wire.Me{Role: "member"}, nil } |
| 67 | } |
| 68 | if mode == "no-runner" { |
| 69 | r.s.authority.PrimaryRunner = func(*gorm.DB, string) (string, error) { return "", nil } |
| 70 | } |
| 71 | specialist := map[string]string{"checks": wire.SpecialistChecks, "review": wire.SpecialistReview, "merge": wire.SpecialistMerge, "publish": wire.SpecialistPublish}[mode] |
| 72 | if specialist == "" { |
| 73 | specialist = wire.SpecialistEngineering |
| 74 | } |
| 75 | person := hkOwner |
| 76 | if mode == "other-step-person" { |
| 77 | person = "other@example.com" |
| 78 | } |
| 79 | offered := mode == "valid" || mode == "checks" || mode == "review" |
| 80 | r.clear() |
| 81 | r.must(r.tx(func(tx *txn) error { |
| 82 | st := stepRecord{ID: "st_notify", TaskID: k.ID, Idx: 1, Person: person, Specialist: specialist, State: wire.StepFailed, Terminated: true, Revision: 1, Attempt: 1, CreatedAt: r.now, UpdatedAt: r.now} |
| 83 | if err := tx.db.Create(&st).Error; err != nil { |
| 84 | return err |
| 85 | } |
| 86 | return tx.saveTask(&k) |
| 87 | })) |
| 88 | check := func(category string, actions *wire.PushActions) { |
| 89 | t.Helper() |
| 90 | if offered && (actions == nil || category != "equipo.stopped.v1" || actions.StepID != "st_notify") { |
| 91 | t.Fatalf("missing retry: %s %+v", category, actions) |
| 92 | } |
| 93 | if !offered && actions != nil { |
| 94 | t.Fatalf("refused step offered retry: %s %+v", category, actions) |
| 95 | } |
| 96 | } |
| 97 | alerts := 0 |
| 98 | for _, row := range r.outbox() { |
| 99 | if row.Kind != push.KindAlert && row.Kind != push.KindPassive { |
| 100 | continue |
| 101 | } |
| 102 | alerts++ |
| 103 | var p struct { |
| 104 | APS struct{ Category string } |
| 105 | Equipo wire.PushData |
| 106 | } |
| 107 | r.must(json.Unmarshal([]byte(row.Payload), &p)) |
| 108 | check(p.APS.Category, p.Equipo.Actions) |
| 109 | } |
| 110 | if mode == "review" || mode == "publish" { |
| 111 | if alerts != 0 { |
| 112 | t.Fatalf("%s failure raised %d task alerts", mode, alerts) |
| 113 | } |
| 114 | var out push.Intent |
| 115 | r.must(r.tx(func(tx *txn) error { |
| 116 | in := push.Intent{Kind: push.KindPassive, Data: wire.PushData{Kind: wire.PushTask, TaskID: k.ID}} |
| 117 | out = tx.notificationIntent(in, push.Registration{Person: hkOwner, ActionsVersion: 1, BindingID: device.BindingID}) |
| 118 | return nil |
| 119 | })) |
| 120 | check(out.Category, out.Data.Actions) |
| 121 | return |
| 122 | } |
| 123 | if alerts == 0 { |
| 124 | t.Fatal("real task push did not enqueue an alert") |
| 125 | } |
| 126 | }) |
| 127 | } |
| 128 | } |
| 129 | |