about summary refs log tree commit diff
path: root/jest
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2023-06-27 20:55:46 -0500
committerGitHub <noreply@github.com>2023-06-27 20:55:46 -0500
commitf8d218e11a1e9fba81f2fc0182a4526b9d7cf39d (patch)
tree9fb5e0a81461e7daa8c1afe49e53ca8eb97a4f52 /jest
parentdce80be0759d9c3fbffd881f3cb88476db2b7fc0 (diff)
downloadvoidsky-f8d218e11a1e9fba81f2fc0182a4526b9d7cf39d.tar.zst
Simulator fixes (#918)
* Update the mock server to use the dev-env to manage the server

* Fix list testIDs

* Fix the invite test construction

* Remove leftover test hardcode
Diffstat (limited to 'jest')
-rw-r--r--jest/test-pds.ts122
1 files changed, 31 insertions, 91 deletions
diff --git a/jest/test-pds.ts b/jest/test-pds.ts
index 3c14c5531..34c3e8bc4 100644
--- a/jest/test-pds.ts
+++ b/jest/test-pds.ts
@@ -1,20 +1,9 @@
-import {AddressInfo} from 'net'
-import os from 'os'
 import net from 'net'
 import path from 'path'
 import fs from 'fs'
-import * as crypto from '@atproto/crypto'
-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 {TestPds as DevEnvTestPDS, TestNetworkNoAppView} from '@atproto/dev-env'
 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 {
   email: string
   did: string
@@ -32,81 +21,13 @@ export interface TestPDS {
 export async function createServer(
   {inviteRequired}: {inviteRequired: boolean} = {inviteRequired: false},
 ): Promise<TestPDS> {
-  const repoSigningKey = await crypto.Secp256k1Keypair.create()
-  const plcRotationKey = await crypto.Secp256k1Keypair.create()
   const port = await getPort()
-
-  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 port2 = await getPort(port + 1)
   const pdsUrl = `http://localhost:${port}`
+  const {pds, plc} = await TestNetworkNoAppView.create({
+    pds: {port, publicUrl: pdsUrl, inviteRequired},
+    plc: {port: port2},
+  })
 
   const profilePic = fs.readFileSync(
     path.join(__dirname, '..', 'assets', 'default-avatar.jpg'),
@@ -116,8 +37,8 @@ export async function createServer(
     pdsUrl,
     mocker: new Mocker(pds, pdsUrl, profilePic),
     async close() {
-      await pds.destroy()
-      await plcServer.destroy()
+      await pds.server.destroy()
+      await plc.server.destroy()
     },
   }
 }
@@ -127,7 +48,7 @@ class Mocker {
   users: Record<string, TestUser> = {}
 
   constructor(
-    public pds: PDS,
+    public pds: DevEnvTestPDS,
     public service: string,
     public profilePic: Uint8Array,
   ) {
@@ -152,7 +73,11 @@ class Mocker {
     const inviteRes = await agent.api.com.atproto.server.createInviteCode(
       {useCount: 1},
       {
-        headers: {authorization: `Basic ${btoa(`admin:${ADMIN_PASSWORD}`)}`},
+        headers: {
+          authorization: `Basic ${btoa(
+            `admin:${this.pds.ctx.cfg.adminPassword}`,
+          )}`,
+        },
         encoding: 'application/json',
       },
     )
@@ -265,6 +190,21 @@ class Mocker {
     return await agent.like(uri, cid)
   }
 
+  async createInvite(forAccount: string) {
+    const agent = new BskyAgent({service: this.agent.service})
+    await agent.api.com.atproto.server.createInviteCode(
+      {useCount: 1, forAccount},
+      {
+        headers: {
+          authorization: `Basic ${btoa(
+            `admin:${this.pds.ctx.cfg.adminPassword}`,
+          )}`,
+        },
+        encoding: 'application/json',
+      },
+    )
+  }
+
   async labelAccount(label: string, user: string) {
     const did = this.users[user]?.did
     if (!did) {
@@ -380,8 +320,8 @@ const checkAvailablePort = (port: number) =>
     })
   })
 
-async function getPort() {
-  for (let i = 3000; i < 65000; i++) {
+async function getPort(start = 3000) {
+  for (let i = start; i < 65000; i++) {
     if (await checkAvailablePort(i)) {
       return i
     }