Skip to content

Commit

Permalink
test: add cid tests (#967)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cafe137 authored Oct 24, 2024
1 parent 7c5662b commit 8d63c6f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/utils/cid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export function convertReferenceToCid(reference: Reference | string, type: 'feed
}

export function convertCidToReference(cid: string): DecodedCID {
const bytes = Binary.base32ToUint8Array(cid.toLowerCase())
const codec = bytes[2]
const bytes = Binary.base32ToUint8Array(cid.toUpperCase().slice(1))
const codec = bytes[1]

if (!CODEC_TABLE[codec]) {
throw new Error('Unknown codec')
Expand Down
12 changes: 7 additions & 5 deletions src/utils/expose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@ export { getCollectionSize } from './collection'
export { getFolderSize } from './collection.node'

export {
Bytes,
FlexBytes,
assertBytes,
assertFlexBytes,
Bytes,
bytesAtOffset,
bytesEqual,
FlexBytes,
flexBytesAtOffset,
isBytes,
isFlexBytes,
} from './bytes'

export {
HexString,
PrefixedHexString,
assertHexString,
assertPrefixedHexString,
bytesToHex,
HexString,
hexToBytes,
intToHex,
isHexString,
makeHexString,
PrefixedHexString,
} from './hex'

export {
capitalizeAddressERC55,
EthAddress,
capitalizeAddressERC55,
ethToSwarmAddress,
fromLittleEndian,
isHexEthAddress,
Expand All @@ -52,3 +52,5 @@ export {
} from './stamps'

export { approximateOverheadForRedundancyLevel, getRedundancyStat, getRedundancyStats } from './redundancy'

export { convertCidToReference, convertReferenceToCid } from './cid'
23 changes: 23 additions & 0 deletions test/unit/utils/cid.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Utils } from '../../../src'

describe('cid', () => {
it('should convert cid', () => {
expect(
Utils.convertReferenceToCid('ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338', 'manifest'),
).toBe('bah5acgzazjrvpieogf6rl3cwb7xtjzgel6hrt4a4g4vkody5u4v7u7y2im4a')

expect(
Utils.convertReferenceToCid('ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338', 'feed'),
).toBe('bah5qcgzazjrvpieogf6rl3cwb7xtjzgel6hrt4a4g4vkody5u4v7u7y2im4a')

expect(Utils.convertCidToReference('bah5acgzazjrvpieogf6rl3cwb7xtjzgel6hrt4a4g4vkody5u4v7u7y2im4a')).toStrictEqual({
type: 'manifest',
reference: 'ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338',
})

expect(Utils.convertCidToReference('bah5qcgzazjrvpieogf6rl3cwb7xtjzgel6hrt4a4g4vkody5u4v7u7y2im4a')).toStrictEqual({
type: 'feed',
reference: 'ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338',
})
})
})

0 comments on commit 8d63c6f

Please sign in to comment.