Enhance Jest configuration and testing infrastructure

- Updated Jest configuration to support ESM and improve test coverage
- Added comprehensive test files for helpers, index, context, and HASS integration
- Configured coverage reporting and added new test scripts
- Updated Jest resolver to handle module resolution for chalk and related packages
- Introduced new test setup files for mocking and environment configuration
This commit is contained in:
jango-blockchained
2025-01-30 09:04:07 +01:00
parent f908d83cbf
commit d7e5fcf764
15 changed files with 1077 additions and 282 deletions

186
src/schemas/hass.ts Normal file
View File

@@ -0,0 +1,186 @@
import { JSONSchemaType } from 'ajv';
import type HomeAssistant from '../types/hass.js';
export const entitySchema: JSONSchemaType<HomeAssistant.Entity> = {
type: 'object',
properties: {
entity_id: { type: 'string' },
state: { type: 'string' },
attributes: {
type: 'object',
additionalProperties: true
},
last_changed: { type: 'string' },
last_updated: { type: 'string' },
context: {
type: 'object',
properties: {
id: { type: 'string' },
parent_id: { type: 'string', nullable: true },
user_id: { type: 'string', nullable: true }
},
required: ['id'],
additionalProperties: false
}
},
required: ['entity_id', 'state', 'attributes', 'last_changed', 'last_updated', 'context'],
additionalProperties: false
};
export const serviceSchema: JSONSchemaType<HomeAssistant.Service> = {
type: 'object',
properties: {
domain: { type: 'string' },
service: { type: 'string' },
target: {
type: 'object',
nullable: true,
properties: {
entity_id: {
anyOf: [
{ type: 'string' },
{ type: 'array', items: { type: 'string' } }
],
nullable: true
},
device_id: {
anyOf: [
{ type: 'string' },
{ type: 'array', items: { type: 'string' } }
],
nullable: true
},
area_id: {
anyOf: [
{ type: 'string' },
{ type: 'array', items: { type: 'string' } }
],
nullable: true
}
},
additionalProperties: false
},
service_data: {
type: 'object',
nullable: true,
additionalProperties: true
}
},
required: ['domain', 'service'],
additionalProperties: false
};
export const stateChangedEventSchema: JSONSchemaType<HomeAssistant.StateChangedEvent> = {
type: 'object',
properties: {
event_type: { type: 'string', const: 'state_changed' },
data: {
type: 'object',
properties: {
entity_id: { type: 'string' },
new_state: {
anyOf: [
{
type: 'object',
properties: {
entity_id: { type: 'string' },
state: { type: 'string' },
attributes: {
type: 'object',
additionalProperties: true
},
last_changed: { type: 'string' },
last_updated: { type: 'string' },
context: {
type: 'object',
properties: {
id: { type: 'string' },
parent_id: { type: 'string', nullable: true },
user_id: { type: 'string', nullable: true }
},
required: ['id'],
additionalProperties: false
}
},
required: ['entity_id', 'state', 'attributes', 'last_changed', 'last_updated', 'context'],
additionalProperties: false
},
{ type: 'null' }
]
},
old_state: {
anyOf: [
{
type: 'object',
properties: {
entity_id: { type: 'string' },
state: { type: 'string' },
attributes: {
type: 'object',
additionalProperties: true
},
last_changed: { type: 'string' },
last_updated: { type: 'string' },
context: {
type: 'object',
properties: {
id: { type: 'string' },
parent_id: { type: 'string', nullable: true },
user_id: { type: 'string', nullable: true }
},
required: ['id'],
additionalProperties: false
}
},
required: ['entity_id', 'state', 'attributes', 'last_changed', 'last_updated', 'context'],
additionalProperties: false
},
{ type: 'null' }
]
}
},
required: ['entity_id', 'new_state', 'old_state'],
additionalProperties: false
},
origin: { type: 'string' },
time_fired: { type: 'string' },
context: {
type: 'object',
properties: {
id: { type: 'string' },
parent_id: { type: 'string', nullable: true },
user_id: { type: 'string', nullable: true }
},
required: ['id'],
additionalProperties: false
}
},
required: ['event_type', 'data', 'origin', 'time_fired', 'context'],
additionalProperties: false
};
export const configSchema: JSONSchemaType<HomeAssistant.Config> = {
type: 'object',
properties: {
latitude: { type: 'number' },
longitude: { type: 'number' },
elevation: { type: 'number' },
unit_system: {
type: 'object',
properties: {
length: { type: 'string' },
mass: { type: 'string' },
temperature: { type: 'string' },
volume: { type: 'string' }
},
required: ['length', 'mass', 'temperature', 'volume'],
additionalProperties: false
},
location_name: { type: 'string' },
time_zone: { type: 'string' },
components: { type: 'array', items: { type: 'string' } },
version: { type: 'string' }
},
required: ['latitude', 'longitude', 'elevation', 'unit_system', 'location_name', 'time_zone', 'components', 'version'],
additionalProperties: false
};

81
src/types/hass.d.ts vendored Normal file
View File

@@ -0,0 +1,81 @@
declare namespace HomeAssistant {
interface Entity {
entity_id: string;
state: string;
attributes: Record<string, any>;
last_changed: string;
last_updated: string;
context: {
id: string;
parent_id?: string;
user_id?: string;
};
}
interface Service {
domain: string;
service: string;
target?: {
entity_id?: string | string[];
device_id?: string | string[];
area_id?: string | string[];
};
service_data?: Record<string, any>;
}
interface WebsocketMessage {
type: string;
id?: number;
[key: string]: any;
}
interface AuthMessage extends WebsocketMessage {
type: 'auth';
access_token: string;
}
interface SubscribeEventsMessage extends WebsocketMessage {
type: 'subscribe_events';
event_type?: string;
}
interface StateChangedEvent {
event_type: 'state_changed';
data: {
entity_id: string;
new_state: Entity | null;
old_state: Entity | null;
};
origin: string;
time_fired: string;
context: {
id: string;
parent_id?: string;
user_id?: string;
};
}
interface Config {
latitude: number;
longitude: number;
elevation: number;
unit_system: {
length: string;
mass: string;
temperature: string;
volume: string;
};
location_name: string;
time_zone: string;
components: string[];
version: string;
}
interface ApiError {
code: string;
message: string;
details?: Record<string, any>;
}
}
export = HomeAssistant;