From 41c1c7e9efaa3c8c998f8bf4806de945391f8ef7 Mon Sep 17 00:00:00 2001 From: Ophestra Date: Tue, 31 Mar 2026 04:11:15 +1100 Subject: [PATCH] cmd/pkgserver: clean up test_ui routes Co-authored-by: Kat <00-kat@proton.me> --- .gitignore | 5 +- cmd/pkgserver/test_ui.go | 100 +++++--------------------- cmd/pkgserver/ui_test/lib/ui.html | 2 +- cmd/pkgserver/ui_test/lib/ui.scss | 12 ++-- cmd/pkgserver/ui_test/sample_tests.ts | 2 +- 5 files changed, 26 insertions(+), 95 deletions(-) diff --git a/.gitignore b/.gitignore index b541d009..9fdf30d6 100644 --- a/.gitignore +++ b/.gitignore @@ -31,10 +31,7 @@ go.work.sum /cmd/pkgserver/ui/static/*.js /cmd/pkgserver/ui/static/*.css* /cmd/pkgserver/ui/static/*.css.map -/cmd/pkgserver/ui_test/*.js -/cmd/pkgserver/ui_test/lib/*.js -/cmd/pkgserver/ui_test/lib/*.css* -/cmd/pkgserver/ui_test/lib/*.css.map +/cmd/pkgserver/ui_test/static /internal/pkg/testdata/testtool /internal/rosa/hakurei_current.tar.gz diff --git a/cmd/pkgserver/test_ui.go b/cmd/pkgserver/test_ui.go index baa2988d..688b689a 100644 --- a/cmd/pkgserver/test_ui.go +++ b/cmd/pkgserver/test_ui.go @@ -4,94 +4,28 @@ package main import ( "embed" + "io/fs" "net/http" - "path" - "strings" ) -//go:generate tsc -p ui_test -//go:generate sass ui_test/lib/ui.scss ui_test/lib/ui.css -//go:embed ui_test/* -var test_content embed.FS +//go:generate mkdir ui_test/ui +//go:generate sh -c "cp ui/static/*.ts ui_test/ui/" +//go:generate tsc --outDir ui_test/static -p ui_test +//go:generate rm -r ui_test/ui/ +//go:generate sass ui_test/lib/ui.scss ui_test/static/style.css +//go:generate cp ui_test/lib/ui.html ui_test/static/index.html +//go:generate sh -c "cd ui_test/lib && cp *.svg ../static/" +//go:embed ui_test/static +var _staticTest embed.FS -func serveTestWebUI(w http.ResponseWriter, r *http.Request) { - w.Header().Set("X-Content-Type-Options", "nosniff") - w.Header().Set("X-XSS-Protection", "1") - w.Header().Set("X-Frame-Options", "DENY") - - http.ServeFileFS(w, r, test_content, "ui_test/lib/ui.html") -} - -func serveTestWebUIStaticContent(w http.ResponseWriter, r *http.Request) { - switch r.URL.Path { - case "/testui/style.css": - http.ServeFileFS(w, r, test_content, "ui_test/lib/ui.css") - case "/testui/skip-closed.svg": - http.ServeFileFS(w, r, test_content, "ui_test/lib/skip-closed.svg") - case "/testui/skip-open.svg": - http.ServeFileFS(w, r, test_content, "ui_test/lib/skip-open.svg") - case "/testui/success-closed.svg": - http.ServeFileFS(w, r, test_content, "ui_test/lib/success-closed.svg") - case "/testui/success-open.svg": - http.ServeFileFS(w, r, test_content, "ui_test/lib/success-open.svg") - case "/testui/failure-closed.svg": - http.ServeFileFS(w, r, test_content, "ui_test/lib/failure-closed.svg") - case "/testui/failure-open.svg": - http.ServeFileFS(w, r, test_content, "ui_test/lib/failure-open.svg") - default: - http.NotFound(w, r) +var staticTest = func() fs.FS { + if f, err := fs.Sub(_staticTest, "ui_test/static"); err != nil { + panic(err) + } else { + return f } -} - -func serveTestLibrary(w http.ResponseWriter, r *http.Request) { - switch r.URL.Path { - case "/test/lib/test.js": - http.ServeFileFS(w, r, test_content, "ui_test/lib/test.js") - default: - http.NotFound(w, r) - } -} - -func serveTests(w http.ResponseWriter, r *http.Request) { - if r.URL.Path == "/test/" { - http.Redirect(w, r, "/test.html", http.StatusMovedPermanently) - return - } - testPath := strings.TrimPrefix(r.URL.Path, "/test/") - - if path.Ext(testPath) != ".js" { - http.Error(w, "403 forbidden", http.StatusForbidden) - } - - w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") - w.Header().Set("Pragma", "no-cache") - w.Header().Set("Expires", "0") - - http.ServeFileFS(w, r, test_content, "ui_test/"+testPath) -} - -func redirectUI(w http.ResponseWriter, r *http.Request) { - // The base path should not redirect to the root. - if r.URL.Path == "/ui/" { - http.NotFound(w, r) - return - } - if path.Ext(r.URL.Path) != ".js" { - http.Error(w, "403 forbidden", http.StatusForbidden) - return - } - - w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") - w.Header().Set("Pragma", "no-cache") - w.Header().Set("Expires", "0") - - http.Redirect(w, r, strings.TrimPrefix(r.URL.Path, "/ui"), http.StatusFound) -} +}() func testUIRoutes(mux *http.ServeMux) { - mux.HandleFunc("GET /test.html", serveTestWebUI) - mux.HandleFunc("GET /testui/", serveTestWebUIStaticContent) - mux.HandleFunc("GET /test/lib/", serveTestLibrary) - mux.HandleFunc("GET /test/", serveTests) - mux.HandleFunc("GET /ui/", redirectUI) + mux.Handle("GET /test/", http.StripPrefix("/test", http.FileServer(http.FS(staticTest)))) } diff --git a/cmd/pkgserver/ui_test/lib/ui.html b/cmd/pkgserver/ui_test/lib/ui.html index e10f2307..d5a2e279 100644 --- a/cmd/pkgserver/ui_test/lib/ui.html +++ b/cmd/pkgserver/ui_test/lib/ui.html @@ -3,7 +3,7 @@ - + PkgServer Tests diff --git a/cmd/pkgserver/ui_test/lib/ui.scss b/cmd/pkgserver/ui_test/lib/ui.scss index 2036ef73..fcbc586f 100644 --- a/cmd/pkgserver/ui_test/lib/ui.scss +++ b/cmd/pkgserver/ui_test/lib/ui.scss @@ -54,24 +54,24 @@ details.test-node { * [3]: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/summary#changing_the_summarys_icon */ color: var(--fg); - content: url("/testui/success-closed.svg") / "success"; + content: url("/test/success-closed.svg") / "success"; } &.success[open] > summary::marker { - content: url("/testui/success-open.svg") / "success"; + content: url("/test/success-open.svg") / "success"; } &.failure > summary::marker { color: red; - content: url("/testui/failure-closed.svg") / "failure"; + content: url("/test/failure-closed.svg") / "failure"; } &.failure[open] > summary::marker { - content: url("/testui/failure-open.svg") / "failure"; + content: url("/test/failure-open.svg") / "failure"; } &.skip > summary::marker { color: blue; - content: url("/testui/skip-closed.svg") / "skip"; + content: url("/test/skip-closed.svg") / "skip"; } &.skip[open] > summary::marker { - content: url("/testui/skip-open.svg") / "skip"; + content: url("/test/skip-open.svg") / "skip"; } } diff --git a/cmd/pkgserver/ui_test/sample_tests.ts b/cmd/pkgserver/ui_test/sample_tests.ts index 6e47cf58..d6e8b5d1 100644 --- a/cmd/pkgserver/ui_test/sample_tests.ts +++ b/cmd/pkgserver/ui_test/sample_tests.ts @@ -1,4 +1,4 @@ -import "../ui/static/index.js"; +import "./ui/index.js"; import { NoOpReporter, TestRegistrar, context, group, suite, test } from "./lib/test.js"; suite("dog", [