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', ).toUint8Array(), representations: { bytes: Bytes.fromHex( '76a914876fbb82ec05caa6af7a3b5e5a983aae6c6cc6d688ac', ).toUint8Array(), 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()).toEqual(raw); }); }); // Test generic from method it.each(testCases)('from(Uint8Array)', ({ raw, representations }) => { const script = Script.from(representations.bytes); expect(script.toRaw()).toEqual(raw); }); it.each(testCases)('from(string)', ({ raw, representations }) => { const script = Script.from(representations.hex); expect(script.toRaw()).toEqual(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]()).toEqual(representations[key]); }); }); // Test fromRaw specifically it.each(testCases)('fromRaw() creates correct instance', ({ raw }) => { const script = Script.fromRaw(raw); expect(script.toRaw()).toEqual(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(); }); }); });