(t *testing.T)
| 209 | } |
| 210 | |
| 211 | func TestShouldRestartDaemon(t *testing.T) { |
| 212 | t.Run("not running returns false", func(t *testing.T) { |
| 213 | withOwnedDaemonProcess(t, func(string) bool { return true }) |
| 214 | if shouldRestartDaemon(t.TempDir(), time.Now()) { |
| 215 | t.Fatal("expected false when daemon is not running") |
| 216 | } |
| 217 | }) |
| 218 | |
| 219 | t.Run("running with fresh state returns false", func(t *testing.T) { |
| 220 | withOwnedDaemonProcess(t, func(string) bool { return true }) |
| 221 | root := t.TempDir() |
| 222 | writeWatchState(t, root, watch.State{ |
| 223 | UpdatedAt: time.Now().Add(-5 * time.Minute), |
| 224 | FileCount: 10, |
| 225 | }) |
| 226 | if shouldRestartDaemon(root, time.Now()) { |
| 227 | t.Fatal("expected false for fresh daemon state") |
| 228 | } |
| 229 | }) |
| 230 | |
| 231 | t.Run("running with stale state returns true", func(t *testing.T) { |
| 232 | withOwnedDaemonProcess(t, func(string) bool { return true }) |
| 233 | root := t.TempDir() |
| 234 | writeWatchState(t, root, watch.State{ |
| 235 | UpdatedAt: time.Now().Add(-3 * time.Hour), |
| 236 | FileCount: 10, |
| 237 | }) |
| 238 | if !shouldRestartDaemon(root, time.Now()) { |
| 239 | t.Fatal("expected true for stale daemon state") |
| 240 | } |
| 241 | }) |
| 242 | |
| 243 | t.Run("running without state returns true", func(t *testing.T) { |
| 244 | withOwnedDaemonProcess(t, func(string) bool { return true }) |
| 245 | root := t.TempDir() |
| 246 | codemapDir := filepath.Join(root, ".codemap") |
| 247 | if err := os.MkdirAll(codemapDir, 0755); err != nil { |
| 248 | t.Fatal(err) |
| 249 | } |
| 250 | if err := watch.WritePID(root); err != nil { |
| 251 | t.Fatal(err) |
| 252 | } |
| 253 | t.Cleanup(func() { watch.RemovePID(root) }) |
| 254 | |
| 255 | if !shouldRestartDaemon(root, time.Now()) { |
| 256 | t.Fatal("expected true when daemon pid exists but state is missing") |
| 257 | } |
| 258 | }) |
| 259 | |
| 260 | t.Run("running stale state but daemon not owned returns false", func(t *testing.T) { |
| 261 | withOwnedDaemonProcess(t, func(string) bool { return false }) |
| 262 | root := t.TempDir() |
| 263 | writeWatchState(t, root, watch.State{ |
| 264 | UpdatedAt: time.Now().Add(-3 * time.Hour), |
| 265 | FileCount: 10, |
| 266 | }) |
| 267 | if shouldRestartDaemon(root, time.Now()) { |
| 268 | t.Fatal("expected false for unowned daemon process") |
nothing calls this directly
no test coverage detected