tests — elevated-mode
tests — elevated-mode
This document provides an overview of the test suite for the elevated-mode module, located at tests/elevated-mode/elevated.test.ts. Its primary purpose is to ensure the robustness, correctness, and expected behavior of the ElevatedModeManager class and its associated utility functions.
1. Introduction
The elevated-mode module (src/elevated-mode/index.js) is responsible for managing application-level permissions, user elevation levels, and handling requests for sensitive operations. It provides a structured way to define permissions, request user consent, and enforce access control based on current elevation levels and granted permissions.
The elevated.test.ts file serves as the comprehensive test suite for this critical module. It covers various aspects of permission management, session handling, and configuration, ensuring that changes to the core elevated-mode logic maintain expected functionality.
2. Test Suite Structure
The test suite is organized into three main describe blocks, reflecting the logical components of the elevated-mode module:
- Permission Utilities: Tests individual helper functions related to permission comparison, pattern matching, and key generation.
ElevatedModeManager: The most extensive section, covering the coreElevatedModeManagerclass's lifecycle, permission checking, request handling, level management, grant management, session management, and configuration.- Singleton: Verifies the singleton pattern implementation for
ElevatedModeManagerviagetElevatedMode()andresetElevatedMode().
Each test block uses beforeEach and afterEach hooks to ensure a clean and isolated state for every test, typically by calling resetElevatedMode() and manager.resetSession().
3. Detailed Test Coverage
3.1. Permission Utilities Tests
This section focuses on the standalone utility functions exported by the elevated-mode module.
compareLevels(levelA, levelB):- Verifies the correct ordinal comparison of
PermissionLevelvalues (e.g., 'user', 'elevated', 'admin', 'system'). - Ensures
0for equality,<0iflevelAis lower, and>0iflevelAis higher. meetsLevel(currentLevel, requiredLevel):- Checks if a
currentLevelsatisfies arequiredLevel. - For example, an 'elevated' user meets a 'user' requirement, but not vice-versa.
matchesPattern(resource, pattern):- Tests resource string matching against patterns.
- Covers exact string matches.
- Validates wildcard (
*) behavior for matching zero or more characters. - Validates single-character wildcard (
?) behavior. permissionKey(permission):- Ensures a consistent string key is generated for a given
Permissionobject. - Handles cases where
resourceis provided and where it's missing (defaulting to*).
3.2. ElevatedModeManager Tests
This is the core of the test suite, validating the ElevatedModeManager class. An instance of ElevatedModeManager is created with default configuration (defaultLevel: 'user', autoGrantSafe: true, requestTimeoutMs: 1000) for most tests.
3.2.1. Permission Checking
manager.hasPermission(permission):- Tests basic permission checks based on the current session level.
- Verifies that permissions requiring a higher level than the current session level are denied.
- Confirms that explicitly granted permissions are recognized.
manager.isSafePermission(permission):- Identifies permissions configured as "safe" (e.g., read operations) which might be auto-granted.
manager.isDangerousPermission(permission):- Identifies permissions configured as "dangerous" (e.g., system modifications) which typically require explicit user consent.
3.2.2. Permission Requests
manager.requestPermission(category, options):- Auto-granting: Verifies that safe permissions are automatically granted when
autoGrantSafeis enabled. - Manual Granting: Tests the flow for elevated permissions:
- A request is made.
- The request appears in
getRequestHistory(). manager.grantRequest(requestId, type)is called to approve it.- The
requestPermissionpromise resolves with the grant.
- Timeouts: Ensures that pending requests expire and resolve to
nullif not granted withinrequestTimeoutMs. - Denial: Tests
manager.denyRequest(requestId, reason)to explicitly reject a request, causing therequestPermissionpromise to resolve tonull. - Event Emission: Verifies that
permission-requestandpermission-denyevents are emitted at appropriate times.
3.2.3. Level Management
manager.getLevel(): Retrieves the current session'sPermissionLevel.manager.elevate(newLevel, durationMs?):- Tests successful elevation to a higher level.
- Ensures elevation to a lower level is prevented.
- Verifies
level-changeevents are emitted. - Timed Elevation: Tests elevation with a specified duration, confirming
isElevated()andgetElevationTimeRemaining()work as expected. - Expiration: Ensures timed elevations automatically revert to the default level after their duration.
manager.dropElevation(): Resets the session's level back to the default.manager.isElevated(): Checks if the current level is higher than the default level.
3.2.4. Grant Management
manager.getGrants(): Lists all currently active permission grants.manager.revokeGrant(grantId): Revokes a specific grant by its ID.manager.revokeCategory(category): Revokes all grants belonging to a specific category.manager.clearGrants(): Revokes all active grants in the session.- Event Emission: Verifies that
grant-expireevents are emitted when grants are revoked or expire.
3.2.5. Session Management
manager.getSession(): Retrieves an object containing current session details (ID, level, grant count, pending request count).manager.getRequestHistory(): Provides a list of all permission requests made during the session, including their status.manager.resetSession():- Resets the entire session state, including current level, grants, and request history.
- Verifies that
session-expireevents are emitted.
3.2.6. Configuration
manager.getConfig(): Retrieves the current configuration of theElevatedModeManager.manager.updateConfig(newConfig): Allows dynamic modification of manager configuration options.
3.2.7. Grant Limits
maxGrantsPerSession: Tests that theElevatedModeManagerrespects the configuredmaxGrantsPerSessionlimit, preventing an excessive number of grants from accumulating.
3.3. Singleton Tests
This section specifically tests the singleton pattern implementation for the ElevatedModeManager.
getElevatedMode():- Verifies that multiple calls to
getElevatedMode()return the exact same instance ofElevatedModeManager. resetElevatedMode():- Confirms that calling
resetElevatedMode()effectively clears the singleton instance, causing subsequent calls togetElevatedMode()to return a new instance.
4. How to Run These Tests
To execute this test suite, navigate to the project root and run your standard test command, typically:
npm test
# or
yarn test
# or, to run only this file with Jest:
npx jest tests/elevated-mode/elevated.test.ts
5. Contributing to Tests
When contributing to the elevated-mode module:
- New Features: Always add new test cases to
elevated.test.ts(or a new, related test file if the feature warrants it) to cover the functionality of your new code. - Bug Fixes: Add a regression test that specifically fails before your fix and passes after it.
- Modifications: Update existing tests if the expected behavior of a function changes.
- Clarity: Ensure test descriptions (
it('should do X')) are clear and concise, explaining the specific behavior being tested. - Isolation: Leverage
beforeEachandafterEachto ensure tests are independent and do not affect each other's state.