package main // App 层的测试:验证「清除已完成」之后,推送历史还在(灰标不消失) import ( "path/filepath" "testing" "eh-downloader-wails/internal/core" "eh-downloader-wails/internal/eh" ) func TestAppClearFinishedKeepsPushedHistory(t *testing.T) { dir := t.TempDir() a := NewApp() // NewApp 会初始化 logMu,否则 emitLog 会卡在 nil channel 上 a.store = core.NewStore(filepath.Join(dir, "state.json")) a.pushed = core.NewPushedStore(filepath.Join(dir, "pushed.json")) // 油猴推送了一个画廊 a.store.AddTasks([]eh.GalleryRef{{GID: "111", Token: "aaa", Raw: "u", Title: "T1"}}) a.pushed.Mark("111", "aaa", "T1") // 下载完成 task := a.store.FindTask("111", "aaa") if task == nil { t.Fatal("任务没了") } task.Status = "done" a.store.UpsertTask(*task) if a.store.CountDone() != 1 { t.Fatalf("应有 1 个已完成,实际 %d", a.store.CountDone()) } // 点「清除已完成」 res := a.ClearFinished() if removed, _ := res["removed"].(int); removed != 1 { t.Fatalf("应清掉 1 个,实际 %v", res["removed"]) } // 任务没了 if a.store.CountDone() != 0 { t.Error("任务应已清空") } if len(a.store.Tasks()) != 0 { t.Errorf("任务列表应为空,实际 %d 个", len(a.store.Tasks())) } // 但推送历史还在 —— 灰标不会消失 if a.pushed.Count() != 1 { t.Fatalf("推送历史应还剩 1 条,实际 %d", a.pushed.Count()) } entries := a.pushed.Entries() if entries[0].GID != "111" { t.Errorf("gid 不对: %s", entries[0].GID) } // 而且被标成了「已完成」 if !entries[0].Completed { t.Errorf("应标记 completed=true: %+v", entries[0]) } } func TestAppPushedHistoryAndClear(t *testing.T) { dir := t.TempDir() a := NewApp() a.store = core.NewStore(filepath.Join(dir, "state.json")) a.pushed = core.NewPushedStore(filepath.Join(dir, "pushed.json")) a.pushed.Mark("111", "aaa", "T1") a.pushed.Mark("222", "bbb", "T2") h := a.PushedHistory() if h.Count != 2 || len(h.Items) != 2 { t.Fatalf("应返回 2 条,实际 count=%d len=%d", h.Count, len(h.Items)) } a.ClearPushedHistory() if a.pushed.Count() != 0 { t.Fatalf("清空后应为 0,实际 %d", a.pushed.Count()) } }