package app import ( "fmt" "os" "path/filepath" "strings" "testing" ) // TestSearchConcurrentOrderStable 并发读盘后结果仍按文件序排列, // 不能因为哪个 worker 先跑完就打乱顺序。 func TestSearchConcurrentOrderStable(t *testing.T) { root := t.TempDir() const n = 24 for i := 0; i < n; i++ { name := fmt.Sprintf("f%02d.txt", i) body := fmt.Sprintf("line one\nneedle in %s\nlast line\n", name) if err := os.WriteFile(filepath.Join(root, name), []byte(body), 0o644); err != nil { t.Fatal(err) } } a := &App{root: root} got, stats := a.searchInWorkspace("needle", 200, searchOpts{}) if len(got) != n { t.Fatalf("应命中 %d 行,实际 %d", n, len(got)) } if stats.hint() != "" { t.Fatalf("没有跳过文件时不该有提示:%q", stats.hint()) } for i, it := range got { want := fmt.Sprintf("f%02d.txt", i) if it.Label != want { t.Fatalf("第 %d 项是 %q,想要 %q(顺序乱了)", i, it.Label, want) } if it.Line != 2 { t.Fatalf("%s 命中行 = %d,想要 2", it.Label, it.Line) } } } // TestSearchStatsTooLarge 超过 512KB 的文件要被数出来,并且提示可见。 func TestSearchStatsTooLarge(t *testing.T) { root := t.TempDir() big := strings.Repeat("x", 512*1024+1) + "\nneedle\n" if err := os.WriteFile(filepath.Join(root, "big.txt"), []byte(big), 0o644); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(root, "small.txt"), []byte("needle\n"), 0o644); err != nil { t.Fatal(err) } a := &App{root: root} got, stats := a.searchInWorkspace("needle", 200, searchOpts{}) if len(got) != 1 || got[0].Label != "small.txt" { t.Fatalf("只该命中 small.txt:%+v", got) } if stats.tooLarge != 1 { t.Fatalf("tooLarge = %d,想要 1", stats.tooLarge) } hint := stats.hint() if !strings.Contains(hint, "512 KB") || !strings.Contains(hint, "1 个文件") { t.Fatalf("提示没说清跳过了什么:%q", hint) } } func TestSearchStatsHint(t *testing.T) { if (searchStats{}).hint() != "" { t.Fatal("没有跳过时不该有提示") } h := (searchStats{tooLarge: 2, unreadable: 3}).hint() if !strings.Contains(h, "2 个文件超过 512 KB") || !strings.Contains(h, "3 个文件读不出") { t.Fatalf("提示缺内容:%q", h) } }