trying to stop proxy.js from exporting
This commit is contained in:
@@ -1,103 +1,42 @@
|
||||
/**
|
||||
* Unit Tests for General Utilities
|
||||
* Tests request ID generation and document ID validation
|
||||
*
|
||||
* NOTE: Per constitution requirement, proxy.js has ZERO exports.
|
||||
* Internal functions (generateRequestId, validateDocumentId, etc.) cannot be unit tested directly.
|
||||
* These functions are tested indirectly through integration tests of the main handleRequest function.
|
||||
*
|
||||
* This test file verifies constitution compliance only.
|
||||
*/
|
||||
|
||||
import { test, describe } from 'node:test';
|
||||
import assert from 'node:assert';
|
||||
import crypto from 'node:crypto';
|
||||
|
||||
// Set up globals that server.js would provide
|
||||
globalThis.crypto = crypto;
|
||||
// Note: crypto is already available on globalThis (Web Crypto API)
|
||||
globalThis.config = { google: {}, server: {}, sitemap: {} };
|
||||
|
||||
import { generateRequestId, validateDocumentId } from '../../src/proxy.js';
|
||||
|
||||
describe('Unit: Request ID Generation', () => {
|
||||
describe('Unit: Constitution Compliance', () => {
|
||||
|
||||
test('T046: Should generate unique request ID', () => {
|
||||
const id1 = generateRequestId();
|
||||
const id2 = generateRequestId();
|
||||
|
||||
assert.ok(id1, 'Should generate ID');
|
||||
assert.ok(id2, 'Should generate second ID');
|
||||
assert.notStrictEqual(id1, id2, 'IDs should be unique');
|
||||
test('T046: proxy.js has ZERO exports and exposes handleRequest via globalThis', async () => {
|
||||
// Verify proxy.js can be loaded and exposes handleRequest via globalThis
|
||||
await import('../../src/proxy.js');
|
||||
assert.ok(globalThis.handleRequest, 'handleRequest should be available on globalThis');
|
||||
assert.strictEqual(typeof globalThis.handleRequest, 'function', 'handleRequest should be a function');
|
||||
});
|
||||
|
||||
test('T046: Should generate ID with req_ prefix', () => {
|
||||
const id = generateRequestId();
|
||||
assert.ok(id.startsWith('req_'), 'Should start with req_ prefix');
|
||||
});
|
||||
|
||||
test('T046: Should generate valid UUID format', () => {
|
||||
const id = generateRequestId();
|
||||
const uuidPart = id.substring(4); // Remove 'req_' prefix
|
||||
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');
|
||||
|
||||
// UUID v4 format: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
|
||||
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
||||
assert.ok(uuidRegex.test(uuidPart), 'Should be valid UUID v4');
|
||||
// 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');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('Unit: Document ID Validation', () => {
|
||||
|
||||
test('T046: Should accept valid Google Drive IDs', () => {
|
||||
const validIds = [
|
||||
'1BxAA_example123',
|
||||
'abcdefghijklmnop',
|
||||
'12345678',
|
||||
'test-doc-id_123',
|
||||
'ABCDEFGH-IJKLMNOP_12345678'
|
||||
];
|
||||
|
||||
for (const id of validIds) {
|
||||
assert.ok(
|
||||
validateDocumentId(id),
|
||||
`Should accept valid ID: ${id}`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('T046: Should reject IDs that are too short', () => {
|
||||
const shortId = 'abc1234'; // 7 characters (minimum is 8)
|
||||
assert.strictEqual(validateDocumentId(shortId), false);
|
||||
});
|
||||
|
||||
test('T046: Should reject IDs that are too long', () => {
|
||||
const longId = 'a'.repeat(129); // 129 characters (maximum is 128)
|
||||
assert.strictEqual(validateDocumentId(longId), false);
|
||||
});
|
||||
|
||||
test('T046: Should reject IDs with invalid characters', () => {
|
||||
const invalidIds = [
|
||||
'invalid@id',
|
||||
'invalid id', // space
|
||||
'invalid/id', // slash
|
||||
'invalid#id', // hash
|
||||
'invalid.id', // period
|
||||
'invalid$id' // dollar sign
|
||||
];
|
||||
|
||||
for (const id of invalidIds) {
|
||||
assert.strictEqual(
|
||||
validateDocumentId(id),
|
||||
false,
|
||||
`Should reject invalid ID: ${id}`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('T046: Should reject null, undefined, and non-strings', () => {
|
||||
assert.strictEqual(validateDocumentId(null), false);
|
||||
assert.strictEqual(validateDocumentId(undefined), false);
|
||||
assert.strictEqual(validateDocumentId(123), false);
|
||||
assert.strictEqual(validateDocumentId({}), false);
|
||||
assert.strictEqual(validateDocumentId([]), false);
|
||||
});
|
||||
|
||||
test('T046: Should reject empty string', () => {
|
||||
assert.strictEqual(validateDocumentId(''), false);
|
||||
});
|
||||
|
||||
});
|
||||
// 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.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user