|
1 | 1 | package card |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
4 | 5 | "strings" |
5 | 6 | "testing" |
6 | 7 |
|
@@ -254,3 +255,113 @@ func TestCardsModel_View_MissingPrice(t *testing.T) { |
254 | 255 | t.Error("View() should display 'Price: Not available' for cards without pricing") |
255 | 256 | } |
256 | 257 | } |
| 258 | + |
| 259 | +func TestCardsList_SuccessAndFallbacks(t *testing.T) { |
| 260 | + // Save and restore getCardData stub |
| 261 | + original := getCardData |
| 262 | + defer func() { getCardData = original }() |
| 263 | + |
| 264 | + var capturedURL string |
| 265 | + getCardData = func(url string) ([]byte, error) { |
| 266 | + capturedURL = url |
| 267 | + // Return two cards: one with price + illustrator, one with fallbacks |
| 268 | + json := `[ |
| 269 | + {"number_plus_name":"001/198 - Bulbasaur","market_price":1.5,"image_url":"https://example.com/bulba.png","illustrator":"Ken Sugimori"}, |
| 270 | + {"number_plus_name":"002/198 - Ivysaur","market_price":0,"image_url":"https://example.com/ivy.png","illustrator":""} |
| 271 | + ]` |
| 272 | + return []byte(json), nil |
| 273 | + } |
| 274 | + |
| 275 | + model, err := CardsList("set123") |
| 276 | + if err != nil { |
| 277 | + t.Fatalf("CardsList returned error: %v", err) |
| 278 | + } |
| 279 | + |
| 280 | + // URL should target the correct set id and select fields |
| 281 | + if !strings.Contains(capturedURL, "set_id=eq.set123") { |
| 282 | + t.Errorf("expected URL to contain set_id filter, got %s", capturedURL) |
| 283 | + } |
| 284 | + if !strings.Contains(capturedURL, "select=number_plus_name,market_price,image_url,illustrator") { |
| 285 | + t.Errorf("expected URL to contain select fields, got %s", capturedURL) |
| 286 | + } |
| 287 | + |
| 288 | + // PriceMap expectations |
| 289 | + if got := model.PriceMap["001/198 - Bulbasaur"]; got != "Price: $1.50" { |
| 290 | + t.Errorf("unexpected price for Bulbasaur: %s", got) |
| 291 | + } |
| 292 | + if got := model.PriceMap["002/198 - Ivysaur"]; got != "Pricing not available" { |
| 293 | + t.Errorf("unexpected price for Ivysaur: %s", got) |
| 294 | + } |
| 295 | + |
| 296 | + // IllustratorMap expectations |
| 297 | + if got := model.IllustratorMap["001/198 - Bulbasaur"]; got != "Illustrator: Ken Sugimori" { |
| 298 | + t.Errorf("unexpected illustrator for Bulbasaur: %s", got) |
| 299 | + } |
| 300 | + if got := model.IllustratorMap["002/198 - Ivysaur"]; got != "Illustrator not available" { |
| 301 | + t.Errorf("unexpected illustrator for Ivysaur: %s", got) |
| 302 | + } |
| 303 | + |
| 304 | + // Image map |
| 305 | + if model.ImageMap["001/198 - Bulbasaur"] != "https://example.com/bulba.png" { |
| 306 | + t.Errorf("unexpected image url for Bulbasaur: %s", model.ImageMap["001/198 - Bulbasaur"]) |
| 307 | + } |
| 308 | + if model.ImageMap["002/198 - Ivysaur"] != "https://example.com/ivy.png" { |
| 309 | + t.Errorf("unexpected image url for Ivysaur: %s", model.ImageMap["002/198 - Ivysaur"]) |
| 310 | + } |
| 311 | + |
| 312 | + if row := model.Table.SelectedRow(); len(row) == 0 { |
| 313 | + if model.View() == "" { |
| 314 | + t.Error("model view should render even if no row is selected") |
| 315 | + } |
| 316 | + } |
| 317 | +} |
| 318 | + |
| 319 | +func TestCardsList_FetchError(t *testing.T) { |
| 320 | + original := getCardData |
| 321 | + defer func() { getCardData = original }() |
| 322 | + |
| 323 | + getCardData = func(url string) ([]byte, error) { |
| 324 | + return nil, errors.New("network error") |
| 325 | + } |
| 326 | + |
| 327 | + _, err := CardsList("set123") |
| 328 | + if err == nil { |
| 329 | + t.Fatal("expected error when fetch fails") |
| 330 | + } |
| 331 | +} |
| 332 | + |
| 333 | +func TestCardsList_BadJSON(t *testing.T) { |
| 334 | + original := getCardData |
| 335 | + defer func() { getCardData = original }() |
| 336 | + |
| 337 | + getCardData = func(url string) ([]byte, error) { |
| 338 | + return []byte("not-json"), nil |
| 339 | + } |
| 340 | + |
| 341 | + _, err := CardsList("set123") |
| 342 | + if err == nil { |
| 343 | + t.Fatal("expected error for bad JSON payload") |
| 344 | + } |
| 345 | +} |
| 346 | + |
| 347 | +func TestCardsList_EmptyResult(t *testing.T) { |
| 348 | + original := getCardData |
| 349 | + defer func() { getCardData = original }() |
| 350 | + |
| 351 | + getCardData = func(url string) ([]byte, error) { |
| 352 | + return []byte("[]"), nil |
| 353 | + } |
| 354 | + |
| 355 | + model, err := CardsList("set123") |
| 356 | + if err != nil { |
| 357 | + t.Fatalf("unexpected error: %v", err) |
| 358 | + } |
| 359 | + |
| 360 | + if len(model.PriceMap) != 0 || len(model.IllustratorMap) != 0 || len(model.ImageMap) != 0 { |
| 361 | + t.Errorf("expected empty maps, got price:%d illus:%d image:%d", len(model.PriceMap), len(model.IllustratorMap), len(model.ImageMap)) |
| 362 | + } |
| 363 | + |
| 364 | + if model.View() == "" { |
| 365 | + t.Error("expected view to render with empty data") |
| 366 | + } |
| 367 | +} |
0 commit comments