95 lines
3.0 KiB
TypeScript
95 lines
3.0 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { BlockHeader } from '../src/block-header';
|
|
import { Bytes } from '../src/bytes';
|
|
|
|
describe('Success cases', () => {
|
|
// Test cases with different block header representations
|
|
type BlockHeaderCase = {
|
|
raw: Bytes;
|
|
representations: {
|
|
bytes: Bytes;
|
|
hex: string;
|
|
};
|
|
};
|
|
|
|
const testCases: BlockHeaderCase[] = [
|
|
{
|
|
raw: Bytes.fromHex(
|
|
'0000602093ea70b294aeef69db85981bd387285824fc5af732e487010000000000000000135576e1ba9cae5509d60b4fbb9e66612f35b5ee8db264cb12612d46cc8db60b5e34a164d46b021864abbb1d',
|
|
),
|
|
representations: {
|
|
bytes: Bytes.fromHex(
|
|
'0000602093ea70b294aeef69db85981bd387285824fc5af732e487010000000000000000135576e1ba9cae5509d60b4fbb9e66612f35b5ee8db264cb12612d46cc8db60b5e34a164d46b021864abbb1d',
|
|
),
|
|
hex: '0000602093ea70b294aeef69db85981bd387285824fc5af732e487010000000000000000135576e1ba9cae5509d60b4fbb9e66612f35b5ee8db264cb12612d46cc8db60b5e34a164d46b021864abbb1d',
|
|
},
|
|
},
|
|
];
|
|
|
|
// Test from methods with their respective types
|
|
const fromMethods = [
|
|
{ name: 'fromBytes', key: 'bytes' },
|
|
{ name: 'fromHex', key: 'hex' },
|
|
] as const;
|
|
|
|
fromMethods.forEach(({ name, key }) => {
|
|
it.each(testCases)(`${name}()`, ({ raw, representations }) => {
|
|
const blockHeader = BlockHeader[name](representations[key]);
|
|
expect(blockHeader.toRaw()).toEqual(raw);
|
|
});
|
|
});
|
|
|
|
// Test generic from method
|
|
it.each(testCases)('from(Uint8Array)', ({ raw, representations }) => {
|
|
const blockHeader = BlockHeader.from(representations.bytes);
|
|
expect(blockHeader.toRaw()).toEqual(raw);
|
|
});
|
|
|
|
it.each(testCases)('from(string)', ({ raw, representations }) => {
|
|
const blockHeader = BlockHeader.from(representations.hex);
|
|
expect(blockHeader.toRaw()).toEqual(raw);
|
|
});
|
|
|
|
// Test to methods with their respective types
|
|
const toMethods = [
|
|
{ name: 'toBytes', key: 'bytes' },
|
|
{ name: 'toHex', key: 'hex' },
|
|
] as const;
|
|
|
|
toMethods.forEach(({ name, key }) => {
|
|
it.each(testCases)(`${name}()`, ({ raw, representations }) => {
|
|
const blockHeader = BlockHeader.fromRaw(raw);
|
|
expect(blockHeader[name]()).toStrictEqual(representations[key]);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('Error cases', () => {
|
|
const invalidCases = [
|
|
{
|
|
method: 'fromBytes',
|
|
input: new Uint8Array(79), // Wrong length (should be 80 bytes)
|
|
},
|
|
{
|
|
method: 'fromHex',
|
|
input: 'invalid_hex',
|
|
},
|
|
{
|
|
method: 'fromHex',
|
|
input: '00'.repeat(79), // Wrong length (should be 80 bytes / 160 hex chars)
|
|
},
|
|
];
|
|
|
|
invalidCases.forEach(({ method, input }) => {
|
|
it(`${method}() throws on invalid input`, () => {
|
|
expect(() => BlockHeader[method](input)).toThrow();
|
|
});
|
|
});
|
|
|
|
it('isValid() returns false for invalid inputs', () => {
|
|
expect(BlockHeader.isValid(new Uint8Array(79))).toBe(false);
|
|
expect(BlockHeader.isValid('invalid_hex')).toBe(false);
|
|
expect(BlockHeader.isValid('00'.repeat(79))).toBe(false);
|
|
});
|
|
});
|