client_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package steam
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "os"
  6. "testing"
  7. )
  8. // QUIRK — Steam has NO public "free games" API; the storefront's own search
  9. // page with maxprice=free&specials=1 is the only machine-readable source.
  10. // It's server-rendered HTML: rows are `a.search_result_row`, the title lives
  11. // in a span.title, the price in div.discount_final_price (text "Free" for
  12. // giveaways). Pinned against the live page: rows are found, IDs/URLs are
  13. // derived from the /app/<id>/ href, and kind is free-to-keep. Also pins the
  14. // quirk that the free page prices carry no whitespace before "Free".
  15. func TestLiveFixture_FreeToKeepExtracted(t *testing.T) {
  16. // Use a server that serves the fixture for the search URL; GetFreeGames
  17. // also queries featuredcategories — serve an empty JSON there.
  18. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  19. raw, err := os.ReadFile("testdata/search_free.html")
  20. if err != nil {
  21. t.Errorf("fixture: %v", err)
  22. return
  23. }
  24. w.Write(raw)
  25. }))
  26. t.Cleanup(srv.Close)
  27. c := NewClient()
  28. c.HTTP = srv.Client()
  29. // Redirect the client to the test server by rewriting the package-level
  30. // URLs through a custom transport? Simpler: the Client has fixed URLs, so
  31. // instead we test parseSearchRows directly on the fixture file.
  32. f, err := os.Open("testdata/search_free.html")
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. defer f.Close()
  37. games, err := c.parseSearchRows(f, parseResultRow)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. if len(games) == 0 {
  42. t.Fatal("no free-to-keep games parsed from live fixture")
  43. }
  44. for _, g := range games {
  45. if g.ID == "" || g.Slug == "" || g.URL == "" {
  46. t.Errorf("game %+v missing id/slug/url", g.Title)
  47. }
  48. if g.Kind != "free-to-keep" {
  49. t.Errorf("game %q kind = %q, want free-to-keep", g.Title, g.Kind)
  50. }
  51. }
  52. }
  53. // QUIRK — the specials search page (?specials=1, any discount) uses THREE
  54. // separate price elements per row, and they are localized strings with the
  55. // currency glued on ("19,99€", no space): div.discount_pct ("-60%"),
  56. // div.discount_original_price, div.discount_final_price. Note the container
  57. // div class "discount_prices" (plural) — a Contains-match for singular
  58. // class names must not hit it. Pinned: every deal row yields percent > 0,
  59. // a final price, and kind "deal".
  60. func TestLiveFixture_DealsHavePricesAndPercent(t *testing.T) {
  61. c := NewClient()
  62. f, err := os.Open("testdata/search_specials.html")
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. defer f.Close()
  67. games, err := c.parseSearchRows(f, parseDealRow)
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. if len(games) == 0 {
  72. t.Fatal("no deals parsed from live specials fixture")
  73. }
  74. for _, g := range games {
  75. if g.DiscountPct <= 0 {
  76. t.Errorf("deal %q has DiscountPct %d, want > 0", g.Title, g.DiscountPct)
  77. }
  78. if g.FinalPriceFmt == "" {
  79. t.Errorf("deal %q has no final price", g.Title)
  80. }
  81. if g.Kind != "deal" {
  82. t.Errorf("deal %q kind = %q", g.Title, g.Kind)
  83. }
  84. }
  85. // Sorted by discount (deepest first) is enforced by GetDeals.
  86. }
  87. // QUIRK (our own bug, pinned as regression): when parseSearchRows was
  88. // refactored to take the row-parse function as a parameter, the body
  89. // kept calling parseResultRow directly. GetDeals then silently returned
  90. // 0 games — no error, just an empty list, easily missed. Pinned: the
  91. // PROVIDED parse function is what gets applied (deals must be non-empty
  92. // when parsing the specials page with parseDealRow).
  93. func TestParseSearchRowsUsesProvidedParseFn(t *testing.T) {
  94. f, err := os.Open("testdata/search_specials.html")
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. defer f.Close()
  99. c := NewClient()
  100. deals, err := c.parseSearchRows(f, parseDealRow)
  101. if err != nil {
  102. t.Fatal(err)
  103. }
  104. if len(deals) == 0 {
  105. t.Fatal("parseDealRow was not applied — parse fn wiring broken (regression)")
  106. }
  107. }
  108. // QUIRK — the free search page mixes genuinely free rows with rows that
  109. // only LOOK free (e.g. 100 % discount badges on DLC without a base game).
  110. // parseResultRow applies the free filter (price text "Free" or −100 %);
  111. // this test pins that surviving rows never carry a final price — anything
  112. // with a real price belongs in the deals list, not here.
  113. func TestLiveFixture_FreeRowsHaveNoFinalPrice(t *testing.T) {
  114. c := NewClient()
  115. f, err := os.Open("testdata/search_free.html")
  116. if err != nil {
  117. t.Fatal(err)
  118. }
  119. defer f.Close()
  120. games, err := c.parseSearchRows(f, parseResultRow)
  121. if err != nil {
  122. t.Fatal(err)
  123. }
  124. for _, g := range games {
  125. if g.FinalPriceFmt != "" {
  126. t.Errorf("free game %q has final price %q", g.Title, g.FinalPriceFmt)
  127. }
  128. }
  129. }
  130. // QUIRK — the featuredcategories JSON is NOT a clean map of sections: it
  131. // mixes section objects with SCALAR values, notably `"status": 1`. A strict
  132. // json.Unmarshal into map[string]featuredSection fails on that key — which
  133. // silently killed the whole Free-Weekend path in production (the error was
  134. // swallowed by getFreeGames' non-fatal handling, so Free Weekend games never
  135. // appeared). Pinned: decoding the live fixture succeeds and yields sections.
  136. // The fix decodes per key and skips scalar junk.
  137. func TestLiveFixture_FeaturedCategoriesParses(t *testing.T) {
  138. raw, err := os.ReadFile("testdata/featured.json")
  139. if err != nil {
  140. t.Fatal(err)
  141. }
  142. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
  143. w.Write(raw)
  144. }))
  145. t.Cleanup(srv.Close)
  146. sections, err := decodeFeaturedSections(raw)
  147. if err != nil {
  148. t.Fatalf("decode featured fixture: %v", err)
  149. }
  150. if len(sections) == 0 {
  151. t.Fatal("no sections in featured fixture")
  152. }
  153. }