Some checks failed
Forge CI / smoke-and-build (pull_request) Failing after 1m42s
- Added scripts/deploy_crons.py to deploy cron jobs from YAML to jobs.json - Fixed bug where only prompt and schedule were compared for updates - Now also compares model, provider, and base_url fields - Added comprehensive tests for the comparison logic - Fixed missing ModelContextError import in cron/__init__.py The deploy-crons.py script reads cron jobs from a YAML configuration file (cron-jobs.yaml) and synchronizes them with the jobs.json file used by the Hermes scheduler. Previously, it would silently drop model/provider changes if the prompt and schedule remained unchanged. Fixes #375
351 lines
12 KiB
Python
351 lines
12 KiB
Python
"""
|
|
Tests for scripts/deploy-crons.py — cron job deployment from YAML.
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
import tempfile
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
# Add parent directory to path for imports
|
|
import sys
|
|
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
|
|
|
|
from scripts.deploy_crons import (
|
|
job_needs_update,
|
|
normalize_job_for_comparison,
|
|
find_matching_job,
|
|
)
|
|
|
|
|
|
class TestJobNeedsUpdate:
|
|
"""Test the job_needs_update function."""
|
|
|
|
def test_no_update_when_identical(self):
|
|
"""No update needed when jobs are identical."""
|
|
current = {
|
|
"prompt": "Check server status",
|
|
"schedule": {"kind": "interval", "minutes": 60},
|
|
"model": "claude-sonnet-4",
|
|
"provider": "anthropic",
|
|
}
|
|
desired = {
|
|
"prompt": "Check server status",
|
|
"schedule": {"kind": "interval", "minutes": 60},
|
|
"model": "claude-sonnet-4",
|
|
"provider": "anthropic",
|
|
}
|
|
assert job_needs_update(current, desired) is False
|
|
|
|
def test_update_when_prompt_changes(self):
|
|
"""Update needed when prompt changes."""
|
|
current = {
|
|
"prompt": "Check server status",
|
|
"schedule": {"kind": "interval", "minutes": 60},
|
|
"model": "claude-sonnet-4",
|
|
"provider": "anthropic",
|
|
}
|
|
desired = {
|
|
"prompt": "Check server health",
|
|
"schedule": {"kind": "interval", "minutes": 60},
|
|
"model": "claude-sonnet-4",
|
|
"provider": "anthropic",
|
|
}
|
|
assert job_needs_update(current, desired) is True
|
|
|
|
def test_update_when_schedule_changes(self):
|
|
"""Update needed when schedule changes."""
|
|
current = {
|
|
"prompt": "Check server status",
|
|
"schedule": {"kind": "interval", "minutes": 60},
|
|
"model": "claude-sonnet-4",
|
|
"provider": "anthropic",
|
|
}
|
|
desired = {
|
|
"prompt": "Check server status",
|
|
"schedule": {"kind": "interval", "minutes": 30},
|
|
"model": "claude-sonnet-4",
|
|
"provider": "anthropic",
|
|
}
|
|
assert job_needs_update(current, desired) is True
|
|
|
|
def test_update_when_model_changes(self):
|
|
"""Update needed when model changes (FIX for issue #375)."""
|
|
current = {
|
|
"prompt": "Check server status",
|
|
"schedule": {"kind": "interval", "minutes": 60},
|
|
"model": "claude-sonnet-4",
|
|
"provider": "anthropic",
|
|
}
|
|
desired = {
|
|
"prompt": "Check server status",
|
|
"schedule": {"kind": "interval", "minutes": 60},
|
|
"model": "claude-sonnet-4-6",
|
|
"provider": "anthropic",
|
|
}
|
|
assert job_needs_update(current, desired) is True
|
|
|
|
def test_update_when_provider_changes(self):
|
|
"""Update needed when provider changes (FIX for issue #375)."""
|
|
current = {
|
|
"prompt": "Check server status",
|
|
"schedule": {"kind": "interval", "minutes": 60},
|
|
"model": "claude-sonnet-4",
|
|
"provider": "anthropic",
|
|
}
|
|
desired = {
|
|
"prompt": "Check server status",
|
|
"schedule": {"kind": "interval", "minutes": 60},
|
|
"model": "claude-sonnet-4",
|
|
"provider": "openrouter",
|
|
}
|
|
assert job_needs_update(current, desired) is True
|
|
|
|
def test_update_when_model_added(self):
|
|
"""Update needed when model is added to a job that didn't have one."""
|
|
current = {
|
|
"prompt": "Check server status",
|
|
"schedule": {"kind": "interval", "minutes": 60},
|
|
"model": None,
|
|
"provider": None,
|
|
}
|
|
desired = {
|
|
"prompt": "Check server status",
|
|
"schedule": {"kind": "interval", "minutes": 60},
|
|
"model": "claude-sonnet-4",
|
|
"provider": "anthropic",
|
|
}
|
|
assert job_needs_update(current, desired) is True
|
|
|
|
def test_update_when_provider_added(self):
|
|
"""Update needed when provider is added to a job that didn't have one."""
|
|
current = {
|
|
"prompt": "Check server status",
|
|
"schedule": {"kind": "interval", "minutes": 60},
|
|
"model": "claude-sonnet-4",
|
|
"provider": None,
|
|
}
|
|
desired = {
|
|
"prompt": "Check server status",
|
|
"schedule": {"kind": "interval", "minutes": 60},
|
|
"model": "claude-sonnet-4",
|
|
"provider": "anthropic",
|
|
}
|
|
assert job_needs_update(current, desired) is True
|
|
|
|
|
|
class TestNormalizeJobForComparison:
|
|
"""Test the normalize_job_for_comparison function."""
|
|
|
|
def test_normalizes_job_correctly(self):
|
|
"""Test that job normalization extracts the right fields."""
|
|
job = {
|
|
"id": "abc123",
|
|
"name": "Test Job",
|
|
"prompt": "Do something",
|
|
"schedule": {"kind": "interval", "minutes": 60},
|
|
"model": "claude-sonnet-4",
|
|
"provider": "anthropic",
|
|
"base_url": "https://api.anthropic.com",
|
|
"extra_field": "ignored",
|
|
}
|
|
normalized = normalize_job_for_comparison(job)
|
|
assert normalized["prompt"] == "Do something"
|
|
assert normalized["schedule"] == {"kind": "interval", "minutes": 60}
|
|
assert normalized["model"] == "claude-sonnet-4"
|
|
assert normalized["provider"] == "anthropic"
|
|
assert normalized["base_url"] == "https://api.anthropic.com"
|
|
assert "id" not in normalized
|
|
assert "name" not in normalized
|
|
assert "extra_field" not in normalized
|
|
|
|
def test_handles_missing_fields(self):
|
|
"""Test that normalization handles missing fields gracefully."""
|
|
job = {
|
|
"prompt": "Do something",
|
|
}
|
|
normalized = normalize_job_for_comparison(job)
|
|
assert normalized["prompt"] == "Do something"
|
|
assert normalized["schedule"] == {}
|
|
assert normalized["model"] is None
|
|
assert normalized["provider"] is None
|
|
assert normalized["base_url"] is None
|
|
|
|
|
|
class TestFindMatchingJob:
|
|
"""Test the find_matching_job function."""
|
|
|
|
def test_finds_by_id(self):
|
|
"""Test finding a job by ID."""
|
|
jobs = [
|
|
{"id": "abc123", "name": "Job 1"},
|
|
{"id": "def456", "name": "Job 2"},
|
|
]
|
|
yaml_job = {"id": "abc123", "name": "Different Name"}
|
|
result = find_matching_job(jobs, yaml_job)
|
|
assert result is not None
|
|
assert result["id"] == "abc123"
|
|
|
|
def test_finds_by_name(self):
|
|
"""Test finding a job by name."""
|
|
jobs = [
|
|
{"id": "abc123", "name": "Job 1"},
|
|
{"id": "def456", "name": "Job 2"},
|
|
]
|
|
yaml_job = {"name": "Job 2"}
|
|
result = find_matching_job(jobs, yaml_job)
|
|
assert result is not None
|
|
assert result["id"] == "def456"
|
|
|
|
def test_returns_none_when_no_match(self):
|
|
"""Test that None is returned when no match is found."""
|
|
jobs = [
|
|
{"id": "abc123", "name": "Job 1"},
|
|
{"id": "def456", "name": "Job 2"},
|
|
]
|
|
yaml_job = {"name": "Nonexistent Job"}
|
|
result = find_matching_job(jobs, yaml_job)
|
|
assert result is None
|
|
|
|
def test_prefers_id_over_name(self):
|
|
"""Test that ID matching takes precedence over name matching."""
|
|
jobs = [
|
|
{"id": "abc123", "name": "Job 1"},
|
|
{"id": "def456", "name": "Job 2"},
|
|
]
|
|
yaml_job = {"id": "abc123", "name": "Job 2"}
|
|
result = find_matching_job(jobs, yaml_job)
|
|
assert result is not None
|
|
assert result["id"] == "abc123" # ID match takes precedence
|
|
|
|
|
|
class TestDeployCronsIntegration:
|
|
"""Integration tests for deploy-crons.py."""
|
|
|
|
@pytest.fixture
|
|
def temp_dir(self, tmp_path):
|
|
"""Create a temporary directory for test files."""
|
|
return tmp_path
|
|
|
|
@pytest.fixture
|
|
def sample_yaml(self, temp_dir):
|
|
"""Create a sample cron-jobs.yaml file."""
|
|
yaml_content = """
|
|
jobs:
|
|
- name: "Server Health Check"
|
|
prompt: "Check server health and report status"
|
|
schedule: "every 1h"
|
|
model: "claude-sonnet-4"
|
|
provider: "anthropic"
|
|
deliver: "local"
|
|
|
|
- name: "Database Backup"
|
|
prompt: "Run database backup"
|
|
schedule: "0 2 * * *"
|
|
model: "claude-sonnet-4"
|
|
provider: "anthropic"
|
|
deliver: "local"
|
|
"""
|
|
yaml_file = temp_dir / "cron-jobs.yaml"
|
|
yaml_file.write_text(yaml_content)
|
|
return yaml_file
|
|
|
|
@pytest.fixture
|
|
def sample_jobs_json(self, temp_dir):
|
|
"""Create a sample jobs.json file."""
|
|
jobs_data = {
|
|
"jobs": [
|
|
{
|
|
"id": "job1",
|
|
"name": "Server Health Check",
|
|
"prompt": "Check server status",
|
|
"schedule": {"kind": "interval", "minutes": 60, "display": "every 1h"},
|
|
"schedule_display": "every 1h",
|
|
"model": "claude-sonnet-4",
|
|
"provider": "anthropic",
|
|
"enabled": True,
|
|
"state": "scheduled",
|
|
},
|
|
{
|
|
"id": "job2",
|
|
"name": "Database Backup",
|
|
"prompt": "Run database backup",
|
|
"schedule": {"kind": "cron", "expr": "0 2 * * *", "display": "0 2 * * *"},
|
|
"schedule_display": "0 2 * * *",
|
|
"model": None, # No model specified
|
|
"provider": None, # No provider specified
|
|
"enabled": True,
|
|
"state": "scheduled",
|
|
},
|
|
],
|
|
"updated_at": "2026-04-13T00:00:00",
|
|
}
|
|
jobs_file = temp_dir / "jobs.json"
|
|
jobs_file.write_text(json.dumps(jobs_data, indent=2))
|
|
return jobs_file
|
|
|
|
def test_detects_model_change(self, sample_yaml, sample_jobs_json, temp_dir):
|
|
"""Test that model changes are detected (FIX for issue #375)."""
|
|
from scripts.deploy_crons import job_needs_update, normalize_job_for_comparison
|
|
|
|
# Simulate a job where model changed
|
|
current_job = {
|
|
"prompt": "Check server health and report status",
|
|
"schedule": {"kind": "interval", "minutes": 60},
|
|
"model": "claude-sonnet-4", # Current model
|
|
"provider": "anthropic",
|
|
}
|
|
desired_job = {
|
|
"prompt": "Check server health and report status",
|
|
"schedule": {"kind": "interval", "minutes": 60},
|
|
"model": "claude-sonnet-4-6", # New model
|
|
"provider": "anthropic",
|
|
}
|
|
assert job_needs_update(current_job, desired_job) is True
|
|
|
|
def test_detects_provider_change(self, sample_yaml, sample_jobs_json, temp_dir):
|
|
"""Test that provider changes are detected (FIX for issue #375)."""
|
|
from scripts.deploy_crons import job_needs_update
|
|
|
|
# Simulate a job where provider changed
|
|
current_job = {
|
|
"prompt": "Check server health and report status",
|
|
"schedule": {"kind": "interval", "minutes": 60},
|
|
"model": "claude-sonnet-4",
|
|
"provider": "anthropic", # Current provider
|
|
}
|
|
desired_job = {
|
|
"prompt": "Check server health and report status",
|
|
"schedule": {"kind": "interval", "minutes": 60},
|
|
"model": "claude-sonnet-4",
|
|
"provider": "openrouter", # New provider
|
|
}
|
|
assert job_needs_update(current_job, desired_job) is True
|
|
|
|
def test_no_update_when_only_prompt_unchanged(self, sample_yaml, sample_jobs_json, temp_dir):
|
|
"""Test that jobs are NOT updated when only prompt is unchanged but model/provider changed."""
|
|
from scripts.deploy_crons import job_needs_update
|
|
|
|
# This is the bug scenario: prompt unchanged, but model/provider changed
|
|
current_job = {
|
|
"prompt": "Check server health and report status",
|
|
"schedule": {"kind": "interval", "minutes": 60},
|
|
"model": None, # No model
|
|
"provider": None, # No provider
|
|
}
|
|
desired_job = {
|
|
"prompt": "Check server health and report status", # Same prompt
|
|
"schedule": {"kind": "interval", "minutes": 60}, # Same schedule
|
|
"model": "claude-sonnet-4", # New model added
|
|
"provider": "anthropic", # New provider added
|
|
}
|
|
# This should return True (needs update) because model/provider changed
|
|
assert job_needs_update(current_job, desired_job) is True
|
|
|
|
|
|
if __name__ == "__main__":
|
|
pytest.main([__file__, "-v"])
|