package equipotest import ( "bytes" "debug/buildinfo" "go/version" "net/http" "os" "os/exec" "path/filepath" "regexp" "strings" "testing" "testing/fstest" "time" "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/testci" "amadan.net/rastrillo/rastrillo" "amadan.net/rastrillo/rastrillo/mail" "amadan.net/rastrillo/rastrillo/ui" ) func TestRastrilloUpgradeContract(t *testing.T) { t.Run("module", func(t *testing.T) { mod, err := os.ReadFile("../../go.mod") if err != nil { t.Fatal(err) } if !strings.Contains(string(mod), "\ngo 1.26.0\n") { t.Error("module Go floor must be 1.26.0") } bin := filepath.Join(t.TempDir(), "equipo") // From this directory, not the root: go test records a child's // working directory, and the root's mtime moves on every CI job. cmd := exec.Command("go", "build", "-o", bin, "../../cmd/equipo") cmd.Env = append(os.Environ(), "CGO_ENABLED=0") testci.GoCommand(t, cmd) if out, err := cmd.CombinedOutput(); err != nil { t.Fatalf("build production binary: %v\n%s", err, out) } info, err := buildinfo.ReadFile(bin) if err != nil { t.Fatal(err) } if version.Compare(info.GoVersion, "go1.26.0") < 0 { t.Errorf("linked Go version = %s, want >= go1.26.0", info.GoVersion) } for _, dep := range info.Deps { if dep.Path == "amadan.net/rastrillo/rastrillo" { if dep.Version != "v0.45.0" || dep.Replace != nil { t.Errorf("linked Rastrillo = %+v, want v0.45.0 without replacement", dep) } return } } t.Fatal("production binary does not link Rastrillo") }) t.Run("default English and assets", func(t *testing.T) { d, logger := foundationDB(t) mux, err := equipo.App(d, testConfig(testOrigin), mail.Logged(logger), logger) if err != nil { t.Fatal(err) } opts := rastrillo.Options{Logger: logger} equipo.Configure(&opts, mux, time.Now()) t.Cleanup(func() { opts.Background.Stop() }) h, closeAll, err := rastrillo.Handler(opts) if err != nil { t.Fatalf("default-English Handler boot: %v", err) } t.Cleanup(func() { if err := closeAll(); err != nil { t.Error(err) } }) assets, ok := ui.VendoredAssets("day") if !ok { t.Fatal("missing day assets") } for _, name := range []string{"live.js", "light.js", "anchor.js"} { if len(assets[name]) == 0 { t.Errorf("missing framework asset %s", name) } } library := fstest.MapFS{} // served minified since v0.31: compare with what Assets serves for name, want := range assets { library["static/"+name] = &fstest.MapFile{Data: want} } for name := range assets { res, want := get(t, h, "/static/"+name), get(t, rastrillo.NewAssets(library).Handler(), "/static/"+name) if res.Code != http.StatusOK || !bytes.Equal(res.Body.Bytes(), want.Body.Bytes()) { t.Errorf("served static/%s: status %d or bytes differ", name, res.Code) } } res := get(t, h, "/signin") if res.Code != http.StatusOK { t.Fatalf("English sign-in: %d", res.Code) } }) t.Run("sidebar live script", func(t *testing.T) { f, c := pageOwner(t) assertLiveAfterBusy(t, pageBody(t, c, f.server.URL, "/conversation")) }) } func assertLiveAfterBusy(t *testing.T, body string) { t.Helper() scripts := regexp.MustCompile(`]*src="([^"]+)"[^>]*>`).FindAllStringSubmatch(body, -1) for i, script := range scripts { if strings.Contains(script[1], "/busy.") && i+1 < len(scripts) && strings.Contains(scripts[i+1][1], "/live.") { return } } t.Error("layout must link live.js immediately after busy.js") }