Fix incorrect engine detection.
This commit is contained in:
@@ -11,6 +11,7 @@ import { ResolvedMediaCache } from '../../src/utils/ha/resolved-media';
|
||||
import { GenericCameraManagerEngine } from '../../src/camera-manager/generic/engine-generic';
|
||||
import { FrigateCameraManagerEngine } from '../../src/camera-manager/frigate/engine-frigate';
|
||||
import { MotionEyeCameraManagerEngine } from '../../src/camera-manager/motioneye/engine-motioneye';
|
||||
import { HassEntities } from 'home-assistant-js-websocket';
|
||||
|
||||
vi.mock('../../src/utils/ha/entity-registry');
|
||||
vi.mock('../../src/utils/ha/entity-registry/cache');
|
||||
@@ -31,8 +32,12 @@ const createCameraConfig = (config: Partial<CameraConfig>): CameraConfig => {
|
||||
return cameraConfigSchema.parse(config);
|
||||
};
|
||||
|
||||
const createHASS = (): HomeAssistant => {
|
||||
return mock<HomeAssistant>();
|
||||
const createHASS = (states?: HassEntities): HomeAssistant => {
|
||||
const hass = mock<HomeAssistant>();
|
||||
if (states) {
|
||||
hass.states = states;
|
||||
}
|
||||
return hass;
|
||||
};
|
||||
|
||||
const createEntity = (entity: Partial<Entity>): Entity => {
|
||||
@@ -62,6 +67,12 @@ describe('CameraManagerEngineFactory.getEngineForCamera()', () => {
|
||||
Engine.MotionEye,
|
||||
);
|
||||
});
|
||||
it('should get generic engine from config', async () => {
|
||||
const config = createCameraConfig({ engine: 'generic' });
|
||||
expect(await createFactory().getEngineForCamera(createHASS(), config)).toBe(
|
||||
Engine.Generic,
|
||||
);
|
||||
});
|
||||
it('should get frigate engine from auto config', async () => {
|
||||
const config = createCameraConfig({ engine: 'auto', camera_entity: 'camera.foo' });
|
||||
const entityRegistryManager = new EntityRegistryManager(new EntityCache());
|
||||
@@ -120,6 +131,49 @@ describe('CameraManagerEngineFactory.getEngineForCamera()', () => {
|
||||
|
||||
entityRegistryManager.getEntity = vi.fn().mockRejectedValue(new Error());
|
||||
|
||||
await expect(
|
||||
createFactory({
|
||||
entityRegistryManager: entityRegistryManager,
|
||||
}).getEngineForCamera(createHASS(), config),
|
||||
).rejects.toThrow();
|
||||
});
|
||||
it('should treat entity not in registry but with state as generic', async () => {
|
||||
const config = createCameraConfig({
|
||||
engine: 'auto',
|
||||
webrtc_card: { entity: 'camera.foo' },
|
||||
});
|
||||
const entityRegistryManager = new EntityRegistryManager(new EntityCache());
|
||||
|
||||
entityRegistryManager.getEntity = vi.fn().mockRejectedValue(new Error());
|
||||
|
||||
expect(
|
||||
await createFactory({
|
||||
entityRegistryManager: entityRegistryManager,
|
||||
}).getEngineForCamera(
|
||||
createHASS({
|
||||
'camera.foo': {
|
||||
entity_id: 'camera.foo',
|
||||
state: 'streaming',
|
||||
last_changed: 'bar',
|
||||
last_updated: 'baz',
|
||||
attributes: {},
|
||||
context: {
|
||||
id: 'context',
|
||||
user_id: null,
|
||||
parent_id: null,
|
||||
},
|
||||
},
|
||||
}),
|
||||
config,
|
||||
),
|
||||
).toBe(Engine.Generic);
|
||||
});
|
||||
it('should get engine from webrtc-card configuration', async () => {
|
||||
const config = createCameraConfig({ engine: 'auto', camera_entity: 'camera.foo' });
|
||||
const entityRegistryManager = new EntityRegistryManager(new EntityCache());
|
||||
|
||||
entityRegistryManager.getEntity = vi.fn().mockRejectedValue(new Error());
|
||||
|
||||
await expect(
|
||||
createFactory({
|
||||
entityRegistryManager: entityRegistryManager,
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import {
|
||||
capEndDate,
|
||||
convertRangeToCacheFriendlyTimes,
|
||||
getCameraEntityFromConfig,
|
||||
sortMedia,
|
||||
} from '../../src/camera-manager/util.js';
|
||||
import { ViewMedia, ViewMediaType } from '../../src/view/media.js';
|
||||
import { CameraConfig, cameraConfigSchema } from '../../src/types.js';
|
||||
|
||||
describe('convertRangeToCacheFriendlyTimes', () => {
|
||||
it('should return cache friendly within hour range', () => {
|
||||
expect(
|
||||
convertRangeToCacheFriendlyTimes({
|
||||
start: new Date('2023-04-29T14:01:02'),
|
||||
end: new Date('2023-04-29T14:11:03'),
|
||||
}),
|
||||
).toEqual({
|
||||
start: new Date('2023-04-29T14:00:00'),
|
||||
end: new Date('2023-04-29T14:59:59.999'),
|
||||
});
|
||||
});
|
||||
|
||||
it('should return cache friendly within day range', () => {
|
||||
expect(
|
||||
convertRangeToCacheFriendlyTimes({
|
||||
start: new Date('2023-04-29T14:01:02'),
|
||||
end: new Date('2023-04-29T15:11:03'),
|
||||
}),
|
||||
).toEqual({
|
||||
start: new Date('2023-04-29T00:00:00'),
|
||||
end: new Date('2023-04-29T23:59:59.999'),
|
||||
});
|
||||
});
|
||||
|
||||
it('should cap end date', () => {
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(new Date('2023-04-29T14:25'));
|
||||
expect(
|
||||
convertRangeToCacheFriendlyTimes(
|
||||
{
|
||||
start: new Date('2023-04-29T14:01:02'),
|
||||
end: new Date('2023-04-29T14:11:03'),
|
||||
},
|
||||
{ endCap: true },
|
||||
),
|
||||
).toEqual({
|
||||
start: new Date('2023-04-29T14:00:00'),
|
||||
end: new Date('2023-04-29T14:25:59.999'),
|
||||
});
|
||||
vi.useRealTimers();
|
||||
});
|
||||
});
|
||||
|
||||
describe('capEndDate', () => {
|
||||
it('should cap end date', () => {
|
||||
const fakeNow = new Date('2023-04-29T14:25');
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(fakeNow);
|
||||
|
||||
expect(capEndDate(new Date('2023-04-29T15:02'))).toEqual(fakeNow);
|
||||
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('should not cap end date', () => {
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(new Date('2023-04-29T14:25'));
|
||||
|
||||
const testDate = new Date('2023-04-29T14:24');
|
||||
expect(capEndDate(testDate)).toEqual(testDate);
|
||||
|
||||
vi.useRealTimers();
|
||||
});
|
||||
});
|
||||
|
||||
// ViewMedia itself has no native way to set startTime and ID that aren't linked
|
||||
// to an engine.
|
||||
class TestViewMedia extends ViewMedia {
|
||||
protected _ID: string | null;
|
||||
protected _startTime: Date;
|
||||
|
||||
constructor(
|
||||
ID: string | null,
|
||||
startTime: Date,
|
||||
mediaType: ViewMediaType,
|
||||
cameraID: string,
|
||||
) {
|
||||
super(mediaType, cameraID);
|
||||
this._ID = ID;
|
||||
this._startTime = startTime;
|
||||
}
|
||||
public getID(): string | null {
|
||||
return this._ID;
|
||||
}
|
||||
public getStartTime(): Date | null {
|
||||
return this._startTime;
|
||||
}
|
||||
}
|
||||
|
||||
describe('sortMedia', () => {
|
||||
const media_1 = new TestViewMedia(
|
||||
'id-1',
|
||||
new Date('2023-04-29T14:25'),
|
||||
'clip',
|
||||
'camera-1',
|
||||
);
|
||||
const media_2 = new TestViewMedia(
|
||||
'id-2',
|
||||
new Date('2023-04-29T14:26'),
|
||||
'clip',
|
||||
'camera-1',
|
||||
);
|
||||
const media_3_dup_id = new TestViewMedia(
|
||||
'id-2',
|
||||
new Date('2023-04-29T14:26'),
|
||||
'clip',
|
||||
'camera-1',
|
||||
);
|
||||
const media_4_no_id = new TestViewMedia(
|
||||
null,
|
||||
new Date('2023-04-29T14:27'),
|
||||
'clip',
|
||||
'camera-1',
|
||||
);
|
||||
|
||||
it('should sort sorted media', () => {
|
||||
const media = [media_1, media_2];
|
||||
expect(sortMedia(media)).toEqual(media);
|
||||
});
|
||||
it('should sort unsorted media', () => {
|
||||
expect(sortMedia([media_2, media_1])).toEqual([media_1, media_2]);
|
||||
});
|
||||
it('should remove duplicate id', () => {
|
||||
expect(sortMedia([media_1, media_2, media_3_dup_id])).toEqual([media_1, media_2]);
|
||||
});
|
||||
it('should remove de-duplicate by object if no id', () => {
|
||||
expect(sortMedia([media_1, media_2, media_4_no_id, media_4_no_id])).toEqual([
|
||||
media_1,
|
||||
media_2,
|
||||
media_4_no_id,
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getCameraEntityFromConfig', () => {
|
||||
const createCameraConfig = (config: Partial<CameraConfig>): CameraConfig => {
|
||||
return cameraConfigSchema.parse(config);
|
||||
};
|
||||
|
||||
it('should get camera_entity', () => {
|
||||
expect(getCameraEntityFromConfig(createCameraConfig({ camera_entity: 'foo' }))).toBe(
|
||||
'foo',
|
||||
);
|
||||
});
|
||||
it('should get camera_entity from webrtc_card config', () => {
|
||||
expect(
|
||||
getCameraEntityFromConfig(createCameraConfig({ webrtc_card: { entity: 'bar' } })),
|
||||
).toBe('bar');
|
||||
});
|
||||
it('should get no camera_entity', () => {
|
||||
expect(getCameraEntityFromConfig(createCameraConfig({}))).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user