import { describe, it, expect, vi } from 'vitest';
import { codeExecute } from '../../activities/code-execute';

describe('codeExecute activity', () => {
  it('executes simple script and returns result', async () => {
    const output = await codeExecute({
      script: 'return 1 + 2;',
    });
    expect(output.result).toBe(3);
  });

  it('has access to data context', async () => {
    const output = await codeExecute({
      script: 'return data.x * 2;',
      data: { x: 5 },
    });
    expect(output.result).toBe(10);
  });

  it('runOnceForEach mode processes each item', async () => {
    const output = await codeExecute({
      script: 'return item * 10;',
      mode: 'runOnceForEach',
      items: [1, 2, 3],
    });
    expect(output.result).toEqual([10, 20, 30]);
  });

  it('respects timeout', async () => {
    await expect(
      codeExecute({
        script: 'while(true) {}',
        timeout: 100,
      }),
    ).rejects.toThrow();
  });

  it('sandbox cannot access Node.js globals', async () => {
    const output = await codeExecute({
      script: 'return typeof require;',
    });
    expect(output.result).toBe('undefined');
  });
});
