import { describe, it, expect } from 'vitest';
import { handler_0xa2_LOG2 } from './0xa2_LOG2.js';
describe('LOG2 (0xa2)', () => {
it('emits log with 2 topics and empty data', () => {
const topic0 = 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaan;
const topic1 = 0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbn;
const frame = createFrame({
stack: [topic1, topic0, 0n, 0n],
gasRemaining: 1000000n,
});
const err = handler_0xa2_LOG2(frame);
expect(err).toBeNull();
expect(frame.logs).toHaveLength(1);
expect(frame.logs[0].topics).toEqual([topic0, topic1]);
expect(frame.gasRemaining).toBe(998875n);
});
it('emits log with 2 topics and data', () => {
const frame = createFrame({
memory: new Map([[0, 0xde], [1, 0xad]]),
stack: [0x2222n, 0x1111n, 2n, 0n],
gasRemaining: 1000000n,
});
handler_0xa2_LOG2(frame);
const log = frame.logs[0];
expect(log.topics).toEqual([0x1111n, 0x2222n]);
expect(log.data).toEqual(new Uint8Array([0xde, 0xad]));
});
it('returns WriteProtection in static context', () => {
const frame = createFrame({ isStatic: true, stack: [0n, 0n, 0n, 0n] });
const err = handler_0xa2_LOG2(frame);
expect(err).toEqual({ type: "WriteProtection" });
});
it('returns StackUnderflow with 3 items', () => {
const frame = createFrame({ stack: [0n, 0n, 0n] });
const err = handler_0xa2_LOG2(frame);
expect(err).toEqual({ type: "StackUnderflow" });
});
it('handles max values for both topics', () => {
const maxUint256 = (1n << 256n) - 1n;
const frame = createFrame({
stack: [maxUint256, maxUint256, 0n, 0n],
gasRemaining: 1000000n,
});
handler_0xa2_LOG2(frame);
expect(frame.logs[0].topics).toEqual([maxUint256, maxUint256]);
});
it('expands memory correctly with large data', () => {
const frame = createFrame({
stack: [0xfffn, 0xfffn, 100n, 50n],
gasRemaining: 1000000n,
});
handler_0xa2_LOG2(frame);
// Memory expands to cover offset 50 + length 100 = 150 bytes
// Word-aligned to 160 bytes (5 words * 32)
expect(frame.memorySize).toBe(160);
});
});