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:
@@ -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>;
|
||||
}
|
||||
Reference in New Issue
Block a user