mirror of
https://gitcode.com/JianFeeeee/ModelRouter.git
synced 2026-10-03 23:54:06 +00:00
CDP 实测发现两个真 bug,都是复用画布时引入的。 一、画布画进了后台标签页。sortCanvasEl() 原本按文档顺序找,先命中 #scr-canvas(全局优先级页的画布)。而弹窗是挂在设置页之上的,两个画布 同时存在于 DOM —— 于是 per-key 的 36 条泳道被画进隐藏的全局画布,弹窗 里空空如也,回���全局页还会发现自己的链被覆盖了。 改为按 sortState.scope 解析:有 scope 就用 #key-auto-canvas,否则用 #scr-canvas。 二、保存成功却弹窗不关。!j.slots 分支里我写了 return,跳过了 keyAutoClose。写确实成功了(服务端已存),但用户看到弹窗还开着、按钮 还是禁用状态,会反复点保存。而且那条 toast 文案写成"保存后 AUTO 请求 会失败",读起来像保存失败了 —— 一并改成"已保存,但槽位无法解析…"。 判据两条,变异验证:恢复按文档顺序查找 → 第一条判红;!slots 分支加 return → 第二条判红。第二条最初只查 keyAutoClose() 是否存在,变异加了 return 仍然通过,属于判据漏放,已改为扫描 try 块内 close 调用之前的 所有 return 语句。 CDP 复测(先开全局页制造干扰,再开 per-key 弹窗): - 弹窗画布 36 泳道,全局画布保持 4 泳道未被污染 - 保存 gpt-5.2@xinjianya 后弹窗关闭、服务端存储正确、scope 清空 - 重开弹窗读回 [["gpt-5.2@xinjianya"]] Co-Authored-By: ModelRouter <noreply@modelrouter.dev>
207 lines
7.9 KiB
Go
207 lines
7.9 KiB
Go
package gateway
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// Per-key AUTO chain UI contract.
|
|
//
|
|
// The per-key editor deliberately REUSES the global lane canvas instead of
|
|
// shipping a second, simpler editor. That is a design choice with teeth: a
|
|
// reduced copy would lack drag ordering and per-slot quota/period/hours, and
|
|
// the two would drift apart silently. These checks pin the sharing, because
|
|
// the only symptom of un-sharing is "the per-key editor got worse" — no error,
|
|
// just a smaller feature set that nobody notices until they use it.
|
|
func TestUIPerKeyAutoWiring(t *testing.T) {
|
|
src := uiSource(t)
|
|
|
|
// The admin key list must offer the editor, otherwise the whole feature is
|
|
// unreachable from the UI. Assert the *call site* inside keyCanvasHtml,
|
|
// not just that the functions exist: removing the button leaves every
|
|
// definition intact, and that is exactly the regression to catch.
|
|
kcStart := strings.Index(src, "function keyCanvasHtml")
|
|
if kcStart < 0 {
|
|
t.Fatal("keyCanvasHtml is gone — cannot check the AUTO chain button")
|
|
}
|
|
kcEnd := strings.Index(src[kcStart:], "\n }")
|
|
if kcEnd < 0 {
|
|
t.Fatal("could not delimit keyCanvasHtml")
|
|
}
|
|
if !strings.Contains(src[kcStart:kcStart+kcEnd], "openKeyAutoModal(") {
|
|
t.Fatal("per-key AUTO chain button is missing from the key card header")
|
|
}
|
|
for _, fn := range []string{
|
|
"function openKeyAutoModal",
|
|
"function keyAutoInherit",
|
|
"function keyAutoSave",
|
|
"function keyAutoClose",
|
|
} {
|
|
if !strings.Contains(src, fn) {
|
|
t.Fatalf("UI is missing %s — the editor cannot function", fn)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The per-key editor must render the SAME canvas the global editor uses.
|
|
// If this regresses, the per-key chain silently loses drag ordering and
|
|
// per-slot quota/period/hours while still looking like it works.
|
|
func TestUIPerKeyAutoReusesGlobalCanvas(t *testing.T) {
|
|
src := uiSource(t)
|
|
|
|
// One canvas implementation, two mount ids.
|
|
if !strings.Contains(src, "function renderSortEditor(") {
|
|
t.Fatal("no shared canvas renderer — the per-key editor must reuse renderSortEditor")
|
|
}
|
|
if !strings.Contains(src, `canvasId: "scr-canvas"`) {
|
|
t.Fatal("the global editor no longer mounts the shared canvas")
|
|
}
|
|
if !strings.Contains(src, `canvasId: "key-auto-canvas"`) {
|
|
t.Fatal("the per-key editor does not mount the shared canvas")
|
|
}
|
|
// Canvas lookup must consider both ids, or drag/drop silently no-ops on
|
|
// whichever surface is not #scr-canvas.
|
|
if !strings.Contains(src, "function sortCanvasEl(") ||
|
|
!strings.Contains(src, `getElementById("key-auto-canvas")`) {
|
|
t.Fatal("sortCanvasEl must resolve both canvas ids")
|
|
}
|
|
// The rules<->lanes conversion is shared too, so the per-key editor sends
|
|
// exactly the fields the server understands (quota/period/hours included).
|
|
for _, fn := range []string{"function buildLanes(", "function lanesToRules("} {
|
|
if !strings.Contains(src, fn) {
|
|
t.Fatalf("%s must be shared between the global and per-key editors", fn)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Saving must route through persistAuto's scope switch, not a hand-rolled
|
|
// request, or the two editors will serialize rules differently.
|
|
func TestUIPerKeyAutoSavesThroughScope(t *testing.T) {
|
|
src := uiSource(t)
|
|
if !strings.Contains(src, "sortState.scope = key") {
|
|
t.Fatal("openKeyAutoModal must set sortState.scope so the save targets the key")
|
|
}
|
|
if !strings.Contains(src, "sortState.scope = null") {
|
|
t.Fatal("closing the modal must clear sortState.scope, or the GLOBAL editor would save to this key")
|
|
}
|
|
if !strings.Contains(src, "if (sortState.scope)") {
|
|
t.Fatal("persistAuto must branch on sortState.scope")
|
|
}
|
|
if !strings.Contains(src, `"/auto"`) {
|
|
t.Fatal("per-key editor must call the /api/keys/{key}/auto endpoint")
|
|
}
|
|
// An empty lane list is how "inherit the global chain" is expressed.
|
|
if !strings.Contains(src, "JSON.stringify({ auto: lanesToRules(sortState.lanes) })") {
|
|
t.Fatal("keyAutoSave must PUT lanesToRules(sortState.lanes) verbatim")
|
|
}
|
|
}
|
|
|
|
// Both languages need the strings, otherwise a non-Chinese admin sees raw
|
|
// i18n keys in the modal.
|
|
func TestUIPerKeyAutoHasBothLanguages(t *testing.T) {
|
|
src := uiSource(t)
|
|
for _, k := range []string{
|
|
"kAutoChainTitle", "kAutoChainHint", "kAutoChainInherit",
|
|
"kAutoChainEmpty", "kAutoChainGlobal", "kAutoChainBad",
|
|
} {
|
|
n := strings.Count(src, k+`: "`)
|
|
if n < 2 {
|
|
t.Fatalf("i18n key %s defined %d time(s), want both zh and en", k, n)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The statistics dashboard must print the window it actually covers. Calendar
|
|
// periods are not nested — "this week" starts on its Monday and therefore
|
|
// reaches into the previous month early in a month, making its total LARGER
|
|
// than "this month". That is correct arithmetic, but without the dates on
|
|
// screen it reads as a bug, and it has been reported as one.
|
|
func TestUIStatsShowsPeriodWindow(t *testing.T) {
|
|
src := uiSource(t)
|
|
if !strings.Contains(src, `id="period-range"`) {
|
|
t.Fatal("the stats dashboard has no element for the period window range")
|
|
}
|
|
if n := strings.Count(src, "periodRange"); n < 2 {
|
|
t.Fatalf("i18n key periodRange appears %d time(s), want both zh and en", n)
|
|
}
|
|
}
|
|
// Both canvases can exist in the DOM at once: the modal is mounted on top of
|
|
// the settings tab, whose #scr-canvas stays alive behind it. Resolving the
|
|
// canvas by document order (whichever comes first) painted the per-key chain
|
|
// into the hidden background tab and left the dialog empty — and worse,
|
|
// overwrote the global chain the user returns to. The lookup must follow
|
|
// sortState.scope instead.
|
|
func TestUISortCanvasResolvesByScope(t *testing.T) {
|
|
src := uiSource(t)
|
|
el := readFuncBody(t, src, "sortCanvasEl")
|
|
if !strings.Contains(el, "if (sortState.scope)") {
|
|
t.Fatal("sortCanvasEl must branch on sortState.scope, not document order")
|
|
}
|
|
if strings.Contains(el, `getElementById("scr-canvas")`) &&
|
|
strings.Index(el, `getElementById("scr-canvas")`) < strings.Index(el, "sortState.scope") {
|
|
t.Fatal("sortCanvasEl returns #scr-canvas before checking scope — the " +
|
|
"per-key editor paints into the global canvas")
|
|
}
|
|
}
|
|
|
|
// A successful save must close the dialog even when the slots do not resolve.
|
|
// The write DID succeed; leaving the modal open makes the user click Save
|
|
// again and again. This regressed once: the !slots branch returned early.
|
|
func TestUIKeyAutoSaveClosesOnSuccess(t *testing.T) {
|
|
src := uiSource(t)
|
|
body := readFuncBody(t, src, "keyAutoSave")
|
|
// keyAutoClose() must be reached on every success path, not only in the
|
|
// happy branch.
|
|
iClose := strings.Index(body, "keyAutoClose()")
|
|
if iClose < 0 {
|
|
t.Fatal("keyAutoSave never calls keyAutoClose — the dialog would stay open")
|
|
}
|
|
iToast := strings.Index(body, "toast(")
|
|
if iToast >= 0 && iClose < iToast {
|
|
t.Fatal("keyAutoClose is called before the toast; the warning must be " +
|
|
"shown first or it is lost with the modal")
|
|
}
|
|
// An early `return` inside a branch bypasses keyAutoClose entirely, so
|
|
// merely finding the call is not enough: every `return` that sits between
|
|
// entering the try block and the close call is a path that leaves the
|
|
// dialog open after a successful write.
|
|
tryStart := strings.Index(body, "try {")
|
|
seg := body[tryStart:iClose]
|
|
for _, line := range strings.Split(seg, "\n") {
|
|
trimmed := strings.TrimSpace(line)
|
|
if trimmed == "return;" || strings.HasPrefix(trimmed, "return ") {
|
|
t.Fatal("a `return` before keyAutoClose leaves the dialog open after " +
|
|
"a successful save: " + trimmed)
|
|
}
|
|
}
|
|
}
|
|
|
|
// readFuncBody returns the source of a top-level function by name, using
|
|
// brace matching so nested blocks do not truncate it.
|
|
func readFuncBody(t *testing.T, src, name string) string {
|
|
t.Helper()
|
|
idx := strings.Index(src, "function "+name+"(")
|
|
if idx < 0 {
|
|
t.Fatalf("function %s not found", name)
|
|
}
|
|
start := strings.Index(src[idx:], "{")
|
|
if start < 0 {
|
|
t.Fatalf("function %s has no body", name)
|
|
}
|
|
i := idx + start
|
|
depth := 0
|
|
for j := i; j < len(src); j++ {
|
|
switch src[j] {
|
|
case '{':
|
|
depth++
|
|
case '}':
|
|
depth--
|
|
if depth == 0 {
|
|
return src[i : j+1]
|
|
}
|
|
}
|
|
}
|
|
t.Fatalf("function %s body is unterminated", name)
|
|
return ""
|
|
}
|