import { afterEach, beforeEach, describe, expect, test } from 'bun:test'; import { modules } from '../db/schema'; import { cleanupTestDatabase, setupTestDatabase } from './database'; import { cleanupMultipleTestModules, cleanupTestModule, getModuleState, importMultipleTestModules, importTestModule, } from './modules'; describe('Module Test Utilities', () => { let db: Awaited>; beforeEach(async () => { db = await setupTestDatabase(); }); afterEach(async () => { await cleanupTestDatabase(db); }); describe('importTestModule', () => { test('imports module from fixture', async () => { const moduleId = await importTestModule('./test-fixtures/modules/artifact-test', db); expect(moduleId).toBe('artifact-test'); const result = await db.select().from(modules); expect(result).toHaveLength(1); expect(result[0].id).toBe('artifact-test'); expect(result[0].state).toBe('IMPORTED'); }); test('imports module with configuration', async () => { const moduleId = await importTestModule('./test-fixtures/modules/artifact-test', db, { config: { vmid: '100', hostname: 'test-host', }, }); expect(moduleId).toBe('artifact-test'); // TODO: Add assertions for config when module config utilities are available }); test('returns module ID', async () => { const moduleId = await importTestModule('./test-fixtures/modules/artifact-test', db); expect(typeof moduleId).toBe('string'); expect(moduleId.length).toBeGreaterThan(0); }); }); describe('cleanupTestModule', () => { test('removes module from database', async () => { const moduleId = await importTestModule('./test-fixtures/modules/artifact-test', db); const before = await db.select().from(modules); expect(before).toHaveLength(1); await cleanupTestModule(moduleId, db); const after = await db.select().from(modules); expect(after).toHaveLength(0); }); test('handles non-existent module gracefully', async () => { await expect(cleanupTestModule('non-existent-module', db)).resolves.toBeUndefined(); }); }); describe('importMultipleTestModules', () => { test('imports multiple modules', async () => { // Note: Using artifact-test twice to test multiple imports // In real scenarios, these would be different modules const moduleIds = await importMultipleTestModules( [{ path: './test-fixtures/modules/artifact-test' }], db, ); expect(moduleIds).toHaveLength(1); expect(moduleIds[0]).toBe('artifact-test'); const result = await db.select().from(modules); expect(result).toHaveLength(1); }); test('imports modules with configuration', async () => { const moduleIds = await importMultipleTestModules( [ { path: './test-fixtures/modules/artifact-test', options: { config: { vmid: '100' } }, }, ], db, ); expect(moduleIds).toHaveLength(1); }); test('handles empty array', async () => { const moduleIds = await importMultipleTestModules([], db); expect(moduleIds).toHaveLength(0); }); }); describe('cleanupMultipleTestModules', () => { test('removes multiple modules', async () => { const moduleIds = await importMultipleTestModules( [{ path: './test-fixtures/modules/artifact-test' }], db, ); const before = await db.select().from(modules); expect(before).toHaveLength(1); await cleanupMultipleTestModules(moduleIds, db); const after = await db.select().from(modules); expect(after).toHaveLength(0); }); test('handles empty array', async () => { await expect(cleanupMultipleTestModules([], db)).resolves.toBeUndefined(); }); }); describe('getModuleState', () => { test('returns module state', async () => { const moduleId = await importTestModule('./test-fixtures/modules/artifact-test', db); const state = await getModuleState(moduleId, db); expect(state).toBe('IMPORTED'); }); test('returns null for non-existent module', async () => { const state = await getModuleState('non-existent', db); expect(state).toBeNull(); }); }); });