git-annex-mirror-orchestrator/gamo/tests/test_store_task.py
Michael Hanke 3cf16c98ad
All checks were successful
Codespell / Check for spelling errors (push) Successful in 42s
Ruff / Code linting (push) Successful in 42s
Type annotations / check (push) Successful in 56s
Unit tests (Linux) / check (push) Successful in 2m27s
feat: due date computation now dynamic
The `next_due` logic now lives entirely in `next-task`, and nothing
stores it anymore.

Previously `next_due` was a stale snapshot: it was computed once by
`update-refs` after a successful run and frozen into the record, so it
couldn't react to anything that changed afterwards (e.g. a rule change).

Computing it on the fly in `next-task` makes the due date a pure, deterministic function of the record's current `last_success` and `last30days_ncommits` at selection time, so the schedule always reflects the dataset's actual activity, records that never succeeded are naturally "due now". It also shrinks the stored record to facts (what happened, when) and pushes the derived scheduling policy into the one component whose sole job is deciding what to run next, which is the classic split between state and scheduling logic.
2026-11-15 19:29:39 +01:00

156 lines
5.1 KiB
Python

"""
Functional tests for ``git-gamo-store-task`` (gamo.store_task).
The installed entry point is exercised against a real local ``git``
worktree. A JSON record is fed to the command on STDIN; the command
must store it as ``mirror/<id>.json``, commit the change, and echo the
stored file to STDOUT.
"""
import json
from dataclasses import dataclass
from pathlib import Path
from subprocess import CompletedProcess
import pytest
from gamo.tests.conftest import GitEnv, entry_point
from gamo.tests.test_next_task import make_record
ENTRY_POINT = 'git-gamo-store-task'
@dataclass
class StoreTaskEnv:
entry: str
base: GitEnv
@property
def work(self) -> Path:
return self.base.work
@property
def mirror(self) -> Path:
return self.work / 'mirror'
def git(self, *args: str) -> CompletedProcess[str]:
return self.base.git(*args)
def git_oneline(self, *args: str) -> str:
return self.base.git_oneline(*args)
def git_success(self, *args: str) -> bool:
return self.base.git_success(*args)
def run(self, *args: str, stdin: str = '') -> CompletedProcess[str]:
return self.base.run(self.entry, *args, stdin=stdin)
@pytest.fixture
def store_task(git_env: GitEnv) -> StoreTaskEnv:
git_env.git('init', '-q', '-b', 'main', str(git_env.work))
git_env.git('config', 'user.email', 't@example.com')
git_env.git('config', 'user.name', 'tester')
return StoreTaskEnv(entry=entry_point(ENTRY_POINT), base=git_env)
def _subject(store_task: StoreTaskEnv) -> str:
return store_task.git_oneline('log', '-1', '--format=%s')
def _ncommits(store_task: StoreTaskEnv) -> str:
return store_task.git_oneline('rev-list', '--count', 'HEAD')
def test_store_writes_and_commits_record(store_task: StoreTaskEnv) -> None:
record = make_record(name='demo', last_success=None, enabled=True)
record['id'] = 'demo'
result = store_task.run(stdin=json.dumps(record))
assert result.returncode == 0, result.stderr
path = store_task.mirror / 'demo.json'
content = path.read_text(encoding='utf-8')
assert json.loads(content) == record
# the stored file is echoed verbatim to standard output
assert result.stdout == content
assert _subject(store_task) == 'chore: update demo'
# the committed state matches the working tree
assert store_task.git('status', '--porcelain').stdout == ''
def test_formatting_is_deterministic(store_task: StoreTaskEnv) -> None:
# keys are deliberately out of order, and one value is non-ASCII
record = {
'state': {'last_attempt': '2026-01-01T00:00:00Z'},
'id': 'demo',
'note': 'über-dataset',
'source': 'https://example.com/demo-source.git',
}
expected = json.dumps(record, ensure_ascii=False, indent=2, sort_keys=True) + '\n'
result = store_task.run(stdin=json.dumps(record))
assert result.returncode == 0, result.stderr
path = store_task.mirror / 'demo.json'
assert path.read_text(encoding='utf-8') == expected
assert result.stdout == expected
def test_empty_stdin_is_a_noop(store_task: StoreTaskEnv) -> None:
result = store_task.run(stdin=' \n')
assert result.returncode == 0, result.stderr
assert result.stdout == ''
assert not store_task.mirror.exists()
assert not store_task.git_success('rev-parse', '--verify', 'HEAD')
def test_custom_commit_message(store_task: StoreTaskEnv) -> None:
record = make_record(name='demo', last_success=None)
record['id'] = 'demo'
result = store_task.run('--message', 'wip: demo', stdin=json.dumps(record))
assert result.returncode == 0, result.stderr
assert _subject(store_task) == 'wip: demo'
def test_custom_mirror_dir(store_task: StoreTaskEnv) -> None:
record = make_record(name='demo', last_success=None)
record['id'] = 'demo'
result = store_task.run('--mirror', 'specs', stdin=json.dumps(record))
assert result.returncode == 0, result.stderr
path = store_task.work / 'specs' / 'demo.json'
assert json.loads(path.read_text(encoding='utf-8')) == record
assert not store_task.mirror.exists()
def test_storing_twice_updates_the_record(store_task: StoreTaskEnv) -> None:
first = make_record(name='demo', last_success=None)
first['id'] = 'demo'
second = make_record(name='demo', last_success='2026-01-02T00:00:00Z', ncommits=7)
second['id'] = 'demo'
result = store_task.run(stdin=json.dumps(first))
assert result.returncode == 0, result.stderr
result = store_task.run(stdin=json.dumps(second))
assert result.returncode == 0, result.stderr
path = store_task.mirror / 'demo.json'
assert json.loads(path.read_text(encoding='utf-8')) == second
assert _ncommits(store_task) == '2'
def test_storing_unchanged_record_does_not_fail(store_task: StoreTaskEnv) -> None:
record = make_record(name='demo', last_success=None)
record['id'] = 'demo'
result = store_task.run(stdin=json.dumps(record))
assert result.returncode == 0, result.stderr
result = store_task.run(stdin=json.dumps(record))
assert result.returncode == 0, result.stderr
# the unchanged record is not committed a second time
assert _ncommits(store_task) == '1'