Files

60 lines
2.4 KiB
JavaScript

/**
* Unit Tests for General Utilities
*
* NOTE: Per constitution requirement, proxy.js has ZERO exports and NO globalThis usage.
* The file is a pure function expression loaded via Function constructor.
*
* This test file verifies constitution compliance only.
*/
import { test, describe } from 'node:test';
import assert from 'node:assert';
import { readFileSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Set up globals that server.js would provide
// Note: crypto is already available on globalThis (Web Crypto API)
globalThis.config = { google: {}, server: {}, sitemap: {} };
describe('Unit: Constitution Compliance', () => {
test('T046: proxy.js has ZERO exports/imports and loads as pure function', () => {
const proxyPath = join(__dirname, '..', '..', 'src', 'proxy.js');
const proxyCode = readFileSync(proxyPath, 'utf-8');
// Verify no exports
assert.ok(!proxyCode.match(/^export /m), 'Should have no export statements');
// Verify no imports
assert.ok(!proxyCode.match(/^import /m), 'Should have no import statements');
// Verify no globalThis usage (except for accessing provided globals)
const globalThisAssignments = proxyCode.match(/globalThis\.[a-zA-Z_]+ =/g);
assert.ok(!globalThisAssignments, 'Should not assign to globalThis');
// Verify it's a function expression that can be executed
assert.ok(proxyCode.includes('(function()'), 'Should contain function expression');
assert.ok(proxyCode.includes('return handleRequest'), 'Should return handleRequest');
});
test('T046: crypto is available on globalThis (Web Crypto API)', () => {
assert.ok(globalThis.crypto, 'crypto should be available');
assert.ok(globalThis.crypto.randomUUID, 'crypto.randomUUID should be available');
// Test that it works
const uuid = globalThis.crypto.randomUUID();
assert.ok(uuid, 'Should generate UUID');
assert.match(uuid, /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i, 'Should be valid UUID format');
});
});
// Note: Previous unit tests for internal functions (generateRequestId, validateDocumentId, etc.)
// have been moved to integration tests where they are tested through handleRequest.
// This maintains test coverage while respecting the constitution's ZERO exports requirement.