Fork of github.com/did-method-plc/did-method-plc
1import { AddressInfo } from 'net'
2import PlcServer, { AppContext } from '../src'
3import Database from '../src/db'
4
5export type CloseFn = () => Promise<void>
6export type TestServerInfo = {
7 ctx: AppContext
8 url: string
9 db: Database
10 close: CloseFn
11}
12
13export const runTestServer = async (opts: {
14 dbSchema: string
15}): Promise<TestServerInfo> => {
16 const { dbSchema } = opts
17 const dbUrl = process.env.DATABASE_URL
18 if (!dbUrl) {
19 throw new Error('No postgres url provided')
20 }
21
22 const db = Database.postgres({
23 url: dbUrl,
24 schema: dbSchema,
25 })
26 await db.migrateToLatestOrThrow()
27
28 const plc = PlcServer.create({ db })
29 const plcServer = await plc.start()
30 const { port } = plcServer.address() as AddressInfo
31
32 return {
33 ctx: plc.ctx,
34 url: `http://localhost:${port}`,
35 db,
36 close: async () => {
37 await plc.destroy()
38 },
39 }
40}