import json
from demos.grownet_trace_main import run_for_test


def test_trace_runner_single_neuron_contains_slot_select():
    output = run_for_test(["--lang", "python", "--trace", "--scenario", "single_neuron", "--value", "0.42"])
    assert "[slot.select]" in output


def test_trace_runner_growth_contains_growth_neuron_create():
    output = run_for_test(["--lang", "python", "--trace", "--scenario", "growth", "--value", "0.42"])
    assert "[growth.neuron.create]" in output


def test_trace_runner_jsonl_parses():
    output = run_for_test(
        ["--lang", "python", "--trace", "--trace-format", "jsonl", "--scenario", "single_neuron", "--value", "0.42"]
    )
    payloads = [json.loads(line) for line in output.splitlines() if line.strip()]
    assert payloads
    assert payloads[0]["event"] == "tick.start"


def test_trace_runner_filtering_suppresses_unwanted_events():
    output = run_for_test(
        [
            "--lang",
            "python",
            "--trace",
            "--trace-format",
            "jsonl",
            "--trace-events",
            "slot,growth",
            "--scenario",
            "growth",
            "--value",
            "0.42",
        ]
    )
    payloads = [json.loads(line) for line in output.splitlines() if line.strip()]
    assert payloads
    assert all(not payload["event"].startswith("tick.") for payload in payloads)
    assert any(payload["event"] == "growth.neuron.create" for payload in payloads)


def test_trace_runner_exact_event_name_filtering_keeps_only_threshold_updates():
    output = run_for_test(
        [
            "--lang",
            "python",
            "--trace",
            "--trace-format",
            "jsonl",
            "--trace-events",
            "threshold.update",
            "--scenario",
            "single_neuron",
            "--value",
            "0.42",
        ]
    )
    payloads = [json.loads(line) for line in output.splitlines() if line.strip()]
    assert payloads
    assert all(payload["event"] == "threshold.update" for payload in payloads)
