context.test.tsโข2.2 kB
import { describe, expect, test, beforeEach, it } from "bun:test";
import { jest } from '@jest/globals';
import { z } from 'zod';
import { DomainSchema } from '../../src/schemas.js';
type MockResponse = { success: boolean };
// Define types for tool and server
interface Tool {
name: string;
description: string;
execute: (params: any) => Promise<MockResponse>;
parameters: z.ZodType<any>;
}
interface MockService {
[key: string]: jest.Mock<Promise<MockResponse>>;
}
interface MockServices {
light: {
turn_on: jest.Mock<Promise<MockResponse>>;
turn_off: jest.Mock<Promise<MockResponse>>;
};
climate: {
set_temperature: jest.Mock<Promise<MockResponse>>;
};
}
interface MockHassInstance {
services: MockServices;
}
// Mock LiteMCP class
class MockLiteMCP {
private tools: Tool[] = [];
constructor(public name: string, public version: string) { }
addTool(tool: Tool) {
this.tools.push(tool);
}
getTools() {
return this.tools;
}
}
const createMockFn = (): jest.Mock<Promise<MockResponse>> => {
return jest.fn<() => Promise<MockResponse>>().mockResolvedValue({ success: true });
};
// Mock the Home Assistant instance
const mockHassServices: MockHassInstance = {
services: {
light: {
turn_on: createMockFn(),
turn_off: createMockFn(),
},
climate: {
set_temperature: createMockFn(),
},
},
};
// Mock get_hass function
const get_hass = jest.fn<() => Promise<MockHassInstance>>().mockResolvedValue(mockHassServices);
describe('Context Tests', () => {
let mockTool: Tool;
beforeEach(() => {
mockTool = {
name: 'test_tool',
description: 'A test tool',
execute: jest.fn<(params: any) => Promise<MockResponse>>().mockResolvedValue({ success: true }),
parameters: z.object({
test: z.string()
})
};
});
// Add your test cases here
test('should execute tool successfully', async () => {
const result = await mockTool.execute({ test: 'value' });
expect(result.success).toBe(true);
});
});