Download internal/upstream/report_test.go from a3216/gcli2api: direct link, hf CLI and curl.
- Browser
- Download file 2.23 kB
-
https://huggingface.co/spaces/a3216/gcli2api/resolve/main/internal/upstream/report_test.go
- Command line
-
hf download hf://spaces/a3216/gcli2api/internal/upstream/report_test.go
-
curl -L -o report_test.go https://huggingface.co/spaces/a3216/gcli2api/resolve/main/internal/upstream/report_test.go
2.23 kB
| package upstream | |
| import ( | |
| "encoding/json" | |
| "io" | |
| "net/http" | |
| "net/http/httptest" | |
| "testing" | |
| "github.com/linguo2625469/workbuddy2api-panel/internal/auth" | |
| ) | |
| // TestReportChatActivitySendsArrayWithUserID 断言出站 body 是数组、含 userId、eventCode 正确。 | |
| func TestReportChatActivitySendsArrayWithUserID(t *testing.T) { | |
| var got []map[string]any | |
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| if r.URL.Path != "/v2/report" { | |
| t.Errorf("path=%s want /v2/report", r.URL.Path) | |
| } | |
| if r.Method != http.MethodPost { | |
| t.Errorf("method=%s want POST", r.Method) | |
| } | |
| raw, _ := io.ReadAll(r.Body) | |
| var arr []map[string]any | |
| if err := json.Unmarshal(raw, &arr); err != nil { | |
| t.Fatalf("body must be a JSON array: %v (body=%s)", err, raw) | |
| } | |
| got = arr | |
| w.Write([]byte(`{"code":0,"msg":"OK"}`)) | |
| })) | |
| defer srv.Close() | |
| c := &Client{HTTP: srv.Client(), BillingBaseCN: srv.URL} | |
| if err := c.ReportChatActivity(&auth.Auth{AccessToken: "at", UID: "u-active"}, "wb2api-123", ""); err != nil { | |
| t.Fatalf("report: %v", err) | |
| } | |
| if len(got) != 1 { | |
| t.Fatalf("events=%d want 1", len(got)) | |
| } | |
| ev := got[0] | |
| if ev["eventCode"] != "chat_request_send" { | |
| t.Errorf("eventCode=%v want chat_request_send", ev["eventCode"]) | |
| } | |
| if ev["userId"] != "u-active" { | |
| t.Errorf("userId=%v want u-active(缺失则服务端 200 但静默丢弃)", ev["userId"]) | |
| } | |
| if ev["conversationId"] != "wb2api-123" { | |
| t.Errorf("conversationId=%v want wb2api-123", ev["conversationId"]) | |
| } | |
| if ev["mode"] != "craft" { | |
| t.Errorf("mode=%v want craft", ev["mode"]) | |
| } | |
| // 出站 body 必须是数组(以 [ 开头),不是单个对象。 | |
| } | |
| // TestReportChatActivityServerError 业务 code 非 0 返回 *Error。 | |
| func TestReportChatActivityServerError(t *testing.T) { | |
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| w.WriteHeader(500) | |
| w.Write([]byte(`boom`)) | |
| })) | |
| defer srv.Close() | |
| c := &Client{HTTP: srv.Client(), BillingBaseCN: srv.URL} | |
| err := c.ReportChatActivity(&auth.Auth{AccessToken: "at", UID: "u1"}, "cid", "") | |
| if err == nil { | |
| t.Fatal("want error on 500") | |
| } | |
| } | |