mirror of
https://gitcode.com/JianFeeeee/ModelRouter.git
synced 2026-11-12 12:17:23 +00:00
## 真实缺陷(v2.0.33 上线后在截图里看到) 日志尾显示成:`能力增强不�` 根因:`tostring(cerr):sub(1, 120)` —— **Lua 的 sub 按字节切**,而中文占 3 字节 ⇒ 切在字符中间产生**替换字符 U+FFFD**。一句本来完整的诊断("…能力增强不可用, 已退回兜底表")看起来像**数据损坏**,实际只是截断方式错。 生产实测复现:该条 120 字节截断后,末尾正好落在"不"字的第 1 字节上, python 侧解码得到 `…能力增强不\ufffd`。**修法**:`plugin.utf8Clip(s, maxBytes)` 先按字节切、再回退到最后一个完整 UTF-8 序列边界(最多退 2 字节)。 三处中文文案截断全部改用它。 ## 判据(1 条静态 + 1 条行为 + 变异 1/1) - `TestLogTruncationIsUTF8Safe`(静态):助手存在 / 三处中文截断不再用裸 sub / 未超长时原样返回 / 回退逻辑处理孤立首字节 - `TestUtf8ClipBehaviour`(**行为**,经 `plugin.__utf8_clip_probe` 走真实函数): 对 6 个长度断言 `utf8.ValidString(out)` 且不超字节上限。静态断言看不出 "结果是否真的合法 UTF-8",所以必须有这一条 —— 实测 n=120 从 `…能力增强不�`(非法)变为 `…能力增强不`(合法) - 变异:把 utf8Clip 换回裸 sub → 报红 ## 排版抛光(截图里可见的三处) - 头部控件原用 `<h2>` + flex,checkbox 被挤到换行且文字竖排 ⇒ 改 `<b>` + 独立 flex 行 + `white-space:nowrap` - 长诊断在窄卡片里被**截断**(而截断的尾巴正是最该看的部分)⇒ `word-break` - 列表加 `line-height`,可读性 ## 兼容性 历史 state 里的 41 条老条目没有 `level` 字段 —— 前端 `e.level || "info"` 已容忍,**不需要数据迁移**(判据亦未假设字段必然存在)。
191 lines
7.6 KiB
Go
191 lines
7.6 KiB
Go
package core
|
||
|
||
import (
|
||
"regexp"
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
// ★ 插件日志面板:级别 + 归属 + 同文折叠(否则用户动作被 96% 的周期噪音淹没)。
|
||
//
|
||
// 真实问题(生产实测):50 条日志里 **48 条(96%)** 是三个定时任务
|
||
// (refresh_models 2h / auto_claim 2h / refresh_creds 0.5h,全为 first_due_now)
|
||
// 的重复回音,唯一模式只有 8 种 ⇒ 约 2 小时就把 50 条环形缓冲冲满一轮,
|
||
// **真正的用户动作**(如「账号删除」)被挤到看不见。
|
||
//
|
||
// 三个结构性修法,每个都在源码里可断言:
|
||
//
|
||
// ① logEvent 支持 level(task/warn/error),前端默认折叠 task
|
||
// ② logEvent 支持 platform,前端按选中渠道过滤(修"点 buddy 看到 trae"的归属错误)
|
||
// ③ 同文折叠:紧邻同文不新增条目,只 n=n+1
|
||
func TestChannelsLogEventCarriesLevelAndPlatform(t *testing.T) {
|
||
src, err := readBundledChannels(t)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
s := string(src)
|
||
|
||
// ① 签名必须收 level 与 platform(三参)
|
||
if !regexp.MustCompile(`local function logEvent\(msg, level, platform\)`).MatchString(s) {
|
||
t.Error("logEvent 必须接收 (msg, level, platform) —— 否则级别与渠道归属都无从表达")
|
||
}
|
||
// ② 写入的条目必须带上这两个字段
|
||
for _, field := range []string{"level = level or", "platform = platform"} {
|
||
if !strings.Contains(s, field) {
|
||
t.Errorf("日志条目必须写入 %s", field)
|
||
}
|
||
}
|
||
// ③ 同文折叠:紧邻同文时只加计数
|
||
if !strings.Contains(s, "top.n = (top.n or 1) + 1") {
|
||
t.Error("必须实现紧邻同文折叠(否则周期任务的重复回音会把 50 条缓冲冲满)")
|
||
}
|
||
// ④ 折叠必须只针对**紧邻**条目 —— 否则一条每小时出现的消息会不断跳到最前,
|
||
// 时间线失去意义。断言比较对象是 log[1](栈顶)而不是任意条目。
|
||
if !strings.Contains(s, "local top = log[1]") {
|
||
t.Error("折叠必须只比较**紧邻**的上一条(log[1]);对任意条目折叠会让时间线错乱")
|
||
}
|
||
t.Log("✓ logEvent 具备 level / platform / 紧邻同文折叠")
|
||
}
|
||
|
||
// ★ 周期任务的**常规**结果必须是 task 级(默认折叠),异常必须是 warn/error。
|
||
//
|
||
// 判据咬的是"这些具体调用点被降级了",而不是"存在 task 这个词" ——
|
||
// 后者只要有一处就为真,测不出"某个噪音源忘了降级"。
|
||
func TestChannelsPeriodicTaskLogsAreDowngraded(t *testing.T) {
|
||
src, err := readBundledChannels(t)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
s := string(src)
|
||
|
||
// 这些是「每次跑都成功」的常规回音 —— 必须 task 级
|
||
routine := []string{
|
||
`logEvent("模型目录已刷新(" .. updated .. " 个平台)", "task")`,
|
||
`logEvent("模型能力元数据已刷新(" .. tostring(cerr or 0) .. " 条)", "task")`,
|
||
`logEvent("模型刷新:跳过 " .. cooled .. " 个冷却中的平台", "task")`,
|
||
}
|
||
for _, r := range routine {
|
||
if !strings.Contains(s, r) {
|
||
t.Errorf("周期任务的常规结果必须降级为 task(否则会淹没用户动作):\n %s", r)
|
||
}
|
||
}
|
||
// 这些是异常 —— 必须 warn(不能被一起折叠掉)
|
||
for _, pat := range []string{
|
||
`logEvent("能力元数据未刷新: " .. plugin.utf8Clip(cerr, 120), "warn")`,
|
||
`logEvent("能力元数据刷新异常: " .. plugin.utf8Clip(cres, 120), "warn")`,
|
||
`logEvent("凭据续期失败 " .. #failed .. " 个:`,
|
||
} {
|
||
if !strings.Contains(s, pat) {
|
||
t.Errorf("异常必须保持 warn(不能被当常规噪音折叠):%s", pat)
|
||
}
|
||
}
|
||
// 「已同步源」是周期重同步的常规回音 —— 降级为 task
|
||
if !strings.Contains(s, `logEvent("已同步源 " .. name .. "(" .. #models .. " 模型)", "task", provider)`) {
|
||
t.Error("「已同步源」是周期重同步的常规回音,必须 task 级且带平台归属")
|
||
}
|
||
t.Log("✓ 周期噪音已降级,异常保持 warn")
|
||
}
|
||
|
||
// ★ 前端的三个过滤维度必须都在(级别门槛 / 例行折叠 / 按渠道过滤)。
|
||
func TestChannelsLogPanelHasFilters(t *testing.T) {
|
||
src, err := readBundledChannels(t)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
s := string(src)
|
||
|
||
// ① 日志卡片必须**独立于渠道详情**(C 的核心:修正归属)
|
||
// 判据:日志卡片的 DOM 不是 ch-detail 的子元素。
|
||
// 做法:取 ch-detail 开始到 ch-log-card 之间的片段,要求中间出现过
|
||
// ch-detail 的闭合(即两者是兄弟而非父子)。
|
||
iDetail := strings.Index(s, `id="ch-detail"`)
|
||
iLogCard := strings.Index(s, `id="ch-log-card"`)
|
||
if iDetail < 0 || iLogCard < 0 {
|
||
t.Fatal("找不到 ch-detail 或 ch-log-card(判据需更新)")
|
||
}
|
||
if iLogCard < iDetail {
|
||
t.Fatal("运行日志卡片应在渠道详情**之后**(作为兄弟节点)")
|
||
}
|
||
// 统计 ch-detail 打开到日志卡片之间未闭合的 div 层数
|
||
seg := s[iDetail:iLogCard]
|
||
depth := 0
|
||
for _, m := range regexp.MustCompile(`</?div`).FindAllString(seg, -1) {
|
||
if strings.HasPrefix(m, "</") {
|
||
depth--
|
||
} else {
|
||
depth++
|
||
}
|
||
}
|
||
if depth > 0 {
|
||
t.Errorf("运行日志卡片仍嵌在渠道详情内部(depth=%d)⇒ 点开某渠道会看到别的渠道的日志(归属错误)", depth)
|
||
}
|
||
|
||
// ② 三个过滤维度
|
||
for _, id := range []string{`id="ch-log-verbose"`, `id="ch-log-level"`} {
|
||
if !strings.Contains(s, id) {
|
||
t.Errorf("缺少日志视图控件 %s", id)
|
||
}
|
||
}
|
||
if !strings.Contains(s, "function chLogRender()") {
|
||
t.Error("必须实现 chLogRender()(否则过滤逻辑无处落地)")
|
||
}
|
||
// ③ 按渠道过滤 + 切渠道重渲染
|
||
if !strings.Contains(s, "if (selPlat) return e.platform === selPlat;") {
|
||
t.Error("必须按当前选中渠道过滤日志(否则归属错误以另一种形式回来)")
|
||
}
|
||
if !strings.Contains(s, "chLogRender();\n };\n") {
|
||
t.Error("chSelect 切换渠道后必须重渲染日志")
|
||
}
|
||
t.Log("✓ 日志面板独立成卡片,三个过滤维度齐备")
|
||
}
|
||
|
||
// ★ 日志截断必须按**字符**切,不能按字节 —— 否则中文被切出无效 UTF-8。
|
||
//
|
||
// 真实缺陷(生产实测,v2.0.33 截图里可见):日志尾显示成
|
||
//
|
||
// `能力增强不<E5BCBA>`
|
||
//
|
||
// 根因:`tostring(cerr):sub(1, 120)` —— Lua 的 sub 按**字节**切,而中文占 3 字节
|
||
// ⇒ 切在字符中间产生替换字符(U+FFFD)。一句本来完整的诊断看起来像数据损坏。
|
||
func TestLogTruncationIsUTF8Safe(t *testing.T) {
|
||
src, err := readBundledChannels(t)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
s := string(src)
|
||
|
||
// ① 必须存在按字符截断的助手
|
||
if !strings.Contains(s, "plugin.utf8Clip = function(s, maxBytes)") {
|
||
t.Fatal("必须实现 plugin.utf8Clip(按字符截断)")
|
||
}
|
||
// ② 中文文案上的截断不得再用裸 sub()
|
||
for _, bad := range []string{
|
||
`tostring(cres):sub(1, 120)`,
|
||
`tostring(cerr):sub(1, 120)`,
|
||
`table.concat(failed, "; "):sub(1, 300)`,
|
||
} {
|
||
if strings.Contains(s, bad) {
|
||
t.Errorf("中文文案的截断不能用裸 sub()(按字节切会产生 U+FFFD):%s", bad)
|
||
}
|
||
}
|
||
// ③ 三处必须已换成 utf8Clip
|
||
for _, want := range []string{
|
||
"plugin.utf8Clip(cres, 120)",
|
||
"plugin.utf8Clip(cerr, 120)",
|
||
`plugin.utf8Clip(table.concat(failed, "; "), 300)`,
|
||
} {
|
||
if !strings.Contains(s, want) {
|
||
t.Errorf("缺少 UTF-8 安全截断调用:%s", want)
|
||
}
|
||
}
|
||
// ④ 助手必须**只在超长时**才截断(否则正常文案会被莫名缩短)
|
||
if !strings.Contains(s, "if #s <= maxBytes then return s end") {
|
||
t.Error("utf8Clip 必须对未超长的输入原样返回")
|
||
}
|
||
// ⑤ 回退逻辑必须处理续字节(10xxxxxx)与孤立首字节
|
||
if !strings.Contains(s, "elseif b >= 0xC0 then") {
|
||
t.Error("utf8Clip 必须回退孤立的 UTF-8 首字节(否则末尾仍是半个字符)")
|
||
}
|
||
t.Log("✓ 截断已 UTF-8 安全")
|
||
}
|