@@ -1,7 +1,11 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { renderTemplate } from 'ha-nunjucks';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { MediaMatcher } from '../../../../src/card-controller/folders/ha/media-matcher';
|
||||
import { Matcher } from '../../../../src/config/schema/folders';
|
||||
import { BrowseMedia } from '../../../../src/ha/browse-media/types';
|
||||
import { createHASS } from '../../../test-utils';
|
||||
|
||||
vi.mock('ha-nunjucks');
|
||||
|
||||
describe('MediaMatcher', () => {
|
||||
describe('match', () => {
|
||||
@@ -22,25 +26,31 @@ describe('MediaMatcher', () => {
|
||||
it('should return false if foldersOnly is true and media.can_expand is false', () => {
|
||||
const mediaMatcher = new MediaMatcher();
|
||||
const media = createMediaItem('Test File', false);
|
||||
expect(mediaMatcher.match(media, [], true)).toBe(false);
|
||||
expect(
|
||||
mediaMatcher.match(createHASS(), media, { matchers: [], foldersOnly: true }),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it('should return true if foldersOnly is true and media.can_expand is true', () => {
|
||||
const mediaMatcher = new MediaMatcher();
|
||||
const media = createMediaItem('Test Folder', true, 'directory');
|
||||
expect(mediaMatcher.match(media, [], true)).toBe(true);
|
||||
expect(
|
||||
mediaMatcher.match(createHASS(), media, { matchers: [], foldersOnly: true }),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('should return true if matchers array is empty', () => {
|
||||
const mediaMatcher = new MediaMatcher();
|
||||
const media = createMediaItem('Test Media', false);
|
||||
expect(mediaMatcher.match(media, [])).toBe(true);
|
||||
expect(mediaMatcher.match(createHASS(), media, { matchers: [] })).toBe(true);
|
||||
});
|
||||
|
||||
it('should return true if matchers array is undefined', () => {
|
||||
const mediaMatcher = new MediaMatcher();
|
||||
const media = createMediaItem('Test Media', false);
|
||||
expect(mediaMatcher.match(media, undefined)).toBe(true);
|
||||
expect(mediaMatcher.match(createHASS(), media, { matchers: undefined })).toBe(
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
describe('with title matcher', () => {
|
||||
@@ -48,14 +58,14 @@ describe('MediaMatcher', () => {
|
||||
const mediaMatcher = new MediaMatcher();
|
||||
const media = createMediaItem('Exact Title', false);
|
||||
const matchers: Matcher[] = [{ type: 'title', title: 'Exact Title' }];
|
||||
expect(mediaMatcher.match(media, matchers)).toBe(true);
|
||||
expect(mediaMatcher.match(createHASS(), media, { matchers })).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false when title does not match exactly', () => {
|
||||
const mediaMatcher = new MediaMatcher();
|
||||
const media = createMediaItem('DOES NOT MATCH', false);
|
||||
const matchers: Matcher[] = [{ type: 'title', title: 'Exact Title' }];
|
||||
expect(mediaMatcher.match(media, matchers)).toBe(false);
|
||||
expect(mediaMatcher.match(createHASS(), media, { matchers })).toBe(false);
|
||||
});
|
||||
|
||||
it('should return true when title matches regexp and extracted value matches matcher.title', () => {
|
||||
@@ -68,7 +78,7 @@ describe('MediaMatcher', () => {
|
||||
title: 'ImportantPart',
|
||||
},
|
||||
];
|
||||
expect(mediaMatcher.match(media, matchers)).toBe(true);
|
||||
expect(mediaMatcher.match(createHASS(), media, { matchers })).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false when title matches regexp but extracted value does not match matcher.title', () => {
|
||||
@@ -81,7 +91,7 @@ describe('MediaMatcher', () => {
|
||||
title: 'WrongPart',
|
||||
},
|
||||
];
|
||||
expect(mediaMatcher.match(media, matchers)).toBe(false);
|
||||
expect(mediaMatcher.match(createHASS(), media, { matchers })).toBe(false);
|
||||
});
|
||||
|
||||
it('should return true when title matches regexp with an explicit title value', () => {
|
||||
@@ -94,7 +104,7 @@ describe('MediaMatcher', () => {
|
||||
// title is undefined.
|
||||
},
|
||||
];
|
||||
expect(mediaMatcher.match(media, matchers)).toBe(true);
|
||||
expect(mediaMatcher.match(createHASS(), media, { matchers })).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false when title does not match regexp', () => {
|
||||
@@ -107,7 +117,7 @@ describe('MediaMatcher', () => {
|
||||
title: 'ImportantPart',
|
||||
},
|
||||
];
|
||||
expect(mediaMatcher.match(media, matchers)).toBe(false);
|
||||
expect(mediaMatcher.match(createHASS(), media, { matchers })).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false when regexp is provided but does not extract the required group', () => {
|
||||
@@ -120,14 +130,120 @@ describe('MediaMatcher', () => {
|
||||
title: 'ImportantPart',
|
||||
},
|
||||
];
|
||||
expect(mediaMatcher.match(media, matchers)).toBe(false);
|
||||
expect(mediaMatcher.match(createHASS(), media, { matchers })).toBe(false);
|
||||
});
|
||||
|
||||
it('should return true when no regexp and no matcher.title (matches any title)', () => {
|
||||
const mediaMatcher = new MediaMatcher();
|
||||
const media = createMediaItem('Any Title Will Do', false);
|
||||
const matchers: Matcher[] = [{ type: 'title' }];
|
||||
expect(mediaMatcher.match(media, matchers)).toBe(true);
|
||||
expect(mediaMatcher.match(createHASS(), media, { matchers })).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with template matcher', () => {
|
||||
it('should return true when template value matches', () => {
|
||||
const mediaMatcher = new MediaMatcher();
|
||||
const title = 'Any Title Will Do';
|
||||
const media = createMediaItem(title, false);
|
||||
|
||||
vi.mocked(renderTemplate).mockReturnValue(true);
|
||||
|
||||
const matchers: Matcher[] = [
|
||||
{
|
||||
type: 'template',
|
||||
value_template: '{{ acc.media.title == "Any Title Will Do" }}',
|
||||
},
|
||||
];
|
||||
const hass = createHASS();
|
||||
expect(mediaMatcher.match(hass, media, { matchers })).toBe(true);
|
||||
|
||||
expect(renderTemplate).toHaveBeenCalledWith(
|
||||
hass,
|
||||
'{{ acc.media.title == "Any Title Will Do" }}',
|
||||
{
|
||||
acc: {
|
||||
media: {
|
||||
title,
|
||||
is_folder: false,
|
||||
},
|
||||
},
|
||||
advanced_camera_card: {
|
||||
media: {
|
||||
title,
|
||||
is_folder: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it('should return false when template value does not match', () => {
|
||||
const mediaMatcher = new MediaMatcher();
|
||||
const title = 'Any Title Will Do';
|
||||
const media = createMediaItem(title, false);
|
||||
|
||||
vi.mocked(renderTemplate).mockReturnValue(false);
|
||||
|
||||
const matchers: Matcher[] = [
|
||||
{
|
||||
type: 'template',
|
||||
value_template: '{{ acc.media.title == "Any Title Will Do" }}',
|
||||
},
|
||||
];
|
||||
const hass = createHASS();
|
||||
expect(mediaMatcher.match(hass, media, { matchers })).toBe(false);
|
||||
|
||||
expect(renderTemplate).toHaveBeenCalledWith(
|
||||
hass,
|
||||
'{{ acc.media.title == "Any Title Will Do" }}',
|
||||
{
|
||||
acc: {
|
||||
media: {
|
||||
title,
|
||||
is_folder: false,
|
||||
},
|
||||
},
|
||||
advanced_camera_card: {
|
||||
media: {
|
||||
title,
|
||||
is_folder: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with or matcher', () => {
|
||||
it('should return true if at least one sub-matcher matches', () => {
|
||||
const mediaMatcher = new MediaMatcher();
|
||||
const media = createMediaItem('Test Media', false);
|
||||
const matcher: Matcher = {
|
||||
type: 'or',
|
||||
matchers: [
|
||||
{ type: 'title', title: 'Non-Matching Title' }, // Fails
|
||||
{ type: 'title', title: 'Test Media' }, // Passes
|
||||
],
|
||||
};
|
||||
expect(mediaMatcher.match(createHASS(), media, { matchers: [matcher] })).toBe(
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
it('should return false if no sub-matcher matches', () => {
|
||||
const mediaMatcher = new MediaMatcher();
|
||||
const media = createMediaItem('Test Media', false);
|
||||
const matcher: Matcher = {
|
||||
type: 'or',
|
||||
matchers: [
|
||||
{ type: 'title', title: 'Non-Matching Title One' }, // Fails
|
||||
{ type: 'title', title: 'Non-Matching Title Two' }, // Fails
|
||||
],
|
||||
};
|
||||
expect(mediaMatcher.match(createHASS(), media, { matchers: [matcher] })).toBe(
|
||||
false,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -138,7 +254,7 @@ describe('MediaMatcher', () => {
|
||||
{ type: 'title', title: 'Test Media One' }, // Pass
|
||||
{ type: 'title', title: 'Test Media Two' }, // Fail
|
||||
];
|
||||
expect(mediaMatcher.match(media, matchers)).toBe(false);
|
||||
expect(mediaMatcher.match(createHASS(), media, { matchers })).toBe(false);
|
||||
});
|
||||
|
||||
it('should return true if all multiple matchers pass', () => {
|
||||
@@ -152,7 +268,7 @@ describe('MediaMatcher', () => {
|
||||
title: 'Test Media One',
|
||||
},
|
||||
];
|
||||
expect(mediaMatcher.match(media, matchers)).toBe(true);
|
||||
expect(mediaMatcher.match(createHASS(), media, { matchers })).toBe(true);
|
||||
});
|
||||
|
||||
it('should ignore matchers of unknown types', () => {
|
||||
@@ -160,7 +276,7 @@ describe('MediaMatcher', () => {
|
||||
const media = createMediaItem('Test Media', false);
|
||||
|
||||
const matchers: Matcher[] = [{ type: 'unknownMatcherType' as 'title' }];
|
||||
expect(mediaMatcher.match(media, matchers)).toBe(true);
|
||||
expect(mediaMatcher.match(createHASS(), media, { matchers })).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user