Skip to content

Commit bb068e7

Browse files
committed
TMP: creating alice avatar blob and profile record
1 parent 4258384 commit bb068e7

File tree

1 file changed

+95
-3
lines changed

1 file changed

+95
-3
lines changed

packages/client/globalSetup.ts

+95-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import fs from 'node:fs/promises';
2+
13
import type { TestProject } from 'vitest/node';
24

35
import { CredentialManager, XRPC } from '@atcute/client';
@@ -16,8 +18,8 @@ export async function setup(project: TestProject) {
1618
handler: manager,
1719
});
1820

19-
await createAccount(rpc, 'alice.test');
20-
await createAccount(rpc, 'bob.test');
21+
await createAccount(rpc, 'alice.test', manager);
22+
await createAccount(rpc, 'bob.test', manager);
2123

2224
project.provide('testPdsUrl', network.pds.url);
2325
project.provide('testPlcUrl', network.plc.url);
@@ -27,7 +29,16 @@ export async function teardown() {
2729
await network.close();
2830
}
2931

30-
const createAccount = async (rpc: XRPC, handle: string) => {
32+
// sleep function with promise
33+
function sleep(sec: number) {
34+
return new Promise((res) => setTimeout(res, sec * 1000));
35+
}
36+
37+
const createAccount = async (
38+
rpc: XRPC,
39+
handle: string,
40+
manager: CredentialManager,
41+
) => {
3142
await rpc.call('com.atproto.server.createAccount', {
3243
data: {
3344
handle: handle,
@@ -36,6 +47,87 @@ const createAccount = async (rpc: XRPC, handle: string) => {
3647
},
3748
});
3849
console.log(`🙋 Created new account: @${handle}`);
50+
51+
// authenticate
52+
// create post
53+
await manager.login({ identifier: 'alice.test', password: 'password' });
54+
55+
const { data: post } = await rpc.call('com.atproto.repo.createRecord', {
56+
data: {
57+
repo: 'alice.test',
58+
collection: 'app.bsky.feed.post',
59+
record: {
60+
$type: 'app.bsky.feed.post',
61+
createdAt: new Date().toISOString(),
62+
text: "Hi, I'm Alice!",
63+
langs: ['en'],
64+
},
65+
},
66+
});
67+
// console.log(`* post=${JSON.stringify(post, null, 2)}`);
68+
/*
69+
* post={
70+
"uri": "at://did:plc:gcd4sqigo6mdn4njqwejknux/app.bsky.feed.post/3lik4gtsx5s2c",
71+
"cid": "bafyreieqmqwthbpxhep2v3pu4zske2gekvwomzlzjlnt5ambn2vftcbyj4",
72+
"commit": {
73+
"cid": "bafyreidxz4gbxqk4bku7g3mk46hfk3ggz7i2eb547hlicboc2xg2y6nn7y",
74+
"rev": "3lik4gtt32s2c"
75+
},
76+
"validationStatus": "valid"
77+
}
78+
*/
79+
80+
const { buffer } = await fs.readFile(
81+
'bafkreifl3xu4hsmmlov54dcblrswri3xrjm6aivrj5zvrqdmgkuyuontpm.jpeg',
82+
);
83+
const { data: blob } = await rpc.call('com.atproto.repo.uploadBlob', {
84+
headers: { 'content-type': 'image/jpeg' },
85+
data: buffer,
86+
});
87+
// console.log(`* blob=${JSON.stringify(blob, null, 2)}`);
88+
/*
89+
{
90+
"blob": {
91+
"$type": "blob",
92+
"ref": {
93+
"$link": "bafkreifl3xu4hsmmlov54dcblrswri3xrjm6aivrj5zvrqdmgkuyuontpm"
94+
},
95+
"mimeType": "image/jpeg",
96+
"size": 315488
97+
}
98+
}
99+
*/
100+
101+
const { data: profile } = await rpc.call('com.atproto.repo.createRecord', {
102+
data: {
103+
repo: 'alice.test',
104+
collection: 'app.bsky.actor.profile',
105+
record: {
106+
$type: 'app.bsky.actor.profile',
107+
avatar: blob.blob,
108+
// avatar: {
109+
// cid: blob.blob.ref.$link,
110+
// mimeType: blob.blob.mimeType,
111+
// size: blob.blob.size,
112+
// },
113+
createdAt: new Date().toISOString(),
114+
description: "I'm Alice!",
115+
displayName: 'alice',
116+
},
117+
},
118+
});
119+
console.log(`profile=${JSON.stringify(profile, null, 2)}`);
120+
121+
const { data: records } = await rpc.get('com.atproto.repo.listRecords', {
122+
params: {
123+
repo: 'alice.test',
124+
collection: 'app.bsky.actor.profile',
125+
// collection: 'app.bsky.feed.post',
126+
},
127+
});
128+
console.log(`* records=${JSON.stringify(records, null, 2)}`);
129+
130+
await sleep(60 * 10);
39131
};
40132

41133
declare module 'vitest' {

0 commit comments

Comments
 (0)