data-gen-bpu/scripts/test_migration_paths.py
bench db5a7a839d Export: read the running checkout's own state wherever it lives
A checkout whose state is elsewhere (BENCH_STATE_DIR, staging) was read as
<checkout>/state, so content review and export found no database and called
a PASS rollout unfinished. One helper, paths.state_dir, used by every lookup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 16:52:42 +08:00

69 lines
3.7 KiB
Python

"""Migration inventory and runtime must resolve the same config and state."""
import contextlib
import io
import json
import os
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch
from harness import paths
from scripts import migration_inventory
class MigrationPaths(unittest.TestCase):
def test_config_precedence_and_state_match_runtime_without_writes(self):
with tempfile.TemporaryDirectory() as temp:
root = Path(temp).resolve()
override = root / 'override.yaml'; override.write_text('paths: {}\n')
explicit = root / 'explicit.yaml'; explicit.write_text('paths: {}\n')
env = {'BENCH_ROOT': str(root), 'BENCH_CONFIG': str(override),
'BENCH_STATE_DIR': str(root / 'not-created')}
with patch.dict(os.environ, env, clear=True):
resolved = paths.resolve_installation()
self.assertEqual(resolved, (root, override, root / 'not-created'))
with contextlib.redirect_stdout(io.StringIO()) as output:
self.assertEqual(migration_inventory.main([]), 0)
report = json.loads(output.getvalue())
self.assertEqual(report['config'], str(override))
self.assertEqual(report['state'], str(root / 'not-created'))
storage = next(x for x in report['checks'] if x['name'] == 'storage.state')
self.assertEqual(storage['resolved'], report['state'])
with contextlib.redirect_stdout(io.StringIO()) as output:
self.assertEqual(migration_inventory.main(['--config', str(explicit)]), 0)
self.assertEqual(json.loads(output.getvalue())['config'], str(explicit))
self.assertFalse((root / 'not-created').exists())
def test_explicit_root_and_relative_paths_use_documented_cwd(self):
with tempfile.TemporaryDirectory() as temp:
root = Path(temp).resolve()
with patch.dict(os.environ, {'BENCH_ROOT': '/unused', 'BENCH_CONFIG': 'alternate.yaml',
'BENCH_STATE_DIR': 'scratch-state'}, clear=True):
actual = paths.resolve_installation(root=root)
self.assertEqual(actual, (root, (Path.cwd() / 'alternate.yaml').resolve(),
(Path.cwd() / 'scratch-state').resolve()))
with patch.dict(os.environ, {}, clear=True):
self.assertEqual(paths.resolve_installation(root=root),
(root, root / 'config.yaml', root / 'state'))
def test_export_reads_this_checkouts_state_wherever_it_lives(self):
# 10-19 staging: state outside the checkout; export looked in <checkout>/state and called a PASS unfinished.
import sqlite3
from export import to_agentrl
with tempfile.TemporaryDirectory() as temp:
checkout, state = Path(temp, 'checkout').resolve(), Path(temp, 'shared-state').resolve()
rd = checkout / 'runs/r/j/rollouts/00'; rd.mkdir(parents=True); state.mkdir()
with sqlite3.connect(state / 'jobs.sqlite') as con:
con.execute('CREATE TABLE rollouts(run_id,job_id,kk,state)')
con.execute("INSERT INTO rollouts VALUES('r','j',0,'PASS')")
con.close()
with patch.object(paths, 'BENCH_ROOT', checkout), patch.object(paths, 'STATE', state):
self.assertEqual(paths.state_dir(checkout), state)
self.assertTrue(to_agentrl._database_terminal(rd, checkout))
self.assertEqual(paths.state_dir(Path(temp, 'other')), Path(temp, 'other', 'state').resolve())
if __name__ == '__main__':
unittest.main()