Files
homeassistant-mcp/__tests__/hass/hass.test.ts
jango-blockchained 4e9ebbbc2c refactor: update TypeScript configuration and test utilities for improved type safety
- Modify tsconfig.json to relax strict type checking for gradual migration
- Update test files to use more flexible type checking and mocking
- Add type-safe mock and test utility functions
- Improve error handling and type inference in test suites
- Export Tool interface and tools list for better testing support
2025-02-05 09:16:21 +01:00

99 lines
2.6 KiB
TypeScript

import { jest, describe, beforeEach, afterAll, it, expect } from '@jest/globals';
import type { Mock } from 'jest-mock';
// Define types
interface MockResponse {
success: boolean;
}
type MockFn = () => Promise<MockResponse>;
interface MockService {
[key: string]: Mock<MockFn>;
}
interface MockServices {
light: {
turn_on: Mock<MockFn>;
turn_off: Mock<MockFn>;
};
climate: {
set_temperature: Mock<MockFn>;
};
}
interface MockHassInstance {
services: MockServices;
}
// Mock instance
let mockInstance: MockHassInstance | null = null;
const createMockFn = (): Mock<MockFn> => {
return jest.fn<MockFn>().mockImplementation(async () => ({ success: true }));
};
// Mock the digital-alchemy modules before tests
jest.unstable_mockModule('@digital-alchemy/core', () => ({
CreateApplication: jest.fn(() => ({
configuration: {},
bootstrap: async () => mockInstance,
services: {}
})),
TServiceParams: mock()
}));
jest.unstable_mockModule('@digital-alchemy/hass', () => ({
LIB_HASS: {
configuration: {},
services: {}
}
}));
describe('Home Assistant Connection', () => {
// Backup the original environment
const originalEnv = { ...process.env };
beforeEach(() => {
// Clear all mocks
jest.clearAllMocks();
// Initialize mock instance
mockInstance = {
services: {
light: {
turn_on: createMockFn(),
turn_off: createMockFn(),
},
climate: {
set_temperature: createMockFn(),
},
},
};
// Reset environment variables
process.env = { ...originalEnv };
});
afterAll(() => {
// Restore original environment
process.env = originalEnv;
});
test('should return a Home Assistant instance with services', async () => {
const { get_hass } = await import('../../src/hass/index.js');
const hass = await get_hass();
expect(hass).toBeDefined();
expect(hass.services).toBeDefined();
expect(typeof hass.services.light.turn_on).toBe('function');
expect(typeof hass.services.light.turn_off).toBe('function');
expect(typeof hass.services.climate.set_temperature).toBe('function');
});
test('should reuse the same instance on subsequent calls', async () => {
const { get_hass } = await import('../../src/hass/index.js');
const firstInstance = await get_hass();
const secondInstance = await get_hass();
expect(firstInstance).toBe(secondInstance);
});
});