vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
| 1 | package web |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | // The type decides whether a browser treats a blob as a document, so only the |
| 6 | // image extensions are named and everything else stays text or a model type no |
| 7 | // browser renders. |
| 8 | func TestRawContentTypeAllowsOnlyMedia(t *testing.T) { |
| 9 | t.Parallel() |
| 10 | cases := []struct{ path, want string }{ |
| 11 | {"shot.png", "image/png"}, |
| 12 | {"SHOT.PNG", "image/png"}, |
| 13 | {"photo.jpeg", "image/jpeg"}, |
| 14 | {"photo.jpg", "image/jpeg"}, |
| 15 | {"anim.gif", "image/gif"}, |
| 16 | {"shot.webp", "image/webp"}, |
| 17 | {"shot.avif", "image/avif"}, |
| 18 | {"shot.bmp", "image/bmp"}, |
| 19 | {"favicon.ico", "image/x-icon"}, |
| 20 | {"logo.svg", "image/svg+xml"}, |
| 21 | {"part.stl", "model/stl"}, |
| 22 | {"part.obj", "model/obj"}, |
| 23 | {"scene.gltf", "model/gltf+json"}, |
| 24 | {"scene.glb", "model/gltf-binary"}, |
| 25 | {"part.3mf", "model/3mf"}, |
| 26 | {"page.html", "text/plain; charset=utf-8"}, |
| 27 | {"notes.txt", "text/plain; charset=utf-8"}, |
| 28 | {"README", "text/plain; charset=utf-8"}, |
| 29 | {"script.js", "text/plain; charset=utf-8"}, |
| 30 | } |
| 31 | for _, tc := range cases { |
| 32 | if got := rawContentType(tc.path); got != tc.want { |
| 33 | t.Errorf("rawContentType(%q) = %q, want %q", tc.path, got, tc.want) |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // The viewer has a loader for each format it claims, and the server names the |
| 39 | // one the browser must use. |
| 40 | func TestModelFormats(t *testing.T) { |
| 41 | t.Parallel() |
| 42 | cases := []struct{ path, format string }{ |
| 43 | {"part.stl", "stl"}, |
| 44 | {"PART.STL", "stl"}, |
| 45 | {"part.obj", "obj"}, |
| 46 | {"scene.gltf", "gltf"}, |
| 47 | {"scene.glb", "glb"}, |
| 48 | {"part.3mf", "3mf"}, |
| 49 | } |
| 50 | for _, tc := range cases { |
| 51 | if !isModel(tc.path) { |
| 52 | t.Errorf("isModel(%q) = false", tc.path) |
| 53 | } |
| 54 | if got := modelFormat(tc.path); got != tc.format { |
| 55 | t.Errorf("modelFormat(%q) = %q, want %q", tc.path, got, tc.format) |
| 56 | } |
| 57 | } |
| 58 | for _, path := range []string{"shot.png", "page.html", "notes.txt"} { |
| 59 | if isModel(path) { |
| 60 | t.Errorf("isModel(%q) = true", path) |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // A diff file names a commit for each side, not a range: the raw route resolves |
| 66 | // one revision. A side that does not exist, or has nothing to show, has no URL. |
| 67 | func TestDiffFileMedia(t *testing.T) { |
| 68 | t.Parallel() |
| 69 | modified := diffFile{ |
| 70 | Repo: "demo", OldRef: "abc", NewRef: "def", |
| 71 | OldPath: "img/before.png", NewPath: "img/after.png", |
| 72 | } |
| 73 | if got, want := modified.OldMedia().Src, "/demo/raw/abc/img/before.png"; got != want { |
| 74 | t.Fatalf("old src = %q, want %q", got, want) |
| 75 | } |
| 76 | if got, want := modified.NewMedia().Src, "/demo/raw/def/img/after.png"; got != want { |
| 77 | t.Fatalf("new src = %q, want %q", got, want) |
| 78 | } |
| 79 | if modified.OldMedia().Model { |
| 80 | t.Fatal("a picture was reported as a model") |
| 81 | } |
| 82 | |
| 83 | models := diffFile{ |
| 84 | Repo: "demo", OldRef: "abc", NewRef: "def", |
| 85 | OldPath: "parts/bracket.stl", NewPath: "parts/bracket.3mf", |
| 86 | } |
| 87 | if old := models.OldMedia(); old.Src != "/demo/raw/abc/parts/bracket.stl" || |
| 88 | !old.Model || old.Format != "stl" { |
| 89 | t.Fatalf("old model = %+v", old) |
| 90 | } |
| 91 | if next := models.NewMedia(); next.Src != "/demo/raw/def/parts/bracket.3mf" || |
| 92 | !next.Model || next.Format != "3mf" { |
| 93 | t.Fatalf("new model = %+v", next) |
| 94 | } |
| 95 | |
| 96 | deleted := diffFile{Repo: "demo", OldRef: "abc", NewRef: "def", OldPath: "img/shot.png"} |
| 97 | if got := deleted.NewMedia().Src; got != "" { |
| 98 | t.Fatalf("new src of a deletion = %q, want none", got) |
| 99 | } |
| 100 | if got, want := deleted.OldMedia().Src, "/demo/raw/abc/img/shot.png"; got != want { |
| 101 | t.Fatalf("deleted src = %q, want %q", got, want) |
| 102 | } |
| 103 | |
| 104 | text := diffFile{Repo: "demo", OldRef: "abc", NewRef: "def", OldPath: "a.txt", NewPath: "a.txt"} |
| 105 | if got := text.OldMedia().Src; got != "" { |
| 106 | t.Fatalf("text file src = %q, want none", got) |
| 107 | } |
| 108 | |
| 109 | unknown := diffFile{Repo: "demo", OldPath: "img/shot.png", NewPath: "img/shot.png"} |
| 110 | if got := unknown.OldMedia().Src; got != "" { |
| 111 | t.Fatalf("src without a commit = %q, want none", got) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // A branch name with a slash is one URL segment, like every other link the code |
| 116 | // browser builds. |
| 117 | func TestDiffFileMediaEscapesTheRef(t *testing.T) { |
| 118 | t.Parallel() |
| 119 | f := diffFile{Repo: "demo", OldRef: "feat/tree", OldPath: "parts/bracket.stl"} |
| 120 | if got, want := f.OldMedia().Src, "/demo/raw/feat%2Ftree/parts/bracket.stl"; got != want { |
| 121 | t.Fatalf("src = %q, want %q", got, want) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func TestDiffHasMedia(t *testing.T) { |
| 126 | t.Parallel() |
| 127 | if !diffHasMedia([]diffFile{{NewPath: "a.txt"}, {OldPath: "b.png"}}) { |
| 128 | t.Fatal("a changed picture was not noticed") |
| 129 | } |
| 130 | if !diffHasMedia([]diffFile{{NewPath: "a.txt"}, {NewPath: "b.stl"}}) { |
| 131 | t.Fatal("a changed model was not noticed") |
| 132 | } |
| 133 | if diffHasMedia([]diffFile{{NewPath: "a.txt"}, {NewPath: "b.md"}}) { |
| 134 | t.Fatal("text files were reported as media") |
| 135 | } |
| 136 | } |
| 137 |