284 lines
9.8 KiB
JavaScript
284 lines
9.8 KiB
JavaScript
/**
|
|
* Configuration Loading Tests
|
|
*/
|
|
import { describe, it, expect } from 'vitest';
|
|
// We'll test the types and helper functions directly
|
|
import { isPostgresEnvironment, isSqlServerEnvironment } from '../config/types.js';
|
|
import { isPostgresConfig, isSqlServerConfig, getDatabaseType } from '../core/types.js';
|
|
describe('Configuration Types', () => {
|
|
describe('isPostgresEnvironment', () => {
|
|
it('should return true for postgres type', () => {
|
|
const config = {
|
|
type: 'postgres',
|
|
connection: {
|
|
host: 'localhost',
|
|
port: 5432,
|
|
database: 'test',
|
|
user: 'user',
|
|
password: 'pass'
|
|
}
|
|
};
|
|
expect(isPostgresEnvironment(config)).toBe(true);
|
|
});
|
|
it('should return true for undefined type (backward compatibility)', () => {
|
|
const config = {
|
|
connection: {
|
|
host: 'localhost',
|
|
port: 5432,
|
|
database: 'test',
|
|
user: 'user',
|
|
password: 'pass'
|
|
}
|
|
};
|
|
expect(isPostgresEnvironment(config)).toBe(true);
|
|
});
|
|
it('should return false for sqlserver type', () => {
|
|
const config = {
|
|
type: 'sqlserver',
|
|
connection: {
|
|
host: 'localhost',
|
|
port: 1433,
|
|
database: 'test',
|
|
user: 'user',
|
|
password: 'pass'
|
|
}
|
|
};
|
|
expect(isPostgresEnvironment(config)).toBe(false);
|
|
});
|
|
});
|
|
describe('isSqlServerEnvironment', () => {
|
|
it('should return true for sqlserver type', () => {
|
|
const config = {
|
|
type: 'sqlserver',
|
|
connection: {
|
|
host: 'localhost',
|
|
port: 1433,
|
|
database: 'test',
|
|
user: 'user',
|
|
password: 'pass'
|
|
}
|
|
};
|
|
expect(isSqlServerEnvironment(config)).toBe(true);
|
|
});
|
|
it('should return false for postgres type', () => {
|
|
const config = {
|
|
type: 'postgres',
|
|
connection: {
|
|
host: 'localhost',
|
|
port: 5432,
|
|
database: 'test',
|
|
user: 'user',
|
|
password: 'pass'
|
|
}
|
|
};
|
|
expect(isSqlServerEnvironment(config)).toBe(false);
|
|
});
|
|
it('should return false for undefined type', () => {
|
|
const config = {
|
|
connection: {
|
|
host: 'localhost',
|
|
port: 5432,
|
|
database: 'test',
|
|
user: 'user',
|
|
password: 'pass'
|
|
}
|
|
};
|
|
expect(isSqlServerEnvironment(config)).toBe(false);
|
|
});
|
|
});
|
|
});
|
|
describe('Core Types', () => {
|
|
describe('isPostgresConfig', () => {
|
|
it('should return true for postgres type', () => {
|
|
const config = {
|
|
name: 'test',
|
|
type: 'postgres',
|
|
connection: {
|
|
host: 'localhost',
|
|
port: 5432,
|
|
database: 'test',
|
|
user: 'user',
|
|
password: 'pass'
|
|
}
|
|
};
|
|
expect(isPostgresConfig(config)).toBe(true);
|
|
});
|
|
it('should return true for undefined type', () => {
|
|
const config = {
|
|
name: 'test',
|
|
connection: {
|
|
host: 'localhost',
|
|
port: 5432,
|
|
database: 'test',
|
|
user: 'user',
|
|
password: 'pass'
|
|
}
|
|
};
|
|
expect(isPostgresConfig(config)).toBe(true);
|
|
});
|
|
});
|
|
describe('isSqlServerConfig', () => {
|
|
it('should return true for sqlserver type', () => {
|
|
const config = {
|
|
name: 'test',
|
|
type: 'sqlserver',
|
|
connection: {
|
|
host: 'localhost',
|
|
port: 1433,
|
|
database: 'test',
|
|
user: 'user',
|
|
password: 'pass'
|
|
}
|
|
};
|
|
expect(isSqlServerConfig(config)).toBe(true);
|
|
});
|
|
it('should return false for postgres type', () => {
|
|
const config = {
|
|
name: 'test',
|
|
type: 'postgres',
|
|
connection: {
|
|
host: 'localhost',
|
|
port: 5432,
|
|
database: 'test',
|
|
user: 'user',
|
|
password: 'pass'
|
|
}
|
|
};
|
|
expect(isSqlServerConfig(config)).toBe(false);
|
|
});
|
|
});
|
|
describe('getDatabaseType', () => {
|
|
it('should return postgres for postgres config', () => {
|
|
const config = {
|
|
name: 'test',
|
|
type: 'postgres',
|
|
connection: {
|
|
host: 'localhost',
|
|
port: 5432,
|
|
database: 'test',
|
|
user: 'user',
|
|
password: 'pass'
|
|
}
|
|
};
|
|
expect(getDatabaseType(config)).toBe('postgres');
|
|
});
|
|
it('should return postgres for undefined type', () => {
|
|
const config = {
|
|
name: 'test',
|
|
connection: {
|
|
host: 'localhost',
|
|
port: 5432,
|
|
database: 'test',
|
|
user: 'user',
|
|
password: 'pass'
|
|
}
|
|
};
|
|
expect(getDatabaseType(config)).toBe('postgres');
|
|
});
|
|
it('should return sqlserver for sqlserver config', () => {
|
|
const config = {
|
|
name: 'test',
|
|
type: 'sqlserver',
|
|
connection: {
|
|
host: 'localhost',
|
|
port: 1433,
|
|
database: 'test',
|
|
user: 'user',
|
|
password: 'pass'
|
|
}
|
|
};
|
|
expect(getDatabaseType(config)).toBe('sqlserver');
|
|
});
|
|
});
|
|
});
|
|
describe('Driver Factory', () => {
|
|
it('should create PostgresDriver for postgres type', async () => {
|
|
const { createDriver } = await import('../drivers/driver-factory.js');
|
|
const driver = createDriver('postgres');
|
|
expect(driver.type).toBe('postgres');
|
|
expect(driver.name).toBe('PostgreSQL Driver');
|
|
});
|
|
it('should create SqlServerDriver for sqlserver type', async () => {
|
|
const { createDriver } = await import('../drivers/driver-factory.js');
|
|
const driver = createDriver('sqlserver');
|
|
expect(driver.type).toBe('sqlserver');
|
|
expect(driver.name).toBe('SQL Server Driver');
|
|
});
|
|
it('should throw for unknown type', async () => {
|
|
const { createDriver } = await import('../drivers/driver-factory.js');
|
|
expect(() => createDriver('mysql')).toThrow();
|
|
});
|
|
});
|
|
describe('DatabaseMcp Factory', () => {
|
|
// Note: These tests require compiled JS files because PostgresMcp/SqlServerMcp
|
|
// use dynamic require() for driver loading. Skipping in unit tests.
|
|
it.skip('should create PostgresMcp for postgres configs', async () => {
|
|
const { createDatabaseMcp } = await import('../core/index.js');
|
|
const configs = [{
|
|
name: 'test',
|
|
type: 'postgres',
|
|
connection: {
|
|
host: 'localhost',
|
|
port: 5432,
|
|
database: 'test',
|
|
user: 'user',
|
|
password: 'pass'
|
|
}
|
|
}];
|
|
const mcp = createDatabaseMcp(configs);
|
|
expect(mcp).toBeDefined();
|
|
expect(mcp.connections).toBeDefined();
|
|
expect(mcp.queries).toBeDefined();
|
|
});
|
|
it.skip('should create SqlServerMcp for sqlserver configs', async () => {
|
|
const { createDatabaseMcp } = await import('../core/index.js');
|
|
const configs = [{
|
|
name: 'test',
|
|
type: 'sqlserver',
|
|
connection: {
|
|
host: 'localhost',
|
|
port: 1433,
|
|
database: 'test',
|
|
user: 'user',
|
|
password: 'pass'
|
|
}
|
|
}];
|
|
const mcp = createDatabaseMcp(configs);
|
|
expect(mcp).toBeDefined();
|
|
expect(mcp.connections).toBeDefined();
|
|
expect(mcp.queries).toBeDefined();
|
|
});
|
|
it('should throw for empty configs', async () => {
|
|
const { createDatabaseMcp } = await import('../core/index.js');
|
|
expect(() => createDatabaseMcp([])).toThrow('At least one environment configuration is required');
|
|
});
|
|
it('should throw for mixed database types', async () => {
|
|
const { createDatabaseMcp } = await import('../core/index.js');
|
|
const configs = [
|
|
{
|
|
name: 'pg',
|
|
type: 'postgres',
|
|
connection: {
|
|
host: 'localhost',
|
|
port: 5432,
|
|
database: 'test',
|
|
user: 'user',
|
|
password: 'pass'
|
|
}
|
|
},
|
|
{
|
|
name: 'sql',
|
|
type: 'sqlserver',
|
|
connection: {
|
|
host: 'localhost',
|
|
port: 1433,
|
|
database: 'test',
|
|
user: 'user',
|
|
password: 'pass'
|
|
}
|
|
}
|
|
];
|
|
expect(() => createDatabaseMcp(configs)).toThrow('Mixed database types detected');
|
|
});
|
|
});
|