Files
pages/cmd/pageswiki/schema_test.go
T
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

107 lines
3.3 KiB
Go

package main
import (
"bytes"
"encoding/json"
"errors"
"os"
"path/filepath"
"testing"
"pageswiki/internal/db"
"pageswiki/internal/store"
)
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)
}
}
}