TestHookPreCompact verifies that pre-compact saves hub state and prints the correct output, while silently skipping when no hubs exist.
(t *testing.T)
| 1117 | // TestHookPreCompact verifies that pre-compact saves hub state and prints the |
| 1118 | // correct output, while silently skipping when no hubs exist. |
| 1119 | func TestHookPreCompact(t *testing.T) { |
| 1120 | t.Run("no hubs: no output and no hubs.txt created", func(t *testing.T) { |
| 1121 | root := t.TempDir() |
| 1122 | // All-empty state → getHubInfo returns nil → hookPreCompact exits early. |
| 1123 | writeWatchState(t, root, watch.State{ |
| 1124 | UpdatedAt: time.Now(), |
| 1125 | FileCount: 5, |
| 1126 | }) |
| 1127 | |
| 1128 | out := captureOutput(func() { |
| 1129 | if err := hookPreCompact(root); err != nil { |
| 1130 | t.Errorf("hookPreCompact returned error: %v", err) |
| 1131 | } |
| 1132 | }) |
| 1133 | |
| 1134 | if out != "" { |
| 1135 | t.Errorf("expected no output when no hubs, got %q", out) |
| 1136 | } |
| 1137 | hubsFile := filepath.Join(root, ".codemap", "hubs.txt") |
| 1138 | if _, err := os.Stat(hubsFile); !os.IsNotExist(err) { |
| 1139 | t.Error("expected no hubs.txt when hub list is empty") |
| 1140 | } |
| 1141 | }) |
| 1142 | |
| 1143 | t.Run("with hubs: creates hubs.txt and prints count", func(t *testing.T) { |
| 1144 | root := t.TempDir() |
| 1145 | writeWatchState(t, root, watch.State{ |
| 1146 | UpdatedAt: time.Now(), |
| 1147 | FileCount: 20, |
| 1148 | Hubs: []string{"types.go", "utils.go", "config.go"}, |
| 1149 | Importers: map[string][]string{ |
| 1150 | "types.go": {"a.go", "b.go", "c.go"}, |
| 1151 | "utils.go": {"a.go", "b.go", "c.go"}, |
| 1152 | "config.go": {"a.go", "b.go", "c.go"}, |
| 1153 | }, |
| 1154 | }) |
| 1155 | |
| 1156 | out := captureOutput(func() { |
| 1157 | if err := hookPreCompact(root); err != nil { |
| 1158 | t.Errorf("hookPreCompact returned error: %v", err) |
| 1159 | } |
| 1160 | }) |
| 1161 | |
| 1162 | if !strings.Contains(out, "3 hub files tracked") { |
| 1163 | t.Errorf("expected hub count in output, got %q", out) |
| 1164 | } |
| 1165 | if !strings.Contains(out, "hubs.txt") { |
| 1166 | t.Errorf("expected hubs.txt mention, got %q", out) |
| 1167 | } |
| 1168 | |
| 1169 | hubsFile := filepath.Join(root, ".codemap", "hubs.txt") |
| 1170 | content, err := os.ReadFile(hubsFile) |
| 1171 | if err != nil { |
| 1172 | t.Fatalf("expected hubs.txt to be created: %v", err) |
| 1173 | } |
| 1174 | for _, hub := range []string{"types.go", "utils.go", "config.go"} { |
| 1175 | if !strings.Contains(string(content), hub) { |
| 1176 | t.Errorf("expected %q in hubs.txt, got %q", hub, content) |
nothing calls this directly
no test coverage detected