refactor: Cache device information centrally (#1598)

This commit is contained in:
Dermot Duffy
2024-09-29 17:17:40 -07:00
committed by GitHub
parent 02fb6e1d4c
commit 9393722fa6
14 changed files with 281 additions and 93 deletions
@@ -0,0 +1,73 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { homeAssistantWSRequest } from '../../../../../src/utils/ha';
import { createHASS, createRegistryDevice } from '../../../../test-utils.js';
import {
createDeviceRegistryCache,
DeviceRegistryManager,
} from '../../../../../src/utils/ha/registry/device';
vi.mock('../../../../../src/utils/ha');
vi.spyOn(global.console, 'warn').mockImplementation(() => true);
describe('DeviceRegistryManager', () => {
afterEach(() => {
vi.clearAllMocks();
});
describe('getDevice', () => {
it('should not fetch when cached', async () => {
const cache = createDeviceRegistryCache();
const testDevice = createRegistryDevice({ id: 'test' });
cache.add(testDevice);
const manager = new DeviceRegistryManager(cache);
expect(await manager.getDevice(createHASS(), 'test')).toEqual(testDevice);
expect(homeAssistantWSRequest).not.toHaveBeenCalled();
});
it('should fetch and cache when not cached', async () => {
const testDevice = createRegistryDevice({ id: 'test' });
const manager = new DeviceRegistryManager(createDeviceRegistryCache());
vi.mocked(homeAssistantWSRequest).mockResolvedValueOnce([testDevice]);
expect(await manager.getDevice(createHASS(), 'test')).toEqual(testDevice);
expect(homeAssistantWSRequest).toBeCalledTimes(1);
expect(await manager.getDevice(createHASS(), 'test')).toEqual(testDevice);
expect(homeAssistantWSRequest).toBeCalledTimes(1);
expect(await manager.getDevice(createHASS(), 'missing')).toBeNull();
// The fetch call is called exactly once.
expect(homeAssistantWSRequest).toBeCalledTimes(1);
});
it('should return null when fetch fails', async () => {
vi.mocked(homeAssistantWSRequest).mockRejectedValueOnce(new Error('Fetch error'));
const manager = new DeviceRegistryManager(createDeviceRegistryCache());
expect(await manager.getDevice(createHASS(), 'test')).toBeNull();
vi.mocked(expect(console.warn)).toBeCalledWith('Fetch error');
});
});
it('getMatchingDevices', async () => {
const matchingDevice = createRegistryDevice({ id: 'matching' });
const notMatchingDevice = createRegistryDevice({ id: 'not-matching' });
const hass = createHASS();
vi.mocked(homeAssistantWSRequest).mockResolvedValueOnce([
matchingDevice,
notMatchingDevice,
]);
const manager = new DeviceRegistryManager(createDeviceRegistryCache());
expect(
await manager.getMatchingDevices(hass, (entity) => entity.id == 'matching'),
).toEqual([matchingDevice]);
});
});
+38 -16
View File
@@ -7,6 +7,7 @@ import {
import { createHASS, createRegistryEntity } from '../../../../test-utils.js';
vi.mock('../../../../../src/utils/ha');
vi.spyOn(global.console, 'warn').mockImplementation(() => true);
describe('EntityRegistryManager', () => {
afterEach(() => {
@@ -44,10 +45,12 @@ describe('EntityRegistryManager', () => {
const manager = new EntityRegistryManager(createEntityRegistryCache());
expect(await manager.getEntity(createHASS(), 'missing')).toBeNull();
vi.mocked(expect(console.warn)).toBeCalledWith('Not found');
});
});
it('getEntities', () => {
it('getEntities', async () => {
const cachedEntity = createRegistryEntity({ entity_id: 'cached' });
const notCachedEntity = createRegistryEntity({ entity_id: 'not-cached' });
@@ -59,34 +62,53 @@ describe('EntityRegistryManager', () => {
vi.mocked(homeAssistantWSRequest).mockRejectedValueOnce(new Error('Not found'));
expect(
manager.getEntities(createHASS(), ['cached', 'not-cached', 'missing']),
).resolves.toEqual(
await manager.getEntities(createHASS(), ['cached', 'not-cached', 'missing']),
).toEqual(
new Map([
['cached', cachedEntity],
['not-cached', notCachedEntity],
]),
);
vi.mocked(expect(console.warn)).toBeCalledWith('Not found');
});
it('fetchEntityList', async () => {
const hass = createHASS();
const entity = createRegistryEntity({ entity_id: 'cached' });
vi.mocked(homeAssistantWSRequest).mockResolvedValueOnce([entity]);
describe('fetchEntityList', async () => {
it('should fetch entire entity list once', async () => {
const hass = createHASS();
const entity = createRegistryEntity({ entity_id: 'cached' });
vi.mocked(homeAssistantWSRequest).mockResolvedValueOnce([entity]);
const manager = new EntityRegistryManager(createEntityRegistryCache());
const manager = new EntityRegistryManager(createEntityRegistryCache());
await manager.fetchEntityList(hass);
await manager.fetchEntityList(hass);
expect(homeAssistantWSRequest).toBeCalledTimes(1);
expect(homeAssistantWSRequest).toBeCalledWith(expect.anything(), expect.anything(), {
type: 'config/entity_registry/list',
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);
});
expect(await manager.getEntity(hass, 'cached')).toEqual(entity);
expect(homeAssistantWSRequest).toBeCalledTimes(1);
it('should log to console on error', async () => {
const hass = createHASS();
vi.mocked(homeAssistantWSRequest).mockRejectedValueOnce(new Error('Fetch error'));
await manager.fetchEntityList(hass);
expect(homeAssistantWSRequest).toBeCalledTimes(1);
const manager = new EntityRegistryManager(createEntityRegistryCache());
await manager.fetchEntityList(hass);
vi.mocked(expect(console.warn)).toBeCalledWith('Fetch error');
});
});
it('getMatchingEntities', async () => {