oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package push
2
3import (
4 "bytes"
5 "encoding/json"
6 "os"
7 "path/filepath"
8 "testing"
9
10 "amadan.net/oficina/equipo/internal/wire"
11)
12
13// copyStub stands for copy.go: the key in brackets.
14func copyStub(key string) string { return "[" + key + "]" }
15
16// TestPayloadGoldens holds every kind of push's APNs payload to its
17// golden in testdata (EQUIPO_PUSH_GOLDEN=1 rewrites them): the shapes the
18// native apps decode.
19func TestPayloadGoldens(t *testing.T) {
20 working := task(wire.StageInProgress, "", true)
21 working.Activity = "Reading README.md"
22 ready := task(wire.StageReadyToMerge, wire.NeedsMerge, false)
23 ready.Tone = wire.ToneWarning
24 done := task(wire.StageDone, "", false)
25 pick := func(c Change, rule string) Intent {
26 for _, in := range Decide(c) {
27 if in.Rule == rule {
28 return in
29 }
30 }
31 t.Fatalf("no %s", rule)
32 return Intent{}
33 }
34 cases := map[string]struct {
35 in Intent
36 badge int
37 typ string
38 }{
39 "card": {pick(Change{Kind: ChangeCard, Card: Card{ID: "c1", Kind: wire.CardMerge, State: wire.CardOpen, Question: "Merge the login fix?", TaskID: "tk1"}}, "card_opens"), 0, TypeAlert},
40 "reply": {pick(Change{Kind: ChangeReply, Reply: Reply{TurnID: "t1", Kind: wire.TurnPerson, State: wire.TurnReplied, Text: "Done. The fix is on its branch.\nMore."}}, "reply_unseen"), 0, TypeAlert},
41 "task-done": {pick(Change{Kind: ChangeTask, Before: seen(ready, true), After: done}, "task_ends"), 0, TypeAlert},
42 "task-failed": {pick(Change{Kind: ChangeTask, Before: seen(working, true), After: task(wire.StageInProgress, wire.NeedsRetry, false)}, "task_fails"), 0, TypeAlert},
43 "badge": {pick(Change{Kind: ChangeTask, Before: seen(working, true), After: ready}, "badge"), 2, TypeAlert},
44 "activity-start": {pick(Change{Kind: ChangeTask, Before: seen(task(wire.StageNoted, "", false), false), After: working}, "activity_start"), 0, TypeLiveActivity},
45 "activity-update": {pick(Change{Kind: ChangeLine, Now: at, Before: seen(task(wire.StageInProgress, "", true), true), After: working}, "activity_line"), 0, TypeLiveActivity},
46 "activity-needs": {pick(Change{Kind: ChangeTask, Before: seen(working, true), After: ready}, "activity_update"), 0, TypeLiveActivity},
47 "activity-end": {pick(Change{Kind: ChangeTask, Before: seen(ready, true), After: done}, "activity_end"), 0, TypeLiveActivity},
48 "activity-replaced": {Replace(working), 0, TypeLiveActivity},
49 }
50 for name, c := range cases {
51 c.in.Origin = "https://oficina.example.test"
52 raw, typ, err := Render(c.in, copyStub, c.badge, at)
53 if err != nil {
54 t.Fatal(name, err)
55 }
56 if typ != c.typ {
57 t.Errorf("%s: push type %s, want %s", name, typ, c.typ)
58 }
59 if len(raw) > 4096 {
60 t.Errorf("%s: %d bytes, over APNs' 4 KB", name, len(raw))
61 }
62 var pretty bytes.Buffer
63 if err := json.Indent(&pretty, raw, "", " "); err != nil {
64 t.Fatal(err)
65 }
66 pretty.WriteByte('\n')
67 path := filepath.Join("testdata", name+".json")
68 if os.Getenv("EQUIPO_PUSH_GOLDEN") == "1" {
69 if err := os.WriteFile(path, pretty.Bytes(), 0o644); err != nil {
70 t.Fatal(err)
71 }
72 continue
73 }
74 want, err := os.ReadFile(path)
75 if err != nil {
76 t.Fatal(err)
77 }
78 if !bytes.Equal(want, pretty.Bytes()) {
79 t.Errorf("%s payload differs from its golden:\n%s", name, pretty.String())
80 }
81 }
82 if TopicFor(TypeLiveActivity) != "works.oficina.equipo.push-type.liveactivity" || TopicFor(TypeAlert) != "works.oficina.equipo" {
83 t.Fatal("topics")
84 }
85}
86
87// TestPayloadContentState: a push-to-start carries the task's content
88// state and attributes as the app's Codable types decode them.
89func TestPayloadContentState(t *testing.T) {
90 k := task(wire.StageInProgress, "", true)
91 raw, _, err := Render(Decide(Change{Kind: ChangeTask, Before: seen(task(wire.StageNoted, "", false), false), After: k})[0], copyStub, 0, at)
92 if err != nil {
93 t.Fatal(err)
94 }
95 var p struct {
96 APS struct {
97 ContentState wire.LiveActivityState `json:"content-state"`
98 Attributes wire.LiveActivityAttributes `json:"attributes"`
99 AttributesType string `json:"attributes-type"`
100 } `json:"aps"`
101 }
102 if err := json.Unmarshal(raw, &p); err != nil {
103 t.Fatal(err)
104 }
105 want := wire.LiveActivityState{TaskID: "tk1", Title: "Fix login", Stage: wire.StageInProgress, Specialist: wire.SpecialistEngineering, NeedsYou: "", Tone: wire.TonePositive}
106 if p.APS.ContentState != want || p.APS.Attributes.TaskID != "tk1" || p.APS.AttributesType != wire.LiveActivityAttributesType {
107 t.Fatalf("content state %+v", p.APS)
108 }
109}
110