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.
229 lines
7.1 KiB
Go
229 lines
7.1 KiB
Go
package server
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"image"
|
|
"image/color"
|
|
"image/png"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"pageswiki/internal/db"
|
|
"pageswiki/internal/documentstore"
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func TestArchiveEntryReadsCanonicalDocumentWithoutRevisionHydration(t *testing.T) {
|
|
database, err := db.Open(filepath.Join(t.TempDir(), "test.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer database.Close()
|
|
if err := db.Migrate(database); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
st := store.NewWithDocumentRoot(database, t.TempDir())
|
|
domain, _ := st.GetDefaultDomain()
|
|
object, err := st.CreatePageAtPathInDomain(domain.UUID, "/archive.zip")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var payload bytes.Buffer
|
|
w := zip.NewWriter(&payload)
|
|
file, err := w.Create("hello.txt")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
file.Write([]byte("hello 日本語"))
|
|
if err := w.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
revision, err := st.SaveRevisionContent(object, payload.Bytes(), "test", "und", "application/zip", "archive.zip")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// The canonical Document must work even after compatibility DB bodies
|
|
// have been removed. This never touches production records.
|
|
if _, err := database.Exec(`UPDATE revisions SET body='' WHERE uuid=?`, revision.UUID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := &Server{store: st}
|
|
r := httptest.NewRequest(http.MethodGet, "/_/archive?path=/archive.zip&entry=hello.txt", nil)
|
|
response := httptest.NewRecorder()
|
|
s.serveZIPDocumentEntry(response, r, revision.DocumentHash, "hello.txt")
|
|
if response.Code != http.StatusOK || response.Body.String() != "hello 日本語" {
|
|
t.Fatalf("status=%d body=%q", response.Code, response.Body.String())
|
|
}
|
|
if response.Header().Get("Vary") != "Authorization, Cookie" {
|
|
t.Fatal("lost authorization cache partition")
|
|
}
|
|
missing := httptest.NewRecorder()
|
|
s.serveZIPDocumentEntry(missing, r, revision.DocumentHash, "missing")
|
|
if missing.Code != http.StatusNotFound {
|
|
t.Fatalf("missing status=%d", missing.Code)
|
|
}
|
|
ctx, cancel := context.WithCancel(r.Context())
|
|
cancel()
|
|
cancelled := httptest.NewRecorder()
|
|
s.serveZIPDocumentEntry(cancelled, r.WithContext(ctx), revision.DocumentHash, "hello.txt")
|
|
if cancelled.Code == http.StatusOK {
|
|
t.Fatal("cancelled request read Document")
|
|
}
|
|
}
|
|
|
|
// Measures the per-request cost of fetching a tiny entry from a large stored
|
|
// ZIP. Setup is outside the timer; no DB body hydration or scratch copy occurs.
|
|
func BenchmarkZIPDocumentEntry(b *testing.B) {
|
|
var payload bytes.Buffer
|
|
w := zip.NewWriter(&payload)
|
|
file, err := w.Create("hello.txt")
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
file.Write([]byte("hello"))
|
|
padding, err := w.CreateHeader(&zip.FileHeader{Name: "padding.bin", Method: zip.Store})
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
padding.Write(bytes.Repeat([]byte{0xaa}, 16<<20))
|
|
if err := w.Close(); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
root := b.TempDir()
|
|
backend, err := documentstore.NewLocal(root)
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
sum := sha256.Sum256(payload.Bytes())
|
|
hash := hex.EncodeToString(sum[:])
|
|
if err := backend.PutIfAbsent(context.Background(), hash, int64(payload.Len()), bytes.NewReader(payload.Bytes())); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
s := &Server{store: &store.Store{DocumentRoot: root}}
|
|
r := httptest.NewRequest(http.MethodGet, "/archive", nil)
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
response := httptest.NewRecorder()
|
|
s.serveZIPDocumentEntry(response, r, hash, "hello.txt")
|
|
if response.Code != http.StatusOK || response.Body.String() != "hello" {
|
|
b.Fatal("entry read failed")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPreviewImageAndZIPWorkWithReaderOnlyLegacyStore(t *testing.T) {
|
|
database, err := db.Open(filepath.Join(t.TempDir(), "test.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer database.Close()
|
|
if err := db.Migrate(database); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
st := store.New(database)
|
|
domain, _ := st.GetDefaultDomain()
|
|
s := &Server{store: st, previewRoot: t.TempDir()}
|
|
var picture bytes.Buffer
|
|
im := image.NewRGBA(image.Rect(0, 0, 10, 10))
|
|
im.Set(0, 0, color.White)
|
|
if err := png.Encode(&picture, im); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var archive bytes.Buffer
|
|
w := zip.NewWriter(&archive)
|
|
for _, name := range []string{"1.png", "2.png", "3.png"} {
|
|
f, err := w.Create(name)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f.Write(picture.Bytes())
|
|
}
|
|
if err := w.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, tc := range []struct {
|
|
kind, filename string
|
|
body []byte
|
|
}{
|
|
{"image", "picture.png", picture.Bytes()}, {"archive", "comic.zip", archive.Bytes()},
|
|
} {
|
|
object, err := st.CreatePageAtPathInDomain(domain.UUID, "/"+tc.filename)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
revision, err := st.SaveRevisionContent(object, tc.body, "test", "und", "application/octet-stream", tc.filename)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.generatePreview(previewEventPayload{DocumentHash: revision.DocumentHash, Kind: tc.kind, Filename: tc.filename}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if info, err := os.Stat(s.previewCachePath(revision.DocumentHash, tc.kind)); err != nil || info.Size() == 0 {
|
|
t.Fatalf("preview: %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProjectionMetadataMaterializesAndCleansSource(t *testing.T) {
|
|
database, err := db.Open(filepath.Join(t.TempDir(), "test.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer database.Close()
|
|
if err := db.Migrate(database); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
st := store.NewWithDocumentRoot(database, t.TempDir())
|
|
domain, _ := st.GetDefaultDomain()
|
|
object, err := st.CreatePageAtPathInDomain(domain.UUID, "/test.pdf")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
revision, err := st.SaveRevisionContent(object, []byte("synthetic PDF fixture"), "test", "und", "application/pdf", "test.pdf")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Synthetic tools validate only the storage handoff, not PDF rendering.
|
|
bin := t.TempDir()
|
|
for name, script := range map[string]string{
|
|
"nice": "#!/bin/sh\nshift\nshift\nexec \"$@\"\n",
|
|
"pdfinfo": "#!/bin/sh\ntest -r \"$3\" || exit 1\nprintf '%s' \"$3\" > \"$PAGESWIKI_TEST_SOURCE\"\nprintf 'Pages: 3\\n'\n",
|
|
} {
|
|
if err := os.WriteFile(filepath.Join(bin, name), []byte(script), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
logFile := filepath.Join(t.TempDir(), "source")
|
|
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
|
|
t.Setenv("PAGESWIKI_TEST_SOURCE", logFile)
|
|
s := &Server{store: st}
|
|
result, err := s.inspectProjectionMetadata(metadataProjectionPayload{DocumentHash: revision.DocumentHash, Kind: "pdf", Version: metadataProjectionVersion})
|
|
if err != nil || result.PDFPages != 3 {
|
|
t.Fatalf("result=%+v err=%v", result, err)
|
|
}
|
|
source, err := os.ReadFile(logFile)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(string(source), "pageswiki-document-") || strings.HasPrefix(string(source), st.DocumentRoot+string(os.PathSeparator)) {
|
|
t.Fatalf("tool received canonical path: %s", source)
|
|
}
|
|
if _, err := os.Stat(string(source)); !os.IsNotExist(err) {
|
|
t.Fatalf("scratch leaked: %v", err)
|
|
}
|
|
reader, err := st.OpenDocument(revision.DocumentHash)
|
|
if err != nil {
|
|
t.Fatal("canonical document removed", err)
|
|
}
|
|
reader.Close()
|
|
}
|