File size: 13,209 Bytes
e48a2d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package server

import (
	"encoding/json"
	"io"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"github.com/misakano7545/workbuddy2api-panel/internal/auth"
	"github.com/misakano7545/workbuddy2api-panel/internal/upstream"
)

// TestFetchEndpoint POST /v1/fetch:DSH 的 DeepSeek Fetch API 形状——
// 请求 {url},回 {content}(上游原文的 title/url 一并保留)。
func TestFetchEndpoint(t *testing.T) {
	const upstreamBody = `{"url":"https://example.com/a","title":"示例","content":"# 标题\n\n正文"}`
	f := &searchFake{status: 200, body: upstreamBody}
	p := testPoolWith(&auth.Auth{UID: "c1", AccessToken: "at", ExpiresAt: 9999999999, Domain: "www.codebuddy.cn"})
	h := NewHandler(Config{Pool: p, Upstream: f.client()})

	rec := httptest.NewRecorder()
	h.ServeHTTP(rec, httptest.NewRequest("POST", "/v1/fetch",
		strings.NewReader(`{"url":"https://example.com/a"}`)))
	if rec.Code != 200 {
		t.Fatalf("code=%d body=%s", rec.Code, rec.Body.String())
	}
	var got map[string]any
	if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil {
		t.Fatalf("响应不是 JSON: %v (%s)", err, rec.Body.String())
	}
	if got["content"] != "# 标题\n\n正文" {
		t.Fatalf("content 透传不符: %v", got["content"])
	}
	if got["title"] != "示例" || got["url"] != "https://example.com/a" {
		t.Fatalf("其余字段应原样保留: %v", got)
	}
	if f.paths[0] != "/agenttool/v1/webfetch" {
		t.Fatalf("落点应为 webfetch: %v", f.paths)
	}
	var sent map[string]any
	_ = json.Unmarshal([]byte(f.bodies[0]), &sent)
	if sent["url"] != "https://example.com/a" {
		t.Fatalf("出站 url 不符: %v", sent)
	}
	if ct := rec.Header().Get("Content-Type"); !strings.Contains(ct, "application/json") {
		t.Fatalf("Content-Type=%q", ct)
	}
}

// TestFetchValidation 本地先拦:缺 url / 非 http(s) 一律 400 且不打上游。
func TestFetchValidation(t *testing.T) {
	f := &searchFake{status: 200, body: `{"content":"x"}`}
	p := testPoolWith(&auth.Auth{UID: "c1", AccessToken: "at", ExpiresAt: 9999999999, Domain: "www.codebuddy.cn"})
	h := NewHandler(Config{Pool: p, Upstream: f.client()})
	for _, body := range []string{
		`{}`,
		`{"url":"  "}`,
		`{"url":"file:///etc/passwd"}`,
		`{"url":"not-a-url"}`,
	} {
		rec := httptest.NewRecorder()
		h.ServeHTTP(rec, httptest.NewRequest("POST", "/v1/fetch", strings.NewReader(body)))
		if rec.Code != http.StatusBadRequest {
			t.Errorf("body=%s code=%d, want 400 (%s)", body, rec.Code, rec.Body)
		}
	}
	if len(f.paths) != 0 {
		t.Fatalf("校验失败不应打上游,实际 %d 次", len(f.paths))
	}
}

// TestFetchUpstreamShape 上游 200 但没有 string content → 502,不伪造空正文。
func TestFetchUpstreamShape(t *testing.T) {
	for _, body := range []string{`{"url":"https://x"}`, `{"content":null}`, `{"content":123}`, `not json`} {
		f := &searchFake{status: 200, body: body}
		p := testPoolWith(&auth.Auth{UID: "c1", AccessToken: "at", ExpiresAt: 9999999999, Domain: "www.codebuddy.cn"})
		h := NewHandler(Config{Pool: p, Upstream: f.client()})
		rec := httptest.NewRecorder()
		h.ServeHTTP(rec, httptest.NewRequest("POST", "/v1/fetch",
			strings.NewReader(`{"url":"https://x"}`)))
		if rec.Code != http.StatusBadGateway {
			t.Errorf("upstream=%s code=%d, want 502 (%s)", body, rec.Code, rec.Body)
		}
		if strings.Contains(rec.Body.String(), `"content":""`) {
			t.Errorf("不应把坏响应伪造成空正文: %s", rec.Body)
		}
	}
}

// TestFetchRequestLevelFailFast 上游 400(url 校验)→ 原文透传、只打一次、不轮转。
func TestFetchRequestLevelFailFast(t *testing.T) {
	f := &searchFake{status: 400, body: `{"code":15013,"msg":"url is required"}`}
	p := testPoolWith(
		&auth.Auth{UID: "c1", AccessToken: "at", ExpiresAt: 9999999999, Domain: "www.codebuddy.cn"},
		&auth.Auth{UID: "c2", AccessToken: "at", ExpiresAt: 9999999999, Domain: "www.codebuddy.cn"},
	)
	h := NewHandler(Config{Pool: p, Upstream: f.client()})
	rec := httptest.NewRecorder()
	h.ServeHTTP(rec, httptest.NewRequest("POST", "/v1/fetch",
		strings.NewReader(`{"url":"https://x"}`)))
	if rec.Code != http.StatusBadRequest || !strings.Contains(rec.Body.String(), "15013") {
		t.Fatalf("请求级 400 应原文透传,得到 %d %s", rec.Code, rec.Body.String())
	}
	if len(f.paths) != 1 {
		t.Fatalf("请求级错误应 fail-fast 不轮转,实际打了 %d 次", len(f.paths))
	}
}

// TestFetchFallsBackToGlobalOnDomainRejected 2026-10-31 实案:CN 域 edgebrowser 的
// 域名路由策略拒绝 Google 系域名(403 code=15021),global 域同一域名正常。
// 换遍全池 CN 号都是同一个 403(域名级),只有换域有救 → /v1/fetch 必须自动改域。
func TestFetchFallsBackToGlobalOnDomainRejected(t *testing.T) {
	var hosts []string
	f := &upstream.Client{
		HTTP: &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
			hosts = append(hosts, r.URL.Host)
			status, body := http.StatusForbidden,
				`{"code":15021,"msg":"edgebrowser: API error: domain rejected by via routing policy"}`
			if r.URL.Host == "global.example" {
				status, body = http.StatusOK,
					`{"url":"https://android.googlesource.com/x","title":"t","content":"# ok"}`
			}
			return &http.Response{
				StatusCode: status,
				Header:     http.Header{"Content-Type": []string{"application/json"}},
				Body:       io.NopCloser(strings.NewReader(body)),
			}, nil
		})},
		GlobalEnabled:  true,
		ChatBaseCN:     "https://cn.example",
		ChatBaseGlobal: "https://global.example",
	}
	p := testPoolWith(
		&auth.Auth{UID: "c1", AccessToken: "at", ExpiresAt: 9999999999, Domain: "www.codebuddy.cn"},
		&auth.Auth{UID: "g1", AccessToken: "at", ExpiresAt: 9999999999, Domain: "www.workbuddy.ai"},
	)
	h := NewHandler(Config{Pool: p, Upstream: f})
	rec := httptest.NewRecorder()
	h.ServeHTTP(rec, httptest.NewRequest("POST", "/v1/fetch",
		strings.NewReader(`{"url":"https://android.googlesource.com/x"}`)))
	if rec.Code != 200 {
		t.Fatalf("应跨域成功,得到 %d %s", rec.Code, rec.Body.String())
	}
	if !strings.Contains(rec.Body.String(), `"content":"# ok"`) {
		t.Fatalf("content 不符: %s", rec.Body.String())
	}
	if len(hosts) != 2 || hosts[0] != "cn.example" || hosts[1] != "global.example" {
		t.Fatalf("应先 CN 后 global,实际 hosts=%v", hosts)
	}
}

// TestFetchNoCrossRealmOnOtherErrors 只有「域名被策略拒绝」才跨域:
// 5xx / 普通 4xx 跨域重试毫无意义(两域积分、账号互不相通),不该白烧 global 号。
func TestFetchNoCrossRealmOnOtherErrors(t *testing.T) {
	var hosts []string
	f := &upstream.Client{
		HTTP: &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
			hosts = append(hosts, r.URL.Host)
			return &http.Response{
				StatusCode: http.StatusInternalServerError,
				Header:     http.Header{"Content-Type": []string{"application/json"}},
				Body:       io.NopCloser(strings.NewReader(`{"code":10000,"msg":"boom"}`)),
			}, nil
		})},
		GlobalEnabled:  true,
		ChatBaseCN:     "https://cn.example",
		ChatBaseGlobal: "https://global.example",
	}
	p := testPoolWith(
		&auth.Auth{UID: "c1", AccessToken: "at", ExpiresAt: 9999999999, Domain: "www.codebuddy.cn"},
		&auth.Auth{UID: "g1", AccessToken: "at", ExpiresAt: 9999999999, Domain: "www.workbuddy.ai"},
	)
	h := NewHandler(Config{Pool: p, Upstream: f})
	rec := httptest.NewRecorder()
	h.ServeHTTP(rec, httptest.NewRequest("POST", "/v1/fetch",
		strings.NewReader(`{"url":"https://example.com/a"}`)))
	if rec.Code == 200 {
		t.Fatalf("5xx 不该成功: %s", rec.Body.String())
	}
	for _, host := range hosts {
		if host != "cn.example" {
			t.Fatalf("5xx 不该跨域重试,实际 hosts=%v", hosts)
		}
	}
}

// TestFetchDownloadHint 下载类 URL(gitiles ?format=TEXT)连 global 域都救不了:
// 上游浏览器回 422「Browser does not support download functionality」。此时应把
// 提示改成可操作的建议(换 HTML 页面地址),而不是把上游内部文案原样甩给用户。
func TestFetchDownloadHint(t *testing.T) {
	f := &upstream.Client{
		HTTP: &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
			status, body := http.StatusForbidden,
				`{"code":15021,"msg":"edgebrowser: API error: domain rejected by via routing policy"}`
			if r.URL.Host == "global.example" {
				status, body = http.StatusInternalServerError,
					`{"code":10000,"msg":"search post: unexpected status 422: Browser does not support download functionality"}`
			}
			return &http.Response{
				StatusCode: status,
				Header:     http.Header{"Content-Type": []string{"application/json"}},
				Body:       io.NopCloser(strings.NewReader(body)),
			}, nil
		})},
		GlobalEnabled:  true,
		ChatBaseCN:     "https://cn.example",
		ChatBaseGlobal: "https://global.example",
	}
	p := testPoolWith(
		&auth.Auth{UID: "c1", AccessToken: "at", ExpiresAt: 9999999999, Domain: "www.codebuddy.cn"},
		&auth.Auth{UID: "g1", AccessToken: "at", ExpiresAt: 9999999999, Domain: "www.workbuddy.ai"},
	)
	h := NewHandler(Config{Pool: p, Upstream: f})
	rec := httptest.NewRecorder()
	h.ServeHTTP(rec, httptest.NewRequest("POST", "/v1/fetch",
		strings.NewReader(`{"url":"https://android.googlesource.com/x?format=TEXT"}`)))
	if rec.Code != http.StatusUnprocessableEntity {
		t.Fatalf("code=%d, want 422 (%s)", rec.Code, rec.Body.String())
	}
	if !strings.Contains(rec.Body.String(), "HTML") || !strings.Contains(rec.Body.String(), "format=TEXT") {
		t.Fatalf("应给出换 HTML 地址的可操作提示: %s", rec.Body.String())
	}
}

// TestFetchInRealmNoPickReturnsError 契约:fetchInRealm 一个号都没选到时必须返回
// 非 nil 错误(而不是 nil)。跨域兜底会把该返回值覆盖到 lastErr 上,nil 会让
// fetch 末尾的 lastErr.Error() 空指针 panic。
func TestFetchInRealmNoPickReturnsError(t *testing.T) {
	f := &searchFake{status: 200, body: `{"content":"x"}`}
	// 池里只有 CN 号;对 realm=global 的取号必然选不到任何账号。
	p := testPoolWith(&auth.Auth{UID: "c1", AccessToken: "at", ExpiresAt: 9999999999, Domain: "www.codebuddy.cn"})
	h := NewHandler(Config{Pool: p, Upstream: f.client()})
	lastErr, handled := h.fetchInRealm(httptest.NewRecorder(), httptest.NewRequest("POST", "/v1/fetch", nil),
		"https://example.com/a", "global", map[string]bool{})
	if handled {
		t.Fatalf("选不到号不应写响应")
	}
	if lastErr == nil {
		t.Fatalf("选不到号必须返回显式错误,nil 会在跨域兜底路径触发空指针 panic")
	}
	if !strings.Contains(lastErr.Error(), "没有可用的 global 账号") {
		t.Fatalf("错误文案不符: %v", lastErr)
	}
}

// TestFetchGlobalAccountDisabledSkipsFallback global 号不可用时跳过跨域兜底:
// 主域 15021 的 403 原样透传(与无兜底路径一致),CN 号零惩罚。
func TestFetchGlobalAccountDisabledSkipsFallback(t *testing.T) {
	f := &searchFake{status: http.StatusForbidden,
		body: `{"code":15021,"msg":"edgebrowser: API error: domain rejected by via routing policy"}`}
	g := &auth.Auth{UID: "g1", AccessToken: "at", ExpiresAt: 9999999999, Domain: "www.workbuddy.ai"}
	p := testPoolWith(
		&auth.Auth{UID: "c1", AccessToken: "at", ExpiresAt: 9999999999, Domain: "www.codebuddy.cn"},
		g,
	)
	p.Disable("g1", "test")
	h := NewHandler(Config{Pool: p, Upstream: f.client()})
	rec := httptest.NewRecorder()
	h.ServeHTTP(rec, httptest.NewRequest("POST", "/v1/fetch",
		strings.NewReader(`{"url":"https://android.googlesource.com/x"}`)))
	if rec.Code != http.StatusForbidden {
		t.Fatalf("code=%d want 403 原样透传 (%s)", rec.Code, rec.Body.String())
	}
	if !strings.Contains(rec.Body.String(), "15021") {
		t.Fatalf("应保留上游 15021 错误文案: %s", rec.Body.String())
	}
	st, _ := p.Status("c1")
	if st.Cooling || st.Disabled || st.ConsecutiveFails != 0 {
		t.Errorf("15021 不得罚 CN 账号: %+v", st)
	}
}

// TestFetchDomainRejectedDoesNotPenalizeAccounts 15021 是域名级策略拒绝(同域所有
// 账号同一结果),不是账号问题。若按 ErrClient 喂连败,连抓几个 Google 文档就会把
// 整个 CN 池逐个降到出池——这里连打 6 轮(> degradeThreshold=5)断言账号零惩罚。
func TestFetchDomainRejectedDoesNotPenalizeAccounts(t *testing.T) {
	f := &searchFake{status: http.StatusForbidden,
		body: `{"code":15021,"msg":"edgebrowser: API error: domain rejected by via routing policy"}`}
	p := testPoolWith(
		&auth.Auth{UID: "c1", AccessToken: "at", ExpiresAt: 9999999999, Domain: "www.codebuddy.cn"},
		&auth.Auth{UID: "c2", AccessToken: "at", ExpiresAt: 9999999999, Domain: "www.codebuddy.cn"},
	)
	h := NewHandler(Config{Pool: p, Upstream: f.client()})
	for i := 1; i <= 6; i++ {
		rec := httptest.NewRecorder()
		h.ServeHTTP(rec, httptest.NewRequest("POST", "/v1/fetch",
			strings.NewReader(`{"url":"https://www.google.com/"}`)))
		if rec.Code != http.StatusForbidden {
			t.Fatalf("run %d: code=%d want 403 (%s)", i, rec.Code, rec.Body)
		}
	}
	for _, uid := range []string{"c1", "c2"} {
		st, _ := p.Status(uid)
		if st.Cooling || st.Disabled || st.ConsecutiveFails != 0 || !st.DegradeUntil.IsZero() {
			t.Errorf("%s: 15021 域名级拒绝不得罚账号: %+v", uid, st)
		}
	}
}