gcli2api / internal /server /gates_test.go
a3216's picture
feat: 同步上游 320f5d4;扩展模型(多免费渠道/自定义映射/访问密钥/模型白名单)
e48a2d8
Raw History Blame Contribute Delete
2.02 kB
package server
import (
"reflect"
"testing"
"github.com/misakano7545/workbuddy2api-panel/internal/upstream"
)
// 闸门只允许产生「假阳性」(多解析一次);结果必须与无条件解析逐项相等。
func TestToolNamesGateEquivalence(t *testing.T) {
bodies := []string{
`{"model":"m","messages":[]}`,
`{"model":"m","messages":[{"role":"user","content":"hi"}]}`,
`{"model":"m","tools":[{"type":"function","function":{"name":"read_file"}}]}`,
`{"model":"m","tools":[{"name":"flat_name"}]}`,
`{"model":"m","messages":[{"role":"assistant","tool_calls":[{"function":{"name":"bash"}}]}]}`,
`{"model":"m","messages":[{"role":"assistant","function_call":{"name":"legacy"}}]}`,
`{"model":"m","functions":[{"name":"legacy_fn"}]}`,
`{"model":"m","tool_choice":"auto","messages":[]}`,
`{"model":"m","messages":[{"role":"user","content":"mention tool in text"}]}`,
`not json`,
``,
}
for _, body := range bodies {
want := upstream.ToolNameAllowlist([]byte(body))
got := toolNamesIfPresent([]byte(body))
if !reflect.DeepEqual(got, want) {
t.Errorf("gate mismatch for %s: got=%v want=%v", body, got, want)
}
}
}
func TestImagePartGateEquivalence(t *testing.T) {
bodies := []string{
`{"model":"m","messages":[{"role":"user","content":"hi"}]}`,
`{"model":"m","messages":[{"role":"user","content":[{"type":"text","text":"hi"}]}]}`,
`{"model":"m","messages":[{"role":"user","content":[{"type":"image_url","image_url":{"url":"https://x/1.png"}}]}]}`,
`{"model":"m","messages":[{"role":"user","content":[{"type":"image_url","image_url":"https://x/1.png"}]}]}`,
// 非 content part 里出现 image_url 键:闸门假阳性,结果仍须一致(false)。
`{"model":"m","metadata":{"image_url":"https://x/1.png"},"messages":[]}`,
`not json`,
``,
}
for _, body := range bodies {
want := hasImagePart([]byte(body))
got := imagePartIfPresent([]byte(body))
if got != want {
t.Errorf("gate mismatch for %s: got=%v want=%v", body, got, want)
}
}
}