Files
pages/internal/store/geduldh_contract_test.go
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

100 lines
3.0 KiB
Go

package store_test
import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"os"
"path/filepath"
"testing"
"pageswiki/internal/db"
"pageswiki/internal/geduldh"
"pageswiki/internal/store"
)
func TestGeduldhPublicationPrecedesRevisionCommit(t *testing.T) {
for _, mode := range []string{"missing-root", "corrupt-existing", "metadata-conflict"} {
t.Run(mode, func(t *testing.T) {
root := t.TempDir()
database, err := db.Open(filepath.Join(root, "pages.db"))
if err != nil {
t.Fatal(err)
}
defer database.Close()
if err := db.Migrate(database); err != nil {
t.Fatal(err)
}
documents := filepath.Join(root, "documents")
st := store.NewWithDocumentRoot(database, documents)
page, err := st.CreatePageAtPath("/geduldh-contract")
if err != nil {
t.Fatal(err)
}
base, err := st.SaveRevisionContent(page, []byte("base"), "base", "en", "text/plain", "base.txt")
if err != nil {
t.Fatal(err)
}
payload := []byte("new immutable payload")
sum := sha256.Sum256(payload)
hash := hex.EncodeToString(sum[:])
filename := filepath.Join(documents, "sha256", hash[:2], hash)
switch mode {
case "missing-root":
st.DocumentRoot = filepath.Join(root, "unavailable")
case "corrupt-existing":
if err := os.MkdirAll(filepath.Dir(filename), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filename, []byte("damaged"), 0644); err != nil {
t.Fatal(err)
}
}
expected := base.UUID
if mode == "metadata-conflict" {
expected = "not-the-current-head"
}
_, err = st.SaveRevisionContentIfHead(page, payload, "must not commit", "en", "text/plain", "new.txt", expected)
if err == nil {
t.Fatal("unsafe revision committed")
}
if mode == "corrupt-existing" && (!errors.Is(err, geduldh.ErrHashMismatch) || !errors.Is(err, store.ErrDocumentHashMismatch)) {
t.Fatalf("storage error identity changed: %v", err)
}
if mode == "metadata-conflict" && !errors.Is(err, store.ErrRevisionConflict) {
t.Fatalf("conflict changed: %v", err)
}
var count int
if err := database.QueryRow(`SELECT count(*) FROM revisions WHERE object_uuid=?`, page).Scan(&count); err != nil {
t.Fatal(err)
}
if count != 1 {
t.Fatalf("failed save added revision: count=%d", count)
}
st.DocumentRoot = documents
loaded, err := st.GetRevision(base.UUID)
if err != nil || loaded.Body != "base" {
t.Fatalf("previous revision lost: %v", err)
}
switch mode {
case "corrupt-existing":
data, err := os.ReadFile(filename)
if err != nil || string(data) != "damaged" {
t.Fatalf("corruption silently repaired: %v", err)
}
case "metadata-conflict":
// A failed metadata transaction may leave a valid unreferenced blob;
// the payload layer must not delete it without reference/retention proof.
backend, err := geduldh.NewLocal(documents)
if err != nil {
t.Fatal(err)
}
if err := backend.Verify(context.Background(), hash); err != nil {
t.Fatal(err)
}
}
})
}
}