This repository has no description
1package db
2
3import (
4 "testing"
5
6 "github.com/bluesky-social/indigo/atproto/syntax"
7)
8
9func subjectsOf(t *testing.T, d *DB, repoDid syntax.DID) []syntax.DID {
10 t.Helper()
11 rows, err := d.ListCollaboratorsByRepoDid(repoDid)
12 if err != nil {
13 t.Fatalf("ListCollaboratorsByRepoDid: %v", err)
14 }
15 out := make([]syntax.DID, 0, len(rows))
16 for _, r := range rows {
17 out = append(out, r.Subject)
18 }
19 return out
20}
21
22func TestAddKnotCollaborator_PersistsAndIsIdempotent(t *testing.T) {
23 d := newTestDB(t)
24 repo := syntax.DID("did:plc:repo")
25 bob := syntax.DID("did:plc:bob")
26
27 if err := d.AddKnotCollaborator(repo, bob); err != nil {
28 t.Fatalf("add: %v", err)
29 }
30 if err := d.AddKnotCollaborator(repo, bob); err != nil {
31 t.Fatalf("re-add: %v", err)
32 }
33
34 got := subjectsOf(t, d, repo)
35 if len(got) != 1 || got[0] != bob {
36 t.Fatalf("collaborators = %v, want exactly [bob]", got)
37 }
38}
39
40func TestDeleteRepoCollaboratorBySubjectRepo(t *testing.T) {
41 d := newTestDB(t)
42 repo := syntax.DID("did:plc:repo")
43 bob := syntax.DID("did:plc:bob")
44 carol := syntax.DID("did:plc:carol")
45
46 if err := d.AddKnotCollaborator(repo, bob); err != nil {
47 t.Fatalf("add bob: %v", err)
48 }
49 if err := d.AddKnotCollaborator(repo, carol); err != nil {
50 t.Fatalf("add carol: %v", err)
51 }
52
53 if err := d.DeleteRepoCollaboratorBySubjectRepo(bob, repo); err != nil {
54 t.Fatalf("delete bob: %v", err)
55 }
56 got := subjectsOf(t, d, repo)
57 if len(got) != 1 || got[0] != carol {
58 t.Fatalf("after removing bob, collaborators = %v, want [carol]", got)
59 }
60
61 if err := d.DeleteRepoCollaboratorBySubjectRepo(bob, repo); err != nil {
62 t.Fatalf("idempotent delete: %v", err)
63 }
64}
65
66func TestKnotCollaborator_NoCollisionAcrossReposAndSubjects(t *testing.T) {
67 d := newTestDB(t)
68 repoA := syntax.DID("did:plc:repoA")
69 repoB := syntax.DID("did:plc:repoB")
70 bob := syntax.DID("did:plc:bob")
71 carol := syntax.DID("did:plc:carol")
72
73 for _, c := range []struct{ repo, subj syntax.DID }{
74 {repoA, bob}, {repoB, bob}, {repoA, carol},
75 } {
76 if err := d.AddKnotCollaborator(c.repo, c.subj); err != nil {
77 t.Fatalf("add %s/%s: %v", c.repo, c.subj, err)
78 }
79 }
80
81 if got := subjectsOf(t, d, repoA); len(got) != 2 {
82 t.Errorf("repoA collaborators = %v, want bob+carol", got)
83 }
84 if got := subjectsOf(t, d, repoB); len(got) != 1 || got[0] != bob {
85 t.Errorf("repoB collaborators = %v, want [bob]", got)
86 }
87
88 if err := d.DeleteRepoCollaboratorBySubjectRepo(bob, repoA); err != nil {
89 t.Fatalf("delete bob@repoA: %v", err)
90 }
91 if got := subjectsOf(t, d, repoB); len(got) != 1 || got[0] != bob {
92 t.Errorf("repoB after removing bob@repoA = %v, want still [bob]", got)
93 }
94}