Update the mock server to use the dev-env to manage the server

This commit is contained in:
Paul Frazee
2023-06-27 20:17:37 -05:00
parent dce80be075
commit b2b0f77fe5
3 changed files with 2595 additions and 1381 deletions
+17 -91
View File
@@ -1,20 +1,9 @@
import {AddressInfo} from 'net'
import os from 'os'
import net from 'net' import net from 'net'
import path from 'path' import path from 'path'
import fs from 'fs' import fs from 'fs'
import * as crypto from '@atproto/crypto' import {TestPds as DevEnvTestPDS, TestNetworkNoAppView} from '@atproto/dev-env'
import {PDS, ServerConfig, Database, MemoryBlobStore} from '@atproto/pds'
import * as plc from '@did-plc/lib'
import {PlcServer, Database as PlcDatabase} from '@did-plc/server'
import {BskyAgent} from '@atproto/api' import {BskyAgent} from '@atproto/api'
const ADMIN_PASSWORD = 'admin-pass'
const SECOND = 1000
const MINUTE = SECOND * 60
const HOUR = MINUTE * 60
const DAY = HOUR * 24
export interface TestUser { export interface TestUser {
email: string email: string
did: string did: string
@@ -32,81 +21,14 @@ export interface TestPDS {
export async function createServer( export async function createServer(
{inviteRequired}: {inviteRequired: boolean} = {inviteRequired: false}, {inviteRequired}: {inviteRequired: boolean} = {inviteRequired: false},
): Promise<TestPDS> { ): Promise<TestPDS> {
const repoSigningKey = await crypto.Secp256k1Keypair.create()
const plcRotationKey = await crypto.Secp256k1Keypair.create()
const port = await getPort() const port = await getPort()
const port2 = await getPort(port + 1)
const plcDb = PlcDatabase.mock()
const plcServer = PlcServer.create({db: plcDb})
const plcListener = await plcServer.start()
const plcPort = (plcListener.address() as AddressInfo).port
const plcUrl = `http://localhost:${plcPort}`
const recoveryKey = (await crypto.Secp256k1Keypair.create()).did()
const plcClient = new plc.Client(plcUrl)
const serverDid = await plcClient.createDid({
signingKey: repoSigningKey.did(),
rotationKeys: [recoveryKey, plcRotationKey.did()],
handle: 'localhost',
pds: `http://localhost:${port}`,
signer: plcRotationKey,
})
const blobstoreLoc = path.join(os.tmpdir(), crypto.randomStr(5, 'base32'))
const cfg = new ServerConfig({
debugMode: true,
version: '0.0.0',
scheme: 'http',
hostname: 'localhost',
port,
serverDid,
recoveryKey,
adminPassword: ADMIN_PASSWORD,
inviteRequired,
didPlcUrl: plcUrl,
didCacheMaxTTL: DAY,
didCacheStaleTTL: HOUR,
jwtSecret: 'jwt-secret',
availableUserDomains: ['.test'],
appUrlPasswordReset: 'app://forgot-password',
emailNoReplyAddress: 'noreply@blueskyweb.xyz',
publicUrl: `http://localhost:${port}`,
imgUriSalt: '9dd04221f5755bce5f55f47464c27e1e',
imgUriKey:
'f23ecd142835025f42c3db2cf25dd813956c178392760256211f9d315f8ab4d8',
dbPostgresUrl: process.env.DB_POSTGRES_URL,
blobstoreLocation: `${blobstoreLoc}/blobs`,
blobstoreTmp: `${blobstoreLoc}/tmp`,
maxSubscriptionBuffer: 200,
repoBackfillLimitMs: HOUR,
userInviteInterval: 1,
labelerDid: 'did:example:labeler',
labelerKeywords: {},
})
const db =
cfg.dbPostgresUrl !== undefined
? Database.postgres({
url: cfg.dbPostgresUrl,
schema: cfg.dbPostgresSchema,
})
: Database.memory()
await db.migrateToLatestOrThrow()
const blobstore = new MemoryBlobStore()
const pds = PDS.create({
db,
blobstore,
repoSigningKey,
plcRotationKey,
config: cfg,
})
await pds.start()
const pdsUrl = `http://localhost:${port}` const pdsUrl = `http://localhost:${port}`
console.log({port})
const {pds, plc} = await TestNetworkNoAppView.create({
pds: {port, publicUrl: pdsUrl, inviteRequired},
plc: {port: port2},
})
const profilePic = fs.readFileSync( const profilePic = fs.readFileSync(
path.join(__dirname, '..', 'assets', 'default-avatar.jpg'), path.join(__dirname, '..', 'assets', 'default-avatar.jpg'),
@@ -116,8 +38,8 @@ export async function createServer(
pdsUrl, pdsUrl,
mocker: new Mocker(pds, pdsUrl, profilePic), mocker: new Mocker(pds, pdsUrl, profilePic),
async close() { async close() {
await pds.destroy() await pds.server.destroy()
await plcServer.destroy() await plc.server.destroy()
}, },
} }
} }
@@ -127,7 +49,7 @@ class Mocker {
users: Record<string, TestUser> = {} users: Record<string, TestUser> = {}
constructor( constructor(
public pds: PDS, public pds: DevEnvTestPDS,
public service: string, public service: string,
public profilePic: Uint8Array, public profilePic: Uint8Array,
) { ) {
@@ -152,7 +74,11 @@ class Mocker {
const inviteRes = await agent.api.com.atproto.server.createInviteCode( const inviteRes = await agent.api.com.atproto.server.createInviteCode(
{useCount: 1}, {useCount: 1},
{ {
headers: {authorization: `Basic ${btoa(`admin:${ADMIN_PASSWORD}`)}`}, headers: {
authorization: `Basic ${btoa(
`admin:${this.pds.ctx.cfg.adminPassword}`,
)}`,
},
encoding: 'application/json', encoding: 'application/json',
}, },
) )
@@ -380,8 +306,8 @@ const checkAvailablePort = (port: number) =>
}) })
}) })
async function getPort() { async function getPort(start = 3000) {
for (let i = 3000; i < 65000; i++) { for (let i = start; i < 65000; i++) {
if (await checkAvailablePort(i)) { if (await checkAvailablePort(i)) {
return i return i
} }
+2 -1
View File
@@ -142,7 +142,8 @@
"zod": "^3.20.2" "zod": "^3.20.2"
}, },
"devDependencies": { "devDependencies": {
"@atproto/pds": "^0.1.10", "@atproto/dev-env": "^0.2.2",
"@atproto/pds": "^0.2.0-beta.2",
"@babel/core": "^7.20.0", "@babel/core": "^7.20.0",
"@babel/preset-env": "^7.20.0", "@babel/preset-env": "^7.20.0",
"@babel/runtime": "^7.20.0", "@babel/runtime": "^7.20.0",
+2576 -1289
View File
File diff suppressed because it is too large Load Diff