From 2d60fcfb398d4212f34496b7b9b66c0a76d621a8 Mon Sep 17 00:00:00 2001 From: Mikhail Chusavitin Date: Mon, 26 Oct 2026 21:07:41 +0300 Subject: [PATCH] test: verify reset CLI and restored startup preserve local work (#7) --- bible-local/issue-plan-2026-10-26.md | 13 ++- cmd/qfs/reset_cli_test.go | 130 +++++++++++++++++++++++++++ 2 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 cmd/qfs/reset_cli_test.go diff --git a/bible-local/issue-plan-2026-10-26.md b/bible-local/issue-plan-2026-10-26.md index 84d1cf1..35230af 100644 --- a/bible-local/issue-plan-2026-10-26.md +++ b/bible-local/issue-plan-2026-10-26.md @@ -11,7 +11,7 @@ on `fix/issue-acceptance-and-migration-safety`. | --- | --- | --- | --- | | 1 | [#2](https://git.mchus.pro/mchus/QuoteForge/issues/2) | Block foreign Host/Origin/Referer and cross-site browser requests in both routers; validate API body types; refuse saved-password reuse for a changed host/port/user. Middleware and real setup/runtime router tests reject attacks without changing saved credentials or user data. | Acceptance passed | | 2 | [#4](https://git.mchus.pro/mchus/QuoteForge/issues/4) | Move LOT-bearing cart/rental events to delegated listeners and escaped data attributes; escape BOM and sync output; share HTML escaping; render toast as text. Malicious LOT regression checks and headless Chrome DOM checks pass. | Acceptance passed | -| 3 | [#7](https://git.mchus.pro/mchus/QuoteForge/issues/7) | Require a fresh verified archive outside rotation before reset, refuse unsynced/legacy states without explicit force, use transactional deletion, remove environment-triggered reset. Verify refusal, ZIP restore, backup failure and rollback. | Implemented | +| 3 | [#7](https://git.mchus.pro/mchus/QuoteForge/issues/7) | Require a fresh verified archive outside rotation before reset, refuse unsynced/legacy states without explicit force, use transactional deletion, remove environment-triggered reset. Verified refusal, ZIP restore, backup failure, rollback and CLI startup after restore. | Acceptance passed | | 4 | [#5](https://git.mchus.pro/mchus/QuoteForge/issues/5) | Persist TLS mode/CA in SQLite, expose setup controls, share policy across setup/sync/migration DSNs. Verify certificate trust, hostname checks, plaintext fallback policy and settings round-trip. | Implemented | | 5 | [#3](https://git.mchus.pro/mchus/QuoteForge/issues/3) | Keep each migration file in one session, execute verify headers before history insertion, propagate FK failures. Verify failed queries/checks never enter history. | Implemented | | 6 | [#6](https://git.mchus.pro/mchus/QuoteForge/issues/6) | Require Go 1.27.3, update vulnerable dependencies, block release on pinned govulncheck. Full tests, vet, security scan and macOS/Windows builds must pass. | Implemented | @@ -68,3 +68,14 @@ preserve malicious strings as text. Real click/change/focus events preserve LOT values through delegated handlers; no injected elements or payload execution are observed. This is a rendering integration check, not a full application navigation test. Set `CHROME_BIN` outside macOS to select a Chrome executable. + +### Issue #7 + +`go test ./cmd/qfs ./internal/localdb -run TestReset -count=1` covers reset +safety and a subprocess executing the real `main` flag/startup path. With +rotating backups disabled, the environment switch does not reset pending work, +normal reset refuses it, and explicit forced reset creates a verified archive. +The test restores ZIP contents and reopens the database through normal startup: +connection settings and the pending queue survive. It deliberately selects +`-migrate-status` with an unavailable loopback database to stop startup before +HTTP/browser launch; no production database or user state is touched. diff --git a/cmd/qfs/reset_cli_test.go b/cmd/qfs/reset_cli_test.go new file mode 100644 index 0000000..b531da0 --- /dev/null +++ b/cmd/qfs/reset_cli_test.go @@ -0,0 +1,130 @@ +package main + +import ( + "archive/zip" + "context" + "flag" + "io" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + "time" + + "git.mchus.pro/mchus/quoteforge/internal/localdb" +) + +func TestResetCLIHelper(t *testing.T) { + if os.Getenv("QFS_RESET_CLI_HELPER") != "1" { + return + } + for i, arg := range os.Args { + if arg == "--" { + os.Args = append([]string{"qfs"}, os.Args[i+1:]...) + flag.CommandLine = flag.NewFlagSet("qfs", flag.ExitOnError) + main() + return + } + } + t.Fatal("missing CLI arguments") +} + +func TestResetCLIAndRestore(t *testing.T) { + dir := t.TempDir() + t.Setenv("QFS_STATE_DIR", dir) + t.Setenv("QFS_BACKUP_DISABLE", "1") + t.Setenv("QFS_RESET_LOCAL_DB", "yes") + t.Setenv("QFS_BACKUP_DIR", filepath.Join(dir, "backups")) + dbPath := filepath.Join(dir, "qfs.db") + configPath := filepath.Join(dir, "config.yaml") + if err := os.WriteFile(configPath, []byte("server:\n host: 127.0.0.1\n"), 0600); err != nil { + t.Fatal(err) + } + local, err := localdb.New(dbPath) + if err != nil { + t.Fatal(err) + } + if err := local.SaveSettings("127.0.0.1", 1, "example", "example", "example"); err != nil { + t.Fatal(err) + } + if err := local.AddPendingChange("configuration", "example", "update", "{}"); err != nil { + t.Fatal(err) + } + if err := local.Close(); err != nil { + t.Fatal(err) + } + run := func(args ...string) string { + t.Helper() + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + defer cancel() + argv := append([]string{"-test.run=^TestResetCLIHelper$", "--", "-localdb", dbPath, "-config", configPath, "-migrate-status"}, args...) + cmd := exec.CommandContext(ctx, os.Args[0], argv...) + cmd.Env = append(os.Environ(), "QFS_RESET_CLI_HELPER=1") + output, err := cmd.CombinedOutput() + if ctx.Err() != nil { + t.Fatalf("CLI timed out: %s", output) + } + if err == nil { + t.Fatalf("expected reset refusal or offline migration-status exit: %s", output) + } + return string(output) + } + assertPending := func(want int64) { + t.Helper() + local, err := localdb.New(dbPath) + if err != nil { + t.Fatal(err) + } + defer local.Close() + if got := local.CountPendingChanges(); got != want { + t.Fatalf("pending=%d want=%d", got, want) + } + if !local.HasSettings() { + t.Fatal("connection settings lost") + } + } + if output := run(); !strings.Contains(output, "QFS_RESET_LOCAL_DB is ignored") { + t.Fatalf("environment reset not diagnosed: %s", output) + } + assertPending(1) + if output := run("-reset-localdb"); !strings.Contains(output, "reset refused") { + t.Fatalf("pending reset not refused: %s", output) + } + assertPending(1) + if output := run("-reset-localdb", "-force-reset-localdb"); !strings.Contains(output, "verified pre-reset backup") { + t.Fatalf("missing safety backup: %s", output) + } + assertPending(0) + archives, err := filepath.Glob(filepath.Join(dir, "backups", "pre-reset", "*.zip")) + if err != nil || len(archives) != 1 { + t.Fatalf("archives=%v err=%v", archives, err) + } + archive, err := zip.OpenReader(archives[0]) + if err != nil { + t.Fatal(err) + } + defer archive.Close() + for _, entry := range archive.File { + if entry.Name != "qfs.db" && entry.Name != "config.yaml" { + t.Fatalf("unexpected archive entry: %s", entry.Name) + } + in, err := entry.Open() + if err != nil { + t.Fatal(err) + } + data, readErr := io.ReadAll(in) + closeErr := in.Close() + if readErr != nil || closeErr != nil { + t.Fatalf("restore entry: read=%v close=%v", readErr, closeErr) + } + if err := os.WriteFile(filepath.Join(dir, entry.Name), data, 0600); err != nil { + t.Fatal(err) + } + } + assertPending(1) + if output := run(); !strings.Contains(output, "cannot run migrations: database not available") { + t.Fatalf("restored startup did not reach migration status: %s", output) + } + assertPending(1) +}