refactor: Refactor entity register manager (#1597)
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { RegistryCache } from '../../../../src/utils/ha/registry/cache';
|
||||
|
||||
interface TestCacheValue {
|
||||
id: string;
|
||||
val?: number;
|
||||
}
|
||||
|
||||
describe('RegistryCache', () => {
|
||||
describe('has', () => {
|
||||
it('positive', () => {
|
||||
const cache = new RegistryCache<TestCacheValue>((arg) => arg.id);
|
||||
cache.add({ id: 'test' });
|
||||
expect(cache.has('test')).toBeTruthy();
|
||||
});
|
||||
|
||||
it('negative', () => {
|
||||
const cache = new RegistryCache<TestCacheValue>((arg) => arg.id);
|
||||
cache.add({ id: 'test' });
|
||||
expect(cache.has('absent')).toBeFalsy();
|
||||
});
|
||||
});
|
||||
|
||||
it('getMatches', () => {
|
||||
const cache = new RegistryCache<TestCacheValue>((arg) => arg.id);
|
||||
cache.add([
|
||||
{ id: 'test-1', val: 1 },
|
||||
{ id: 'test-5', val: 5 },
|
||||
{ id: 'test-8', val: 8 },
|
||||
]);
|
||||
expect(cache.getMatches((obj) => !!obj.val && obj.val >= 5)).toEqual([
|
||||
{ id: 'test-5', val: 5 },
|
||||
{ id: 'test-8', val: 8 },
|
||||
]);
|
||||
});
|
||||
|
||||
describe('get', () => {
|
||||
it('positive', () => {
|
||||
const cache = new RegistryCache<TestCacheValue>((arg) => arg.id);
|
||||
cache.add({ id: 'test', val: 42 });
|
||||
expect(cache.get('test')).toEqual({ id: 'test', val: 42 });
|
||||
});
|
||||
|
||||
it('negative', () => {
|
||||
const cache = new RegistryCache<TestCacheValue>((arg) => arg.id);
|
||||
expect(cache.get('test')).toBeNull();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,110 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { homeAssistantWSRequest } from '../../../../../src/utils/ha';
|
||||
import {
|
||||
createEntityRegistryCache,
|
||||
EntityRegistryManager,
|
||||
} from '../../../../../src/utils/ha/registry/entity';
|
||||
import { createHASS, createRegistryEntity } from '../../../../test-utils.js';
|
||||
|
||||
vi.mock('../../../../../src/utils/ha');
|
||||
|
||||
describe('EntityRegistryManager', () => {
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('getEntity', () => {
|
||||
it('should not fetch when cached', async () => {
|
||||
const cache = createEntityRegistryCache();
|
||||
const testEntity = createRegistryEntity({ entity_id: 'test' });
|
||||
|
||||
cache.add(testEntity);
|
||||
|
||||
const manager = new EntityRegistryManager(cache);
|
||||
expect(await manager.getEntity(createHASS(), 'test')).toEqual(testEntity);
|
||||
|
||||
expect(homeAssistantWSRequest).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fetch and cache when not cached', async () => {
|
||||
const testEntity = createRegistryEntity({ entity_id: 'test' });
|
||||
|
||||
const manager = new EntityRegistryManager(createEntityRegistryCache());
|
||||
vi.mocked(homeAssistantWSRequest).mockResolvedValueOnce(testEntity);
|
||||
|
||||
expect(await manager.getEntity(createHASS(), 'test')).toEqual(testEntity);
|
||||
expect(homeAssistantWSRequest).toBeCalledTimes(1);
|
||||
|
||||
expect(await manager.getEntity(createHASS(), 'test')).toEqual(testEntity);
|
||||
expect(homeAssistantWSRequest).toBeCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should return null when entity does not exist', async () => {
|
||||
vi.mocked(homeAssistantWSRequest).mockRejectedValueOnce(new Error('Not found'));
|
||||
|
||||
const manager = new EntityRegistryManager(createEntityRegistryCache());
|
||||
expect(await manager.getEntity(createHASS(), 'missing')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
it('getEntities', () => {
|
||||
const cachedEntity = createRegistryEntity({ entity_id: 'cached' });
|
||||
const notCachedEntity = createRegistryEntity({ entity_id: 'not-cached' });
|
||||
|
||||
const cache = createEntityRegistryCache();
|
||||
cache.add(cachedEntity);
|
||||
|
||||
const manager = new EntityRegistryManager(cache);
|
||||
vi.mocked(homeAssistantWSRequest).mockResolvedValueOnce(notCachedEntity);
|
||||
vi.mocked(homeAssistantWSRequest).mockRejectedValueOnce(new Error('Not found'));
|
||||
|
||||
expect(
|
||||
manager.getEntities(createHASS(), ['cached', 'not-cached', 'missing']),
|
||||
).resolves.toEqual(
|
||||
new Map([
|
||||
['cached', cachedEntity],
|
||||
['not-cached', notCachedEntity],
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
it('fetchEntityList', async () => {
|
||||
const hass = createHASS();
|
||||
const entity = createRegistryEntity({ entity_id: 'cached' });
|
||||
vi.mocked(homeAssistantWSRequest).mockResolvedValueOnce([entity]);
|
||||
|
||||
const manager = new EntityRegistryManager(createEntityRegistryCache());
|
||||
|
||||
await manager.fetchEntityList(hass);
|
||||
|
||||
expect(homeAssistantWSRequest).toBeCalledTimes(1);
|
||||
expect(homeAssistantWSRequest).toBeCalledWith(expect.anything(), expect.anything(), {
|
||||
type: 'config/entity_registry/list',
|
||||
});
|
||||
|
||||
expect(await manager.getEntity(hass, 'cached')).toEqual(entity);
|
||||
expect(homeAssistantWSRequest).toBeCalledTimes(1);
|
||||
|
||||
await manager.fetchEntityList(hass);
|
||||
expect(homeAssistantWSRequest).toBeCalledTimes(1);
|
||||
});
|
||||
|
||||
it('getMatchingEntities', async () => {
|
||||
const matchingEntity = createRegistryEntity({ entity_id: 'matching' });
|
||||
const notMatchingEntity = createRegistryEntity({ entity_id: 'not-matching' });
|
||||
const hass = createHASS();
|
||||
|
||||
vi.mocked(homeAssistantWSRequest).mockResolvedValueOnce([
|
||||
matchingEntity,
|
||||
notMatchingEntity,
|
||||
]);
|
||||
|
||||
const manager = new EntityRegistryManager(createEntityRegistryCache());
|
||||
expect(
|
||||
await manager.getMatchingEntities(
|
||||
hass,
|
||||
(entity) => entity.entity_id == 'matching',
|
||||
),
|
||||
).toEqual([matchingEntity]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user