Files
pages/internal/server/editor_resilience_test.go
T
Anya Tokugawa d5d21b7841
Build and publish container images / images (push) Successful in 9m34s
Release container images / release (push) Successful in 36s
Refactor architecture into eight components and consolidate reliability work
Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts.

Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation.

Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
2026-11-01 15:23:31 +09:00

148 lines
5.0 KiB
Go

package server
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"strings"
"testing"
"time"
)
func editorFixture(t *testing.T) (*Server, string, string, string) {
s, token, domain := configFixture(t)
s.mux.HandleFunc("/e/", s.handleEditorRoute)
s.mux.HandleFunc("/_/api/v1/editor/state", s.handleEditorState)
s.mux.HandleFunc("/_/api/v1/editor/convert", s.handleEditorConvert)
s.mux.HandleFunc("/_/api/v1/revisions/save", s.handleSaveRevisionAPI)
s.mux.HandleFunc("/_/api/v1/revisions/list", s.handleRevisionListAPI)
s.mux.HandleFunc("/_/api/v1/render/markdown", s.handleMarkdownRenderAPI)
object, err := s.store.CreatePageAtPathInDomain(domain, "/editor-fixture")
if err != nil {
t.Fatal(err)
}
if _, err = s.store.SaveRevisionContent(object, []byte("<h1>Base</h1><p>one two three</p>"), "base", "und", "text/html", ""); err != nil {
t.Fatal(err)
}
return s, token, domain, object
}
func TestEditorResilienceAPI(t *testing.T) {
s, token, domain, object := editorFixture(t)
principal, err := s.store.PrincipalForSession(domain, token)
if err != nil {
t.Fatal(err)
}
base, err := s.store.GetRevisionForExactLanguage(object, "und")
if err != nil {
t.Fatal(err)
}
payload := map[string]any{"object_uuid": object, "body": "<h1>Saved</h1>", "lang": "und", "content_type": "text/html", "base_revision_uuid": base.UUID, "editor_principal_uuid": principal.UUID}
save := func(token string) *httptest.ResponseRecorder {
b, _ := json.Marshal(payload)
r := httptest.NewRequest("POST", "http://localhost/_/api/v1/revisions/save", bytes.NewReader(b))
r.Header.Set("Authorization", "Bearer "+token)
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
return w
}
if w := save(token); w.Code != 200 {
t.Fatalf("save %d %s", w.Code, w.Body.String())
}
if w := save(token); w.Code != 409 {
t.Fatalf("stale %d %s", w.Code, w.Body.String())
}
payload["editor_principal_uuid"] = "different-user"
if w := save(token); w.Code != 401 {
t.Fatalf("identity %d", w.Code)
}
if w := save("expired"); w.Code != 401 {
t.Fatalf("expired %d", w.Code)
}
for _, query := range []string{"&metadata=1", "&revision=" + base.UUID, "&lang=en"} {
w := configRequest(s, token, "GET", "/_/api/v1/editor/state?uuid="+object+query, nil)
if w.Code != 200 {
t.Fatalf("state %s %d %s", query, w.Code, w.Body.String())
}
var out map[string]any
if err = json.Unmarshal(w.Body.Bytes(), &out); err != nil {
t.Fatal(err)
}
if out["principal_uuid"] != principal.UUID {
t.Fatal("identity omitted")
}
if query == "&metadata=1" {
if _, ok := out["body"]; ok {
t.Fatal("poll hydrated body")
}
}
if query == "&revision="+base.UUID && out["body"] != base.Body {
t.Fatal("old revision missing")
}
if query == "&lang=en" && out["revision_uuid"] != "" {
t.Fatal("language fallback")
}
}
other, err := s.store.CreatePageAtPathInDomain(domain, "/other")
if err != nil {
t.Fatal(err)
}
old, err := s.store.SaveRevisionForLanguage(other, "private unrelated body", "", "und")
if err != nil {
t.Fatal(err)
}
if w := configRequest(s, token, "GET", "/_/api/v1/editor/state?uuid="+object+"&revision="+old.UUID, nil); w.Code != 404 {
t.Fatalf("other object revision %d", w.Code)
}
w := configRequest(s, token, "GET", "/e/-/editor-fixture?lang=en&edittype=markdown", nil)
if w.Code != 200 || !strings.Contains(w.Body.String(), `data-base-revision=""`) || !strings.Contains(w.Body.String(), `data-owner="`+principal.UUID+`"`) {
t.Fatalf("editor language/owner %d", w.Code)
}
// Legacy API callers can still save without CAS during the migration.
delete(payload, "base_revision_uuid")
payload["editor_principal_uuid"] = principal.UUID
if w := save(token); w.Code != 200 {
t.Fatalf("legacy API %d", w.Code)
}
}
func TestEditorResilienceChromium(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
s, token, domain, _ := editorFixture(t)
if err := s.store.SetConfigInDomain(domain, "editor_keymap", "vim"); err != nil {
t.Fatal(err)
}
script, err := os.ReadFile("internal/server/testdata/editor-resilience.js")
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 == "/fixture" {
w.Header().Set("Content-Type", "text/html")
_, _ = io.WriteString(w, `<!doctype html><body><script>`+string(script)+`</script></body>`)
return
}
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer server.Close()
ctx, cancel := context.WithTimeout(context.Background(), 55*time.Second)
defer cancel()
args := []string{"internal/server/testdata/vim-browser-smoke.mjs", server.URL + "/fixture", t.TempDir()}
if shot := os.Getenv("EDITOR_TEST_SCREENSHOT"); shot != "" {
args = append(args, shot)
}
if out, err := exec.CommandContext(ctx, "node", args...).CombinedOutput(); err != nil {
t.Fatalf("browser %v %s", err, out)
}
}