from __future__ import annotations import shutil import subprocess import unittest from pathlib import Path SCRIPT = Path(__file__).with_name("cutover-recovered-public-db.ps1") class CutoverRecoveredPublicDatabaseTest(unittest.TestCase): @classmethod def setUpClass(cls) -> None: cls.source = SCRIPT.read_text(encoding="utf-8") def test_requires_explicit_confirmation_and_disables_watchdog_first(self) -> None: self.assertIn("if (-not $ConfirmCutover)", self.source) disable = self.source.index("Disable-ScheduledTask") stop = self.source.index("@('stop', $activeName)") self.assertLess(disable, stop) def test_preserves_both_old_and_failed_containers(self) -> None: self.assertIn("vignette-dev-db-pre-recovery-$stamp", self.source) self.assertIn("vignette-recovered-cutover-failed-$stamp", self.source) self.assertNotRegex( self.source, r"(?im)^\s*(?:&\s*)?docker(?:\.exe)?\s+(?:container\s+)?rm\b", ) self.assertNotRegex( self.source, r"(?im)^\s*(?:&\s*)?docker(?:\.exe)?\s+volume\s+rm\b", ) def test_gates_public_restart_on_exact_recovered_counts(self) -> None: counts = self.source.index("'84|30|705|0|0'") restart = self.source.index("Restart-PublicApi", counts) self.assertLess(counts, restart) self.assertIn("openapi_paths", self.source) def test_recovered_database_survives_docker_restart(self) -> None: self.assertIn("'--restart', 'unless-stopped'", self.source) def test_windows_powershell_51_parser_accepts_script(self) -> None: powershell = shutil.which("powershell.exe") if powershell is None: self.skipTest("Windows PowerShell 5.1 is unavailable") escaped = str(SCRIPT.resolve()).replace("'", "''") command = ( "$tokens=$null; $errors=$null; " "[System.Management.Automation.Language.Parser]::ParseFile(" f"'{escaped}', [ref]$tokens, [ref]$errors) | Out-Null; " "if ($errors.Count -gt 0) { exit 1 }; exit 0" ) completed = subprocess.run( [powershell, "-NoProfile", "-Command", command], check=False, capture_output=True, text=True, encoding="utf-8", ) self.assertEqual(completed.returncode, 0, completed.stderr) if __name__ == "__main__": unittest.main()