Files
backend_v2/internal/handler/crawl_render_test.go
toom1996 51e3d13383 feat: 抓取进度可见 + 已入库秀预检(避免重复下载)
一、防重复下载
图片 key 是内容 sha1,不下载无法判断是否已存在;又没存 source_url,
于是每次重爬都把几百张已下过的图再下一遍(线上实测 Celine 62 分钟、0 新增)。
新增 GET /admin/internal/shows/covered?brand_uid=,按 (年份,季节,类型)
聚合已入库图片数,供 spider 抓前整场跳过。

二、进度可见
一次抓取可能跑几十分钟,此前只有 running/done,看不出「在干什么」,
也分不清「慢」和「卡死」。
- crawl_tasks 增加 progress_done/total/text/at
- POST /admin/internal/crawl/tasks/:id/progress(spider 每场 + 每批图片上报)
- GET /admin/crawl/live(页面 4 秒轮询,局部刷新状态/进度/已提交)
- progress_at 超过 10 分钟没更新标黄「可能卡住」
- 渲染测试守住进度列与轮询锚点
2026-10-25 21:22:56 +08:00

67 lines
1.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package handler
import (
"bytes"
"strings"
"testing"
"fashionapi/internal/model"
"fashionapi/internal/pkg/pagination"
"github.com/gin-gonic/gin"
)
// TestCrawlPageRendersProgress 守住抓取列表页的「进度」列。
//
// 这一列由模板 + 前端轮询共同驱动,出错时不会编译失败、只会在打开页面时 500 或空白,
// 所以用一个渲染测试把「运行中 + 卡住 + 有进度文案」这几种情况固定下来。
func TestCrawlPageRendersProgress(t *testing.T) {
tpl, ok := pageTemplates["crawl"]
if !ok {
t.Fatal("crawl 模板未注册")
}
data := gin.H{
"NavKey": "crawl",
"PendingReviews": 0,
"Rows": []crawlRowView{{
ID: 7,
BrandUID: "abc123",
BrandName: "Celine",
Status: model.CrawlRunning,
StatusLabel: "执行中",
Shows: 3,
Worker: "vm-1-100-0",
UpdatedAt: "2026-10-25 21:00",
ProgressLabel: "3/13 场 · Fall 2021 Menswear(24/38 张)",
Stale: true,
CanCancel: true,
}},
"Total": 1,
"Queued": 0,
"Running": 1,
"Done": 0,
"Failed": 0,
"Added": 0,
"Pager": pagination.Pager{Label: "第 1 / 1 页"},
}
var buf bytes.Buffer
if err := tpl.ExecuteTemplate(&buf, "layout", data); err != nil {
t.Fatalf("渲染抓取列表页失败: %v", err)
}
out := buf.String()
for _, want := range []string{
`data-live-progress="7"`, // 轮询锚点:JS 靠它定位单元格
`data-live-status="7"`, // 状态徽标也要能刷新
"3/13 场 · Fall 2021 Menswear(24/38 张)", // 进度文案原样输出
"progress stale", // 心跳停了要有警示样式
"/admin/crawl/live", // 轮询地址
} {
if !strings.Contains(out, want) {
t.Errorf("页面缺少 %q", want)
}
}
}