128 lines
4.4 KiB
JavaScript
128 lines
4.4 KiB
JavaScript
/**
|
|
* Unit Tests: Format Selection Logic
|
|
*
|
|
* Tests the selectExportFormat helper function
|
|
* Verifies priority ordering: Markdown > HTML > PDF
|
|
*/
|
|
|
|
import { describe, it } from 'node:test';
|
|
import assert from 'node:assert';
|
|
|
|
// Load helper functions using vm.Script pattern (similar to proxy.js)
|
|
import { readFileSync } from 'fs';
|
|
import { Script } from 'vm';
|
|
|
|
// Create VM context with required globals
|
|
const vmContext = {
|
|
crypto: globalThis.crypto,
|
|
console: console
|
|
};
|
|
|
|
// Load googleDriveAdapterHelper.js
|
|
const helperCode = readFileSync('./src/globalVariables/googleDriveAdapterHelper.js', 'utf8');
|
|
const wrappedCode = `(function() {\n${helperCode}\n})()`;
|
|
const script = new Script(wrappedCode);
|
|
const helpers = script.runInNewContext(vmContext);
|
|
|
|
describe('Format Selection', () => {
|
|
describe('selectExportFormat', () => {
|
|
it('should prioritize text/x-markdown over other formats', () => {
|
|
const exportLinks = {
|
|
'text/x-markdown': 'https://example.com/export?format=md',
|
|
'text/html': 'https://example.com/export?format=html',
|
|
'application/pdf': 'https://example.com/export?format=pdf'
|
|
};
|
|
|
|
const result = helpers.selectExportFormat(exportLinks);
|
|
|
|
assert.strictEqual(result.contentType, 'text/x-markdown');
|
|
assert.strictEqual(result.extension, 'md');
|
|
assert.strictEqual(result.url, 'https://example.com/export?format=md');
|
|
});
|
|
|
|
it('should select text/html when markdown is unavailable', () => {
|
|
const exportLinks = {
|
|
'text/html': 'https://example.com/export?format=html',
|
|
'application/pdf': 'https://example.com/export?format=pdf'
|
|
};
|
|
|
|
const result = helpers.selectExportFormat(exportLinks);
|
|
|
|
assert.strictEqual(result.contentType, 'text/html');
|
|
assert.strictEqual(result.extension, 'html');
|
|
assert.strictEqual(result.url, 'https://example.com/export?format=html');
|
|
});
|
|
|
|
it('should select application/pdf when markdown and html are unavailable', () => {
|
|
const exportLinks = {
|
|
'application/pdf': 'https://example.com/export?format=pdf'
|
|
};
|
|
|
|
const result = helpers.selectExportFormat(exportLinks);
|
|
|
|
assert.strictEqual(result.contentType, 'application/pdf');
|
|
assert.strictEqual(result.extension, 'pdf');
|
|
assert.strictEqual(result.url, 'https://example.com/export?format=pdf');
|
|
});
|
|
|
|
it('should return null when no supported formats are available', () => {
|
|
const exportLinks = {
|
|
'text/plain': 'https://example.com/export?format=txt',
|
|
'application/json': 'https://example.com/export?format=json'
|
|
};
|
|
|
|
const result = helpers.selectExportFormat(exportLinks);
|
|
|
|
assert.strictEqual(result, null);
|
|
});
|
|
|
|
it('should return null when exportLinks is null', () => {
|
|
const result = helpers.selectExportFormat(null);
|
|
|
|
assert.strictEqual(result, null);
|
|
});
|
|
|
|
it('should return null when exportLinks is undefined', () => {
|
|
const result = helpers.selectExportFormat(undefined);
|
|
|
|
assert.strictEqual(result, null);
|
|
});
|
|
|
|
it('should return null when exportLinks is empty object', () => {
|
|
const result = helpers.selectExportFormat({});
|
|
|
|
assert.strictEqual(result, null);
|
|
});
|
|
|
|
it('should respect priority order even when formats appear in different order', () => {
|
|
const exportLinks = {
|
|
'application/pdf': 'https://example.com/export?format=pdf',
|
|
'text/x-markdown': 'https://example.com/export?format=md',
|
|
'text/html': 'https://example.com/export?format=html'
|
|
};
|
|
|
|
const result = helpers.selectExportFormat(exportLinks);
|
|
|
|
// Should still select Markdown despite PDF being first in object
|
|
assert.strictEqual(result.contentType, 'text/x-markdown');
|
|
});
|
|
});
|
|
|
|
describe('EXPORT_FORMATS constant', () => {
|
|
it('should define correct priority order', () => {
|
|
assert.ok(Array.isArray(helpers.EXPORT_FORMATS));
|
|
assert.strictEqual(helpers.EXPORT_FORMATS.length, 3);
|
|
|
|
// Verify order: Markdown > HTML > PDF
|
|
assert.strictEqual(helpers.EXPORT_FORMATS[0].mimeType, 'text/x-markdown');
|
|
assert.strictEqual(helpers.EXPORT_FORMATS[0].extension, 'md');
|
|
|
|
assert.strictEqual(helpers.EXPORT_FORMATS[1].mimeType, 'text/html');
|
|
assert.strictEqual(helpers.EXPORT_FORMATS[1].extension, 'html');
|
|
|
|
assert.strictEqual(helpers.EXPORT_FORMATS[2].mimeType, 'application/pdf');
|
|
assert.strictEqual(helpers.EXPORT_FORMATS[2].extension, 'pdf');
|
|
});
|
|
});
|
|
});
|