import { bench, describe } from 'vitest';
import { N8nNodeLoader } from '../../src/loaders/node-loader';
import { NodeRepository } from '../../src/database/node-repository';
import { SQLiteStorageService } from '../../src/services/sqlite-storage-service';
import path from 'path';

describe('Node Loading Performance', () => {
  let loader: N8nNodeLoader;
  let repository: NodeRepository;
  let storage: SQLiteStorageService;

  beforeAll(() => {
    storage = new SQLiteStorageService(':memory:');
    repository = new NodeRepository(storage);
    loader = new N8nNodeLoader(repository);
  });

  afterAll(() => {
    storage.close();
  });

  bench('loadPackage - n8n-nodes-base', async () => {
    await loader.loadPackage('n8n-nodes-base');
  }, {
    iterations: 5,
    warmupIterations: 2,
    warmupTime: 1000,
    time: 5000
  });

  bench('loadPackage - @n8n/n8n-nodes-langchain', async () => {
    await loader.loadPackage('@n8n/n8n-nodes-langchain');
  }, {
    iterations: 5,
    warmupIterations: 2,
    warmupTime: 1000,
    time: 5000
  });

  bench('loadNodesFromPath - single file', async () => {
    const testPath = path.join(process.cwd(), 'node_modules/n8n-nodes-base/dist/nodes/HttpRequest');
    await loader.loadNodesFromPath(testPath, 'n8n-nodes-base');
  }, {
    iterations: 100,
    warmupIterations: 10,
    warmupTime: 500,
    time: 3000
  });

  bench('parsePackageJson', async () => {
    const packageJsonPath = path.join(process.cwd(), 'node_modules/n8n-nodes-base/package.json');
    await loader['parsePackageJson'](packageJsonPath);
  }, {
    iterations: 1000,
    warmupIterations: 100,
    warmupTime: 100,
    time: 2000
  });
});