Sorry, your request has been blocked due to unusual activity.
Request ID: 0bd6a2b417592xxxx
errors.aliyun.compackage gateway import ( "encoding/json" "io" "net/http" "net/http/httptest" "strings" "sync" "testing" "github.com/yetone/magpie/internal/provider" ) // countAsked is an upstream that counts tokens, noting each time it's asked. type countAsked struct { mu sync.Mutex paths []string } func (c *countAsked) ServeHTTP(w http.ResponseWriter, r *http.Request) { c.mu.Lock() c.paths = append(c.paths, r.URL.Path) c.mu.Unlock() w.Header().Set("Content-Type", "application/json") io.WriteString(w, `{"input_tokens":999}`) } func (c *countAsked) asked() []string { c.mu.Lock() defer c.mu.Unlock() return append([]string(nil), c.paths...) } func estimated(t *testing.T, rec *httptest.ResponseRecorder) int { t.Helper() var got struct { InputTokens int `json:"input_tokens"` } if err := json.Unmarshal(rec.Body.Bytes(), &got); rec.Code != 200 || err != nil || got.InputTokens == 0 { t.Fatalf("count_tokens: %d %s", rec.Code, rec.Body) } return got.InputTokens } // Claude Code asks count_tokens before each turn; ZCode itself never does, // and zcode.z.ai answers it with an error every time. A ZCode account's // count is estimated, the vendor not asked. func TestZCodeCountTokensEstimated(t *testing.T) { fresh(t) up := &countAsked{} srv := httptest.NewServer(up) t.Cleanup(srv.Close) p := provider.Provider{ID: "zcode", Name: "ZCode", Anthropic: srv.URL + "/api/v1/zcode-plan/anthropic", Models: []string{"glm-5.1"}, Account: &provider.Account{Agent: "zcode", User: "trial@example.com", Plan: "Start Plan"}} body := []byte(`{"model":"glm-5.1","messages":[{"role":"user","content":"hello there"}]}`) rec := httptest.NewRecorder() New().countOn(rec, httptest.NewRequest("POST", "/v1/messages/count_tokens", nil), p, "glm-5.1", true, body) if n := estimated(t, rec); n == 999 || len(up.asked()) > 0 { t.Fatalf("zcode.z.ai was asked to count: %v, %d", up.asked(), n) } } // ZCode moved onto its plugin estimates as the built-in does. func TestMovedZCodeCountTokensEstimated(t *testing.T) { up := &countAsked{} movedFake(t, "zcode", up) rec := httptest.NewRecorder() New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/messages/count_tokens", strings.NewReader(`{"model":"zcode/fake-claude","messages":[{"role":"user","content":"hello there"}]}`))) if n := estimated(t, rec); n == 999 || len(up.asked()) > 0 { t.Fatalf("the vendor was asked to count: %v, %d", up.asked(), n) } } // edgeBlockPage is Alibaba Cloud's ESA block page, as zcode.z.ai served it. const edgeBlockPage = `
Request ID: 0bd6a2b417592xxxx
errors.aliyun.com