Skip to content

Commit

Permalink
rename to ozone, db migrations, add to dev-env & pds cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
dholms committed Dec 19, 2023
1 parent b8af648 commit df4d40b
Show file tree
Hide file tree
Showing 372 changed files with 298 additions and 1,260 deletions.
1 change: 1 addition & 0 deletions packages/dev-env/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@atproto/crypto": "workspace:^",
"@atproto/identity": "workspace:^",
"@atproto/lexicon": "workspace:^",
"@atproto/ozone": "workspace:^",
"@atproto/pds": "workspace:^",
"@atproto/syntax": "workspace:^",
"@atproto/xrpc-server": "workspace:^",
Expand Down
21 changes: 19 additions & 2 deletions packages/dev-env/src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ import { TestServerParams } from './types'
import { TestPlc } from './plc'
import { TestPds } from './pds'
import { TestBsky } from './bsky'
import { TestOzone } from './ozone'
import { mockNetworkUtilities } from './util'
import { TestNetworkNoAppView } from './network-no-appview'

const ADMIN_USERNAME = 'admin'
const ADMIN_PASSWORD = 'admin-pass'

export class TestNetwork extends TestNetworkNoAppView {
constructor(public plc: TestPlc, public pds: TestPds, public bsky: TestBsky) {
constructor(
public plc: TestPlc,
public pds: TestPds,
public bsky: TestBsky,
public ozone?: TestOzone,
) {
super(plc, pds)
}

Expand All @@ -30,6 +36,16 @@ export class TestNetwork extends TestNetworkNoAppView {

const plc = await TestPlc.create(params.plc ?? {})

let ozone: TestOzone | undefined = undefined
if (params.ozone?.enabled) {
ozone = await TestOzone.create({
plcUrl: plc.url,
dbPostgresSchema: `ozone_${dbPostgresSchema}`,
dbPrimaryPostgresUrl: dbPostgresUrl,
...params.ozone,
})
}

const bskyPort = params.bsky?.port ?? (await getPort())
const pdsPort = params.pds?.port ?? (await getPort())
const bsky = await TestBsky.create({
Expand All @@ -48,7 +64,8 @@ export class TestNetwork extends TestNetworkNoAppView {
didPlcUrl: plc.url,
bskyAppViewUrl: bsky.url,
bskyAppViewDid: bsky.ctx.cfg.serverDid,
bskyAppViewModeration: true,
modServiceUrl: ozone?.url ?? 'https://admin.invalid',
modServiceDid: ozone?.ctx.cfg.serverDid ?? 'did:example:admin_invalid',
...params.pds,
})

Expand Down
117 changes: 117 additions & 0 deletions packages/dev-env/src/ozone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import getPort from 'get-port'
import * as ui8 from 'uint8arrays'
import * as ozone from '@atproto/ozone'
import { DAY, HOUR, MINUTE, SECOND } from '@atproto/common-web'
import { AtpAgent } from '@atproto/api'
import { Secp256k1Keypair } from '@atproto/crypto'
import { Client as PlcClient } from '@did-plc/lib'
import { OzoneConfig } from './types'
import { ADMIN_PASSWORD, MOD_PASSWORD, TRIAGE_PASSWORD } from './const'

export class TestOzone {
constructor(
public url: string,
public port: number,
public server: ozone.OzoneService,
) {}

static async create(cfg: OzoneConfig): Promise<TestOzone> {
const serviceKeypair = await Secp256k1Keypair.create()
const plcClient = new PlcClient(cfg.plcUrl)

const port = cfg.port || (await getPort())
const url = `http://localhost:${port}`
const serverDid = await plcClient.createDid({
signingKey: serviceKeypair.did(),
rotationKeys: [serviceKeypair.did()],
handle: 'bsky.test',
pds: `http://localhost:${port}`,
signer: serviceKeypair,
})

const config = new ozone.ServerConfig({
version: '0.0.0',
port,
didPlcUrl: cfg.plcUrl,
publicUrl: 'https://bsky.public.url',
serverDid,
didCacheStaleTTL: HOUR,
didCacheMaxTTL: DAY,
labelCacheStaleTTL: 30 * SECOND,
labelCacheMaxTTL: MINUTE,
...cfg,
// Each test suite gets its own lock id for the repo subscription
adminPassword: ADMIN_PASSWORD,
moderatorPassword: MOD_PASSWORD,
triagePassword: TRIAGE_PASSWORD,
labelerDid: 'did:example:labeler',
feedGenDid: 'did:example:feedGen',
rateLimitsEnabled: false,
})

// shared across server, ingester, and indexer in order to share pool, avoid too many pg connections.
const db = new ozone.Database({
schema: cfg.dbPostgresSchema,
url: cfg.dbPrimaryPostgresUrl,
poolSize: 10,
})

// Separate migration db in case migration changes some connection state that we need in the tests, e.g. "alter database ... set ..."
const migrationDb = new ozone.Database({
schema: cfg.dbPostgresSchema,
url: cfg.dbPrimaryPostgresUrl,
})
if (cfg.migration) {
await migrationDb.migrateToOrThrow(cfg.migration)
} else {
await migrationDb.migrateToLatestOrThrow()
}
await migrationDb.close()

// api server
const server = ozone.OzoneService.create({
db,
config,
signingKey: serviceKeypair,
})

await server.start()

return new TestOzone(url, port, server)
}

get ctx(): ozone.AppContext {
return this.server.ctx
}

getClient() {
return new AtpAgent({ service: this.url })
}

adminAuth(role: 'admin' | 'moderator' | 'triage' = 'admin'): string {
const password =
role === 'triage'
? this.ctx.cfg.triagePassword
: role === 'moderator'
? this.ctx.cfg.moderatorPassword
: this.ctx.cfg.adminPassword
return (
'Basic ' +
ui8.toString(ui8.fromString(`admin:${password}`, 'utf8'), 'base64pad')
)
}

adminAuthHeaders(role?: 'admin' | 'moderator' | 'triage') {
return {
authorization: this.adminAuth(role),
}
}

async processAll() {
await Promise.all([this.ctx.backgroundQueue.processAll()])
}

async close() {
await this.server.destroy()
}
}
9 changes: 9 additions & 0 deletions packages/dev-env/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as pds from '@atproto/pds'
import * as bsky from '@atproto/bsky'
import * as ozone from '@atproto/ozone'
import { ImageInvalidator } from '@atproto/bsky/src/image/invalidator'

export type PlcConfig = {
Expand All @@ -25,10 +26,18 @@ export type BskyConfig = Partial<bsky.ServerConfig> & {
ingester?: Partial<bsky.IngesterConfig>
}

export type OzoneConfig = Partial<ozone.ServerConfig> & {
enabled?: boolean
plcUrl: string
dbPrimaryPostgresUrl: string
migration?: string
}

export type TestServerParams = {
dbPostgresUrl: string
dbPostgresSchema: string
pds: Partial<PdsConfig>
plc: Partial<PlcConfig>
bsky: Partial<BskyConfig>
ozone: Partial<OzoneConfig>
}
Loading

0 comments on commit df4d40b

Please sign in to comment.