oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package equipotest
2
3import (
4 "bytes"
5 "debug/buildinfo"
6 "go/version"
7 "net/http"
8 "os"
9 "os/exec"
10 "path/filepath"
11 "regexp"
12 "strings"
13 "testing"
14 "testing/fstest"
15 "time"
16
17 "amadan.net/oficina/equipo/internal/equipo"
18 "amadan.net/oficina/equipo/internal/testci"
19 "amadan.net/rastrillo/rastrillo"
20 "amadan.net/rastrillo/rastrillo/mail"
21 "amadan.net/rastrillo/rastrillo/ui"
22)
23
24func TestRastrilloUpgradeContract(t *testing.T) {
25 t.Run("module", func(t *testing.T) {
26 mod, err := os.ReadFile("../../go.mod")
27 if err != nil {
28 t.Fatal(err)
29 }
30 if !strings.Contains(string(mod), "\ngo 1.26.0\n") {
31 t.Error("module Go floor must be 1.26.0")
32 }
33 bin := filepath.Join(t.TempDir(), "equipo")
34 // From this directory, not the root: go test records a child's
35 // working directory, and the root's mtime moves on every CI job.
36 cmd := exec.Command("go", "build", "-o", bin, "../../cmd/equipo")
37 cmd.Env = append(os.Environ(), "CGO_ENABLED=0")
38 testci.GoCommand(t, cmd)
39 if out, err := cmd.CombinedOutput(); err != nil {
40 t.Fatalf("build production binary: %v\n%s", err, out)
41 }
42 info, err := buildinfo.ReadFile(bin)
43 if err != nil {
44 t.Fatal(err)
45 }
46 if version.Compare(info.GoVersion, "go1.26.0") < 0 {
47 t.Errorf("linked Go version = %s, want >= go1.26.0", info.GoVersion)
48 }
49 for _, dep := range info.Deps {
50 if dep.Path == "amadan.net/rastrillo/rastrillo" {
51 if dep.Version != "v0.45.0" || dep.Replace != nil {
52 t.Errorf("linked Rastrillo = %+v, want v0.45.0 without replacement", dep)
53 }
54 return
55 }
56 }
57 t.Fatal("production binary does not link Rastrillo")
58 })
59 t.Run("default English and assets", func(t *testing.T) {
60 d, logger := foundationDB(t)
61 mux, err := equipo.App(d, testConfig(testOrigin), mail.Logged(logger), logger)
62 if err != nil {
63 t.Fatal(err)
64 }
65 opts := rastrillo.Options{Logger: logger}
66 equipo.Configure(&opts, mux, time.Now())
67 t.Cleanup(func() { opts.Background.Stop() })
68 h, closeAll, err := rastrillo.Handler(opts)
69 if err != nil {
70 t.Fatalf("default-English Handler boot: %v", err)
71 }
72 t.Cleanup(func() {
73 if err := closeAll(); err != nil {
74 t.Error(err)
75 }
76 })
77 assets, ok := ui.VendoredAssets("day")
78 if !ok {
79 t.Fatal("missing day assets")
80 }
81 for _, name := range []string{"live.js", "light.js", "anchor.js"} {
82 if len(assets[name]) == 0 {
83 t.Errorf("missing framework asset %s", name)
84 }
85 }
86 library := fstest.MapFS{} // served minified since v0.31: compare with what Assets serves
87 for name, want := range assets {
88 library["static/"+name] = &fstest.MapFile{Data: want}
89 }
90 for name := range assets {
91 res, want := get(t, h, "/static/"+name), get(t, rastrillo.NewAssets(library).Handler(), "/static/"+name)
92 if res.Code != http.StatusOK || !bytes.Equal(res.Body.Bytes(), want.Body.Bytes()) {
93 t.Errorf("served static/%s: status %d or bytes differ", name, res.Code)
94 }
95 }
96 res := get(t, h, "/signin")
97 if res.Code != http.StatusOK {
98 t.Fatalf("English sign-in: %d", res.Code)
99 }
100 })
101 t.Run("sidebar live script", func(t *testing.T) {
102 f, c := pageOwner(t)
103 assertLiveAfterBusy(t, pageBody(t, c, f.server.URL, "/conversation"))
104 })
105}
106
107func assertLiveAfterBusy(t *testing.T, body string) {
108 t.Helper()
109 scripts := regexp.MustCompile(`<script\b[^>]*src="([^"]+)"[^>]*>`).FindAllStringSubmatch(body, -1)
110 for i, script := range scripts {
111 if strings.Contains(script[1], "/busy.") && i+1 < len(scripts) && strings.Contains(scripts[i+1][1], "/live.") {
112 return
113 }
114 }
115 t.Error("layout must link live.js immediately after busy.js")
116}
117