This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathsharness-t0053-dag.js
182 lines (150 loc) · 7.94 KB
/
sharness-t0053-dag.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* eslint-env mocha */
import { expect } from 'aegir/chai'
import { getDescribe, getIt } from '../utils/mocha.js'
import { base64pad } from 'multiformats/bases/base64'
import { base58btc } from 'multiformats/bases/base58'
import { CID } from 'multiformats'
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
*/
/**
* @param {Factory} factory
* @param {object} options
*/
export function testDagSharnessT0053 (factory, options) {
const describe = getDescribe(options)
const it = getIt(options)
describe('.dag (sharness-t0053-dag)', () => {
/** @type {import('ipfs-core-types').IPFS} */
let ipfs
before(async () => { ipfs = (await factory.spawn()).api })
after(() => factory.clean())
/** @type {CID} */
let hash1
/** @type {CID} */
let hash2
/** @type {CID} */
let hash3
/** @type {CID} */
let hash4
/** @type {Uint8Array} */
let ipldObject
/** @type {Uint8Array} */
let ipldObjectDagCbor
/** @type {Uint8Array} */
let ipldObjectDagPb
/** @type {Uint8Array} */
let ipldObjectDagJson
const ipldHash = 'bafyreiblwimnjbqcdoeafiobk6q27jcw64ew7n2fmmhdpldd63edmjecde'
const ipldDagCborHash = 'bafyreieculsmrexh3ty5jentbvuku452o27mst4h2tq2rb2zntqhgcstji'
const ipldDagJsonHash = 'baguqeerajwksxu3lxpomdwxvosl542zl3xknhjgxtq3277gafrhl6vdw5tcq'
const ipldDagPbHash = 'bafybeibazl2z4vqp2tmwcfag6wirmtpnomxknqcgrauj7m2yisrz3qjbom'
before(async () => {
hash1 = (await ipfs.add({ content: 'foo\n', path: 'file1' })).cid
hash2 = (await ipfs.add({ content: 'bar\n', path: 'file2' })).cid
hash3 = (await ipfs.add({ content: 'baz\n', path: 'file3' })).cid
hash4 = (await ipfs.add({ content: 'qux\n', path: 'file4' })).cid
ipldObject = new TextEncoder().encode(`{"hello":"world","cats":[{"/":"${hash1}"},{"water":{"/":"${hash2}"}}],"magic":{"/":"${hash3}"},"sub":{"dict":"ionary","beep":[0,"bop"]}}`)
ipldObjectDagCbor = base64pad.decode('MomREYXRhRQABAgMEZUxpbmtzgA==')
ipldObjectDagPb = base64pad.decode('MCgUAAQIDBA==')
ipldObjectDagJson = new TextEncoder().encode('{"Data":{"/":{"bytes":"AAECAwQ"}},"Links":[]}')
})
it('sanity check', () => {
expect(hash1.toString()).to.equal('QmYNmQKp6SuaVrpgWRsPTgCQCnpxUYGq76YEKBXuj2N4H6')
expect(hash2.toString()).to.equal('QmTz3oc4gdpRMKP2sdGUPZTAGRngqjsi99BPoztyP53JMM')
expect(hash3.toString()).to.equal('QmWLdkp93sNxGRjnFHPaYg8tCQ35NBY3XPn6KiETd3Z4WR')
expect(hash4.toString()).to.equal('QmZCoKN8vvRbxfn4BMG9678UQTSUwPXRJsRA9jnjoucHUj')
})
it('can add an ipld object using defaults (dag-json to dag-cbor)', async () => {
// dag-json is default on CLI, force it to interpret our bytes here
const cid = await ipfs.dag.put(ipldObject, { inputCodec: 'dag-json' })
expect(cid.toString()).to.equal(ipldHash)
})
it('can add an ipld object using dag-json to dag-json', async () => {
const cid = await ipfs.dag.put(ipldObject, { inputCodec: 'dag-json', storeCodec: 'dag-json' })
expect(cid.toString()).to.equal('baguqeera6gviseelmbzn2ugoddo5vulxlshqs3kw5ymgsb6w4cabnoh4ldpa')
})
it('can add an ipld object using dag-json to dag-cbor', async () => {
const cid = await ipfs.dag.put(ipldObject, { inputCodec: 'dag-json', storeCodec: 'dag-cbor' })
expect(cid.toString()).to.equal(ipldHash)
})
// this is not testing what the upstream sharness is testing since we're converting it locally
// and not asking the CLI for it, but it's included for completeness
it('can add an ipld object using cid-base=base58btc', async () => {
const cid = await ipfs.dag.put(ipldObject, { inputCodec: 'dag-json' })
expect(cid.toString(base58btc)).to.equal('zdpuAoN1XJ3GsrxEzMuCbRKZzRUVJekJUCbPVgCgE4D9yYqVi')
})
// (1) dag-cbor input
it('can add a dag-cbor input block stored as dag-cbor', async () => {
const cid = await ipfs.dag.put(ipldObjectDagCbor, { inputCodec: 'dag-cbor', storeCodec: 'dag-cbor' })
expect(cid.toString()).to.equal(ipldDagCborHash)
})
it('can add a dag-cbor input block stored as dag-pb', async () => {
const cid = await ipfs.dag.put(ipldObjectDagCbor, { inputCodec: 'dag-cbor', storeCodec: 'dag-pb' })
expect(cid.toString()).to.equal(ipldDagPbHash)
})
it('can add a dag-cbor input block stored as dag-json', async () => {
const cid = await ipfs.dag.put(ipldObjectDagCbor, { inputCodec: 'dag-cbor', storeCodec: 'dag-json' })
expect(cid.toString()).to.equal(ipldDagJsonHash)
})
// (2) dag-json input
it('can add a dag-json input block stored as dag-cbor', async () => {
const cid = await ipfs.dag.put(ipldObjectDagJson, { inputCodec: 'dag-json', storeCodec: 'dag-cbor' })
expect(cid.toString()).to.equal(ipldDagCborHash)
})
it('can add a dag-json input block stored as dag-pb', async () => {
const cid = await ipfs.dag.put(ipldObjectDagJson, { inputCodec: 'dag-json', storeCodec: 'dag-pb' })
expect(cid.toString()).to.equal(ipldDagPbHash)
})
it('can add a dag-json input block stored as dag-json', async () => {
const cid = await ipfs.dag.put(ipldObjectDagJson, { inputCodec: 'dag-json', storeCodec: 'dag-json' })
expect(cid.toString()).to.equal(ipldDagJsonHash)
})
// (3) dag-pb input
it('can add a dag-pb input block stored as dag-cbor', async () => {
const cid = await ipfs.dag.put(ipldObjectDagPb, { inputCodec: 'dag-pb', storeCodec: 'dag-cbor' })
expect(cid.toString()).to.equal(ipldDagCborHash)
})
it('can add a dag-pb input block stored as dag-pb', async () => {
const cid = await ipfs.dag.put(ipldObjectDagPb, { inputCodec: 'dag-pb', storeCodec: 'dag-pb' })
expect(cid.toString()).to.equal(ipldDagPbHash)
})
it('can add a dag-pb input block stored as dag-json', async () => {
const cid = await ipfs.dag.put(ipldObjectDagPb, { inputCodec: 'dag-pb', storeCodec: 'dag-json' })
expect(cid.toString()).to.equal(ipldDagJsonHash)
})
it('can get dag-cbor, dag-json, dag-pb blocks as dag-json', async () => {
const resultCbor = await ipfs.dag.get(CID.parse(ipldDagCborHash))
const resultJson = await ipfs.dag.get(CID.parse(ipldDagJsonHash))
const resultPb = await ipfs.dag.get(CID.parse(ipldDagPbHash))
expect(resultCbor).to.deep.equal(resultJson)
expect(resultCbor).to.deep.equal(resultPb)
})
/*
This is illustrative only - it's not testing anything meaningful. It's supposed to test
`outputCodec` which isn't supported for the http client or core since we get the decoded JS
form of the node. But this test code as it's written is doing the encode locally and
asserting on that .. which is just testing the codec.
it('can get dag-pb block transcoded as dag-cbor', async () => {
const { value } = await ipfs.dag.get(CID.parse(ipldDagPbHash), { outputCodec: 'dag-cbor' })
const block = await Block.encode({ value, codec: dagCbor, hasher: sha256 })
expect(bytes.toHex(block.cid.multihash.bytes)).to.equal('122082a2e4c892e7dcf1d491b30d68aa73ba76bec94f87d4e1a887596ce0730a534a')
})
*/
// Skipped: 'dag put and dag get transcodings match' - tests the round-trip of the above
it('resolving sub-objects works', async () => {
let result = await ipfs.dag.get(CID.parse(ipldHash), { path: 'hello' })
expect(result.value).to.equal('world')
result = await ipfs.dag.get(CID.parse(ipldHash), { path: 'sub' })
expect(result.value).to.deep.equal({ beep: [0, 'bop'], dict: 'ionary' })
result = await ipfs.dag.get(CID.parse(ipldHash), { path: 'sub/beep' })
expect(result.value).to.deep.equal([0, 'bop'])
result = await ipfs.dag.get(CID.parse(ipldHash), { path: 'sub/beep/0' })
expect(result.value).to.equal(0)
result = await ipfs.dag.get(CID.parse(ipldHash), { path: 'sub/beep/1' })
expect(result.value).to.equal('bop')
})
// Skipped: 'traversals using /ipld/ work' - not implemented here, yet?
// Skipped additional pin, resolve and other tests
})
}