#!/usr/bin/env python3
"""Synthetic/source checks for the A18/M4 manual exploit prompt lifecycle.

No exploit, Location Services change, or UIKit presentation is performed.
"""
from pathlib import Path

ROOT = Path(__file__).resolve().parents[2]
SETTINGS = (ROOT / "Cyanide/SettingsViewController.m").read_text()
HEADER = (ROOT / "Cyanide/SettingsViewController.h").read_text()
SCENE = (ROOT / "Cyanide/SceneDelegate.m").read_text()

start = SETTINGS.index("static void settings_run_exploit_with_log_reason")
end = SETTINGS.index("BOOL settings_kernel_access_available", start)
prompt = SETTINGS[start:end]

# A missing, alert, transitioning, or still-presented controller now settles
# the caller instead of returning without a completion.
assert "if (!presenter || !presenter.view.window" in prompt
assert "presenter.presentedViewController" in prompt
assert "presenter.isBeingDismissed" in prompt
assert "presenter.isBeingPresented" in prompt
assert "if (completion) completion(NO);" in prompt
assert "Run Exploit prompt was not presented" in prompt

# The accepted Run Exploit action also has a definite result if its weak
# presenter disappears before the log can be started.
assert "if (p) {\n            settings_run_exploit_with_log_confirmed(p, completion);" in prompt
assert "else if (completion) {" in prompt
assert "presenting scene disappeared" in prompt

# Existing successful paths remain intact: the alert is presented, confirmation
# starts the log, and the normal ready callback remains available to callers.
assert "[presenter presentViewController:ac animated:YES completion:nil];" in prompt
assert "settings_run_exploit_with_log_confirmed(presenter, completion);" in prompt
assert "if (completion) completion(YES);" in prompt
assert "settings_run_exploit_with_log([me topViewController], ^(BOOL ready)" in SCENE
assert "handleActionURL:url completion:finish" in SCENE
assert "Completion is on the" in HEADER and "main queue and is called with NO" in HEADER


class PromptTransaction:
    """Tiny model of the external action's prompt boundary."""

    def __init__(self):
        self.busy = True
        self.outcomes = []
        self.settled = False

    def finish(self, ok, message):
        if not self.settled:
            self.settled = True
            self.outcomes.append((ok, message))

    def prompt_cannot_present(self):
        # Explicit NO is terminal, and the action is no longer busy.
        self.busy = False
        self.finish(False, "prompt unavailable")

    def accepted_presenter_disappears(self):
        self.busy = False
        self.finish(False, "presenter disappeared")


missing = PromptTransaction()
missing.prompt_cannot_present()
missing.prompt_cannot_present()  # duplicate lower-layer notification is inert
assert missing.outcomes == [(False, "prompt unavailable")]
assert not missing.busy and missing.settled

disappeared = PromptTransaction()
disappeared.accepted_presenter_disappears()
assert disappeared.outcomes == [(False, "presenter disappeared")]
assert not disappeared.busy

print("manual exploit prompt lifecycle synthetic/source contract: PASS")
