orchard
mirrorEvery site I host, in one repo, along with the Cloudflare Tunnel and Caddy that front them. It's all Go, Vite, and SQLite, and it runs on a desktop at home with nothing listening on an inbound port.
blogbuncaddycloudflare-tunneldockergogolanghomelabhtml-templatemonorepoself-hostedseosqlitestatic-sitetypstuptime-monitoringviteweb-analytics
1package main
2
3import (
4 "io/fs"
5 "testing"
6)
7
8// Every /media/ URL the logs have actually seen, so a rename in content/images
9// that breaks one of these fails here rather than quietly going back to 404.
10func TestWagtailMediaURLsResolve(t *testing.T) {
11 images, err := fs.Sub(contentFS, "content/images")
12 if err != nil {
13 t.Fatal(err)
14 }
15 idx := newMediaIndex(images)
16
17 for _, tc := range []struct{ in, want string }{
18 {"alpine-linux.2e16d0ba.fill-1920x1080.format-webp.webp", "alpine-linux.webp"},
19 {"alpine-linux.2e16d0ba.fill-640x480.format-webp.webp", "alpine-linux.webp"},
20 {"black-logo.2e16d0ba.fill-640x480.format-webp.webp", "black-logo.webp"},
21 {"borg-logo.2e16d0ba.fill-1920x1080.format-webp.webp", "borg-logo.webp"},
22 {"caddyserver.com.2e16d0ba.fill-1920x1080.format-webp.webp", "caddyserver.com.webp"},
23 {"editorconfig.org.2e16d0ba.fill-1920x1080.format-webp.webp", "editorconfig.org.webp"},
24 {"codemirror_website.2e16d0ba.fill-640x480.format-webp.webp", "codemirror-website.webp"},
25 {"dark-mode-light-mode.2e16d0ba.fill-640x480.format-webp.webp", "dark-mode-light-mode.webp"},
26 {"openssh_logo.2e16d0ba.fill-640x480.format-webp.webp", "openssh-logo.webp"},
27 {"scrapy-logo-banner.2e16d0ba.fill-640x480.format-webp.webp", "scrapy-logo-banner.webp"},
28 {"timelite-pwa-icon.max-1920x1080.format-webp.webp", "timelite-pwa-icon.webp"},
29 {"timelite-pwa-screenshot.max-1920x1080.format-webp.webp", "timelite-pwa-screenshot.webp"},
30 {"new-tab-extension.max-1920x1080.format-webp.webp", "new-tab-extension.webp"},
31 {"new-tab-extension-complete.max-1920x1080.format-webp.webp", "new-tab-extension-complete.webp"},
32 // Wagtail truncated the stem to twenty characters.
33 {"django_dockerfile_edi.2e16d0ba.fill-640x480.format-webp.webp", "django-dockerfile-edited.webp"},
34 {"lighthouse-pwa-chec.2e16d0ba.fill-1920x1080.format-webp.webp", "lighthouse-pwa-check.webp"},
35 {"lighthouse-pwa-check.2e16d0ba.fill-640x480.format-webp.webp", "lighthouse-pwa-check.webp"},
36 // Truncated a misspelling, so only the alias table gets these.
37 {"postgrseql_row_coun.2e16d0ba.fill-1920x1080.format-webp.webp", "postgresql-row-count-output.webp"},
38 {"postgrseql_row_count_.2e16d0ba.fill-640x480.format-webp.webp", "postgresql-row-count-output.webp"},
39 } {
40 got, ok := idx.resolve(tc.in)
41 if !ok || got != tc.want {
42 t.Errorf("resolve(%q) = %q,%v want %q", tc.in, got, ok, tc.want)
43 }
44 }
45}
46
47// An unresolvable name must 404 rather than redirect to whatever sorted first.
48func TestUnknownMediaIsNotGuessed(t *testing.T) {
49 images, err := fs.Sub(contentFS, "content/images")
50 if err != nil {
51 t.Fatal(err)
52 }
53 idx := newMediaIndex(images)
54 for _, in := range []string{
55 "nothing-like-this.2e16d0ba.fill-640x480.format-webp.webp",
56 "a.2e16d0ba.fill-640x480.format-webp.webp",
57 "",
58 } {
59 if got, ok := idx.resolve(in); ok {
60 t.Errorf("resolve(%q) = %q, want no match", in, got)
61 }
62 }
63}
64
65func TestMediaTargets(t *testing.T) {
66 images, err := fs.Sub(contentFS, "content/images")
67 if err != nil {
68 t.Fatal(err)
69 }
70 idx := newMediaIndex(images)
71
72 for _, tc := range []struct {
73 path string
74 want string
75 }{
76 {"/media/images/openssh_logo.2e16d0ba.fill-640x480.format-webp.webp", "/content/images/openssh-logo.webp"},
77 {"/media/avatar_images/avatar_0da51063-cf05-4913-9b84-b1ce72bb2dfc_Avatar.webp", "/content/images/avatar.webp"},
78 // Hashed with nothing recoverable in the name.
79 {"/media/og_images/abc179d186894844a64d14ce317cc9ae.png", ""},
80 {"/media/images/does-not-exist.2e16d0ba.fill-640x480.format-webp.webp", ""},
81 {"/media/", ""},
82 } {
83 got, ok := idx.target(tc.path)
84 if tc.want == "" {
85 if ok {
86 t.Errorf("target(%q) = %q, want no match", tc.path, got)
87 }
88 continue
89 }
90 if !ok || got != tc.want {
91 t.Errorf("target(%q) = %q,%v want %q", tc.path, got, ok, tc.want)
92 }
93 }
94}