Fork of github.com/did-method-plc/did-method-plc
1import { cidForCbor, DAY } from '@atproto/common'
2import { Secp256k1Keypair } from '@atproto/crypto'
3import * as plc from '@did-plc/lib'
4import { Kysely } from 'kysely'
5import { Database } from '../../src'
6
7describe('did-locks migration', () => {
8 let db: Database
9 let rawDb: Kysely<any>
10
11 beforeAll(async () => {
12 const dbUrl = process.env.DATABASE_URL
13 if (!dbUrl) {
14 throw new Error('No postgres url provided')
15 }
16 db = Database.postgres({
17 url: dbUrl,
18 schema: 'migration_did_locks',
19 })
20
21 await db.migrateToOrThrow('_20230223T215019669Z')
22 rawDb = db.db
23 })
24
25 afterAll(async () => {
26 await db.close()
27 })
28
29 const dids: string[] = []
30
31 it('fills the database with some operations', async () => {
32 const ops: any[] = []
33 for (let i = 0; i < 100; i++) {
34 const signingKey = await Secp256k1Keypair.create()
35 const recoveryKey = await Secp256k1Keypair.create()
36 const { op, did } = await plc.createOp({
37 signingKey: signingKey.did(),
38 rotationKeys: [recoveryKey.did()],
39 handle: `user${i}.test`,
40 pds: 'https://example.com',
41 signer: recoveryKey,
42 })
43 const cid = await cidForCbor(op)
44 const randomOffset = Math.floor(Math.random() * DAY * 60)
45 const time = new Date(Date.now() - randomOffset).toISOString()
46 ops.push({
47 did,
48 operation: JSON.stringify(op),
49 cid: cid.toString(),
50 nullified: 0,
51 createdAt: time,
52 })
53 dids.push(did)
54 const op2 = await plc.updateHandleOp(op, recoveryKey, `user${i}-2.test`)
55 const cid2 = await cidForCbor(op2)
56 ops.push({
57 did,
58 operation: JSON.stringify(op2),
59 cid: cid2.toString(),
60 nullified: 0,
61 createdAt: new Date().toISOString(),
62 })
63 }
64 await rawDb.insertInto('operations').values(ops).execute()
65 })
66
67 it('migrates', async () => {
68 await db.migrateToOrThrow('_20230406T174552885Z')
69 })
70
71 it('correctly filled in dids', async () => {
72 const migrated = await rawDb.selectFrom('dids').selectAll().execute()
73 const sorted = migrated.map((row) => row.did).sort()
74 expect(sorted).toEqual(dids.sort())
75 })
76})