feat: add job maintenance dashboard and automated stale job expiration system

This commit is contained in:
2026-08-22 01:51:43 -07:00
parent 367929e7d0
commit 7ec91f384f
12 changed files with 633 additions and 56 deletions
+50
View File
@@ -82,3 +82,53 @@ func TestHandleDeleteTarget(t *testing.T) {
func osWriteFile(path string, data []byte) error {
return os.WriteFile(path, data, 0644)
}
func TestMaintenanceHandlers(t *testing.T) {
tempDir := t.TempDir()
dbPath := filepath.Join(tempDir, "test_ui_maintenance.db")
database, err := db.InitDB(dbPath)
if err != nil {
t.Fatalf("failed to init db: %v", err)
}
defer func() { _ = database.Close() }()
// Insert job
job, _, err := database.InsertRawJob("canon-m-1", nil, "Software Engineer", "Google", "Seattle", "https://example.com/job/1", "Go backend")
if err != nil {
t.Fatalf("failed to insert raw job: %v", err)
}
// Make stale (25 days old)
_, _ = database.Exec(`UPDATE raw_jobs SET last_seen_at = datetime('now', '-25 days') WHERE id = ?`, job.ID)
cfg := &config.Config{}
server := NewServer(cfg, database, nil)
mux := http.NewServeMux()
server.RegisterRoutes(mux)
// 1. Test POST /api/maintenance/stale-expire
req := httptest.NewRequest("POST", "/api/maintenance/stale-expire", strings.NewReader("days=14&protect=true"))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected status 200, got %d: %s", w.Code, w.Body.String())
}
if !strings.Contains(w.Body.String(), `"status":"success"`) || !strings.Contains(w.Body.String(), `"count":1`) {
t.Errorf("unexpected body: %s", w.Body.String())
}
// 2. Test GET /api/maintenance/stats
reqStats := httptest.NewRequest("GET", "/api/maintenance/stats", nil)
wStats := httptest.NewRecorder()
mux.ServeHTTP(wStats, reqStats)
if wStats.Code != http.StatusOK {
t.Fatalf("expected status 200, got %d: %s", wStats.Code, wStats.Body.String())
}
if !strings.Contains(wStats.Body.String(), `"total_jobs":1`) {
t.Errorf("expected stats to show total_jobs:1, got %s", wStats.Body.String())
}
}