git-annex-mirror-orchestrator/gamo/tests/test_next_task.py
Michael Hanke 66edb55934
All checks were successful
Ruff / Code linting (push) Successful in 37s
Codespell / Check for spelling errors (push) Successful in 48s
Type annotations / check (push) Successful in 47s
Unit tests (Linux) / check (push) Successful in 2m44s
feat: next-task now queues mirrors with missing keys after minimum interval
2026-11-16 06:45:59 +01:00

306 lines
9.9 KiB
Python

"""
Functional tests for ``git-gamo-next-task`` (gamo.next_task).
The installed entry point is exercised against a real local ``git``
worktree. Records are written as JSON files into a ``mirror`` directory;
the command must emit the single most-urgent record as one JSON line.
"""
import json
from dataclasses import dataclass
from datetime import UTC, datetime, timedelta
from pathlib import Path
from subprocess import CompletedProcess
import pytest
from gamo.tests.conftest import GitEnv, entry_point
ENTRY_POINT = 'git-gamo-next-task'
def _iso(dt: datetime) -> str:
return dt.isoformat().replace('+00:00', 'Z')
def _past(hours: int = 1) -> str:
return _iso(datetime.now(UTC) - timedelta(hours=hours))
def make_record(
*,
name: str,
last_success: str | None,
ncommits: int = 0,
enabled: bool | None = None,
missing_keys: int = 0,
) -> dict:
"""Build a production-shaped record for the next-task command."""
record: dict = {
'id': f'mirror/{name}',
'source': f'https://example.com/{name}-source.git',
'target': f'https://example.com/{name}-target.git',
'schedule': {
'auto-disable-after-nerrors': 10,
'minimum-interval': 28800,
'maximum-interval': 2592000,
},
'state': {
'nerrors': 0,
'last_attempt': '2026-01-01T00:00:00Z',
'last30days_ncommits': ncommits,
},
}
if last_success is not None:
record['state']['last_success'] = last_success
if enabled is not None:
record['enabled'] = enabled
if missing_keys:
record['state']['keys_missing_n'] = missing_keys
return record
@dataclass
class NextTaskEnv:
entry: str
base: GitEnv
@property
def work(self) -> Path:
return self.base.work
def write(self, records: dict[str, dict], name: str = 'mirror') -> Path:
directory = self.work / name
directory.mkdir(parents=True, exist_ok=True)
for fname, record in records.items():
(directory / fname).write_text(json.dumps(record), encoding='utf-8')
return directory
def run(self, *args: str) -> CompletedProcess[str]:
return self.base.run(self.entry, *args)
@pytest.fixture
def next_task(git_env: GitEnv) -> NextTaskEnv:
git_env.git('init', '-q', '-b', 'main', str(git_env.work))
return NextTaskEnv(entry=entry_point(ENTRY_POINT), base=git_env)
def _emitted_id(result: CompletedProcess[str]) -> str:
return json.loads(result.stdout)['id']
def test_never_succeeded_sorts_first(next_task: NextTaskEnv) -> None:
past = make_record(name='past', last_success=_past(hours=100), ncommits=100)
nodue = make_record(name='nodue', last_success=None)
# the due record has the earlier filename; it must still lose
next_task.write({'a_past.json': past, 'z_nodue.json': nodue})
result = next_task.run()
assert result.returncode == 0, result.stderr
assert _emitted_id(result) == 'mirror/nodue'
def test_earliest_computed_due_wins(next_task: NextTaskEnv) -> None:
# with 100 commits in 30 days, the interval clamps to the 8 h minimum,
# so the due dates are 20 h and 12 h in the past respectively
earlier = make_record(name='earlier', last_success=_past(hours=20), ncommits=100)
later = make_record(name='later', last_success=_past(hours=12), ncommits=100)
# the earlier-due record has the later filename; the computed due date
# must dominate the file order
next_task.write({'z_later.json': later, 'a_earlier.json': earlier})
result = next_task.run()
assert result.returncode == 0, result.stderr
assert _emitted_id(result) == 'mirror/earlier'
def test_tie_broken_by_last30days_ncommits_highest_first(
next_task: NextTaskEnv,
) -> None:
# 3 commits in 30 days -> a 240 h interval; 10 commits -> 72 h; the
# last_success dates are picked so that both due dates land on the
# exact same instant
base = datetime.now(UTC)
low = make_record(
name='low',
last_success=_iso(base - timedelta(hours=240)),
ncommits=3,
)
high = make_record(
name='high',
last_success=_iso(base - timedelta(hours=72)),
ncommits=10,
)
# identical due date; the higher-ncommits record has the later filename
next_task.write({'a_low.json': low, 'z_high.json': high})
result = next_task.run()
assert result.returncode == 0, result.stderr
assert _emitted_id(result) == 'mirror/high'
def test_due_preferred_over_not_yet_due(next_task: NextTaskEnv) -> None:
# 100 commits in 30 days -> the 8 h minimum interval: 100 h ago is due,
# 1 h ago is not
past = make_record(name='past', last_success=_past(hours=100), ncommits=100)
future = make_record(name='future', last_success=_past(hours=1), ncommits=100)
next_task.write({'a.json': past, 'b.json': future})
result = next_task.run()
assert result.returncode == 0, result.stderr
assert _emitted_id(result) == 'mirror/past'
def test_not_yet_due_records_are_not_emitted(next_task: NextTaskEnv) -> None:
# zero commits in 30 days -> the 30 day maximum interval
future = make_record(name='future', last_success=_past(hours=1), ncommits=0)
next_task.write({'a.json': future})
result = next_task.run()
assert result.returncode == 0, result.stderr
assert result.stdout == ''
def test_stored_next_due_is_ignored(next_task: NextTaskEnv) -> None:
# the stale record is due per its stored next_due but not per the
# computed one; the due record is the other way around
stale = make_record(
name='stale',
last_success=_past(hours=1),
ncommits=0,
)
stale['schedule']['next_due'] = _past(hours=2)
due = make_record(
name='due',
last_success=_past(hours=100),
ncommits=100,
)
due['schedule']['next_due'] = '2999-01-01T00:00:00Z'
# trusting the stored values, stale would win
next_task.write({'a_stale.json': stale, 'z_due.json': due})
result = next_task.run()
assert result.returncode == 0, result.stderr
assert _emitted_id(result) == 'mirror/due'
def test_per_dataset_interval_override_is_honored(next_task: NextTaskEnv) -> None:
# 5000 commits in 30 days would compute a 8 h interval (not due after
# 4 h), but the per-dataset override shortens the minimum to 10 min
record = make_record(
name='demo',
last_success=_past(hours=4),
ncommits=5000,
)
record['schedule']['minimum-interval'] = 600
next_task.write({'a.json': record})
result = next_task.run()
assert result.returncode == 0, result.stderr
assert _emitted_id(result) == 'mirror/demo'
def test_custom_mirror_dir(next_task: NextTaskEnv) -> None:
past = make_record(name='past', last_success=_past(hours=100), ncommits=100)
next_task.write({'a.json': past}, name='custom')
result = next_task.run('--mirror', 'custom')
assert result.returncode == 0, result.stderr
assert _emitted_id(result) == 'mirror/past'
def test_disabled_record_is_ignored(next_task: NextTaskEnv) -> None:
# the disabled record has no last_success, so it would sort first and
# be emitted; it must be ignored completely instead
disabled = make_record(name='disabled', last_success=None, enabled=False)
active = make_record(
name='active',
last_success=_past(hours=100),
ncommits=100,
enabled=True,
)
next_task.write({'a_disabled.json': disabled, 'z_active.json': active})
result = next_task.run()
assert result.returncode == 0, result.stderr
assert _emitted_id(result) == 'mirror/active'
def test_only_disabled_records_are_not_emitted(next_task: NextTaskEnv) -> None:
disabled = make_record(
name='disabled',
last_success=_past(hours=100),
ncommits=100,
enabled=False,
)
next_task.write({'a.json': disabled})
result = next_task.run()
assert result.returncode == 0, result.stderr
assert result.stdout == ''
def test_incomplete_dormant_mirror_is_due_after_minimum_interval(
next_task: NextTaskEnv,
) -> None:
# zero commits in 30 days would mean the 30 day maximum interval for a
# complete mirror; missing keys pull the due date to the 8 h minimum
record = make_record(
name='incomplete',
last_success=_past(hours=9),
ncommits=0,
missing_keys=5,
)
next_task.write({'a.json': record})
result = next_task.run()
assert result.returncode == 0, result.stderr
assert _emitted_id(result) == 'mirror/incomplete'
def test_incomplete_mirror_sorts_before_earlier_due_complete_mirror(
next_task: NextTaskEnv,
) -> None:
# the complete mirror has been due for 92 h; the incomplete one only
# for 1 h, but the missing keys take priority
complete = make_record(
name='complete',
last_success=_past(hours=100),
ncommits=100,
)
incomplete = make_record(
name='incomplete',
last_success=_past(hours=9),
ncommits=0,
missing_keys=5,
)
# the incomplete record has the later filename
next_task.write({'z_incomplete.json': incomplete, 'a_complete.json': complete})
result = next_task.run()
assert result.returncode == 0, result.stderr
assert _emitted_id(result) == 'mirror/incomplete'
def test_not_yet_due_incomplete_mirror_does_not_block_due_records(
next_task: NextTaskEnv,
) -> None:
# the incomplete mirror comes due in 7 h; without the scan it would sit
# at the head of the sort and block the due record behind it
incomplete = make_record(
name='incomplete',
last_success=_past(hours=1),
ncommits=0,
missing_keys=5,
)
complete = make_record(
name='complete',
last_success=_past(hours=100),
ncommits=100,
)
next_task.write({'a_incomplete.json': incomplete, 'z_complete.json': complete})
result = next_task.run()
assert result.returncode == 0, result.stderr
assert _emitted_id(result) == 'mirror/complete'