Files
pages/internal/server/uuid_browser_test.go
Anya Tokugawa 9dd425f765
Build and publish container images / images (push) Successful in 7m55s
Release container images / release (push) Successful in 18s
Fix UUID generation in browsers without crypto.randomUUID
2026-09-29 12:39:58 +09:00

62 lines
2.0 KiB
Go

package server
import (
"context"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"strings"
"testing"
"time"
)
func TestUUIDCompatibilityJS(t *testing.T) {
node, err := exec.LookPath("node")
if err != nil {
t.Skip("node is required for UUID compatibility tests")
}
t.Chdir("../..")
if out, err := exec.Command(node, "internal/server/testdata/uuid.test.cjs").CombinedOutput(); err != nil {
t.Fatalf("UUID compatibility: %v\n%s", err, out)
}
}
func TestTaskProjectUUIDFallbackChromium(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
s, token, _, _ := taskFixture(t)
script, err := os.ReadFile("internal/server/testdata/uuid-project-browser.js")
if err != nil {
t.Fatal(err)
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Set("Authorization", "Bearer "+token)
if r.Method == "GET" && strings.HasPrefix(r.URL.Path, "/v/plan/") {
rec := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(rec, r)
for key, values := range rec.Header() {
w.Header()[key] = values
}
body := strings.Replace(rec.Body.String(), "<head>", `<head><script>Object.defineProperty(crypto,'randomUUID',{value:undefined,writable:true,configurable:true});window.uuidNativeWasMissing=typeof crypto.randomUUID==='undefined';</script>`, 1)
if r.URL.Path == "/v/plan/projects" {
body = strings.Replace(body, "</body>", "<script>"+string(script)+"</script></body>", 1)
}
w.Header().Del("Content-Length")
w.WriteHeader(rec.Code)
_, _ = w.Write([]byte(body))
return
}
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer server.Close()
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
defer cancel()
target := strings.Replace(server.URL, "127.0.0.1", "localhost", 1) + "/v/plan/projects"
cmd := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", target, t.TempDir())
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("project creation without randomUUID: %v\n%s", err, out)
}
}