fix: Allow multiple overrides to operate independently (#2063)
- Closes #1954
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
import { isEqual } from 'lodash-es';
|
import { isEqual } from 'lodash-es';
|
||||||
import { ConditionsManager } from '../../conditions/conditions-manager.js';
|
|
||||||
import { isConfigUpgradeable } from '../../config/management.js';
|
import { isConfigUpgradeable } from '../../config/management.js';
|
||||||
import { setProfiles } from '../../config/profiles/set-profiles.js';
|
import { setProfiles } from '../../config/profiles/set-profiles.js';
|
||||||
import {
|
import {
|
||||||
@@ -12,11 +11,11 @@ import { localize } from '../../localize/localize.js';
|
|||||||
import { getParseErrorPaths } from '../../utils/zod.js';
|
import { getParseErrorPaths } from '../../utils/zod.js';
|
||||||
import { InitializationAspect } from '../initialization-manager.js';
|
import { InitializationAspect } from '../initialization-manager.js';
|
||||||
import { CardConfigAPI } from '../types.js';
|
import { CardConfigAPI } from '../types.js';
|
||||||
import { getOverriddenConfig } from './get-overridden-config.js';
|
|
||||||
import { setAutomationsFromConfig } from './load-automations.js';
|
import { setAutomationsFromConfig } from './load-automations.js';
|
||||||
import { setRemoteControlEntityFromConfig } from './load-control-entities.js';
|
import { setRemoteControlEntityFromConfig } from './load-control-entities.js';
|
||||||
import { setFoldersFromConfig } from './load-folders.js';
|
import { setFoldersFromConfig } from './load-folders.js';
|
||||||
import { setKeyboardShortcutsFromConfig } from './load-keyboard-shortcuts.js';
|
import { setKeyboardShortcutsFromConfig } from './load-keyboard-shortcuts.js';
|
||||||
|
import { OverridesManager } from './overrides-manager.js';
|
||||||
|
|
||||||
export class ConfigManager {
|
export class ConfigManager {
|
||||||
protected _api: CardConfigAPI;
|
protected _api: CardConfigAPI;
|
||||||
@@ -29,7 +28,9 @@ export class ConfigManager {
|
|||||||
protected _overriddenConfig: AdvancedCameraCardConfig | null = null;
|
protected _overriddenConfig: AdvancedCameraCardConfig | null = null;
|
||||||
protected _rawConfig: RawAdvancedCameraCardConfig | null = null;
|
protected _rawConfig: RawAdvancedCameraCardConfig | null = null;
|
||||||
protected _cardWideConfig: CardWideConfig | null = null;
|
protected _cardWideConfig: CardWideConfig | null = null;
|
||||||
protected _overridesConditionsManager: ConditionsManager | null = null;
|
protected _overridesManager = new OverridesManager(() =>
|
||||||
|
this._processOverrideConfig(),
|
||||||
|
);
|
||||||
|
|
||||||
constructor(api: CardConfigAPI) {
|
constructor(api: CardConfigAPI) {
|
||||||
this._api = api;
|
this._api = api;
|
||||||
@@ -89,14 +90,10 @@ export class ConfigManager {
|
|||||||
debug: config.debug,
|
debug: config.debug,
|
||||||
};
|
};
|
||||||
|
|
||||||
this._overridesConditionsManager?.destroy();
|
this._overridesManager.set(
|
||||||
this._overridesConditionsManager = this._config.overrides?.length
|
|
||||||
? new ConditionsManager(
|
|
||||||
this._config.overrides.map((override) => override.conditions).flat(),
|
|
||||||
this._api.getConditionStateManager(),
|
this._api.getConditionStateManager(),
|
||||||
)
|
this._config.overrides,
|
||||||
: null;
|
);
|
||||||
this._overridesConditionsManager?.addListener(() => this._processOverrideConfig());
|
|
||||||
|
|
||||||
this._api.getConditionStateManager().setState({
|
this._api.getConditionStateManager().setState({
|
||||||
view: undefined,
|
view: undefined,
|
||||||
@@ -121,11 +118,6 @@ export class ConfigManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected _processOverrideConfig(): void {
|
protected _processOverrideConfig(): void {
|
||||||
/* istanbul ignore if: No (current) way to reach this code -- @preserve */
|
|
||||||
if (!this._config) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const overriddenConfig = this._getOverriddenConfig();
|
const overriddenConfig = this._getOverriddenConfig();
|
||||||
|
|
||||||
// Save on Lit re-rendering costs by only updating the configuration if it
|
// Save on Lit re-rendering costs by only updating the configuration if it
|
||||||
@@ -163,15 +155,13 @@ export class ConfigManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected _getOverriddenConfig(): AdvancedCameraCardConfig | null {
|
protected _getOverriddenConfig(): AdvancedCameraCardConfig | null {
|
||||||
if (!this._overridesConditionsManager || !this._config) {
|
/* istanbul ignore if: No (current) way to reach this code -- @preserve */
|
||||||
return this._config;
|
if (!this._config) {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return getOverriddenConfig(this._overridesConditionsManager, this._config, {
|
return this._overridesManager.getConfig(this._config);
|
||||||
configOverrides: this._config.overrides,
|
|
||||||
schema: advancedCameraCardConfigSchema,
|
|
||||||
});
|
|
||||||
} catch (ev) {
|
} catch (ev) {
|
||||||
this._api.getMessageManager().setErrorIfHigherPriority(ev);
|
this._api.getMessageManager().setErrorIfHigherPriority(ev);
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -1,77 +0,0 @@
|
|||||||
import { merge } from 'lodash-es';
|
|
||||||
import { ZodType as ZodSchema } from 'zod';
|
|
||||||
import { ConditionsManagerReadonlyInterface } from '../../conditions/types';
|
|
||||||
import {
|
|
||||||
copyConfig,
|
|
||||||
deleteConfigValue,
|
|
||||||
getConfigValue,
|
|
||||||
setConfigValue,
|
|
||||||
} from '../../config/management';
|
|
||||||
import { Overrides } from '../../config/schema/overrides';
|
|
||||||
import { RawAdvancedCameraCardConfig } from '../../config/types';
|
|
||||||
import { localize } from '../../localize/localize';
|
|
||||||
import { AdvancedCameraCardError } from '../../types';
|
|
||||||
import { desparsifyArrays } from '../../utils/basic';
|
|
||||||
|
|
||||||
class OverrideConfigurationError extends AdvancedCameraCardError {}
|
|
||||||
|
|
||||||
export function getOverriddenConfig<RT extends RawAdvancedCameraCardConfig>(
|
|
||||||
manager: ConditionsManagerReadonlyInterface,
|
|
||||||
config: Readonly<RT>,
|
|
||||||
options?: {
|
|
||||||
configOverrides?: Readonly<Overrides>;
|
|
||||||
schema?: ZodSchema;
|
|
||||||
},
|
|
||||||
): RT {
|
|
||||||
if (!options?.configOverrides) {
|
|
||||||
return config;
|
|
||||||
}
|
|
||||||
|
|
||||||
let output = copyConfig(config);
|
|
||||||
let overridden = false;
|
|
||||||
for (const override of options.configOverrides) {
|
|
||||||
if (manager.getEvaluation()?.result) {
|
|
||||||
override.delete?.forEach((deletionKey) => {
|
|
||||||
deleteConfigValue(output, deletionKey);
|
|
||||||
});
|
|
||||||
|
|
||||||
Object.keys(override.set ?? {}).forEach((setKey) => {
|
|
||||||
setConfigValue(output, setKey, override.set?.[setKey]);
|
|
||||||
});
|
|
||||||
|
|
||||||
Object.keys(override.merge ?? {}).forEach((mergeKey) => {
|
|
||||||
setConfigValue(
|
|
||||||
output,
|
|
||||||
mergeKey,
|
|
||||||
merge({}, getConfigValue(output, mergeKey), override.merge?.[mergeKey]),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
overridden = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!overridden) {
|
|
||||||
// Return the same configuration object if it has not been overridden (to
|
|
||||||
// reduce re-renders for a configuration that has not changed).
|
|
||||||
return config;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options?.configOverrides?.some((override) => override.delete?.length)) {
|
|
||||||
// If anything was deleted during this override, empty undefined slots may
|
|
||||||
// be left in arrays where values were unset. Desparsify them.
|
|
||||||
output = desparsifyArrays(output);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options?.schema) {
|
|
||||||
const parseResult = options.schema.safeParse(output);
|
|
||||||
if (!parseResult.success) {
|
|
||||||
throw new OverrideConfigurationError(
|
|
||||||
localize('error.invalid_configuration_override'),
|
|
||||||
[parseResult.error.errors, output],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return parseResult.data;
|
|
||||||
}
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,102 @@
|
|||||||
|
import { merge } from 'lodash-es';
|
||||||
|
import { ConditionsManager } from '../../conditions/conditions-manager';
|
||||||
|
import { ConditionStateManagerReadonlyInterface } from '../../conditions/types';
|
||||||
|
import {
|
||||||
|
copyConfig,
|
||||||
|
deleteConfigValue,
|
||||||
|
getConfigValue,
|
||||||
|
setConfigValue,
|
||||||
|
} from '../../config/management';
|
||||||
|
import { Override } from '../../config/schema/overrides';
|
||||||
|
import {
|
||||||
|
AdvancedCameraCardConfig,
|
||||||
|
advancedCameraCardConfigSchema,
|
||||||
|
} from '../../config/schema/types';
|
||||||
|
import { localize } from '../../localize/localize';
|
||||||
|
import { AdvancedCameraCardError } from '../../types';
|
||||||
|
import { desparsifyArrays } from '../../utils/basic.js';
|
||||||
|
|
||||||
|
type OverridesCallback = () => void;
|
||||||
|
|
||||||
|
class OverrideConfigurationError extends AdvancedCameraCardError {}
|
||||||
|
|
||||||
|
export class OverridesManager {
|
||||||
|
private _overrides = new Map<Override, ConditionsManager>();
|
||||||
|
private _callback: OverridesCallback;
|
||||||
|
|
||||||
|
constructor(callback: OverridesCallback) {
|
||||||
|
this._callback = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
private _clear(): void {
|
||||||
|
this._overrides.forEach((manager) => manager.destroy());
|
||||||
|
this._overrides.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public hasOverrides(): boolean {
|
||||||
|
return !!this._overrides.size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public set(
|
||||||
|
stateManager: ConditionStateManagerReadonlyInterface,
|
||||||
|
overrides?: Override[],
|
||||||
|
): void {
|
||||||
|
this._clear();
|
||||||
|
|
||||||
|
overrides?.forEach((override) => {
|
||||||
|
const manager = new ConditionsManager(override.conditions, stateManager);
|
||||||
|
manager.addListener(this._callback);
|
||||||
|
this._overrides.set(override, manager);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public getConfig(base: AdvancedCameraCardConfig): AdvancedCameraCardConfig {
|
||||||
|
let output = copyConfig(base);
|
||||||
|
let overridden = false;
|
||||||
|
let desparsify = false;
|
||||||
|
|
||||||
|
for (const [override, manager] of this._overrides.entries()) {
|
||||||
|
if (manager.getEvaluation()?.result) {
|
||||||
|
override.delete?.forEach((deletionKey) => {
|
||||||
|
deleteConfigValue(output, deletionKey);
|
||||||
|
desparsify = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.keys(override.set ?? {}).forEach((setKey) => {
|
||||||
|
setConfigValue(output, setKey, override.set?.[setKey]);
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.keys(override.merge ?? {}).forEach((mergeKey) => {
|
||||||
|
setConfigValue(
|
||||||
|
output,
|
||||||
|
mergeKey,
|
||||||
|
merge({}, getConfigValue(output, mergeKey), override.merge?.[mergeKey]),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
overridden = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!overridden) {
|
||||||
|
// Return the same configuration object if it has not been overridden (to
|
||||||
|
// reduce re-renders for a configuration that has not changed).
|
||||||
|
return base;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (desparsify) {
|
||||||
|
// If anything was deleted during this override, empty undefined slots may
|
||||||
|
// be left in arrays where values were unset. Desparsify them.
|
||||||
|
output = desparsifyArrays(output);
|
||||||
|
}
|
||||||
|
|
||||||
|
const parseResult = advancedCameraCardConfigSchema.safeParse(output);
|
||||||
|
if (!parseResult.success) {
|
||||||
|
throw new OverrideConfigurationError(
|
||||||
|
localize('error.invalid_configuration_override'),
|
||||||
|
[parseResult.error.errors, output],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return parseResult.data;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,12 @@
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { advancedCameraCardConditionSchema } from './conditions/types';
|
import { advancedCameraCardConditionSchema } from './conditions/types';
|
||||||
|
|
||||||
export const overridesSchema = z
|
const overrideSchema = z.object({
|
||||||
.object({
|
|
||||||
conditions: advancedCameraCardConditionSchema.array(),
|
conditions: advancedCameraCardConditionSchema.array(),
|
||||||
merge: z.object({}).passthrough().optional(),
|
merge: z.object({}).passthrough().optional(),
|
||||||
set: z.object({}).passthrough().optional(),
|
set: z.object({}).passthrough().optional(),
|
||||||
delete: z.string().array().optional(),
|
delete: z.string().array().optional(),
|
||||||
})
|
});
|
||||||
.array()
|
export type Override = z.infer<typeof overrideSchema>;
|
||||||
.optional();
|
|
||||||
export type Overrides = z.infer<typeof overridesSchema>;
|
export const overridesSchema = overrideSchema.array().optional();
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { ConfigManager } from '../../../src/card-controller/config/config-manage
|
|||||||
import { InitializationAspect } from '../../../src/card-controller/initialization-manager';
|
import { InitializationAspect } from '../../../src/card-controller/initialization-manager';
|
||||||
import { ConditionStateManager } from '../../../src/conditions/state-manager';
|
import { ConditionStateManager } from '../../../src/conditions/state-manager';
|
||||||
import { advancedCameraCardConfigSchema } from '../../../src/config/schema/types';
|
import { advancedCameraCardConfigSchema } from '../../../src/config/schema/types';
|
||||||
import { createCardAPI, flushPromises } from '../../test-utils';
|
import { createCardAPI, createConfig, flushPromises } from '../../test-utils';
|
||||||
|
|
||||||
describe('ConfigManager', () => {
|
describe('ConfigManager', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -162,6 +162,7 @@ describe('ConfigManager', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('should override', () => {
|
||||||
it('should ignore overrides with same config', () => {
|
it('should ignore overrides with same config', () => {
|
||||||
const api = createCardAPI();
|
const api = createCardAPI();
|
||||||
const stateManager = new ConditionStateManager();
|
const stateManager = new ConditionStateManager();
|
||||||
@@ -192,15 +193,13 @@ describe('ConfigManager', () => {
|
|||||||
expect(api.getStyleManager().updateFromConfig).toBeCalledTimes(1);
|
expect(api.getStyleManager().updateFromConfig).toBeCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should override', () => {
|
it('should honor override', () => {
|
||||||
const api = createCardAPI();
|
const api = createCardAPI();
|
||||||
const stateManager = new ConditionStateManager();
|
const stateManager = new ConditionStateManager();
|
||||||
vi.mocked(api.getConditionStateManager).mockReturnValue(stateManager);
|
vi.mocked(api.getConditionStateManager).mockReturnValue(stateManager);
|
||||||
|
|
||||||
const manager = new ConfigManager(api);
|
const manager = new ConfigManager(api);
|
||||||
const config = {
|
const config = createConfig({
|
||||||
type: 'custom:advanced-camera-card',
|
|
||||||
cameras: [{ camera_entity: 'camera.office' }],
|
|
||||||
menu: {
|
menu: {
|
||||||
style: 'hidden',
|
style: 'hidden',
|
||||||
},
|
},
|
||||||
@@ -210,7 +209,7 @@ describe('ConfigManager', () => {
|
|||||||
set: { 'menu.style': 'none' },
|
set: { 'menu.style': 'none' },
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
});
|
||||||
|
|
||||||
manager.setConfig(config);
|
manager.setConfig(config);
|
||||||
expect(manager.getConfig()?.menu?.style).toBe('hidden');
|
expect(manager.getConfig()?.menu?.style).toBe('hidden');
|
||||||
@@ -393,4 +392,5 @@ describe('ConfigManager', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,305 +0,0 @@
|
|||||||
import { describe, expect, it } from 'vitest';
|
|
||||||
import { mock } from 'vitest-mock-extended';
|
|
||||||
import { z } from 'zod';
|
|
||||||
import { getOverriddenConfig } from '../../../src/card-controller/config/get-overridden-config';
|
|
||||||
import { ConditionsManagerReadonlyInterface } from '../../../src/conditions/types';
|
|
||||||
|
|
||||||
describe('getOverriddenConfig', () => {
|
|
||||||
const config = {
|
|
||||||
menu: {
|
|
||||||
style: 'none',
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
it('should not override without overrides', () => {
|
|
||||||
const manager = mock<ConditionsManagerReadonlyInterface>();
|
|
||||||
manager.getEvaluation.mockReturnValue({ result: true });
|
|
||||||
|
|
||||||
expect(getOverriddenConfig(manager, config)).toBe(config);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should not override when conditions do not match', () => {
|
|
||||||
const manager = mock<ConditionsManagerReadonlyInterface>();
|
|
||||||
manager.getEvaluation.mockReturnValue({ result: false });
|
|
||||||
|
|
||||||
expect(
|
|
||||||
getOverriddenConfig(manager, config, {
|
|
||||||
configOverrides: [
|
|
||||||
{
|
|
||||||
merge: {
|
|
||||||
menu: {
|
|
||||||
style: 'hidden',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
delete: ['menu.style'],
|
|
||||||
set: {
|
|
||||||
'menu.style': 'overlay',
|
|
||||||
},
|
|
||||||
conditions: [
|
|
||||||
{
|
|
||||||
condition: 'fullscreen' as const,
|
|
||||||
fullscreen: true,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
).toBe(config);
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('should merge', () => {
|
|
||||||
it('with path', () => {
|
|
||||||
const manager = mock<ConditionsManagerReadonlyInterface>();
|
|
||||||
manager.getEvaluation.mockReturnValue({ result: true });
|
|
||||||
|
|
||||||
expect(
|
|
||||||
getOverriddenConfig(manager, config, {
|
|
||||||
configOverrides: [
|
|
||||||
{
|
|
||||||
merge: {
|
|
||||||
'live.controls.thumbnails': {
|
|
||||||
mode: 'none',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
conditions: [
|
|
||||||
{
|
|
||||||
condition: 'fullscreen' as const,
|
|
||||||
fullscreen: true,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
).toEqual({
|
|
||||||
menu: {
|
|
||||||
style: 'none',
|
|
||||||
},
|
|
||||||
live: {
|
|
||||||
controls: {
|
|
||||||
thumbnails: {
|
|
||||||
mode: 'none',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('without path', () => {
|
|
||||||
const manager = mock<ConditionsManagerReadonlyInterface>();
|
|
||||||
manager.getEvaluation.mockReturnValue({ result: true });
|
|
||||||
|
|
||||||
expect(
|
|
||||||
getOverriddenConfig(manager, config, {
|
|
||||||
configOverrides: [
|
|
||||||
{
|
|
||||||
merge: {
|
|
||||||
menu: {
|
|
||||||
style: 'hidden',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
conditions: [
|
|
||||||
{
|
|
||||||
condition: 'fullscreen' as const,
|
|
||||||
fullscreen: true,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
).toEqual({
|
|
||||||
menu: {
|
|
||||||
style: 'hidden',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('with invalid merge', () => {
|
|
||||||
const manager = mock<ConditionsManagerReadonlyInterface>();
|
|
||||||
manager.getEvaluation.mockReturnValue({ result: true });
|
|
||||||
|
|
||||||
expect(
|
|
||||||
getOverriddenConfig(manager, config, {
|
|
||||||
configOverrides: [
|
|
||||||
{
|
|
||||||
merge: 6 as unknown as Record<string, unknown>,
|
|
||||||
conditions: [
|
|
||||||
{
|
|
||||||
condition: 'fullscreen' as const,
|
|
||||||
fullscreen: true,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
).toEqual({
|
|
||||||
menu: {
|
|
||||||
style: 'none',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('should set', () => {
|
|
||||||
it('leaf node', () => {
|
|
||||||
const manager = mock<ConditionsManagerReadonlyInterface>();
|
|
||||||
manager.getEvaluation.mockReturnValue({ result: true });
|
|
||||||
|
|
||||||
expect(
|
|
||||||
getOverriddenConfig(manager, config, {
|
|
||||||
configOverrides: [
|
|
||||||
{
|
|
||||||
set: {
|
|
||||||
'menu.style': 'hidden',
|
|
||||||
},
|
|
||||||
conditions: [
|
|
||||||
{
|
|
||||||
condition: 'fullscreen' as const,
|
|
||||||
fullscreen: true,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
).toEqual({
|
|
||||||
menu: {
|
|
||||||
style: 'hidden',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('root node', () => {
|
|
||||||
const manager = mock<ConditionsManagerReadonlyInterface>();
|
|
||||||
manager.getEvaluation.mockReturnValue({ result: true });
|
|
||||||
|
|
||||||
expect(
|
|
||||||
getOverriddenConfig(manager, config, {
|
|
||||||
configOverrides: [
|
|
||||||
{
|
|
||||||
set: {
|
|
||||||
menu: {
|
|
||||||
style: 'hidden',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
conditions: [
|
|
||||||
{
|
|
||||||
condition: 'fullscreen' as const,
|
|
||||||
fullscreen: true,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
).toEqual({
|
|
||||||
menu: {
|
|
||||||
style: 'hidden',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('should delete', () => {
|
|
||||||
it('leaf node', () => {
|
|
||||||
const manager = mock<ConditionsManagerReadonlyInterface>();
|
|
||||||
manager.getEvaluation.mockReturnValue({ result: true });
|
|
||||||
|
|
||||||
expect(
|
|
||||||
getOverriddenConfig(manager, config, {
|
|
||||||
configOverrides: [
|
|
||||||
{
|
|
||||||
delete: ['menu.style' as const],
|
|
||||||
conditions: [
|
|
||||||
{
|
|
||||||
condition: 'fullscreen' as const,
|
|
||||||
fullscreen: true,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
).toEqual({
|
|
||||||
menu: {},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('root node', () => {
|
|
||||||
const manager = mock<ConditionsManagerReadonlyInterface>();
|
|
||||||
manager.getEvaluation.mockReturnValue({ result: true });
|
|
||||||
|
|
||||||
expect(
|
|
||||||
getOverriddenConfig(manager, config, {
|
|
||||||
configOverrides: [
|
|
||||||
{
|
|
||||||
delete: ['menu' as const],
|
|
||||||
conditions: [
|
|
||||||
{
|
|
||||||
condition: 'fullscreen' as const,
|
|
||||||
fullscreen: true,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
).toEqual({});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('should validate schema', () => {
|
|
||||||
const testSchema = z.object({
|
|
||||||
menu: z.object({
|
|
||||||
style: z.enum(['none', 'hidden']),
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
it('passing', () => {
|
|
||||||
const manager = mock<ConditionsManagerReadonlyInterface>();
|
|
||||||
manager.getEvaluation.mockReturnValue({ result: true });
|
|
||||||
|
|
||||||
expect(
|
|
||||||
getOverriddenConfig(manager, config, {
|
|
||||||
configOverrides: [
|
|
||||||
{
|
|
||||||
conditions: [
|
|
||||||
{
|
|
||||||
condition: 'fullscreen' as const,
|
|
||||||
fullscreen: true,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
set: {
|
|
||||||
'menu.style': 'hidden',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
schema: testSchema,
|
|
||||||
}),
|
|
||||||
).toEqual({
|
|
||||||
menu: {
|
|
||||||
style: 'hidden',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('failing', () => {
|
|
||||||
const manager = mock<ConditionsManagerReadonlyInterface>();
|
|
||||||
manager.getEvaluation.mockReturnValue({ result: true });
|
|
||||||
|
|
||||||
expect(() =>
|
|
||||||
getOverriddenConfig(manager, config, {
|
|
||||||
configOverrides: [
|
|
||||||
{
|
|
||||||
conditions: [
|
|
||||||
{
|
|
||||||
condition: 'fullscreen' as const,
|
|
||||||
fullscreen: true,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
set: {
|
|
||||||
'menu.style': 'NOT_A_STYLE',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
schema: testSchema,
|
|
||||||
}),
|
|
||||||
).toThrowError(/Invalid override configuration/);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -0,0 +1,395 @@
|
|||||||
|
import { describe, expect, it, vi } from 'vitest';
|
||||||
|
import { OverridesManager } from '../../../src/card-controller/config/overrides-manager';
|
||||||
|
import { ConditionStateManager } from '../../../src/conditions/state-manager';
|
||||||
|
import { createConfig } from '../../test-utils';
|
||||||
|
|
||||||
|
describe('OverridesManager', () => {
|
||||||
|
it('should add overrides', () => {
|
||||||
|
const config = createConfig({
|
||||||
|
overrides: [
|
||||||
|
{
|
||||||
|
set: {
|
||||||
|
'menu.style': 'overlay',
|
||||||
|
},
|
||||||
|
conditions: [
|
||||||
|
{
|
||||||
|
condition: 'fullscreen' as const,
|
||||||
|
fullscreen: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const manager = new OverridesManager(vi.fn());
|
||||||
|
manager.set(new ConditionStateManager(), config.overrides);
|
||||||
|
|
||||||
|
expect(manager.hasOverrides()).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should clear overrides', () => {
|
||||||
|
const config = createConfig({
|
||||||
|
overrides: [
|
||||||
|
{
|
||||||
|
set: {
|
||||||
|
'menu.style': 'overlay',
|
||||||
|
},
|
||||||
|
conditions: [
|
||||||
|
{
|
||||||
|
condition: 'fullscreen' as const,
|
||||||
|
fullscreen: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const stateManager = new ConditionStateManager();
|
||||||
|
const manager = new OverridesManager(vi.fn());
|
||||||
|
manager.set(stateManager, config.overrides);
|
||||||
|
|
||||||
|
expect(manager.getConfig(config).menu?.style).toBe('hidden');
|
||||||
|
|
||||||
|
manager.set(stateManager, []);
|
||||||
|
|
||||||
|
stateManager.setState({ fullscreen: true });
|
||||||
|
expect(manager.getConfig(config).menu?.style).toBe('hidden');
|
||||||
|
|
||||||
|
expect(manager.hasOverrides()).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not override when conditions do not match', () => {
|
||||||
|
const config = createConfig({
|
||||||
|
overrides: [
|
||||||
|
{
|
||||||
|
set: {
|
||||||
|
'menu.style': 'overlay',
|
||||||
|
},
|
||||||
|
conditions: [
|
||||||
|
{
|
||||||
|
condition: 'fullscreen' as const,
|
||||||
|
fullscreen: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const manager = new OverridesManager(vi.fn());
|
||||||
|
manager.set(new ConditionStateManager(), config.overrides);
|
||||||
|
|
||||||
|
expect(manager.getConfig(config)).toBe(config);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should callback on change', () => {
|
||||||
|
const config = createConfig({
|
||||||
|
overrides: [
|
||||||
|
{
|
||||||
|
set: {
|
||||||
|
'menu.style': 'overlay',
|
||||||
|
},
|
||||||
|
conditions: [
|
||||||
|
{
|
||||||
|
condition: 'fullscreen' as const,
|
||||||
|
fullscreen: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const callback = vi.fn();
|
||||||
|
const stateManager = new ConditionStateManager();
|
||||||
|
const manager = new OverridesManager(callback);
|
||||||
|
manager.set(stateManager, config.overrides);
|
||||||
|
|
||||||
|
expect(manager.getConfig(config).menu?.style).toBe('hidden');
|
||||||
|
|
||||||
|
expect(callback).not.toBeCalled();
|
||||||
|
|
||||||
|
stateManager.setState({ fullscreen: true });
|
||||||
|
|
||||||
|
expect(callback).toBeCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('should handle override merge', () => {
|
||||||
|
it('with path', () => {
|
||||||
|
const config = createConfig({
|
||||||
|
overrides: [
|
||||||
|
{
|
||||||
|
merge: {
|
||||||
|
'live.controls.thumbnails': {
|
||||||
|
mode: 'none',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
conditions: [
|
||||||
|
{
|
||||||
|
condition: 'fullscreen' as const,
|
||||||
|
fullscreen: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const stateManager = new ConditionStateManager();
|
||||||
|
stateManager.setState({ fullscreen: true });
|
||||||
|
|
||||||
|
const manager = new OverridesManager(vi.fn());
|
||||||
|
manager.set(stateManager, config.overrides);
|
||||||
|
|
||||||
|
const overriddenConfig = manager.getConfig(config);
|
||||||
|
|
||||||
|
expect(config.live.controls.thumbnails.mode).toBe('right');
|
||||||
|
expect(overriddenConfig.live.controls.thumbnails.mode).toBe('none');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('without path', () => {
|
||||||
|
const config = createConfig({
|
||||||
|
overrides: [
|
||||||
|
{
|
||||||
|
merge: {
|
||||||
|
live: {
|
||||||
|
controls: {
|
||||||
|
thumbnails: {
|
||||||
|
mode: 'none',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
conditions: [
|
||||||
|
{
|
||||||
|
condition: 'fullscreen' as const,
|
||||||
|
fullscreen: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const stateManager = new ConditionStateManager();
|
||||||
|
stateManager.setState({ fullscreen: true });
|
||||||
|
|
||||||
|
const manager = new OverridesManager(vi.fn());
|
||||||
|
manager.set(stateManager, config.overrides);
|
||||||
|
|
||||||
|
const overriddenConfig = manager.getConfig(config);
|
||||||
|
|
||||||
|
expect(config.live.controls.thumbnails.mode).toBe('right');
|
||||||
|
expect(overriddenConfig.live.controls.thumbnails.mode).toBe('none');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('should handle override set', () => {
|
||||||
|
it('leaf node', () => {
|
||||||
|
const config = createConfig({
|
||||||
|
overrides: [
|
||||||
|
{
|
||||||
|
set: {
|
||||||
|
'live.controls.thumbnails.mode': 'none',
|
||||||
|
},
|
||||||
|
conditions: [
|
||||||
|
{
|
||||||
|
condition: 'fullscreen' as const,
|
||||||
|
fullscreen: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const stateManager = new ConditionStateManager();
|
||||||
|
stateManager.setState({ fullscreen: true });
|
||||||
|
|
||||||
|
const manager = new OverridesManager(vi.fn());
|
||||||
|
manager.set(stateManager, config.overrides);
|
||||||
|
|
||||||
|
const overriddenConfig = manager.getConfig(config);
|
||||||
|
|
||||||
|
expect(config.live.controls.thumbnails.mode).toBe('right');
|
||||||
|
expect(overriddenConfig.live.controls.thumbnails.mode).toBe('none');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('root node', () => {
|
||||||
|
const config = createConfig({
|
||||||
|
overrides: [
|
||||||
|
{
|
||||||
|
set: {
|
||||||
|
live: {
|
||||||
|
controls: {
|
||||||
|
thumbnails: {
|
||||||
|
mode: 'none',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
conditions: [
|
||||||
|
{
|
||||||
|
condition: 'fullscreen' as const,
|
||||||
|
fullscreen: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const stateManager = new ConditionStateManager();
|
||||||
|
stateManager.setState({ fullscreen: true });
|
||||||
|
|
||||||
|
const manager = new OverridesManager(vi.fn());
|
||||||
|
manager.set(stateManager, config.overrides);
|
||||||
|
|
||||||
|
const overriddenConfig = manager.getConfig(config);
|
||||||
|
|
||||||
|
expect(config.live.controls.thumbnails.mode).toBe('right');
|
||||||
|
expect(overriddenConfig.live.controls.thumbnails.mode).toBe('none');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('should handle override delete', () => {
|
||||||
|
it('leaf node', () => {
|
||||||
|
const config = createConfig({
|
||||||
|
live: {
|
||||||
|
controls: {
|
||||||
|
thumbnails: {
|
||||||
|
mode: 'left',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
overrides: [
|
||||||
|
{
|
||||||
|
delete: ['live.controls.thumbnails.mode' as const],
|
||||||
|
conditions: [
|
||||||
|
{
|
||||||
|
condition: 'fullscreen' as const,
|
||||||
|
fullscreen: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const stateManager = new ConditionStateManager();
|
||||||
|
stateManager.setState({ fullscreen: true });
|
||||||
|
|
||||||
|
const manager = new OverridesManager(vi.fn());
|
||||||
|
manager.set(stateManager, config.overrides);
|
||||||
|
|
||||||
|
const overriddenConfig = manager.getConfig(config);
|
||||||
|
|
||||||
|
expect(config.live.controls.thumbnails.mode).toBe('left');
|
||||||
|
expect(overriddenConfig.live.controls.thumbnails.mode).toBe('right');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('root node', () => {
|
||||||
|
const config = createConfig({
|
||||||
|
live: {
|
||||||
|
controls: {
|
||||||
|
thumbnails: {
|
||||||
|
mode: 'left',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
overrides: [
|
||||||
|
{
|
||||||
|
delete: ['live' as const],
|
||||||
|
conditions: [
|
||||||
|
{
|
||||||
|
condition: 'fullscreen' as const,
|
||||||
|
fullscreen: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const stateManager = new ConditionStateManager();
|
||||||
|
stateManager.setState({ fullscreen: true });
|
||||||
|
|
||||||
|
const manager = new OverridesManager(vi.fn());
|
||||||
|
manager.set(stateManager, config.overrides);
|
||||||
|
|
||||||
|
const overriddenConfig = manager.getConfig(config);
|
||||||
|
|
||||||
|
expect(config.live.controls.thumbnails.mode).toBe('left');
|
||||||
|
expect(overriddenConfig.live.controls.thumbnails.mode).toBe('right');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw on invalid schema', () => {
|
||||||
|
const config = createConfig({
|
||||||
|
overrides: [
|
||||||
|
{
|
||||||
|
conditions: [
|
||||||
|
{
|
||||||
|
condition: 'fullscreen' as const,
|
||||||
|
fullscreen: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
config.overrides![0].merge = 6 as unknown as Record<string, unknown>;
|
||||||
|
|
||||||
|
const stateManager = new ConditionStateManager();
|
||||||
|
stateManager.setState({ fullscreen: true });
|
||||||
|
|
||||||
|
const manager = new OverridesManager(vi.fn());
|
||||||
|
manager.set(stateManager, config.overrides);
|
||||||
|
|
||||||
|
expect(() => manager.getConfig(config)).toThrowError(
|
||||||
|
/Invalid override configuration/,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
// See: https://github.com/dermotduffy/advanced-camera-card/issues/1954
|
||||||
|
it('should handle overrides separately', () => {
|
||||||
|
const config = createConfig({
|
||||||
|
live: {
|
||||||
|
controls: {
|
||||||
|
thumbnails: {
|
||||||
|
mode: 'right',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
overrides: [
|
||||||
|
{
|
||||||
|
set: { 'live.controls.thumbnails.mode': 'left' },
|
||||||
|
conditions: [
|
||||||
|
{
|
||||||
|
condition: 'fullscreen' as const,
|
||||||
|
fullscreen: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
set: { 'live.controls.thumbnails.mode': 'none' },
|
||||||
|
conditions: [
|
||||||
|
{
|
||||||
|
condition: 'expand' as const,
|
||||||
|
expand: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const stateManager = new ConditionStateManager();
|
||||||
|
const manager = new OverridesManager(vi.fn());
|
||||||
|
manager.set(stateManager, config.overrides);
|
||||||
|
|
||||||
|
expect(manager.getConfig(config).live.controls.thumbnails.mode).toBe('right');
|
||||||
|
|
||||||
|
stateManager.setState({ fullscreen: true });
|
||||||
|
expect(manager.getConfig(config).live.controls.thumbnails.mode).toBe('left');
|
||||||
|
|
||||||
|
stateManager.setState({ expand: true });
|
||||||
|
expect(manager.getConfig(config).live.controls.thumbnails.mode).toBe('none');
|
||||||
|
|
||||||
|
stateManager.setState({ fullscreen: false });
|
||||||
|
expect(manager.getConfig(config).live.controls.thumbnails.mode).toBe('none');
|
||||||
|
|
||||||
|
stateManager.setState({ expand: false });
|
||||||
|
expect(manager.getConfig(config).live.controls.thumbnails.mode).toBe('right');
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user