Draft Inverse Bloom Filter
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { Bytes } from '~/src/bytes';
|
||||
import { Script } from '~/src/script';
|
||||
|
||||
describe('Success cases', () => {
|
||||
// Test cases with different script representations
|
||||
type ScriptCase = {
|
||||
raw: Uint8Array;
|
||||
representations: {
|
||||
bytes: Uint8Array;
|
||||
asm: string;
|
||||
hex: string;
|
||||
};
|
||||
};
|
||||
|
||||
const testCases: ScriptCase[] = [
|
||||
{
|
||||
raw: Bytes.fromHex(
|
||||
'76a914876fbb82ec05caa6af7a3b5e5a983aae6c6cc6d688ac',
|
||||
).toBytes(),
|
||||
representations: {
|
||||
bytes: Bytes.fromHex(
|
||||
'76a914876fbb82ec05caa6af7a3b5e5a983aae6c6cc6d688ac',
|
||||
).toBytes(),
|
||||
asm: 'OP_DUP\nOP_HASH160\nOP_PUSHBYTES_20\n0x876fbb82ec05caa6af7a3b5e5a983aae6c6cc6d6\nOP_EQUALVERIFY\nOP_CHECKSIG',
|
||||
hex: '76a914876fbb82ec05caa6af7a3b5e5a983aae6c6cc6d688ac',
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
// Test from methods with their respective types
|
||||
const fromMethods = [
|
||||
{ name: 'fromBytes', key: 'bytes' },
|
||||
{ name: 'fromASM', key: 'asm' },
|
||||
{ name: 'fromHex', key: 'hex' },
|
||||
] as const;
|
||||
|
||||
fromMethods.forEach(({ name, key }) => {
|
||||
it.each(testCases)(`${name}()`, ({ raw, representations }) => {
|
||||
const script = Script[name](representations[key]);
|
||||
expect(script.toRaw()).toStrictEqual(raw);
|
||||
});
|
||||
});
|
||||
|
||||
// Test generic from method
|
||||
it.each(testCases)('from(Uint8Array)', ({ raw, representations }) => {
|
||||
const script = Script.from(representations.bytes);
|
||||
expect(script.toRaw()).toStrictEqual(raw);
|
||||
});
|
||||
|
||||
it.each(testCases)('from(string)', ({ raw, representations }) => {
|
||||
const script = Script.from(representations.hex);
|
||||
expect(script.toRaw()).toStrictEqual(raw);
|
||||
});
|
||||
|
||||
// Test to methods with their respective types
|
||||
const toMethods = [
|
||||
{ name: 'toBytes', key: 'bytes' },
|
||||
{ name: 'toASM', key: 'asm' },
|
||||
{ name: 'toHex', key: 'hex' },
|
||||
] as const;
|
||||
|
||||
toMethods.forEach(({ name, key }) => {
|
||||
it.each(testCases)(`${name}()`, ({ raw, representations }) => {
|
||||
const script = Script.fromRaw(raw);
|
||||
expect(script[name]()).toStrictEqual(representations[key]);
|
||||
});
|
||||
});
|
||||
|
||||
// Test fromRaw specifically
|
||||
it.each(testCases)('fromRaw() creates correct instance', ({ raw }) => {
|
||||
const script = Script.fromRaw(raw);
|
||||
expect(script.toRaw()).toStrictEqual(raw);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Error cases', () => {
|
||||
const invalidCases = [
|
||||
{
|
||||
method: 'fromASM',
|
||||
input: 'OP_INVALID_OPCODE',
|
||||
},
|
||||
{
|
||||
method: 'fromHex',
|
||||
input: 'INVALID_HEX',
|
||||
},
|
||||
];
|
||||
|
||||
invalidCases.forEach(({ method, input }) => {
|
||||
it(`${method}() throws on invalid input`, () => {
|
||||
expect(() => Script[method](input)).toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user