"""Seed and run the Unicode clipping reproduction against a running T3 dev server.

Usage: python3 reproduce.py CHECKOUT FIXTURE_PATH
Start each checkout with T3CODE_DEV_SEED=0 vp run dev first. In Settings >
Providers, add OpenCode with default configuration. Use the SAME FIXTURE_PATH
for both checkouts. This uses the real installed OpenCode CLI and signed-in model.

Browser steps on each side (1280x800 CSS viewport, light theme):
Open the created thread, collapse sidebar and thread details, expand Worked for,
expand Searched src/**/*.txt, then scroll its preview to the end. Inspect the
last line. Base shows a replacement glyph; head stops before the emoji.
Read pre.textContent.length and its final charCodeAt for corroboration.
"""

import json
from pathlib import Path
import subprocess
import sys

checkout = Path(sys.argv[1]).resolve()
fixture = Path(sys.argv[2]).resolve()
(fixture / "src").mkdir(parents=True, exist_ok=True)
prefix_units = len(str(fixture / "src" / "m00_").encode("utf-16-le")) // 2
line_units = next(
    n for n in range(max(97, prefix_units + 7), 500)
    if prefix_units <= 7999 % n <= n - 7 and 7999 // n < 89
)
emoji_column = 7999 % line_units
for i in range(89):
    name = (
        f"m{i:02}_" + "a" * (emoji_column - prefix_units)
        + "😀" + "x" * (line_units - emoji_column - 7) + ".txt"
    )
    (fixture / "src" / name).touch()
subprocess.run(["git", "init", "-q", str(fixture)], check=True)
listing = "".join(str(p) + "\n" for p in (fixture / "src").iterdir())
units = listing.encode("utf-16-le")
assert int.from_bytes(units[15998:16000], "little") == 0xD83D
print(json.dumps({"files": 89, "lineUnits": line_units, "boundary": "D83D at 7999"}))

def cli(*args):
    subprocess.run(
        ["node", "apps/server/src/bin.ts", *args, "--base-dir", ".t3"],
        cwd=checkout, check=True,
    )

cli("project", "add", str(fixture), "--title", "Unicode preview")
cli(
    "thread", "start", str(fixture),
    "Actually invoke the native glob tool exactly once with pattern src/**/*.txt. "
    "Do not use bash or other tools. After the glob result, reply only with the "
    "number of files. You must call glob, not just say done.",
    "--title", "Unicode search preview", "--provider", "opencode",
    "--model", "anthropic/claude-haiku-5-5", "--wait", "--json",
)
