-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathglobalSetup.ts
More file actions
93 lines (78 loc) · 2.47 KB
/
globalSetup.ts
File metadata and controls
93 lines (78 loc) · 2.47 KB
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
import fs from 'node:fs/promises';
import type { TestProject } from 'vitest/node';
import { CredentialManager, XRPC } from '@atcute/client';
import { TestNetwork } from '@atproto/dev-env';
declare module 'vitest' {
export interface ProvidedContext {
testPdsUrl: string;
testPlcUrl: string;
}
}
let network: TestNetwork;
export async function setup(project: TestProject) {
network = await TestNetwork.create({});
console.log(
`🌐 Created test network:\n- pds: ${network.pds.url}\n- plc: ${network.plc.url}`,
);
const manager = new CredentialManager({ service: network.pds.url });
const rpc = new XRPC({
handler: manager,
});
await createAccount(rpc, 'alice.test');
await createAccount(rpc, 'bob.test');
await manager.login({ identifier: 'alice.test', password: 'password' });
await createProfileRecord(rpc, 'alice.test');
await createSamplePosts(rpc, 'alice.test');
await manager.login({ identifier: 'bob.test', password: 'password' });
await createProfileRecord(rpc, 'bob.test');
await createSamplePosts(rpc, 'bob.test');
project.provide('testPdsUrl', network.pds.url);
project.provide('testPlcUrl', network.plc.url);
}
export async function teardown() {
await network.close();
}
const createAccount = async (rpc: XRPC, handle: string) => {
await rpc.call('com.atproto.server.createAccount', {
data: {
handle: handle,
email: `${handle}@example.com`,
password: 'password',
},
});
console.log(`🙋 Created new account: @${handle}`);
};
async function createProfileRecord(rpc: XRPC, handle: string) {
const imageBuffer = await fs.readFile('alice-avatar.jpeg');
const { data: blob } = await rpc.call('com.atproto.repo.uploadBlob', {
headers: { 'content-type': 'image/jpeg' },
data: imageBuffer,
});
await rpc.call('com.atproto.repo.createRecord', {
data: {
repo: handle,
collection: 'app.bsky.actor.profile',
record: {
$type: 'app.bsky.actor.profile',
avatar: blob.blob,
createdAt: new Date().toISOString(),
description: "I'm Alice!",
displayName: 'alice',
},
},
});
}
async function createSamplePosts(rpc: XRPC, handle: string) {
await rpc.call('com.atproto.repo.createRecord', {
data: {
repo: handle,
collection: 'app.bsky.feed.post',
record: {
$type: 'app.bsky.feed.post',
createdAt: new Date().toISOString(),
text: `Hi, I'm ${handle}!`,
langs: ['en'],
},
},
});
}