import { describe, it } from 'mocha';
import { expect } from 'chai';
describe('Convert2Doc Module', () => {
describe('Module Structure', () => {
it('should export the required functions', async () => {
const module = await import('../index.js');
expect(module.register).to.be.a('function');
expect(module.unregister).to.be.a('function');
expect(module.metadata).to.be.an('object');
});
it('should have correct metadata', async () => {
const { metadata } = await import('../index.js');
expect(metadata.name).to.equal('Convert2Doc Module');
expect(metadata.version).to.equal('1.0.0');
expect(metadata.description).to.include('Document conversion');
expect(metadata.author).to.equal('Profullstack, Inc.');
expect(metadata.tools).to.include('convert2doc');
expect(metadata.endpoints).to.be.an('array');
expect(metadata.endpoints.length).to.be.greaterThan(0);
expect(metadata.supportedConversions).to.be.an('array');
});
});
describe('Supported Formats Validation', () => {
// Test the getSupportedFormats function indirectly through module import
it('should have valid supported formats structure', async () => {
// Since getSupportedFormats is not exported, we'll test the expected structure
const expectedFromFormats = ['pdf', 'docx', 'doc', 'epub', 'txt', 'pptx', 'xlsx', 'html'];
const expectedToFormats = ['markdown', 'html', 'pdf'];
// These should be the formats our module supports
expect(expectedFromFormats).to.include('pdf');
expect(expectedFromFormats).to.include('docx');
expect(expectedFromFormats).to.include('html');
expect(expectedToFormats).to.include('markdown');
expect(expectedToFormats).to.include('html');
});
});
describe('Input Validation', () => {
// Test validation logic that would be used in the convertDocument function
it('should validate required parameters', () => {
const validateParams = (apiKey, fileBase64, filename, fromFormat) => {
const errors = [];
if (!apiKey) errors.push('API key is required');
if (!fileBase64) errors.push('File content is required');
if (!filename) errors.push('Filename is required');
if (!fromFormat) errors.push('From format is required');
return errors;
};
// Test missing API key
let errors = validateParams('', 'base64content', 'test.pdf', 'pdf');
expect(errors).to.include('API key is required');
// Test missing file content
errors = validateParams('api-key', '', 'test.pdf', 'pdf');
expect(errors).to.include('File content is required');
// Test missing filename
errors = validateParams('api-key', 'base64content', '', 'pdf');
expect(errors).to.include('Filename is required');
// Test missing format
errors = validateParams('api-key', 'base64content', 'test.pdf', '');
expect(errors).to.include('From format is required');
// Test valid parameters
errors = validateParams('api-key', 'base64content', 'test.pdf', 'pdf');
expect(errors).to.have.length(0);
});
it('should validate supported formats', () => {
const supportedFromFormats = ['pdf', 'docx', 'doc', 'epub', 'txt', 'pptx', 'xlsx', 'html'];
const supportedToFormats = ['markdown', 'html', 'pdf'];
const validateFormat = (format, supportedFormats, formatType) => {
if (!supportedFormats.includes(format.toLowerCase())) {
return `Unsupported ${formatType} format: ${format}. Supported formats: ${supportedFormats.join(', ')}`;
}
return null;
};
// Test valid formats
expect(validateFormat('pdf', supportedFromFormats, 'source')).to.be.null;
expect(validateFormat('markdown', supportedToFormats, 'target')).to.be.null;
// Test invalid formats
expect(validateFormat('xyz', supportedFromFormats, 'source')).to.include(
'Unsupported source format'
);
expect(validateFormat('abc', supportedToFormats, 'target')).to.include(
'Unsupported target format'
);
});
});
describe('URL Construction', () => {
it('should construct correct API endpoints', () => {
const buildEndpoint = (baseUrl, fromFormat, toFormat) => {
return `${baseUrl}/api/1/${fromFormat.toLowerCase()}-to-${toFormat.toLowerCase()}`;
};
// Test various format combinations
expect(buildEndpoint('https://convert2doc.com', 'pdf', 'markdown')).to.equal(
'https://convert2doc.com/api/1/pdf-to-markdown'
);
expect(buildEndpoint('https://convert2doc.com', 'DOCX', 'HTML')).to.equal(
'https://convert2doc.com/api/1/docx-to-html'
);
expect(buildEndpoint('https://custom-api.com', 'html', 'markdown')).to.equal(
'https://custom-api.com/api/1/html-to-markdown'
);
});
});
describe('Request Body Construction', () => {
it('should construct correct request body', () => {
const buildRequestBody = (fileBase64, filename, store = false) => {
return {
file: fileBase64,
filename: filename,
store: store,
};
};
const body = buildRequestBody('base64content', 'test.pdf', true);
expect(body).to.deep.equal({
file: 'base64content',
filename: 'test.pdf',
store: true,
});
const bodyDefault = buildRequestBody('base64content', 'test.pdf');
expect(bodyDefault.store).to.be.false;
});
});
describe('Error Handling', () => {
it('should handle API errors properly', () => {
const handleApiError = (response, errorText) => {
return `Conversion failed: ${response.status} ${response.statusText} - ${errorText}`;
};
const mockResponse = { status: 401, statusText: 'Unauthorized' };
const error = handleApiError(mockResponse, 'Invalid API key');
expect(error).to.include('401');
expect(error).to.include('Unauthorized');
expect(error).to.include('Invalid API key');
});
it('should handle network errors', () => {
const handleNetworkError = error => {
return `Network error: ${error.message}`;
};
const mockError = new Error('Connection timeout');
const errorMessage = handleNetworkError(mockError);
expect(errorMessage).to.include('Network error');
expect(errorMessage).to.include('Connection timeout');
});
});
describe('Response Processing', () => {
it('should process successful responses correctly', () => {
const processResponse = (originalFilename, fromFormat, toFormat, convertedContent) => {
return {
success: true,
originalFilename,
fromFormat,
toFormat,
convertedContent,
timestamp: new Date().toISOString(),
};
};
const response = processResponse(
'test.pdf',
'pdf',
'markdown',
'# Test Document\n\nContent...'
);
expect(response.success).to.be.true;
expect(response.originalFilename).to.equal('test.pdf');
expect(response.fromFormat).to.equal('pdf');
expect(response.toFormat).to.equal('markdown');
expect(response.convertedContent).to.include('# Test Document');
expect(response.timestamp).to.be.a('string');
});
});
describe('Module Configuration', () => {
it('should have proper default configuration', () => {
const defaultConfig = {
baseUrl: 'https://convert2doc.com',
timeout: 10000,
store: false,
defaultToFormat: 'markdown',
};
expect(defaultConfig.baseUrl).to.equal('https://convert2doc.com');
expect(defaultConfig.timeout).to.equal(10000);
expect(defaultConfig.store).to.be.false;
expect(defaultConfig.defaultToFormat).to.equal('markdown');
});
});
});