package services
import (
"strings"
"testing"
"arma3-web-server/internal/models"
)
func TestParseModlistHTML_SingleMod(t *testing.T) {
html := `
`
_, mods, err := ParseModlistHTML(strings.NewReader(html))
if err != nil {
t.Fatalf("ParseModlistHTML() error = %v", err)
}
if len(mods) != 1 {
t.Fatalf("got %d mods, want 1", len(mods))
}
if mods[0].ID != "450814997" {
t.Errorf("mods[0].ID = %q, want %q", mods[0].ID, "450814997")
}
if mods[0].Name != "CBA_A3" {
t.Errorf("mods[0].Name = %q, want %q", mods[0].Name, "CBA_A3")
}
if !mods[0].Enabled {
t.Error("mods[0].Enabled should be true")
}
}
func TestParseModlistHTML_MultipleMods(t *testing.T) {
html := ``
_, mods, err := ParseModlistHTML(strings.NewReader(html))
if err != nil {
t.Fatalf("ParseModlistHTML() error = %v", err)
}
if len(mods) != 2 {
t.Fatalf("got %d mods, want 2", len(mods))
}
if mods[0].Name != "CBA_A3" || mods[1].Name != "ACE3" {
t.Errorf("mods = [%q, %q], want [CBA_A3, ACE3]", mods[0].Name, mods[1].Name)
}
}
func TestParseModlistHTML_Deduplication(t *testing.T) {
html := ``
_, mods, _ := ParseModlistHTML(strings.NewReader(html))
if len(mods) != 1 {
t.Errorf("got %d mods, want 1 (deduplication)", len(mods))
}
}
func TestParseModlistHTML_EmptyTable(t *testing.T) {
html := ``
_, mods, err := ParseModlistHTML(strings.NewReader(html))
if err != nil {
t.Fatalf("ParseModlistHTML() error = %v", err)
}
if len(mods) != 0 {
t.Errorf("got %d mods, want 0", len(mods))
}
}
func TestParseModlistHTML_InvalidHTML(t *testing.T) {
// Go's html parser is lenient and returns empty results for garbage input
_, mods, err := ParseModlistHTML(strings.NewReader("not html at all <>"))
if err != nil {
t.Fatalf("ParseModlistHTML() should not error on lenient parser, got: %v", err)
}
if len(mods) != 0 {
t.Errorf("ParseModlistHTML() returned %d mods for garbage input, want 0", len(mods))
}
}
func TestRenderModlistHTML(t *testing.T) {
mods := []models.ModEntry{
{ID: "450814997", Name: "CBA_A3", Enabled: true},
{ID: "463939057", Name: "ACE3", Enabled: true},
}
output, err := RenderModlistHTML("Test", mods)
if err != nil {
t.Fatalf("RenderModlistHTML() error = %v", err)
}
if !strings.Contains(output, "450814997") {
t.Error("output should contain mod ID 450814997")
}
if !strings.Contains(output, "CBA_A3") {
t.Error("output should contain mod name CBA_A3")
}
if !strings.Contains(output, "463939057") {
t.Error("output should contain mod ID 463939057")
}
if !strings.Contains(output, "