Skip to content
quarry Artifact [6bb9477ace]
Login

Artifact [6bb9477ace]

Artifact 6bb9477acee4729964a671748b4931253276c68ec3e2a9783bbbc11ecc5df810:


package oltp

import (
	"path/filepath"
	"slices"
	"testing"
)

// A pin is one account's own mark on a repository, and the board is
// ordered by it: what the store owes is the order the account pinned in,
// a second pin that changes nothing, a clean unpin, and no leak from one
// account to another.
func TestPins(t *testing.T) {
	d, err := Open(filepath.Join(t.TempDir(), "oltp.db"))
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { d.Close() })

	for _, name := range []string{"zeta", "alpha", "mid"} {
		if err := d.SetPin("dan", name, true); err != nil {
			t.Fatal(err)
		}
	}
	if err := d.SetPin("eve", "alpha", true); err != nil {
		t.Fatal(err)
	}
	got, err := d.Pins("dan")
	if err != nil {
		t.Fatal(err)
	}
	if want := []string{"zeta", "alpha", "mid"}; !slices.Equal(got, want) {
		t.Errorf("FAIL: pins %v, want the order they were made in, %v", got, want)
	}
	// Pinning what is pinned keeps its place; it is not a second entry.
	if err := d.SetPin("dan", "zeta", true); err != nil {
		t.Fatal(err)
	}
	if err := d.SetPin("dan", "alpha", true); err != nil {
		t.Fatal(err)
	}
	if got, _ := d.Pins("dan"); !slices.Equal(got, []string{"zeta", "alpha", "mid"}) {
		t.Errorf("FAIL: pinning twice moved a pin: %v", got)
	}
	// Unpinning drops it, a repository never pinned is not an error, and
	// pinning it again puts it last.
	if err := d.SetPin("dan", "alpha", false); err != nil {
		t.Fatal(err)
	}
	if err := d.SetPin("dan", "never", false); err != nil {
		t.Fatal(err)
	}
	if err := d.SetPin("dan", "alpha", true); err != nil {
		t.Fatal(err)
	}
	if got, _ := d.Pins("dan"); !slices.Equal(got, []string{"zeta", "mid", "alpha"}) {
		t.Errorf("FAIL: a re-pin did not go last: %v", got)
	}
	if got, _ := d.Pins("eve"); !slices.Equal(got, []string{"alpha"}) {
		t.Errorf("FAIL: eve's pins are %v", got)
	}
	if got, err := d.Pins(""); err != nil || len(got) != 0 {
		t.Errorf("FAIL: no account has pins: %v, %v", got, err)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// Deleting an account takes its pins with it, so a successor of the same
// name does not inherit them.
func TestPinsGoWithTheAccount(t *testing.T) {
	d, err := Open(filepath.Join(t.TempDir(), "oltp.db"))
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { d.Close() })
	if err := d.AddAccount("dan", "pw-dan-12345", false); err != nil {
		t.Fatal(err)
	}
	if err := d.SetPin("dan", "alpha", true); err != nil {
		t.Fatal(err)
	}
	if err := d.DeleteAccount("dan"); err != nil {
		t.Fatal(err)
	}
	if got, _ := d.Pins("dan"); len(got) != 0 {
		t.Errorf("FAIL: a deleted account's pins remain: %v", got)
	}
}