-
Notifications
You must be signed in to change notification settings - Fork 573
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename to ozone, db migrations, add to dev-env & pds cfg
- Loading branch information
Showing
372 changed files
with
298 additions
and
1,260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.