From 194383d7db61ab0185c46cfaead7599c3a75a265 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 9 Apr 2024 19:20:00 +0100 Subject: [PATCH] chore: fix code --- packages/blockstore-core/src/identity.ts | 12 +-- .../blockstore-core/test/identity.spec.ts | 79 ++++++++++++++++--- 2 files changed, 74 insertions(+), 17 deletions(-) diff --git a/packages/blockstore-core/src/identity.ts b/packages/blockstore-core/src/identity.ts index da4e8776..fdebafe4 100644 --- a/packages/blockstore-core/src/identity.ts +++ b/packages/blockstore-core/src/identity.ts @@ -17,7 +17,7 @@ export class IdentityBlockstore extends BaseBlockstore { } put (key: CID, block: Uint8Array): Await { - if (key.code === IDENTITY_CODEC) { + if (key.multihash.code === IDENTITY_CODEC) { return key } @@ -29,7 +29,7 @@ export class IdentityBlockstore extends BaseBlockstore { } get (key: CID): Await { - if (key.code === IDENTITY_CODEC) { + if (key.multihash.code === IDENTITY_CODEC) { return key.multihash.digest } @@ -41,7 +41,7 @@ export class IdentityBlockstore extends BaseBlockstore { } has (key: CID): Await { - if (key.code === IDENTITY_CODEC) { + if (key.multihash.code === IDENTITY_CODEC) { return true } @@ -62,9 +62,11 @@ export class IdentityBlockstore extends BaseBlockstore { } } - * getAll (options?: AbortOptions): AwaitIterable { + getAll (options?: AbortOptions): AwaitIterable { if (this.child != null) { - yield * this.child.getAll(options) + return this.child.getAll(options) } + + return [] } } diff --git a/packages/blockstore-core/test/identity.spec.ts b/packages/blockstore-core/test/identity.spec.ts index 2913dd99..9d64cb0a 100644 --- a/packages/blockstore-core/test/identity.spec.ts +++ b/packages/blockstore-core/test/identity.spec.ts @@ -1,6 +1,7 @@ /* eslint-env mocha */ import { expect } from 'aegir/chai' +import all from 'it-all' import drain from 'it-drain' import { CID } from 'multiformats/cid' import * as raw from 'multiformats/codecs/raw' @@ -28,18 +29,6 @@ describe('identity', () => { expect(blockstore.get(cid)).to.equalBytes(block) }) - it('retrieves CIDs from child', async () => { - const block = Uint8Array.from([0, 1, 2, 3, 4]) - const multihash = await sha256.digest(block) - const cid = CID.createV1(raw.code, multihash) - - await child.put(cid, block) - - blockstore = new IdentityBlockstore(child) - expect(blockstore.has(cid)).to.be.true() - expect(blockstore.get(cid)).to.equalBytes(block) - }) - it('does not have a non-identity CID', async () => { const block = Uint8Array.from([0, 1, 2, 3, 4]) const multihash = await sha256.digest(block) @@ -71,4 +60,70 @@ describe('identity', () => { expect(blockstore.has(cid)).to.be.true() }) + + it('puts CIDs to child', async () => { + const block = Uint8Array.from([0, 1, 2, 3, 4]) + const multihash = await sha256.digest(block) + const cid = CID.createV1(raw.code, multihash) + + blockstore = new IdentityBlockstore(child) + + await blockstore.put(cid, block) + expect(child.has(cid)).to.be.true() + expect(child.get(cid)).to.equalBytes(block) + }) + + it('gets CIDs from child', async () => { + const block = Uint8Array.from([0, 1, 2, 3, 4]) + const multihash = await sha256.digest(block) + const cid = CID.createV1(raw.code, multihash) + + await child.put(cid, block) + + blockstore = new IdentityBlockstore(child) + expect(blockstore.has(cid)).to.be.true() + expect(blockstore.get(cid)).to.equalBytes(block) + }) + + it('has CIDs from child', async () => { + const block = Uint8Array.from([0, 1, 2, 3, 4]) + const multihash = await sha256.digest(block) + const cid = CID.createV1(raw.code, multihash) + + await child.put(cid, block) + + blockstore = new IdentityBlockstore(child) + expect(blockstore.has(cid)).to.be.true() + }) + + it('deletes CIDs from child', async () => { + const block = Uint8Array.from([0, 1, 2, 3, 4]) + const multihash = await sha256.digest(block) + const cid = CID.createV1(raw.code, multihash) + + await child.put(cid, block) + + blockstore = new IdentityBlockstore(child) + expect(blockstore.has(cid)).to.be.true() + + await blockstore.delete(cid) + + expect(blockstore.has(cid)).to.be.false() + }) + + it('gets all pairs from child', async () => { + const block = Uint8Array.from([0, 1, 2, 3, 4]) + const multihash = await sha256.digest(block) + const cid = CID.createV1(raw.code, multihash) + + await child.put(cid, block) + + blockstore = new IdentityBlockstore(child) + expect(blockstore.has(cid)).to.be.true() + + const result = await all(blockstore.getAll()) + + expect(result).to.have.lengthOf(1) + expect(result[0].cid.toString()).to.equal(cid.toString()) + }) })