package cards import ( "fmt" "net/http" "net/http/httptest" "net/url" "strings" "testing" "git.t5de.dev/t5de/t5de-proxy/internal/proxy" ) func TestAPPlusBadge(t *testing.T) { asked := 0 imvu := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { asked++ id := strings.TrimPrefix(r.URL.Path, "/user/user-") fmt.Fprintf(w, `{"denormalized": {"https://api.imvu.com/user/user-%s": {"data": {"is_ap_plus": %v}}}}`, id, id == "5") })) defer imvu.Close() c := &Cards{Base: imvu.URL} card := func(cid, body string) string { t.Helper() u, _ := url.Parse("https://client-dynamic.imvu.com/api/avatarcard.php?cid=" + cid + "&viewer_cid=1") x := &proxy.Exchange{URL: u} if err := c.Intercept(x); err != nil || x.ModifyResponse == nil { t.Fatalf("not intercepted: %v", err) } out, err := x.ModifyResponse(&http.Response{StatusCode: 200}, []byte(body)) if err != nil { t.Fatal(err) } return string(out) } if out := card("5", `{"avatarname": "Ann", "show_ap": 1}`); !strings.Contains(out, `"show_ap_plus":1`) || !strings.Contains(out, `"avatarname":"Ann"`) { t.Fatalf("AP+ card %s", out) } if out := card("6", `{"avatarname": "Bo"}`); strings.Contains(out, "show_ap_plus") { t.Fatalf("no AP+ %s", out) } card("5", `{}`) if asked != 2 { t.Fatalf("IMVU asked %d times (kept for a while)", asked) } // Already set by IMVU: left alone, IMVU not asked. if out := card("7", `{"show_ap_plus": true}`); out != `{"show_ap_plus": true}` || asked != 2 { t.Fatalf("set already %s (%d)", out, asked) } }