from fastapi.testclient import TestClient

from polyform_backend.api import create_app


def test_public_docs_and_deep_links_use_spa(tmp_path, monkeypatch):
    dashboard = tmp_path / "dashboard"
    dashboard.mkdir()
    (dashboard / "index.html").write_text("<h1>Polyform site</h1>")
    (dashboard / "shell.html").write_text('<div id="root"></div><script src="/assets/app.js"></script>')
    rendered_docs = dashboard / "prerender" / "docs"
    rendered_docs.mkdir(parents=True)
    (rendered_docs / "index.html").write_text("<h1>Readable Polyform docs</h1>")
    (dashboard / "llms.txt").write_text("# Polyform\nReadable by agents.\n")
    (dashboard / "robots.txt").write_text("User-agent: *\nAllow: /\n")
    (dashboard / "sitemap.xml").write_text("<urlset></urlset>")
    (dashboard / "assets").mkdir()
    (dashboard / "assets" / "app.js").write_text("console.log('polyform')")
    monkeypatch.setenv("POLYFORM_DASHBOARD_DIR", str(dashboard))

    with TestClient(create_app(str(tmp_path / "routes.db"))) as client:
        docs = client.get("/docs")
        assert "Readable Polyform docs" in docs.text
        assert docs.headers["cache-control"] == "no-store, max-age=0"
        assert 'id="root"' in client.get("/someone/project").text
        assert client.get("/api/docs").status_code == 200
        assert client.get("/assets/app.js").status_code == 200
        assert client.get("/assets/missing.js").status_code == 404
        assert "Readable by agents" in client.get("/llms.txt").text
        assert "Allow: /" in client.get("/robots.txt").text


def test_public_profiles_and_projects_have_server_rendered_content(tmp_path, monkeypatch):
    dashboard = tmp_path / "dashboard"
    dashboard.mkdir()
    shell = '<!doctype html><title>Polyform</title><div id="root"></div><script src="/assets/app.js"></script>'
    (dashboard / "index.html").write_text(shell)
    (dashboard / "shell.html").write_text(shell)
    monkeypatch.setenv("POLYFORM_DASHBOARD_DIR", str(dashboard))
    monkeypatch.setenv("POLYFORM_ROOT_PATH", "/polyform")

    with TestClient(create_app(str(tmp_path / "public-pages.db"))) as client:
        signup = client.post("/api/v1/auth/signup", json={
            "username": "ada", "email": "private@example.test", "password": "correct horse battery",
        })
        assert signup.status_code == 201
        client.cookies.set("polyform_session", signup.cookies["polyform_session"], path="/")
        project = client.post("/api/v1/projects", json={
            "slug": "fizz-buzz",
            "name": "Agent-built Fizz Buzz",
            "description": "Several interchangeable implementations.",
            "repository_url": "https://github.com/ada/fizz-buzz",
            "spec_path": "specs/polyform.toml",
        })
        assert project.status_code == 201

        profile_html = client.get("/ada").text
        project_html = client.get("/ada/fizz-buzz").text
        assert "@ada" in profile_html
        assert "Agent-built Fizz Buzz" in profile_html
        assert "/polyform/ada/fizz-buzz" in profile_html
        assert "Several interchangeable implementations" in project_html
        assert "https://github.com/ada/fizz-buzz" in project_html
        assert "specs/polyform.toml" in project_html
        assert "private@example.test" not in profile_html + project_html


def test_only_published_downloads_are_served(tmp_path, monkeypatch):
    downloads = tmp_path / "downloads"
    downloads.mkdir()
    (downloads / "install.sh").write_text("#!/bin/sh\n")
    (downloads / "private.txt").write_text("secret")
    monkeypatch.setenv("POLYFORM_DOWNLOAD_DIR", str(downloads))

    with TestClient(create_app(str(tmp_path / "downloads.db"))) as client:
        assert client.get("/downloads/install.sh").status_code == 200
        assert client.get("/downloads/private.txt").status_code == 404
