package cloudflaredocs

import (
	"strings"
	"testing"
)

const testCloudflarePageHTML = `
<!DOCTYPE html>
<html>
<head>
  <title>Workers API | Cloudflare Docs</title>
  <meta name="description" content="Build and deploy serverless apps.">
</head>
<body>
  <div class="product-name">Workers</div>
  <main>
    <h1 id="workers-api">Workers API</h1>
    <p>Cloudflare Workers lets you run JavaScript at the edge.</p>
    <h2 id="endpoints">Endpoints</h2>
    <pre><code class="language-http">GET /client/v4/accounts/{id}/workers/scripts</code></pre>
  </main>
</body>
</html>
`

func TestParsePage(t *testing.T) {
	parser := NewParser()

	page, err := parser.ParsePage(testCloudflarePageHTML, "https://developers.cloudflare.com/workers/api/")
	if err != nil {
		t.Fatalf("ParsePage failed: %v", err)
	}

	if page.Title != "Workers API" {
		t.Fatalf("unexpected title: %q", page.Title)
	}
	if page.Product != "Workers" {
		t.Fatalf("unexpected product: %q", page.Product)
	}
	if page.Description == "" {
		t.Fatal("expected non-empty description")
	}
	if len(page.Sections) < 2 {
		t.Fatalf("expected at least 2 sections, got %d", len(page.Sections))
	}
	if len(page.CodeBlocks) == 0 {
		t.Fatal("expected at least one code block")
	}
	if len(page.APIs) == 0 {
		t.Fatal("expected at least one parsed API endpoint")
	}
	if page.APIs[0].Method != "GET" {
		t.Fatalf("expected API method GET, got %q", page.APIs[0].Method)
	}
	if !strings.HasPrefix(page.APIs[0].Endpoint, "/client/v4/") {
		t.Fatalf("unexpected endpoint: %q", page.APIs[0].Endpoint)
	}
}

func TestParseSidebar(t *testing.T) {
	parser := NewParser()

	html := `
<div class="sidebar">
  <a href="/workers/">Workers</a>
  <a href="/dns/">DNS</a>
</div>`

	sections, err := parser.ParseSidebar(html)
	if err != nil {
		t.Fatalf("ParseSidebar failed: %v", err)
	}

	if len(sections) != 2 {
		t.Fatalf("expected 2 sections, got %d", len(sections))
	}
	if sections[0].DocURL != "https://developers.cloudflare.com/workers/" {
		t.Fatalf("unexpected resolved URL: %q", sections[0].DocURL)
	}
}

func TestResolveURL(t *testing.T) {
	got := resolveURL("https://developers.cloudflare.com", "/workers/")
	if got != "https://developers.cloudflare.com/workers/" {
		t.Fatalf("resolveURL returned %q", got)
	}
}
