2023-03-31 20:17:26 +02:00
|
|
|
import net from 'net'
|
2023-01-24 16:06:27 +01:00
|
|
|
import path from 'path'
|
2023-04-27 19:38:23 +02:00
|
|
|
import fs from 'fs'
|
2023-10-11 00:46:27 +02:00
|
|
|
import {TestNetwork} from '@atproto/dev-env'
|
2023-08-15 18:59:49 +02:00
|
|
|
import {AtUri, BskyAgent} from '@atproto/api'
|
2023-03-31 20:17:26 +02:00
|
|
|
|
2023-01-24 16:06:27 +01:00
|
|
|
export interface TestUser {
|
|
|
|
email: string
|
|
|
|
did: string
|
|
|
|
handle: string
|
|
|
|
password: string
|
2023-03-31 20:17:26 +02:00
|
|
|
agent: BskyAgent
|
2023-01-24 16:06:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface TestPDS {
|
|
|
|
pdsUrl: string
|
2023-03-31 20:17:26 +02:00
|
|
|
mocker: Mocker
|
2023-01-24 16:06:27 +01:00
|
|
|
close: () => Promise<void>
|
|
|
|
}
|
|
|
|
|
2023-10-11 00:46:27 +02:00
|
|
|
class StringIdGenerator {
|
|
|
|
_nextId = [0]
|
|
|
|
constructor(
|
|
|
|
public _chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
|
|
|
) {}
|
|
|
|
|
|
|
|
next() {
|
|
|
|
const r = []
|
|
|
|
for (const char of this._nextId) {
|
|
|
|
r.unshift(this._chars[char])
|
|
|
|
}
|
|
|
|
this._increment()
|
|
|
|
return r.join('')
|
|
|
|
}
|
|
|
|
|
|
|
|
_increment() {
|
|
|
|
for (let i = 0; i < this._nextId.length; i++) {
|
|
|
|
const val = ++this._nextId[i]
|
|
|
|
if (val >= this._chars.length) {
|
|
|
|
this._nextId[i] = 0
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._nextId.push(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
*[Symbol.iterator]() {
|
|
|
|
while (true) {
|
|
|
|
yield this.next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ids = new StringIdGenerator()
|
|
|
|
|
2023-04-06 01:56:02 +02:00
|
|
|
export async function createServer(
|
|
|
|
{inviteRequired}: {inviteRequired: boolean} = {inviteRequired: false},
|
|
|
|
): Promise<TestPDS> {
|
2023-03-31 20:17:26 +02:00
|
|
|
const port = await getPort()
|
2023-06-28 03:55:46 +02:00
|
|
|
const port2 = await getPort(port + 1)
|
2023-12-05 21:50:56 +01:00
|
|
|
const port3 = await getPort(port2 + 1)
|
2023-03-31 20:17:26 +02:00
|
|
|
const pdsUrl = `http://localhost:${port}`
|
2023-10-11 00:46:27 +02:00
|
|
|
const id = ids.next()
|
2023-12-05 21:50:56 +01:00
|
|
|
|
2023-10-11 00:46:27 +02:00
|
|
|
const testNet = await TestNetwork.create({
|
|
|
|
pds: {
|
|
|
|
port,
|
2023-12-05 21:50:56 +01:00
|
|
|
hostname: 'localhost',
|
2023-10-11 00:46:27 +02:00
|
|
|
dbPostgresSchema: `pds_${id}`,
|
2023-12-05 21:50:56 +01:00
|
|
|
inviteRequired,
|
2023-10-11 00:46:27 +02:00
|
|
|
},
|
|
|
|
bsky: {
|
|
|
|
dbPostgresSchema: `bsky_${id}`,
|
2023-12-05 21:50:56 +01:00
|
|
|
port: port3,
|
|
|
|
publicUrl: 'http://localhost:2584',
|
2023-10-11 00:46:27 +02:00
|
|
|
},
|
2023-06-28 03:55:46 +02:00
|
|
|
plc: {port: port2},
|
|
|
|
})
|
2023-01-24 16:06:27 +01:00
|
|
|
|
2023-08-09 00:54:36 +02:00
|
|
|
const pic = fs.readFileSync(
|
2023-04-27 19:38:23 +02:00
|
|
|
path.join(__dirname, '..', 'assets', 'default-avatar.jpg'),
|
|
|
|
)
|
|
|
|
|
2023-01-24 16:06:27 +01:00
|
|
|
return {
|
|
|
|
pdsUrl,
|
2023-09-21 04:47:56 +02:00
|
|
|
mocker: new Mocker(testNet, pdsUrl, pic),
|
2023-01-24 16:06:27 +01:00
|
|
|
async close() {
|
2023-09-21 04:47:56 +02:00
|
|
|
await testNet.pds.server.destroy()
|
|
|
|
await testNet.plc.server.destroy()
|
2023-01-24 16:06:27 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 20:17:26 +02:00
|
|
|
class Mocker {
|
|
|
|
agent: BskyAgent
|
|
|
|
users: Record<string, TestUser> = {}
|
2023-01-24 16:06:27 +01:00
|
|
|
|
2023-04-27 19:38:23 +02:00
|
|
|
constructor(
|
2023-10-11 00:46:27 +02:00
|
|
|
public testNet: TestNetwork,
|
2023-04-27 19:38:23 +02:00
|
|
|
public service: string,
|
2023-08-09 00:54:36 +02:00
|
|
|
public pic: Uint8Array,
|
2023-04-27 19:38:23 +02:00
|
|
|
) {
|
2023-03-31 20:17:26 +02:00
|
|
|
this.agent = new BskyAgent({service})
|
2023-01-24 16:06:27 +01:00
|
|
|
}
|
2023-03-31 20:17:26 +02:00
|
|
|
|
2023-09-21 04:47:56 +02:00
|
|
|
get pds() {
|
|
|
|
return this.testNet.pds
|
|
|
|
}
|
|
|
|
|
2023-10-11 00:46:27 +02:00
|
|
|
get bsky() {
|
|
|
|
return this.testNet.bsky
|
|
|
|
}
|
|
|
|
|
2023-09-21 04:47:56 +02:00
|
|
|
get plc() {
|
|
|
|
return this.testNet.plc
|
|
|
|
}
|
|
|
|
|
2023-03-31 20:17:26 +02:00
|
|
|
// NOTE
|
|
|
|
// deterministic date generator
|
|
|
|
// we use this to ensure the mock dataset is always the same
|
|
|
|
// which is very useful when testing
|
|
|
|
*dateGen() {
|
|
|
|
let start = 1657846031914
|
|
|
|
while (true) {
|
|
|
|
yield new Date(start).toISOString()
|
|
|
|
start += 1e3
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async createUser(name: string) {
|
|
|
|
const agent = new BskyAgent({service: this.agent.service})
|
2023-04-06 01:56:02 +02:00
|
|
|
|
|
|
|
const inviteRes = await agent.api.com.atproto.server.createInviteCode(
|
|
|
|
{useCount: 1},
|
|
|
|
{
|
2023-10-11 00:46:27 +02:00
|
|
|
headers: this.pds.adminAuthHeaders('admin'),
|
2023-04-06 01:56:02 +02:00
|
|
|
encoding: 'application/json',
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2023-03-31 20:17:26 +02:00
|
|
|
const email = `fake${Object.keys(this.users).length + 1}@fake.com`
|
|
|
|
const res = await agent.createAccount({
|
2023-04-06 01:56:02 +02:00
|
|
|
inviteCode: inviteRes.data.code,
|
2023-03-31 20:17:26 +02:00
|
|
|
email,
|
|
|
|
handle: name + '.test',
|
2023-01-24 16:06:27 +01:00
|
|
|
password: 'hunter2',
|
|
|
|
})
|
2023-04-27 19:38:23 +02:00
|
|
|
await agent.upsertProfile(async () => {
|
2023-08-09 00:54:36 +02:00
|
|
|
const blob = await agent.uploadBlob(this.pic, {
|
2023-04-27 19:38:23 +02:00
|
|
|
encoding: 'image/jpeg',
|
|
|
|
})
|
|
|
|
return {
|
|
|
|
displayName: name,
|
|
|
|
avatar: blob.data.blob,
|
|
|
|
}
|
|
|
|
})
|
2023-03-31 20:17:26 +02:00
|
|
|
this.users[name] = {
|
|
|
|
did: res.data.did,
|
|
|
|
email,
|
|
|
|
handle: name + '.test',
|
|
|
|
password: 'hunter2',
|
|
|
|
agent: agent,
|
|
|
|
}
|
2023-01-24 16:06:27 +01:00
|
|
|
}
|
|
|
|
|
2023-03-31 20:17:26 +02:00
|
|
|
async follow(a: string, b: string) {
|
|
|
|
await this.users[a].agent.follow(this.users[b].did)
|
|
|
|
}
|
|
|
|
|
|
|
|
async generateStandardGraph() {
|
|
|
|
await this.createUser('alice')
|
|
|
|
await this.createUser('bob')
|
|
|
|
await this.createUser('carla')
|
|
|
|
|
|
|
|
await this.users.alice.agent.upsertProfile(() => ({
|
|
|
|
displayName: 'Alice',
|
|
|
|
description: 'Test user 1',
|
|
|
|
}))
|
|
|
|
|
|
|
|
await this.users.bob.agent.upsertProfile(() => ({
|
|
|
|
displayName: 'Bob',
|
|
|
|
description: 'Test user 2',
|
|
|
|
}))
|
|
|
|
|
|
|
|
await this.users.carla.agent.upsertProfile(() => ({
|
|
|
|
displayName: 'Carla',
|
|
|
|
description: 'Test user 3',
|
|
|
|
}))
|
|
|
|
|
|
|
|
await this.follow('alice', 'bob')
|
|
|
|
await this.follow('alice', 'carla')
|
|
|
|
await this.follow('bob', 'alice')
|
|
|
|
await this.follow('bob', 'carla')
|
|
|
|
await this.follow('carla', 'alice')
|
|
|
|
await this.follow('carla', 'bob')
|
2023-01-24 16:06:27 +01:00
|
|
|
}
|
2023-04-27 19:38:23 +02:00
|
|
|
|
|
|
|
async createPost(user: string, text: string) {
|
|
|
|
const agent = this.users[user]?.agent
|
|
|
|
if (!agent) {
|
|
|
|
throw new Error(`Not a user: ${user}`)
|
|
|
|
}
|
|
|
|
return await agent.post({
|
|
|
|
text,
|
2023-08-04 07:08:30 +02:00
|
|
|
langs: ['en'],
|
2023-04-27 19:38:23 +02:00
|
|
|
createdAt: new Date().toISOString(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-09 00:54:36 +02:00
|
|
|
async createImagePost(user: string, text: string) {
|
|
|
|
const agent = this.users[user]?.agent
|
|
|
|
if (!agent) {
|
|
|
|
throw new Error(`Not a user: ${user}`)
|
|
|
|
}
|
|
|
|
const blob = await agent.uploadBlob(this.pic, {
|
|
|
|
encoding: 'image/jpeg',
|
|
|
|
})
|
|
|
|
return await agent.post({
|
|
|
|
text,
|
|
|
|
langs: ['en'],
|
|
|
|
embed: {
|
|
|
|
$type: 'app.bsky.embed.images',
|
|
|
|
images: [{image: blob.data.blob, alt: ''}],
|
|
|
|
},
|
|
|
|
createdAt: new Date().toISOString(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-27 19:38:23 +02:00
|
|
|
async createQuotePost(
|
|
|
|
user: string,
|
|
|
|
text: string,
|
|
|
|
{uri, cid}: {uri: string; cid: string},
|
|
|
|
) {
|
|
|
|
const agent = this.users[user]?.agent
|
|
|
|
if (!agent) {
|
|
|
|
throw new Error(`Not a user: ${user}`)
|
|
|
|
}
|
|
|
|
return await agent.post({
|
|
|
|
text,
|
|
|
|
embed: {$type: 'app.bsky.embed.record', record: {uri, cid}},
|
2023-08-04 07:08:30 +02:00
|
|
|
langs: ['en'],
|
2023-04-27 19:38:23 +02:00
|
|
|
createdAt: new Date().toISOString(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async createReply(
|
|
|
|
user: string,
|
|
|
|
text: string,
|
|
|
|
{uri, cid}: {uri: string; cid: string},
|
|
|
|
) {
|
|
|
|
const agent = this.users[user]?.agent
|
|
|
|
if (!agent) {
|
|
|
|
throw new Error(`Not a user: ${user}`)
|
|
|
|
}
|
|
|
|
return await agent.post({
|
|
|
|
text,
|
|
|
|
reply: {root: {uri, cid}, parent: {uri, cid}},
|
2023-08-04 07:08:30 +02:00
|
|
|
langs: ['en'],
|
2023-04-27 19:38:23 +02:00
|
|
|
createdAt: new Date().toISOString(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async like(user: string, {uri, cid}: {uri: string; cid: string}) {
|
|
|
|
const agent = this.users[user]?.agent
|
|
|
|
if (!agent) {
|
|
|
|
throw new Error(`Not a user: ${user}`)
|
|
|
|
}
|
|
|
|
return await agent.like(uri, cid)
|
|
|
|
}
|
|
|
|
|
2023-09-21 04:47:56 +02:00
|
|
|
async createFeed(user: string, rkey: string, posts: string[]) {
|
2023-08-15 18:59:49 +02:00
|
|
|
const agent = this.users[user]?.agent
|
|
|
|
if (!agent) {
|
|
|
|
throw new Error(`Not a user: ${user}`)
|
|
|
|
}
|
2023-09-21 04:47:56 +02:00
|
|
|
const fgUri = AtUri.make(
|
2023-08-15 18:59:49 +02:00
|
|
|
this.users[user].did,
|
|
|
|
'app.bsky.feed.generator',
|
2023-09-21 04:47:56 +02:00
|
|
|
rkey,
|
2023-08-15 18:59:49 +02:00
|
|
|
)
|
2023-09-21 04:47:56 +02:00
|
|
|
const fg1 = await this.testNet.createFeedGen({
|
|
|
|
[fgUri.toString()]: async () => {
|
|
|
|
return {
|
|
|
|
encoding: 'application/json',
|
|
|
|
body: {
|
|
|
|
feed: posts.slice(0, 30).map(uri => ({post: uri})),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
2023-08-15 18:59:49 +02:00
|
|
|
const avatarRes = await agent.api.com.atproto.repo.uploadBlob(this.pic, {
|
|
|
|
encoding: 'image/png',
|
|
|
|
})
|
|
|
|
return await agent.api.app.bsky.feed.generator.create(
|
2023-09-21 04:47:56 +02:00
|
|
|
{repo: this.users[user].did, rkey},
|
2023-08-15 18:59:49 +02:00
|
|
|
{
|
2023-09-21 04:47:56 +02:00
|
|
|
did: fg1.did,
|
|
|
|
displayName: rkey,
|
2023-08-15 18:59:49 +02:00
|
|
|
description: 'all my fav stuff',
|
|
|
|
avatar: avatarRes.data.blob,
|
|
|
|
createdAt: new Date().toISOString(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-06-28 03:55:46 +02:00
|
|
|
async createInvite(forAccount: string) {
|
|
|
|
const agent = new BskyAgent({service: this.agent.service})
|
|
|
|
await agent.api.com.atproto.server.createInviteCode(
|
|
|
|
{useCount: 1, forAccount},
|
|
|
|
{
|
2023-10-11 00:46:27 +02:00
|
|
|
headers: this.pds.adminAuthHeaders('admin'),
|
2023-06-28 03:55:46 +02:00
|
|
|
encoding: 'application/json',
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-04-27 19:38:23 +02:00
|
|
|
async labelAccount(label: string, user: string) {
|
|
|
|
const did = this.users[user]?.did
|
|
|
|
if (!did) {
|
|
|
|
throw new Error(`Invalid user: ${user}`)
|
|
|
|
}
|
2023-10-11 00:46:27 +02:00
|
|
|
const ctx = this.bsky.ctx
|
2023-04-27 19:38:23 +02:00
|
|
|
if (!ctx) {
|
2023-10-11 00:46:27 +02:00
|
|
|
throw new Error('Invalid appview')
|
2023-04-27 19:38:23 +02:00
|
|
|
}
|
2023-10-11 00:46:27 +02:00
|
|
|
const labelSrvc = ctx.services.label(ctx.db.getPrimary())
|
|
|
|
await labelSrvc.createLabels([
|
|
|
|
{
|
|
|
|
src: ctx.cfg.labelerDid,
|
|
|
|
uri: did,
|
|
|
|
cid: '',
|
|
|
|
val: label,
|
|
|
|
neg: false,
|
|
|
|
cts: new Date().toISOString(),
|
|
|
|
},
|
|
|
|
])
|
2023-04-27 19:38:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async labelProfile(label: string, user: string) {
|
|
|
|
const agent = this.users[user]?.agent
|
|
|
|
const did = this.users[user]?.did
|
|
|
|
if (!did) {
|
|
|
|
throw new Error(`Invalid user: ${user}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
const profile = await agent.app.bsky.actor.profile.get({
|
|
|
|
repo: user + '.test',
|
|
|
|
rkey: 'self',
|
|
|
|
})
|
|
|
|
|
2023-10-11 00:46:27 +02:00
|
|
|
const ctx = this.bsky.ctx
|
2023-04-27 19:38:23 +02:00
|
|
|
if (!ctx) {
|
2023-10-11 00:46:27 +02:00
|
|
|
throw new Error('Invalid appview')
|
2023-04-27 19:38:23 +02:00
|
|
|
}
|
2023-10-11 00:46:27 +02:00
|
|
|
const labelSrvc = ctx.services.label(ctx.db.getPrimary())
|
|
|
|
await labelSrvc.createLabels([
|
|
|
|
{
|
|
|
|
src: ctx.cfg.labelerDid,
|
|
|
|
uri: profile.uri,
|
|
|
|
cid: profile.cid,
|
|
|
|
val: label,
|
|
|
|
neg: false,
|
|
|
|
cts: new Date().toISOString(),
|
|
|
|
},
|
|
|
|
])
|
2023-04-27 19:38:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async labelPost(label: string, {uri, cid}: {uri: string; cid: string}) {
|
2023-10-11 00:46:27 +02:00
|
|
|
const ctx = this.bsky.ctx
|
2023-04-27 19:38:23 +02:00
|
|
|
if (!ctx) {
|
2023-10-11 00:46:27 +02:00
|
|
|
throw new Error('Invalid appview')
|
2023-04-27 19:38:23 +02:00
|
|
|
}
|
2023-10-11 00:46:27 +02:00
|
|
|
const labelSrvc = ctx.services.label(ctx.db.getPrimary())
|
|
|
|
await labelSrvc.createLabels([
|
|
|
|
{
|
|
|
|
src: ctx.cfg.labelerDid,
|
|
|
|
uri,
|
|
|
|
cid,
|
|
|
|
val: label,
|
|
|
|
neg: false,
|
|
|
|
cts: new Date().toISOString(),
|
|
|
|
},
|
|
|
|
])
|
2023-04-27 19:38:23 +02:00
|
|
|
}
|
2023-05-11 23:08:21 +02:00
|
|
|
|
|
|
|
async createMuteList(user: string, name: string): Promise<string> {
|
|
|
|
const res = await this.users[user]?.agent.app.bsky.graph.list.create(
|
|
|
|
{repo: this.users[user]?.did},
|
|
|
|
{
|
|
|
|
purpose: 'app.bsky.graph.defs#modlist',
|
|
|
|
name,
|
|
|
|
createdAt: new Date().toISOString(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await this.users[user]?.agent.app.bsky.graph.muteActorList({
|
|
|
|
list: res.uri,
|
|
|
|
})
|
|
|
|
return res.uri
|
|
|
|
}
|
|
|
|
|
|
|
|
async addToMuteList(owner: string, list: string, subject: string) {
|
|
|
|
await this.users[owner]?.agent.app.bsky.graph.listitem.create(
|
|
|
|
{repo: this.users[owner]?.did},
|
|
|
|
{
|
|
|
|
list,
|
|
|
|
subject,
|
|
|
|
createdAt: new Date().toISOString(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2023-01-24 16:06:27 +01:00
|
|
|
}
|
|
|
|
|
2023-03-31 20:17:26 +02:00
|
|
|
const checkAvailablePort = (port: number) =>
|
|
|
|
new Promise(resolve => {
|
|
|
|
const server = net.createServer()
|
|
|
|
server.unref()
|
|
|
|
server.on('error', () => resolve(false))
|
|
|
|
server.listen({port}, () => {
|
|
|
|
server.close(() => {
|
|
|
|
resolve(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-06-28 03:55:46 +02:00
|
|
|
async function getPort(start = 3000) {
|
|
|
|
for (let i = start; i < 65000; i++) {
|
2023-03-31 20:17:26 +02:00
|
|
|
if (await checkAvailablePort(i)) {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new Error('Unable to find an available port')
|
2023-01-24 16:06:27 +01:00
|
|
|
}
|