1
0

meitu_service_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //go:build !service
  2. // meitu_service_test.go — 「美图」工具纯函数单元测试。
  3. package main
  4. import (
  5. "strings"
  6. "testing"
  7. )
  8. // TestMeituCategories 验证分类注册表:至少包含「女装」且字段完整。
  9. func TestMeituCategories(t *testing.T) {
  10. svc := NewMeituService()
  11. cats := svc.Categories()
  12. if len(cats) == 0 {
  13. t.Fatal("Categories() 不应为空")
  14. }
  15. var found bool
  16. for _, c := range cats {
  17. if c.ID == "beauty" && c.Name == "女装" && c.PostID == "108629" {
  18. found = true
  19. if c.URL == "" {
  20. t.Error("女装分类缺少 URL")
  21. }
  22. }
  23. }
  24. if !found {
  25. t.Fatalf("未找到「女装」分类,当前: %+v", cats)
  26. }
  27. }
  28. // TestMeituFindCategory 验证按 ID 查找分类。
  29. func TestMeituFindCategory(t *testing.T) {
  30. svc := NewMeituService()
  31. if c := svc.findCategory("beauty"); c == nil {
  32. t.Fatal("findCategory(beauty) 应命中")
  33. }
  34. if c := svc.findCategory("nonexistent"); c != nil {
  35. t.Fatalf("findCategory(unknown) 应为 nil,得到 %+v", c)
  36. }
  37. }
  38. // TestParseMeituImages 验证从 API 响应提取图片:
  39. // 提取 content 内 <img src>、跳过非 http 地址、去重、携带作者与时间。
  40. func TestParseMeituImages(t *testing.T) {
  41. resp := &meituAPIResponse{
  42. Code: 0,
  43. Data: struct {
  44. TotalPages int `json:"total_pages"`
  45. CurrentPage int `json:"current_page"`
  46. List []struct {
  47. ID int64 `json:"id"`
  48. Author string `json:"author"`
  49. DateGmt string `json:"date_gmt"`
  50. Content string `json:"content"`
  51. } `json:"list"`
  52. }{
  53. List: []struct {
  54. ID int64 `json:"id"`
  55. Author string `json:"author"`
  56. DateGmt string `json:"date_gmt"`
  57. Content string `json:"content"`
  58. }{
  59. {
  60. ID: 1, Author: "四月天", DateGmt: "2026-11-24",
  61. Content: `<img src="https://img.wangmoyu.com/mw600/aaa.jpeg" />`,
  62. },
  63. {
  64. ID: 2, Author: "路人", DateGmt: "2026-11-23",
  65. Content: `<p>正文</p><img src="https://img.wangmoyu.com/mw600/bbb.jpeg.webp" />` +
  66. `<img src="data:image/gif;base64,xxx" />` + // 非 http 跳过
  67. `<img src="/relative/path.jpg" />`, // 相对路径跳过
  68. },
  69. {
  70. ID: 3, Author: "路人", DateGmt: "2026-11-23",
  71. Content: `<img src="https://img.wangmoyu.com/mw600/aaa.jpeg" />`, // 重复 → 去重
  72. },
  73. },
  74. },
  75. }
  76. imgs := parseMeituImages(resp)
  77. if len(imgs) != 2 {
  78. t.Fatalf("parseMeituImages() 应去重后得 2 张,得到 %d: %+v", len(imgs), imgs)
  79. }
  80. if imgs[0].URL != "https://img.wangmoyu.com/mw600/aaa.jpeg" {
  81. t.Errorf("首图 URL = %q", imgs[0].URL)
  82. }
  83. if imgs[0].Author != "四月天" || imgs[0].Date != "2026-11-24" {
  84. t.Errorf("首图作者/时间 = %q/%q", imgs[0].Author, imgs[0].Date)
  85. }
  86. if imgs[1].URL != "https://img.wangmoyu.com/mw600/bbb.jpeg.webp" {
  87. t.Errorf("二图 URL = %q", imgs[1].URL)
  88. }
  89. }
  90. // TestParseMeituImagesEmpty 验证空响应/无图片时返回空列表。
  91. func TestParseMeituImagesEmpty(t *testing.T) {
  92. if got := parseMeituImages(nil); len(got) != 0 {
  93. t.Fatalf("parseMeituImages(nil) = %+v, want empty", got)
  94. }
  95. resp := &meituAPIResponse{Code: 0}
  96. if got := parseMeituImages(resp); len(got) != 0 {
  97. t.Fatalf("parseMeituImages(空) = %+v, want empty", got)
  98. }
  99. }
  100. // TestMeituImgRe 验证正则只抓 http(s) 图片且大小写不敏感。
  101. func TestMeituImgRe(t *testing.T) {
  102. content := `<IMG SRC="https://img.wangmoyu.com/mw600/xx.JPEG">` +
  103. `<img src='https://img.wangmoyu.com/mw600/yy.webp'>` +
  104. `<img src="https://other.com/zz.png">`
  105. ms := meituImgRe.FindAllStringSubmatch(content, -1)
  106. if len(ms) != 3 {
  107. t.Fatalf("正则应匹配 3 个 img,得到 %d", len(ms))
  108. }
  109. if !strings.Contains(ms[0][1], "xx.JPEG") {
  110. t.Errorf("大写 SRC 未匹配: %q", ms[0][1])
  111. }
  112. }