队列数字原本取 len(查询结果),而 ListActive 带 LIMIT 200: 1 个执行中 + 199 个排队刚好凑满,页面就永远显示 199, 看起来像「队列被卡住了、只有这么点任务」。 - 数字改用 SELECT count(*) GROUP BY status,不受 LIMIT 影响 - 表格仍只列前 50 个(渲染上千行会拖慢页面), 并在标题下注明「共 N 个,下面只列前 50 个」 - 渲染测试新增一条:截断时必须出现提示文案
90 lines
2.8 KiB
Go
90 lines
2.8 KiB
Go
package handler
|
||
|
||
import (
|
||
"bytes"
|
||
"strings"
|
||
"testing"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// TestSpiderPageRenders 守住「爬虫」页的三块内容与轮询锚点。
|
||
//
|
||
// 这类模板错误不会编译失败,只会在打开页面时 500 或空白,
|
||
// 所以用一个渲染测试把「有任务 / 卡住 / 有入库结果」这几种情况固定下来。
|
||
func TestSpiderPageRenders(t *testing.T) {
|
||
tpl, ok := pageTemplates["spider"]
|
||
if !ok {
|
||
t.Fatal("spider 模板未注册")
|
||
}
|
||
|
||
data := gin.H{
|
||
"NavKey": "spider",
|
||
"PendingReviews": 0,
|
||
"State": "可能卡住",
|
||
"StateClass": "failed",
|
||
"RunningCount": 1,
|
||
"QueuedCount": 2,
|
||
"QueuedShown": 2,
|
||
"QueuedPreview": 50,
|
||
"PendingIngest": 3,
|
||
"Running": []spiderRunView{{
|
||
ID: 85,
|
||
BrandName: "Amiri",
|
||
Progress: "12/47 场 · Fall 2021 Menswear(24/38 张)",
|
||
Beat: "11 分钟",
|
||
Stale: true,
|
||
Worker: "vm-1-74218-0",
|
||
}},
|
||
"Queued": []spiderQueueView{
|
||
{BrandName: "Celine", Since: "10-25 23:30", Waiting: "8 分钟"},
|
||
{BrandName: "Gucci", Since: "10-25 23:35", Waiting: "3 分钟"},
|
||
},
|
||
"Results": []spiderResultView{
|
||
{At: "10-25 23:12", BrandName: "Amiri", Title: "Fall 2019 Menswear", Result: "新建", Note: "新建 34 张图"},
|
||
{At: "10-25 23:08", BrandName: "Gucci", Title: "Fall 2020 RTW", Result: "失败", Note: "图片下载超时", Bad: true},
|
||
},
|
||
}
|
||
|
||
var buf bytes.Buffer
|
||
if err := tpl.ExecuteTemplate(&buf, "layout", data); err != nil {
|
||
t.Fatalf("渲染爬虫页失败: %v", err)
|
||
}
|
||
out := buf.String()
|
||
|
||
for _, want := range []string{
|
||
`id="running-body"`, // 轮询重建的目标容器
|
||
"/admin/spider/live", // 轮询地址
|
||
"12/47 场 · Fall 2021 Menswear(24/38 张)", // 进度文案原样输出
|
||
"Celine", // 待抓队列
|
||
"新建 34 张图", // 入库结果说明
|
||
} {
|
||
if !strings.Contains(out, want) {
|
||
t.Errorf("页面缺少 %q", want)
|
||
}
|
||
}
|
||
|
||
// 卡住要能看出来:进度单元格带 stale 样式。
|
||
if !strings.Contains(out, "progress stale") {
|
||
t.Error("疑似卡住的任务没有 stale 样式")
|
||
}
|
||
// 失败结果要走 failed 徽标,正常结果走 done 徽标。
|
||
if !strings.Contains(out, `class="badge failed"`) {
|
||
t.Error("失败结果没有 failed 徽标")
|
||
}
|
||
if !strings.Contains(out, `class="badge done"`) {
|
||
t.Error("正常结果没有 done 徽标")
|
||
}
|
||
|
||
// 队列被截断时要说清楚:否则「队列 1620 个」配一张 50 行的表会让人以为数据错了。
|
||
data["QueuedCount"] = 1620
|
||
data["QueuedShown"] = 50
|
||
var buf2 bytes.Buffer
|
||
if err := tpl.ExecuteTemplate(&buf2, "layout", data); err != nil {
|
||
t.Fatalf("渲染截断提示失败: %v", err)
|
||
}
|
||
if !strings.Contains(buf2.String(), "只列前 50 个") {
|
||
t.Error("队列被截断时缺少提示")
|
||
}
|
||
}
|