Keep personal notes private by default and require review before publishing shared notes. Restore backups and replay forgotten notes in one transaction, and retain the installed release when an unattended rollback could bring back forgotten notes.
110 lines
3.5 KiB
Go
110 lines
3.5 KiB
Go
package deletions
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// TestDirResolvesLikeTheOtherBinaries guards the path the writer and the restore
|
|
// read: the shipped tilde form, an empty value with an instance name, an
|
|
// XDG data home, an absolute path, and a relative path nobody can agree on.
|
|
func TestDirResolvesLikeTheOtherBinaries(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("XDG_DATA_HOME", "")
|
|
t.Setenv("ARCHIE_INSTANCE", "")
|
|
|
|
cases := []struct {
|
|
name string
|
|
configured string
|
|
env map[string]string
|
|
want string
|
|
err bool
|
|
}{
|
|
{name: "the shipped tilde form", configured: "~/.local/share/archie", want: filepath.Join(home, ".local/share/archie")},
|
|
{name: "tilde alone", configured: "~", want: home},
|
|
{name: "an absolute path", configured: "/var/lib/archie", want: "/var/lib/archie"},
|
|
{name: "an absolute path with a trailing separator", configured: "/var/lib/archie/", want: "/var/lib/archie"},
|
|
{name: "empty, the default instance", configured: "", want: filepath.Join(home, ".local/share/archie")},
|
|
{
|
|
name: "empty, a named instance", configured: "",
|
|
env: map[string]string{"ARCHIE_INSTANCE": "staging"},
|
|
want: filepath.Join(home, ".local/share/staging"),
|
|
},
|
|
{
|
|
name: "empty, an XDG data home", configured: "",
|
|
env: map[string]string{"XDG_DATA_HOME": "/work/data"},
|
|
want: "/work/data/archie",
|
|
},
|
|
{name: "a relative path two processes would read differently", configured: "archie-state", err: true},
|
|
{name: "a relative path under a dot", configured: "./state", err: true},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
for key, value := range tc.env {
|
|
t.Setenv(key, value)
|
|
}
|
|
got, err := Dir(tc.configured)
|
|
if tc.err {
|
|
if err == nil {
|
|
t.Fatalf("Dir(%q) = %q, want a refusal", tc.configured, got)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got != tc.want {
|
|
t.Fatalf("Dir(%q) = %q, want %q", tc.configured, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestAppendCreatesTheLogDurably guards the record a restore reads: the first
|
|
// deletion creates the log and any missing directories, and the lines are there
|
|
// to read afterwards.
|
|
func TestAppendCreatesTheLogDurably(t *testing.T) {
|
|
root := t.TempDir()
|
|
log := NewLog(Path(filepath.Join(root, "state/archie")))
|
|
if exists, err := log.Exists(); err != nil || exists {
|
|
t.Fatalf("Exists = %v, %v before any deletion, want false", exists, err)
|
|
}
|
|
id := uuid.New()
|
|
if err := log.Append(t.Context(), Entry{Table: "memory.notes", OrgID: "default", ID: id}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if exists, err := log.Exists(); err != nil || !exists {
|
|
t.Fatalf("Exists = %v, %v after a deletion, want true", exists, err)
|
|
}
|
|
entries, err := log.Entries()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(entries) != 1 || entries[0].ID != id {
|
|
t.Fatalf("entries = %+v, want the one deletion", entries)
|
|
}
|
|
}
|
|
|
|
// TestEnsureLeavesAnEmptyLog guards the state a running instance is in before it
|
|
// deletes anything: the log exists and holds nothing, which is what tells its
|
|
// absence apart from a lost file.
|
|
func TestEnsureLeavesAnEmptyLog(t *testing.T) {
|
|
log := NewLog(Path(filepath.Join(t.TempDir(), "state")))
|
|
if err := log.Ensure(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if exists, err := log.Exists(); err != nil || !exists {
|
|
t.Fatalf("Exists = %v, %v after Ensure, want true", exists, err)
|
|
}
|
|
entries, err := log.Entries()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(entries) != 0 {
|
|
t.Fatalf("a fresh log holds %+v", entries)
|
|
}
|
|
}
|