|
1 | 1 | /* eslint-env mocha */
|
2 | 2 |
|
3 | 3 | import { expect } from 'aegir/chai'
|
| 4 | +import all from 'it-all' |
4 | 5 | import drain from 'it-drain'
|
5 | 6 | import { CID } from 'multiformats/cid'
|
6 | 7 | import * as raw from 'multiformats/codecs/raw'
|
7 | 8 | import { identity } from 'multiformats/hashes/identity'
|
8 | 9 | import { sha256 } from 'multiformats/hashes/sha2'
|
9 | 10 | import { IdentityBlockstore } from '../src/identity.js'
|
| 11 | +import { MemoryBlockstore } from '../src/memory.js' |
10 | 12 | import type { Blockstore } from 'interface-blockstore'
|
11 | 13 |
|
12 | 14 | describe('identity', () => {
|
13 | 15 | let blockstore: Blockstore
|
| 16 | + let child: Blockstore |
14 | 17 |
|
15 | 18 | beforeEach(() => {
|
16 | 19 | blockstore = new IdentityBlockstore()
|
| 20 | + child = new MemoryBlockstore() |
17 | 21 | })
|
18 | 22 |
|
19 | 23 | it('has an identity CID', () => {
|
@@ -56,4 +60,70 @@ describe('identity', () => {
|
56 | 60 |
|
57 | 61 | expect(blockstore.has(cid)).to.be.true()
|
58 | 62 | })
|
| 63 | + |
| 64 | + it('puts CIDs to child', async () => { |
| 65 | + const block = Uint8Array.from([0, 1, 2, 3, 4]) |
| 66 | + const multihash = await sha256.digest(block) |
| 67 | + const cid = CID.createV1(raw.code, multihash) |
| 68 | + |
| 69 | + blockstore = new IdentityBlockstore(child) |
| 70 | + |
| 71 | + await blockstore.put(cid, block) |
| 72 | + expect(child.has(cid)).to.be.true() |
| 73 | + expect(child.get(cid)).to.equalBytes(block) |
| 74 | + }) |
| 75 | + |
| 76 | + it('gets CIDs from child', async () => { |
| 77 | + const block = Uint8Array.from([0, 1, 2, 3, 4]) |
| 78 | + const multihash = await sha256.digest(block) |
| 79 | + const cid = CID.createV1(raw.code, multihash) |
| 80 | + |
| 81 | + await child.put(cid, block) |
| 82 | + |
| 83 | + blockstore = new IdentityBlockstore(child) |
| 84 | + expect(blockstore.has(cid)).to.be.true() |
| 85 | + expect(blockstore.get(cid)).to.equalBytes(block) |
| 86 | + }) |
| 87 | + |
| 88 | + it('has CIDs from child', async () => { |
| 89 | + const block = Uint8Array.from([0, 1, 2, 3, 4]) |
| 90 | + const multihash = await sha256.digest(block) |
| 91 | + const cid = CID.createV1(raw.code, multihash) |
| 92 | + |
| 93 | + await child.put(cid, block) |
| 94 | + |
| 95 | + blockstore = new IdentityBlockstore(child) |
| 96 | + expect(blockstore.has(cid)).to.be.true() |
| 97 | + }) |
| 98 | + |
| 99 | + it('deletes CIDs from child', async () => { |
| 100 | + const block = Uint8Array.from([0, 1, 2, 3, 4]) |
| 101 | + const multihash = await sha256.digest(block) |
| 102 | + const cid = CID.createV1(raw.code, multihash) |
| 103 | + |
| 104 | + await child.put(cid, block) |
| 105 | + |
| 106 | + blockstore = new IdentityBlockstore(child) |
| 107 | + expect(blockstore.has(cid)).to.be.true() |
| 108 | + |
| 109 | + await blockstore.delete(cid) |
| 110 | + |
| 111 | + expect(blockstore.has(cid)).to.be.false() |
| 112 | + }) |
| 113 | + |
| 114 | + it('gets all pairs from child', async () => { |
| 115 | + const block = Uint8Array.from([0, 1, 2, 3, 4]) |
| 116 | + const multihash = await sha256.digest(block) |
| 117 | + const cid = CID.createV1(raw.code, multihash) |
| 118 | + |
| 119 | + await child.put(cid, block) |
| 120 | + |
| 121 | + blockstore = new IdentityBlockstore(child) |
| 122 | + expect(blockstore.has(cid)).to.be.true() |
| 123 | + |
| 124 | + const result = await all(blockstore.getAll()) |
| 125 | + |
| 126 | + expect(result).to.have.lengthOf(1) |
| 127 | + expect(result[0].cid.toString()).to.equal(cid.toString()) |
| 128 | + }) |
59 | 129 | })
|
0 commit comments