package steam import ( "net/http" "net/http/httptest" "os" "testing" ) // QUIRK — Steam has NO public "free games" API; the storefront's own search // page with maxprice=free&specials=1 is the only machine-readable source. // It's server-rendered HTML: rows are `a.search_result_row`, the title lives // in a span.title, the price in div.discount_final_price (text "Free" for // giveaways). Pinned against the live page: rows are found, IDs/URLs are // derived from the /app// href, and kind is free-to-keep. Also pins the // quirk that the free page prices carry no whitespace before "Free". func TestLiveFixture_FreeToKeepExtracted(t *testing.T) { // Use a server that serves the fixture for the search URL; GetFreeGames // also queries featuredcategories — serve an empty JSON there. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { raw, err := os.ReadFile("testdata/search_free.html") if err != nil { t.Errorf("fixture: %v", err) return } w.Write(raw) })) t.Cleanup(srv.Close) c := NewClient() c.HTTP = srv.Client() // Redirect the client to the test server by rewriting the package-level // URLs through a custom transport? Simpler: the Client has fixed URLs, so // instead we test parseSearchRows directly on the fixture file. f, err := os.Open("testdata/search_free.html") if err != nil { t.Fatal(err) } defer f.Close() games, err := c.parseSearchRows(f, parseResultRow) if err != nil { t.Fatal(err) } if len(games) == 0 { t.Fatal("no free-to-keep games parsed from live fixture") } for _, g := range games { if g.ID == "" || g.Slug == "" || g.URL == "" { t.Errorf("game %+v missing id/slug/url", g.Title) } if g.Kind != "free-to-keep" { t.Errorf("game %q kind = %q, want free-to-keep", g.Title, g.Kind) } } } // QUIRK — the specials search page (?specials=1, any discount) uses THREE // separate price elements per row, and they are localized strings with the // currency glued on ("19,99€", no space): div.discount_pct ("-60%"), // div.discount_original_price, div.discount_final_price. Note the container // div class "discount_prices" (plural) — a Contains-match for singular // class names must not hit it. Pinned: every deal row yields percent > 0, // a final price, and kind "deal". func TestLiveFixture_DealsHavePricesAndPercent(t *testing.T) { c := NewClient() f, err := os.Open("testdata/search_specials.html") if err != nil { t.Fatal(err) } defer f.Close() games, err := c.parseSearchRows(f, parseDealRow) if err != nil { t.Fatal(err) } if len(games) == 0 { t.Fatal("no deals parsed from live specials fixture") } for _, g := range games { if g.DiscountPct <= 0 { t.Errorf("deal %q has DiscountPct %d, want > 0", g.Title, g.DiscountPct) } if g.FinalPriceFmt == "" { t.Errorf("deal %q has no final price", g.Title) } if g.Kind != "deal" { t.Errorf("deal %q kind = %q", g.Title, g.Kind) } } // Sorted by discount (deepest first) is enforced by GetDeals. } // QUIRK (our own bug, pinned as regression): when parseSearchRows was // refactored to take the row-parse function as a parameter, the body // kept calling parseResultRow directly. GetDeals then silently returned // 0 games — no error, just an empty list, easily missed. Pinned: the // PROVIDED parse function is what gets applied (deals must be non-empty // when parsing the specials page with parseDealRow). func TestParseSearchRowsUsesProvidedParseFn(t *testing.T) { f, err := os.Open("testdata/search_specials.html") if err != nil { t.Fatal(err) } defer f.Close() c := NewClient() deals, err := c.parseSearchRows(f, parseDealRow) if err != nil { t.Fatal(err) } if len(deals) == 0 { t.Fatal("parseDealRow was not applied — parse fn wiring broken (regression)") } } // QUIRK — the free search page mixes genuinely free rows with rows that // only LOOK free (e.g. 100 % discount badges on DLC without a base game). // parseResultRow applies the free filter (price text "Free" or −100 %); // this test pins that surviving rows never carry a final price — anything // with a real price belongs in the deals list, not here. func TestLiveFixture_FreeRowsHaveNoFinalPrice(t *testing.T) { c := NewClient() f, err := os.Open("testdata/search_free.html") if err != nil { t.Fatal(err) } defer f.Close() games, err := c.parseSearchRows(f, parseResultRow) if err != nil { t.Fatal(err) } for _, g := range games { if g.FinalPriceFmt != "" { t.Errorf("free game %q has final price %q", g.Title, g.FinalPriceFmt) } } } // QUIRK — the featuredcategories JSON is NOT a clean map of sections: it // mixes section objects with SCALAR values, notably `"status": 1`. A strict // json.Unmarshal into map[string]featuredSection fails on that key — which // silently killed the whole Free-Weekend path in production (the error was // swallowed by getFreeGames' non-fatal handling, so Free Weekend games never // appeared). Pinned: decoding the live fixture succeeds and yields sections. // The fix decodes per key and skips scalar junk. func TestLiveFixture_FeaturedCategoriesParses(t *testing.T) { raw, err := os.ReadFile("testdata/featured.json") if err != nil { t.Fatal(err) } srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.Write(raw) })) t.Cleanup(srv.Close) sections, err := decodeFeaturedSections(raw) if err != nil { t.Fatalf("decode featured fixture: %v", err) } if len(sections) == 0 { t.Fatal("no sections in featured fixture") } }