| 83 | if err := os.WriteFile(path, []byte(want), 0o644); err != nil { |
| 84 | t.Fatal(err) |
| 85 | } |
| 86 | var got strings.Builder |
| 87 | for offset := 1; offset <= 900; offset += 100 { |
| 88 | page := ReadFile(path, offset, 100) |
| 89 | if i := strings.Index(page, "\n\n[lines "); i >= 0 { |
| 90 | page = page[:i] + "\n" |
| 91 | } |
| 92 | got.WriteString(page) |
| 93 | } |
| 94 | if got.String() != want { |
| 95 | t.Fatalf("paged read did not reconstruct the file: got %d bytes, want %d", got.Len(), len(want)) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // TestReadFileOffsetPastEnd: a bad offset must say so, not return an empty |
| 100 | // string the model would read as "the file is empty". |
| 101 | func TestReadFileOffsetPastEnd(t *testing.T) { |
| 102 | dir := t.TempDir() |
| 103 | path := filepath.Join(dir, "small.txt") |
| 104 | if err := os.WriteFile(path, []byte("a\nb\n"), 0o644); err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | got := ReadFile(path, 99, 0) |