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.
177 lines
5.7 KiB
Go
177 lines
5.7 KiB
Go
package documentstore_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"pageswiki/internal/documentstore"
|
|
"pageswiki/internal/geduldh"
|
|
)
|
|
|
|
// These assignments intentionally fail compilation if aliases become wrappers
|
|
// with different type identities or constructor signatures.
|
|
var (
|
|
_ func(string) (*geduldh.Local, error) = documentstore.NewLocal
|
|
_ func(documentstore.Source, documentstore.MaterializeOptions) (*documentstore.Materializer, error) = geduldh.NewMaterializer
|
|
_ documentstore.Store = (*geduldh.Local)(nil)
|
|
_ geduldh.Store = (*documentstore.Local)(nil)
|
|
)
|
|
|
|
func TestGeduldhLegacyLayoutAndSharedIdentity(t *testing.T) {
|
|
ctx := context.Background()
|
|
root := t.TempDir()
|
|
body := []byte("existing Document 日本語\x00\xff")
|
|
sum := sha256.Sum256(body)
|
|
hash := hex.EncodeToString(sum[:])
|
|
// Seed the pre-extraction layout independently of either Key implementation.
|
|
filename := filepath.Join(root, "sha256", hash[:2], hash)
|
|
if err := os.MkdirAll(filepath.Dir(filename), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filename, body, 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
before, err := os.Stat(filename)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
old, err := documentstore.NewLocal(root)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
current, err := geduldh.NewLocal(root)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, backend := range []geduldh.Store{old, current} {
|
|
// The old normalization rules and payload bytes stay unchanged.
|
|
inputHash := " " + strings.ToUpper(hash) + " "
|
|
info, err := backend.Stat(ctx, inputHash)
|
|
if err != nil || info.Hash != hash || info.Size != int64(len(body)) {
|
|
t.Fatalf("stat=%+v err=%v", info, err)
|
|
}
|
|
reader, err := backend.Open(ctx, inputHash)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
func() {
|
|
defer reader.Close()
|
|
if _, err := reader.Seek(2, io.SeekStart); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
buf := make([]byte, 4)
|
|
if _, err := reader.ReadAt(buf, 5); err != nil || !bytes.Equal(buf, body[5:9]) {
|
|
t.Fatalf("ReadAt=%q err=%v", buf, err)
|
|
}
|
|
// ReaderAt must not move the sequential stream cursor (ZIP and Range).
|
|
data, err := io.ReadAll(reader)
|
|
if err != nil || !bytes.Equal(data, body[2:]) {
|
|
t.Fatalf("stream=%q err=%v", data, err)
|
|
}
|
|
}()
|
|
if err := backend.PutIfAbsent(ctx, hash, int64(len(body)), bytes.NewReader(body)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := backend.Verify(ctx, hash); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
after, err := os.Stat(filename)
|
|
if err != nil || !os.SameFile(before, after) {
|
|
t.Fatalf("canonical inode replaced: %v", err)
|
|
}
|
|
oldKey, err := documentstore.Key(hash)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
newKey, err := geduldh.Key(hash)
|
|
if err != nil || oldKey != newKey || oldKey != "sha256/"+hash[:2]+"/"+hash {
|
|
t.Fatalf("key changed: %s %s %v", oldKey, newKey, err)
|
|
}
|
|
if documentstore.ErrInvalidHash != geduldh.ErrInvalidHash || documentstore.ErrHashMismatch != geduldh.ErrHashMismatch || documentstore.ErrSizeMismatch != geduldh.ErrSizeMismatch || documentstore.ErrMaterializeLimit != geduldh.ErrMaterializeLimit {
|
|
t.Fatal("legacy error identity changed")
|
|
}
|
|
if err := os.WriteFile(filename, []byte("corrupt"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, backend := range []geduldh.Store{old, current} {
|
|
if err := backend.PutIfAbsent(ctx, hash, int64(len(body)), bytes.NewReader(body)); !errors.Is(err, documentstore.ErrHashMismatch) {
|
|
t.Fatalf("corruption repaired or error changed: %v", err)
|
|
}
|
|
}
|
|
corrupt, err := os.ReadFile(filename)
|
|
if err != nil || string(corrupt) != "corrupt" {
|
|
t.Fatalf("corrupt canonical overwritten: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestGeduldhCompatibilityMaterializer(t *testing.T) {
|
|
root, scratch := t.TempDir(), t.TempDir()
|
|
backend, err := geduldh.NewLocal(root)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body := []byte("shared scratch bytes")
|
|
sum := sha256.Sum256(body)
|
|
hash := hex.EncodeToString(sum[:])
|
|
ctx := context.Background()
|
|
if err := backend.PutIfAbsent(ctx, hash, int64(len(body)), bytes.NewReader(body)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var materializer *geduldh.Materializer
|
|
materializer, err = documentstore.NewMaterializer(backend, documentstore.MaterializeOptions{TempDir: scratch, MaxBytes: 1024, Concurrent: 1, Timeout: time.Second})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var first *documentstore.Lease
|
|
first, err = materializer.Acquire(ctx, hash, 1024)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer first.Close()
|
|
second, err := materializer.Acquire(ctx, hash, 1024)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer second.Close()
|
|
if first.Path != second.Path {
|
|
t.Fatal("legacy/current leases no longer share the same copy")
|
|
}
|
|
first.Close()
|
|
data, err := os.ReadFile(second.Path)
|
|
if err != nil || !bytes.Equal(data, body) {
|
|
t.Fatalf("active lease lost: %v", err)
|
|
}
|
|
second.Close()
|
|
if _, err := os.Stat(second.Path); !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("scratch not reclaimed: %v", err)
|
|
}
|
|
if err := backend.Verify(ctx, hash); err != nil {
|
|
t.Fatal("lease cleanup affected canonical content", err)
|
|
}
|
|
}
|
|
|
|
func TestGeduldhConstructorsDoNotProvisionRoots(t *testing.T) {
|
|
root := filepath.Join(t.TempDir(), "missing")
|
|
for _, create := range []func(string) (*geduldh.Local, error){documentstore.NewLocal, geduldh.NewLocal} {
|
|
if _, err := create(root); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := os.Stat(root); !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("constructor wrote to storage root: %v", err)
|
|
}
|
|
if _, err := create(" "); err == nil {
|
|
t.Fatal("empty root accepted")
|
|
}
|
|
}
|
|
}
|