package file

import (
	"ragflow/internal/utility"
	"testing"
)

func TestBytesLooksLikePDF(t *testing.T) {
	cases := []struct {
		name string
		data []byte
		want bool
	}{
		{"valid header", []byte("%PDF-1.4 content"), true},
		{"too short", []byte("%PD"), false},
		{"plain text", []byte("hello"), false},
		{"nil", nil, false},
	}
	for _, c := range cases {
		if got := utility.BytesLooksLikePDF(c.data); got != c.want {
			t.Errorf("%s: utility.BytesLooksLikePDF = %v, want %v", c.name, got, c.want)
		}
	}
}

func TestLooksLikeHTML(t *testing.T) {
	if !utility.LooksLikeHTML([]byte("<html><body>x</body></html>")) {
		t.Error("should detect <html>")
	}
	if !utility.LooksLikeHTML([]byte("<DIV>x</DIV>")) {
		t.Error("should detect <div> case-insensitively")
	}
	if !utility.LooksLikeHTML([]byte("<body>x</body>")) {
		t.Error("should detect <body>")
	}
	if utility.LooksLikeHTML([]byte("just text")) {
		t.Error("should not detect plain text")
	}
}

func TestParseFileContent_HTMLOutputFormat(t *testing.T) {
	ctx := t.Context()
	// CSV parser produces OutputFormat "html".
	result := parseFileContent(ctx, "data.csv", []byte("a,b,c\n1,2,3\n"))
	if result == "" || result == string([]byte("a,b,c\n1,2,3\n")) {
		t.Skip("CSV parser not available or returned raw text; integration-only test")
	}
	// CSV parser emits an HTML table; must not contain raw CSV comma-separated rows.
	if result == "a,b,c\n1,2,3\n" {
		t.Errorf("CSV should produce HTML output, got raw CSV: %q", result)
	}
}

func TestParseFileContent_MarkdownOutputFormat(t *testing.T) {
	ctx := t.Context()
	// .md files fall through to default parser or produce text/markdown.
	result := parseFileContent(ctx, "doc.md", []byte("# Title\n\nBody"))
	// This is integration-dependent; verify it doesn't crash and returns something.
	if result == "" {
		t.Skip("markdown parser returned empty (integration-only)")
	}
}
