forked from bluesky-social/atproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
follows.test.ts
218 lines (181 loc) · 6.42 KB
/
follows.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import AtpAgent from '@atproto/api'
import {
runTestServer,
forSnapshot,
CloseFn,
paginateAll,
processAll,
stripViewer,
} from '../_util'
import { SeedClient } from '../seeds/client'
import followsSeed from '../seeds/follows'
describe('pds follow views', () => {
let agent: AtpAgent
let close: CloseFn
let sc: SeedClient
// account dids, for convenience
let alice: string
beforeAll(async () => {
const server = await runTestServer({
dbPostgresSchema: 'views_follows',
})
close = server.close
agent = new AtpAgent({ service: server.url })
const pdsAgent = new AtpAgent({ service: server.pdsUrl })
sc = new SeedClient(pdsAgent)
await followsSeed(sc)
await processAll(server)
alice = sc.dids.alice
})
afterAll(async () => {
await close()
})
// TODO(bsky) blocks followers by actor takedown via labels
// TODO(bsky) blocks follows by actor takedown via labels
it('fetches followers', async () => {
const aliceFollowers = await agent.api.app.bsky.graph.getFollowers(
{ actor: sc.dids.alice },
{ headers: sc.getHeaders(alice, true) },
)
expect(forSnapshot(aliceFollowers.data)).toMatchSnapshot()
const bobFollowers = await agent.api.app.bsky.graph.getFollowers(
{ actor: sc.dids.bob },
{ headers: sc.getHeaders(alice, true) },
)
expect(forSnapshot(bobFollowers.data)).toMatchSnapshot()
const carolFollowers = await agent.api.app.bsky.graph.getFollowers(
{ actor: sc.dids.carol },
{ headers: sc.getHeaders(alice, true) },
)
expect(forSnapshot(carolFollowers.data)).toMatchSnapshot()
const danFollowers = await agent.api.app.bsky.graph.getFollowers(
{ actor: sc.dids.dan },
{ headers: sc.getHeaders(alice, true) },
)
expect(forSnapshot(danFollowers.data)).toMatchSnapshot()
const eveFollowers = await agent.api.app.bsky.graph.getFollowers(
{ actor: sc.dids.eve },
{ headers: sc.getHeaders(alice, true) },
)
expect(forSnapshot(eveFollowers.data)).toMatchSnapshot()
})
it('fetches followers by handle', async () => {
const byDid = await agent.api.app.bsky.graph.getFollowers(
{ actor: sc.dids.alice },
{ headers: sc.getHeaders(alice, true) },
)
const byHandle = await agent.api.app.bsky.graph.getFollowers(
{ actor: sc.accounts[alice].handle },
{ headers: sc.getHeaders(alice, true) },
)
expect(byHandle.data).toEqual(byDid.data)
})
it('paginates followers', async () => {
const results = (results) => results.flatMap((res) => res.followers)
const paginator = async (cursor?: string) => {
const res = await agent.api.app.bsky.graph.getFollowers(
{
actor: sc.dids.alice,
cursor,
limit: 2,
},
{ headers: sc.getHeaders(alice, true) },
)
return res.data
}
const paginatedAll = await paginateAll(paginator)
paginatedAll.forEach((res) =>
expect(res.followers.length).toBeLessThanOrEqual(2),
)
const full = await agent.api.app.bsky.graph.getFollowers(
{ actor: sc.dids.alice },
{ headers: sc.getHeaders(alice, true) },
)
expect(full.data.followers.length).toEqual(4)
expect(results(paginatedAll)).toEqual(results([full.data]))
})
it('fetches followers unauthed', async () => {
const { data: authed } = await agent.api.app.bsky.graph.getFollowers(
{ actor: sc.dids.alice },
{ headers: sc.getHeaders(alice, true) },
)
const { data: unauthed } = await agent.api.app.bsky.graph.getFollowers({
actor: sc.dids.alice,
})
expect(unauthed.followers.length).toBeGreaterThan(0)
expect(unauthed.followers).toEqual(authed.followers.map(stripViewer))
})
it('fetches follows', async () => {
const aliceFollowers = await agent.api.app.bsky.graph.getFollows(
{ actor: sc.dids.alice },
{ headers: sc.getHeaders(alice, true) },
)
expect(forSnapshot(aliceFollowers.data)).toMatchSnapshot()
const bobFollowers = await agent.api.app.bsky.graph.getFollows(
{ actor: sc.dids.bob },
{ headers: sc.getHeaders(alice, true) },
)
expect(forSnapshot(bobFollowers.data)).toMatchSnapshot()
const carolFollowers = await agent.api.app.bsky.graph.getFollows(
{ actor: sc.dids.carol },
{ headers: sc.getHeaders(alice, true) },
)
expect(forSnapshot(carolFollowers.data)).toMatchSnapshot()
const danFollowers = await agent.api.app.bsky.graph.getFollows(
{ actor: sc.dids.dan },
{ headers: sc.getHeaders(alice, true) },
)
expect(forSnapshot(danFollowers.data)).toMatchSnapshot()
const eveFollowers = await agent.api.app.bsky.graph.getFollows(
{ actor: sc.dids.eve },
{ headers: sc.getHeaders(alice, true) },
)
expect(forSnapshot(eveFollowers.data)).toMatchSnapshot()
})
it('fetches follows by handle', async () => {
const byDid = await agent.api.app.bsky.graph.getFollows(
{ actor: sc.dids.alice },
{ headers: sc.getHeaders(alice, true) },
)
const byHandle = await agent.api.app.bsky.graph.getFollows(
{ actor: sc.accounts[alice].handle },
{ headers: sc.getHeaders(alice, true) },
)
expect(byHandle.data).toEqual(byDid.data)
})
it('paginates follows', async () => {
const results = (results) => results.flatMap((res) => res.follows)
const paginator = async (cursor?: string) => {
const res = await agent.api.app.bsky.graph.getFollows(
{
actor: sc.dids.alice,
cursor,
limit: 2,
},
{ headers: sc.getHeaders(alice, true) },
)
return res.data
}
const paginatedAll = await paginateAll(paginator)
paginatedAll.forEach((res) =>
expect(res.follows.length).toBeLessThanOrEqual(2),
)
const full = await agent.api.app.bsky.graph.getFollows(
{ actor: sc.dids.alice },
{ headers: sc.getHeaders(alice, true) },
)
expect(full.data.follows.length).toEqual(4)
expect(results(paginatedAll)).toEqual(results([full.data]))
})
it('fetches follows unauthed', async () => {
const { data: authed } = await agent.api.app.bsky.graph.getFollows(
{ actor: sc.dids.alice },
{ headers: sc.getHeaders(alice, true) },
)
const { data: unauthed } = await agent.api.app.bsky.graph.getFollows({
actor: sc.dids.alice,
})
expect(unauthed.follows.length).toBeGreaterThan(0)
expect(unauthed.follows).toEqual(authed.follows.map(stripViewer))
})
})