Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

59.4 KB
Raw Blame
"""tools/release_build.py: the local desktop release script (#482 Phases 0-1). Every subprocess (git, Unity, the player,
the lock) is faked; nothing here starts Unity or touches the network."""
import hashlib
import json
from pathlib import Path

import pytest

import release_build as rb

H1950 = "ca953c983faa2a85216efdf264bf1da9167c9787a06d21a73c76717dfab0f8b4"
H1952 = "b42380ab7935e4daabd0577fa3df231c474f3ad92f6006af0a05918a46743915"
SHA = "1b7a36cb" + "0" * 32
LICENCE_TEXTS = ("CC-BY-4.0.txt", "CC-BY-SA-4.0.txt", "CC0-1.0.txt", "YamlDotNet-MIT.txt")
NOTICES = ["LICENSE", "LICENSES/CC-BY-4.0.txt", "LICENSES/CC-BY-SA-4.0.txt", "LICENSES/CC0-1.0.txt",
           "LICENSES/YamlDotNet-MIT.txt", "NOTICE", "OFL-EBGaramond.txt", "OFL-FrankRuhlLibre.txt", "THIRD-PARTY-NOTICES.md"]
FONT_SOURCES = {"OFL-EBGaramond.txt": "unity/Assets/UI/Fonts/OFL.txt",
                "OFL-FrankRuhlLibre.txt": "unity/Assets/UI/Fonts/FrankRuhlLibre/OFL.txt"}


@pytest.fixture
def repo(tmp_path):
    """A minimal repository tree with the files the script reads."""
    root = tmp_path / "repo"
    (root / "unity/ProjectSettings").mkdir(parents=True)
    (root / "unity/ProjectSettings/ProjectVersion.txt").write_text(
        "m_EditorVersion: 6000.3.25f1\nm_EditorVersionWithRevision: 6000.3.25f1 (e1dba0a9aba4)\n")
    (root / "unity/ProjectSettings/ProjectSettings.asset").write_text("PlayerSettings:\n  bundleVersion: 0.1.0\n")
    (root / "sim/Statecraft.Sim").mkdir(parents=True)
    (root / "sim/Statecraft.Sim/SimInfo.cs").write_text('        public const string Version = "0.1.0";\n')
    (root / "sim/Statecraft.Sim.Tests/golden").mkdir(parents=True)
    (root / "sim/Statecraft.Sim.Tests/golden/manifest.txt").write_text(f"# pinned\nhash1950 {H1950}\nhash1952 {H1952}\nsmoke5 {'c' * 64}\n")
    for name in ("LICENSE", "NOTICE", "THIRD-PARTY-NOTICES.md"):
        (root / name).write_text(f"{name} text\n")
    (root / "LICENSES").mkdir()
    for name in LICENCE_TEXTS:
        (root / "LICENSES" / name).write_text(f"{name} text\n")
    for name, src in FONT_SOURCES.items():
        (root / src).parent.mkdir(parents=True, exist_ok=True)
        (root / src).write_text(f"{name}: Copyright line of this font\n")
    return root


@pytest.fixture
def editor(tmp_path):
    """A fake Unity install with every module of the three platforms; tests remove one to make it missing."""
    root = tmp_path / "Editor" / "6000.3.25f1"
    exe = root / "Unity.app/Contents/MacOS/Unity"
    exe.parent.mkdir(parents=True)
    exe.write_text("")
    write_editor_version(exe, "6000.3.25f1 (e1dba0a9aba4)")
    for module in rb.PLATFORMS.values():
        (root / module.module_dir).mkdir(parents=True, exist_ok=True)
    return exe


def write_editor_version(exe, version):
    (exe.parents[1] / "Info.plist").write_bytes(
        b'<?xml version="1.0" encoding="UTF-8"?><plist version="1.0"><dict><key>CFBundleGetInfoString</key>'
        + f"<string>Unity version {version}. (c) 2005-2026 Unity Technologies.</string></dict></plist>".encode())


class Fake:
    """Records every command and plays git, Unity and the player."""

    steam_library = True

    def __init__(self, dirty="", golden_ok=True, build_ok=True, branch="main", tags=("v0.1.0",), worktrees=None,
                 build_raises=None, dirty_after="", on_origin_main=True, fail_status=False,
                 fail_status_after=False, fail_worktree_list=False, sim_ok=True, sim_output="", fail_checkout=False,
                 lipo_code=0, lipo_out="x86_64 arm64\n", photo_manifest=True):
        self.calls: list[list[str]] = []
        self.dirty, self.golden_ok, self.build_ok = dirty, golden_ok, build_ok
        self.branch, self.tags, self.worktrees = branch, tags, worktrees
        self.build_raises, self.dirty_after, self.built = build_raises, dirty_after, False
        self.on_origin_main = on_origin_main
        self.fail_status = fail_status
        self.fail_status_after = fail_status_after
        self.fail_worktree_list = fail_worktree_list
        self.sim_ok, self.sim_output = sim_ok, sim_output
        self.fail_checkout = fail_checkout
        self.lipo_code = lipo_code
        self.lipo_out = lipo_out
        self.photo_manifest = photo_manifest

    def __call__(self, cmd, **kw):
        cmd = [str(c) for c in cmd]
        self.calls.append(cmd)
        out, code = "", 0
        if Path(cmd[-1]).as_posix().endswith("tools/build_sim_for_unity.py"):
            if "stdout" in kw:
                kw["stdout"].write(self.sim_output)
            code = 0 if self.sim_ok else 1
        elif cmd[:2] == ["git", "status"]:
            status_failed = self.fail_status_after if self.built else self.fail_status
            if status_failed:
                code = 1
            else:
                out = self.dirty_after if self.built else self.dirty
        elif cmd[:2] == ["git", "rev-parse"]:
            out = self.branch + "\n" if "--abbrev-ref" in cmd else SHA + "\n"
        elif cmd[:2] == ["git", "merge-base"]:
            code = 0 if self.on_origin_main else 1
        elif cmd[:3] == ["git", "tag", "--points-at"]:
            out = "".join(t + "\n" for t in self.tags)
        elif cmd[:3] == ["git", "worktree", "list"]:
            if self.fail_worktree_list:
                code = 1
            else:
                out = "".join(f"worktree {w}\nHEAD {SHA}\nbranch refs/heads/x\n\n" for w in self.worktrees or [])
        elif cmd[:2] == ["git", "describe"]:
            out = "v0.1.0-5-g1b7a36cb\n"
        elif cmd[:2] == ["git", "check-ignore"]:
            code = 0   # as if git ignored every path: being ignored must not make a folder in a checkout acceptable
        elif cmd[:3] == ["git", "checkout", "--"]:
            code = 1 if self.fail_checkout else 0
        elif "-executeMethod" in cmd:
            self.built = True
            if self.build_raises:
                raise self.build_raises
            target = Path(cmd[cmd.index("-out") + 1])
            log = Path(cmd[cmd.index("-logFile") + 1])
            if target.suffix == ".app":
                (target / "Contents/MacOS").mkdir(parents=True)
                (target / "Contents/Info.plist").write_bytes(
                    b'<?xml version="1.0" encoding="UTF-8"?><plist version="1.0"><dict>'
                    b"<key>CFBundleExecutable</key><string>Statecraft Israel</string></dict></plist>")
                (target / "Contents/MacOS/Statecraft Israel").write_bytes(b"player")
            else:
                target.parent.mkdir(parents=True, exist_ok=True)
                target.write_bytes(b"player " + target.name.encode())
            if self.photo_manifest:
                (target.parent / "event-photos.json").write_text("{}\n")
            if "-steam" in cmd and self.steam_library:   # #2154's Build.cs ships Valve's plugin only in -steam builds
                lib = {".app": "Contents/PlugIns/steam_api.bundle/Contents/MacOS/libsteam_api.dylib",
                       ".exe": "../Statecraft_Data/Plugins/x86_64/steam_api64.dll",
                       ".x86_64": "../Statecraft_Data/Plugins/libsteam_api.so"}[target.suffix]
                path = (target / lib) if target.suffix == ".app" else (target.parent / lib[3:])
                path.parent.mkdir(parents=True, exist_ok=True)
                path.write_bytes(b"valve")
            log.write_text("BUILD Succeeded StandaloneOSX -> x: 1.0 MB on disk, 0 errors, 321s\n")
            if not self.build_ok:   # Unity failed, but a (partial or older) player is still there
                log.write_text("BUILD Failed StandaloneOSX -> x: 0.0 MB on disk, 3 errors, 12s\n")
                code = 1
        elif "-golden" in cmd:
            log = Path(cmd[cmd.index("-logFile") + 1])
            second = f"GOLDEN OK hash1952 {H1952}" if self.golden_ok else f"GOLDEN FAIL hash1952 {'f' * 64} expected {H1952}"
            log.write_text(f"boot\nGOLDEN OK hash1950 {H1950}\n{second}\nGOLDEN OK smoke5 {'c' * 64}\n")
            code = 0 if self.golden_ok else 1
        elif cmd[0] == "ditto":
            # Plays `ditto -c -k --norsrc --keepParent SRC ZIP`: a real zip, SRC's name as the top folder.
            import zipfile
            src, dest = Path(cmd[-2]), Path(cmd[-1])
            with zipfile.ZipFile(dest, "w") as z:
                for p in sorted(src.rglob("*")):
                    if p.is_file():
                        z.write(p, (Path(src.name) / p.relative_to(src)).as_posix())
        elif cmd[0] == "lipo":
            out = self.lipo_out
            code = self.lipo_code
        return rb.Result(code, out)


def run_main(argv, repo, editor, fake, monkeypatch):
    monkeypatch.setattr(rb, "REPO", repo)
    monkeypatch.setattr(rb, "run", fake)
    if fake.worktrees is None:
        fake.worktrees = [repo]
    return rb.main(["--unity", str(editor), *argv])


def unity_calls(fake):
    return [c for c in fake.calls if "-executeMethod" in c]


# --- arguments ---------------------------------------------------------------------------------------------------------

def test_the_platform_flag_selects_one_or_all():
    assert rb.selected(rb.parse(["--platform", "mac"])) == ["mac"]
    assert rb.selected(rb.parse(["--platform", "all"])) == ["mac", "linux", "windows"]
    assert rb.selected(rb.parse([])) == ["mac", "linux", "windows"], "a release builds every platform by default"
    with pytest.raises(SystemExit):
        rb.parse(["--platform", "ios"])


def test_out_dry_run_and_dev_are_flags():
    a = rb.parse(["--out", "/tmp/x", "--dry-run", "--dev"])
    assert a.out == Path("/tmp/x") and a.dry_run and a.dev
    a = rb.parse([])
    assert a.out is None and not a.dry_run and not a.dev


def test_the_default_output_folder_is_outside_the_repository(repo):
    out = rb.default_out(SHA, "0.1.0", "20261014T120000Z")
    assert out == Path.home() / "statecraft-release" / "v0.1.0-1b7a36cb-20261014T120000Z"
    assert not out.is_relative_to(rb.REPO)


def test_an_output_folder_inside_the_repository_is_refused(repo, editor, monkeypatch):
    fake = Fake()
    assert run_main(["--platform", "mac", "--out", str(repo / "builds")], repo, editor, fake, monkeypatch) == 1
    assert unity_calls(fake) == []


@pytest.mark.parametrize("inside", ["unity/Assets/StreamingAssets/content/r", "unity/Library/out", "unity/Builds/r",
                                    ".worktrees/x/out"])
def test_an_ignored_folder_inside_the_repository_is_refused_too(repo, editor, monkeypatch, inside):
    """Ignored is not safe: ContentSync deletes StreamingAssets/content on every build (ContentManifest.cs), which would
    take an earlier platform's player with it (review of #542, item 2 and mutation M4)."""
    fake = Fake()
    assert run_main(["--platform", "all", "--out", str(repo / inside)], repo, editor, fake, monkeypatch) == 1
    assert unity_calls(fake) == []


def test_the_contentsync_wipe_cannot_reach_the_output(repo, editor, monkeypatch):
    """--platform all into StreamingAssets/content: refused before the first build, so no later build can wipe it."""
    fake = Fake()
    out = repo / "unity/Assets/StreamingAssets/content/r"
    assert run_main(["--platform", "all", "--out", str(out)], repo, editor, fake, monkeypatch) == 1
    assert unity_calls(fake) == [] and not out.exists()


def test_a_case_variant_of_the_repository_path_is_refused(repo, editor, monkeypatch):
    """APFS is case-insensitive: /…/REPO/unity/Assets/out is the repository's own unity/Assets."""
    fake = Fake()
    variant = Path(str(repo).upper()) / "unity/Assets/out"
    assert run_main(["--platform", "mac", "--out", str(variant)], repo, editor, fake, monkeypatch) == 1
    assert unity_calls(fake) == []


def test_a_dotdot_or_symlink_into_the_repository_is_refused(repo, editor, monkeypatch, tmp_path):
    link = tmp_path / "link"
    link.symlink_to(repo / "unity")
    for out in (link / "Assets/out", tmp_path / "elsewhere/../repo/unity/x"):
        fake = Fake()
        assert run_main(["--platform", "mac", "--out", str(out)], repo, editor, fake, monkeypatch) == 1, out
        assert unity_calls(fake) == []


def test_every_checkout_of_the_repository_is_refused(repo, editor, monkeypatch, tmp_path):
    """The main checkout and every other worktree, from `git worktree list --porcelain`, not only the running one."""
    main, other = tmp_path / "main-checkout", tmp_path / "wt" / "other"
    for root in (main, other):
        (root / "unity/Assets").mkdir(parents=True)
    for out in (main / "unity/Assets/rel", other / "unity/Assets/rel", main / "x"):
        fake = Fake(worktrees=[main, repo, other])
        assert run_main(["--platform", "mac", "--out", str(out)], repo, editor, fake, monkeypatch) == 1, out
        assert unity_calls(fake) == []


def test_a_failed_worktree_list_is_refused_before_build(repo, editor, monkeypatch, tmp_path, capsys):
    fake = Fake(fail_worktree_list=True)
    assert run_main(["--platform", "mac", "--out", str(tmp_path / "out")],
                    repo, editor, fake, monkeypatch) == 1
    assert unity_calls(fake) == []
    assert "cannot enumerate every checkout" in capsys.readouterr().err


def test_a_non_empty_output_folder_is_refused(repo, editor, monkeypatch, tmp_path):
    out = tmp_path / "out"
    (out / "players/linux").mkdir(parents=True)
    (out / "players/linux/stale.so").write_bytes(b"old")
    fake = Fake()
    assert run_main(["--platform", "linux", "--out", str(out)], repo, editor, fake, monkeypatch) == 1
    assert unity_calls(fake) == []
    empty = tmp_path / "empty"
    empty.mkdir()
    assert run_main(["--platform", "mac", "--out", str(empty)], repo, editor, Fake(), monkeypatch) == 0


# --- a release is main at its tag (spec 6.4 step 1) -------------------------------------------------------------------

def test_a_release_off_main_is_refused(repo, editor, monkeypatch, tmp_path, capsys):
    fake = Fake(branch="tools/feature", tags=("v0.1.0",))
    assert run_main(["--platform", "mac", "--tag", "v0.1.0", "--out", str(tmp_path / "o")], repo, editor, fake, monkeypatch) == 1
    assert unity_calls(fake) == [] and "main" in capsys.readouterr().err


def test_a_release_whose_tag_is_not_at_head_is_refused(repo, editor, monkeypatch, tmp_path, capsys):
    fake = Fake(branch="main", tags=("v0.0.9",))
    assert run_main(["--platform", "mac", "--tag", "v0.1.0", "--out", str(tmp_path / "o")], repo, editor, fake, monkeypatch) == 1
    assert unity_calls(fake) == [] and "v0.1.0" in capsys.readouterr().err
    fake = Fake(branch="main", tags=())
    assert run_main(["--platform", "mac", "--out", str(tmp_path / "p")], repo, editor, fake, monkeypatch) == 1, \
        "the default tag v<bundleVersion> must be at HEAD too"


def test_a_release_on_main_at_its_tag_runs(repo, editor, monkeypatch, tmp_path):
    fake = Fake(branch="main", tags=("v0.1.0-rc.2", "other"))
    out = tmp_path / "o"
    assert run_main(["--platform", "mac", "--tag", "v0.1.0-rc.2", "--out", str(out)], repo, editor, fake, monkeypatch) == 0
    assert (out / "statecraft-israel-v0.1.0-rc.2-macos-universal.zip").exists()


def test_a_dev_build_runs_anywhere_under_a_dev_marked_name(repo, editor, monkeypatch, tmp_path):
    """--dev: any branch, no tag needed, never a release name: v<bundleVersion>-dev.<commit8>, and no -shareable."""
    fake = Fake(branch="tools/feature", tags=())
    out = tmp_path / "o"
    assert run_main(["--platform", "mac", "--dev", "--out", str(out)], repo, editor, fake, monkeypatch) == 0
    assert (out / "statecraft-israel-v0.1.0-dev.1b7a36cb-macos-universal.zip").exists()
    record = json.loads((out / "build-record.json").read_text())
    assert record["tag"] == "v0.1.0-dev.1b7a36cb" and record["release"] is False and record["branch"] == "tools/feature"


def test_a_dev_build_takes_no_release_tag(repo, editor, monkeypatch, tmp_path):
    fake = Fake(branch="main", tags=("v1.0.0",))
    assert run_main(["--platform", "mac", "--dev", "--tag", "v1.0.0", "--out", str(tmp_path / "o")], repo, editor, fake, monkeypatch) == 1
    assert unity_calls(fake) == []


# --- the editor must be the project's version -------------------------------------------------------------------------

def test_an_editor_of_another_version_is_refused(repo, editor, monkeypatch, tmp_path, capsys):
    write_editor_version(editor, "6000.4.1f1 (0123456789ab)")
    for extra in ([], ["--dev"]):
        fake = Fake()
        assert run_main(["--platform", "mac", *extra, "--out", str(tmp_path / f"o{len(extra)}")], repo, editor, fake, monkeypatch) == 1
        assert unity_calls(fake) == []
    err = capsys.readouterr().err
    assert "6000.4.1f1" in err and "6000.3.25f1" in err


def test_the_editor_version_is_read_from_its_info_plist(editor):
    assert rb.editor_version(editor) == ("6000.3.25f1", "e1dba0a9aba4")


# --- the mutants that survived the review ------------------------------------------------------------------------------

def test_the_settings_are_restored_when_the_build_call_raises(repo, editor, monkeypatch, tmp_path):
    """Mutation M2: the try/finally around the build. A Ctrl-C while Unity runs still drops the settings churn."""
    fake = Fake(build_raises=KeyboardInterrupt())
    with pytest.raises(KeyboardInterrupt):
        run_main(["--platform", "mac", "--out", str(tmp_path / "o")], repo, editor, fake, monkeypatch)
    build = next(i for i, c in enumerate(fake.calls) if "-executeMethod" in c)
    assert [c for c in fake.calls[build + 1:] if c[:3] == ["git", "checkout", "--"]]


def test_a_failed_settings_restore_stops_before_goldens_or_records(repo, editor, monkeypatch, tmp_path, capsys):
    fake = Fake(fail_checkout=True)
    out = tmp_path / "o"
    assert run_main(["--platform", "mac", "--out", str(out)], repo, editor, fake, monkeypatch) == 1
    assert [c for c in fake.calls if c[:3] == ["git", "checkout", "--"]]
    assert not [c for c in fake.calls if "-golden" in c]
    assert not (out / "build-record.json").exists()
    assert "restoring Unity settings" in capsys.readouterr().err


def test_a_failed_lipo_with_plausible_output_stops_before_goldens_or_records(repo, editor, monkeypatch, tmp_path, capsys):
    fake = Fake(lipo_code=1)
    out = tmp_path / "o"
    assert run_main(["--platform", "mac", "--out", str(out)], repo, editor, fake, monkeypatch) == 1
    lipo = [c for c in fake.calls if c[0] == "lipo"]
    assert len(lipo) == 1 and lipo[0][1] == "-archs"
    assert not [c for c in fake.calls if "-golden" in c]
    assert not (out / "build-record.json").exists()
    assert "lipo could not inspect" in capsys.readouterr().err


def test_a_silent_lipo_that_exits_zero_stops_before_goldens_or_records(repo, editor, monkeypatch, tmp_path, capsys):
    """#2690 review: an empty architecture list is as unknown as a failed lipo."""
    fake = Fake(lipo_out="")
    out = tmp_path / "o"
    assert run_main(["--platform", "mac", "--out", str(out)], repo, editor, fake, monkeypatch) == 1
    assert [c for c in fake.calls if c[0] == "lipo"]
    assert not [c for c in fake.calls if "-golden" in c]
    assert not (out / "build-record.json").exists()
    assert "lipo could not inspect" in capsys.readouterr().err


def test_a_failed_unity_exit_fails_even_when_a_player_is_left_behind(repo, editor, monkeypatch, tmp_path):
    """Mutation M1: Unity's exit code counts, not only whether the target exists."""
    fake = Fake(build_ok=False)
    out = tmp_path / "o"
    assert run_main(["--platform", "mac", "--out", str(out)], repo, editor, fake, monkeypatch) == 1
    assert (out / "players/mac/Statecraft.app").exists() and not (out / "build-record.json").exists()
    assert not [c for c in fake.calls if "-golden" in c]


def test_an_empty_or_malformed_manifest_never_passes():
    assert rb.golden_status("GOLDEN OK one nope\n", 0, {"one": "nope"}) == "fail"
    assert rb.golden_status("", 0, {}) == "fail"


# --- the record's tree state ------------------------------------------------------------------------------------------

def test_a_dirty_tree_after_the_build_is_named_and_writes_no_record(repo, editor, monkeypatch, tmp_path, capsys):
    dirty = "M unity/Packages/packages-lock.json"
    fake = Fake(dirty_after=f" {dirty}\n")
    out = tmp_path / "o"
    assert run_main(["--platform", "mac", "--out", str(out)], repo, editor, fake, monkeypatch) == 1
    assert not (out / "build-record.json").exists()
    describe = next(i for i, c in enumerate(fake.calls) if c[:2] == ["git", "describe"])
    assert describe < next(i for i, c in enumerate(fake.calls) if "-executeMethod" in c), "describe is taken before"
    err = capsys.readouterr().err
    assert "working tree is dirty after the builds" in err and dirty in err


def test_a_failed_postbuild_git_status_writes_no_clean_record(repo, editor, monkeypatch, tmp_path, capsys):
    out = tmp_path / "out"
    fake = Fake(fail_status_after=True)
    assert run_main(["--platform", "mac", "--out", str(out)], repo, editor, fake, monkeypatch) == 1
    assert len(unity_calls(fake)) == 1
    assert not (out / "build-record.json").exists()
    assert not (out / "build-record.md").exists()
    assert "cannot prove the working tree remained clean" in capsys.readouterr().err


# --- what the script reads ------------------------------------------------------------------------------------------------

def test_unity_version_and_goldens_come_from_the_repository(repo, monkeypatch):
    monkeypatch.setattr(rb, "REPO", repo)
    assert rb.unity_version() == ("6000.3.25f1", "e1dba0a9aba4")
    assert rb.expected_goldens() == {"hash1950": H1950, "hash1952": H1952, "smoke5": "c" * 64}
    assert rb.versions() == {"bundle_version": "0.1.0", "sim_version": "0.1.0"}


# --- refusals ---------------------------------------------------------------------------------------------------------

def test_a_dirty_tree_is_refused_before_any_build(repo, editor, monkeypatch, capsys, tmp_path):
    fake = Fake(dirty=" M unity/ProjectSettings/ProjectSettings.asset\n")
    assert run_main(["--platform", "mac", "--out", str(tmp_path / "out")], repo, editor, fake, monkeypatch) == 1
    assert unity_calls(fake) == []
    err = capsys.readouterr().err
    assert "REFUSED" in err and "ProjectSettings.asset" in err
    assert not (tmp_path / "out").exists()


def test_a_failed_initial_git_status_is_refused_before_build(repo, editor, monkeypatch, tmp_path, capsys):
    fake = Fake(fail_status=True)
    assert run_main(["--platform", "mac", "--out", str(tmp_path / "out")],
                    repo, editor, fake, monkeypatch) == 1
    assert unity_calls(fake) == []
    assert "cannot prove the working tree is clean" in capsys.readouterr().err


class GitWontStart(Fake):
    def __call__(self, cmd, **kw):
        if [str(c) for c in cmd][:2] == ["git", "status"]:
            raise FileNotFoundError(2, "No such file or directory", "git")
        return super().__call__(cmd, **kw)


def test_a_git_that_cannot_start_is_refused_with_a_message(repo, editor, monkeypatch, tmp_path, capsys):
    fake = GitWontStart()
    assert run_main(["--platform", "mac", "--out", str(tmp_path / "out")],
                    repo, editor, fake, monkeypatch) == 1
    assert unity_calls(fake) == []
    assert "cannot prove the working tree is clean: `git status --porcelain --untracked-files=normal` could not start" \
        in capsys.readouterr().err


def test_untracked_files_count_as_dirty(repo, editor, monkeypatch, tmp_path):
    fake = Fake(dirty="?? notes.txt\n")
    assert run_main(["--platform", "mac", "--out", str(tmp_path / "out")], repo, editor, fake, monkeypatch) == 1
    status = next(c for c in fake.calls if c[:2] == ["git", "status"])
    assert "--porcelain" in status and "--untracked-files=normal" in status


def test_missing_modules_fail_clearly_and_name_what_to_install(repo, editor, monkeypatch, capsys, tmp_path):
    root = editor.parents[3]
    for name in ("linux", "windows"):
        (root / rb.PLATFORMS[name].module_dir).rmdir()
    fake = Fake()
    assert run_main(["--platform", "all", "--out", str(tmp_path / "out")], repo, editor, fake, monkeypatch) == 2
    assert unity_calls(fake) == [], "no platform is built when any selected module is missing"
    err = capsys.readouterr().err
    assert "linux-mono" in err and "windows-mono" in err and "mac-il2cpp" not in err
    assert "Unity Hub" in err and "install-modules" in err and "-m linux-mono -m windows-mono" in err


def test_only_the_selected_platforms_modules_are_required(repo, editor, monkeypatch, tmp_path):
    root = editor.parents[3]
    (root / rb.PLATFORMS["windows"].module_dir).rmdir()
    fake = Fake()
    assert run_main(["--platform", "mac", "--out", str(tmp_path / "out")], repo, editor, fake, monkeypatch) == 0


def test_a_missing_editor_fails_clearly(repo, tmp_path, monkeypatch, capsys):
    monkeypatch.setattr(rb, "REPO", repo)
    monkeypatch.setattr(rb, "run", Fake())
    assert rb.main(["--unity", str(tmp_path / "nope/Unity"), "--platform", "mac"]) == 2
    assert "6000.3.25f1" in capsys.readouterr().err


# --- the lock and the commands ----------------------------------------------------------------------------------------------

def test_every_unity_build_runs_under_the_shared_lock_and_waits_for_unity(repo, editor, monkeypatch, tmp_path):
    fake = Fake()
    assert run_main(["--platform", "all", "--out", str(tmp_path / "out")], repo, editor, fake, monkeypatch) == 0
    builds = unity_calls(fake)
    assert len(builds) == 3
    for c in builds:
        lock = c.index("--")
        head = c[:lock]
        # The command holds a native path (backslashes on Windows); compare it in POSIX form.
        assert head[:2] == ["uv", "run"] and Path(head[2]).as_posix().endswith("tools/machine_lock.py")
        assert "--shared" in head and "--wait-unity" in head
        assert c[lock + 1] == str(editor), "Unity is the locked command itself"
        assert "-nographics" not in c, "never -nographics for the editor"
        for flag in ("-batchmode", "-quit", "-CI", "-GITHUB_ACTIONS", "-TF_BUILD"):
            assert flag in c
    methods = [c[c.index("-executeMethod") + 1] for c in builds]
    assert methods == ["Statecraft.Game.EditorTools.Build.Mac", "Statecraft.Game.EditorTools.Build.LinuxMono",
                       "Statecraft.Game.EditorTools.Build.WindowsMono"]
    assert [c[c.index("-buildTarget") + 1] for c in builds] == ["OSXUniversal", "Linux64", "Win64"]


def test_the_lock_never_times_out_on_its_own_hour(repo, editor, monkeypatch, tmp_path):
    fake = Fake()
    run_main(["--platform", "mac", "--out", str(tmp_path / "out")], repo, editor, fake, monkeypatch)
    for c in unity_calls(fake):
        assert int(c[c.index("--timeout") + 1]) >= 7 * 24 * 3600, "a long wait is reported, never cancelled"


def test_release_builds_are_shareable_and_dev_builds_are_not(repo, editor, monkeypatch, tmp_path):
    fake = Fake()
    run_main(["--platform", "mac", "--out", str(tmp_path / "a")], repo, editor, fake, monkeypatch)
    assert "-shareable" in unity_calls(fake)[0]
    fake = Fake()
    run_main(["--platform", "mac", "--dev", "--out", str(tmp_path / "b")], repo, editor, fake, monkeypatch)
    assert "-shareable" not in unity_calls(fake)[0]
    assert json.loads((tmp_path / "b/build-record.json").read_text())["shareable"] is False


def test_settings_churn_is_dropped_after_each_build(repo, editor, monkeypatch, tmp_path):
    fake = Fake()
    run_main(["--platform", "all", "--out", str(tmp_path / "out")], repo, editor, fake, monkeypatch)
    checkouts = [i for i, c in enumerate(fake.calls) if c[:3] == ["git", "checkout", "--"]]
    builds = [i for i, c in enumerate(fake.calls) if "-executeMethod" in c]
    assert len(checkouts) == 3 and all(b < k for b, k in zip(builds, checkouts))
    assert fake.calls[checkouts[0]][3:] == ["unity/ProjectSettings", "unity/Assets/Settings"]


def test_the_sim_dlls_are_built_before_unity(repo, editor, monkeypatch, tmp_path):
    fake = Fake()
    run_main(["--platform", "mac", "--out", str(tmp_path / "out")], repo, editor, fake, monkeypatch)
    sim = next(i for i, c in enumerate(fake.calls) if Path(c[-1]).as_posix().endswith("tools/build_sim_for_unity.py"))
    assert sim < next(i for i, c in enumerate(fake.calls) if "-executeMethod" in c)


def test_a_failed_sim_build_has_a_named_log(repo, editor, monkeypatch, tmp_path, capsys):
    fake = Fake(sim_ok=False, sim_output="sim compiler failure\n")
    out = tmp_path / "out"
    assert run_main(["--platform", "mac", "--out", str(out)], repo, editor, fake, monkeypatch) == 1
    sim_log = out / "logs/build-sim.log"
    assert sim_log.read_text() == "sim compiler failure\n"
    assert str(sim_log) in capsys.readouterr().err
    assert unity_calls(fake) == []


def test_no_push_upload_commit_or_network(repo, editor, monkeypatch, tmp_path):
    fake = Fake()
    run_main(["--platform", "all", "--out", str(tmp_path / "out")], repo, editor, fake, monkeypatch)
    for c in fake.calls:
        if c[0] == "git":
            assert c[1] in {"status", "rev-parse", "describe", "checkout", "tag", "worktree", "merge-base"}, c
        assert c[0] not in {"gh", "curl", "wget", "scp", "rsync", "steamcmd", "xcrun"}, c
        assert c[:2] not in (["git", "fetch"], ["git", "pull"], ["git", "ls-remote"]), "no network, not even a fetch"
        assert "push" not in c and "commit" not in c
        if c[:2] == ["git", "tag"]:
            assert c[2] == "--points-at", "tags are only read"
        if c[:2] == ["git", "worktree"]:
            assert c[2] == "list", "worktrees are only read"
    source = Path(rb.__file__).read_text()
    for module in ("urllib", "http.client", "socket", "requests", "ftplib", "smtplib"):
        assert f"import {module}" not in source and f"from {module}" not in source


def test_a_dry_run_builds_nothing_and_writes_nothing(repo, editor, monkeypatch, tmp_path, capsys):
    fake = Fake()
    assert run_main(["--platform", "all", "--dry-run", "--out", str(tmp_path / "out")], repo, editor, fake, monkeypatch) == 0
    assert unity_calls(fake) == [] and not [c for c in fake.calls if "-golden" in c or c[:2] == ["git", "checkout"]]
    assert not (tmp_path / "out").exists()
    out = capsys.readouterr().out
    assert "Build.Mac" in out and "Build.WindowsMono" in out and "machine_lock.py" in out


def test_a_failed_build_stops_with_no_record(repo, editor, monkeypatch, tmp_path):
    fake = Fake(build_ok=False)
    assert run_main(["--platform", "mac", "--out", str(tmp_path / "out")], repo, editor, fake, monkeypatch) == 1
    assert not (tmp_path / "out/build-record.json").exists()
    assert [c for c in fake.calls if c[:2] == ["git", "checkout"]], "the settings are restored even after a failure"


def test_a_build_without_its_photo_manifest_stops_before_archive_and_record(repo, editor, monkeypatch, tmp_path, capsys):
    fake = Fake(photo_manifest=False)
    out = tmp_path / "out"
    assert run_main(["--platform", "mac", "--out", str(out)], repo, editor, fake, monkeypatch) == 1
    assert not (out / "players/mac/LICENSE").exists()
    assert not (out / "event-photos-mac.json").exists()
    assert not (out / "files-mac.sha256").exists()
    assert not (out / "statecraft-israel-v0.1.0-macos-universal.zip").exists()
    assert not (out / "build-record.json").exists()
    err = capsys.readouterr().err
    assert "FAILED" in err and "mac" in err and "event-photos.json" in err


# --- the record ----------------------------------------------------------------------------------------------------------

def test_the_record_holds_commit_unity_date_hashes_sizes_and_goldens(repo, editor, monkeypatch, tmp_path, capsys):
    monkeypatch.setattr(rb, "utc_now", lambda: rb.datetime(2026, 10, 14, 12, 0, 0, tzinfo=rb.timezone.utc))
    fake = Fake()
    out = tmp_path / "out"
    assert run_main(["--platform", "all", "--out", str(out)], repo, editor, fake, monkeypatch) == 0
    record = json.loads((out / "build-record.json").read_text())
    assert record["commit"] == SHA and record["tree_clean"] is True
    assert record["unity"] == {"version": "6000.3.25f1", "revision": "e1dba0a9aba4"}
    assert record["date_utc"] == "2026-10-14T12:00:00Z"
    assert record["version"] == {"bundle_version": "0.1.0", "sim_version": "0.1.0"}
    assert record["shareable"] is True and record["signing"] == "none" and record["tag"] == "v0.1.0"
    arts = {a["platform"]: a for a in record["artifacts"]}
    assert set(arts) == {"mac", "linux", "windows"}
    assert arts["mac"]["backend"] == "IL2CPP" and arts["windows"]["backend"] == "Mono" and arts["linux"]["backend"] == "Mono"
    assert arts["mac"]["architecture"] == "x86_64 arm64"
    assert arts["mac"]["unity_build_seconds"] == 321

    mac = arts["mac"]
    manifest = out / mac["file_list"]
    lines = manifest.read_text().splitlines()
    exe = out / "players/mac/Statecraft.app/Contents/MacOS/Statecraft Israel"
    assert f"{hashlib.sha256(b'player').hexdigest()}  Statecraft.app/Contents/MacOS/Statecraft Israel" in lines
    assert not any(line.endswith("event-photos.json") for line in lines), "the list is exactly what the zip holds"
    assert all(line.split("  ", 1)[1].startswith("Statecraft.app/") or line.split("  ", 1)[1] in NOTICES for line in lines)
    assert [l.split("  ", 1)[1] for l in lines if not l.split("  ", 1)[1].startswith("Statecraft.app/")] == NOTICES
    photos = out / mac["photo_manifest"]["file"]
    assert photos.name == "event-photos-mac.json" and photos.read_text() == "{}\n"
    assert mac["photo_manifest"]["sha256"] == hashlib.sha256(photos.read_bytes()).hexdigest()
    assert lines == sorted(lines, key=lambda l: l.split("  ", 1)[1])
    assert mac["file_list_sha256"] == hashlib.sha256(manifest.read_bytes()).hexdigest()
    for name, asset in (("mac", "statecraft-israel-v0.1.0-macos-universal.zip"),
                        ("linux", "statecraft-israel-v0.1.0-linux-x64.tar.gz"),
                        ("windows", "statecraft-israel-v0.1.0-windows-x64.zip")):
        archive = out / asset
        assert arts[name]["archive"] == {"name": asset, "bytes": archive.stat().st_size,
                                         "sha256": hashlib.sha256(archive.read_bytes()).hexdigest()}
    assert mac["files"] == len(lines)
    assert mac["bytes"] == sum(p.stat().st_size for p in (out / "players/mac/Statecraft.app").rglob("*") if p.is_file()) \
        + sum((out / "players/mac" / n).stat().st_size for n in NOTICES)
    assert exe.is_file()

    assert mac["golden"]["status"] == "pass"
    assert mac["golden"]["lines"] == [f"GOLDEN OK hash1950 {H1950}", f"GOLDEN OK hash1952 {H1952}", f"GOLDEN OK smoke5 {'c' * 64}"]
    assert mac["golden"]["exit_code"] == 0
    for name in ("linux", "windows"):
        assert arts[name]["golden"]["status"] == "unchecked"
        assert "CI" in arts[name]["golden"]["note"]

    md = (out / "build-record.md").read_text()
    assert SHA in md and "6000.3.25f1" in md and mac["archive"]["sha256"] in md and mac["archive"]["name"] in md and "unchecked" in md and "pass" in md
    printed = capsys.readouterr().out
    assert str(out / "build-record.json") in printed


def test_the_golden_runs_on_the_mac_player_headless_under_the_lock(repo, editor, monkeypatch, tmp_path):
    fake = Fake()
    run_main(["--platform", "all", "--out", str(tmp_path / "out")], repo, editor, fake, monkeypatch)
    goldens = [c for c in fake.calls if "-golden" in c]
    assert len(goldens) == 1, "only the macOS player can run on the Mac"
    c = goldens[0]
    head = c[:c.index("--")]
    assert Path(head[2]).as_posix().endswith("tools/machine_lock.py") and "--shared" in head
    player = c[c.index("--") + 1]
    assert Path(player).as_posix().endswith("Statecraft.app/Contents/MacOS/Statecraft Israel")
    assert {"-batchmode", "-nographics", "-golden"} <= set(c), "a player may run -nographics (docs/determinism.md)"
    assert Path(c[c.index("-logFile") + 1]).is_absolute()


def test_a_failed_golden_is_recorded_and_fails_the_run(repo, editor, monkeypatch, tmp_path):
    fake = Fake(golden_ok=False)
    out = tmp_path / "out"
    assert run_main(["--platform", "mac", "--out", str(out)], repo, editor, fake, monkeypatch) == 1
    mac = json.loads((out / "build-record.json").read_text())["artifacts"][0]
    assert mac["golden"]["status"] == "fail" and mac["golden"]["exit_code"] == 1
    assert any(line.startswith("GOLDEN FAIL hash1952") for line in mac["golden"]["lines"])


def test_golden_lines_must_match_the_pinned_hashes_not_just_say_ok():
    expected = {"1950": H1950, "1952": H1952}
    assert rb.golden_status(f"GOLDEN OK 1950 {H1950}\nGOLDEN OK 1952 {H1952}\n", 0, expected) == "pass"
    assert rb.golden_status(f"GOLDEN OK 1950 {H1950}\nGOLDEN OK 1952 {'0' * 64}\n", 0, expected) == "fail"
    assert rb.golden_status(f"GOLDEN OK 1950 {H1950}\n", 0, expected) == "fail", "both years are required"
    assert rb.golden_status(f"GOLDEN OK 1950 {H1950}\nGOLDEN OK 1952 {H1952}\n", 1, expected) == "fail"


def test_symlinks_are_listed_by_target_not_followed(tmp_path):
    folder = tmp_path / "p"
    (folder / "A.app/Contents").mkdir(parents=True)
    (folder / "A.app/Contents/real").write_bytes(b"x")
    (folder / "A.app/Contents/link").symlink_to("real")
    text, files, size = rb.tree_manifest(folder)
    assert f"{hashlib.sha256(b'x').hexdigest()}  A.app/Contents/real" in text.splitlines()
    assert any(line.endswith("A.app/Contents/link -> real") for line in text.splitlines())
    assert files == 2 and size == 1


# --- the archives (the asset-name contract with the CI player check, PR #533) ----------------------------------------------

def test_archive_names_follow_the_ci_contract():
    assert rb.archive_name("v0.2.0", "windows") == "statecraft-israel-v0.2.0-windows-x64.zip"
    assert rb.archive_name("v0.2.0", "linux") == "statecraft-israel-v0.2.0-linux-x64.tar.gz"
    assert rb.archive_name("v0.2.0-rc.1", "mac") == "statecraft-israel-v0.2.0-rc.1-macos-universal.zip"


def test_the_mac_archive_is_made_with_ditto_keeping_the_bundle(repo, editor, monkeypatch, tmp_path):
    fake = Fake(tags=("v0.1.0",))
    out = tmp_path / "out"
    run_main(["--platform", "mac", "--tag", "v0.1.0", "--out", str(out)], repo, editor, fake, monkeypatch)
    ditto = next(c for c in fake.calls if c[0] == "ditto")
    assert ditto[1:5] == ["-c", "-k", "--norsrc", "--keepParent"], "no AppleDouble ._* entries the file list lacks"
    assert ditto[-2] == str(out / "players/mac/Statecraft.app")
    assert ditto[-1] == str(out / "statecraft-israel-v0.1.0-macos-universal.zip")


def test_the_linux_and_windows_archives_hold_the_player_folder(repo, editor, monkeypatch, tmp_path):
    import tarfile
    import zipfile
    out = tmp_path / "out"
    assert run_main(["--platform", "all", "--out", str(out)], repo, editor, Fake(), monkeypatch) == 0
    with tarfile.open(out / "statecraft-israel-v0.1.0-linux-x64.tar.gz") as t:
        assert [m.name for m in t.getmembers() if m.isfile()] == sorted(NOTICES + ["Statecraft.x86_64"])
    with zipfile.ZipFile(out / "statecraft-israel-v0.1.0-windows-x64.zip") as z:
        assert [n for n in z.namelist() if not n.endswith("/")] == sorted(NOTICES + ["Statecraft.exe"])


def test_the_linux_archive_keeps_the_executable_bit(tmp_path):
    import tarfile
    folder = tmp_path / "linux"
    folder.mkdir()
    exe = folder / "Statecraft.x86_64"
    exe.write_bytes(b"elf")
    exe.chmod(0o755)
    rb.make_archive(rb.PLATFORMS["linux"], folder, exe, tmp_path / "a.tar.gz")
    with tarfile.open(tmp_path / "a.tar.gz") as t:
        assert t.getmember("Statecraft.x86_64").mode & 0o111


def test_a_bad_tag_is_refused(repo, editor, monkeypatch, tmp_path):
    fake = Fake()
    assert run_main(["--platform", "mac", "--tag", "0.1.0", "--out", str(tmp_path / "o")], repo, editor, fake, monkeypatch) == 1
    assert unity_calls(fake) == []


def test_a_folder_with_two_players_breaks_the_contract(tmp_path):
    folder = tmp_path / "w"
    folder.mkdir()
    for name in ("Statecraft.exe", "Other.exe", "UnityCrashHandler64.exe"):
        (folder / name).write_bytes(b"x")
    assert "found 2" in rb.player_problem(rb.PLATFORMS["windows"], folder)
    (folder / "Other.exe").unlink()
    assert rb.player_problem(rb.PLATFORMS["windows"], folder) is None, "the crash handler does not count"


def test_a_boot_fail_line_fails_the_golden_even_with_both_ok_lines():
    expected = {"1950": H1950, "1952": H1952}
    log = f"GOLDEN OK 1950 {H1950}\nGOLDEN OK 1952 {H1952}\nBOOT FAIL could not read content\n"
    assert rb.golden_status(log, 0, expected) == "fail"


def test_unitys_do_not_ship_folders_are_left_out_of_the_list_and_the_archives(tmp_path):
    """IL2CPP and Burst leave Statecraft_BackUpThisFolder_ButDontShipItWithYourGame (2 GB in the first real run) and
    <product>_BurstDebugInformation_DoNotShip beside the player; neither is part of the release."""
    import zipfile
    folder = tmp_path / "w"
    (folder / "Statecraft_BackUpThisFolder_ButDontShipItWithYourGame").mkdir(parents=True)
    (folder / "Statecraft_BackUpThisFolder_ButDontShipItWithYourGame/big.cpp").write_bytes(b"x" * 10)
    (folder / "Statecraft Israel_BurstDebugInformation_DoNotShip").mkdir()
    (folder / "Statecraft Israel_BurstDebugInformation_DoNotShip/d.pdb").write_bytes(b"y")
    (folder / "Statecraft.exe").write_bytes(b"exe")
    text, files, size = rb.tree_manifest(folder)
    assert text.splitlines() == [f"{hashlib.sha256(b'exe').hexdigest()}  Statecraft.exe"] and (files, size) == (1, 3)
    rb.make_archive(rb.PLATFORMS["windows"], folder, folder / "Statecraft.exe", tmp_path / "w.zip")
    assert zipfile.ZipFile(tmp_path / "w.zip").namelist() == ["Statecraft.exe"]


# --- review of #542, round 3 ----------------------------------------------------------------------------------------------

def test_a_release_tag_must_be_the_bundle_version(repo, editor, monkeypatch, tmp_path, capsys):
    """A v2.0.0 tag at HEAD with bundleVersion 0.1.0 would name archives after a version the player does not have."""
    fake = Fake(tags=("v2.0.0",))
    assert run_main(["--platform", "mac", "--tag", "v2.0.0", "--out", str(tmp_path / "o")], repo, editor, fake, monkeypatch) == 1
    assert unity_calls(fake) == [] and "0.1.0" in capsys.readouterr().err
    fake = Fake(tags=("v0.1.0-rc.1",))
    assert run_main(["--platform", "mac", "--tag", "v0.1.0-rc.1", "--out", str(tmp_path / "p")], repo, editor, fake, monkeypatch) == 0, \
        "a pre-release suffix on the bundle version is a release of that version"


def test_a_malformed_tag_at_head_is_refused(repo, editor, monkeypatch, tmp_path, capsys):
    """Pins the tag-format check itself: this tag is at HEAD and starts with v<bundleVersion>-."""
    fake = Fake(tags=("v0.1.0-rc_1",))
    assert run_main(["--platform", "mac", "--tag", "v0.1.0-rc_1", "--out", str(tmp_path / "o")], repo, editor, fake, monkeypatch) == 1
    assert unity_calls(fake) == [] and "not a release tag" in capsys.readouterr().err


def test_a_release_must_be_on_origin_main(repo, editor, monkeypatch, tmp_path, capsys):
    """A local main with commits GitHub's main lacks is not a release; the script reads origin/main and never fetches."""
    fake = Fake(on_origin_main=False)
    assert run_main(["--platform", "mac", "--out", str(tmp_path / "o")], repo, editor, fake, monkeypatch) == 1
    err = capsys.readouterr().err
    assert unity_calls(fake) == [] and "origin/main" in err and "git fetch" in err
    check = next(c for c in fake.calls if c[:2] == ["git", "merge-base"])
    assert check == ["git", "merge-base", "--is-ancestor", "HEAD", "refs/remotes/origin/main"]
    fake = Fake(on_origin_main=False, branch="tools/x", tags=())
    assert run_main(["--platform", "mac", "--dev", "--out", str(tmp_path / "d")], repo, editor, fake, monkeypatch) == 0, \
        "a dev build may be anywhere"


def test_a_detached_head_says_so(repo, editor, monkeypatch, tmp_path, capsys):
    fake = Fake(branch="HEAD")
    assert run_main(["--platform", "mac", "--out", str(tmp_path / "o")], repo, editor, fake, monkeypatch) == 1
    assert "HEAD is detached" in capsys.readouterr().err


def test_a_dangling_symlink_into_the_repository_is_refused(repo, editor, monkeypatch, tmp_path):
    link = tmp_path / "dangling"
    link.symlink_to(repo / "unity/Assets/NotYetThere")
    for extra in ([], ["--dry-run"]):
        fake = Fake()
        assert run_main(["--platform", "mac", "--dev", *extra, "--out", str(link / "x")], repo, editor, fake, monkeypatch) == 1
        assert unity_calls(fake) == []


@pytest.mark.parametrize("tag", ["v٠.١.٠", "v0.1.0", "v0.1.0-rc.١"])
def test_a_tag_with_non_ascii_digits_is_not_a_release_tag(tag):
    """\\d without re.ASCII matches Arabic-Indic and full-width digits; a tag reaches file names and the CI check."""
    assert rb.TAG.fullmatch(tag) is None


# --- the notices in every desktop archive (RELEASING.md step 7; P47) -------------------------------------------------------

def archive_files(path):
    import tarfile
    import zipfile
    if path.name.endswith(".tar.gz"):
        with tarfile.open(path) as t:
            return sorted(m.name for m in t.getmembers() if m.isfile() or m.issym())
    with zipfile.ZipFile(path) as z:
        return sorted(n for n in z.namelist() if not n.endswith("/"))


def test_every_archive_carries_the_notices_and_matches_its_file_list(repo, editor, monkeypatch, tmp_path):
    out = tmp_path / "out"
    assert run_main(["--platform", "all", "--out", str(out)], repo, editor, Fake(), monkeypatch) == 0
    record = json.loads((out / "build-record.json").read_text())
    for a in record["artifacts"]:
        names = archive_files(out / a["archive"]["name"])
        for notice in NOTICES:
            assert notice in names, (a["platform"], notice)
            # The file's exact bytes: read_text() would turn the \r\n that a Windows write gives into \n.
            source = repo / FONT_SOURCES.get(notice, notice)
            assert zip_bytes(out / a["archive"]["name"], notice) == source.read_bytes()
        listed = sorted(l.split("  ", 1)[1].split(" -> ")[0] for l in (out / a["file_list"]).read_text().splitlines())
        assert listed == names, f"{a['platform']}: the file list is exactly the archive"


def zip_bytes(path, name):
    import tarfile
    import zipfile
    if path.name.endswith(".tar.gz"):
        with tarfile.open(path) as t:
            return t.extractfile(name).read()
    with zipfile.ZipFile(path) as z:
        return z.read(name)


def test_the_mac_zip_keeps_the_app_whole_with_the_notices_beside_it_and_no_appledouble(repo, editor, monkeypatch, tmp_path):
    out = tmp_path / "out"
    fake = Fake()
    assert run_main(["--platform", "mac", "--out", str(out)], repo, editor, fake, monkeypatch) == 0
    names = archive_files(out / "statecraft-israel-v0.1.0-macos-universal.zip")
    assert not [n for n in names if Path(n).name.startswith("._")]
    tops = sorted({n.split("/")[0] for n in names})
    assert tops == sorted({"Statecraft.app", "LICENSE", "LICENSES", "NOTICE", "OFL-EBGaramond.txt", "OFL-FrankRuhlLibre.txt",
                           "THIRD-PARTY-NOTICES.md"})
    ditto = next(c for c in fake.calls if c[0] == "ditto")
    assert "--norsrc" in ditto and ditto[-2].endswith("Statecraft.app"), "the bundle is still zipped by ditto"


def test_the_one_player_rule_still_holds_with_the_notices(repo, editor, monkeypatch, tmp_path):
    out = tmp_path / "out"
    assert run_main(["--platform", "all", "--out", str(out)], repo, editor, Fake(), monkeypatch) == 0
    for name in ("mac", "linux", "windows"):
        assert rb.player_problem(rb.PLATFORMS[name], out / "players" / name) is None


@pytest.mark.parametrize("missing", ["NOTICE", "LICENSES/CC-BY-4.0.txt", "unity/Assets/UI/Fonts/OFL.txt",
                                     "unity/Assets/UI/Fonts/FrankRuhlLibre/OFL.txt"])
def test_a_missing_notice_file_is_refused_before_any_build(repo, editor, monkeypatch, tmp_path, capsys, missing):
    (repo / missing).unlink()
    fake = Fake()
    assert run_main(["--platform", "linux", "--out", str(tmp_path / "o")], repo, editor, fake, monkeypatch) == 1
    assert unity_calls(fake) == []
    assert missing in capsys.readouterr().err


def test_an_archive_without_its_notices_is_caught(tmp_path):
    import zipfile
    z = tmp_path / "a.zip"
    with zipfile.ZipFile(z, "w") as f:
        f.writestr("Statecraft.exe", "x")
        f.writestr("LICENSE", "x")
    assert "NOTICE" in rb.missing_notices(z)
    assert rb.missing_notices(z) == sorted(set(NOTICES) - {"LICENSE"})


def test_the_notice_list_is_the_web_packagers():
    """One list: the files scripts/oss/package_web.sh copies and checks in the web zip (index.html aside)."""
    import re
    script = (Path(rb.__file__).resolve().parents[1] / "scripts/oss/package_web.sh").read_text()
    loop = re.search(r"for f in (.*?); do", script, re.S).group(1).replace("\\\n", " ")
    web = sorted(set(loop.split()) - {"index.html"})
    assert sorted(rb.REQUIRED_NOTICES) == web == NOTICES


def test_every_shipped_fonts_licence_is_a_required_notice():
    """The OFL asks that each font's copyright notice and licence travel with it: every OFL.txt under
    unity/Assets/UI/Fonts (one per font folder) is shipped under its own name, so the next font cannot be missed."""
    root = Path(rb.__file__).resolve().parents[1]
    found = sorted(p.relative_to(root).as_posix() for p in (root / "unity/Assets/UI/Fonts").rglob("OFL.txt"))
    assert found == sorted(FONT_SOURCES.values())
    assert sorted(rb.FONT_LICENCES.values()) == found
    for name in rb.FONT_LICENCES:
        assert name in rb.REQUIRED_NOTICES
    assert rb.FONT_LICENCES == FONT_SOURCES, "the font licence names the archives carry"


def test_the_golden_rule_wants_the_exact_bare_lines_the_player_prints():
    """Review T53 S-B: the real Mac player log (the #542 run) has the GOLDEN lines bare, one per line. A prefixed line is
    not that format, and a hash with one more hex character is a different hash; both fail, as in CI (#533)."""
    expected = {"1950": H1950, "1952": H1952}
    assert rb.golden_status(f"Initialize engine\nGOLDEN OK 1950 {H1950}\r\nGOLDEN OK 1952 {H1952}\n", 0, expected) == "pass"
    assert rb.golden_status(f"12:00:01 GOLDEN OK 1950 {H1950}\n12:00:02 GOLDEN OK 1952 {H1952}\n", 0, expected) == "fail"
    assert rb.golden_status(f"GOLDEN OK 1950 {H1950}a\nGOLDEN OK 1952 {H1952}\n", 0, expected) == "fail"
    assert rb.golden_lines(f"GOLDEN OK 1950 {H1950}a\n") == [f"GOLDEN OK 1950 {H1950}a"], "the record shows what was printed"


# --- Steam (plan 2026-11-01-steamworks-integration Task 7, #2155) -------------------------------------------------------

VALVE_NAMES = ["Statecraft_Data/Plugins/x86_64/steam_api64.dll", "Statecraft_Data/Plugins/steam_api.dll",
               "Statecraft.app/Contents/PlugIns/steam_api.bundle/",
               "Statecraft.app/Contents/PlugIns/steam_api.bundle/Contents/MacOS/libsteam_api.dylib",
               "Statecraft_Data/Plugins/libsteam_api.so", "steam_appid.txt"]


@pytest.mark.parametrize("name", VALVE_NAMES)
def test_a_non_steam_archive_never_carries_valve_files(name):
    clean = ["Statecraft.exe", "Statecraft_Data/Plugins/x86_64/lib_burst_generated.dll",
             "Statecraft_Data/StreamingAssets/vehicle-steam-locomotive-1948-v2.bundle", "LICENSE"]
    assert rb.refuse_valve_files(clean) is None
    problem = rb.refuse_valve_files([*clean, name])
    assert problem is not None and name in problem


class ValveFake(Fake):
    """A player build that leaves `extra` (a path relative to the player's folder) beside the player."""

    def __init__(self, extra, **kw):
        super().__init__(**kw)
        self.extra = extra

    def __call__(self, cmd, **kw):
        result = super().__call__(cmd, **kw)
        cmd = [str(c) for c in cmd]
        if "-executeMethod" in cmd:
            folder = Path(cmd[cmd.index("-out") + 1]).parent
            (folder / self.extra).parent.mkdir(parents=True, exist_ok=True)
            (folder / self.extra).write_text("valve")
        return result


def test_a_valve_file_in_a_github_archive_fails_the_build(repo, editor, monkeypatch, tmp_path, capsys):
    out = tmp_path / "out"
    fake = ValveFake("Statecraft_Data/Plugins/x86_64/steam_api64.dll")
    assert run_main(["--platform", "windows", "--out", str(out)], repo, editor, fake, monkeypatch) == 1
    assert "steam_api64.dll" in capsys.readouterr().err
    assert not (out / "build-record.json").exists()


def test_steam_folders_are_written_not_archived(repo, editor, monkeypatch, tmp_path, capsys):
    out = tmp_path / "out"
    assert run_main(["--platform", "all", "--steam", "--dry-run", "--out", str(out)], repo, editor, Fake(),
                    monkeypatch) == 0
    text = capsys.readouterr().out
    builds = [line for line in text.splitlines() if "-executeMethod" in line]
    assert len(builds) == 3 and all("-steam" in line.split() for line in builds)
    for platform in ("windows", "mac", "linux"):
        assert f"steam folder: {out / 'steam' / platform}" in text
    assert "archive:" not in text
    # A GitHub build never asks for the Steam define.
    assert run_main(["--platform", "all", "--dry-run", "--out", str(out)], repo, editor, Fake(), monkeypatch) == 0
    assert "-steam" not in capsys.readouterr().out
    # The real run: one folder per platform with the player and the notices, and no archive anywhere.
    assert run_main(["--platform", "all", "--steam", "--out", str(out)], repo, editor, Fake(), monkeypatch) == 0
    for platform, player in (("windows", "Statecraft.exe"), ("linux", "Statecraft.x86_64"), ("mac", "Statecraft.app")):
        folder = out / "steam" / platform
        assert (folder / player).exists() and all((folder / n).is_file() for n in NOTICES)
    assert not list(out.glob("*.zip")) and not list(out.glob("*.tar.gz"))
    record = json.loads((out / "build-record.json").read_text())
    assert record["steam"] is True and record["channel"] == "beta" and record["dev"] is False
    for a in record["artifacts"]:
        assert a["archive"] is None and a["steam_folder"] == f"steam/{a['platform']}"


def test_every_steam_build_passes_the_steam_flag(repo, editor, monkeypatch, tmp_path):
    fake = Fake()
    assert run_main(["--platform", "all", "--steam", "--out", str(tmp_path / "out")], repo, editor, fake,
                    monkeypatch) == 0
    assert len(unity_calls(fake)) == 3 and all("-steam" in c for c in unity_calls(fake))


def test_a_steam_folder_with_steam_appid_txt_is_refused(repo, editor, monkeypatch, tmp_path, capsys):
    out = tmp_path / "out"
    assert run_main(["--platform", "linux", "--steam", "--out", str(out)], repo, editor, ValveFake("steam_appid.txt"),
                    monkeypatch) == 1
    assert "steam_appid.txt" in capsys.readouterr().err
    assert not (out / "build-record.json").exists()


def test_a_steam_folder_may_carry_the_steam_library(repo, editor, monkeypatch, tmp_path):
    out = tmp_path / "out"
    fake = ValveFake("Statecraft_Data/Plugins/libsteam_api.so")
    assert run_main(["--platform", "linux", "--steam", "--out", str(out)], repo, editor, fake, monkeypatch) == 0
    assert (out / "steam/linux/Statecraft_Data/Plugins/libsteam_api.so").is_file()


def test_a_dev_steam_build_is_recorded_as_dev(repo, editor, monkeypatch, tmp_path):
    out = tmp_path / "out"
    assert run_main(["--platform", "linux", "--steam", "--dev", "--out", str(out)], repo, editor, Fake(),
                    monkeypatch) == 0
    record = json.loads((out / "build-record.json").read_text())
    assert record["steam"] is True and record["dev"] is True and record["shareable"] is False
    # A GitHub release is not a Steam build and not dev.
    out2 = tmp_path / "out2"
    assert run_main(["--platform", "linux", "--out", str(out2)], repo, editor, Fake(), monkeypatch) == 0
    plain = json.loads((out2 / "build-record.json").read_text())
    assert plain["steam"] is False and plain["dev"] is False and plain["channel"] is None


SETTINGS = "unity/ProjectSettings/ProjectSettings.asset"


class StampFake(Fake):
    """The nightly's clone: ProjectSettings.asset differs from HEAD only by tonight's bundleVersion stamp. `git show`
    gives HEAD's text and `git checkout` restores it, as git would; each build records the bundleVersion it saw."""

    def __init__(self, repo, head_text, **kw):
        super().__init__(**kw)
        self.repo, self.head_text, self.seen = repo, head_text, []

    def __call__(self, cmd, **kw):
        cmd = [str(c) for c in cmd]
        if cmd[:2] == ["git", "show"]:
            self.calls.append(cmd)
            return rb.Result(0, self.head_text) if cmd[2] == f"HEAD:{SETTINGS}" else rb.Result(128, "")
        if cmd[:2] == ["git", "checkout"]:
            (self.repo / SETTINGS).write_text(self.head_text)
        if "-executeMethod" in cmd:
            self.seen.append(rb.versions()["bundle_version"])
        return super().__call__(cmd, **kw)


def stamped(repo, version="0.1.5", extra=""):
    """The nightly's own stamp (tools/nightly.py), so the accepted shape is the one the nightly really writes."""
    from nightly import stamp_bundle_version
    head = (repo / SETTINGS).read_text()
    stamp_bundle_version(repo / SETTINGS, version)
    if extra:
        (repo / SETTINGS).write_text((repo / SETTINGS).read_text() + extra)
    return head


def test_nightly_version_accepts_only_the_bundle_version_stamp(repo, editor, monkeypatch, tmp_path, capsys):
    argv = ["--platform", "all", "--steam", "--tag", "v0.1.5", "--nightly-version", "0.1.5"]
    head = stamped(repo)
    fake = StampFake(repo, head, dirty=f" M {SETTINGS}\n", tags=("v0.1.5",))
    out = tmp_path / "out"
    assert run_main([*argv, "--out", str(out)], repo, editor, fake, monkeypatch) == 0
    assert fake.seen == ["0.1.5"] * 3, "every Unity build carries tonight's version, not only the first"
    record = json.loads((out / "build-record.json").read_text())
    assert record["channel"] == "nightly" and record["tag"] == "v0.1.5" and record["nightly_version"] == "0.1.5"
    assert record["version"]["bundle_version"] == "0.1.5" and record["tree_clean"] is True
    # A clean tree (the nightly restores its stamp after the web builds) is accepted too.
    (repo / SETTINGS).write_text(head)
    fake = StampFake(repo, head, tags=("v0.1.5",))
    assert run_main([*argv, "--out", str(tmp_path / "clean")], repo, editor, fake, monkeypatch) == 0
    assert fake.seen == ["0.1.5"] * 3
    capsys.readouterr()


@pytest.mark.parametrize("case", ["other-file", "other-version", "other-line"])
def test_nightly_version_refuses_any_other_change(repo, editor, monkeypatch, tmp_path, capsys, case):
    argv = ["--platform", "linux", "--steam", "--tag", "v0.1.5", "--nightly-version", "0.1.5",
            "--out", str(tmp_path / "out")]
    dirty = f" M {SETTINGS}\n"
    if case == "other-file":
        head, dirty = stamped(repo), dirty + " M content/events/x.yaml\n"
    elif case == "other-version":
        head = stamped(repo, "0.1.6")
    else:
        head = stamped(repo, extra="  companyName: Someone\n")
    fake = StampFake(repo, head, dirty=dirty, tags=("v0.1.5",))
    assert run_main(argv, repo, editor, fake, monkeypatch) == 1
    assert unity_calls(fake) == [] and "REFUSED" in capsys.readouterr().err


@pytest.mark.parametrize("argv", [
    ["--tag", "v0.1.5", "--nightly-version", "0.1.5"],                       # not a Steam build
    ["--steam", "--dev", "--nightly-version", "0.1.5"],                      # a nightly is not a dev build
    ["--steam", "--tag", "v0.1.6", "--nightly-version", "0.1.5"],            # the tag is the nightly's own
    ["--steam", "--tag", "v0.1.5-x", "--nightly-version", "0.1.5-x"],        # X.Y.Z only
])
def test_nightly_version_needs_steam_and_its_own_tag(repo, editor, monkeypatch, tmp_path, argv):
    fake = Fake(tags=("v0.1.5", "v0.1.6", "v0.1.5-x"))
    assert run_main(["--platform", "linux", *argv, "--out", str(tmp_path / "out")], repo, editor, fake,
                    monkeypatch) == 1
    assert unity_calls(fake) == []


@pytest.mark.parametrize("platform", ["windows", "linux", "mac"])
def test_a_steam_folder_without_valves_library_is_refused(repo, editor, monkeypatch, tmp_path, capsys, platform):
    # Review I4 (#2155): a --steam run whose players lack Steam (an editor without #2154's -steam handling) must not
    # produce folders that look uploadable.
    class NoLibrary(Fake):
        steam_library = False
    out = tmp_path / "out"
    assert run_main(["--platform", platform, "--steam", "--out", str(out)], repo, editor, NoLibrary(), monkeypatch) == 1
    assert "Steam library" in capsys.readouterr().err and not (out / "build-record.json").exists()