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) } }) } }