This repository has no description
1package db
2
3import (
4 "context"
5 "fmt"
6 "slices"
7 "testing"
8
9 "github.com/bluesky-social/indigo/atproto/syntax"
10 "tangled.org/core/consts"
11)
12
13func insertRepoWithSpindle(t *testing.T, d *DB, did, name, spindle string) {
14 t.Helper()
15 if _, err := d.Exec(
16 `insert into repos (did, name, knot, rkey, at_uri, spindle) values (?, ?, 'knot.test', ?, ?, ?)`,
17 did, name, name, fmt.Sprintf("at://%s/sh.tangled.repo/%s", did, name), spindle,
18 ); err != nil {
19 t.Fatalf("insert repo %q: %v", name, err)
20 }
21}
22
23func TestRecentSpindles(t *testing.T) {
24 d := newTestDB(t)
25 const user = "did:plc:akshay"
26
27 // oldest first; "one" and "three" share a spindle
28 insertRepoWithSpindle(t, d, user, "one", "a.spindle.test")
29 insertRepoWithSpindle(t, d, user, "two", "b.spindle.test")
30 insertRepoWithSpindle(t, d, user, "three", "a.spindle.test")
31 insertRepoWithSpindle(t, d, user, "no-spindle", "")
32 if _, err := d.Exec(
33 `insert into repos (did, name, knot, rkey, at_uri) values (?, 'null-spindle', 'knot.test', 'null-spindle', ?)`,
34 user, fmt.Sprintf("at://%s/sh.tangled.repo/null-spindle", user),
35 ); err != nil {
36 t.Fatalf("insert null-spindle repo: %v", err)
37 }
38 insertRepoWithSpindle(t, d, "did:plc:someone-else", "theirs", "other.spindle.test")
39
40 got, err := RecentSpindles(context.Background(), d, syntax.DID(user))
41 if err != nil {
42 t.Fatalf("RecentSpindles: %v", err)
43 }
44
45 want := []string{"a.spindle.test", "b.spindle.test", consts.DefaultSpindle}
46 if !slices.Equal(got, want) {
47 t.Errorf("RecentSpindles = %v, want %v", got, want)
48 }
49}
50
51func TestRecentSpindlesNoRepos(t *testing.T) {
52 d := newTestDB(t)
53
54 got, err := RecentSpindles(context.Background(), d, syntax.DID("did:plc:akshay"))
55 if err != nil {
56 t.Fatalf("RecentSpindles: %v", err)
57 }
58
59 // a fresh account still gets something to pick
60 if !slices.Equal(got, []string{consts.DefaultSpindle}) {
61 t.Errorf("RecentSpindles = %v, want [%s]", got, consts.DefaultSpindle)
62 }
63}