|
| 1 | +import protobuf from 'protobufjs'; |
| 2 | +import upath from 'upath'; |
| 3 | +import { Package } from './package'; |
| 4 | +import { Signed } from './signed'; |
| 5 | + |
| 6 | +function protobufLoad(file: string): Promise<protobuf.Root> { |
| 7 | + const resolvedFile = upath.join(__dirname, file); |
| 8 | + return new Promise((resolve, reject) => { |
| 9 | + protobuf.load(resolvedFile, (err, root) => { |
| 10 | + if (err) { |
| 11 | + reject(err); |
| 12 | + return; |
| 13 | + } |
| 14 | + |
| 15 | + if (!root) { |
| 16 | + reject(new Error('Root is empty')); |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + resolve(root); |
| 21 | + }); |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +describe('modules/datasource/hex/v2/index', () => { |
| 26 | + describe('Signed', () => { |
| 27 | + async function encodeSigned(input: unknown): Promise<Buffer> { |
| 28 | + const message = Signed.fromJSON(input); |
| 29 | + const root = await protobufLoad('signed.proto'); |
| 30 | + const x = root.lookupType('Signed').encode(message).finish(); |
| 31 | + return Buffer.from(x); |
| 32 | + } |
| 33 | + |
| 34 | + it('roundtrip', async () => { |
| 35 | + const input = { |
| 36 | + payload: Buffer.from('foo'), |
| 37 | + signature: Buffer.from('bar'), |
| 38 | + }; |
| 39 | + const encodedBuf = await encodeSigned(input); |
| 40 | + |
| 41 | + const output = Signed.decode(encodedBuf); |
| 42 | + |
| 43 | + expect(output).toEqual(input); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('Package', () => { |
| 48 | + async function encodePackage(input: unknown): Promise<Buffer> { |
| 49 | + const message = Package.fromJSON(input); |
| 50 | + const root = await protobufLoad('package.proto'); |
| 51 | + const x = root.lookupType('Package').encode(message).finish(); |
| 52 | + return Buffer.from(x); |
| 53 | + } |
| 54 | + |
| 55 | + it('roundtrip', async () => { |
| 56 | + const input: Package = { |
| 57 | + name: 'foo', |
| 58 | + repository: 'hexpm', |
| 59 | + releases: [ |
| 60 | + { |
| 61 | + version: '1.0.0', |
| 62 | + innerChecksum: new Uint8Array(), |
| 63 | + dependencies: [], |
| 64 | + }, |
| 65 | + { |
| 66 | + version: '2.0.0', |
| 67 | + innerChecksum: new Uint8Array(), |
| 68 | + dependencies: [], |
| 69 | + }, |
| 70 | + { |
| 71 | + version: '3.0.0', |
| 72 | + innerChecksum: new Uint8Array(), |
| 73 | + dependencies: [], |
| 74 | + }, |
| 75 | + ], |
| 76 | + }; |
| 77 | + const encodedBuf = await encodePackage(input); |
| 78 | + |
| 79 | + const output = Package.decode(encodedBuf); |
| 80 | + |
| 81 | + expect(output).toMatchObject({ |
| 82 | + name: 'foo', |
| 83 | + repository: 'hexpm', |
| 84 | + releases: [ |
| 85 | + { version: '1.0.0' }, |
| 86 | + { version: '2.0.0' }, |
| 87 | + { version: '3.0.0' }, |
| 88 | + ], |
| 89 | + }); |
| 90 | + }); |
| 91 | + }); |
| 92 | +}); |
0 commit comments