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.
279 lines
4.6 KiB
Go
279 lines
4.6 KiB
Go
package store_test
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"pageswiki/internal/db"
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func TestDocumentStoreIsCanonicalRevisionPayload(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)
|
|
}
|
|
|
|
documentRoot :=
|
|
filepath.Join(
|
|
root,
|
|
"documents",
|
|
)
|
|
|
|
st :=
|
|
store.NewWithDocumentRoot(
|
|
database,
|
|
documentRoot,
|
|
)
|
|
|
|
pageUUID, err := st.CreatePageAtPath("/document-store")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
body := "<p>canonical Document</p>"
|
|
|
|
revision, err :=
|
|
st.SaveRevisionForLanguage(
|
|
pageUUID,
|
|
body,
|
|
"save",
|
|
"en",
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
sum := sha256.Sum256([]byte(body))
|
|
expectedHash := hex.EncodeToString(sum[:])
|
|
|
|
if revision.DocumentHash != expectedHash {
|
|
t.Fatalf(
|
|
"Document hash = %q, want %q",
|
|
revision.DocumentHash,
|
|
expectedHash,
|
|
)
|
|
}
|
|
|
|
documentPath :=
|
|
filepath.Join(
|
|
documentRoot,
|
|
"sha256",
|
|
expectedHash[:2],
|
|
expectedHash,
|
|
)
|
|
|
|
payload, err := os.ReadFile(documentPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if string(payload) != body {
|
|
t.Fatalf("Document payload = %q", string(payload))
|
|
}
|
|
|
|
_, err = database.Exec(`
|
|
UPDATE revisions
|
|
SET body = 'corrupt compatibility cache'
|
|
WHERE uuid = ?
|
|
`, revision.UUID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
loaded, err := st.GetRevision(revision.UUID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if loaded.Body != body {
|
|
t.Fatalf("loaded canonical payload = %q", loaded.Body)
|
|
}
|
|
}
|
|
|
|
func TestBinaryRevisionPreservesContentMetadataAndPayload(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)
|
|
}
|
|
st := store.NewWithDocumentRoot(
|
|
database,
|
|
filepath.Join(root, "documents"),
|
|
)
|
|
pageUUID, err := st.CreatePageAtPath("/pixel.png")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
payload := []byte{0x89, 'P', 'N', 'G', 0x00, 0xff}
|
|
revision, err := st.SaveRevisionContent(
|
|
pageUUID,
|
|
payload,
|
|
"upload",
|
|
"und",
|
|
"image/png",
|
|
"pixel.png",
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
loaded, err := st.GetRevision(revision.UUID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if loaded.ContentType != "image/png" ||
|
|
loaded.Filename != "pixel.png" ||
|
|
string([]byte(loaded.Body)) != string(payload) {
|
|
t.Fatalf("loaded binary Revision = %#v", loaded)
|
|
}
|
|
}
|
|
|
|
func TestDocumentStoreBackfillsExistingRevisions(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)
|
|
}
|
|
|
|
legacyStore := store.New(database)
|
|
|
|
pageUUID, err := legacyStore.CreatePageAtPath("/legacy-document")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
revision, err :=
|
|
legacyStore.SaveRevisionForLanguage(
|
|
pageUUID,
|
|
"legacy payload",
|
|
"legacy",
|
|
"und",
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
documentRoot :=
|
|
filepath.Join(
|
|
root,
|
|
"documents",
|
|
)
|
|
|
|
store.NewWithDocumentRoot(
|
|
database,
|
|
documentRoot,
|
|
)
|
|
|
|
documentPath :=
|
|
filepath.Join(
|
|
documentRoot,
|
|
"sha256",
|
|
revision.DocumentHash[:2],
|
|
revision.DocumentHash,
|
|
)
|
|
|
|
payload, err := os.ReadFile(documentPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if string(payload) != revision.Body {
|
|
t.Fatalf("backfilled payload = %q", string(payload))
|
|
}
|
|
}
|
|
|
|
func TestLegacyDocumentRootIsRenamed(t *testing.T) {
|
|
dataDir := t.TempDir()
|
|
legacyRoot :=
|
|
filepath.Join(
|
|
dataDir,
|
|
".documents",
|
|
)
|
|
|
|
if err := os.MkdirAll(
|
|
filepath.Join(
|
|
legacyRoot,
|
|
"sha256",
|
|
),
|
|
0755,
|
|
); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
documentRoot, err :=
|
|
store.MigrateLegacyDocumentRoot(
|
|
dataDir,
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
expectedRoot :=
|
|
filepath.Join(
|
|
dataDir,
|
|
"documents",
|
|
)
|
|
|
|
if documentRoot != expectedRoot {
|
|
t.Fatalf(
|
|
"Document root = %q, want %q",
|
|
documentRoot,
|
|
expectedRoot,
|
|
)
|
|
}
|
|
|
|
if _, err := os.Stat(legacyRoot); !os.IsNotExist(err) {
|
|
t.Fatalf("legacy root still exists: %v", err)
|
|
}
|
|
|
|
if _, err := os.Stat(
|
|
filepath.Join(
|
|
documentRoot,
|
|
"sha256",
|
|
),
|
|
); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestDocumentRootMigrationRejectsSplitStores(t *testing.T) {
|
|
dataDir := t.TempDir()
|
|
|
|
for _, name := range []string{
|
|
".documents",
|
|
"documents",
|
|
} {
|
|
if err := os.Mkdir(
|
|
filepath.Join(
|
|
dataDir,
|
|
name,
|
|
),
|
|
0755,
|
|
); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
if _, err := store.MigrateLegacyDocumentRoot(dataDir); err == nil {
|
|
t.Fatal("split Document roots were accepted")
|
|
}
|
|
}
|