mirror of
https://gitcode.com/JianFeeeee/ModelRouter.git
synced 2026-11-07 09:47:23 +00:00
产品代码统一为纯文本风格(照参考实现),去掉 emoji:billing/quota/usage 插件、 channels 的提示文案、WebUI 文案,以及 go 测试、docs、packaging 脚本中的 注意/OK/FAIL 等符号化写法。第三方目录(vendor、node_modules、.probe)不动。 另:packaging/upload-release.sh 的仓库名改为真实全名 JianFeeeee/ModelRouter → HeNanMU-EEA/ModelRouter(前者是重定向,POST/PUT 不跟随 会 404)。
74 lines
2.6 KiB
Go
74 lines
2.6 KiB
Go
package core
|
||
|
||
import (
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
// TestDevicePollMutants 是 device-poll 执行器的变异测试。
|
||
//
|
||
// 用户要求:移植要照参考实现的行为形成测试项,且要变异验证测试真的能杀掉
|
||
// 「行为走样」的代码。每个用例把 channels 源里的一个决策分支做语义变异,
|
||
// 断言决策表**会失败**(变异体被杀)。变异体幸存 = 该分支没有覆盖。
|
||
func TestDevicePollMutants(t *testing.T) {
|
||
src, err := readBundledChannels(t)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
|
||
// 锚点会命中 deviceOneStep 里两处同形代码(workos 分支 + minimax 分支),
|
||
// 全部替换:两个决策表(minimax + workos)都会杀,任一幸存即失败。
|
||
muts := []struct {
|
||
name string
|
||
from string
|
||
to string
|
||
}{
|
||
{
|
||
name: "slow_down 不再加 interval",
|
||
from: `sess.device_interval = (sess.device_interval or d.interval or 5) + step`,
|
||
to: `sess.device_interval = (sess.device_interval or d.interval or 5)`,
|
||
},
|
||
{
|
||
name: "denied 被当成继续等待",
|
||
from: `if resp.status == 200 and (status == "denied" or status == "access_denied") then`,
|
||
to: `if resp.status == 200 and (status == "denied_x" or status == "access_denied") then`,
|
||
},
|
||
{
|
||
name: "expired_token 不被识别",
|
||
from: `if resp.status == 200 and (status == "expired" or status == "expired_token") then`,
|
||
to: `if resp.status == 200 and (status == "expired" or status == "expired_token_x") then`,
|
||
},
|
||
{
|
||
name: "authorization_pending 被当成失败",
|
||
from: `if ecode == "authorization_pending" then return false end`,
|
||
to: `if ecode == "authorization_pending_x" then return false end`,
|
||
},
|
||
{
|
||
name: "成功分支被禁用",
|
||
from: `if resp.status == 200 and rec.access_token then`,
|
||
to: `if resp.status == 200 and rec.access_token and false then`,
|
||
},
|
||
}
|
||
|
||
for _, mu := range muts {
|
||
t.Run(mu.name, func(t *testing.T) {
|
||
if !strings.Contains(src, mu.from) {
|
||
t.Fatalf("锚点丢失,先更新实现与锚点:%q", mu.from)
|
||
}
|
||
// 锚点在 deviceOneStep 的 workos/minimax 两个分支各有一份同形代码,
|
||
// 全部替换:任一分支的变异体被任一决策表杀掉才算有覆盖。
|
||
mutated := strings.ReplaceAll(src, mu.from, mu.to)
|
||
if mutated == src {
|
||
t.Fatal("变异未生效")
|
||
}
|
||
// 基准必须全过(测试自身有效),变异体必须被杀(有覆盖)
|
||
if !newDeviceHarness(t, src).runDecisionTable() {
|
||
t.Fatal("基准决策表未全过 —— 测试自身失效")
|
||
}
|
||
if newDeviceHarness(t, mutated).runDecisionTable() {
|
||
t.Fatalf("变异体幸存(%s)—— 该分支无覆盖", mu.name)
|
||
}
|
||
})
|
||
}
|
||
}
|