oficina / fichas Public

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

Plain git — no account needed to clone.

Download

Download this file

1package fichastest
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "regexp"
7 "strings"
8 "testing"
9
10 fichas "amadan.net/oficina/fichas/internal/fichas"
11)
12
13// hashedStylesheet is the fingerprinted URL shape the index page
14// links: /static/tokens.<16 hex>.css.
15var hashedStylesheet = regexp.MustCompile(`/static/tokens\.[0-9a-f]{16}\.css`)
16
17func TestIndexRenders(t *testing.T) {
18 rec := get(t, newApp(t), "/")
19 if rec.Code != http.StatusOK {
20 t.Fatalf("GET /: status %d, want 200", rec.Code)
21 }
22 body := rec.Body.String()
23 if !strings.Contains(body, "Customers") {
24 t.Errorf("GET /: page is missing its heading:\n%s", body)
25 }
26 // An empty state that says what the screen is FOR, not merely that
27 // it is empty (AGENTS.md).
28 if !strings.Contains(body, "No customers yet") {
29 t.Errorf("GET /: no empty state on a fresh instance:\n%s", body)
30 }
31}
32
33func TestIndexLinksFingerprintedStylesheet(t *testing.T) {
34 h := newApp(t)
35 href := hashedStylesheet.FindString(get(t, h, "/").Body.String())
36 if href == "" {
37 t.Fatal("index page links no fingerprinted stylesheet")
38 }
39 rec := get(t, h, href)
40 if rec.Code != http.StatusOK {
41 t.Fatalf("GET %s: status %d, want 200", href, rec.Code)
42 }
43 if got := rec.Header().Get("Cache-Control"); got != "public, max-age=31536000, immutable" {
44 t.Errorf("GET %s: Cache-Control = %q, want immutable year", href, got)
45 }
46}
47
48// The bare name keeps working, just never long-cached — so a changed
49// file always shows on an ordinary reload.
50func TestBareAssetNameStaysFresh(t *testing.T) {
51 rec := get(t, newApp(t), "/static/tokens.css")
52 if rec.Code != http.StatusOK {
53 t.Fatalf("GET /static/tokens.css: status %d, want 200", rec.Code)
54 }
55 if got := rec.Header().Get("Cache-Control"); got != "no-cache" {
56 t.Errorf("Cache-Control = %q, want no-cache", got)
57 }
58}
59
60// ErrorPage is what main.go points Options.ErrorPage at (and the seam
61// to wire into Ctx.ErrorPage as handlers grow); called directly, it
62// pins that a panic's page is the framework's error-page partial, not
63// a stray net/http default.
64func TestErrorPageRendersFrameworkPartial(t *testing.T) {
65 rec := httptest.NewRecorder()
66 fichas.ErrorPage(rec, httptest.NewRequest(http.MethodGet, "/", nil), http.StatusInternalServerError, "abc123")
67 if rec.Code != http.StatusInternalServerError {
68 t.Fatalf("ErrorPage: status %d, want %d", rec.Code, http.StatusInternalServerError)
69 }
70 body := rec.Body.String()
71 if !strings.Contains(body, "Something went wrong on our side") {
72 t.Errorf("ErrorPage body missing the 500 title:\n%s", body)
73 }
74 if !strings.Contains(body, "abc123") {
75 t.Errorf("ErrorPage body missing the ref:\n%s", body)
76 }
77}
78