"""Tests for the Pub/Sub emulator."""


def test_create_and_get_topic(client):
    resp = client.put(
        "/v1/projects/test-proj/topics/my-topic",
        json={"labels": {"env": "test"}},
    )
    assert resp.status_code == 200
    topic = resp.get_json()
    assert topic["name"] == "projects/test-proj/topics/my-topic"

    resp = client.get("/v1/projects/test-proj/topics/my-topic")
    assert resp.status_code == 200
    assert resp.get_json()["labels"]["env"] == "test"


def test_list_topics(client):
    client.put("/v1/projects/test-proj/topics/topic-a", json={})
    client.put("/v1/projects/test-proj/topics/topic-b", json={})
    resp = client.get("/v1/projects/test-proj/topics")
    data = resp.get_json()
    assert len(data["topics"]) == 2


def test_create_and_get_subscription(client):
    client.put("/v1/projects/test-proj/topics/events", json={})
    resp = client.put(
        "/v1/projects/test-proj/subscriptions/events-sub",
        json={
            "topic": "projects/test-proj/topics/events",
            "ackDeadlineSeconds": 30,
        },
    )
    assert resp.status_code == 200
    sub = resp.get_json()
    assert sub["ackDeadlineSeconds"] == 30

    resp = client.get("/v1/projects/test-proj/subscriptions/events-sub")
    assert resp.status_code == 200


def test_delete_topic(client):
    client.put("/v1/projects/test-proj/topics/tmp", json={})
    resp = client.delete("/v1/projects/test-proj/topics/tmp")
    assert resp.status_code == 200

    resp = client.get("/v1/projects/test-proj/topics/tmp")
    assert resp.status_code == 404
