Files
pages/cmd/pageswiki/schema_test.go
T
2026-11-15 05:47:14 +09:00

158 lines
5.2 KiB
Go

package main
import (
"bytes"
"encoding/json"
"errors"
"os"
"path/filepath"
"testing"
"pageswiki/internal/db"
"pageswiki/internal/metadata"
"pageswiki/internal/store"
)
func TestPortabilityCommandIsReadOnlyAndNeverClaimsApplicationReady(t *testing.T) {
dir := t.TempDir()
database, err := db.Open(filepath.Join(dir, "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(dir, "documents"))
if _, err := st.CreatePageAtPath("/preserved"); err != nil {
t.Fatal(err)
}
if _, err := database.Exec(`CREATE TABLE private_extension_data(secret TEXT); INSERT INTO private_extension_data VALUES ('DO_NOT_PRINT_PRIVATE_VALUE')`); err != nil {
t.Fatal(err)
}
var out bytes.Buffer
if err := runSchema([]string{"portability", "--db", filepath.Join(dir, "pages.db"), "--json"}, &out); !errors.Is(err, errPostgreSQLNotReady) {
t.Fatal("unsafe readiness result", err)
}
var report metadata.PortabilityReport
if err := json.Unmarshal(out.Bytes(), &report); err != nil {
t.Fatal(err)
}
if report.ApplicationReady || report.CoreCompatible || len(report.Blockers) == 0 {
t.Fatal("legacy application marked portable", report)
}
if bytes.Contains(out.Bytes(), []byte("DO_NOT_PRINT_PRIVATE_VALUE")) {
t.Fatal("row values leaked")
}
if _, err := st.ResolvePath("/preserved"); err != nil {
t.Fatal("inspection changed existing data", err)
}
var count int
if err := database.QueryRow(`SELECT count(*) FROM schema_migration_steps WHERE component='core-metadata'`).Scan(&count); err != nil || count != 0 {
t.Fatal("inspection migrated database", err)
}
}
func TestPortabilityCommandMissingDatabaseDoesNotCreateFiles(t *testing.T) {
dir := filepath.Join(t.TempDir(), "not-created")
if err := runSchema([]string{"portability", "--data-dir", dir}, &bytes.Buffer{}); err == nil {
t.Fatal("nonexistent DB accepted")
}
if _, err := os.Stat(dir); !errors.Is(err, os.ErrNotExist) {
t.Fatal("directory created", err)
}
}
func TestSchemaCommandMissingDatabaseDoesNotInitialize(t *testing.T) {
dir := filepath.Join(t.TempDir(), "missing")
var out bytes.Buffer
if err := runSchema([]string{"status", "--data-dir", dir}, &out); err == nil {
t.Fatal("missing DB accepted")
}
if _, err := os.Stat(dir); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("directory created: %v", err)
}
}
func TestSchemaCommandPendingAndBlocked(t *testing.T) {
dir := t.TempDir()
database, err := db.Open(filepath.Join(dir, "pages.db"))
if err != nil {
t.Fatal(err)
}
defer database.Close()
if err := database.Ping(); err != nil {
t.Fatal(err)
}
var out bytes.Buffer
if err := runSchema([]string{"status", "--data-dir", dir, "--json"}, &out); err != nil {
t.Fatal(err)
}
var report store.SchemaInspection
if err := json.Unmarshal(out.Bytes(), &report); err != nil {
t.Fatal(err)
}
if report.Coverage != "managed-components-only" || len(report.Components) == 0 || len(report.LegacyAreas) == 0 {
t.Fatalf("report=%+v", report)
}
out.Reset()
if err := runSchema([]string{"check", "--db", filepath.Join(dir, "pages.db")}, &out); !errors.Is(err, errSchemaPending) {
t.Fatalf("pending: %v", err)
}
if _, err := database.Exec(`CREATE TABLE schema_migration_steps(component TEXT,version INTEGER,name TEXT,checksum TEXT,applied_at_ms INTEGER); INSERT INTO schema_migration_steps VALUES('future-component',1,'future','unknown',0)`); err != nil {
t.Fatal(err)
}
out.Reset()
if err := runSchema([]string{"check", "--data-dir", dir}, &out); !errors.Is(err, errSchemaBlocked) {
t.Fatalf("unknown component: %v", err)
}
var count int
if err := database.QueryRow(`SELECT count(*) FROM sqlite_master WHERE type='table'`).Scan(&count); err != nil || count != 1 {
t.Fatalf("command initialized DB: %d %v", count, err)
}
}
func TestSchemaCommandArguments(t *testing.T) {
for _, args := range [][]string{{"apply"}, {"check", "--timeout", "0s"}, {"check", "extra"}, {"check", "--db", "a", "--data-dir", "b"}} {
if err := runSchema(args, &bytes.Buffer{}); err == nil {
t.Fatalf("accepted %v", args)
}
}
for _, args := range [][]string{nil, {"--help"}, {"check", "--help"}} {
if err := runSchema(args, &bytes.Buffer{}); err != nil {
t.Fatalf("help %v: %v", args, err)
}
}
}
func TestSchemaCommandCurrentApplicationDatabase(t *testing.T) {
dir := t.TempDir()
database, err := db.Open(filepath.Join(dir, "pages.db"))
if err != nil {
t.Fatal(err)
}
defer database.Close()
if err := db.Init(database); err != nil {
t.Fatal(err)
}
if err := db.Migrate(database); err != nil {
t.Fatal(err)
}
// Initialize only this synthetic fixture. runSchema must not run the
// constructor itself, even for an already initialized application DB.
store.NewWithDocumentRoot(database, filepath.Join(dir, "documents"))
var out bytes.Buffer
if err := runSchema([]string{"check", "--data-dir", dir, "--json"}, &out); err != nil {
t.Fatalf("current database: %v\n%s", err, out.String())
}
var report store.SchemaInspection
if err := json.Unmarshal(out.Bytes(), &report); err != nil {
t.Fatal(err)
}
for _, component := range report.Components {
if component.State != "current" {
t.Fatalf("component=%+v", component)
}
}
}