"""Doyle P3TDCW7C: provision omitted release fixture, exercise only untested wedge cell."""
import importlib.util
from pathlib import Path
import sys
sys.dont_write_bytecode = True
SOURCE = Path(__file__).with_name('run.py')
spec = importlib.util.spec_from_file_location('field302fixturebase',SOURCE)
p = importlib.util.module_from_spec(spec)
spec.loader.exec_module(p)
TEST = 'attach_wedge_dead_child_plus_dropped_pump_does_not_wedge_the_broker'

class FixturePhase(p.FieldPhase):
    def prepare(self):
        super().prepare()
        self.receipt['authorization_message'] = 'P3TDCW7C'
        self.receipt['artifact_invariance_authorization'] = 'OEUO574A'
        self.receipt['fixture_driver_sha256'] = p.g.digest(Path(__file__))
        self.receipt['original_attach_receipt_sha256'] = p.g.digest(p.OUT/'attach/receipt.json')
        self.flush()

    def execute(self):
        if self.name == 'wedge-provisioned':
            subjects = [p.g.TARGET/'release/spt.exe',p.g.TARGET/'release/spt.pdb']
            before = {str(path):p.g.digest(path) for path in subjects}
            self.receipt['spt_before_fixture'] = before
            self.flush()
            try:
                self.run('fixture-build',['cargo','build','--release','--locked','-p','mock-adapter','--bin','mock-session'])
            finally:
                after = {str(path):p.g.digest(path) for path in subjects}
                self.receipt['spt_after_fixture'] = after
                self.flush()
            if before != after:
                raise RuntimeError('fixture-only build changed SPT executable/PDB')
            fixture = p.g.TARGET/'release/mock-session.exe'
            if not fixture.is_file():
                raise RuntimeError('release mock-session absent after successful build')
            self.receipt['fixture'] = dict(path=str(fixture),sha256=p.g.digest(fixture),bytes=fixture.stat().st_size)
            scope = ['-p','spt','--test','attach_wedge_e2e']
            expression = 'test(='+TEST+')'
            selected = self.inventory('inventory',scope,expression)
            if selected != {('spt','attach_wedge_e2e',TEST)}:
                raise RuntimeError('exact failed-precondition cell inventory required')
            self.run('tests',['cargo','nextest','run','--release','--locked',*scope,'--profile','ci-windows','--test-threads','2','--retries','0','--no-fail-fast','--success-output','immediate','-E',expression])
            self.receipt['spt_after_wedge'] = {str(path):p.g.digest(path) for path in subjects}
            self.flush()
            if self.receipt['spt_after_wedge'] != before:
                raise RuntimeError('wedge cell changed SPT executable/PDB')
        elif self.name == 'checks-post-fixture':
            self.run('clippy',['cargo','clippy','--release','--locked','-p','spt-net','-p','spt-daemon','-p','spt-net-diag','--lib'],timeout=3600)
            self.run('trace',['traceable-reqs','check','--json'],timeout=600)
        elif self.name == 'version-post-fixture':
            self.run('version',[str(p.g.TARGET/'release/spt.exe'),'--version'],timeout=30)
        else:
            raise RuntimeError('unknown fixture followup phase')

p.r.Phase = FixturePhase
if __name__ == '__main__':
    sys.exit(p.r.main())
