import { describe, it, expect } from 'vitest';
describe('CLI Commands', () => {
describe('JSON Parsing', () => {
it('should parse valid JSON for insert-data command', () => {
const jsonData = '{"name": "John", "email": "john@example.com"}';
const parsed = JSON.parse(jsonData);
expect(parsed).toEqual({ name: 'John', email: 'john@example.com' });
});
it('should parse JSON array for insert-data command', () => {
const jsonData = '[{"name": "John"}, {"name": "Jane"}]';
const parsed = JSON.parse(jsonData);
expect(parsed).toHaveLength(2);
expect(Array.isArray(parsed)).toBe(true);
});
it('should throw on invalid JSON', () => {
const invalidJson = '{name: "John"}'; // Missing quotes around key
expect(() => JSON.parse(invalidJson)).toThrow();
});
it('should parse JSON for update-data command', () => {
const updates = '{"status": "active", "updated_at": "2024-01-01"}';
const parsed = JSON.parse(updates);
expect(parsed).toEqual({ status: 'active', updated_at: '2024-01-01' });
});
it('should parse JSON array for create-index columns', () => {
const columns = '["name", "email"]';
const parsed = JSON.parse(columns);
expect(parsed).toEqual(['name', 'email']);
});
it('should parse JSON for exec-proc params', () => {
const params = '{"userId": 1, "status": "active"}';
const parsed = JSON.parse(params);
expect(parsed).toEqual({ userId: 1, status: 'active' });
});
it('should handle undefined params for exec-proc', () => {
const params = undefined;
const parameters = params ? JSON.parse(params) : undefined;
expect(parameters).toBeUndefined();
});
it('should parse column definitions for create-table', () => {
const columns = '[{"name": "id", "type": "INT", "primaryKey": true}, {"name": "name", "type": "NVARCHAR(100)"}]';
const parsed = JSON.parse(columns);
expect(parsed).toHaveLength(2);
expect(parsed[0].primaryKey).toBe(true);
});
});
describe('Command Options', () => {
describe('delete-data --confirm flag', () => {
it('should recognize --confirm as true', () => {
const options = { confirm: true };
expect(options.confirm).toBe(true);
});
it('should default to undefined when not provided', () => {
const options: { confirm?: boolean } = {};
expect(options.confirm).toBeUndefined();
});
});
describe('create-index options', () => {
it('should recognize --unique flag', () => {
const options = { unique: true, clustered: false };
expect(options.unique).toBe(true);
expect(options.clustered).toBe(false);
});
it('should recognize --clustered flag', () => {
const options = { unique: false, clustered: true };
expect(options.clustered).toBe(true);
});
});
describe('get-ddl options', () => {
it('should recognize --no-indexes flag', () => {
// Commander.js negates boolean options with --no- prefix
const options = { indexes: false, constraints: true };
const includeIndexes = options.indexes !== false;
expect(includeIndexes).toBe(false);
});
it('should recognize --no-constraints flag', () => {
const options = { indexes: true, constraints: false };
const includeConstraints = options.constraints !== false;
expect(includeConstraints).toBe(false);
});
it('should default both to true', () => {
const options: { indexes?: boolean; constraints?: boolean } = {};
const includeIndexes = options.indexes !== false;
const includeConstraints = options.constraints !== false;
expect(includeIndexes).toBe(true);
expect(includeConstraints).toBe(true);
});
});
});
describe('Schema Arguments', () => {
it('should handle empty schemas array', () => {
const schemas: string[] = [];
const result = schemas && schemas.length > 0 ? schemas : undefined;
expect(result).toBeUndefined();
});
it('should pass through non-empty schemas array', () => {
const schemas = ['dbo', 'sales'];
const result = schemas && schemas.length > 0 ? schemas : undefined;
expect(result).toEqual(['dbo', 'sales']);
});
});
describe('Table Name Parsing', () => {
it('should accept simple table name', () => {
const tableName = 'users';
expect(tableName).toBe('users');
});
it('should accept schema-qualified table name', () => {
const tableName = 'dbo.users';
expect(tableName).toBe('dbo.users');
});
it('should handle three-part name', () => {
const tableName = 'database.dbo.users';
expect(tableName).toBe('database.dbo.users');
});
});
});