~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/stream

jetstream v2 in zig · mirror of https://tangled.org/zat.dev/stream

0
0
126 lines · 4.5 KB Raw History
#!/usr/bin/env python3
"""Run Stream through the pinned upstream Jetstream V2 oracle offline.

The Go test lives in this repository, but is overlaid into upstream's
``internal/oracle`` package for compilation.  That deliberately reuses the
pinned simulator, JSS reader, event-log normalizer, final-state model, public
client, and comparison code instead of growing a second interpretation here.
"""

import json
import os
from pathlib import Path
import platform
import subprocess
import tempfile


PIN = "289b0328c2e1a0ccf8c870cb45de0b2397de19fb"
ROOT = Path(__file__).resolve().parents[1]
UPSTREAM = Path(
    os.environ.get(
        "STREAM_UPSTREAM_REPO",
        Path.home() / "github.com/bluesky-social/jetstream",
    )
).resolve()
SOURCE = ROOT / "tests/upstream_oracle/stream_differential_test.go"
FAULT_SOURCE = ROOT / "tests/upstream_oracle/stream_fault_differential_test.go"
RESTART_SOURCE = ROOT / "tests/upstream_oracle/stream_restart_predicate_test.go"
RESYNC_CAPACITY_SOURCE = ROOT / "tests/upstream_oracle/stream_resync_capacity_test.go"
STREAM_BIN = (ROOT / "zig-out/bin/stream").resolve()


def output(*args: str, cwd: Path) -> str:
    return subprocess.check_output(args, cwd=cwd, text=True).strip()


def go_binary() -> str:
    explicit = os.environ.get("STREAM_GO")
    if explicit:
        return explicit
    if "go1.26.5 " in output("go", "version", cwd=ROOT):
        return "go"
    modcache = Path(output("go", "env", "GOMODCACHE", cwd=ROOT))
    goos = {"Darwin": "darwin", "Linux": "linux"}.get(platform.system())
    machine = {"arm64": "arm64", "aarch64": "arm64", "x86_64": "amd64"}.get(
        platform.machine()
    )
    if goos is None or machine is None:
        raise SystemExit(f"unsupported cached Go toolchain platform: {platform.platform()}")
    cached = (
        modcache
        / f"golang.org/toolchain@v0.0.1-go1.26.5.{goos}-{machine}"
        / "bin/go"
    )
    if not cached.is_file():
        raise SystemExit(
            "Go 1.26.5 is not available offline; install or cache it before this contract"
        )
    return str(cached)


def main() -> None:
    if not STREAM_BIN.is_file():
        raise SystemExit("missing zig-out/bin/stream; run `zig build -Doptimize=ReleaseSafe`")
    if not (UPSTREAM / "go.mod").is_file():
        raise SystemExit(f"pinned upstream checkout not found at {UPSTREAM}")

    head = output("git", "rev-parse", "HEAD", cwd=UPSTREAM)
    if head != PIN:
        raise SystemExit(f"upstream pin mismatch: want {PIN}, got {head}")
    go = go_binary()
    run_pattern = os.environ.get(
        "STREAM_ORACLE_RUN",
        "^TestStream(Differential|ReplayFault|FrameFault|Adversarial|ResyncCapacity|BootstrapControls|BackfillWorkerControls)Oracle$",
    )

    # -overlay can add a test file to an existing Go package.  Compiling it as
    # part of internal/oracle means Go's internal-package rule remains intact,
    # while the authoritative source and every committed edit stay in Stream.
    virtual = UPSTREAM / "internal/oracle/stream_differential_test.go"
    fault_virtual = UPSTREAM / "internal/oracle/stream_fault_differential_test.go"
    restart_virtual = UPSTREAM / "internal/oracle/stream_restart_predicate_test.go"
    resync_capacity_virtual = UPSTREAM / "internal/oracle/stream_resync_capacity_test.go"
    with tempfile.TemporaryDirectory(prefix="stream-differential-oracle-") as tmp:
        overlay = Path(tmp) / "overlay.json"
        overlay.write_text(
            json.dumps(
                {
                    "Replace": {
                        str(virtual): str(SOURCE.resolve()),
                        str(fault_virtual): str(FAULT_SOURCE.resolve()),
                        str(restart_virtual): str(RESTART_SOURCE.resolve()),
                        str(resync_capacity_virtual): str(RESYNC_CAPACITY_SOURCE.resolve()),
                    }
                }
            ),
            encoding="utf-8",
        )
        env = os.environ.copy()
        env.update(
            {
                "GOPROXY": "off",
                "GOSUMDB": "off",
                "GOTOOLCHAIN": "local",
                "STREAM_ORACLE_BIN": str(STREAM_BIN),
                "STREAM_ORACLE_EXPECTED_PIN": PIN,
            }
        )
        subprocess.run(
            [
                go,
                "test",
                f"-overlay={overlay}",
                "-count=1",
                f"-run={run_pattern}",
                "-v",
                "./internal/oracle",
            ],
            cwd=UPSTREAM,
            env=env,
            check=True,
        )


if __name__ == "__main__":
    main()