返回 DeepSeek-Reasonix
readfile_window_test.go
根目录 / internal / tool / builtin / readfile_window_test.go
1 package builtin
2
3 import (
4 "bytes"
5 "fmt"
6 "io"
7 "strings"
8 "testing"
9 )
10
11 type countingReader struct {
12 r io.Reader
13 n int
14 }
15
16 func (c *countingReader) Read(p []byte) (int, error) {
17 n, err := c.r.Read(p)
18 c.n += n
19 return n, err
20 }
21
22 // TestScanWindowedReadDoesNotConsumeWholeFile guards the regression where scan()
23 // drained the entire file (a second `for scanner.Scan()` loop) just to count the
24 // remaining lines for the pagination trailer. A small windowed read of a large
25 // file must read only a small prefix, not all of it.
26 func TestScanWindowedReadDoesNotConsumeWholeFile(t *testing.T) {
27 var buf bytes.Buffer
28 for i := 1; i <= 100_000; i++ {
29 fmt.Fprintf(&buf, "line %d\n", i)
30 }
31 total := buf.Len()
32 if total < 500*1024 {
33 t.Fatalf("test fixture too small (%d bytes) to be meaningful", total)
34 }
35
36 cr := &countingReader{r: bytes.NewReader(buf.Bytes())}
37 out, err := readFile{}.scan(cr, 0, 3)
38 if err != nil {
39 t.Fatalf("scan: %v", err)
40 }
41
42 if !strings.Contains(out, "1→line 1") || !strings.Contains(out, "3→line 3") {
43 t.Fatalf("window content wrong:\n%s", out)
44 }
45 if strings.Contains(out, "line 4") {
46 t.Fatalf("window leaked line 4:\n%s", out)
47 }
48 if !strings.Contains(out, "more lines below") {
49 t.Fatalf("pagination trailer missing:\n%s", out)
50 }
51 if cr.n > 100*1024 {
52 t.Fatalf("read %d of %d bytes for a 3-line window; should read only a small prefix", cr.n, total)
53 }
54 t.Logf("read %d of %d bytes (%.1f%%) for a 3-line window", cr.n, total, 100*float64(cr.n)/float64(total))
55 }
56
56 lines GO