/** * ESM Import Tests - TDD approach * * These tests verify that the package can be imported as a proper ES module * and doesn't use CommonJS patterns like require.resolve() that break in ES modules. * * This would have caught the bug where require.resolve() was used in an ES module, * causing "ReferenceError: require is not defined in ES module scope" */ import { describe, expect, it } from '@jest/globals'; describe('ES Module Import Tests', () => { describe('Schema imports', () => { it('should import schemas object from index', async () => { // Dynamic import to test ESM loading const { schemas } = await import('../schemas/index.js'); expect(schemas).toBeDefined(); expect(typeof schemas).toBe('object'); expect(schemas.conversationList).toBeDefined(); expect(schemas.agentSecondOpinion).toBeDefined(); }); it('should import individual schema exports', async () => { const { agentSecondOpinionSchema, conversationDeleteSchema, conversationGetHistorySchema, conversationHealthSchema, conversationListSchema, conversationSendMessageSchema } = await import('../schemas/index.js'); expect(agentSecondOpinionSchema).toBeDefined(); expect(conversationDeleteSchema).toBeDefined(); expect(conversationGetHistorySchema).toBeDefined(); expect(conversationHealthSchema).toBeDefined(); expect(conversationListSchema).toBeDefined(); expect(conversationSendMessageSchema).toBeDefined(); }); it('should load schema JSON files with dynamic imports', async () => { // This tests that JSON imports work in ESM const conversationListSchema = await import( '../schemas/conversation.list.schema.json' ); expect(conversationListSchema.default).toBeDefined(); expect(conversationListSchema.default.$schema).toBeDefined(); expect(conversationListSchema.default.type).toBe('object'); }); }); describe('Module compatibility', () => { it('should be loadable in Node ESM mode', async () => { // This simulates what happens when the frontend loads the published package // In a real ESM environment, this import should not throw // This test would catch if require() or other CommonJS patterns were used await expect( import('../schemas/index.js') ).resolves.toBeDefined(); }); }); describe('Core module exports', () => { it('should import schema module directly', async () => { // Test that the schemas can be imported as ESM const schemaModule = await import('../schemas/index.js'); expect(schemaModule).toBeDefined(); expect(schemaModule.schemas).toBeDefined(); }); it('should import tool helpers module', async () => { // Test that tool helpers can be imported const { createMCPToolResult, extractMCPResult } = await import('../ToolHelpers.js'); expect(createMCPToolResult).toBeDefined(); expect(extractMCPResult).toBeDefined(); }); }); describe('JSON import compatibility', () => { it('should support JSON imports in ESM mode', async () => { // JSON imports should work with resolveJsonModule enabled const agentSchema = await import( '../schemas/agent.second-opinion.schema.json' ); expect(agentSchema.default).toBeDefined(); expect(agentSchema.default.title).toBe('Agent Second Opinion Response'); }); it('should have proper schema structure in JSON files', async () => { const schemas = [ 'agent.second-opinion.schema.json', 'conversation.delete.schema.json', 'conversation.get-history.schema.json', 'conversation.health.schema.json', 'conversation.list.schema.json', 'conversation.send-message.schema.json' ]; for (const schemaFile of schemas) { const schema = await import( `../schemas/${schemaFile}` ); expect(schema.default).toBeDefined(); expect(schema.default.$schema).toBeDefined(); expect(schema.default.type).toBeDefined(); } }); }); describe('Regression: require.resolve() in ESM', () => { it('should not use require.resolve() anywhere in the module', async () => { // This is the bug that was causing the frontend failure // The code had require.resolve() in an ES module, which throws: // "ReferenceError: require is not defined in ES module scope" // Load the schema module const schemaModule = await import('../schemas/index.js'); // Verify it loads without throwing expect(schemaModule).toBeDefined(); expect(schemaModule.schemas).toBeDefined(); // The old code had: // const schemaPaths = { // agentSecondOpinion: require.resolve('./agent.second-opinion.schema.json'), // ... // } // This would throw in ESM mode // Verify the new implementation doesn't have schemaPaths export expect(schemaModule).not.toHaveProperty('schemaPaths'); }); it('should load successfully in strict ESM environment', async () => { // This simulates the exact scenario that was failing in frontend tests // When they installed the published npm package with "type": "module" let error: Error | null = null; try { await import('../schemas/index.js'); } catch (e) { error = e as Error; } // Should not throw "require is not defined" error expect(error).toBeNull(); }); }); });