| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- // updater_test.go — check contra API GitHub simulada (httptest) y shape del
- // estado; apply con script sintético (STEP:*/exit).
- package updater
- import (
- "context"
- "fmt"
- "net/http"
- "net/http/httptest"
- "os"
- "os/exec"
- "path/filepath"
- "strings"
- "sync"
- "sync/atomic"
- "testing"
- "time"
- )
- func gitAvailable() bool { _, err := exec.LookPath("git"); return err == nil }
- func withAPI(t *testing.T, h http.HandlerFunc) {
- t.Helper()
- srv := httptest.NewServer(h)
- t.Cleanup(srv.Close)
- old := APIBase
- APIBase = srv.URL
- t.Cleanup(func() { APIBase = old })
- // El check de conectividad del readiness usa probeURL (web pública de
- // GitHub, #956): apuntarlo también al httptest para tests herméticos.
- oldProbe := probeURL
- probeURL = srv.URL
- t.Cleanup(func() { probeURL = oldProbe })
- }
- func TestCheckUpdateAvailable(t *testing.T) {
- withAPI(t, func(w http.ResponseWriter, r *http.Request) {
- switch r.URL.Path {
- case "/repos/owner/netpulse/commits/main":
- w.Header().Set("Content-Type", "application/json")
- fmt.Fprint(w, `{"sha":"abc1234deadbeef","commit":{"message":"feat: algo\n\nbody"}}`)
- case "/repos/owner/netpulse/releases":
- w.Header().Set("Content-Type", "application/json")
- fmt.Fprint(w, `[]`)
- case "/repos/owner/netpulse/releases/latest":
- w.Header().Set("Content-Type", "application/json")
- fmt.Fprint(w, `{"tag_name":"v2.5.0"}`)
- default:
- // Compare del changelog (issue #490): cualquier path /compare/.
- if strings.Contains(r.URL.Path, "/compare/") {
- w.Header().Set("Content-Type", "application/json")
- fmt.Fprint(w, `{"commits":[{"sha":"abc1234deadbeef","commit":{"message":"feat: algo"}}]}`)
- return
- }
- t.Errorf("path: %s", r.URL.Path)
- }
- })
- // repoRoot con git válido (commit distinto) para updateAvailable.
- // deploy/update.sh → modo rolling (compara contra main HEAD).
- root := t.TempDir()
- writeDeployScript(t, root)
- writeGitHead(t, root)
- u := New(root, "owner/netpulse", "", "2.0.0", nil)
- st := u.Check(context.Background())
- if st.Error != nil {
- t.Fatalf("error: %v", *st.Error)
- }
- if st.Latest == nil || *st.Latest != "abc1234" {
- t.Fatalf("latest: %v", st.Latest)
- }
- if st.LatestMsg == nil || *st.LatestMsg != "feat: algo" {
- t.Fatalf("latestMsg: %v", st.LatestMsg)
- }
- // Issue #280: el body del commit llega como changelog del asistente.
- if st.LatestBody == nil || *st.LatestBody != "body" {
- t.Fatalf("latestBody: %v", st.LatestBody)
- }
- if !st.UpdateAvailable {
- t.Fatalf("debería haber update: %+v", st)
- }
- if !st.CanApply || st.Mode != "rolling" {
- t.Fatalf("debería ser rolling/canApply: canApply=%v mode=%s", st.CanApply, st.Mode)
- }
- if st.Updating != false || st.HasToken || st.Repo != "owner/netpulse" {
- t.Fatalf("shape: %+v", st)
- }
- if st.LastCheck == nil {
- t.Fatal("lastCheck nil")
- }
- }
- // Issue #404: si el body del commit está vacío, el backend debe buscar las
- // notas del release asociado al commit y devolverlas como changelog.
- func TestCheckUsesReleaseBodyWhenCommitBodyEmpty(t *testing.T) {
- withAPI(t, func(w http.ResponseWriter, r *http.Request) {
- switch r.URL.Path {
- case "/repos/owner/netpulse/commits/main":
- w.Header().Set("Content-Type", "application/json")
- fmt.Fprint(w, `{"sha":"deadbeefabc1234","commit":{"message":"chore(release): v2.5.0"}}`)
- case "/repos/owner/netpulse/releases":
- w.Header().Set("Content-Type", "application/json")
- fmt.Fprint(w, `[{"body":"Release notes line 1\nRelease notes line 2","target_commitish":"deadbeefabc1234"}]`)
- case "/repos/owner/netpulse/releases/latest":
- w.Header().Set("Content-Type", "application/json")
- fmt.Fprint(w, `{"tag_name":"v2.5.0"}`)
- default:
- // Compare del changelog (issue #490).
- if strings.Contains(r.URL.Path, "/compare/") {
- fmt.Fprint(w, `{"commits":[]}`)
- return
- }
- t.Errorf("path: %s", r.URL.Path)
- }
- })
- root := t.TempDir()
- writeDeployScript(t, root)
- writeGitHead(t, root)
- u := New(root, "owner/netpulse", "", "2.0.0", nil)
- st := u.Check(context.Background())
- if st.Error != nil {
- t.Fatalf("error: %v", *st.Error)
- }
- if st.LatestBody == nil || *st.LatestBody != "Release notes line 1\nRelease notes line 2" {
- t.Fatalf("latestBody: %v", st.LatestBody)
- }
- }
- func TestCheckNoToken(t *testing.T) {
- withAPI(t, func(w http.ResponseWriter, r *http.Request) {
- w.WriteHeader(http.StatusNotFound)
- })
- // Sin deploy/update.sh → modo estable. El 404 llega tanto a /releases/latest
- // como a /commits/main; el error es el mismo.
- u := New(t.TempDir(), "owner/netpulse", "", "2.0.0", nil)
- st := u.Check(context.Background())
- if st.Error == nil || *st.Error != "no_token" {
- t.Fatalf("error: %v", st.Error)
- }
- if st.CanApply || st.Mode != "stable" {
- t.Fatalf("debería ser stable/!canApply: canApply=%v mode=%s", st.CanApply, st.Mode)
- }
- }
- func TestCheckMismaVersion(t *testing.T) {
- root := t.TempDir()
- writeDeployScript(t, root) // modo rolling
- short := writeGitHead(t, root)
- withAPI(t, func(w http.ResponseWriter, r *http.Request) {
- fmt.Fprintf(w, `{"sha":"%sffffffffff","commit":{"message":"x"}}`, short)
- })
- u := New(root, "owner/netpulse", "", "2.0.0", nil)
- st := u.Check(context.Background())
- if st.UpdateAvailable {
- t.Fatal("no debería haber update")
- }
- }
- // writeGitHead crea un repo git mínimo con un commit y devuelve su short SHA.
- func writeGitHead(t *testing.T, root string) string {
- t.Helper()
- if !gitAvailable() {
- t.Skip("git no disponible")
- }
- run := func(args ...string) {
- cmd := exec.Command("git", append([]string{"-C", root}, args...)...)
- if out, err := cmd.CombinedOutput(); err != nil {
- t.Fatalf("git %v: %v %s", args, err, out)
- }
- }
- run("init", "-q")
- run("config", "user.email", "t@t")
- run("config", "user.name", "t")
- if err := os.WriteFile(filepath.Join(root, "f"), []byte("x"), 0o644); err != nil {
- t.Fatal(err)
- }
- run("add", ".")
- run("commit", "-qm", "init")
- out, err := exec.Command("git", "-C", root, "rev-parse", "--short", "HEAD").Output()
- if err != nil {
- t.Fatal(err)
- }
- return strings.TrimSpace(string(out))
- }
- // writeDeployScript crea deploy/update.sh en root (marca el layout como
- // git/rolling: el updater detecta que puede auto-aplicar).
- func writeDeployScript(t *testing.T, root string) {
- t.Helper()
- if err := os.MkdirAll(filepath.Join(root, "deploy"), 0o755); err != nil {
- t.Fatal(err)
- }
- if err := os.WriteFile(filepath.Join(root, "deploy", "update.sh"), []byte("#!/bin/bash\ntrue\n"), 0o755); err != nil {
- t.Fatal(err)
- }
- }
- func TestApplyYaEnCursoYScript(t *testing.T) {
- if !gitAvailable() {
- t.Skip("git no disponible")
- }
- root := t.TempDir()
- if err := os.MkdirAll(filepath.Join(root, "deploy"), 0o755); err != nil {
- t.Fatal(err)
- }
- script := "#!/bin/bash\necho STEP:backend\nsleep 0.1\necho STEP:done-steps\ntrue\n"
- if err := os.WriteFile(filepath.Join(root, "deploy", "update.sh"), []byte(script), 0o755); err != nil {
- t.Fatal(err)
- }
- u := New(root, "owner/netpulse", "", "2.0.0", nil)
- if !u.Apply() {
- t.Fatal("apply debería arrancar")
- }
- // Segundo apply concurrente → 409 already_updating
- if u.Apply() {
- t.Fatal("apply duplicado debería ser false")
- }
- deadline := time.Now().Add(30 * time.Second)
- for {
- st := u.Status()
- if m, ok := st.Updating.(progress); ok && m.Step == "done" {
- break
- }
- if time.Now().After(deadline) {
- t.Fatalf("timeout esperando done: %+v", st)
- }
- time.Sleep(50 * time.Millisecond)
- }
- st := u.Status()
- if st.LastLog == nil || *st.LastLog == "" {
- t.Fatalf("lastLog: %+v", st)
- }
- }
- func TestStableModeUpdateAvailable(t *testing.T) {
- withAPI(t, func(w http.ResponseWriter, r *http.Request) {
- // Compare del changelog (issue #490).
- if strings.Contains(r.URL.Path, "/compare/") {
- fmt.Fprint(w, `{"commits":[]}`)
- return
- }
- if r.URL.Path != "/repos/owner/netpulse/releases/latest" {
- t.Errorf("path: %s (modo estable debe consultar releases/latest)", r.URL.Path)
- }
- w.Header().Set("Content-Type", "application/json")
- fmt.Fprint(w, `{"tag_name":"v2.7.3","name":"v2.7.3"}`)
- })
- // Sin deploy/update.sh → modo estable. Versión embebida 2.7.2 < 2.7.3.
- u := New(t.TempDir(), "owner/netpulse", "", "2.7.2", nil)
- st := u.Check(context.Background())
- if st.Error != nil {
- t.Fatalf("error: %v", *st.Error)
- }
- if !st.UpdateAvailable {
- t.Fatal("debería haber update (2.7.2 < 2.7.3)")
- }
- if st.Latest == nil || *st.Latest != "v2.7.3" {
- t.Fatalf("latest: %v", st.Latest)
- }
- if st.Current != "2.7.2" {
- t.Fatalf("current: %s", st.Current)
- }
- if st.CanApply || st.Mode != "stable" {
- t.Fatalf("debería ser stable/!canApply: canApply=%v mode=%s", st.CanApply, st.Mode)
- }
- }
- func TestStableModeNoUpdateWhenSameVersion(t *testing.T) {
- withAPI(t, func(w http.ResponseWriter, r *http.Request) {
- fmt.Fprint(w, `{"tag_name":"v2.7.2","name":"v2.7.2"}`)
- })
- u := New(t.TempDir(), "owner/netpulse", "", "2.7.2", nil)
- st := u.Check(context.Background())
- if st.UpdateAvailable {
- t.Fatal("no debería haber update (misma versión)")
- }
- }
- func TestStableModeNoUpdateWhenOlder(t *testing.T) {
- withAPI(t, func(w http.ResponseWriter, r *http.Request) {
- fmt.Fprint(w, `{"tag_name":"v2.7.0","name":"v2.7.0"}`)
- })
- u := New(t.TempDir(), "owner/netpulse", "", "2.7.2", nil)
- st := u.Check(context.Background())
- if st.UpdateAvailable {
- t.Fatal("no debería haber update (2.7.0 < 2.7.2)")
- }
- }
- func TestCompareSemver(t *testing.T) {
- cases := []struct {
- a, b string
- want int
- }{
- {"2.7.2", "2.7.2", 0},
- {"v2.7.2", "2.7.2", 0},
- {"v2.7.3", "v2.7.2", 1},
- {"2.8.0", "2.7.9", 1},
- {"3.0.0", "2.9.9", 1},
- {"2.7.1", "2.7.2", -1},
- {"2.6.9", "2.7.0", -1},
- {"v2.7.2-rc1", "v2.7.2", 0}, // pre-release suffix descartado
- {"1.0.0", "1.0", 0}, // minor faltante = 0
- }
- for _, c := range cases {
- got := compareSemver(c.a, c.b)
- // normalizar a -1/0/1 para comparar
- if (c.want == 0 && got != 0) || (c.want > 0 && got <= 0) || (c.want < 0 && got >= 0) {
- t.Errorf("compareSemver(%q, %q) = %d, want sign %d", c.a, c.b, got, c.want)
- }
- }
- }
- func TestCanApplyDetection(t *testing.T) {
- // Sin deploy/update.sh → stable, !canApply
- u1 := New(t.TempDir(), "owner/netpulse", "", "1.0.0", nil)
- if u1.CanApply() || u1.mode != "stable" {
- t.Fatal("sin deploy/update.sh debería ser stable/!canApply")
- }
- // Con deploy/update.sh → rolling, canApply
- root := t.TempDir()
- writeDeployScript(t, root)
- u2 := New(root, "owner/netpulse", "", "1.0.0", nil)
- if !u2.CanApply() || u2.mode != "rolling" {
- t.Fatal("con deploy/update.sh debería ser rolling/canApply")
- }
- }
- // Issue #280: pesos por paso, PROGRESS explícito (clamp) y shape del status.
- func TestProgressWeightsAndClamp(t *testing.T) {
- u := New(t.TempDir(), "owner/netpulse", "", "2.0.0", nil)
- pctOf := func() int {
- t.Helper()
- m, ok := u.Status().Updating.(progress)
- if !ok {
- t.Fatal("debería estar updating")
- }
- return m.Progress
- }
- u.appendLog("STEP:start\n", true)
- if p := pctOf(); p != stepWeight["start"] {
- t.Fatalf("start: %d", p)
- }
- u.appendLog("STEP:fetch\n", true)
- if p := pctOf(); p != stepWeight["fetch"] {
- t.Fatalf("fetch: %d", p)
- }
- u.appendLog("STEP:download\n", true)
- if p := pctOf(); p != stepWeight["download"] {
- t.Fatalf("download: %d", p)
- }
- // PROGRESS por debajo del peso del paso no retrocede.
- u.appendLog("PROGRESS:5\n", true)
- if p := pctOf(); p != stepWeight["download"] {
- t.Fatalf("progreso no puede bajar del peso: %d", p)
- }
- // PROGRESS dentro del paso avanza.
- u.appendLog("PROGRESS:55\n", true)
- if p := pctOf(); p != 55 {
- t.Fatalf("55: %d", p)
- }
- // PROGRESS absurdo se recorta a 99 (100 solo con done).
- u.appendLog("PROGRESS:150\n", true)
- if p := pctOf(); p != 99 {
- t.Fatalf("150 → 99: %d", p)
- }
- // done siempre 100.
- u.appendLog("STEP:done\n", true)
- if p := pctOf(); p != 100 {
- t.Fatalf("done: %d", p)
- }
- // Paso legado (script viejo) sin PROGRESS: peso por defecto del mapa.
- u.appendLog("STEP:binary\n", true)
- if p := pctOf(); p != stepWeight["binary"] {
- t.Fatalf("binary legado: %d", p)
- }
- }
- // Issue #280: el broadcaster notifica cambios de paso y el cancel retira.
- func TestSubscribeBroadcast(t *testing.T) {
- u := New(t.TempDir(), "owner/netpulse", "", "2.0.0", nil)
- ch, cancel := u.Subscribe()
- defer cancel()
- u.appendLog("STEP:download\n", true)
- select {
- case st := <-ch:
- m, ok := st.Updating.(progress)
- if !ok || m.Step != "download" || m.Progress != stepWeight["download"] {
- t.Fatalf("evento: %+v", st.Updating)
- }
- case <-time.After(2 * time.Second):
- t.Fatal("sin evento tras STEP")
- }
- // Un segundo evento (cambio de pct) y luego la baja no bloquea.
- u.appendLog("PROGRESS:50\n", true)
- select {
- case st := <-ch:
- m, _ := st.Updating.(progress)
- if m.Progress != 50 {
- t.Fatalf("progreso: %d", m.Progress)
- }
- case <-time.After(2 * time.Second):
- t.Fatal("sin evento tras PROGRESS")
- }
- cancel()
- // Tras cancel, broadcast no entrega ni bloquea.
- u.appendLog("STEP:verify\n", true)
- select {
- case <-ch:
- t.Fatal("no debería llegar tras cancel")
- default:
- }
- }
- func TestCheckSerializedUnderConcurrency(t *testing.T) {
- var inFlight, maxInFlight atomic.Int32
- withAPI(t, func(w http.ResponseWriter, r *http.Request) {
- switch {
- case r.URL.Path == "/repos/owner/netpulse/commits/main":
- n := inFlight.Add(1)
- for {
- m := maxInFlight.Load()
- if n <= m || maxInFlight.CompareAndSwap(m, n) {
- break
- }
- }
- defer inFlight.Add(-1)
- time.Sleep(50 * time.Millisecond)
- w.Header().Set("Content-Type", "application/json")
- fmt.Fprint(w, `{"sha":"abc1234deadbeef","commit":{"message":"feat: algo"}}`)
- case r.URL.Path == "/repos/owner/netpulse/releases/latest":
- w.Header().Set("Content-Type", "application/json")
- fmt.Fprint(w, `{"tag_name":"v2.5.0"}`)
- case r.URL.Path == "/repos/owner/netpulse/releases":
- w.Header().Set("Content-Type", "application/json")
- fmt.Fprint(w, `[]`)
- case strings.Contains(r.URL.Path, "/compare/"):
- w.Header().Set("Content-Type", "application/json")
- fmt.Fprint(w, `{"commits":[]}`)
- default:
- t.Errorf("path: %s", r.URL.Path)
- }
- })
- root := t.TempDir()
- writeDeployScript(t, root)
- writeGitHead(t, root)
- u := New(root, "owner/netpulse", "", "2.0.0", nil)
- var wg sync.WaitGroup
- for i := 0; i < 2; i++ {
- wg.Add(1)
- go func() {
- defer wg.Done()
- u.Check(context.Background())
- }()
- }
- wg.Wait()
- if got := maxInFlight.Load(); got != 1 {
- t.Fatalf("commits/main max in flight=%d want 1", got)
- }
- }
|