package scraper

import (
	"testing"

	"github.com/yourorg/devour/pkg/tsdocs"
)

func TestTSDocsSubDocsFallbackToModuleURL(t *testing.T) {
	s := &TSDocsScraper{}
	module := &tsdocs.Module{
		Name:   "Module",
		DocURL: "https://www.typescriptlang.org/docs/handbook/2/basic-types.html",
	}

	cases := []struct {
		name    string
		build   func() *Document
		docType string
	}{
		{
			name: "interface",
			build: func() *Document {
				return s.interfaceToDocument(&tsdocs.Interface{Name: "User", DocURL: ""}, module, "ts")
			},
			docType: "ts-interface",
		},
		{
			name: "function",
			build: func() *Document {
				return s.functionToDocument(&tsdocs.Function{Name: "parse", DocURL: ""}, module, "ts")
			},
			docType: "ts-function",
		},
		{
			name: "class",
			build: func() *Document {
				return s.classToDocument(&tsdocs.Class{Name: "Service", DocURL: ""}, module, "ts")
			},
			docType: "ts-class",
		},
		{
			name: "type alias",
			build: func() *Document {
				return s.typeAliasToDocument(&tsdocs.TypeAlias{Name: "ID", Type: "string", DocURL: ""}, module, "ts")
			},
			docType: "ts-type",
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			doc := tc.build()
			if doc.URL != module.DocURL {
				t.Fatalf("expected fallback URL %q, got %q", module.DocURL, doc.URL)
			}
			if got := doc.Metadata["doc_url"]; got != module.DocURL {
				t.Fatalf("expected metadata doc_url %q, got %#v", module.DocURL, got)
			}
			if doc.Type != tc.docType {
				t.Fatalf("expected doc type %q, got %q", tc.docType, doc.Type)
			}
		})
	}
}
