package main

import "testing"

func TestParseArgsCPU(t *testing.T) {
	opts, err := parseArgs([]string{"--cpu", "Ryzen 3600x"})
	if err != nil {
		t.Fatalf("parseArgs returned error: %v", err)
	}
	if opts.kind != kindCPU || opts.query != "Ryzen 3600x" {
		t.Fatalf("unexpected options: %+v", opts)
	}
}

func TestParseArgsGPU(t *testing.T) {
	opts, err := parseArgs([]string{"--gpu", "Radeon RX 6650 XT"})
	if err != nil {
		t.Fatalf("parseArgs returned error: %v", err)
	}
	if opts.kind != kindGPU || opts.query != "Radeon RX 6650 XT" {
		t.Fatalf("unexpected options: %+v", opts)
	}
}

func TestPassMarkSelectedStatsCPU(t *testing.T) {
	html := `
<script>var cpuDetailsId = "3494";</script>
<li id="pk3494"><span class="more_details" onclick="p(event, '18,123', 1103, 8702, '$97.71');"></span>
<a><span class="prdname">AMD Ryzen 5 3600X</span><span class="count">185.5</span><span class="mark-neww">18,123</span><span class="price-neww">$97.71</span></a></li>`
	stats := passmarkSelectedStats(html)
	want := map[string]string{
		"Matched Product": "AMD Ryzen 5 3600X",
		"PassMark Score":  "18,123",
		"Value":           "185.5",
		"Price":           "$97.71",
		"Rank":            "1103",
		"Samples":         "8702",
	}
	assertStats(t, stats, want)
}

func TestPassMarkSelectedStatsGPU(t *testing.T) {
	html := `
<script>var gpuDetailsId = "4541";</script>
<li id="pk4541"><span class="more_details" onclick="p(event, 124, 17111, 5093, 299.99);"></span>
<a><span class="prdname">Radeon RX 6650 XT</span><span class="count">57.0</span><span class="mark-neww">17111</span><span class="price-neww">299.99*</span></a></li>`
	stats := passmarkSelectedStats(html)
	want := map[string]string{
		"Matched Product": "Radeon RX 6650 XT",
		"PassMark Score":  "17111",
		"Value":           "57.0",
		"Price":           "299.99*",
		"Rank":            "124",
		"Samples":         "5093",
	}
	assertStats(t, stats, want)
}

func TestBestCandidateSplitsGPUModelNumbers(t *testing.T) {
	best, ok := bestCandidate("Radeon RX6650xt", []candidate{
		{Name: "256MB DDR Radeon 9800 XT", URL: "old"},
		{Name: "Radeon RX 6650 XT", URL: "new"},
	})
	if !ok {
		t.Fatal("expected a match")
	}
	if best.URL != "new" {
		t.Fatalf("matched %q, want Radeon RX 6650 XT", best.Name)
	}
}

func assertStats(t *testing.T, stats []stat, want map[string]string) {
	t.Helper()
	got := make(map[string]string)
	for _, s := range stats {
		got[s.Name] = s.Value
	}
	for k, v := range want {
		if got[k] != v {
			t.Fatalf("stat %q = %q, want %q; all stats: %+v", k, got[k], v, stats)
		}
	}
}
