package oltp
import (
"path/filepath"
"testing"
)
func TestAutoAcceptRoundTrip(t *testing.T) {
db, err := Open(filepath.Join(t.TempDir(), "o.db"))
if err != nil {
t.Fatal(err)
}
defer db.Close()
if _, ok := db.AutoAcceptOf("alpha", "l1"); ok {
t.Fatal("armed before anyone armed it")
}
if err := db.SetAutoAccept("alpha", "l1", "dan"); err != nil {
t.Fatal(err)
}
if err := db.SetAutoAccept("alpha", "l1", "mel"); err != nil {
t.Fatal(err) // re-arming moves the hand
}
if by, ok := db.AutoAcceptOf("alpha", "l1"); !ok || by != "mel" {
t.Fatalf("armed by %q, want mel", by)
}
all, err := db.AutoAccepts()
if err != nil || len(all) != 1 || all[0].Account != "mel" {
t.Fatalf("list: %v %v", all, err)
}
if err := db.ClearAutoAccept("alpha", "l1"); err != nil {
t.Fatal(err)
}
if _, ok := db.AutoAcceptOf("alpha", "l1"); ok {
t.Fatal("still armed after clearing")
}
if err := db.ClearAutoAccept("alpha", "l1"); err != nil {
t.Fatal("clearing twice must be harmless")
}
}