UNPKG

fastcomments-sdk

Version:

FastComments API Client - A SDK for interacting with the FastComments API

170 lines 9.2 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const node_test_1 = require("node:test"); const node_assert_1 = __importDefault(require("node:assert")); const fs_1 = require("fs"); const path_1 = require("path"); const happy_dom_1 = require("happy-dom"); (0, node_test_1.describe)('Browser Compatibility Tests', () => { (0, node_test_1.describe)('Validation: Testing environment catches Node.js imports', () => { (0, node_test_1.it)('should demonstrate that our testing approach works by catching Node.js imports', () => { // Store originals const originalRequire = global.require; // Mock require to reject Node.js built-ins global.require = (module) => { const nodeBuiltins = ['fs', 'crypto', 'path', 'os', 'util', 'events', 'stream']; if (nodeBuiltins.some(builtin => module === builtin || module.startsWith(builtin + '/'))) { throw new Error(`Cannot resolve module '${module}' in browser environment`); } return originalRequire(module); }; try { // Test that our mock catches Node.js built-in imports node_assert_1.default.throws(() => { const mockRequire = global.require; mockRequire('fs'); // This should throw }, /Cannot resolve module 'fs' in browser environment/, 'Mocked require should reject Node.js built-ins'); node_assert_1.default.throws(() => { const mockRequire = global.require; mockRequire('crypto'); // This should throw }, /Cannot resolve module 'crypto' in browser environment/, 'Mocked require should reject Node.js crypto module'); } finally { // Restore original require global.require = originalRequire; } }); }); (0, node_test_1.describe)('Static Analysis: Check compiled browser export for Node.js imports', () => { (0, node_test_1.it)('should not contain require() calls for Node.js built-ins', () => { const browserJsPath = (0, path_1.join)(process.cwd(), 'dist', 'browser.js'); let browserJsContent; try { browserJsContent = (0, fs_1.readFileSync)(browserJsPath, 'utf-8'); } catch (error) { throw new Error(`Cannot read dist/browser.js. Did you run 'npm run build' first? ${error}`); } // Node.js built-in modules that should NOT appear in browser bundle const nodeBuiltins = [ 'crypto', 'fs', 'path', 'os', 'util', 'events', 'stream', 'buffer', 'url', 'http', 'https', 'net', 'tls', 'zlib', 'child_process', 'cluster' ]; const foundImports = []; for (const builtin of nodeBuiltins) { // Check for various import patterns const patterns = [ `require("${builtin}")`, `require('${builtin}')`, `require(\`${builtin}\`)`, `from "${builtin}"`, `from '${builtin}'`, `from \`${builtin}\`` ]; for (const pattern of patterns) { if (browserJsContent.includes(pattern)) { foundImports.push(`Found: ${pattern}`); } } } if (foundImports.length > 0) { node_assert_1.default.fail(`Browser export contains Node.js built-in imports:\n${foundImports.join('\n')}`); } }); }); (0, node_test_1.describe)('Runtime Verification: Browser export works in browser-like environment', () => { let originalRequire; let window; (0, node_test_1.beforeEach)(() => { // Create clean browser-like environment window = new happy_dom_1.Window(); // Store original require originalRequire = global.require; // Remove Node.js built-ins to simulate real browser global.require = (module) => { const nodeBuiltins = ['fs', 'crypto', 'path', 'os', 'util', 'events', 'stream']; if (nodeBuiltins.some(builtin => module === builtin || module.startsWith(builtin + '/'))) { throw new Error(`Module '${module}' not available in browser`); } return originalRequire(module); }; // Set up minimal browser-like globals if (!global.fetch) { global.fetch = (() => Promise.reject(new Error('fetch not available in test'))); } }); (0, node_test_1.afterEach)(() => { // Restore original require global.require = originalRequire; }); (0, node_test_1.it)('should load browser export without errors', () => { try { // This should not throw if the browser export is truly browser-compatible const browserExport = require('../../dist/browser.js'); node_assert_1.default.ok(browserExport, 'Browser export should load successfully'); node_assert_1.default.ok(typeof browserExport === 'object', 'Browser export should be an object'); } catch (error) { if (error.message.includes('not available in browser')) { node_assert_1.default.fail(`Browser export tried to use Node.js built-in: ${error.message}`); } throw error; } }); (0, node_test_1.it)('should be able to create browser SDK instance', () => { try { const { createFastCommentsBrowserSDK } = require('../../dist/browser.js'); node_assert_1.default.ok(typeof createFastCommentsBrowserSDK === 'function', 'createFastCommentsBrowserSDK should be a function'); const sdk = createFastCommentsBrowserSDK(); node_assert_1.default.ok(sdk, 'Should be able to create SDK instance'); node_assert_1.default.ok(sdk.publicApi, 'SDK should have publicApi'); } catch (error) { if (error.message.includes('not available in browser')) { node_assert_1.default.fail(`SDK creation tried to use Node.js built-in: ${error.message}`); } throw error; } }); (0, node_test_1.it)('should have functional getFeedPostsStats API', () => { try { const { createFastCommentsBrowserSDK } = require('../../dist/browser.js'); const sdk = createFastCommentsBrowserSDK(); node_assert_1.default.ok(sdk.publicApi.getFeedPostsStats, 'getFeedPostsStats should be available'); node_assert_1.default.ok(typeof sdk.publicApi.getFeedPostsStats === 'function', 'getFeedPostsStats should be a function'); } catch (error) { if (error.message.includes('not available in browser')) { node_assert_1.default.fail(`getFeedPostsStats access tried to use Node.js built-in: ${error.message}`); } throw error; } }); (0, node_test_1.it)('should have functional live utilities', () => { try { const browserExports = require('../../dist/browser.js'); // Check that live utilities are available node_assert_1.default.ok(browserExports.makeRequest, 'makeRequest should be available'); node_assert_1.default.ok(typeof browserExports.makeRequest === 'function', 'makeRequest should be a function'); node_assert_1.default.ok(browserExports.subscribeToChanges, 'subscribeToChanges should be available'); node_assert_1.default.ok(typeof browserExports.subscribeToChanges === 'function', 'subscribeToChanges should be a function'); } catch (error) { if (error.message.includes('not available in browser')) { node_assert_1.default.fail(`Live utilities tried to use Node.js built-in: ${error.message}`); } throw error; } }); (0, node_test_1.it)('should NOT have SSO functionality (which requires Node.js crypto)', () => { const browserExports = require('../../dist/browser.js'); // Browser export should not include SSO functionality node_assert_1.default.ok(!browserExports.FastCommentsSSO, 'FastCommentsSSO should not be in browser export'); node_assert_1.default.ok(!browserExports.SecureSSOPayloadBuilder, 'SecureSSOPayloadBuilder should not be in browser export'); }); }); }); //# sourceMappingURL=browser-compatibility.test.js.map