committed by
dermotduffy
parent
9384785d37
commit
1cd5520154
@@ -133,7 +133,12 @@ describe('Camera', () => {
|
||||
'http://go2rtc/api/streams?src=stream&video=all&audio=allµphone',
|
||||
sign: false,
|
||||
},
|
||||
expect.anything(),
|
||||
expect.objectContaining({
|
||||
dynamic: true,
|
||||
ssl_verification: true,
|
||||
ssl_ciphers: 'default',
|
||||
enabled: false,
|
||||
}),
|
||||
);
|
||||
|
||||
expect(camera.getCapabilities()?.has('2-way-audio')).toBe(true);
|
||||
@@ -184,7 +189,99 @@ describe('Camera', () => {
|
||||
expect.anything(),
|
||||
20,
|
||||
expect.anything(),
|
||||
expect.objectContaining({ enabled: false }),
|
||||
);
|
||||
});
|
||||
|
||||
it('should pass proxy config when web proxy is available', async () => {
|
||||
const camera = new Camera(
|
||||
createCameraConfig({
|
||||
go2rtc: {
|
||||
url: 'http://go2rtc',
|
||||
stream: 'stream',
|
||||
},
|
||||
proxy: {
|
||||
live: true,
|
||||
},
|
||||
}),
|
||||
new GenericCameraManagerEngine(mock<StateWatcherSubscriptionInterface>()),
|
||||
);
|
||||
|
||||
vi.mocked(liveProviderSupports2WayAudio).mockResolvedValue(true);
|
||||
|
||||
const hass = createHASS();
|
||||
hass.config.components = ['hass_web_proxy'];
|
||||
|
||||
await camera.initialize({
|
||||
hass,
|
||||
stateWatcher: mock<StateWatcherSubscriptionInterface>(),
|
||||
});
|
||||
|
||||
expect(liveProviderSupports2WayAudio).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
expect.anything(),
|
||||
2,
|
||||
expect.anything(),
|
||||
{
|
||||
dynamic: true,
|
||||
ssl_verification: true,
|
||||
ssl_ciphers: 'default',
|
||||
live: true,
|
||||
media: false,
|
||||
enabled: true,
|
||||
enforce: true,
|
||||
},
|
||||
);
|
||||
|
||||
expect(camera.getCapabilities()?.has('2-way-audio')).toBe(true);
|
||||
});
|
||||
|
||||
it('should return live proxy config', () => {
|
||||
const camera = new Camera(
|
||||
createCameraConfig({
|
||||
proxy: { live: true },
|
||||
}),
|
||||
new GenericCameraManagerEngine(mock<StateWatcherSubscriptionInterface>()),
|
||||
);
|
||||
expect(camera.getLiveProxyConfig()).toEqual(
|
||||
expect.objectContaining({ enabled: true, enforce: true }),
|
||||
);
|
||||
});
|
||||
|
||||
it('should return media proxy config', () => {
|
||||
const camera = new Camera(
|
||||
createCameraConfig({
|
||||
proxy: { media: true },
|
||||
}),
|
||||
new GenericCameraManagerEngine(mock<StateWatcherSubscriptionInterface>()),
|
||||
);
|
||||
expect(camera.getMediaProxyConfig()).toEqual(
|
||||
expect.objectContaining({ enabled: true, enforce: true }),
|
||||
);
|
||||
});
|
||||
|
||||
it('should not enforce live proxy config when live proxying is auto', () => {
|
||||
const camera = new Camera(
|
||||
createCameraConfig({
|
||||
live_provider: 'go2rtc',
|
||||
go2rtc: { url: 'http://go2rtc' },
|
||||
}),
|
||||
new GenericCameraManagerEngine(mock<StateWatcherSubscriptionInterface>()),
|
||||
);
|
||||
expect(camera.getLiveProxyConfig()).toEqual(
|
||||
expect.objectContaining({ enabled: true, enforce: false }),
|
||||
);
|
||||
});
|
||||
|
||||
it('should not enforce media proxy config when media proxying is auto', () => {
|
||||
const camera = new Camera(
|
||||
createCameraConfig({
|
||||
proxy: { media: 'auto' },
|
||||
}),
|
||||
new GenericCameraManagerEngine(mock<StateWatcherSubscriptionInterface>()),
|
||||
);
|
||||
expect(camera.getMediaProxyConfig()).toEqual(
|
||||
expect.objectContaining({ enabled: false, enforce: false }),
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -119,4 +119,10 @@ describe('supports2WayAudio', () => {
|
||||
|
||||
expect(await supports2WayAudio(hass, 2, endpoint)).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false if proxied endpoint is null', async () => {
|
||||
vi.mocked(createProxiedEndpointIfNecessary).mockResolvedValue(null);
|
||||
|
||||
expect(await supports2WayAudio(hass, 2, endpoint)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,7 +6,7 @@ import { createCardAPI } from '../../../test-utils';
|
||||
describe('ActionSet', () => {
|
||||
it('should execute single action', async () => {
|
||||
const api = createCardAPI();
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
|
||||
const set = new ActionSet({}, createLogAction('Hello, world!'));
|
||||
|
||||
const consoleSpy = vi.spyOn(global.console, 'info').mockReturnValue(undefined);
|
||||
@@ -30,7 +30,7 @@ describe('ActionSet', () => {
|
||||
|
||||
it('should stop execution', async () => {
|
||||
const api = createCardAPI();
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
|
||||
const set = new ActionSet({}, createLogAction('Hello, world!'));
|
||||
|
||||
const consoleSpy = vi.spyOn(global.console, 'info').mockReturnValue(undefined);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { format } from 'date-fns';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { ViewItemManager } from '../../../src/card-controller/view/item-manager';
|
||||
import { homeAssistantSignPath } from '../../../src/ha/sign-path.js';
|
||||
import { homeAssistantGetSignedURLIfNecessary } from '../../../src/ha/sign-path.js';
|
||||
import { downloadURL } from '../../../src/utils/download';
|
||||
import { ViewFolder, ViewMediaType } from '../../../src/view/item';
|
||||
import {
|
||||
@@ -57,6 +57,12 @@ describe('ViewItemManager', () => {
|
||||
});
|
||||
|
||||
describe('download', () => {
|
||||
beforeEach(() => {
|
||||
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockImplementation(
|
||||
async (_hass, endpoint) => endpoint.endpoint,
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
@@ -85,7 +91,7 @@ describe('ViewItemManager', () => {
|
||||
});
|
||||
|
||||
const signError = new Error('sign-error');
|
||||
vi.mocked(homeAssistantSignPath).mockRejectedValue(signError);
|
||||
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockRejectedValue(signError);
|
||||
|
||||
expect(await manager.download(item)).toBe(false);
|
||||
expect(api.getMessageManager().setErrorIfHigherPriority).toHaveBeenCalledWith(
|
||||
@@ -108,7 +114,9 @@ describe('ViewItemManager', () => {
|
||||
endpoint: 'foo',
|
||||
});
|
||||
|
||||
vi.mocked(homeAssistantSignPath).mockResolvedValue('http://foo/signed-url');
|
||||
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockResolvedValue(
|
||||
'http://foo/signed-url',
|
||||
);
|
||||
|
||||
expect(await manager.download(item)).toBe(true);
|
||||
expect(downloadURL).toBeCalledWith(
|
||||
@@ -129,7 +137,9 @@ describe('ViewItemManager', () => {
|
||||
endpoint: 'foo',
|
||||
});
|
||||
|
||||
vi.mocked(homeAssistantSignPath).mockResolvedValue('http://foo/signed-url');
|
||||
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockResolvedValue(
|
||||
'http://foo/signed-url',
|
||||
);
|
||||
|
||||
expect(await manager.download(item)).toBe(true);
|
||||
expect(downloadURL).toBeCalledWith('http://foo/signed-url', 'media_id.mp4');
|
||||
@@ -147,8 +157,6 @@ describe('ViewItemManager', () => {
|
||||
endpoint: 'foo',
|
||||
});
|
||||
|
||||
expect(homeAssistantSignPath).not.toBeCalled();
|
||||
|
||||
expect(await manager.download(item)).toBe(true);
|
||||
expect(downloadURL).toBeCalledWith('foo', 'camera-office_id.mp4');
|
||||
});
|
||||
|
||||
@@ -9,25 +9,50 @@ describe('CachedValueController', () => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('should not restart timer on hostUpdate if not connected', () => {
|
||||
const host = mock<ReactiveControllerHost & HTMLElement>();
|
||||
const callback = vi.fn().mockReturnValue(42);
|
||||
let refreshSeconds: number | null = 10;
|
||||
|
||||
const controller = new CachedValueController(host, () => refreshSeconds, callback);
|
||||
|
||||
Object.defineProperty(host, 'isConnected', { get: () => false });
|
||||
|
||||
refreshSeconds = 20;
|
||||
controller.hostUpdate();
|
||||
expect(controller.hasTimer()).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should not restart timer if unchanged on hostUpdate', () => {
|
||||
const host = mock<ReactiveControllerHost & HTMLElement>();
|
||||
const callback = vi.fn().mockReturnValue(42);
|
||||
const refreshSeconds: number | null = 10;
|
||||
|
||||
const controller = new CachedValueController(host, () => refreshSeconds, callback);
|
||||
|
||||
Object.defineProperty(host, 'isConnected', { get: () => true });
|
||||
|
||||
// This starts the timer as isConnected is true
|
||||
controller.hostConnected();
|
||||
|
||||
const startTimerSpy = vi.spyOn(controller, 'startTimer');
|
||||
|
||||
controller.hostUpdate();
|
||||
|
||||
// Should not restart since refreshSeconds hasn't changed
|
||||
expect(startTimerSpy).not.toBeCalled();
|
||||
});
|
||||
|
||||
it('should construct', () => {
|
||||
const host = mock<ReactiveControllerHost>();
|
||||
const host = mock<ReactiveControllerHost & HTMLElement>();
|
||||
const callback = vi.fn();
|
||||
const controller = new CachedValueController(host, 10, callback);
|
||||
const controller = new CachedValueController(host, () => 10, callback);
|
||||
|
||||
expect(controller).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should remove host', () => {
|
||||
const host = mock<ReactiveControllerHost>();
|
||||
const callback = vi.fn();
|
||||
const controller = new CachedValueController(host, 10, callback);
|
||||
|
||||
controller.removeController();
|
||||
expect(host.removeController).toBeCalled();
|
||||
});
|
||||
|
||||
it('should have timer', () => {
|
||||
const host = mock<ReactiveControllerHost>();
|
||||
const host = mock<ReactiveControllerHost & HTMLElement>();
|
||||
const callback = vi.fn();
|
||||
const startCallback = vi.fn();
|
||||
const stopCallback = vi.fn();
|
||||
@@ -36,7 +61,7 @@ describe('CachedValueController', () => {
|
||||
|
||||
const controller = new CachedValueController(
|
||||
host,
|
||||
10,
|
||||
() => 10,
|
||||
callback,
|
||||
startCallback,
|
||||
stopCallback,
|
||||
@@ -49,13 +74,13 @@ describe('CachedValueController', () => {
|
||||
vi.runOnlyPendingTimers();
|
||||
expect(callback).toBeCalled();
|
||||
expect(host.requestUpdate).toBeCalled();
|
||||
expect(controller.value).toBe(3);
|
||||
expect(controller.getValue()).toBe(3);
|
||||
|
||||
callback.mockReturnValue(4);
|
||||
vi.runOnlyPendingTimers();
|
||||
expect(callback).toBeCalled();
|
||||
expect(host.requestUpdate).toBeCalled();
|
||||
expect(controller.value).toBe(4);
|
||||
expect(controller.getValue()).toBe(4);
|
||||
|
||||
expect(controller.hasTimer()).toBeTruthy();
|
||||
|
||||
@@ -68,42 +93,129 @@ describe('CachedValueController', () => {
|
||||
});
|
||||
|
||||
it('should clear value', () => {
|
||||
const host = mock<ReactiveControllerHost>();
|
||||
const host = mock<ReactiveControllerHost & HTMLElement>();
|
||||
const callback = vi.fn().mockReturnValue(42);
|
||||
|
||||
vi.useFakeTimers();
|
||||
|
||||
const controller = new CachedValueController(host, 10, callback);
|
||||
const controller = new CachedValueController(host, () => 10, callback);
|
||||
controller.startTimer();
|
||||
|
||||
vi.runOnlyPendingTimers();
|
||||
expect(controller.value).equal(42);
|
||||
expect(controller.getValue()).equal(42);
|
||||
|
||||
controller.clearValue();
|
||||
expect(controller.value).toBeUndefined();
|
||||
expect(controller.getValue()).toBeNull();
|
||||
});
|
||||
|
||||
it('should connect and disconnect host', () => {
|
||||
const host = mock<ReactiveControllerHost>();
|
||||
const host = mock<ReactiveControllerHost & HTMLElement>();
|
||||
const callback = vi.fn().mockReturnValue(43);
|
||||
const startCallback = vi.fn();
|
||||
const stopCallback = vi.fn();
|
||||
|
||||
const controller = new CachedValueController(
|
||||
host,
|
||||
10,
|
||||
() => 10,
|
||||
callback,
|
||||
startCallback,
|
||||
stopCallback,
|
||||
);
|
||||
|
||||
controller.hostConnected();
|
||||
expect(controller.value).equal(43);
|
||||
expect(controller.getValue()).equal(43);
|
||||
expect(startCallback).toBeCalled();
|
||||
expect(host.requestUpdate).toBeCalled();
|
||||
|
||||
controller.hostDisconnected();
|
||||
expect(controller.value).toBeUndefined();
|
||||
expect(controller.getValue()).toBeNull();
|
||||
expect(stopCallback).toBeCalled();
|
||||
});
|
||||
|
||||
it('should call timer tick callback on each tick before updateValue', () => {
|
||||
const host = mock<ReactiveControllerHost & HTMLElement>();
|
||||
const callback = vi.fn().mockReturnValue('value');
|
||||
const tickCallback = vi.fn();
|
||||
|
||||
vi.useFakeTimers();
|
||||
|
||||
const controller = new CachedValueController(
|
||||
host,
|
||||
() => 5,
|
||||
callback,
|
||||
undefined,
|
||||
undefined,
|
||||
tickCallback,
|
||||
);
|
||||
|
||||
controller.startTimer();
|
||||
|
||||
vi.runOnlyPendingTimers();
|
||||
expect(tickCallback).toHaveBeenCalledTimes(1);
|
||||
expect(callback).toHaveBeenCalledTimes(1);
|
||||
|
||||
vi.runOnlyPendingTimers();
|
||||
expect(tickCallback).toHaveBeenCalledTimes(2);
|
||||
expect(callback).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('should not call timerTickCallback on manual updateValue', () => {
|
||||
const host = mock<ReactiveControllerHost & HTMLElement>();
|
||||
const callback = vi.fn().mockReturnValue('value');
|
||||
const tickCallback = vi.fn();
|
||||
|
||||
const controller = new CachedValueController(
|
||||
host,
|
||||
() => 5,
|
||||
callback,
|
||||
undefined,
|
||||
undefined,
|
||||
tickCallback,
|
||||
);
|
||||
|
||||
controller.updateValue();
|
||||
expect(callback).toHaveBeenCalledTimes(1);
|
||||
expect(tickCallback).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should restart timer with new interval on hostUpdate', () => {
|
||||
const host = mock<ReactiveControllerHost & HTMLElement>();
|
||||
const callback = vi.fn().mockReturnValue(42);
|
||||
let refreshSeconds: number | null = null;
|
||||
|
||||
const controller = new CachedValueController(host, () => refreshSeconds, callback);
|
||||
|
||||
vi.useFakeTimers();
|
||||
Object.defineProperty(host, 'isConnected', { get: () => true });
|
||||
controller.hostConnected();
|
||||
expect(controller.hasTimer()).toBeFalsy();
|
||||
|
||||
refreshSeconds = 20;
|
||||
controller.hostUpdate();
|
||||
|
||||
// Timer should have been restarted. Fast forward 15 seconds. If it didn't
|
||||
// restart, it would fire at 10 seconds. Since it restarted at 20 seconds,
|
||||
// it shouldn't fire at 15 seconds.
|
||||
callback.mockClear();
|
||||
vi.advanceTimersByTime(15 * 1000);
|
||||
expect(callback).not.toBeCalled();
|
||||
|
||||
vi.advanceTimersByTime(5 * 1000);
|
||||
expect(callback).toBeCalled();
|
||||
|
||||
// Now set it to null -> stops timer
|
||||
refreshSeconds = null;
|
||||
controller.hostUpdate();
|
||||
expect(controller.hasTimer()).toBeFalsy();
|
||||
|
||||
// Now set it to 0 -> stops timer
|
||||
refreshSeconds = 0;
|
||||
controller.hostUpdate();
|
||||
expect(controller.hasTimer()).toBeFalsy();
|
||||
|
||||
// Now set it to negative -> stops timer
|
||||
refreshSeconds = -1;
|
||||
controller.hostUpdate();
|
||||
expect(controller.hasTimer()).toBeFalsy();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -144,7 +144,7 @@ describe('UpdatingImageMediaPlayerController', () => {
|
||||
it('should return screenshot URL with cached value controller', async () => {
|
||||
const url = 'data:image/png;base64,';
|
||||
const cachedValueController = mock<CachedValueController<string>>();
|
||||
Object.defineProperty(cachedValueController, 'value', { value: url });
|
||||
cachedValueController.getValue.mockReturnValue(url);
|
||||
|
||||
const controller = new UpdatingImageMediaPlayerController(
|
||||
createLitElement(),
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -77,6 +77,12 @@ describe('config defaults', () => {
|
||||
elements: [],
|
||||
image: {
|
||||
mode: 'auto',
|
||||
proxy: {
|
||||
dynamic: true,
|
||||
ssl_ciphers: 'auto',
|
||||
ssl_verification: 'auto',
|
||||
enabled: false,
|
||||
},
|
||||
refresh_seconds: 1,
|
||||
zoomable: true,
|
||||
},
|
||||
|
||||
+35
-17
@@ -1,7 +1,7 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { z } from 'zod';
|
||||
import { homeAssistantSignAndFetch } from '../../src/ha/fetch';
|
||||
import { homeAssistantSignPath } from '../../src/ha/sign-path';
|
||||
import { homeAssistantGetSignedURLIfNecessary } from '../../src/ha/sign-path';
|
||||
import { AdvancedCameraCardError, Endpoint } from '../../src/types';
|
||||
import { createHASS } from '../test-utils';
|
||||
|
||||
@@ -18,7 +18,6 @@ describe('homeAssistantSignAndFetch', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
vi.mocked(homeAssistantSignPath).mockResolvedValue('http://signed');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -27,26 +26,31 @@ describe('homeAssistantSignAndFetch', () => {
|
||||
});
|
||||
|
||||
it('should return parsed data on successful call with endpoint', async () => {
|
||||
const endpoint: Endpoint = { endpoint: 'http://example.com' };
|
||||
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockResolvedValue(
|
||||
'http://example.com',
|
||||
);
|
||||
fetchMock.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => response,
|
||||
});
|
||||
|
||||
const endpoint: Endpoint = { endpoint: 'http://example.com' };
|
||||
expect(await homeAssistantSignAndFetch(createHASS(), endpoint, schema)).toEqual(
|
||||
response,
|
||||
);
|
||||
expect(homeAssistantSignPath).not.toHaveBeenCalled();
|
||||
const hass = createHASS();
|
||||
expect(await homeAssistantSignAndFetch(hass, endpoint, schema)).toEqual(response);
|
||||
expect(homeAssistantGetSignedURLIfNecessary).toHaveBeenCalledWith(hass, endpoint);
|
||||
expect(fetchMock).toHaveBeenCalledWith('http://example.com', {});
|
||||
});
|
||||
|
||||
it('should pass timeout signal when timeoutSeconds is provided', async () => {
|
||||
const endpoint: Endpoint = { endpoint: 'http://example.com' };
|
||||
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockResolvedValue(
|
||||
'http://example.com',
|
||||
);
|
||||
fetchMock.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => response,
|
||||
});
|
||||
|
||||
const endpoint: Endpoint = { endpoint: 'http://example.com' };
|
||||
expect(
|
||||
await homeAssistantSignAndFetch(createHASS(), endpoint, schema, {
|
||||
timeoutSeconds: 5,
|
||||
@@ -58,6 +62,7 @@ describe('homeAssistantSignAndFetch', () => {
|
||||
});
|
||||
|
||||
it('should sign path if requested', async () => {
|
||||
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockResolvedValue('http://signed');
|
||||
fetchMock.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => response,
|
||||
@@ -69,12 +74,14 @@ describe('homeAssistantSignAndFetch', () => {
|
||||
};
|
||||
const hass = createHASS();
|
||||
expect(await homeAssistantSignAndFetch(hass, endpoint, schema)).toEqual(response);
|
||||
expect(homeAssistantSignPath).toHaveBeenCalledWith(hass, 'http://example.com');
|
||||
expect(homeAssistantGetSignedURLIfNecessary).toHaveBeenCalledWith(hass, endpoint);
|
||||
expect(fetchMock).toHaveBeenCalledWith('http://signed', {});
|
||||
});
|
||||
|
||||
it('should throw on sign failure', async () => {
|
||||
vi.mocked(homeAssistantSignPath).mockRejectedValueOnce(new Error('Sign failed'));
|
||||
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockRejectedValueOnce(
|
||||
new Error('Sign failed'),
|
||||
);
|
||||
|
||||
const endpoint: Endpoint = {
|
||||
endpoint: 'http://example.com',
|
||||
@@ -85,8 +92,8 @@ describe('homeAssistantSignAndFetch', () => {
|
||||
).rejects.toThrow(/Could not sign Home Assistant URL/);
|
||||
});
|
||||
|
||||
it('should throw if sign path returns null', async () => {
|
||||
vi.mocked(homeAssistantSignPath).mockResolvedValue(null);
|
||||
it('should throw if sign endpoint returns null', async () => {
|
||||
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockResolvedValue(null);
|
||||
|
||||
const endpoint: Endpoint = {
|
||||
endpoint: 'http://example.com',
|
||||
@@ -98,9 +105,12 @@ describe('homeAssistantSignAndFetch', () => {
|
||||
});
|
||||
|
||||
it('should throw on fetch failure', async () => {
|
||||
const endpoint: Endpoint = { endpoint: 'http://example.com' };
|
||||
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockResolvedValue(
|
||||
'http://example.com',
|
||||
);
|
||||
fetchMock.mockRejectedValueOnce(new Error('Fetch failed'));
|
||||
|
||||
const endpoint: Endpoint = { endpoint: 'http://example.com' };
|
||||
try {
|
||||
await homeAssistantSignAndFetch(createHASS(), endpoint, schema);
|
||||
expect.fail('Should have thrown');
|
||||
@@ -117,6 +127,10 @@ describe('homeAssistantSignAndFetch', () => {
|
||||
});
|
||||
|
||||
it('should throw on non-ok response', async () => {
|
||||
const endpoint: Endpoint = { endpoint: 'http://example.com' };
|
||||
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockResolvedValue(
|
||||
'http://example.com',
|
||||
);
|
||||
const response = {
|
||||
ok: false,
|
||||
status: 404,
|
||||
@@ -124,7 +138,6 @@ describe('homeAssistantSignAndFetch', () => {
|
||||
} as Response;
|
||||
fetchMock.mockResolvedValueOnce(response);
|
||||
|
||||
const endpoint: Endpoint = { endpoint: 'http://example.com' };
|
||||
try {
|
||||
await homeAssistantSignAndFetch(createHASS(), endpoint, schema);
|
||||
expect.fail('Should have thrown');
|
||||
@@ -140,6 +153,10 @@ describe('homeAssistantSignAndFetch', () => {
|
||||
});
|
||||
|
||||
it('should throw on JSON parse failure', async () => {
|
||||
const endpoint: Endpoint = { endpoint: 'http://example.com' };
|
||||
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockResolvedValue(
|
||||
'http://example.com',
|
||||
);
|
||||
const response = {
|
||||
ok: true,
|
||||
json: async () => {
|
||||
@@ -148,7 +165,6 @@ describe('homeAssistantSignAndFetch', () => {
|
||||
} as unknown as Response;
|
||||
fetchMock.mockResolvedValueOnce(response);
|
||||
|
||||
const endpoint: Endpoint = { endpoint: 'http://example.com' };
|
||||
try {
|
||||
await homeAssistantSignAndFetch(createHASS(), endpoint, schema);
|
||||
expect.fail('Should have thrown');
|
||||
@@ -166,6 +182,10 @@ describe('homeAssistantSignAndFetch', () => {
|
||||
});
|
||||
|
||||
it('should throw on schema validation failure', async () => {
|
||||
const endpoint: Endpoint = { endpoint: 'http://example.com' };
|
||||
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockResolvedValue(
|
||||
'http://example.com',
|
||||
);
|
||||
const data = { val: 'string' };
|
||||
const response = {
|
||||
ok: true,
|
||||
@@ -173,8 +193,6 @@ describe('homeAssistantSignAndFetch', () => {
|
||||
} as unknown as Response;
|
||||
fetchMock.mockResolvedValueOnce(response);
|
||||
|
||||
const endpoint: Endpoint = { endpoint: 'http://example.com' };
|
||||
|
||||
try {
|
||||
await homeAssistantSignAndFetch(createHASS(), endpoint, schema);
|
||||
expect.fail('Should have thrown');
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { homeAssistantSignPath } from '../../src/ha/sign-path';
|
||||
import {
|
||||
homeAssistantGetSignedURLIfNecessary,
|
||||
homeAssistantSignPath,
|
||||
} from '../../src/ha/sign-path';
|
||||
import { homeAssistantWSRequest } from '../../src/ha/ws-request.js';
|
||||
import { signedPathSchema } from '../../src/types';
|
||||
import { createHASS } from '../test-utils';
|
||||
@@ -36,3 +39,52 @@ describe('homeAssistantSignPath', () => {
|
||||
expect(await homeAssistantSignPath(createHASS(), 'unsigned/path', 42)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('homeAssistantSignEndpoint', () => {
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should return endpoint URL without signing when sign is false', async () => {
|
||||
const endpoint = { endpoint: 'http://example.com', sign: false };
|
||||
expect(await homeAssistantGetSignedURLIfNecessary(createHASS(), endpoint)).toBe(
|
||||
'http://example.com',
|
||||
);
|
||||
expect(homeAssistantWSRequest).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return endpoint URL without signing when sign is undefined', async () => {
|
||||
const endpoint = { endpoint: 'http://example.com' };
|
||||
expect(await homeAssistantGetSignedURLIfNecessary(createHASS(), endpoint)).toBe(
|
||||
'http://example.com',
|
||||
);
|
||||
expect(homeAssistantWSRequest).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should sign endpoint when sign is true', async () => {
|
||||
const hass = createHASS();
|
||||
vi.mocked(homeAssistantWSRequest).mockResolvedValue({
|
||||
path: 'signed/path',
|
||||
});
|
||||
vi.mocked(hass.hassUrl).mockImplementation((url) => 'hass:' + url);
|
||||
|
||||
const endpoint = { endpoint: 'http://example.com', sign: true };
|
||||
expect(await homeAssistantGetSignedURLIfNecessary(hass, endpoint, 60)).toBe(
|
||||
'hass:signed/path',
|
||||
);
|
||||
expect(homeAssistantWSRequest).toHaveBeenCalledWith(hass, signedPathSchema, {
|
||||
type: 'auth/sign_path',
|
||||
path: 'http://example.com',
|
||||
expires: 60,
|
||||
});
|
||||
});
|
||||
|
||||
it('should return null when signing fails', async () => {
|
||||
vi.mocked(homeAssistantWSRequest).mockResolvedValue(null);
|
||||
|
||||
const endpoint = { endpoint: 'http://example.com', sign: true };
|
||||
expect(
|
||||
await homeAssistantGetSignedURLIfNecessary(createHASS(), endpoint),
|
||||
).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
+56
-97
@@ -1,10 +1,9 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { CameraProxyConfig } from '../../src/camera-manager/types.js';
|
||||
import { EnabledProxyConfig } from '../../src/config/schema/common/proxy.js';
|
||||
import {
|
||||
addDynamicProxyURL,
|
||||
createProxiedEndpointIfNecessary,
|
||||
getWebProxiedURL,
|
||||
shouldUseWebProxy,
|
||||
} from '../../src/ha/web-proxy.js';
|
||||
import { createHASS } from '../test-utils.js';
|
||||
|
||||
@@ -28,50 +27,19 @@ describe('getWebProxiedURL', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('shouldUseWebProxy', () => {
|
||||
const createProxyConfig = (
|
||||
config: Partial<CameraProxyConfig> = {},
|
||||
): CameraProxyConfig => ({
|
||||
media: true,
|
||||
live: true,
|
||||
ssl_verification: true,
|
||||
ssl_ciphers: 'default',
|
||||
dynamic: true,
|
||||
...config,
|
||||
});
|
||||
|
||||
it('should return false without a the proxy installed', () => {
|
||||
const hass = createHASS();
|
||||
hass.config.components = [];
|
||||
|
||||
expect(shouldUseWebProxy(hass, createProxyConfig())).toBe(false);
|
||||
});
|
||||
|
||||
it('should return when proxy config does not want proxying', () => {
|
||||
const hass = createHASS();
|
||||
hass.config.components = ['hass_web_proxy'];
|
||||
|
||||
const proxyConfig = createProxyConfig({ media: false });
|
||||
expect(shouldUseWebProxy(hass, proxyConfig, 'media')).toBe(false);
|
||||
});
|
||||
|
||||
it('should return when proxy config does want proxying', () => {
|
||||
const hass = createHASS();
|
||||
hass.config.components = ['hass_web_proxy'];
|
||||
|
||||
const proxyConfig = createProxyConfig({ media: true });
|
||||
expect(shouldUseWebProxy(hass, proxyConfig, 'media')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('addDynamicProxyURL', () => {
|
||||
it('should add dynamic proxy URL', async () => {
|
||||
it('should add dynamic proxy URL with proxy config', async () => {
|
||||
const hass = createHASS();
|
||||
const proxyConfig: EnabledProxyConfig = {
|
||||
dynamic: true,
|
||||
ssl_verification: true,
|
||||
ssl_ciphers: 'modern',
|
||||
enabled: true,
|
||||
};
|
||||
|
||||
await addDynamicProxyURL(hass, 'http://example.com', {
|
||||
proxyConfig,
|
||||
urlID: 'id',
|
||||
sslVerification: true,
|
||||
sslCiphers: 'modern',
|
||||
openLimit: 5,
|
||||
ttl: 60,
|
||||
allowUnauthenticated: false,
|
||||
@@ -92,40 +60,29 @@ describe('addDynamicProxyURL', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('should add dynamic proxy URL using config defaults', async () => {
|
||||
it('should add dynamic proxy URL without options', async () => {
|
||||
const hass = createHASS();
|
||||
const proxyConfig: CameraProxyConfig = {
|
||||
media: true,
|
||||
live: true,
|
||||
ssl_verification: false,
|
||||
ssl_ciphers: 'insecure',
|
||||
dynamic: true,
|
||||
};
|
||||
|
||||
await addDynamicProxyURL(hass, 'http://example.com', {
|
||||
proxyConfig: proxyConfig,
|
||||
});
|
||||
await addDynamicProxyURL(hass, 'http://example.com');
|
||||
|
||||
expect(hass.callService).toHaveBeenCalledWith(
|
||||
'hass_web_proxy',
|
||||
'create_proxied_url',
|
||||
expect.objectContaining({
|
||||
ssl_verification: false,
|
||||
ssl_ciphers: 'insecure',
|
||||
}),
|
||||
{
|
||||
url_pattern: 'http://example.com',
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('createProxiedEndpointIfNecessary', () => {
|
||||
const createProxyConfig = (
|
||||
config: Partial<CameraProxyConfig> = {},
|
||||
): CameraProxyConfig => ({
|
||||
media: true,
|
||||
live: true,
|
||||
const createEnabledProxyConfig = (
|
||||
config: Partial<EnabledProxyConfig> = {},
|
||||
): EnabledProxyConfig => ({
|
||||
ssl_verification: true,
|
||||
ssl_ciphers: 'default',
|
||||
dynamic: true,
|
||||
enabled: true,
|
||||
...config,
|
||||
});
|
||||
|
||||
@@ -133,33 +90,41 @@ describe('createProxiedEndpointIfNecessary', () => {
|
||||
|
||||
it('should return original endpoint when proxyConfig is undefined', async () => {
|
||||
const hass = createHASS();
|
||||
hass.config.components = ['hass_web_proxy'];
|
||||
|
||||
const result = await createProxiedEndpointIfNecessary(hass, testEndpoint);
|
||||
expect(result).toBe(testEndpoint);
|
||||
});
|
||||
|
||||
it('should return original endpoint when proxy is not available', async () => {
|
||||
const hass = createHASS();
|
||||
hass.config.components = [];
|
||||
|
||||
const result = await createProxiedEndpointIfNecessary(
|
||||
hass,
|
||||
testEndpoint,
|
||||
createProxyConfig(),
|
||||
);
|
||||
expect(result).toBe(testEndpoint);
|
||||
});
|
||||
|
||||
it('should return original endpoint when context is not enabled', async () => {
|
||||
it('should return original endpoint when enabled is false', async () => {
|
||||
const hass = createHASS();
|
||||
hass.config.components = ['hass_web_proxy'];
|
||||
|
||||
const result = await createProxiedEndpointIfNecessary(
|
||||
hass,
|
||||
testEndpoint,
|
||||
createProxyConfig({ media: false }),
|
||||
{ context: 'media' },
|
||||
createEnabledProxyConfig({ enabled: false }),
|
||||
);
|
||||
expect(result).toBe(testEndpoint);
|
||||
});
|
||||
|
||||
it('should return null when web proxy is not available and enforced', async () => {
|
||||
const hass = createHASS();
|
||||
|
||||
const result = await createProxiedEndpointIfNecessary(
|
||||
hass,
|
||||
testEndpoint,
|
||||
createEnabledProxyConfig({ enforce: true }),
|
||||
);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should return original endpoint when web proxy is not available but not enforced', async () => {
|
||||
const hass = createHASS();
|
||||
|
||||
const result = await createProxiedEndpointIfNecessary(
|
||||
hass,
|
||||
testEndpoint,
|
||||
createEnabledProxyConfig(),
|
||||
);
|
||||
expect(result).toBe(testEndpoint);
|
||||
});
|
||||
@@ -171,8 +136,8 @@ describe('createProxiedEndpointIfNecessary', () => {
|
||||
const result = await createProxiedEndpointIfNecessary(
|
||||
hass,
|
||||
testEndpoint,
|
||||
createProxyConfig(),
|
||||
{ context: 'media', ttl: 300, openLimit: 5 },
|
||||
createEnabledProxyConfig(),
|
||||
{ ttl: 300, openLimit: 5 },
|
||||
);
|
||||
|
||||
expect(hass.callService).toHaveBeenCalledWith(
|
||||
@@ -200,7 +165,11 @@ describe('createProxiedEndpointIfNecessary', () => {
|
||||
sign: false,
|
||||
};
|
||||
|
||||
await createProxiedEndpointIfNecessary(hass, endpointWithHash, createProxyConfig());
|
||||
await createProxiedEndpointIfNecessary(
|
||||
hass,
|
||||
endpointWithHash,
|
||||
createEnabledProxyConfig(),
|
||||
);
|
||||
|
||||
expect(hass.callService).toHaveBeenCalledWith(
|
||||
'hass_web_proxy',
|
||||
@@ -218,7 +187,7 @@ describe('createProxiedEndpointIfNecessary', () => {
|
||||
const result = await createProxiedEndpointIfNecessary(
|
||||
hass,
|
||||
testEndpoint,
|
||||
createProxyConfig({ dynamic: false }),
|
||||
createEnabledProxyConfig({ dynamic: false }),
|
||||
);
|
||||
|
||||
expect(hass.callService).not.toHaveBeenCalled();
|
||||
@@ -235,7 +204,7 @@ describe('createProxiedEndpointIfNecessary', () => {
|
||||
const result = await createProxiedEndpointIfNecessary(
|
||||
hass,
|
||||
testEndpoint,
|
||||
createProxyConfig({ dynamic: false }),
|
||||
createEnabledProxyConfig({ dynamic: false }),
|
||||
{ websocket: true },
|
||||
);
|
||||
|
||||
@@ -245,25 +214,15 @@ describe('createProxiedEndpointIfNecessary', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('should use live context when specified', async () => {
|
||||
const hass = createHASS();
|
||||
hass.config.components = ['hass_web_proxy'];
|
||||
|
||||
const result = await createProxiedEndpointIfNecessary(
|
||||
hass,
|
||||
testEndpoint,
|
||||
createProxyConfig({ media: false, live: true }),
|
||||
{ context: 'live' },
|
||||
);
|
||||
|
||||
expect(result.endpoint).toContain('/api/hass_web_proxy/');
|
||||
});
|
||||
|
||||
it('should default openLimit to 0 when not specified', async () => {
|
||||
const hass = createHASS();
|
||||
hass.config.components = ['hass_web_proxy'];
|
||||
|
||||
await createProxiedEndpointIfNecessary(hass, testEndpoint, createProxyConfig());
|
||||
await createProxiedEndpointIfNecessary(
|
||||
hass,
|
||||
testEndpoint,
|
||||
createEnabledProxyConfig(),
|
||||
);
|
||||
|
||||
expect(hass.callService).toHaveBeenCalledWith(
|
||||
'hass_web_proxy',
|
||||
|
||||
@@ -130,6 +130,7 @@ export const createHASS = (states?: HassEntities, user?: CurrentUser): HomeAssis
|
||||
if (user) {
|
||||
hass.user = user;
|
||||
}
|
||||
hass.config.components = [];
|
||||
hass.connection.subscribeMessage = vi.fn();
|
||||
|
||||
// ha-nunjucks calls sendMessagePromise to fetch label registry; return empty array to prevent crash.
|
||||
|
||||
@@ -7,7 +7,9 @@ describe('getParseErrorPaths', () => {
|
||||
const result = z
|
||||
.object({ a: z.string(), b: z.number() })
|
||||
.safeParse({ a: 1, b: 'a' });
|
||||
if (result.success) return;
|
||||
if (result.success) {
|
||||
return;
|
||||
}
|
||||
expect(getParseErrorPaths(result.error)).toEqual(new Set(['a', 'b']));
|
||||
});
|
||||
|
||||
@@ -15,13 +17,17 @@ describe('getParseErrorPaths', () => {
|
||||
const result = z
|
||||
.object({ a: z.object({ b: z.string() }) })
|
||||
.safeParse({ a: { b: 1 } });
|
||||
if (result.success) return;
|
||||
if (result.success) {
|
||||
return;
|
||||
}
|
||||
expect(getParseErrorPaths(result.error)).toEqual(new Set(['a.b']));
|
||||
});
|
||||
|
||||
it('should get array error paths', () => {
|
||||
const result = z.array(z.string()).safeParse([1, 'a', 2]);
|
||||
if (result.success) return;
|
||||
if (result.success) {
|
||||
return;
|
||||
}
|
||||
expect(getParseErrorPaths(result.error)).toEqual(new Set(['[0]', '[2]']));
|
||||
});
|
||||
|
||||
@@ -29,7 +35,9 @@ describe('getParseErrorPaths', () => {
|
||||
const result = z
|
||||
.object({ a: z.array(z.object({ b: z.string() })) })
|
||||
.safeParse({ a: [{ b: 1 }, { b: 'a' }, { b: 2 }] });
|
||||
if (result.success) return;
|
||||
if (result.success) {
|
||||
return;
|
||||
}
|
||||
expect(getParseErrorPaths(result.error)).toEqual(new Set(['a[0].b', 'a[2].b']));
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user