@@ -1023,5 +1023,75 @@ describe('ConditionsManager', () => {
|
||||
expect(manager.evaluateConditions(conditions)).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('with user agent condition', () => {
|
||||
const userAgent =
|
||||
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36';
|
||||
|
||||
it('should match exact user agent', () => {
|
||||
const manager = new ConditionsManager(createCardAPI());
|
||||
const conditions = [{ condition: 'user_agent' as const, user_agent: userAgent }];
|
||||
expect(manager.evaluateConditions(conditions)).toBeFalsy();
|
||||
manager.setState({
|
||||
user_agent: userAgent,
|
||||
});
|
||||
expect(manager.evaluateConditions(conditions)).toBeTruthy();
|
||||
manager.setState({
|
||||
user_agent: 'Something else',
|
||||
});
|
||||
expect(manager.evaluateConditions(conditions)).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match user agent regex', () => {
|
||||
const manager = new ConditionsManager(createCardAPI());
|
||||
const conditions = [
|
||||
{ condition: 'user_agent' as const, user_agent_re: 'Chrome/' },
|
||||
];
|
||||
expect(manager.evaluateConditions(conditions)).toBeFalsy();
|
||||
manager.setState({
|
||||
user_agent: userAgent,
|
||||
});
|
||||
expect(manager.evaluateConditions(conditions)).toBeTruthy();
|
||||
manager.setState({
|
||||
user_agent: 'Something else',
|
||||
});
|
||||
expect(manager.evaluateConditions(conditions)).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match companion app', () => {
|
||||
const manager = new ConditionsManager(createCardAPI());
|
||||
const conditions = [{ condition: 'user_agent' as const, companion: true }];
|
||||
expect(manager.evaluateConditions(conditions)).toBeFalsy();
|
||||
manager.setState({
|
||||
user_agent: 'Home Assistant/',
|
||||
});
|
||||
expect(manager.evaluateConditions(conditions)).toBeTruthy();
|
||||
manager.setState({
|
||||
user_agent: userAgent,
|
||||
});
|
||||
expect(manager.evaluateConditions(conditions)).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match multiple parameters', () => {
|
||||
const manager = new ConditionsManager(createCardAPI());
|
||||
const conditions = [
|
||||
{
|
||||
condition: 'user_agent' as const,
|
||||
companion: true,
|
||||
user_agent: 'Home Assistant/',
|
||||
user_agent_re: 'Home.Assistant',
|
||||
},
|
||||
];
|
||||
expect(manager.evaluateConditions(conditions)).toBeFalsy();
|
||||
manager.setState({
|
||||
user_agent: 'Home Assistant/',
|
||||
});
|
||||
expect(manager.evaluateConditions(conditions)).toBeTruthy();
|
||||
manager.setState({
|
||||
user_agent: 'Something else',
|
||||
});
|
||||
expect(manager.evaluateConditions(conditions)).toBeFalsy();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
conditionalSchema,
|
||||
customSchema,
|
||||
dimensionsConfigSchema,
|
||||
frigateCardConditionSchema,
|
||||
frigateCardCustomActionsBaseSchema,
|
||||
frigateCardCustomActionSchema,
|
||||
} from '../../src/config/types';
|
||||
@@ -401,6 +402,29 @@ it('should transform dimensions.aspect_ratio', () => {
|
||||
).toEqual(expect.objectContaining({ aspect_ratio: [16, 9] }));
|
||||
});
|
||||
|
||||
describe('should refine user_agent_re conditions', () => {
|
||||
it('should successfully parse valid user_agent_re condition', () => {
|
||||
expect(
|
||||
frigateCardConditionSchema.parse({
|
||||
condition: 'user_agent',
|
||||
user_agent_re: 'Chrome/',
|
||||
}),
|
||||
).toEqual({
|
||||
condition: 'user_agent',
|
||||
user_agent_re: 'Chrome/',
|
||||
});
|
||||
});
|
||||
|
||||
it('should reject invalid user_agent_re conditions', () => {
|
||||
expect(() =>
|
||||
frigateCardConditionSchema.parse({
|
||||
condition: 'user_agent',
|
||||
user_agent_re: '[',
|
||||
}),
|
||||
).toThrowError(/Invalid regular expression/);
|
||||
});
|
||||
});
|
||||
|
||||
it('should transform action', () => {
|
||||
expect(
|
||||
frigateCardCustomActionsBaseSchema.parse({
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { isCompanionApp } from '../../src/utils/companion';
|
||||
|
||||
describe('isCompanionApp', () => {
|
||||
it('should return true for userAgent starting with "Home Assistant/"', () => {
|
||||
expect(isCompanionApp('Home Assistant/1.0')).toBe(true);
|
||||
});
|
||||
|
||||
it('should return true for userAgent starting with "HomeAssistant/"', () => {
|
||||
expect(isCompanionApp('HomeAssistant/1.0')).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false for userAgent not starting with "Home Assistant/" or "HomeAssistant/"', () => {
|
||||
expect(isCompanionApp('Mozilla/5.0')).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for an empty userAgent', () => {
|
||||
expect(isCompanionApp('')).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user