syntropylog
Version:
An instance manager with observability for Node.js applications
99 lines (98 loc) • 4.22 kB
TypeScript
import type { IContextManager } from './IContextManager';
import { ContextValue, ContextData, ContextConfig, ContextCallback, ContextHeaders, FilteredContext, LoggingMatrix } from '../types';
/**
* @class MockContextManager
* @description A mock implementation of `IContextManager` for testing purposes.
* It uses a simple in-memory object instead of AsyncLocalStorage,
* making context management predictable and synchronous in tests.
* @implements {IContextManager}
*/
export declare class MockContextManager implements IContextManager {
/** @private The in-memory key-value store for the context. */
private store;
/** @private The HTTP header name used for the correlation ID. */
private correlationIdHeader;
/** @private The HTTP header name used for the transaction ID. */
private transactionIdHeader;
/**
* Configures the mock context manager.
* @param options The configuration options.
* @param options.correlationIdHeader The custom header name to use for the correlation ID.
* @param options.transactionIdHeader The custom header name for the transaction ID.
*/
configure(options?: ContextConfig): void;
/**
* Simulates running a function within a new, isolated context.
* It saves the current context, creates a new one inheriting the parent's values,
* runs the callback, and then restores the original context. This process
* correctly handles both synchronous and asynchronous callbacks.
* @template T The return type of the callback.
* @param {() => T} callback The function to execute within the new context.
* @returns {T} The result of the callback.
*/
run(fn: ContextCallback): Promise<void>;
/**
* Gets a value from the mock context by its key.
* @template T The expected type of the value.
* @param {string} key The key of the value to retrieve.
* @returns The value, or `undefined` if not found.
*/
get<T = ContextValue>(key: string): T | undefined;
/**
* Gets a shallow copy of the entire mock context store.
* @returns {ContextData} An object containing all context data.
*/
getAll(): ContextData;
/**
* Sets a key-value pair in the mock context.
* @param {string} key The key for the value.
* @param {ContextValue} value The value to store.
* @returns {void}
*/
set(key: string, value: ContextValue): void;
/**
* Clears the in-memory store.
* Useful for resetting state between tests (e.g., in a `beforeEach` hook).
* @returns {void}
*/
clear(): void;
/**
* A convenience method to get the correlation ID from the mock context.
* @returns {string} The correlation ID, or a generated one if not set.
*/
getCorrelationId(): string;
/**
* A convenience method to get the transaction ID from the mock context.
* @returns {string | undefined} The transaction ID, or undefined if not set.
*/
getTransactionId(): string | undefined;
/**
* A convenience method to set the transaction ID in the mock context.
* @param {string} transactionId The transaction ID to set.
*/
setTransactionId(transactionId: string): void;
/**
* Gets the configured HTTP header name used for the correlation ID.
* @returns {string} The header name.
*/
getCorrelationIdHeaderName(): string;
/**
* Gets the configured HTTP header name used for the transaction ID.
* @returns {string} The header name.
*/
getTransactionIdHeaderName(): string;
/**
* Mock implementation for getting trace context headers.
* In a real tracing scenario, this would be populated.
* @returns `undefined` as this mock does not implement tracing.
*/
getTraceContextHeaders(): ContextHeaders;
getFilteredContext(): FilteredContext;
/**
* Reconfigures the logging matrix dynamically.
* This method allows changing which context fields are included in logs
* without affecting security configurations like masking or log levels.
* @param newMatrix The new logging matrix configuration
*/
reconfigureLoggingMatrix(newMatrix: LoggingMatrix): void;
}