Compare commits
2 Commits
fix/902-cr
...
feat/673
| Author | SHA1 | Date | |
|---|---|---|---|
| 34e7de6a4c | |||
| dbabe0e6ae |
149
agent/crisis_resources.py
Normal file
149
agent/crisis_resources.py
Normal file
@@ -0,0 +1,149 @@
|
||||
"""
|
||||
988 Suicide & Crisis Lifeline Integration (#673).
|
||||
|
||||
When crisis is detected, provides immediate access to help:
|
||||
- Phone: 988 (call or text)
|
||||
- Text: Text HOME to 988
|
||||
- Chat: 988lifeline.org/chat
|
||||
- Spanish: 1-888-628-9454
|
||||
- Emergency: 911
|
||||
|
||||
This module provides the resource data. agent/crisis_protocol.py
|
||||
handles detection. This module formats the resources for display.
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import List
|
||||
|
||||
|
||||
@dataclass
|
||||
class CrisisResource:
|
||||
"""A crisis support contact method."""
|
||||
name: str
|
||||
contact: str
|
||||
description: str
|
||||
url: str = ""
|
||||
available: str = "24/7"
|
||||
language: str = "English"
|
||||
|
||||
|
||||
# 988 Suicide & Crisis Lifeline — all channels
|
||||
LIFELINE_988 = CrisisResource(
|
||||
name="988 Suicide and Crisis Lifeline",
|
||||
contact="Call or text 988",
|
||||
description="Free, confidential support for people in suicidal crisis or emotional distress.",
|
||||
url="https://988lifeline.org",
|
||||
available="24/7",
|
||||
language="English",
|
||||
)
|
||||
|
||||
LIFELINE_988_TEXT = CrisisResource(
|
||||
name="988 Crisis Text Line",
|
||||
contact="Text HOME to 988",
|
||||
description="Free, 24/7 crisis support via text message.",
|
||||
url="",
|
||||
available="24/7",
|
||||
language="English",
|
||||
)
|
||||
|
||||
LIFELINE_988_CHAT = CrisisResource(
|
||||
name="988 Lifeline Chat",
|
||||
contact="988lifeline.org/chat",
|
||||
description="Free, confidential online chat with a trained crisis counselor.",
|
||||
url="https://988lifeline.org/chat",
|
||||
available="24/7",
|
||||
language="English",
|
||||
)
|
||||
|
||||
LIFELINE_988_SPANISH = CrisisResource(
|
||||
name="988 Lifeline (Spanish)",
|
||||
contact="1-888-628-9454",
|
||||
description="Línea de prevención del suicidio en español.",
|
||||
url="https://988lifeline.org/help-yourself/en-espanol/",
|
||||
available="24/7",
|
||||
language="Spanish",
|
||||
)
|
||||
|
||||
CRISIS_TEXT_LINE = CrisisResource(
|
||||
name="Crisis Text Line",
|
||||
contact="Text HOME to 741741",
|
||||
description="Free, 24/7 crisis support via text message.",
|
||||
url="https://www.crisistextline.org",
|
||||
available="24/7",
|
||||
language="English",
|
||||
)
|
||||
|
||||
EMERGENCY_911 = CrisisResource(
|
||||
name="Emergency Services",
|
||||
contact="911",
|
||||
description="Immediate danger — police, fire, ambulance.",
|
||||
url="",
|
||||
available="24/7",
|
||||
language="Any",
|
||||
)
|
||||
|
||||
# All resources in priority order
|
||||
ALL_RESOURCES: List[CrisisResource] = [
|
||||
EMERGENCY_911,
|
||||
LIFELINE_988,
|
||||
LIFELINE_988_TEXT,
|
||||
LIFELINE_988_CHAT,
|
||||
CRISIS_TEXT_LINE,
|
||||
LIFELINE_988_SPANISH,
|
||||
]
|
||||
|
||||
|
||||
def get_crisis_resources(language: str = None) -> List[CrisisResource]:
|
||||
"""Get crisis resources, optionally filtered by language.
|
||||
|
||||
Args:
|
||||
language: Filter by language ("English", "Spanish", or None for all)
|
||||
|
||||
Returns:
|
||||
List of CrisisResource objects
|
||||
"""
|
||||
if language:
|
||||
return [r for r in ALL_RESOURCES if r.language.lower() == language.lower()]
|
||||
return ALL_RESOURCES
|
||||
|
||||
|
||||
def format_crisis_resources(resources: List[CrisisResource] = None) -> str:
|
||||
"""Format crisis resources as a user-facing message.
|
||||
|
||||
Args:
|
||||
resources: List of resources to format. Defaults to all resources.
|
||||
|
||||
Returns:
|
||||
Formatted string suitable for displaying to a user in crisis.
|
||||
"""
|
||||
if resources is None:
|
||||
resources = ALL_RESOURCES
|
||||
|
||||
lines = ["**Please reach out — help is available right now:**
|
||||
"]
|
||||
|
||||
for r in resources:
|
||||
if r.url:
|
||||
lines.append(f"- **{r.name}:** {r.contact} ({r.url})")
|
||||
else:
|
||||
lines.append(f"- **{r.name}:** {r.contact}")
|
||||
|
||||
lines.append("")
|
||||
lines.append("All services are free, confidential, and available 24/7.")
|
||||
lines.append("You are not alone.")
|
||||
|
||||
return "
|
||||
".join(lines)
|
||||
|
||||
|
||||
def get_immediate_help_message() -> str:
|
||||
"""Get the most urgent crisis help message.
|
||||
|
||||
Used when crisis is detected at CRITICAL level.
|
||||
"""
|
||||
return (
|
||||
"If you are in immediate danger, call **911** right now.
|
||||
|
||||
"
|
||||
+ format_crisis_resources()
|
||||
)
|
||||
@@ -1,50 +0,0 @@
|
||||
# Cron Audit & Cleanup
|
||||
|
||||
Identifies and removes dead/stale cron jobs that waste scheduler cycles.
|
||||
|
||||
## Problem
|
||||
|
||||
Of 69 cron jobs, 9 had zero completions — running on schedule but never producing results. Each execution costs API tokens and scheduler cycles.
|
||||
|
||||
## Dead Jobs Found (2026-04-14)
|
||||
|
||||
| Job | Schedule | Completions |
|
||||
|-----|----------|-------------|
|
||||
| exp-swarm-pipeline | every 10 min | 0 |
|
||||
| exp-music-generator | every 2h | 0 |
|
||||
| exp-paper-citations | every 3h | 0 |
|
||||
| exp-gbrain-patterns | every 2h | 0 |
|
||||
| exp-infra-hardening | every 2h | 0 |
|
||||
| gemma4-multimodal-burn | every 1h | 0 |
|
||||
| morning-paper-report | daily | 0 |
|
||||
| overnight-collector | every 15 min | 0 |
|
||||
| morning-experiment-report | daily | 0 |
|
||||
|
||||
## Usage
|
||||
|
||||
```bash
|
||||
# Show dead jobs (zero completions)
|
||||
python3 scripts/cron_audit.py
|
||||
|
||||
# Show all jobs with status
|
||||
python3 scripts/cron_audit.py --all
|
||||
|
||||
# Find stale jobs (no runs in 7 days)
|
||||
python3 scripts/cron_audit.py --older-than 7
|
||||
|
||||
# Disable dead jobs (pause them)
|
||||
python3 scripts/cron_audit.py --disable
|
||||
|
||||
# Delete dead jobs permanently
|
||||
python3 scripts/cron_audit.py --delete
|
||||
|
||||
# JSON output for automation
|
||||
python3 scripts/cron_audit.py --json
|
||||
```
|
||||
|
||||
## Job Categories
|
||||
|
||||
- **Healthy**: enabled, has completions, ran recently
|
||||
- **Dead**: enabled, zero completions, exists for N+ days
|
||||
- **Stale**: enabled, no successful run in N days
|
||||
- **Disabled**: paused or completed
|
||||
@@ -1,279 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Cron Audit & Cleanup — find and remove dead/stale cron jobs.
|
||||
|
||||
Identifies jobs that waste scheduler cycles:
|
||||
- Dead jobs: zero completions despite running for days
|
||||
- Stale jobs: no successful run in N days
|
||||
- Error jobs: high error ratio
|
||||
|
||||
Usage:
|
||||
# Show dead jobs (zero completions)
|
||||
python3 scripts/cron_audit.py
|
||||
|
||||
# Show stale jobs (no runs in 7 days)
|
||||
python3 scripts/cron_audit.py --older-than 7
|
||||
|
||||
# Show all jobs with status
|
||||
python3 scripts/cron_audit.py --all
|
||||
|
||||
# Disable dead jobs (sets enabled=False, state=paused)
|
||||
python3 scripts/cron_audit.py --disable
|
||||
|
||||
# Delete dead jobs permanently
|
||||
python3 scripts/cron_audit.py --delete
|
||||
|
||||
# Custom threshold: dead = 0 completions after N days
|
||||
python3 scripts/cron_audit.py --min-age 3
|
||||
|
||||
# JSON output
|
||||
python3 scripts/cron_audit.py --json
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
# hermes cron is a sibling module
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
|
||||
try:
|
||||
from cron.jobs import (
|
||||
list_jobs,
|
||||
load_jobs,
|
||||
save_jobs,
|
||||
update_job,
|
||||
remove_job,
|
||||
)
|
||||
except ImportError:
|
||||
# Fallback: direct file access
|
||||
JOBS_FILE = Path.home() / ".hermes" / "cron" / "jobs.json"
|
||||
|
||||
def load_jobs() -> list:
|
||||
if JOBS_FILE.exists():
|
||||
return json.loads(JOBS_FILE.read_text())
|
||||
return []
|
||||
|
||||
def save_jobs(jobs: list):
|
||||
JOBS_FILE.parent.mkdir(parents=True, exist_ok=True)
|
||||
JOBS_FILE.write_text(json.dumps(jobs, indent=2))
|
||||
|
||||
def list_jobs(include_disabled=False):
|
||||
jobs = load_jobs()
|
||||
if not include_disabled:
|
||||
jobs = [j for j in jobs if j.get("enabled", True)]
|
||||
return jobs
|
||||
|
||||
def update_job(job_id, updates):
|
||||
jobs = load_jobs()
|
||||
for job in jobs:
|
||||
if job["id"] == job_id:
|
||||
job.update(updates)
|
||||
save_jobs(jobs)
|
||||
return job
|
||||
return None
|
||||
|
||||
def remove_job(job_id):
|
||||
jobs = load_jobs()
|
||||
original = len(jobs)
|
||||
jobs = [j for j in jobs if j["id"] != job_id]
|
||||
if len(jobs) < original:
|
||||
save_jobs(jobs)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
# ── Analysis ──────────────────────────────────────────────────────────────
|
||||
|
||||
def parse_schedule_display(job: dict) -> str:
|
||||
"""Get human-readable schedule from job."""
|
||||
sched = job.get("schedule", {})
|
||||
if isinstance(sched, dict):
|
||||
return sched.get("display", job.get("schedule_display", "?"))
|
||||
return str(sched)
|
||||
|
||||
|
||||
def get_last_run_age(job: dict) -> Optional[timedelta]:
|
||||
"""Get time since last run."""
|
||||
last = job.get("last_run_at")
|
||||
if not last:
|
||||
return None
|
||||
try:
|
||||
if isinstance(last, str):
|
||||
last_dt = datetime.fromisoformat(last.replace("Z", "+00:00"))
|
||||
else:
|
||||
return None
|
||||
return datetime.now(timezone.utc) - last_dt
|
||||
except (ValueError, TypeError):
|
||||
return None
|
||||
|
||||
|
||||
def analyze_jobs(min_age_days: int = 0, stale_days: int = 0) -> Dict[str, List[dict]]:
|
||||
"""Analyze all jobs and categorize them.
|
||||
|
||||
Returns dict with keys: dead, stale, healthy, disabled, completed.
|
||||
"""
|
||||
all_jobs = list_jobs(include_disabled=True)
|
||||
now = datetime.now(timezone.utc)
|
||||
|
||||
result = {"dead": [], "stale": [], "healthy": [], "disabled": [], "completed": []}
|
||||
|
||||
for job in all_jobs:
|
||||
job_id = job.get("id", "?")
|
||||
name = job.get("name", job_id)
|
||||
enabled = job.get("enabled", True)
|
||||
state = job.get("state", "scheduled")
|
||||
completed = job.get("repeat", {}).get("completed", 0)
|
||||
schedule = parse_schedule_display(job)
|
||||
last_run = job.get("last_run_at")
|
||||
last_status = job.get("last_status", "never")
|
||||
last_error = job.get("last_error")
|
||||
created = job.get("created_at", "")
|
||||
|
||||
# Calculate age
|
||||
age_days = 0
|
||||
if created:
|
||||
try:
|
||||
created_dt = datetime.fromisoformat(created.replace("Z", "+00:00"))
|
||||
age_days = (now - created_dt).days
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
|
||||
last_age = get_last_run_age(job)
|
||||
|
||||
entry = {
|
||||
"id": job_id,
|
||||
"name": name,
|
||||
"schedule": schedule,
|
||||
"enabled": enabled,
|
||||
"state": state,
|
||||
"completed": completed,
|
||||
"last_run_at": last_run,
|
||||
"last_status": last_status,
|
||||
"last_error": last_error,
|
||||
"age_days": age_days,
|
||||
"last_run_age_days": last_age.days if last_age else None,
|
||||
}
|
||||
|
||||
if not enabled or state == "completed":
|
||||
result["disabled"].append(entry)
|
||||
elif completed == 0 and age_days >= min_age_days:
|
||||
result["dead"].append(entry)
|
||||
elif stale_days > 0 and last_age and last_age.days >= stale_days:
|
||||
result["stale"].append(entry)
|
||||
else:
|
||||
result["healthy"].append(entry)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
# ── Actions ───────────────────────────────────────────────────────────────
|
||||
|
||||
def disable_jobs(jobs: List[dict]) -> int:
|
||||
"""Disable dead/stale jobs (pause them)."""
|
||||
count = 0
|
||||
for j in jobs:
|
||||
result = update_job(j["id"], {"enabled": False, "state": "paused"})
|
||||
if result:
|
||||
count += 1
|
||||
print(f" DISABLED: {j['name']} ({j['schedule']})")
|
||||
return count
|
||||
|
||||
|
||||
def delete_jobs(jobs: List[dict]) -> int:
|
||||
"""Permanently delete jobs."""
|
||||
count = 0
|
||||
for j in jobs:
|
||||
if remove_job(j["id"]):
|
||||
count += 1
|
||||
print(f" DELETED: {j['name']} ({j['schedule']})")
|
||||
return count
|
||||
|
||||
|
||||
# ── Report ────────────────────────────────────────────────────────────────
|
||||
|
||||
def print_table(jobs: List[dict], title: str):
|
||||
"""Print a table of jobs."""
|
||||
if not jobs:
|
||||
return
|
||||
print(f"
|
||||
{title} ({len(jobs)}):")
|
||||
print(f" {'Name':<35} {'Schedule':<15} {'Completed':<10} {'Last Run':<15} {'Status'}")
|
||||
print(f" {'-'*35} {'-'*15} {'-'*10} {'-'*15} {'-'*10}")
|
||||
for j in jobs:
|
||||
last_run = "never"
|
||||
if j["last_run_age_days"] is not None:
|
||||
last_run = f"{j['last_run_age_days']}d ago"
|
||||
elif j["last_run_at"]:
|
||||
last_run = j["last_run_at"][:10]
|
||||
status = j["last_status"] or "never"
|
||||
print(f" {j['name']:<35} {j['schedule']:<15} {j['completed']:<10} {last_run:<15} {status}")
|
||||
|
||||
|
||||
# ── CLI ───────────────────────────────────────────────────────────────────
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Cron audit & cleanup")
|
||||
parser.add_argument("--all", action="store_true", help="Show all jobs including healthy")
|
||||
parser.add_argument("--older-than", type=int, default=0,
|
||||
help="Stale threshold in days (jobs with no runs in N days)")
|
||||
parser.add_argument("--min-age", type=int, default=0,
|
||||
help="Minimum job age in days to be considered dead")
|
||||
parser.add_argument("--disable", action="store_true", help="Disable dead jobs")
|
||||
parser.add_argument("--delete", action="store_true", help="Delete dead jobs permanently")
|
||||
parser.add_argument("--json", dest="json_output", action="store_true", help="JSON output")
|
||||
args = parser.parse_args()
|
||||
|
||||
analysis = analyze_jobs(min_age_days=args.min_age, stale_days=args.older_than)
|
||||
|
||||
if args.json_output:
|
||||
print(json.dumps(analysis, indent=2))
|
||||
return
|
||||
|
||||
# Summary
|
||||
total = sum(len(v) for v in analysis.values())
|
||||
print(f"Cron Audit — {total} total jobs")
|
||||
print(f" Healthy: {len(analysis['healthy'])}")
|
||||
print(f" Dead: {len(analysis['dead'])}")
|
||||
print(f" Stale: {len(analysis['stale'])}")
|
||||
print(f" Disabled: {len(analysis['disabled'])}")
|
||||
print(f" Completed: {len(analysis['completed'])}")
|
||||
|
||||
if args.all:
|
||||
print_table(analysis["healthy"], "HEALTHY")
|
||||
print_table(analysis["dead"], "DEAD (zero completions)")
|
||||
print_table(analysis["stale"], "STALE (no recent runs)")
|
||||
|
||||
if not args.disable and not args.delete:
|
||||
if analysis["dead"] or analysis["stale"]:
|
||||
print(f"
|
||||
To clean up: --disable (pause) or --delete (permanent)")
|
||||
return
|
||||
|
||||
targets = analysis["dead"] + analysis["stale"]
|
||||
if not targets:
|
||||
print("
|
||||
Nothing to clean up.")
|
||||
return
|
||||
|
||||
if args.delete:
|
||||
confirm = input(f"
|
||||
Delete {len(targets)} jobs permanently? [y/N] ")
|
||||
if confirm.lower() != "y":
|
||||
print("Aborted.")
|
||||
return
|
||||
count = delete_jobs(targets)
|
||||
print(f"
|
||||
Deleted {count} jobs.")
|
||||
elif args.disable:
|
||||
count = disable_jobs(targets)
|
||||
print(f"
|
||||
Disabled {count} jobs.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
79
tests/test_crisis_resources.py
Normal file
79
tests/test_crisis_resources.py
Normal file
@@ -0,0 +1,79 @@
|
||||
"""Tests for 988 Crisis Lifeline integration (#673)."""
|
||||
|
||||
import pytest
|
||||
from agent.crisis_resources import (
|
||||
LIFELINE_988,
|
||||
LIFELINE_988_TEXT,
|
||||
LIFELINE_988_CHAT,
|
||||
LIFELINE_988_SPANISH,
|
||||
CRISIS_TEXT_LINE,
|
||||
EMERGENCY_911,
|
||||
ALL_RESOURCES,
|
||||
get_crisis_resources,
|
||||
format_crisis_resources,
|
||||
get_immediate_help_message,
|
||||
CrisisResource,
|
||||
)
|
||||
|
||||
|
||||
class TestCrisisResources:
|
||||
def test_988_phone(self):
|
||||
assert "988" in LIFELINE_988.contact
|
||||
assert "24/7" in LIFELINE_988.available
|
||||
|
||||
def test_988_text(self):
|
||||
assert "HOME" in LIFELINE_988_TEXT.contact
|
||||
assert "988" in LIFELINE_988_TEXT.contact
|
||||
|
||||
def test_988_chat(self):
|
||||
assert "988lifeline.org/chat" in LIFELINE_988_CHAT.url
|
||||
|
||||
def test_988_spanish(self):
|
||||
assert "1-888-628-9454" in LIFELINE_988_SPANISH.contact
|
||||
assert LIFELINE_988_SPANISH.language == "Spanish"
|
||||
|
||||
def test_crisis_text_line(self):
|
||||
assert "741741" in CRISIS_TEXT_LINE.contact
|
||||
|
||||
def test_911(self):
|
||||
assert "911" in EMERGENCY_911.contact
|
||||
|
||||
def test_all_resources_not_empty(self):
|
||||
assert len(ALL_RESOURCES) >= 5
|
||||
|
||||
|
||||
class TestGetResources:
|
||||
def test_returns_all_by_default(self):
|
||||
assert len(get_crisis_resources()) == len(ALL_RESOURCES)
|
||||
|
||||
def test_filter_english(self):
|
||||
english = get_crisis_resources("English")
|
||||
assert all(r.language == "English" for r in english)
|
||||
assert len(english) > 0
|
||||
|
||||
def test_filter_spanish(self):
|
||||
spanish = get_crisis_resources("Spanish")
|
||||
assert len(spanish) >= 1
|
||||
assert all(r.language == "Spanish" for r in spanish)
|
||||
|
||||
|
||||
class TestFormatting:
|
||||
def test_format_includes_988(self):
|
||||
msg = format_crisis_resources()
|
||||
assert "988" in msg
|
||||
|
||||
def test_format_includes_741741(self):
|
||||
msg = format_crisis_resources()
|
||||
assert "741741" in msg
|
||||
|
||||
def test_format_includes_911(self):
|
||||
msg = format_crisis_resources()
|
||||
assert "911" in msg
|
||||
|
||||
def test_immediate_help_includes_911_first(self):
|
||||
msg = get_immediate_help_message()
|
||||
assert msg.startswith("If you are in immediate danger")
|
||||
|
||||
def test_format_not_empty(self):
|
||||
msg = format_crisis_resources()
|
||||
assert len(msg) > 100
|
||||
@@ -1,143 +0,0 @@
|
||||
"""Tests for cron audit script."""
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Test data
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _make_job(name, job_id=None, completed=0, enabled=True, state="scheduled",
|
||||
last_run=None, last_status=None, created=None, schedule="every 1h"):
|
||||
return {
|
||||
"id": job_id or f"job-{name}",
|
||||
"name": name,
|
||||
"enabled": enabled,
|
||||
"state": state,
|
||||
"schedule": {"display": schedule} if isinstance(schedule, str) else schedule,
|
||||
"repeat": {"completed": completed},
|
||||
"last_run_at": last_run,
|
||||
"last_status": last_status,
|
||||
"created_at": created or datetime.now(timezone.utc).isoformat(),
|
||||
}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# analyze_jobs
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestAnalyzeJobs:
|
||||
@patch("cron_audit.list_jobs")
|
||||
def test_dead_job_detected(self, mock_list):
|
||||
"""Job with 0 completions and old creation date is dead."""
|
||||
old_date = (datetime.now(timezone.utc) - timedelta(days=5)).isoformat()
|
||||
mock_list.return_value = [
|
||||
_make_job("dead-job", completed=0, created=old_date),
|
||||
]
|
||||
from cron_audit import analyze_jobs
|
||||
result = analyze_jobs(min_age_days=1)
|
||||
assert len(result["dead"]) == 1
|
||||
assert result["dead"][0]["name"] == "dead-job"
|
||||
|
||||
@patch("cron_audit.list_jobs")
|
||||
def test_healthy_job_not_dead(self, mock_list):
|
||||
"""Job with completions is healthy."""
|
||||
mock_list.return_value = [
|
||||
_make_job("good-job", completed=42, last_run=datetime.now(timezone.utc).isoformat()),
|
||||
]
|
||||
from cron_audit import analyze_jobs
|
||||
result = analyze_jobs()
|
||||
assert len(result["dead"]) == 0
|
||||
assert len(result["healthy"]) == 1
|
||||
|
||||
@patch("cron_audit.list_jobs")
|
||||
def test_disabled_job_categorized(self, mock_list):
|
||||
"""Disabled job goes to disabled category."""
|
||||
mock_list.return_value = [
|
||||
_make_job("paused-job", enabled=False, state="paused"),
|
||||
]
|
||||
from cron_audit import analyze_jobs
|
||||
result = analyze_jobs()
|
||||
assert len(result["disabled"]) == 1
|
||||
|
||||
@patch("cron_audit.list_jobs")
|
||||
def test_stale_job_detected(self, mock_list):
|
||||
"""Job with last run > N days ago is stale."""
|
||||
old_run = (datetime.now(timezone.utc) - timedelta(days=10)).isoformat()
|
||||
mock_list.return_value = [
|
||||
_make_job("stale-job", completed=5, last_run=old_run),
|
||||
]
|
||||
from cron_audit import analyze_jobs
|
||||
result = analyze_jobs(stale_days=7)
|
||||
assert len(result["stale"]) == 1
|
||||
|
||||
@patch("cron_audit.list_jobs")
|
||||
def test_completed_job_disabled(self, mock_list):
|
||||
"""Job with state=completed goes to disabled."""
|
||||
mock_list.return_value = [
|
||||
_make_job("done-job", completed=100, state="completed", enabled=False),
|
||||
]
|
||||
from cron_audit import analyze_jobs
|
||||
result = analyze_jobs()
|
||||
assert len(result["disabled"]) == 1
|
||||
|
||||
@patch("cron_audit.list_jobs")
|
||||
def test_empty_jobs(self, mock_list):
|
||||
"""No jobs returns empty categories."""
|
||||
mock_list.return_value = []
|
||||
from cron_audit import analyze_jobs
|
||||
result = analyze_jobs()
|
||||
for v in result.values():
|
||||
assert len(v) == 0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# get_last_run_age
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestGetLastRunAge:
|
||||
def test_returns_timedelta(self):
|
||||
from cron_audit import get_last_run_age
|
||||
recent = (datetime.now(timezone.utc) - timedelta(hours=2)).isoformat()
|
||||
age = get_last_run_age({"last_run_at": recent})
|
||||
assert age is not None
|
||||
assert age.days == 0
|
||||
assert age.seconds >= 7000 # ~2 hours
|
||||
|
||||
def test_returns_none_for_no_run(self):
|
||||
from cron_audit import get_last_run_age
|
||||
assert get_last_run_age({"last_run_at": None}) is None
|
||||
assert get_last_run_age({}) is None
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# disable_jobs / delete_jobs
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestJobActions:
|
||||
@patch("cron_audit.update_job")
|
||||
def test_disable_calls_update(self, mock_update):
|
||||
mock_update.return_value = {"id": "x"}
|
||||
from cron_audit import disable_jobs
|
||||
jobs = [{"id": "j1", "name": "test", "schedule": "1h"}]
|
||||
count = disable_jobs(jobs)
|
||||
assert count == 1
|
||||
mock_update.assert_called_once_with("j1", {"enabled": False, "state": "paused"})
|
||||
|
||||
@patch("cron_audit.remove_job")
|
||||
def test_delete_calls_remove(self, mock_remove):
|
||||
mock_remove.return_value = True
|
||||
from cron_audit import delete_jobs
|
||||
jobs = [{"id": "j1", "name": "test", "schedule": "1h"}]
|
||||
count = delete_jobs(jobs)
|
||||
assert count == 1
|
||||
mock_remove.assert_called_once_with("j1")
|
||||
Reference in New Issue
Block a user