feat: Add hardened error handling and retries (#2451)

- Closes #1830
 - Closes #2099
This commit is contained in:
Dermot Duffy
2026-06-30 17:45:12 -07:00
committed by dermotduffy
parent 4bc787e2b7
commit 47bcce93d3
182 changed files with 7877 additions and 4043 deletions
@@ -0,0 +1,71 @@
import { describe, expect, it } from 'vitest';
import { ConfigErrorIssue } from '../../../../src/card-controller/issues/issues/config-error';
describe('ConfigErrorIssue', () => {
it('should have correct key', () => {
const issue = new ConfigErrorIssue();
expect(issue.key).toBe('config_error');
});
it('should report no issue when untriggered', () => {
const issue = new ConfigErrorIssue();
expect(issue.hasIssue()).toBe(false);
expect(issue.getIssue()).toBeNull();
});
it('should report an issue after trigger with an error', () => {
const issue = new ConfigErrorIssue();
issue.trigger({ error: new Error('bad config') });
expect(issue.hasIssue()).toBe(true);
});
it('should report an issue after trigger with a string error', () => {
const issue = new ConfigErrorIssue();
issue.trigger({ error: 'string error' });
expect(issue.hasIssue()).toBe(true);
});
it('should treat a triggered null/undefined error as no issue', () => {
const issue = new ConfigErrorIssue();
issue.trigger({ error: undefined });
expect(issue.hasIssue()).toBe(false);
expect(issue.getIssue()).toBeNull();
});
it('isFullCardIssue should return true', () => {
const issue = new ConfigErrorIssue();
expect(issue.isFullCardIssue()).toBe(true);
});
it('getIssue should return a IssueDescription with expected shape', () => {
const issue = new ConfigErrorIssue();
issue.trigger({ error: new Error('config is invalid') });
const result = issue.getIssue();
expect(result).toEqual(
expect.objectContaining({
icon: 'mdi:alert',
severity: 'high',
notification: expect.objectContaining({
body: expect.objectContaining({
text: 'config is invalid',
}),
}),
}),
);
});
it('should clear the issue after reset', () => {
const issue = new ConfigErrorIssue();
issue.trigger({ error: new Error('oops') });
expect(issue.hasIssue()).toBe(true);
issue.reset();
expect(issue.hasIssue()).toBe(false);
expect(issue.getIssue()).toBeNull();
});
});
@@ -0,0 +1,71 @@
import { describe, expect, it, vi } from 'vitest';
import { ConfigUpgradeIssue } from '../../../../src/card-controller/issues/issues/config-upgrade';
import { isConfigUpgradeable } from '../../../../src/config/management';
import { RawAdvancedCameraCardConfig } from '../../../../src/config/types';
import { createCardAPI } from '../../../test-utils';
vi.mock('../../../../src/config/management.js');
const createAPI = (rawConfig?: RawAdvancedCameraCardConfig) => {
const api = createCardAPI();
vi.mocked(api.getConfigManager().getRawConfig).mockReturnValue(rawConfig ?? null);
return api;
};
describe('ConfigUpgradeIssue', () => {
it('should have correct key', () => {
const issue = new ConfigUpgradeIssue(createAPI());
expect(issue.key).toBe('config_upgrade');
});
it('should detect upgradeable config', async () => {
vi.mocked(isConfigUpgradeable).mockReturnValue(true);
const rawConfig = { type: 'custom:frigate-card' };
const issue = new ConfigUpgradeIssue(createAPI(rawConfig));
await issue.detectStatic();
expect(issue.hasIssue()).toBe(true);
expect(isConfigUpgradeable).toBeCalledWith(rawConfig);
});
it('should detect non-upgradeable config', async () => {
vi.mocked(isConfigUpgradeable).mockReturnValue(false);
const rawConfig = { type: 'custom:advanced-camera-card' };
const issue = new ConfigUpgradeIssue(createAPI(rawConfig));
await issue.detectStatic();
expect(issue.hasIssue()).toBe(false);
});
it('should handle null raw config', async () => {
const issue = new ConfigUpgradeIssue(createAPI());
await issue.detectStatic();
expect(issue.hasIssue()).toBe(false);
expect(issue.getIssue()).toBeNull();
});
it('should return result when upgradeable', async () => {
vi.mocked(isConfigUpgradeable).mockReturnValue(true);
const issue = new ConfigUpgradeIssue(createAPI({ type: 'custom:frigate-card' }));
await issue.detectStatic();
const result = issue.getIssue();
expect(result).toEqual(
expect.objectContaining({
icon: 'mdi:update',
severity: 'medium',
notification: expect.objectContaining({
heading: expect.objectContaining({
icon: 'mdi:update',
severity: 'medium',
}),
}),
}),
);
});
});
@@ -0,0 +1,127 @@
import { STATE_RUNNING, STATE_STARTING } from 'home-assistant-js-websocket';
import { describe, expect, it } from 'vitest';
import { ConnectionIssue } from '../../../../src/card-controller/issues/issues/connection';
import { createHASS } from '../../../test-utils';
describe('ConnectionIssue', () => {
it('should have correct key', () => {
const issue = new ConnectionIssue();
expect(issue.key).toBe('connection');
});
it('should report no issue when hass has never been set', () => {
const issue = new ConnectionIssue();
issue.detectDynamic({});
expect(issue.hasIssue()).toBe(false);
expect(issue.getIssue()).toBeNull();
});
it('should report a lost issue when hass is disconnected', () => {
const issue = new ConnectionIssue();
const hass = createHASS();
hass.connected = false;
issue.detectDynamic({ hass });
expect(issue.hasIssue()).toBe(true);
expect(issue.getIssue()).toEqual(
expect.objectContaining({
icon: 'mdi:lan-disconnect',
severity: 'high',
notification: expect.objectContaining({
in_progress: true,
heading: expect.objectContaining({
text: 'Connection lost',
icon: 'mdi:lan-disconnect',
severity: 'high',
}),
body: expect.objectContaining({
text: 'Connection to Home Assistant lost',
}),
}),
}),
);
});
it('should report a starting issue when hass is connected but not running', () => {
const issue = new ConnectionIssue();
const hass = createHASS();
hass.connected = true;
hass.config.state = STATE_STARTING;
issue.detectDynamic({ hass });
expect(issue.hasIssue()).toBe(true);
expect(issue.getIssue()).toEqual(
expect.objectContaining({
icon: 'mdi:home-assistant',
severity: 'medium',
notification: expect.objectContaining({
in_progress: true,
heading: expect.objectContaining({
text: 'Home Assistant is starting',
icon: 'mdi:home-assistant',
severity: 'medium',
}),
body: expect.objectContaining({
text: 'Waiting for Home Assistant startup to complete',
}),
}),
}),
);
});
it('should not report an issue when hass is connected and running', () => {
const issue = new ConnectionIssue();
const hass = createHASS();
hass.connected = true;
hass.config.state = STATE_RUNNING;
issue.detectDynamic({ hass });
expect(issue.hasIssue()).toBe(false);
expect(issue.getIssue()).toBeNull();
});
it('should clear when hass transitions lost → starting → ready', () => {
const issue = new ConnectionIssue();
const hass = createHASS();
hass.connected = false;
issue.detectDynamic({ hass });
expect(issue.hasIssue()).toBe(true);
expect(issue.getIssue()?.notification.heading?.text).toBe('Connection lost');
hass.connected = true;
hass.config.state = STATE_STARTING;
issue.detectDynamic({ hass });
expect(issue.hasIssue()).toBe(true);
expect(issue.getIssue()?.notification.heading?.text).toBe(
'Home Assistant is starting',
);
hass.config.state = STATE_RUNNING;
issue.detectDynamic({ hass });
expect(issue.hasIssue()).toBe(false);
});
it('should return true for isFullCardIssue', () => {
const issue = new ConnectionIssue();
expect(issue.isFullCardIssue()).toBe(true);
});
it('should clear the issue after reset', () => {
const issue = new ConnectionIssue();
const hass = createHASS();
hass.connected = false;
issue.detectDynamic({ hass });
expect(issue.hasIssue()).toBe(true);
issue.reset();
expect(issue.hasIssue()).toBe(false);
expect(issue.getIssue()).toBeNull();
});
});
@@ -0,0 +1,153 @@
import { assert, describe, expect, it, vi } from 'vitest';
import { CardController } from '../../../../src/card-controller/controller';
import { InitializationIssue } from '../../../../src/card-controller/issues/issues/initialization';
import { InternalCallbackActionConfig } from '../../../../src/config/schema/actions/custom/internal';
import { createCardAPI } from '../../../test-utils';
describe('InitializationIssue', () => {
const createAPI = (isInitializedMandatory = false): CardController => {
const api = createCardAPI();
vi.mocked(api.getInitializationManager().isInitializedMandatory).mockReturnValue(
isInitializedMandatory,
);
return api;
};
it('should have correct key', () => {
const issue = new InitializationIssue(createAPI());
expect(issue.key).toBe('initialization');
});
it('should report no issue when untriggered', () => {
const issue = new InitializationIssue(createAPI());
expect(issue.hasIssue()).toBe(false);
expect(issue.getIssue()).toBeNull();
});
it('should report an issue after trigger', () => {
const issue = new InitializationIssue(createAPI());
issue.trigger({ error: new Error('init failed') });
expect(issue.hasIssue()).toBe(true);
});
it('should treat a triggered null/undefined error as no issue', () => {
const issue = new InitializationIssue(createAPI());
issue.trigger({ error: undefined });
expect(issue.hasIssue()).toBe(false);
expect(issue.getIssue()).toBeNull();
expect(issue.needsRetry()).toBe(false);
});
it('should return true for isFullCardIssue', () => {
const issue = new InitializationIssue(createAPI());
expect(issue.isFullCardIssue()).toBe(true);
});
it('should return notification from error via getIssue', () => {
const issue = new InitializationIssue(createAPI());
issue.trigger({ error: new Error('init failed') });
const result = issue.getIssue();
expect(result).toEqual(
expect.objectContaining({
icon: 'mdi:alert',
severity: 'high',
notification: expect.objectContaining({
heading: expect.objectContaining({ text: 'Initialization failed' }),
body: expect.objectContaining({ text: 'init failed' }),
controls: expect.arrayContaining([
expect.objectContaining({ icon: 'mdi:refresh', dismiss: true }),
]),
}),
}),
);
expect(result?.notification.in_progress).toBeUndefined();
});
it('should call manager.retry with the issue key from getIssue retry control callback', async () => {
const api = createCardAPI();
const issue = new InitializationIssue(api);
issue.trigger({ error: new Error('init failed') });
const control = issue.getIssue()?.notification.controls?.[0];
assert(control);
const tapAction = control.actions?.tap_action as InternalCallbackActionConfig;
await tapAction.callback(api);
expect(api.getIssueManager().retry).toBeCalledWith('initialization', true);
});
describe('detectDynamic', () => {
it('should clear the issue when initialization is now mandatory', () => {
const issue = new InitializationIssue(createAPI(true));
issue.trigger({ error: new Error('init failed') });
expect(issue.hasIssue()).toBe(true);
issue.detectDynamic();
expect(issue.hasIssue()).toBe(false);
expect(issue.getIssue()).toBeNull();
});
it('should keep the issue when initialization is still not mandatory', () => {
const issue = new InitializationIssue(createAPI(false));
issue.trigger({ error: new Error('init failed') });
issue.detectDynamic();
expect(issue.hasIssue()).toBe(true);
});
it('should do nothing when not failed', () => {
const api = createAPI(false);
const issue = new InitializationIssue(api);
issue.detectDynamic();
expect(issue.hasIssue()).toBe(false);
expect(api.getInitializationManager().isInitializedMandatory).not.toBeCalled();
});
});
describe('needsRetry', () => {
it('should return true when failed', () => {
const issue = new InitializationIssue(createAPI());
issue.trigger({ error: new Error('init failed') });
expect(issue.needsRetry()).toBe(true);
});
it('should return false when not failed', () => {
const issue = new InitializationIssue(createAPI());
expect(issue.needsRetry()).toBe(false);
});
});
describe('retry', () => {
it('should uninitialize mandatory initialization and destroy camera manager', () => {
const api = createAPI();
const issue = new InitializationIssue(api);
issue.trigger({ error: new Error('init failed') });
const result = issue.retry();
expect(result).toBe(false);
expect(issue.hasIssue()).toBe(false);
expect(issue.needsRetry()).toBe(false);
expect(api.getInitializationManager().uninitializeMandatory).toBeCalled();
expect(api.getCameraManager().destroy).toBeCalled();
});
});
it('should clear the issue after reset', () => {
const issue = new InitializationIssue(createAPI());
issue.trigger({ error: new Error('oops') });
expect(issue.hasIssue()).toBe(true);
issue.reset();
expect(issue.hasIssue()).toBe(false);
expect(issue.getIssue()).toBeNull();
});
});
@@ -0,0 +1,474 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { LegacyResourceIssue } from '../../../../src/card-controller/issues/issues/legacy-resource';
import { HomeAssistant } from '../../../../src/ha/types';
import { createCardAPI, createHASS, createUser } from '../../../test-utils';
const setupHASSResources = (
hass: HomeAssistant,
resources: { id: string; type: string; url: string }[],
): void => {
vi.mocked(hass.hassUrl).mockReturnValue('http://homeassistant.local:8123');
vi.mocked(hass.callWS).mockResolvedValue(resources);
};
describe('LegacyResourceIssue', () => {
beforeEach(() => {
vi.resetAllMocks();
});
it('should have correct key', () => {
const issue = new LegacyResourceIssue();
expect(issue.key).toBe('legacy_resource');
});
describe('detectStatic', () => {
it('should skip non-admin users', async () => {
const issue = new LegacyResourceIssue();
const hass = createHASS(undefined, createUser({ is_admin: false }));
await issue.detectStatic(hass);
expect(issue.hasIssue()).toBe(false);
});
it('should detect legacy resource regardless of directory', async () => {
const issue = new LegacyResourceIssue();
const hass = createHASS(undefined, createUser({ is_admin: true }));
setupHASSResources(hass, [
{
id: '1',
type: 'module',
url: '/some/arbitrary/path/frigate-hass-card.js?v=1',
},
]);
await issue.detectStatic(hass);
expect(issue.hasIssue()).toBe(true);
});
it('should not detect when only advanced-camera-card exists', async () => {
const issue = new LegacyResourceIssue();
const hass = createHASS(undefined, createUser({ is_admin: true }));
setupHASSResources(hass, [
{
id: '1',
type: 'module',
url: '/hacsfiles/advanced-camera-card/advanced-camera-card.js',
},
]);
await issue.detectStatic(hass);
expect(issue.hasIssue()).toBe(false);
});
it('should handle invalid resource data', async () => {
const issue = new LegacyResourceIssue();
const hass = createHASS(undefined, createUser({ is_admin: true }));
vi.mocked(hass.callWS).mockResolvedValue('not-an-array');
await issue.detectStatic(hass);
expect(issue.hasIssue()).toBe(false);
});
it('should handle websocket failure', async () => {
const issue = new LegacyResourceIssue();
const hass = createHASS(undefined, createUser({ is_admin: true }));
vi.mocked(hass.callWS).mockRejectedValue(new Error('connection lost'));
await issue.detectStatic(hass);
expect(issue.hasIssue()).toBe(false);
});
it('should handle missing user', async () => {
const issue = new LegacyResourceIssue();
const hass = createHASS();
Object.defineProperty(hass, 'user', { value: undefined });
await issue.detectStatic(hass);
expect(issue.hasIssue()).toBe(false);
});
});
describe('getIssue', () => {
it('should return controls and link when both resources exist', async () => {
const issue = new LegacyResourceIssue();
const hass = createHASS(undefined, createUser({ is_admin: true }));
setupHASSResources(hass, [
{
id: '1',
type: 'module',
url: '/hacsfiles/frigate-hass-card/frigate-hass-card.js',
},
{
id: '2',
type: 'module',
url: '/hacsfiles/advanced-camera-card/advanced-camera-card.js',
},
]);
await issue.detectStatic(hass);
const result = issue.getIssue();
expect(result).not.toBeNull();
expect(result?.notification.controls).toHaveLength(1);
expect(result?.notification.link).toBeDefined();
});
it('should return link without controls when only legacy exists', async () => {
const issue = new LegacyResourceIssue();
const hass = createHASS(undefined, createUser({ is_admin: true }));
setupHASSResources(hass, [
{
id: '1',
type: 'module',
url: '/hacsfiles/frigate-hass-card/frigate-hass-card.js',
},
]);
await issue.detectStatic(hass);
const result = issue.getIssue();
expect(result).not.toBeNull();
expect(result?.notification.link).toBeDefined();
expect(result?.notification.controls).toBeUndefined();
});
it('should return null when no result', () => {
const issue = new LegacyResourceIssue();
expect(issue.getIssue()).toBeNull();
});
});
describe('fix', () => {
it('should remove legacy resources when correct resource exists', async () => {
const onChange = vi.fn();
const issue = new LegacyResourceIssue(onChange);
const hass = createHASS(undefined, createUser({ is_admin: true }));
vi.mocked(hass.hassUrl).mockReturnValue('http://homeassistant.local:8123');
vi.mocked(hass.callWS).mockResolvedValueOnce([
{
id: '1',
type: 'module',
url: '/hacsfiles/frigate-hass-card/frigate-hass-card.js',
},
{
id: '2',
type: 'module',
url: '/hacsfiles/advanced-camera-card/advanced-camera-card.js',
},
]);
await issue.detectStatic(hass);
vi.mocked(hass.callWS)
.mockResolvedValueOnce(undefined)
.mockResolvedValueOnce([
{
id: '2',
type: 'module',
url: '/hacsfiles/advanced-camera-card/advanced-camera-card.js',
},
]);
const result = await issue.fix(hass);
expect(result).toBe(true);
expect(hass.callWS).toBeCalledWith(
expect.objectContaining({
type: 'lovelace/resources/delete',
resource_id: '1',
}),
);
expect(issue.hasIssue()).toBe(false);
expect(onChange).toBeCalled();
});
it('should not fix when only legacy resource exists', async () => {
const issue = new LegacyResourceIssue();
const hass = createHASS(undefined, createUser({ is_admin: true }));
setupHASSResources(hass, [
{
id: '1',
type: 'module',
url: '/hacsfiles/frigate-hass-card/frigate-hass-card.js',
},
]);
await issue.detectStatic(hass);
const result = await issue.fix(hass);
expect(result).toBe(false);
});
it('should not fix for non-admin', async () => {
const issue = new LegacyResourceIssue();
const hass = createHASS(undefined, createUser({ is_admin: false }));
const result = await issue.fix(hass);
expect(result).toBe(false);
});
it('should return false on websocket failure during fix', async () => {
const onChange = vi.fn();
const issue = new LegacyResourceIssue(onChange);
const hass = createHASS(undefined, createUser({ is_admin: true }));
vi.mocked(hass.hassUrl).mockReturnValue('http://homeassistant.local:8123');
vi.mocked(hass.callWS).mockResolvedValueOnce([
{
id: '1',
type: 'module',
url: '/hacsfiles/frigate-hass-card/frigate-hass-card.js',
},
{
id: '2',
type: 'module',
url: '/hacsfiles/advanced-camera-card/advanced-camera-card.js',
},
]);
await issue.detectStatic(hass);
vi.mocked(hass.callWS).mockRejectedValue(new Error('connection lost'));
const result = await issue.fix(hass);
expect(result).toBe(false);
expect(onChange).not.toBeCalled();
});
it('should return false when the verification fetch silently fails after a successful delete', async () => {
const onChange = vi.fn();
const issue = new LegacyResourceIssue(onChange);
const hass = createHASS(undefined, createUser({ is_admin: true }));
vi.mocked(hass.hassUrl).mockReturnValue('http://homeassistant.local:8123');
// Initial detection: finds one legacy resource + one correct resource.
vi.mocked(hass.callWS).mockResolvedValueOnce([
{
id: '1',
type: 'module',
url: '/hacsfiles/frigate-hass-card/frigate-hass-card.js',
},
{
id: '2',
type: 'module',
url: '/hacsfiles/advanced-camera-card/advanced-camera-card.js',
},
]);
await issue.detectStatic(hass);
// Delete succeeds; verification fetch fails. detectStatic silently
// swallows the WS error, so the fix path must not interpret the
// absence of a positive signal as success.
vi.mocked(hass.callWS)
.mockResolvedValueOnce(undefined)
.mockRejectedValueOnce(new Error('connection lost'));
const result = await issue.fix(hass);
expect(result).toBe(false);
expect(onChange).not.toBeCalled();
});
it('should return false when re-detection still finds legacy resource', async () => {
const onChange = vi.fn();
const issue = new LegacyResourceIssue(onChange);
const hass = createHASS(undefined, createUser({ is_admin: true }));
vi.mocked(hass.hassUrl).mockReturnValue('http://homeassistant.local:8123');
vi.mocked(hass.callWS).mockResolvedValueOnce([
{
id: '1',
type: 'module',
url: '/hacsfiles/frigate-hass-card/frigate-hass-card.js',
},
{
id: '2',
type: 'module',
url: '/hacsfiles/advanced-camera-card/advanced-camera-card.js',
},
]);
await issue.detectStatic(hass);
// Delete succeeds, but re-detection still finds the legacy resource.
vi.mocked(hass.callWS)
.mockResolvedValueOnce(undefined)
.mockResolvedValueOnce([
{
id: '1',
type: 'module',
url: '/hacsfiles/frigate-hass-card/frigate-hass-card.js',
},
{
id: '2',
type: 'module',
url: '/hacsfiles/advanced-camera-card/advanced-camera-card.js',
},
]);
const result = await issue.fix(hass);
expect(result).toBe(false);
expect(onChange).not.toBeCalled();
});
it('should fix multiple legacy resources', async () => {
const issue = new LegacyResourceIssue();
const hass = createHASS(undefined, createUser({ is_admin: true }));
vi.mocked(hass.hassUrl).mockReturnValue('http://homeassistant.local:8123');
vi.mocked(hass.callWS).mockResolvedValueOnce([
{
id: '1',
type: 'module',
url: '/hacsfiles/frigate-hass-card/frigate-hass-card.js',
},
{ id: '3', type: 'module', url: '/local/frigate-hass-card.js' },
{
id: '2',
type: 'module',
url: '/hacsfiles/advanced-camera-card/advanced-camera-card.js',
},
]);
await issue.detectStatic(hass);
vi.mocked(hass.callWS)
.mockResolvedValueOnce(undefined)
.mockResolvedValueOnce(undefined)
.mockResolvedValueOnce([
{
id: '2',
type: 'module',
url: '/hacsfiles/advanced-camera-card/advanced-camera-card.js',
},
]);
expect(await issue.fix(hass)).toBe(true);
});
});
describe('getResourcePath fallback', () => {
it('should handle invalid URLs by stripping query string', async () => {
const issue = new LegacyResourceIssue();
const hass = createHASS(undefined, createUser({ is_admin: true }));
vi.mocked(hass.hassUrl).mockReturnValue('not-a-valid-url');
vi.mocked(hass.callWS).mockResolvedValue([
{
id: '1',
type: 'module',
url: '/hacsfiles/frigate-hass-card/frigate-hass-card.js?v=1',
},
]);
await issue.detectStatic(hass);
expect(issue.hasIssue()).toBe(true);
});
it('should handle invalid URLs without query string', async () => {
const issue = new LegacyResourceIssue();
const hass = createHASS(undefined, createUser({ is_admin: true }));
vi.mocked(hass.hassUrl).mockReturnValue('not-a-valid-url');
vi.mocked(hass.callWS).mockResolvedValue([
{
id: '1',
type: 'module',
url: '/hacsfiles/frigate-hass-card/frigate-hass-card.js',
},
]);
await issue.detectStatic(hass);
expect(issue.hasIssue()).toBe(true);
});
});
describe('callback action', () => {
const getCallback = (
issue: LegacyResourceIssue,
): ((api: unknown) => Promise<void>) | null => {
const result = issue.getIssue();
const action = result?.notification.controls?.[0]?.actions?.tap_action;
if (action && 'callback' in action) {
return (action as { callback: (api: unknown) => Promise<void> }).callback;
}
return null;
};
it('should call fix via the notification control action', async () => {
const issue = new LegacyResourceIssue();
const hass = createHASS(undefined, createUser({ is_admin: true }));
vi.mocked(hass.hassUrl).mockReturnValue('http://homeassistant.local:8123');
vi.mocked(hass.callWS).mockResolvedValueOnce([
{
id: '1',
type: 'module',
url: '/hacsfiles/frigate-hass-card/frigate-hass-card.js',
},
{
id: '2',
type: 'module',
url: '/hacsfiles/advanced-camera-card/advanced-camera-card.js',
},
]);
await issue.detectStatic(hass);
const callback = getCallback(issue);
expect(callback).toBeDefined();
const api = createCardAPI();
vi.mocked(api.getHASSManager().getHASS).mockReturnValue(hass);
vi.mocked(hass.callWS)
.mockResolvedValueOnce(undefined)
.mockResolvedValueOnce([
{
id: '2',
type: 'module',
url: '/hacsfiles/advanced-camera-card/advanced-camera-card.js',
},
]);
await callback?.(api);
expect(hass.callWS).toBeCalledWith(
expect.objectContaining({
type: 'lovelace/resources/delete',
}),
);
});
it('should handle missing hass in callback', async () => {
const issue = new LegacyResourceIssue();
const hass = createHASS(undefined, createUser({ is_admin: true }));
vi.mocked(hass.hassUrl).mockReturnValue('http://homeassistant.local:8123');
vi.mocked(hass.callWS).mockResolvedValueOnce([
{
id: '1',
type: 'module',
url: '/hacsfiles/frigate-hass-card/frigate-hass-card.js',
},
{
id: '2',
type: 'module',
url: '/hacsfiles/advanced-camera-card/advanced-camera-card.js',
},
]);
await issue.detectStatic(hass);
const callback = getCallback(issue);
expect(callback).toBeDefined();
const api = createCardAPI();
vi.mocked(api.getHASSManager().getHASS).mockReturnValue(null);
await callback?.(api);
});
});
});
@@ -0,0 +1,623 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { mock } from 'vitest-mock-extended';
import { MediaLoadIssue } from '../../../../src/card-controller/issues/issues/media-load';
import { InternalCallbackActionConfig } from '../../../../src/config/schema/actions/custom/internal';
import { View } from '../../../../src/view/view';
import { createCardAPI, createMediaLoadedInfo } from '../../../test-utils';
import { IMAGE_VIEW_TARGET_ID_SENTINEL } from '../../../../src/view/target-id';
const createAPI = () => createCardAPI();
// @vitest-environment jsdom
describe('MediaLoadIssue', () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
it('should have correct key', () => {
const issue = new MediaLoadIssue(createAPI());
expect(issue.key).toBe('media_load');
});
describe('detectDynamic', () => {
it.each([
['live' as const],
['clip' as const],
['folder' as const],
['media' as const],
['snapshot' as const],
['recording' as const],
['review' as const],
])('should start timer when view is %s and not loaded', (view) => {
const onChange = vi.fn();
const issue = new MediaLoadIssue(createAPI(), onChange);
issue.detectDynamic({ targetID: 'target-1', view });
expect(issue.hasIssue()).toBe(false);
vi.advanceTimersByTime(10000);
expect(issue.hasIssue()).toBe(true);
expect(onChange).toBeCalled();
});
it('should not start timer when targetID is null (no provider rendering)', () => {
const issue = new MediaLoadIssue(createAPI());
// Media view but no targetID, e.g. viewer showing "No media to display"
// instead of mounting a provider.
issue.detectDynamic({ view: 'media' });
vi.advanceTimersByTime(10000);
expect(issue.hasIssue()).toBe(false);
});
it('should deactivate when targetID becomes null', () => {
const issue = new MediaLoadIssue(createAPI());
issue.detectDynamic({ targetID: 'camera-1', view: 'live' });
vi.advanceTimersByTime(10000);
expect(issue.hasIssue()).toBe(true);
// Target cleared (e.g. switched to a view with no media provider).
issue.detectDynamic({ view: 'live' });
expect(issue.hasIssue()).toBe(false);
});
it('should not start timer when view is not a media view', () => {
const issue = new MediaLoadIssue(createAPI());
issue.detectDynamic({ view: 'timeline' });
vi.advanceTimersByTime(10000);
expect(issue.hasIssue()).toBe(false);
});
it('should not start timer when view is undefined', () => {
const issue = new MediaLoadIssue(createAPI());
issue.detectDynamic({});
vi.advanceTimersByTime(10000);
expect(issue.hasIssue()).toBe(false);
});
it('should not start timer when media is loaded', () => {
const issue = new MediaLoadIssue(createAPI());
issue.detectDynamic({ view: 'live', mediaLoadedInfo: createMediaLoadedInfo() });
vi.advanceTimersByTime(10000);
expect(issue.hasIssue()).toBe(false);
});
it('should clear timeout when media loads', () => {
const issue = new MediaLoadIssue(createAPI());
issue.detectDynamic({ targetID: 'camera-1', view: 'live' });
vi.advanceTimersByTime(5000);
issue.detectDynamic({ view: 'live', mediaLoadedInfo: createMediaLoadedInfo() });
vi.advanceTimersByTime(5000);
expect(issue.hasIssue()).toBe(false);
});
it('should clear timeout when view changes to a non-media view', () => {
const issue = new MediaLoadIssue(createAPI());
issue.detectDynamic({ targetID: 'camera-1', view: 'live' });
vi.advanceTimersByTime(5000);
issue.detectDynamic({ view: 'timeline' });
vi.advanceTimersByTime(5000);
expect(issue.hasIssue()).toBe(false);
});
it('should remain active across media views for the same target', () => {
const issue = new MediaLoadIssue(createAPI());
issue.detectDynamic({ targetID: 'camera-1', view: 'live' });
vi.advanceTimersByTime(10000);
expect(issue.hasIssue()).toBe(true);
// Same target, different media view — issue stays active.
issue.detectDynamic({ targetID: 'camera-1', view: 'clip' });
expect(issue.hasIssue()).toBe(true);
});
it('should deactivate when target changes to non-errored target', () => {
const issue = new MediaLoadIssue(createAPI());
issue.detectDynamic({ targetID: 'camera-1', view: 'live' });
vi.advanceTimersByTime(10000);
expect(issue.hasIssue()).toBe(true);
// Switch to camera-2 which has no error — should deactivate and start
// a fresh timer for the new target.
issue.detectDynamic({ targetID: 'camera-2', view: 'live' });
expect(issue.hasIssue()).toBe(false);
// camera-2 gets its own timeout window.
vi.advanceTimersByTime(10000);
expect(issue.hasIssue()).toBe(true);
});
it('should stay active when target changes to errored target', () => {
const issue = new MediaLoadIssue(createAPI());
issue.trigger({ targetID: 'camera-1' });
issue.trigger({ targetID: 'camera-2' });
issue.detectDynamic({ targetID: 'camera-1', view: 'live' });
expect(issue.hasIssue()).toBe(true);
// Switch to camera-2 which also has an error — should stay active.
issue.detectDynamic({ targetID: 'camera-2', view: 'live' });
expect(issue.hasIssue()).toBe(true);
});
it('should clear timed-out state when media loads', () => {
const issue = new MediaLoadIssue(createAPI());
issue.detectDynamic({ targetID: 'camera-1', view: 'live' });
vi.advanceTimersByTime(10000);
expect(issue.hasIssue()).toBe(true);
issue.detectDynamic({ view: 'live', mediaLoadedInfo: createMediaLoadedInfo() });
expect(issue.hasIssue()).toBe(false);
});
it('should restart timer when target changes', () => {
const onChange = vi.fn();
const issue = new MediaLoadIssue(createAPI(), onChange);
issue.detectDynamic({
targetID: 'camera-1',
view: 'live',
});
vi.advanceTimersByTime(5000);
// Switch to camera-2: timer restarts from 0 for the new target.
issue.detectDynamic({
targetID: 'camera-2',
view: 'live',
});
// 5 more seconds is not enough for the new 10s timer.
vi.advanceTimersByTime(5000);
expect(issue.hasIssue()).toBe(false);
// Full 10s from camera-2's timer start.
vi.advanceTimersByTime(5000);
expect(issue.hasIssue()).toBe(true);
expect(onChange).toBeCalledTimes(1);
});
it('should not restart timer for same target while running', () => {
const onChange = vi.fn();
const issue = new MediaLoadIssue(createAPI(), onChange);
issue.detectDynamic({
targetID: 'camera-1',
view: 'live',
});
vi.advanceTimersByTime(5000);
// Same target again: timer should continue, not restart.
issue.detectDynamic({
targetID: 'camera-1',
view: 'live',
});
// 5 more seconds completes the original 10s timer.
vi.advanceTimersByTime(5000);
expect(issue.hasIssue()).toBe(true);
expect(onChange).toBeCalledTimes(1);
});
it('should not restart timer when targetID is undefined and matches', () => {
const onChange = vi.fn();
const issue = new MediaLoadIssue(createAPI(), onChange);
issue.detectDynamic({ targetID: 'camera-1', view: 'live' });
vi.advanceTimersByTime(5000);
// Same undefined target: timer should continue.
issue.detectDynamic({ targetID: 'camera-1', view: 'live' });
vi.advanceTimersByTime(5000);
expect(issue.hasIssue()).toBe(true);
expect(onChange).toBeCalledTimes(1);
});
it('should not restart timer if already timed out', () => {
const onChange = vi.fn();
const issue = new MediaLoadIssue(createAPI(), onChange);
issue.detectDynamic({ targetID: 'camera-1', view: 'live' });
vi.advanceTimersByTime(10000);
expect(onChange).toBeCalledTimes(1);
// Calling detectDynamic again should not restart timer.
issue.detectDynamic({ targetID: 'camera-1', view: 'live' });
vi.advanceTimersByTime(10000);
expect(onChange).toBeCalledTimes(1);
});
});
describe('trigger', () => {
it('should activate immediately when target has error and view is a media view', () => {
const issue = new MediaLoadIssue(createAPI());
issue.trigger({ targetID: 'camera-1' });
issue.detectDynamic({
targetID: 'camera-1',
view: 'live',
});
expect(issue.hasIssue()).toBe(true);
});
it('should not activate with only a trigger', () => {
const issue = new MediaLoadIssue(createAPI());
issue.trigger({ targetID: 'camera-1' });
expect(issue.hasIssue()).toBe(false);
});
it('should clear target error when media loads', () => {
const issue = new MediaLoadIssue(createAPI());
issue.trigger({ targetID: 'camera-1' });
issue.detectDynamic({
targetID: 'camera-1',
view: 'live',
});
expect(issue.hasIssue()).toBe(true);
// Media loaded clears the error for this target.
issue.detectDynamic({
targetID: 'camera-1',
view: 'live',
mediaLoadedInfo: createMediaLoadedInfo(),
});
// Target error was cleared by the successful load, so this unloaded state
// falls back to the timer (issue would not activate until after the
// timer is reached).
issue.detectDynamic({
targetID: 'camera-1',
view: 'live',
});
expect(issue.hasIssue()).toBe(false);
});
it('should not activate for a different target', () => {
const issue = new MediaLoadIssue(createAPI());
issue.trigger({ targetID: 'camera-1' });
issue.detectDynamic({
targetID: 'camera-2',
view: 'live',
});
// camera-2 has no error, so it falls back to timeout behavior.
expect(issue.hasIssue()).toBe(false);
});
});
describe('getNotification', () => {
it('should return notification regardless of active state', () => {
const issue = new MediaLoadIssue(createAPI());
const notification = issue.getNotification();
expect(notification).toEqual(
expect.objectContaining({
heading: expect.objectContaining({
text: expect.any(String),
}),
link: expect.objectContaining({
url: expect.any(String),
}),
}),
);
});
it('should include metadata for errored targets', () => {
const issue = new MediaLoadIssue(createAPI());
issue.trigger({ targetID: 'camera.office' });
const notification = issue.getNotification();
expect(notification.metadata).toEqual([
expect.objectContaining({ text: 'camera.office', icon: 'mdi:cctv' }),
]);
});
it('should include the pending timer target in metadata', () => {
const issue = new MediaLoadIssue(createAPI());
// Start a load timer (no explicit error yet, just slow-loading).
issue.detectDynamic({ targetID: 'camera.garden', view: 'live' });
const notification = issue.getNotification();
expect(notification.metadata).toEqual([
expect.objectContaining({ text: 'camera.garden', icon: 'mdi:cctv' }),
]);
});
it('should use camera title when available', () => {
const api = createAPI();
vi.mocked(api.getCameraManager().getCameraMetadata).mockReturnValue({
title: 'Office',
icon: { icon: 'mdi:cctv' },
});
const issue = new MediaLoadIssue(api);
issue.trigger({ targetID: 'camera.office' });
const notification = issue.getNotification();
expect(notification.metadata).toEqual([
expect.objectContaining({ text: 'Office' }),
]);
});
it('should use localized label and image icon for the image-view sentinel', () => {
const issue = new MediaLoadIssue(createAPI());
issue.trigger({ targetID: IMAGE_VIEW_TARGET_ID_SENTINEL });
const notification = issue.getNotification();
expect(notification.metadata).toEqual([
expect.objectContaining({ text: 'Image', icon: 'mdi:image' }),
]);
});
it('should include a retry control with wired callback', async () => {
const api = createCardAPI();
const issue = new MediaLoadIssue(api);
const control = issue.getNotification().controls?.[0];
expect(control).toMatchObject({ icon: 'mdi:refresh', dismiss: true });
const tapAction = control?.actions?.tap_action as InternalCallbackActionConfig;
await tapAction.callback(api);
expect(api.getIssueManager().retry).toBeCalledWith('media_load', true);
});
});
describe('getIssue', () => {
it('should return result when timed out', () => {
const issue = new MediaLoadIssue(createAPI());
issue.detectDynamic({ targetID: 'camera-1', view: 'live' });
vi.advanceTimersByTime(10000);
const result = issue.getIssue();
expect(result).toEqual(
expect.objectContaining({
icon: 'mdi:cctv-off',
severity: 'high',
notification: expect.objectContaining({
link: expect.objectContaining({
url: expect.any(String),
}),
}),
}),
);
});
it('should return null when not timed out', () => {
const issue = new MediaLoadIssue(createAPI());
expect(issue.getIssue()).toBeNull();
});
});
describe('needsRetry', () => {
it('should return true when issue is active', () => {
const issue = new MediaLoadIssue(createAPI());
issue.detectDynamic({ targetID: 'camera-1', view: 'live' });
vi.advanceTimersByTime(10000);
expect(issue.needsRetry()).toBe(true);
});
it('should return false when issue is not active', () => {
const issue = new MediaLoadIssue(createAPI());
expect(issue.needsRetry()).toBe(false);
});
});
describe('retry', () => {
it('should keep issue active after retry so error stays visible', () => {
const onChange = vi.fn();
const issue = new MediaLoadIssue(createAPI(), onChange);
issue.detectDynamic({ targetID: 'camera-1', view: 'live' });
vi.advanceTimersByTime(10000);
expect(issue.hasIssue()).toBe(true);
issue.retry();
// Issue remains active — no new 10s grace period. The error stays
// visible while the provider re-attempts loading underneath.
expect(issue.hasIssue()).toBe(true);
});
it('should return false when no targets have errors', () => {
const api = createAPI();
const issue = new MediaLoadIssue(api);
expect(issue.retry()).toBe(false);
});
it('should bump mediaEpoch for targets with errors and call setViewWithMergedContext', () => {
const api = createAPI();
vi.mocked(api.getViewManager().getView).mockReturnValue(mock<View>());
const issue = new MediaLoadIssue(api);
issue.trigger({ targetID: 'camera-1' });
issue.trigger({ targetID: 'media-1' });
const result = issue.retry();
expect(result).toEqual(false);
expect(api.getViewManager().setViewWithMergedContext).toBeCalledWith({
mediaEpoch: { 'camera-1': 1, 'media-1': 1 },
});
});
it('should bump mediaEpoch for the image-view sentinel', () => {
const api = createAPI();
vi.mocked(api.getViewManager().getView).mockReturnValue(mock<View>());
const issue = new MediaLoadIssue(api);
issue.trigger({ targetID: IMAGE_VIEW_TARGET_ID_SENTINEL });
const result = issue.retry();
expect(result).toEqual(false);
expect(api.getViewManager().setViewWithMergedContext).toBeCalledWith({
mediaEpoch: { [IMAGE_VIEW_TARGET_ID_SENTINEL]: 1 },
});
});
it('should increment existing epoch values from current view context', () => {
const api = createAPI();
vi.mocked(api.getViewManager().getView).mockReturnValue(
mock<View>({ context: { mediaEpoch: { 'camera-1': 5, 'camera-2': 3 } } }),
);
const issue = new MediaLoadIssue(api);
issue.trigger({ targetID: 'camera-1' });
const result = issue.retry();
expect(result).toEqual(false);
expect(api.getViewManager().setViewWithMergedContext).toBeCalledWith({
mediaEpoch: { 'camera-1': 6, 'camera-2': 3 },
});
});
it('should include pending timer target in retry', () => {
const api = createAPI();
vi.mocked(api.getViewManager().getView).mockReturnValue(mock<View>());
const issue = new MediaLoadIssue(api);
// Start the timer for camera-1 (not yet timed out).
issue.detectDynamic({ targetID: 'camera-1', view: 'live' });
issue.retry();
expect(api.getViewManager().setViewWithMergedContext).toBeCalledWith({
mediaEpoch: { 'camera-1': 1 },
});
});
it('should keep errored targets and issue state after retry', () => {
const api = createAPI();
vi.mocked(api.getViewManager().getView).mockReturnValue(mock<View>());
const issue = new MediaLoadIssue(api);
issue.trigger({ targetID: 'camera-1' });
issue.detectDynamic({ targetID: 'camera-1', view: 'live' });
expect(issue.hasIssue()).toBe(true);
issue.retry();
// After retry, the issue stays active and the errored target is
// preserved — no new 10s grace period. If media:loaded fires, the
// existing _handleMediaLoaded path will clear everything.
expect(issue.hasIssue()).toBe(true);
issue.detectDynamic({ targetID: 'camera-1', view: 'live' });
expect(issue.hasIssue()).toBe(true);
});
});
describe('reset', () => {
it('should stop timer', () => {
const onChange = vi.fn();
const issue = new MediaLoadIssue(createAPI(), onChange);
issue.detectDynamic({ targetID: 'camera-1', view: 'live' });
issue.reset();
vi.advanceTimersByTime(10000);
expect(issue.hasIssue()).toBe(false);
expect(onChange).not.toBeCalled();
});
});
describe('suspend', () => {
it('should stop the pending-load timer so it cannot mature offscreen', () => {
const onChange = vi.fn();
const issue = new MediaLoadIssue(createAPI(), onChange);
// Enter loading state. Timer arms but has not yet fired.
issue.detectDynamic({ targetID: 'camera-1', view: 'live' });
vi.advanceTimersByTime(5000);
expect(issue.hasIssue()).toBe(false);
// Card detaches: timer must stop.
issue.suspend();
// Full 10s later (plus margin) the timer has NOT matured — the user
// was offscreen and that time does not count against them.
vi.advanceTimersByTime(20000);
expect(issue.hasIssue()).toBe(false);
expect(onChange).not.toBeCalled();
});
it('should preserve an already-active issue across suspend', () => {
const issue = new MediaLoadIssue(createAPI());
// Issue activates (timeout fires).
issue.detectDynamic({ targetID: 'camera-1', view: 'live' });
vi.advanceTimersByTime(10000);
expect(issue.hasIssue()).toBe(true);
// Card detaches — issue must remain visible on reattach.
issue.suspend();
expect(issue.hasIssue()).toBe(true);
});
it('should rearm a fresh timer window on resume via detectDynamic', () => {
const onChange = vi.fn();
const issue = new MediaLoadIssue(createAPI(), onChange);
issue.detectDynamic({ targetID: 'camera-1', view: 'live' });
vi.advanceTimersByTime(5000);
issue.suspend();
// Reattach: the manager's resume() triggers evaluate() → detectDynamic.
// The target is still loading, so the timer arms with a fresh 10s
// window — not whatever was left when we suspended.
issue.detectDynamic({ targetID: 'camera-1', view: 'live' });
vi.advanceTimersByTime(9999);
expect(issue.hasIssue()).toBe(false);
vi.advanceTimersByTime(1);
expect(issue.hasIssue()).toBe(true);
expect(onChange).toBeCalled();
});
});
});
@@ -0,0 +1,150 @@
import { describe, expect, it } from 'vitest';
import { CardController } from '../../../../src/card-controller/controller';
import { MediaQueryIssue } from '../../../../src/card-controller/issues/issues/media-query';
import { InternalCallbackActionConfig } from '../../../../src/config/schema/actions/custom/internal';
import { createCardAPI } from '../../../test-utils';
const createIssue = (): {
issue: MediaQueryIssue;
api: CardController;
} => {
const api = createCardAPI();
const issue = new MediaQueryIssue(api);
return { issue, api };
};
describe('MediaQueryIssue', () => {
it('should have correct key', () => {
const { issue } = createIssue();
expect(issue.key).toBe('media_query');
});
it('should report no issue when untriggered', () => {
const { issue } = createIssue();
expect(issue.hasIssue()).toBe(false);
expect(issue.getIssue()).toBeNull();
});
it('should report an issue after trigger with an error', () => {
const { issue } = createIssue();
issue.trigger({ error: new Error('query failed') });
expect(issue.hasIssue()).toBe(true);
});
it('should treat a triggered null/undefined error as no issue', () => {
const { issue } = createIssue();
issue.trigger({ error: undefined });
expect(issue.hasIssue()).toBe(false);
expect(issue.getIssue()).toBeNull();
expect(issue.needsRetry()).toBe(false);
});
it('should return expected shape from getIssue when triggered with an error', () => {
const { issue } = createIssue();
issue.trigger({ error: new Error('media query failed') });
const result = issue.getIssue();
expect(result).toEqual(
expect.objectContaining({
icon: 'mdi:alert',
severity: 'high',
notification: expect.objectContaining({
body: expect.objectContaining({
text: 'media query failed',
}),
}),
}),
);
});
describe('getNotification', () => {
it('should return null when no error is set', () => {
const { issue } = createIssue();
expect(issue.getNotification()).toBeNull();
});
it('should return notification with retry control when triggered with an error', () => {
const { issue } = createIssue();
issue.trigger({ error: new Error('query failed') });
const notification = issue.getNotification();
expect(notification?.controls).toHaveLength(1);
expect(notification?.controls?.[0]).toMatchObject({
icon: 'mdi:refresh',
dismiss: true,
});
});
it('should call manager.retry with the issue key from retry control callback', async () => {
const { issue, api } = createIssue();
issue.trigger({ error: new Error('query failed') });
const control = issue.getNotification()?.controls?.[0];
const tapAction = control?.actions?.tap_action as InternalCallbackActionConfig;
await tapAction.callback(api);
expect(api.getIssueManager().retry).toBeCalledWith('media_query', true);
});
});
describe('needsRetry', () => {
it('should return true after trigger with an error', () => {
const { issue } = createIssue();
issue.trigger({ error: new Error('query failed') });
expect(issue.needsRetry()).toBe(true);
});
it('should return false when not triggered', () => {
const { issue } = createIssue();
expect(issue.needsRetry()).toBe(false);
});
});
describe('retry', () => {
it('should return requery action and clear error and needsRetry', () => {
const { issue, api } = createIssue();
issue.trigger({ error: new Error('query failed') });
const result = issue.retry();
expect(result).toEqual(true);
expect(api.getViewManager().setViewByParametersWithNewQuery).toBeCalled();
expect(issue.needsRetry()).toBe(false);
expect(issue.hasIssue()).toBe(false);
});
it('should return null when needsRetry is false', () => {
const { issue } = createIssue();
const result = issue.retry();
expect(result).toBe(false);
});
});
it('should clear the issue after reset', () => {
const { issue } = createIssue();
issue.trigger({ error: new Error('oops') });
expect(issue.hasIssue()).toBe(true);
issue.reset();
expect(issue.hasIssue()).toBe(false);
expect(issue.getIssue()).toBeNull();
});
it('should clear needsRetry after reset', () => {
const { issue } = createIssue();
issue.trigger({ error: new Error('oops') });
expect(issue.needsRetry()).toBe(true);
issue.reset();
expect(issue.needsRetry()).toBe(false);
});
});
@@ -0,0 +1,109 @@
import { describe, expect, it, vi } from 'vitest';
import { mock } from 'vitest-mock-extended';
import { CardController } from '../../../../src/card-controller/controller';
import { ViewIncompatibleIssue } from '../../../../src/card-controller/issues/issues/view-incompatible';
import { AdvancedCameraCardError } from '../../../../src/types';
import { View } from '../../../../src/view/view';
import { createCardAPI } from '../../../test-utils';
describe('ViewIncompatibleIssue', () => {
const createAPI = (hasView = false): CardController => {
const api = createCardAPI();
vi.mocked(api.getViewManager().getView).mockReturnValue(
hasView ? mock<View>() : null,
);
return api;
};
it('should have correct key', () => {
const issue = new ViewIncompatibleIssue(createAPI());
expect(issue.key).toBe('view_incompatible');
});
it('should report no issue when untriggered', () => {
const issue = new ViewIncompatibleIssue(createAPI());
expect(issue.hasIssue()).toBe(false);
expect(issue.getIssue()).toBeNull();
});
it('should report an issue after trigger', () => {
const issue = new ViewIncompatibleIssue(createAPI());
issue.trigger({ error: new Error('boom') });
expect(issue.hasIssue()).toBe(true);
});
it('should treat a triggered null/undefined error as no issue', () => {
const issue = new ViewIncompatibleIssue(createAPI());
issue.trigger({ error: undefined });
expect(issue.hasIssue()).toBe(false);
expect(issue.getIssue()).toBeNull();
});
describe('isFullCardIssue', () => {
it('should return true when no view is set', () => {
const issue = new ViewIncompatibleIssue(createAPI(false));
expect(issue.isFullCardIssue()).toBe(true);
});
it('should return false when a view is set', () => {
const issue = new ViewIncompatibleIssue(createAPI(true));
expect(issue.isFullCardIssue()).toBe(false);
});
});
describe('getIssue', () => {
it('should return a notification with heading, body, and no retry control', () => {
const issue = new ViewIncompatibleIssue(createAPI());
issue.trigger({ error: new Error('boom') });
expect(issue.getIssue()).toEqual({
icon: 'mdi:video-off',
severity: 'high',
notification: {
heading: {
text: 'View not supported',
icon: 'mdi:video-off',
severity: 'high',
},
body: { text: 'The selected camera or media does not support this view' },
},
});
});
it('should include error context on AdvancedCameraCardError', () => {
const issue = new ViewIncompatibleIssue(createAPI());
issue.trigger({
error: new AdvancedCameraCardError('err', {
view: 'snapshot',
camera: 'cam.office',
}),
});
const result = issue.getIssue();
expect(result?.notification.context).toEqual([
expect.stringContaining('view: snapshot'),
]);
});
it('should omit context on plain errors', () => {
const issue = new ViewIncompatibleIssue(createAPI());
issue.trigger({ error: new Error('plain') });
const result = issue.getIssue();
expect(result?.notification.context).toBeUndefined();
});
});
it('should clear the issue after reset', () => {
const issue = new ViewIncompatibleIssue(createAPI());
issue.trigger({ error: new Error('boom') });
expect(issue.hasIssue()).toBe(true);
issue.reset();
expect(issue.hasIssue()).toBe(false);
expect(issue.getIssue()).toBeNull();
});
});