TestGetLastSessionEvents verifies that the 20-line budget is enforced when reading the events log, protecting hook startup output from bloating context.
(t *testing.T)
| 822 | // TestGetLastSessionEvents verifies that the 20-line budget is enforced when |
| 823 | // reading the events log, protecting hook startup output from bloating context. |
| 824 | func TestGetLastSessionEvents(t *testing.T) { |
| 825 | t.Run("missing file returns nil", func(t *testing.T) { |
| 826 | if got := getLastSessionEvents(t.TempDir()); got != nil { |
| 827 | t.Errorf("expected nil for missing file, got %v", got) |
| 828 | } |
| 829 | }) |
| 830 | |
| 831 | t.Run("empty file returns nil", func(t *testing.T) { |
| 832 | root := t.TempDir() |
| 833 | codemapDir := filepath.Join(root, ".codemap") |
| 834 | if err := os.MkdirAll(codemapDir, 0755); err != nil { |
| 835 | t.Fatal(err) |
| 836 | } |
| 837 | if err := os.WriteFile(filepath.Join(codemapDir, "events.log"), []byte(""), 0644); err != nil { |
| 838 | t.Fatal(err) |
| 839 | } |
| 840 | if got := getLastSessionEvents(root); got != nil { |
| 841 | t.Errorf("expected nil for empty file, got %v", got) |
| 842 | } |
| 843 | }) |
| 844 | |
| 845 | t.Run("returns all lines when fewer than 20", func(t *testing.T) { |
| 846 | root := t.TempDir() |
| 847 | codemapDir := filepath.Join(root, ".codemap") |
| 848 | os.MkdirAll(codemapDir, 0755) |
| 849 | lines := "ts|WRITE|a.go\nts|WRITE|b.go\nts|WRITE|c.go" |
| 850 | os.WriteFile(filepath.Join(codemapDir, "events.log"), []byte(lines), 0644) |
| 851 | |
| 852 | got := getLastSessionEvents(root) |
| 853 | if len(got) != 3 { |
| 854 | t.Errorf("expected 3 events, got %d: %v", len(got), got) |
| 855 | } |
| 856 | }) |
| 857 | |
| 858 | t.Run("caps at 20 lines for large log (context bloat protection)", func(t *testing.T) { |
| 859 | root := t.TempDir() |
| 860 | codemapDir := filepath.Join(root, ".codemap") |
| 861 | os.MkdirAll(codemapDir, 0755) |
| 862 | |
| 863 | var sb strings.Builder |
| 864 | for i := 1; i <= 35; i++ { |
| 865 | fmt.Fprintf(&sb, "ts|WRITE|file%02d.go\n", i) |
| 866 | } |
| 867 | os.WriteFile(filepath.Join(codemapDir, "events.log"), []byte(sb.String()), 0644) |
| 868 | |
| 869 | got := getLastSessionEvents(root) |
| 870 | if len(got) != 20 { |
| 871 | t.Errorf("expected 20 events (cap), got %d", len(got)) |
| 872 | } |
| 873 | // Should be the *last* 20 (file16.go through file35.go). |
| 874 | if !strings.Contains(got[0], "file16.go") { |
| 875 | t.Errorf("expected first retained event to be file16.go, got %q", got[0]) |
| 876 | } |
| 877 | if !strings.Contains(got[19], "file35.go") { |
| 878 | t.Errorf("expected last retained event to be file35.go, got %q", got[19]) |
| 879 | } |
| 880 | }) |
| 881 |
nothing calls this directly
no test coverage detected