refactor: improve SSE types and testing utilities

- Enhanced SSE event type definitions with more precise typing
- Added type guard and safe type assertion functions for mock objects
- Updated security test suite to use new type utilities
- Improved type safety for token validation and mock function handling
- Refined event data type to support more flexible event structures
This commit is contained in:
jango-blockchained
2025-02-03 22:22:26 +01:00
parent a53cec7b28
commit 89f2278c25
2 changed files with 45 additions and 21 deletions

View File

@@ -12,9 +12,13 @@ export interface SSEClient {
connectionTime: number;
}
export interface HassEventData {
[key: string]: unknown;
}
export interface SSEEvent {
event_type: string;
data: unknown;
data: HassEventData;
origin: string;
time_fired: string;
context: {
@@ -39,4 +43,20 @@ export interface SSEManagerConfig {
}
export type MockSendFn = (data: string) => void;
export type MockSend = Mock<MockSendFn>;
export type MockSend = Mock<MockSendFn>;
export type ValidateTokenFn = (token: string, ip?: string) => { valid: boolean; error?: string };
export type MockValidateToken = Mock<ValidateTokenFn>;
// Type guard for mock functions
export function isMockFunction(value: unknown): value is Mock<unknown> {
return typeof value === 'function' && 'mock' in value;
}
// Safe type assertion for mock objects
export function asMockFunction<T extends (...args: any[]) => any>(value: unknown): Mock<T> {
if (!isMockFunction(value)) {
throw new Error('Value is not a mock function');
}
return value as Mock<T>;
}