tests for identity.find() etc

This commit is contained in:
Andre Staltz 2023-07-10 15:41:28 +03:00
parent a442a26c2a
commit a3fcb641eb
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
2 changed files with 81 additions and 2 deletions

View File

@ -51,8 +51,10 @@
"typescript": "^5.1.3"
},
"scripts": {
"build": "tsc --build --clean && tsc --build",
"test": "node --test",
"clean-check": "tsc --build --clean",
"prepublishOnly": "npm run clean-check && tsc --build",
"postpublish": "npm run clean-check",
"test": "npm run clean-check && node --test",
"format-code": "prettier --write \"(lib|test)/**/*.js\"",
"format-code-staged": "pretty-quick --staged --pattern \"(lib|test)/**/*.js\"",
"coverage": "c8 --reporter=lcov npm run test"

View File

@ -36,6 +36,7 @@ test('identity.create() with just "domain"', async (t) => {
})
test('identity.create() with "keypair" and "domain"', async (t) => {
rimraf.sync(DIR)
const keypair = Keypair.generate('ed25519', 'alice')
const peer = SecretStack({ appKey: caps.shse })
@ -62,3 +63,79 @@ test('identity.create() with "keypair" and "domain"', async (t) => {
await p(peer.close)()
})
test('identity.find() can find', async (t) => {
rimraf.sync(DIR)
const keypair = Keypair.generate('ed25519', 'alice')
const domain = 'person'
const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.use(require('ssb-box'))
.call(null, { keypair, path: DIR })
await peer.db.loaded()
const identity = await p(peer.db.identity.create)({ keypair, domain })
assert.ok(identity, 'identity created')
const found = await p(peer.db.identity.find)({ keypair, domain })
assert.equal(found, identity, 'found')
await p(peer.close)()
})
test('identity.findOrCreate() can find', async (t) => {
rimraf.sync(DIR)
const keypair = Keypair.generate('ed25519', 'alice')
const domain = 'person'
const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.use(require('ssb-box'))
.call(null, { keypair, path: DIR })
await peer.db.loaded()
const identity = await p(peer.db.identity.create)({ keypair, domain })
assert.ok(identity, 'identity created')
const found = await p(peer.db.identity.findOrCreate)({ keypair, domain })
assert.equal(found, identity, 'found')
await p(peer.close)()
})
test('identity.findOrCreate() can create', async (t) => {
rimraf.sync(DIR)
const keypair = Keypair.generate('ed25519', 'alice')
const domain = 'person'
const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.use(require('ssb-box'))
.call(null, { keypair, path: DIR })
await peer.db.loaded()
let gotError = false
await p(peer.db.identity.find)({ keypair, domain }).catch(err => {
assert.equal(err.cause, 'ENOENT');
gotError = true
})
assert.ok(gotError, 'identity not found')
const identity = await p(peer.db.identity.findOrCreate)({ keypair, domain })
assert.ok(identity, 'identity created')
const msg = peer.db.get(identity)
assert.equal(msg.data.add, keypair.public, 'msg.data.add')
assert.equal(msg.metadata.identity, 'self', 'msg.metadata.identity')
assert.equal(msg.metadata.identityTips, null, 'msg.metadata.identityTips')
assert.deepEqual(
Object.keys(msg.metadata.tangles),
[],
'msg.metadata.tangles'
)
assert.equal(msg.pubkey, keypair.public, 'msg.pubkey')
await p(peer.close)()
})