UNPKG

@carthooks/test-context

Version:

A test context manager for sharing data across test files in E2E testing

102 lines 3.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MetricsPlugin = exports.EncryptionPlugin = exports.PersistencePlugin = exports.ValidationPlugin = void 0; class ValidationPlugin { constructor() { this.name = 'validation'; this.requiredKeys = new Set(); } install(context) { // Add validation methods to context context.addRequiredKey = (key) => { this.requiredKeys.add(key); }; context.removeRequiredKey = (key) => { this.requiredKeys.delete(key); }; context.validateRequired = () => { const missing = Array.from(this.requiredKeys).filter(key => !context.has(key)); if (missing.length > 0) { throw new Error(`Missing required keys: ${missing.join(', ')}`); } }; } uninstall() { this.requiredKeys.clear(); } } exports.ValidationPlugin = ValidationPlugin; class PersistencePlugin { constructor() { this.name = 'persistence'; this.autoSaveInterval = null; } install(context) { // Auto-save every 30 seconds this.autoSaveInterval = setInterval(() => { if ('save' in context && typeof context.save === 'function') { context.save(); } }, 30000); } uninstall() { if (this.autoSaveInterval) { clearInterval(this.autoSaveInterval); this.autoSaveInterval = null; } } } exports.PersistencePlugin = PersistencePlugin; class EncryptionPlugin { constructor(encryptionKey) { this.name = 'encryption'; this.encryptionKey = encryptionKey; // Use encryptionKey to avoid unused variable warning void this.encryptionKey; } install(context) { // Add encryption methods context.encrypt = (value) => { // Simple base64 encoding for demo - in production use proper encryption return Buffer.from(JSON.stringify(value)).toString('base64'); }; context.decrypt = (encryptedValue) => { try { return JSON.parse(Buffer.from(encryptedValue, 'base64').toString()); } catch { throw new Error('Failed to decrypt value'); } }; } } exports.EncryptionPlugin = EncryptionPlugin; class MetricsPlugin { constructor() { this.name = 'metrics'; this.metrics = {}; } install(context) { const originalSet = context.set.bind(context); const originalGet = context.get.bind(context); const originalDelete = context.delete.bind(context); context.set = (key, value) => { this.metrics['set'] = (this.metrics['set'] || 0) + 1; originalSet(key, value); }; context.get = (key) => { this.metrics['get'] = (this.metrics['get'] || 0) + 1; return originalGet(key); }; context.delete = (key) => { this.metrics['delete'] = (this.metrics['delete'] || 0) + 1; originalDelete(key); }; context.getMetrics = () => ({ ...this.metrics }); context.resetMetrics = () => { this.metrics = {}; }; } } exports.MetricsPlugin = MetricsPlugin; //# sourceMappingURL=index.js.map