|
| 1 | +const assert = require('assert'); |
| 2 | + |
| 3 | +// Test Requirement 2.2: Backward compatibility with .default accessor |
| 4 | +// Both require('@rbac/rbac') and require('@rbac/rbac').default should work identically |
| 5 | + |
| 6 | +console.log('Testing backward compatibility with .default accessor...'); |
| 7 | + |
| 8 | +// Import using both patterns |
| 9 | +const RBACDirect = require('@rbac/rbac'); |
| 10 | +const RBACWithDefault = require('@rbac/rbac').default; |
| 11 | + |
| 12 | +// Verify both are functions |
| 13 | +assert.strictEqual(typeof RBACDirect, 'function', 'require("@rbac/rbac") should be a function'); |
| 14 | +assert.strictEqual(typeof RBACWithDefault, 'function', 'require("@rbac/rbac").default should be a function'); |
| 15 | + |
| 16 | +console.log('✅ Type check passed: Both patterns return functions'); |
| 17 | + |
| 18 | +// Verify they return the SAME function (reference equality) |
| 19 | +assert.strictEqual(RBACDirect, RBACWithDefault, |
| 20 | + 'require("@rbac/rbac") and require("@rbac/rbac").default should return the same function'); |
| 21 | + |
| 22 | +console.log('✅ Identity check passed: Both patterns return the same function reference'); |
| 23 | + |
| 24 | +// Test that both patterns work identically with the same input |
| 25 | +const roles = { |
| 26 | + user: { |
| 27 | + can: ['read'] |
| 28 | + }, |
| 29 | + admin: { |
| 30 | + can: ['read', 'write'] |
| 31 | + } |
| 32 | +}; |
| 33 | + |
| 34 | +const rbacInstance1 = RBACDirect({ enableLogger: false })(roles); |
| 35 | +const rbacInstance2 = RBACWithDefault({ enableLogger: false })(roles); |
| 36 | + |
| 37 | +assert.strictEqual(typeof rbacInstance1, 'object', 'RBACDirect() should return an object'); |
| 38 | +assert.strictEqual(typeof rbacInstance1.can, 'function', 'RBACDirect instance should have a can method'); |
| 39 | + |
| 40 | +assert.strictEqual(typeof rbacInstance2, 'object', 'RBACWithDefault() should return an object'); |
| 41 | +assert.strictEqual(typeof rbacInstance2.can, 'function', 'RBACWithDefault instance should have a can method'); |
| 42 | + |
| 43 | +console.log('✅ Functional check passed: Both patterns create valid RBAC instances'); |
| 44 | + |
| 45 | +// Test that both instances behave identically |
| 46 | +(async () => { |
| 47 | + try { |
| 48 | + const canRead1 = await rbacInstance1.can('user', 'read'); |
| 49 | + const canRead2 = await rbacInstance2.can('user', 'read'); |
| 50 | + assert.strictEqual(canRead1, canRead2, 'Both instances should return same result for user read permission'); |
| 51 | + assert.strictEqual(canRead1, true, 'User should be able to read'); |
| 52 | + |
| 53 | + const canWrite1 = await rbacInstance1.can('user', 'write'); |
| 54 | + const canWrite2 = await rbacInstance2.can('user', 'write'); |
| 55 | + assert.strictEqual(canWrite1, canWrite2, 'Both instances should return same result for user write permission'); |
| 56 | + assert.strictEqual(canWrite1, false, 'User should not be able to write'); |
| 57 | + |
| 58 | + const canWriteAdmin1 = await rbacInstance1.can('admin', 'write'); |
| 59 | + const canWriteAdmin2 = await rbacInstance2.can('admin', 'write'); |
| 60 | + assert.strictEqual(canWriteAdmin1, canWriteAdmin2, 'Both instances should return same result for admin write permission'); |
| 61 | + assert.strictEqual(canWriteAdmin1, true, 'Admin should be able to write'); |
| 62 | + |
| 63 | + console.log('✅ Behavior check passed: Both patterns produce identical results'); |
| 64 | + console.log('✅ All backward compatibility tests passed successfully!'); |
| 65 | + } catch (error) { |
| 66 | + console.error('❌ Test failed:', error.message); |
| 67 | + process.exit(1); |
| 68 | + } |
| 69 | +})(); |
0 commit comments