Files
pages/internal/server/city_labels_browser_test.go
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

136 lines
5.0 KiB
Go

package server
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
)
func TestCityLabelsChromium(t *testing.T) {
runCityBrowserFixture(t, "city-labels.js", []string{"/life/home/readme", "/life/work/tools/guide"}, []string{"normal", "popup"})
}
func TestCityOrganizerChromium(t *testing.T) {
for _, mode := range []string{"normal", "popup"} {
t.Run(mode, func(t *testing.T) {
runCityBrowserFixture(t, "city-organizer.js", []string{"/life/home/readme", "/life/home/notes", "/life/home/alpha/page", "/life/home/beta/page", "/life/work/tools/guide"}, []string{mode})
})
}
}
func TestCityImmersiveChromium(t *testing.T) {
paths := []string{"/life/home/readme", "/life/home/alpha"}
for i := 0; i < 27; i++ {
paths = append(paths, fmt.Sprintf("/life/home/alpha/page-%02d", i))
}
for i := 0; i < 7; i++ {
paths = append(paths, fmt.Sprintf("/life/home/spot-%02d/page", i))
}
runCityBrowserFixture(t, "city-immersive.js", paths, []string{"normal", "popup", "menu", "mobile-light"})
}
func TestCityKeyboardChromium(t *testing.T) {
paths := []string{"/life/home/alpha", "/life/home/beta/page", "/life/work/tools/page", "/work/dev/code/page"}
for i := 0; i < 6; i++ {
paths = append(paths, fmt.Sprintf("/life/home/alpha/page-%d", i))
}
runCityBrowserFixture(t, "city-keyboard.js", paths, []string{"normal", "popup"})
}
func TestCityWorkerChromium(t *testing.T) {
runCityBrowserFixture(t, "city-worker.js", []string{"/life/home/alpha/page", "/life/work/tools/page"}, []string{"normal", "popup"})
}
func TestCityNamedNavigationChromium(t *testing.T) {
runCityBrowserFixture(t, "city-links.js", []string{"/life/home/alpha/page?#%", "/work/home/beta/page"}, []string{"normal", "popup", "quartier", "street", "spot"})
}
func runCityBrowserFixture(t *testing.T, scriptName string, paths, modes []string) {
t.Helper()
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
s, token, domain := configFixture(t)
for _, path := range paths {
if _, err := s.store.CreatePageAtPathInDomain(domain, path); err != nil {
t.Fatal(err)
}
}
if _, err := s.applyCityAutoPlan(domain, "test"); err != nil {
t.Fatal(err)
}
s.mux.HandleFunc("/v/city", s.handleCityMap)
s.mux.HandleFunc("/v/city/", s.handleCityMap)
s.mux.HandleFunc("/v/search", s.handleSearch)
s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI)
script, err := os.ReadFile("internal/server/testdata/" + scriptName)
if err != nil {
t.Fatal(err)
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Host = "localhost"
r.Header.Set("Authorization", "Bearer "+token)
if r.URL.Path == "/static/vendor/city-generator/gen-city.bundle.js" && scriptName != "city-worker.js" {
// Deliberately vertical/diagonal roads: labels must remain horizontal.
// Real template, renderer, styles, data and authorization are retained.
w.Header().Set("Content-Type", "text/javascript")
_, _ = io.WriteString(w, `globalThis.PagesWikiGenCity={generateFabric:async(key,width,height)=>({version:'test',width,height,roads:Array.from({length:24},(_,i)=>[20+(i%6)*12,15+Math.floor(i/6)*18,20+(i%6)*12+(i%2?4:0),30+Math.floor(i/6)*18].map((v,j)=>v*(j%2?height:width)/100)),buildings:[[[25,30],[28,30],[28,34],[25,34]],[[32,50],[35,50],[35,54],[32,54]],[[62,32],[65,32],[65,36],[62,36]],[[78,50],[81,50],[81,54],[78,54]]].map(b=>b.map(p=>[p[0]*width/100,p[1]*height/100]))})};`)
return
}
if r.URL.Path == "/v/city" {
rec := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(rec, r)
for key, values := range rec.Header() {
w.Header()[key] = values
}
w.WriteHeader(rec.Code)
_, _ = io.WriteString(w, strings.Replace(rec.Body.String(), "</body>", browserFixtureScript(rec.Header(), script)+"</body>", 1))
return
}
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer server.Close()
for _, mode := range modes {
url := server.URL + "/v/city"
if mode == "popup" {
url += "?popup=1"
} else if mode == "menu" || mode == "mobile-light" {
url += "?fixture=" + mode
} else if mode == "quartier" || mode == "street" || mode == "spot" {
id := map[string]string{"quartier": "life", "street": "life/home", "spot": "alpha"}[mode]
node, err := s.store.CityNode(domain, mode, id)
if err != nil {
t.Fatal(err)
}
url += "?" + mode + "=" + node.UUID
}
ctx, cancel := context.WithTimeout(context.Background(), 35*time.Second)
cmd := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", url, t.TempDir())
screenshot := ""
if dir := os.Getenv("PAGESWIKI_BROWSER_ARTIFACTS"); dir != "" {
if err := os.MkdirAll(dir, 0755); err != nil {
cancel()
t.Fatal(err)
}
screenshot = filepath.Join(dir, strings.TrimSuffix(scriptName, ".js")+"-"+mode+".png")
}
cmd.Args = append(cmd.Args, screenshot)
if mode == "mobile-light" {
cmd.Args = append(cmd.Args, "390", "844")
}
out, err := cmd.CombinedOutput()
cancel()
if err != nil {
t.Fatalf("%s: %v %s", mode, err, out)
}
}
}