import { describe, it, expect } from 'vitest';
import { handler_0x57_JUMPI } from './0x57_JUMPI.js';
describe('JUMPI (0x57)', () => {
it('jumps when condition is non-zero', () => {
const bytecode = new Uint8Array([
0x60, 0x05, // PUSH1 5
0x60, 0x01, // PUSH1 1
0x57, // JUMPI
0x00, // STOP
0x5b, // JUMPDEST
]);
const frame = createFrame({
bytecode,
stack: [1n, 5n],
pc: 4,
});
expect(handler_0x57_JUMPI(frame)).toBeNull();
expect(frame.pc).toBe(5);
});
it('continues when condition is zero', () => {
const bytecode = new Uint8Array([0x57, 0x00]);
const frame = createFrame({
bytecode,
stack: [0n, 5n],
pc: 0,
});
expect(handler_0x57_JUMPI(frame)).toBeNull();
expect(frame.pc).toBe(1);
});
it('rejects invalid destination when jumped', () => {
const bytecode = new Uint8Array([0x60, 0x00]);
const frame = createFrame({
bytecode,
stack: [1n, 1n],
pc: 0,
});
expect(handler_0x57_JUMPI(frame)).toEqual({ type: 'InvalidJump' });
});
it('does not validate destination when not jumped', () => {
const bytecode = new Uint8Array([0x57, 0x00]);
const frame = createFrame({
bytecode,
stack: [0n, 999n], // Invalid dest but not validated
pc: 0,
});
expect(handler_0x57_JUMPI(frame)).toBeNull();
expect(frame.pc).toBe(1);
});
it('treats any non-zero as truthy', () => {
const conditions = [1n, 42n, 0xffffffffn];
for (const condition of conditions) {
const bytecode = new Uint8Array([0x57, 0x5b]);
const frame = createFrame({
bytecode,
stack: [condition, 1n],
pc: 0,
});
handler_0x57_JUMPI(frame);
expect(frame.pc).toBe(1);
}
});
});