Files
WoW-Hybrid-gateway/tests/content_batch_jobs_test.py
praxisandClaude Fable 5.2 7f2139d8d0 Offline test fixtures
883 files, from working commit b1a079a.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 01:32:48 +02:00

127 lines
9.4 KiB
Python

#!/usr/bin/env python3
"""Offline queue isolation/resume tests; no real converter, client or server."""
import contextlib
import io
import json
from pathlib import Path
import sys
import tempfile
import unittest
from unittest.mock import patch
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'ops'))
import content_batch_jobs as jobs
class JobsTest(unittest.TestCase):
def setUp(self):
self.temp=tempfile.TemporaryDirectory();self.root=Path(self.temp.name)
(self.root/'ops').mkdir();(self.root/'runtime').mkdir()
for name in ('content_batch_jobs.py','m2_equipment_convert.py','content_package_stage.py'):(self.root/'ops'/name).write_text('fixture')
(self.root/'source').write_text('source')
self.manifest=self.root/'models.json'
self.manifest.write_text(json.dumps(dict(matches=[dict(member='fixture.blp')])))
self.plan=self.root/'plan.json';self.output=self.root/'runtime/jobs'
self.guard=patch.object(jobs,'ROOT',self.root);self.guard.start()
self.command=dict(id='models',command=['m2_equipment_convert.py','--source-manifest','models.json','--output','{output}'],inputs=['source'],timeout_seconds=10)
def tearDown(self):self.guard.stop();self.temp.cleanup()
def run_jobs(self,entries,execute=True):
self.plan.write_text(json.dumps(dict(schema_version=1,jobs=entries)))
with contextlib.redirect_stdout(io.StringIO()):return jobs.run(self.plan,self.output,execute=execute)
def converter(self,argv,**kwargs):
dest=Path(argv[-1]);dest.mkdir();(dest/'manifest.json').write_text('{"converted":true}')
return type('Result',(),dict(returncode=0))()
def test_selection_runs_dependency_closure_and_keeps_unrelated_jobs_idle(self):
dependent=dict(self.command,id='publication',depends_on=['models'])
unrelated=dict(self.command,id='unrelated')
self.plan.write_text(json.dumps(dict(schema_version=1,jobs=[self.command,unrelated,dependent])))
with contextlib.redirect_stdout(io.StringIO()),patch.object(jobs.subprocess,'run',side_effect=self.converter)as call:
rows=jobs.run(self.plan,self.output,execute=True,only=['publication'])
self.assertEqual([r['id']for r in rows],['models','publication'])
self.assertEqual(call.call_count,2)
rows=jobs.run(self.plan,self.output,execute=True,only=['publication'])
self.assertEqual([r['status']for r in rows],['reused','reused'])
self.assertEqual(call.call_count,2)
with self.assertRaisesRegex(ValueError,'canonical job'):
jobs.run(self.plan,self.output,execute=True,only=['unknown'])
def test_execute_then_resume_requires_no_second_conversion(self):
with patch.object(jobs.subprocess,'run',side_effect=self.converter)as call:
first=self.run_jobs([self.command]);second=self.run_jobs([self.command])
self.assertEqual([first[0]['status'],second[0]['status']],['complete','reused']);self.assertEqual(call.call_count,1)
self.assertFalse(first[0]['installed']);self.assertFalse(first[0]['engine_loaded'])
def test_blocked_group_does_not_block_independent_converter(self):
with patch.object(jobs.subprocess,'run',side_effect=self.converter):
rows=self.run_jobs([dict(id='particles',blocked='converter required'),self.command])
self.assertEqual([r['status']for r in rows],['blocked','complete'])
def test_failed_job_blocks_dependents_but_not_independent_jobs(self):
dependent=dict(self.command,id='dependent',depends_on=['models'])
independent=dict(self.command,id='independent')
def call(argv,**kwargs):
if Path(argv[-1]).name.startswith('models-'):return type('Result',(),dict(returncode=1))()
return self.converter(argv,**kwargs)
with patch.object(jobs.subprocess,'run',side_effect=call):rows=self.run_jobs([self.command,dependent,independent])
self.assertEqual([r['status']for r in rows],['failed','blocked','complete'])
def test_dry_run_never_executes(self):
with patch.object(jobs.subprocess,'run',side_effect=AssertionError('dry run')):
self.assertEqual(self.run_jobs([self.command],False)[0]['status'],'ready')
def test_changed_input_gets_fresh_output_preserving_old_artifacts(self):
with patch.object(jobs.subprocess,'run',side_effect=self.converter)as call:
self.run_jobs([self.command]);(self.root/'source').write_text('changed')
self.run_jobs([self.command]);self.assertEqual(call.call_count,2)
self.assertEqual(len(list(self.output.glob('models-*/manifest.json'))),2)
def test_output_tampering_is_not_reused(self):
with patch.object(jobs.subprocess,'run',side_effect=self.converter):self.run_jobs([self.command])
next(self.output.glob('models-*/manifest.json')).write_text('changed')
with patch.object(jobs.subprocess,'run',side_effect=self.converter)as call:
rows=self.run_jobs([self.command]);self.assertEqual(rows[0]['status'],'complete');self.assertEqual(call.call_count,1)
def test_unreviewed_tool_and_forward_dependency_reject(self):
for entry in (dict(self.command,command=['start_native_client.py','--output','{output}']),dict(self.command,depends_on=['later'])):
with self.assertRaises(ValueError):self.run_jobs([entry])
def test_input_changed_during_job_does_not_publish_success_receipt(self):
def changing(argv,**kwargs):
result=self.converter(argv,**kwargs);(self.root/'source').write_text('changed');return result
with patch.object(jobs.subprocess,'run',side_effect=changing):rows=self.run_jobs([self.command])
self.assertEqual(rows[0]['status'],'failed');self.assertFalse((self.output/'models.json').exists())
def test_nested_source_model_change_invalidates_reuse(self):
model=self.root/('a'*64+'.m2');model.write_bytes(b'first')
self.manifest.write_text(json.dumps(dict(matches=[dict(member='fixture.m2',file=model.name)])))
with patch.object(jobs.subprocess,'run',side_effect=self.converter)as call:
self.run_jobs([self.command]);model.write_bytes(b'second')
self.run_jobs([self.command]);self.assertEqual(call.call_count,2)
def test_deferred_ui_work_is_distinct_from_a_technical_blocker(self):
with patch.object(jobs.subprocess,'run',side_effect=AssertionError('deferred')):
self.assertEqual(self.run_jobs([dict(id='login-ui',deferred='after content')])[0]['status'],'deferred')
def test_dependent_command_consumes_actual_output_of_this_run(self):
dependent=dict(id='stage',depends_on=['models'],command=['content_package_stage.py','--component','{models}/manifest.json','--output','{output}'],inputs=['{models}/manifest.json'],timeout_seconds=10)
with patch.object(jobs.subprocess,'run',side_effect=self.converter)as call:
rows=self.run_jobs([self.command,dependent])
self.assertEqual([r['status']for r in rows],['complete','complete'])
self.assertEqual(call.call_args_list[1].args[0][3],rows[0]['output_directory']+'/manifest.json')
def test_asset_stage_pins_nested_source_converted_and_baseline_files(self):
for name,value in [('source-assets.json',dict(matches=[dict(file='texture.blp')])),
('converted.json',dict(records=[dict(outputs=[dict(file='model.m2')])])),
('baseline.json',dict(assets=[dict(file='baseline.skin')]))]:
(self.root/name).write_text(json.dumps(value))
for name in ('texture.blp','model.m2','baseline.skin','root.bin'):(self.root/name).write_bytes(b'fixture')
command=['item_model_assets.py','--source-manifest','source-assets.json','--conversion-manifest','converted.json','--baseline-manifest','baseline.json','--native-root','root.bin','--output','{output}']
entry=dict(command=command,inputs=['source'])
paths=jobs.input_paths(entry,self.root/'ops/item_model_assets.py')
self.assertTrue({self.root/n for n in ('texture.blp','model.m2','baseline.skin','root.bin')}<=set(paths))
(self.root/'baseline.json').write_text(json.dumps(dict(assets=[dict(file='../escape')])) )
with self.assertRaisesRegex(ValueError,'Flat asset'):jobs.input_paths(entry,self.root/'ops/item_model_assets.py')
def test_framing_pins_native_metadata_and_each_actual_reuse_payload(self):
context=self.root/'native';context.mkdir();proof=self.root/'proof';proof.mkdir()
names=('target-encoding.bin','target-download.bin','native-tvfs-default.bin','native-vfs-root.bin','download-native-controls.json','tvfs-default-identity.json')
for name in names:(context/name).write_bytes(b'fixture')
key='a'*32;(proof/'report.json').write_text(json.dumps(dict(matches=[dict(content_key=key)])));(proof/(key+'.bin')).write_bytes(b'payload')
(self.root/'assets.json').write_text(json.dumps(dict(assets=[],root=dict(staged_root_md5='fixture'))));(self.root/'root.bin').write_bytes(b'root')
command=['casc_asset_package.py','--stage-framed-assets','--source-manifest','assets.json','--native-context',str(context),'--reuse-directory',str(proof),'--output','{output}']
paths=jobs.input_paths(dict(command=command,inputs=['source']),self.root/'ops/casc_asset_package.py')
self.assertTrue({context/n for n in names}<=set(paths));self.assertIn(proof/(key+'.bin'),paths);self.assertIn(self.root/'root.bin',paths)
if __name__=='__main__':unittest.main()