updater_compare_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // updater_compare_test.go — changelog humano del asistente (issue #490):
  2. // el Check expone los commits current→latest (compare de GitHub) en ambos
  3. // modos, tolera fallos del compare y cachea por par (current, latest).
  4. package updater
  5. import (
  6. "context"
  7. "fmt"
  8. "net/http"
  9. "strings"
  10. "testing"
  11. )
  12. // mockCompare: respuesta del compare con 3 commits oldest→newest (GitHub los
  13. // devuelve en orden cronológico; el updater los invierte).
  14. const compareBody = `{
  15. "commits": [
  16. {"sha": "1111111111111111", "commit": {"message": "fix(wifi): parse freq as float (#475)\n\ncon cuerpo"}},
  17. {"sha": "2222222222222222", "commit": {"message": "feat(routers): one-click agent install (#483)"}},
  18. {"sha": "deadbee1234567890", "commit": {"message": "feat: newest\n\nlong body"}}
  19. ]
  20. }`
  21. func TestCheckFetchesCompareCommits(t *testing.T) {
  22. root := t.TempDir()
  23. writeDeployScript(t, root)
  24. short := writeGitHead(t, root)
  25. withAPI(t, func(w http.ResponseWriter, r *http.Request) {
  26. switch {
  27. case r.URL.Path == "/repos/owner/netpulse/commits/main":
  28. w.Header().Set("Content-Type", "application/json")
  29. fmt.Fprint(w, `{"sha":"deadbee1234567890","commit":{"message":"feat: newest\n\nlong body"}}`)
  30. case strings.HasPrefix(r.URL.Path, "/repos/owner/netpulse/compare/"):
  31. w.Header().Set("Content-Type", "application/json")
  32. fmt.Fprint(w, compareBody)
  33. case r.URL.Path == "/repos/owner/netpulse/releases/latest":
  34. w.Header().Set("Content-Type", "application/json")
  35. fmt.Fprint(w, `{"tag_name":"v2.5.0"}`)
  36. default:
  37. t.Errorf("path inesperado: %s", r.URL.Path)
  38. }
  39. })
  40. u := New(root, "owner/netpulse", "", "2.0.0", nil)
  41. st := u.Check(context.Background())
  42. if st.Error != nil {
  43. t.Fatalf("error: %v", *st.Error)
  44. }
  45. if !st.UpdateAvailable {
  46. t.Fatal("debería haber update")
  47. }
  48. if len(st.Commits) != 3 {
  49. t.Fatalf("commits: %+v", st.Commits)
  50. }
  51. // Newest first y subjects = primera línea del mensaje.
  52. if st.Commits[0].SHA != "deadbee" || st.Commits[0].Subject != "feat: newest" {
  53. t.Fatalf("commits[0]: %+v", st.Commits[0])
  54. }
  55. if st.Commits[2].SHA != "1111111" || st.Commits[2].Subject != "fix(wifi): parse freq as float (#475)" {
  56. t.Fatalf("commits[2]: %+v", st.Commits[2])
  57. }
  58. want := "https://github.com/owner/netpulse/compare/" + short + "...deadbee1234567890"
  59. if st.CompareURL != want {
  60. t.Fatalf("compareUrl: %q, want %q", st.CompareURL, want)
  61. }
  62. }
  63. func TestCheckCompareStableUsesTags(t *testing.T) {
  64. withAPI(t, func(w http.ResponseWriter, r *http.Request) {
  65. switch {
  66. case r.URL.Path == "/repos/owner/netpulse/releases/latest":
  67. w.Header().Set("Content-Type", "application/json")
  68. fmt.Fprint(w, `{"tag_name":"v2.1.0","name":"v2.1.0","body":"release notes"}`)
  69. case r.URL.Path == "/repos/owner/netpulse/compare/v2.0.0...v2.1.0":
  70. w.Header().Set("Content-Type", "application/json")
  71. fmt.Fprint(w, `{"commits":[{"sha":"abcdef0123456789","commit":{"message":"feat: uno"}}]}`)
  72. default:
  73. t.Errorf("path inesperado: %s", r.URL.Path)
  74. }
  75. })
  76. // Sin deploy/update.sh → estable; version sin prefijo "v" (ldflags).
  77. u := New(t.TempDir(), "owner/netpulse", "", "2.0.0", nil)
  78. st := u.Check(context.Background())
  79. if !st.UpdateAvailable {
  80. t.Fatal("debería haber update 2.0.0 → 2.1.0")
  81. }
  82. if len(st.Commits) != 1 || st.Commits[0].Subject != "feat: uno" || st.Commits[0].SHA != "abcdef0" {
  83. t.Fatalf("commits: %+v", st.Commits)
  84. }
  85. if st.CompareURL != "https://github.com/owner/netpulse/compare/v2.0.0...v2.1.0" {
  86. t.Fatalf("compareUrl: %q", st.CompareURL)
  87. }
  88. }
  89. func TestCheckCompareFailureTolerated(t *testing.T) {
  90. root := t.TempDir()
  91. writeDeployScript(t, root)
  92. writeGitHead(t, root)
  93. withAPI(t, func(w http.ResponseWriter, r *http.Request) {
  94. switch {
  95. case r.URL.Path == "/repos/owner/netpulse/commits/main":
  96. w.Header().Set("Content-Type", "application/json")
  97. fmt.Fprint(w, `{"sha":"deadbee1234567890","commit":{"message":"feat: newest\n\nbody"}}`)
  98. case strings.HasPrefix(r.URL.Path, "/repos/owner/netpulse/compare/"):
  99. http.Error(w, "boom", http.StatusInternalServerError)
  100. case r.URL.Path == "/repos/owner/netpulse/releases":
  101. // #404: body no vacío en el mock → no llega aquí, pero por si acaso.
  102. fmt.Fprint(w, `[]`)
  103. case r.URL.Path == "/repos/owner/netpulse/releases/latest":
  104. w.Header().Set("Content-Type", "application/json")
  105. fmt.Fprint(w, `{"tag_name":"v2.5.0"}`)
  106. default:
  107. t.Errorf("path inesperado: %s", r.URL.Path)
  108. }
  109. })
  110. u := New(root, "owner/netpulse", "", "2.0.0", nil)
  111. st := u.Check(context.Background())
  112. if st.Error != nil {
  113. t.Fatalf("el fallo del compare no debe ser error: %v", *st.Error)
  114. }
  115. if !st.UpdateAvailable {
  116. t.Fatal("debería haber update pese al compare caído")
  117. }
  118. if st.Commits != nil || st.CompareURL != "" {
  119. t.Fatalf("compare fallido: %+v %q", st.Commits, st.CompareURL)
  120. }
  121. }
  122. func TestCheckCompareCachedPerPair(t *testing.T) {
  123. root := t.TempDir()
  124. writeDeployScript(t, root)
  125. writeGitHead(t, root)
  126. compareCalls := 0
  127. withAPI(t, func(w http.ResponseWriter, r *http.Request) {
  128. switch {
  129. case r.URL.Path == "/repos/owner/netpulse/commits/main":
  130. w.Header().Set("Content-Type", "application/json")
  131. fmt.Fprint(w, `{"sha":"deadbee1234567890","commit":{"message":"feat: newest\n\nbody"}}`)
  132. case strings.HasPrefix(r.URL.Path, "/repos/owner/netpulse/compare/"):
  133. compareCalls++
  134. w.Header().Set("Content-Type", "application/json")
  135. fmt.Fprint(w, compareBody)
  136. case r.URL.Path == "/repos/owner/netpulse/releases/latest":
  137. w.Header().Set("Content-Type", "application/json")
  138. fmt.Fprint(w, `{"tag_name":"v2.5.0"}`)
  139. default:
  140. t.Errorf("path inesperado: %s", r.URL.Path)
  141. }
  142. })
  143. u := New(root, "owner/netpulse", "", "2.0.0", nil)
  144. st1 := u.Check(context.Background())
  145. if len(st1.Commits) != 3 {
  146. t.Fatalf("commits primer check: %+v", st1.Commits)
  147. }
  148. st2 := u.Check(context.Background())
  149. if compareCalls != 1 {
  150. t.Fatalf("el compare se reconsultó con el mismo par: %d llamadas", compareCalls)
  151. }
  152. if len(st2.Commits) != 3 {
  153. t.Fatalf("commits cacheados: %+v", st2.Commits)
  154. }
  155. }
  156. // #1245: BuildCommit local puede tener 8+ chars (git rev-parse --short) vs
  157. // latest de 7 del API: el mismo commit no debe marcar update disponible.
  158. func TestCheckRollingNormalizaLongitudSHA(t *testing.T) {
  159. old := BuildCommit
  160. BuildCommit = "e19bec93"
  161. defer func() { BuildCommit = old }()
  162. root := t.TempDir()
  163. writeDeployScript(t, root)
  164. writeGitHead(t, root)
  165. withAPI(t, func(w http.ResponseWriter, r *http.Request) {
  166. switch r.URL.Path {
  167. case "/repos/owner/netpulse/commits/main":
  168. w.Header().Set("Content-Type", "application/json")
  169. fmt.Fprint(w, `{"sha":"e19bec935566778899aabbccddeeff00112233","commit":{"message":"feat: same commit"}}`)
  170. case "/repos/owner/netpulse/releases":
  171. w.Header().Set("Content-Type", "application/json")
  172. fmt.Fprint(w, `[]`)
  173. case "/repos/owner/netpulse/releases/latest":
  174. w.Header().Set("Content-Type", "application/json")
  175. fmt.Fprint(w, `{"tag_name":"v2.5.0"}`)
  176. default:
  177. if strings.Contains(r.URL.Path, "/compare/") {
  178. fmt.Fprint(w, `{"commits":[]}`)
  179. return
  180. }
  181. t.Errorf("path inesperado: %s", r.URL.Path)
  182. }
  183. })
  184. u := New(root, "owner/netpulse", "", "2.0.0", nil)
  185. st := u.Check(context.Background())
  186. if st.Error != nil {
  187. t.Fatalf("error: %v", *st.Error)
  188. }
  189. if st.UpdateAvailable {
  190. t.Fatalf("mismo commit (8 vs 7 chars) no debe marcar update: current=%v latest=%v", st.Current, *st.Latest)
  191. }
  192. }