#!/usr/bin/env python3
"""Fake ``claude -p --output-format stream-json`` for the conformance suite.

Reads a JSON events spec from ``$NVSH_FAKE_EVENTS`` (a list of
``{"kind": ..., "text": ..., "error": ...}`` dicts -- see
``tests/_fake_adapters.py``) and emits claude's stream-json envelope lines
so ``nvsh.agent.claude.ClaudeAgent`` can be exercised end to end without a
real ``claude`` binary on PATH.
"""

from __future__ import annotations

import json
import os
import sys


def main() -> int:
    events_path = os.environ.get("NVSH_FAKE_EVENTS")
    events = json.loads(open(events_path, encoding="utf-8").read()) if events_path else []

    for event in events:
        kind = event.get("kind")
        if kind == "status":
            line = {"type": "system", "subtype": "status", "text": event.get("text", "")}
        elif kind == "text_delta":
            line = {
                "type": "assistant",
                "message": {"content": [{"type": "text", "text": event.get("text", "")}]},
            }
        elif kind == "error":
            print(json.dumps({"type": "result", "subtype": "error", "error": event.get("error", "")}))
            sys.stdout.flush()
            return 1
        elif kind == "done":
            print(json.dumps({"type": "result", "subtype": "success"}))
            sys.stdout.flush()
            return 0
        else:
            continue
        print(json.dumps(line))
        sys.stdout.flush()
    return 0


if __name__ == "__main__":
    sys.exit(main())
