|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "strings" |
| 7 | + "sync/atomic" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func marketingClaimDelivery(position, id string) M { |
| 12 | + visual := "background" |
| 13 | + if position == "popup" { |
| 14 | + visual = "hero" |
| 15 | + } |
| 16 | + return M{"resource_position": position, position: M{ |
| 17 | + "buttons": []any{M{"action": M{"type": "close"}}, M{"action": M{"type": "claim_zcode_plan", "args": M{"plan_id": id}}}}, |
| 18 | + visual: M{"args": M{"zcode_plan": M{ |
| 19 | + "plan_id": id, "name": "Weekend Build", "starts_at": 100, "ends_at": 300, |
| 20 | + "entitlements": []any{M{"show_name": "GLM-5.3-Flash", "grant_units": 300000000, "unit_type": "token", "effective_at": 200}}, |
| 21 | + }}}, |
| 22 | + "success_popup": M{"buttons": []any{M{"action": M{"type": "claim_zcode_plan", "args": M{"plan_id": "not-available"}}}}}, |
| 23 | + }} |
| 24 | +} |
| 25 | + |
| 26 | +func TestMarketingClaimsDiscoveryAndAutoClaim(t *testing.T) { |
| 27 | + a := testApp(t) |
| 28 | + _, _, accounts := testPool(t, a, 1) |
| 29 | + id := str(accounts[0]["id"]) |
| 30 | + const planID = "weekend-card" |
| 31 | + var claims atomic.Int32 |
| 32 | + installUpstream(t, a, func(w http.ResponseWriter, r *http.Request) { |
| 33 | + if r.Header.Get("Authorization") != "Bearer synthetic-0" || r.Method == "GET" && r.URL.Path == "/api/v1/marketing/touch" && r.Header.Get("X-Device-Mid") != str(accounts[0]["device_mid"]) { |
| 34 | + t.Error("missing account authorization or device identity") |
| 35 | + } |
| 36 | + switch r.URL.Path { |
| 37 | + case "/api/v1/marketing/touch": |
| 38 | + writeJSON(w, 200, M{"code": 0, "data": M{"deliveries": []any{ |
| 39 | + marketingClaimDelivery("banner", planID), marketingClaimDelivery("popup", planID), |
| 40 | + }}}) |
| 41 | + case "/api/v1/zcode-plan/billing/preview": |
| 42 | + // Reproduce the legacy endpoint's observed success without data. |
| 43 | + writeJSON(w, 200, M{"code": 0, "msg": ""}) |
| 44 | + case "/api/v1/incentive/referral/summary": |
| 45 | + writeJSON(w, 200, M{"code": 0, "data": M{"gameplay": M{"status": "online"}, "inviter": M{"rewards": []any{ |
| 46 | + M{"state": "in_progress", "plan": M{"plan_id": "unearned-referral-card"}}, |
| 47 | + }}}}) |
| 48 | + case "/api/v1/zcode-plan/billing/claim": |
| 49 | + claims.Add(1) |
| 50 | + equal(t, r.Method, "POST") |
| 51 | + body, e := readBody(w, r) |
| 52 | + must(t, e) |
| 53 | + equal(t, body["plan_id"], planID) |
| 54 | + writeJSON(w, 200, M{"code": 0, "data": M{"plan": M{"plan_id": planID, "user_plan_id": "claimed"}}}) |
| 55 | + case "/api/v1/zcode-plan/billing/balance": |
| 56 | + writeJSON(w, 200, M{"code": 0, "data": M{"plans": []any{}, "balances": []any{}}}) |
| 57 | + default: |
| 58 | + t.Errorf("unexpected request: %s", r.URL.Path) |
| 59 | + w.WriteHeader(404) |
| 60 | + } |
| 61 | + }) |
| 62 | + |
| 63 | + discovery := a.discover(context.Background(), id) |
| 64 | + equal(t, len(arr(discovery["previews"])), 1) |
| 65 | + preview := obj(arr(discovery["previews"])[0]) |
| 66 | + equal(t, preview["error"], nil) |
| 67 | + if !strings.Contains(str(preview["warning"]), "缺少 data") { |
| 68 | + t.Fatalf("missing partial-source warning: %s", js(preview)) |
| 69 | + } |
| 70 | + plans := arr(preview["plans"]) |
| 71 | + equal(t, len(plans), 1) |
| 72 | + plan := obj(plans[0]) |
| 73 | + equal(t, plan["plan_id"], planID) |
| 74 | + equal(t, plan["name"], "Weekend Build") |
| 75 | + equal(t, plan["ends_at"], 300) |
| 76 | + entitlement := obj(arr(plan["entitlements"])[0]) |
| 77 | + equal(t, entitlement["grant_units"], 300000000) |
| 78 | + equal(t, entitlement["effective_at"], 200) |
| 79 | + |
| 80 | + dry, e := a.autoClaim(context.Background(), true) |
| 81 | + must(t, e) |
| 82 | + jobs := []any{} |
| 83 | + for _, v := range arr(dry["results"]) { |
| 84 | + if truth(obj(v)["would_claim"]) { |
| 85 | + jobs = append(jobs, v) |
| 86 | + } |
| 87 | + } |
| 88 | + equal(t, jobs, []any{M{"account": id, "plan_id": planID, "would_claim": true}}) |
| 89 | + equal(t, claims.Load(), 0) |
| 90 | + |
| 91 | + result, e := a.autoClaim(context.Background(), false) |
| 92 | + must(t, e) |
| 93 | + equal(t, result["claimed"], 1) |
| 94 | + equal(t, claims.Load(), 1) |
| 95 | + equal(t, a.rows("SELECT plan_id,trigger,ok FROM claim_log"), []any{M{"plan_id": planID, "trigger": "auto", "ok": 1}}) |
| 96 | +} |
| 97 | + |
| 98 | +func TestMarketingPreviewRequiresExplicitClaimAction(t *testing.T) { |
| 99 | + a := testApp(t) |
| 100 | + _, _, accounts := testPool(t, a, 1) |
| 101 | + delivery := marketingClaimDelivery("popup", "action-plan") |
| 102 | + content := obj(delivery["popup"]) |
| 103 | + obj(obj(obj(content["hero"])["args"])["zcode_plan"])["plan_id"] = "different-metadata-plan" |
| 104 | + metadataOnly := marketingClaimDelivery("banner", "metadata-only") |
| 105 | + obj(metadataOnly["banner"])["buttons"] = []any{M{"action": M{"type": "open_url", "args": M{"plan_id": "not-a-claim"}}}} |
| 106 | + installUpstream(t, a, func(w http.ResponseWriter, r *http.Request) { |
| 107 | + writeJSON(w, 200, M{"code": 0, "data": M{"deliveries": []any{metadataOnly, delivery}}}) |
| 108 | + }) |
| 109 | + preview, e := a.marketingPreview(context.Background(), accounts[0]) |
| 110 | + must(t, e) |
| 111 | + equal(t, preview["plans"], []any{M{"plan_id": "action-plan", "name": "action-plan", "entitlements": []any{}}}) |
| 112 | +} |
| 113 | + |
| 114 | +func TestClaimPreviewSourcesAndValidation(t *testing.T) { |
| 115 | + emptyBilling := M{"code": 0, "data": M{"plans": []any{}}} |
| 116 | + emptyMarketing := M{"code": 0, "data": M{"deliveries": []any{}}} |
| 117 | + billingCard := M{"code": 0, "data": M{"plans": []any{M{"plan_id": "legacy-card"}}}} |
| 118 | + marketingCard := M{"code": 0, "data": M{"deliveries": []any{marketingClaimDelivery("banner", "legacy-card")}}} |
| 119 | + for _, tc := range []struct { |
| 120 | + name string |
| 121 | + billing, marketing M |
| 122 | + count int |
| 123 | + warning, failure bool |
| 124 | + }{ |
| 125 | + {"empty lists", emptyBilling, emptyMarketing, 0, false, false}, |
| 126 | + {"legacy fallback", billingCard, M{"code": 500, "msg": "unavailable"}, 1, true, false}, |
| 127 | + {"deduplicate sources", billingCard, marketingCard, 1, false, false}, |
| 128 | + {"missing billing data", M{"code": 0}, emptyMarketing, 0, true, false}, |
| 129 | + {"invalid billing plans", M{"code": 0, "data": M{"plans": M{}}}, emptyMarketing, 0, true, false}, |
| 130 | + {"missing deliveries", emptyBilling, M{"code": 0, "data": M{}}, 0, true, false}, |
| 131 | + {"both unavailable", M{"code": 0}, M{"code": 0}, 0, false, true}, |
| 132 | + } { |
| 133 | + t.Run(tc.name, func(t *testing.T) { |
| 134 | + a := testApp(t) |
| 135 | + _, _, accounts := testPool(t, a, 1) |
| 136 | + installUpstream(t, a, func(w http.ResponseWriter, r *http.Request) { |
| 137 | + if r.URL.Path == "/api/v1/marketing/touch" { |
| 138 | + writeJSON(w, 200, tc.marketing) |
| 139 | + } else { |
| 140 | + writeJSON(w, 200, tc.billing) |
| 141 | + } |
| 142 | + }) |
| 143 | + preview, e := a.preview(context.Background(), accounts[0]) |
| 144 | + equal(t, e != nil, tc.failure) |
| 145 | + equal(t, truth(preview["warning"]), tc.warning) |
| 146 | + equal(t, len(arr(preview["plans"])), tc.count) |
| 147 | + }) |
| 148 | + } |
| 149 | +} |
0 commit comments