| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http/httptest" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/equipo/personapi" |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | "github.com/go-chi/chi/v5" |
| 13 | ) |
| 14 | |
| 15 | func TestNotificationHTTPRetryCompatibility(t *testing.T) { |
| 16 | for _, mode := range []string{"legacy", "empty", "bound", "stale", "partial", "zero", "unknown", "null"} { |
| 17 | t.Run(mode, func(t *testing.T) { |
| 18 | r := newPushRig(t) |
| 19 | r.s.SetNotificationActions(true) |
| 20 | k := r.task("tk-http", "Fix", hkOwner) |
| 21 | r.step(&k, "st-http", wire.SpecialistEngineering, wire.StepFailed) |
| 22 | k = r.load(k.ID) |
| 23 | input := wire.RetryTask{Revision: k.Revision, StepID: "st-http", StepRevision: 1, Attempt: 1, IdempotencyKey: "notification-http-retry"} |
| 24 | if mode == "stale" { |
| 25 | input.Revision++ |
| 26 | } |
| 27 | raw, _ := json.Marshal(input) |
| 28 | body := string(raw) |
| 29 | switch mode { |
| 30 | case "legacy": |
| 31 | body = "" |
| 32 | case "empty": |
| 33 | body = "{ }" |
| 34 | case "partial": |
| 35 | body = `{"revision":1}` |
| 36 | case "zero": |
| 37 | body = `{"revision":0}` |
| 38 | case "unknown": |
| 39 | body = `{"extra":1}` |
| 40 | case "null": |
| 41 | body = "null" |
| 42 | } |
| 43 | send := func() *httptest.ResponseRecorder { |
| 44 | route := chi.NewRouteContext() |
| 45 | route.URLParams.Add("id", k.ID) |
| 46 | req := httptest.NewRequest("POST", "/tasks/"+k.ID+"/retry", strings.NewReader(body)).WithContext(context.WithValue(r.ctx, chi.RouteCtxKey, route)) |
| 47 | if body != "" { |
| 48 | req.Header.Set("Content-Type", "application/json") |
| 49 | } |
| 50 | res := httptest.NewRecorder() |
| 51 | (&HTTP{Store: r.s}).retryTask(res, req, personapi.PersonPrincipal{Address: hkOwner, CredentialID: "cr_phone"}) |
| 52 | return res |
| 53 | } |
| 54 | got := send() |
| 55 | want := 422 |
| 56 | if mode == "legacy" || mode == "empty" || mode == "bound" { |
| 57 | want = 202 |
| 58 | } |
| 59 | if mode == "stale" { |
| 60 | want = 409 |
| 61 | } |
| 62 | if got.Code != want { |
| 63 | t.Fatal(mode, got.Code, got.Body.String()) |
| 64 | } |
| 65 | if mode == "bound" { |
| 66 | again := send() |
| 67 | if again.Code != 202 || again.Body.String() != got.Body.String() { |
| 68 | t.Fatal("replay", again.Code, again.Body.String()) |
| 69 | } |
| 70 | } |
| 71 | }) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func TestNotificationHTTPAnswerReceipt(t *testing.T) { |
| 76 | r := newPushRig(t) |
| 77 | r.s.SetNotificationActions(true) |
| 78 | c := r.card("c-http", wire.CardOpen) |
| 79 | send := func(choice string) *httptest.ResponseRecorder { |
| 80 | raw, _ := json.Marshal(wire.AnswerCard{Revision: c.Revision, Choice: choice, IdempotencyKey: "notification-http-answer"}) |
| 81 | route := chi.NewRouteContext() |
| 82 | route.URLParams.Add("id", c.ID) |
| 83 | req := httptest.NewRequest("POST", "/cards/"+c.ID+"/answer", strings.NewReader(string(raw))).WithContext(context.WithValue(r.ctx, chi.RouteCtxKey, route)) |
| 84 | req.Header.Set("Content-Type", "application/json") |
| 85 | res := httptest.NewRecorder() |
| 86 | (&HTTP{Store: r.s}).answer(res, req, personapi.PersonPrincipal{Address: hkOwner, CredentialID: "cr_phone"}) |
| 87 | return res |
| 88 | } |
| 89 | first, again := send("main"), send("main") |
| 90 | if first.Code != 200 || again.Code != 200 { |
| 91 | t.Fatal(first.Code, again.Code) |
| 92 | } |
| 93 | var a, b wire.Answered |
| 94 | r.must(json.Unmarshal(first.Body.Bytes(), &a)) |
| 95 | r.must(json.Unmarshal(again.Body.Bytes(), &b)) |
| 96 | // Receipts retain result identities, not duplicate message text or a stale |
| 97 | // response snapshot. A later read can include newer presentation timestamps. |
| 98 | if a.Message.ID == "" || a.Message.ID != b.Message.ID || a.Turn.ID != b.Turn.ID || a.Card.ID != b.Card.ID || a.Card.Revision != b.Card.Revision { |
| 99 | t.Fatal(a, b) |
| 100 | } |
| 101 | changed := send("release") |
| 102 | if changed.Code != 409 || !strings.Contains(changed.Body.String(), "idempotency_conflict") { |
| 103 | t.Fatal(changed.Code, changed.Body.String()) |
| 104 | } |
| 105 | } |
| 106 | |