package backup import ( "database/sql" "os" "path/filepath" "strings" "testing" "time" ) func value(t *testing.T, path, key string) string { t.Helper() d, err := sql.Open("sqlite", "file:"+path) if err != nil { t.Fatal(err) } defer d.Close() var v string d.QueryRow(`SELECT value FROM settings WHERE key = ?`, key).Scan(&v) return v } func TestStageAndApply(t *testing.T) { data := t.TempDir() cur := newDB(t, filepath.Join(data, "app.db")) now := time.Date(2026, 11, 22, 15, 0, 0, 0, time.Local) old, err := Make(cur, Dir(data), Manual, "good", now.Add(-time.Hour)) if err != nil { t.Fatal(err) } cur.Exec(`UPDATE settings SET value = 'changed' WHERE key = 'a'`) // Too new, damaged and foreign files are refused, and nothing is staged. newer := filepath.Join(t.TempDir(), "newer.db") nd := newDB(t, newer) nd.Exec(`PRAGMA user_version = 99`) nd.Close() junk := filepath.Join(t.TempDir(), "junk.db") os.WriteFile(junk, []byte("junk"), 0o600) for src, want := range map[string]string{newer: "newer Supermarket", junk: "not a database", "/nope.db": "no such file"} { if _, err := Stage(cur, data, src, 24, now); err == nil || !strings.Contains(err.Error(), want) { t.Errorf("%s: %v", src, err) } if Pending(data) { t.Errorf("%s was staged", src) } } r, err := Stage(cur, data, old.Path, 24, now) if err != nil { t.Fatal(err) } if r.From != old.Name || !strings.HasSuffix(r.Backup, "-restore.db") || r.Secrets || !Pending(data) { t.Errorf("staged: %+v", r) } // What was there is backed up, with the change. if v := value(t, filepath.Join(Dir(data), r.Backup), "a"); v != "changed" { t.Errorf("safety backup: %q", v) } cur.Close() os.WriteFile(filepath.Join(data, "app.db-wal"), []byte("stale"), 0o600) got, err := ApplyPending(data, now.Add(time.Minute)) if err != nil || got == nil || got.From != old.Name { t.Fatalf("applied: %+v %v", got, err) } if v := value(t, filepath.Join(data, "app.db"), "a"); v != "1" { t.Errorf("restored value: %q", v) } if _, err := os.Stat(filepath.Join(data, "app.db-wal")); err == nil { t.Error("the old -wal is still there") } if Pending(data) || LastRestore(data) == nil || LastRestore(data).From != old.Name { t.Errorf("after: pending=%v last=%+v", Pending(data), LastRestore(data)) } if r, err := ApplyPending(data, now); r != nil || err != nil { t.Errorf("applied twice: %v %v", r, err) } } func TestCancel(t *testing.T) { data := t.TempDir() cur := newDB(t, filepath.Join(data, "app.db")) defer cur.Close() b, _ := Make(cur, Dir(data), Manual, "", time.Now()) if _, err := Stage(cur, data, b.Path, 24, time.Now()); err != nil { t.Fatal(err) } Cancel(data) if Pending(data) { t.Error("still pending") } if r, _ := ApplyPending(data, time.Now()); r != nil { t.Error("applied a cancelled restore") } } func TestArchiveWithSecrets(t *testing.T) { // Machine one: a database, secrets and a token, archived. one := t.TempDir() d1 := newDB(t, filepath.Join(one, "app.db")) d1.Exec(`UPDATE settings SET value = 'from-one' WHERE key = 'a'`) os.WriteFile(filepath.Join(one, "secrets.json"), []byte(`{"stripe":"sk_one"}`), 0o600) os.WriteFile(filepath.Join(one, "mcp-token"), []byte("token-one"), 0o600) now := time.Date(2026, 11, 22, 16, 0, 0, 0, time.Local) path, err := Archive(d1, one, now) d1.Close() if err != nil { t.Fatal(err) } if st, _ := os.Stat(path); st.Mode().Perm() != 0o600 || !strings.HasSuffix(path, "-with-secrets.tar.gz") { t.Errorf("archive: %s %v", path, st.Mode()) } if bs, _ := List(Dir(one)); len(bs) != 0 { t.Errorf("the archive is listed with the backups: %v", bs) } // Machine two: restored from it, secrets included; its own token stays. two := t.TempDir() d2 := newDB(t, filepath.Join(two, "app.db")) os.WriteFile(filepath.Join(two, "secrets.json"), []byte(`{"stripe":"sk_two"}`), 0o600) os.WriteFile(filepath.Join(two, "mcp-token"), []byte("token-two"), 0o600) r, err := Stage(d2, two, path, 24, now) d2.Close() if err != nil || !r.Secrets { t.Fatalf("staged: %+v %v", r, err) } if _, err := ApplyPending(two, now); err != nil { t.Fatal(err) } if v := value(t, filepath.Join(two, "app.db"), "a"); v != "from-one" { t.Errorf("database: %q", v) } raw, _ := os.ReadFile(filepath.Join(two, "secrets.json")) tok, _ := os.ReadFile(filepath.Join(two, "mcp-token")) if string(raw) != `{"stripe":"sk_one"}` || string(tok) != "token-two" { t.Errorf("secrets %s, token %s", raw, tok) } kept, _ := filepath.Glob(filepath.Join(Dir(two), "secrets-before-restore-*.json")) if len(kept) != 1 { t.Fatalf("the old secrets weren't kept: %v", kept) } if old, _ := os.ReadFile(kept[0]); string(old) != `{"stripe":"sk_two"}` { t.Errorf("kept: %s", old) } }