package api import ( "context" "encoding/json" "net/http" "net/http/httptest" "testing" "git.ipao.vip/rogee/creator-hub/internal/creator" hub "git.ipao.vip/rogee/creator-hub/internal/environment" xiaohongshu "git.ipao.vip/rogee/creator-hub/internal/platform/xiaohongshu" ) const testXiaohongshuIdentityURL = "https://edith.xiaohongshu.com/api/sns/web/v2/user/me" func TestXiaohongshuGatewayBrowserFencesAccountGeneration(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { if request.Header.Get("Authorization") != "Bearer gateway-token-1" { t.Fatal("missing gateway authorization") } var body map[string]any if json.NewDecoder(request.Body).Decode(&body) != nil || body["binding_version"] != float64(2) || body["runtime_id"] != "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" || body["network_id"] != "native-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" || body["network_exit_id"] != "exit-a" { t.Fatalf("generation fence missing: %#v", body) } if request.URL.Path != "/v1/browsers/account-a/xiaohongshu/get" || body["url"] != testXiaohongshuIdentityURL { t.Fatalf("unexpected request: path=%s body=%#v", request.URL.Path, body) } _ = json.NewEncoder(response).Encode(map[string]any{"status": 200, "body": `{"success":true}`, "challenge": ""}) })) defer server.Close() browser := xiaohongshuGatewayBrowser{gateway: hub.Gateway{Endpoint: server.URL, Token: "gateway-token-1"}, environment: readyDouyinEnvironment()} result, err := browser.Get(context.Background(), testXiaohongshuIdentityURL) if err != nil || result.Status != 200 || string(result.Body) != `{"success":true}` { t.Fatalf("unexpected result: %#v err=%v", result, err) } } func TestXiaohongshuGatewayBrowserResolvesShareLinks(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { if request.URL.Path != "/v1/browsers/account-a/xiaohongshu/resolve" { t.Fatalf("unexpected path: %s", request.URL.Path) } _ = json.NewEncoder(response).Encode(map[string]any{"url": "https://www.xiaohongshu.com/explore/n-1"}) })) defer server.Close() browser := xiaohongshuGatewayBrowser{gateway: hub.Gateway{Endpoint: server.URL, Token: "gateway-token-1"}, environment: readyDouyinEnvironment()} resolved, err := browser.Resolve(context.Background(), "https://xhslink.com/a/abc") if err != nil || resolved != "https://www.xiaohongshu.com/explore/n-1" { t.Fatalf("resolved URL=%q err=%v", resolved, err) } } func TestNewDouyinCollectorResolvesTargetIdentitySeparately(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { var body map[string]any if json.NewDecoder(request.Body).Decode(&body) != nil { t.Fatal("invalid gateway request") } switch request.URL.Path { case "/v1/browsers/account-a/douyin/identity": if body["expected_account_key"] != "viewer-1" { t.Fatalf("viewer identity key=%#v", body["expected_account_key"]) } _ = json.NewEncoder(response).Encode(map[string]any{"uid": "viewer-1", "sec_uid": "viewer-sec", "unique_id": "viewer"}) case "/v1/browsers/account-a/douyin/get": if body["url"] != "https://www.douyin.com/aweme/v1/web/user/profile/other/?aid=6383&device_platform=webapp&user_id=2328120603967913" { t.Fatalf("target profile URL=%#v", body["url"]) } _ = json.NewEncoder(response).Encode(map[string]any{"status": 200, "body": `{"status_code":0,"user":{"uid":"2328120603967913","sec_uid":"target-sec","unique_id":"target"}}`, "challenge": ""}) default: t.Fatalf("unexpected path: %s", request.URL.Path) } })) defer server.Close() collector, target, err := newCreatorCollector(context.Background(), creator.PlatformDouyin, hub.Gateway{Endpoint: server.URL, Token: "gateway-token-1"}, readyDouyinEnvironment(), "viewer-1", "2328120603967913", "", creator.SourceCompetitor, "source-1") if err != nil || collector == nil || target != "target-sec" { t.Fatalf("new Douyin collector: collector=%#v target=%q err=%v", collector, target, err) } } func TestNewXiaohongshuCollectorKeepsViewerAndTargetSeparate(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { if request.URL.Path != "/v1/browsers/account-a/xiaohongshu/identity" { t.Fatalf("unexpected path: %s", request.URL.Path) } _ = json.NewEncoder(response).Encode(map[string]any{"uid": "viewer-1"}) })) defer server.Close() collector, target, err := newCreatorCollector(context.Background(), creator.PlatformXiaohongshu, hub.Gateway{Endpoint: server.URL, Token: "gateway-token-1"}, readyDouyinEnvironment(), "viewer-1", "target-1", "https://www.xiaohongshu.com/user/profile/target-1?xsec_source=pc_search", creator.SourceCompetitor, "source-1") if err != nil { t.Fatalf("new collector: %v", err) } xhsCollector, ok := collector.(*xiaohongshu.Collector) if !ok || xhsCollector.AccountKey != "target-1" || target != "target-1" || xhsCollector.HomepageURL == "" { t.Fatalf("collector=%#v target=%q", collector, target) } } func TestXiaohongshuGatewayBrowserPostCarriesJSONBody(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { var body map[string]any if json.NewDecoder(request.Body).Decode(&body) != nil || body["url"] != "https://so.xiaohongshu.com/api/sns/web/v2/search/notes" { t.Fatalf("unexpected request body: %#v", body) } if _, ok := body["body"].(map[string]any); !ok { t.Fatalf("missing nested request body: %#v", body) } _ = json.NewEncoder(response).Encode(map[string]any{"status": 200, "body": `{"success":true}`, "challenge": ""}) })) defer server.Close() browser := xiaohongshuGatewayBrowser{gateway: hub.Gateway{Endpoint: server.URL, Token: "gateway-token-1"}, environment: readyDouyinEnvironment()} if _, err := browser.Post(context.Background(), "https://so.xiaohongshu.com/api/sns/web/v2/search/notes", []byte(`{"keyword":"x"}`)); err != nil { t.Fatalf("post failed: %v", err) } }