from promrs import CollectorRegistry, Counter, Gauge, Histogram, generate_latest


def test_empty_registry() -> None:
    registry = CollectorRegistry()
    output = generate_latest(registry).decode()
    assert output == "# EOF\n"


def test_multiple_metrics() -> None:
    registry = CollectorRegistry()
    c = Counter("counter_metric", "A counter", ["k"], registry=registry)
    g = Gauge("gauge_metric", "A gauge", ["k"], registry=registry)
    h = Histogram("hist_metric", "A histogram", ["k"], registry=registry, buckets=[1.0])
    c.labels(k="v").inc()
    g.labels(k="v").set(42.0)
    h.labels(k="v").observe(0.5)
    output = generate_latest(registry).decode()
    assert "counter_metric" in output
    assert "gauge_metric" in output
    assert "hist_metric" in output


def test_generate_latest_returns_bytes() -> None:
    registry = CollectorRegistry()
    result = generate_latest(registry)
    assert isinstance(result, bytes)


def test_help_text_in_output() -> None:
    registry = CollectorRegistry()
    Counter("my_counter", "This is help text", ["l"], registry=registry)
    output = generate_latest(registry).decode()
    assert "# HELP my_counter This is help text." in output


def test_type_annotations_in_output() -> None:
    registry = CollectorRegistry()
    Counter("c", "counter help", ["l"], registry=registry)
    Gauge("g", "gauge help", ["l"], registry=registry)
    Histogram("h", "histogram help", ["l"], registry=registry, buckets=[1.0])
    output = generate_latest(registry).decode()
    # OpenMetrics: TYPE uses base name, counter type is "counter" (not "c_total counter")
    assert "# TYPE c counter" in output
    assert "# TYPE g gauge" in output
    assert "# TYPE h histogram" in output


def test_eof_marker() -> None:
    registry = CollectorRegistry()
    Counter("test_eof", "test", ["l"], registry=registry)
    output = generate_latest(registry).decode()
    assert output.endswith("# EOF\n")
