package webpage

import (
	"context"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
)

func TestFetchableURL(t *testing.T) {
	if FetchableURL("https://example.com/article") != true {
		t.Fatal("expected fetchable")
	}
	if FetchableURL("https://www.threads.net/@foo") != false {
		t.Fatal("threads should be skipped")
	}
	if FetchableURL("ftp://example.com") != false {
		t.Fatal("ftp should be skipped")
	}
}

func TestFetchDigests_ExtractsMarkdown(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		w.Header().Set("Content-Type", "text/html; charset=utf-8")
		_, _ = w.Write([]byte(`<!doctype html><html><head><title>備孕指南</title></head><body><article><h1>備孕指南</h1><p>葉酸與鋅很重要，作息要穩定。</p></article></body></html>`))
	}))
	defer srv.Close()

	digests := FetchDigests(context.Background(), []string{srv.URL}, FetchOptions{MaxPages: 1})
	if len(digests) != 1 {
		t.Fatalf("digests = %v", digests)
	}
	if digests[0].Error != "" {
		t.Fatalf("unexpected error: %s", digests[0].Error)
	}
	if digests[0].Markdown == "" {
		t.Fatal("expected markdown")
	}
	if !strings.Contains(digests[0].Markdown, "備孕指南") || !strings.Contains(digests[0].Markdown, "葉酸") {
		t.Fatalf("markdown = %q", digests[0].Markdown)
	}
}
