Files
advanced-camera-card/tests/components-lib/timeline/source.test.ts
T
Dermot DuffyandClaude Opus 4.8 b701366762 feat: Align automations with Home Assistant triggers and conditions (#2527)
Split automations into HA-style `triggers`, ongoing `conditions`, and
`actions`, with compatibility migrations for existing Advanced Camera
Card configs.

## Summary

At a glance (details below):

- **Added** `triggers:` -- a required, HA-shaped block: stock `state` /
`numeric_state` / `template` plus card-specific triggers (`camera`,
`view`, `fullscreen`, ...).
- **Added** the HA-native `if` / `then` / `else` action.
- **Removed** `actions_not` (replaced by `if` / `then` / `else`).
- **Removed** the ambient `advanced_camera_card` template namespace (use
`acc` instead).
- **Changed** the trigger template surface to a top-level `trigger.*`
variable (as in HA); the nested `acc.trigger.*` paths are removed.
- **Changed** `conditions:` to ongoing gates only -- they no longer wake
an automation, and change-only forms (`config`, valueless `camera` /
`view` / `state`) become triggers, not conditions.
- **Changed** action templates to render per step, so a later action
sees state an earlier one changed.
- **Compatibility:** HA-shaped YAML is accepted (singular keys,
single-or-list, `and` / `or` / `not` shorthand, `entity` / `entity_id`).
- **Migration:** existing configs upgrade automatically; anything that
cannot be converted faithfully is recorded under `__UPGRADE_FAILURE__`
for manual fixup.

## Breaking Changes

### 1. Automations now require triggers

Before this PR, `automations[].conditions` served two roles:

- They decided whether the automation should run.
- They also acted as the thing that woke the automation up.

After this PR:

- `triggers` wake the automation.
- `conditions` only gate it at the instant a trigger fires.

Most existing automations are migrated automatically from `conditions:`
to `triggers:`.

### 2. `actions_not` is retired

Legacy `actions_not` is replaced by an HA-style `if` action with `then`
/ `else`.

Faithful conversions are automatic. Cases that cannot be faithfully
converted are recorded under `__UPGRADE_FAILURE__.automations` and must
be migrated manually.

### 3. Template surface aligned with Home Assistant

Two related template changes, both auto-migrated:

- **Top-level `trigger.*`.** Automation actions now receive a top-level
`trigger` template variable, like Home Assistant. Legacy nested paths
such as `acc.trigger.state.to` and
`advanced_camera_card.trigger.camera.to` are migrated automatically when
they appear inside template strings.
- **The ambient `advanced_camera_card` template namespace is removed.**
The long-form ambient namespace (`advanced_camera_card.camera`,
`advanced_camera_card.view`, `advanced_camera_card.config`) is retired
in favour of its shorter `acc` alias -- supported since v7.1.0, and the
only spelling the new trigger surface uses. Existing templates are
migrated automatically by rewriting the `advanced_camera_card.` prefix
to `acc.`.

### 4. Trigger-only condition forms are no longer valid conditions

Some legacy "conditions" were really change detectors. These are now
triggers only:

- `condition: config`
- valueless `camera`
- valueless `view`
- valueless `state` / picture-elements state condition with neither
`state` nor `state_not`

These are automatically promoted in automations and stripped from
overrides/elements where they would no longer be meaningful as ongoing
conditions.

### 5. Template truthiness now follows Home Assistant behavior

Template conditions and template triggers intentionally use different
truthiness rules, matching HA:

- A template condition passes only when the rendered value is `true`
(case-insensitive), matching HA's `condition.py`.
- A template trigger uses HA's broader `result_as_boolean` coercion: a
non-zero number, or `1` / `true` / `yes` / `on` / `enable`
(case-insensitive), counts as true.

### 6. Action templates render when each action executes

Action templates are now rendered per action step, not once for the
whole sequence. This means a later action can see card-local state
changed by an earlier action in the same sequence.

The `trigger` context is fixed for the automation run. HA entity state
updates still depend on the frontend receiving updated HASS state over
the websocket.

## Automatic Migrations

### Automation `conditions:` to `triggers:`

Simple legacy automation:

```yaml
# Before
automations:
  - conditions:
      - condition: fullscreen
        fullscreen: true
    actions:
      - action: custom:advanced-camera-card-action
        advanced_camera_card_action: substream_on
```

```yaml
# After, automatic
automations:
  - triggers:
      - trigger: fullscreen
        fullscreen: true
    actions:
      - action: custom:advanced-camera-card-action
        advanced_camera_card_action: substream_on
```

State conditions become HA-style state triggers:

```yaml
# Before
automations:
  - conditions:
      - condition: state
        entity_id: binary_sensor.front_door
        state: 'on'
    actions:
      - action: custom:advanced-camera-card-action
        advanced_camera_card_action: live
```

```yaml
# After, automatic
automations:
  - triggers:
      - trigger: state
        entity_id: binary_sensor.front_door
        to: 'on'
    actions:
      - action: custom:advanced-camera-card-action
        advanced_camera_card_action: live
```

Multiple conditions become both triggers and ongoing conditions:

```yaml
# Before
automations:
  - conditions:
      - condition: camera
        cameras: [front_door]
      - condition: fullscreen
        fullscreen: true
    actions:
      - action: custom:advanced-camera-card-action
        advanced_camera_card_action: substream_on
```

```yaml
# After, automatic
automations:
  - triggers:
      - trigger: camera
        cameras: [front_door]
      - trigger: fullscreen
        fullscreen: true
    conditions:
      - condition: camera
        cameras: [front_door]
      - condition: fullscreen
        fullscreen: true
    actions:
      - action: custom:advanced-camera-card-action
        advanced_camera_card_action: substream_on
```

The flattened trigger list is an implicit OR. The retained `conditions:`
list is an implicit AND checked when any trigger fires.

### Trigger-only legacy conditions

Legacy `config` conditions become `config` triggers:

```yaml
# Before
automations:
  - conditions:
      - condition: config
        paths: [menu.style]
    actions:
      - action: custom:advanced-camera-card-action
        advanced_camera_card_action: status_bar
```

```yaml
# After, automatic
automations:
  - triggers:
      - trigger: config
        paths: [menu.style]
    actions:
      - action: custom:advanced-camera-card-action
        advanced_camera_card_action: status_bar
```

Trigger-only leaves are removed from retained `conditions:` blocks
because they no longer describe an ongoing state.

### `actions_not` to `if` / `then` / `else`

```yaml
# Before
automations:
  - conditions:
      - condition: state
        entity_id: input_boolean.camera_alerts
        state: 'on'
    actions:
      - action: custom:advanced-camera-card-action
        advanced_camera_card_action: live
    actions_not:
      - action: none
```

```yaml
# After, automatic
automations:
  - triggers:
      - trigger: state
        entity_id: input_boolean.camera_alerts
    actions:
      - if:
          - condition: state
            entity_id: input_boolean.camera_alerts
            state: 'on'
        then:
          - action: custom:advanced-camera-card-action
            advanced_camera_card_action: live
        else:
          - action: none
```

If the legacy automation had no conditions, or only trigger-only
conditions, `actions_not` is dropped because the old `else` branch could
not be reproduced as an ongoing predicate.

### Trigger template paths

```yaml
# Before
message: 'Door is {{ acc.trigger.state.to }} from {{ acc.trigger.state.from }}'
```

```yaml
# After, automatic
message: 'Door is {{ trigger.to_state.state }} from {{ trigger.from_state.state }}'
```

Path rewrites performed automatically:

| Old path                   | New path                   |
| -------------------------- | -------------------------- |
| `acc.trigger.state.entity` | `trigger.entity_id`        |
| `acc.trigger.state.from`   | `trigger.from_state.state` |
| `acc.trigger.state.to`     | `trigger.to_state.state`   |
| `acc.trigger.camera.from`  | `trigger.from_acc.camera`  |
| `acc.trigger.camera.to`    | `trigger.to_acc.camera`    |
| `acc.trigger.view.from`    | `trigger.from_acc.view`    |
| `acc.trigger.view.to`      | `trigger.to_acc.view`      |
| `acc.trigger.config.from`  | `trigger.from_acc.config`  |
| `acc.trigger.config.to`    | `trigger.to_acc.config`    |

The same rewrites are applied for the older
`advanced_camera_card.trigger.*` namespace.

### Ambient template namespace

Any remaining long-form ambient `advanced_camera_card.*` references
(outside the trigger surface) are rewritten to the `acc.*` alias:

```yaml
# Before
title: 'Now viewing {{ advanced_camera_card.camera }}'
```

```yaml
# After, automatic
title: 'Now viewing {{ acc.camera }}'
```

## Manual Migration Cases

### `__UPGRADE_FAILURE__.automations`

If a legacy automation cannot be converted faithfully, the original
automation is recorded under:

```yaml
__UPGRADE_FAILURE__:
  automations:
    - ...
```

These entries require manual migration.

The main known case is legacy `actions_not` with a condition whose
trigger can only fire on a rising edge, such as:

- `condition: template`
- `condition: screen`
- `condition: numeric_state` without an entity-backed state to watch

Those conditions can start the `then` branch, but cannot reliably start
the `else` branch when they stop matching.

### Unsupported HA conditions and triggers

This PR aligns the card with HA where supported, but it is not a full HA
automation engine.

Unsupported HA condition families include:

- `time`
- `zone`
- `sun`
- `location`
- `device`
- `condition: trigger`

Unsupported HA trigger platforms include:

- `event`
- `time`
- `time_pattern`
- `sun`
- `zone`
- `calendar`
- `webhook`
- `tag`
- `device`
- `mqtt`

The card-specific camera `triggers:` feature (which auto-selects and
wakes the card on camera events such as motion) is a separate feature
from automation `triggers:`, despite the shared word.

### Trigger IDs and variables

HA keys such as `id`, `alias`, and `variables` are accepted so pasted HA
YAML validates, but they are ignored by the card. There is no
`trigger.id` support in this PR.

## New Compatibility Features

This PR also makes card config more forgiving for HA-style YAML:

- `trigger`, `condition`, and `action` singular keys are accepted and
normalized to `triggers`, `conditions`, and `actions`.
- Single trigger, condition, and action objects are accepted where lists
are expected.
- `if`, `then`, and `else` accept a single item or a list.
- Composite condition shorthand is accepted:
  - `{ and: [...] }`
  - `{ or: [...] }`
  - `{ not: [...] }`
  - `{ condition: [...] }` as an implicit AND
- State conditions resolve expected state values that name another
entity, matching HA/Lovelace behavior.
- Both `entity` and `entity_id` are accepted on state and numeric
conditions and triggers (a superset of HA's two dialects), so there is
no forced rename.
- `state_not` remains supported as a card/Lovelace-friendly extension.

## Trigger Payloads

Automation action templates receive a top-level `trigger` object.

For stock `state` and `numeric_state` triggers:

```yaml
trigger.platform
trigger.entity_id
trigger.entity
trigger.from_state
trigger.to_state
```

For template triggers:

```yaml
trigger.platform
```

For card-specific triggers:

```yaml
trigger.platform # "acc"
trigger.type
trigger.from_acc
trigger.to_acc
```

The card does not currently expose HA's `id`, `idx`, `for`, `attribute`,
`above`, `below`, or `alias` trigger fields.

BREAKING CHANGE: Automations now follow Home Assistant's `triggers:` /
`conditions:` / `actions:` model. Automations require a `triggers:`
block and `conditions:` no longer wake an automation; `actions_not` is
removed in favour of an `if` / `then` / `else` action; the nested
`acc.trigger.*` template paths and the ambient `advanced_camera_card`
template namespace are removed (use the top-level `trigger.*` surface
and the `acc` alias); trigger-only condition forms (`config`, valueless
`camera` / `view` / `state`) are no longer valid conditions; and
template-condition vs template-trigger truthiness now follow HA.
Existing configs are upgraded automatically where a faithful conversion
exists; anything that cannot be converted is recorded under
`__UPGRADE_FAILURE__` for manual migration.

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-30 17:45:13 -07:00

1264 lines
37 KiB
TypeScript

import { add } from 'date-fns';
import { NonEmptyTuple } from 'type-fest';
import { DataSet } from 'vis-data';
import { TimelineWindow } from 'vis-timeline';
import {
afterAll,
assert,
beforeAll,
beforeEach,
describe,
expect,
it,
vi,
} from 'vitest';
import { mock } from 'vitest-mock-extended';
import { CameraManager } from '../../../src/camera-manager/manager';
import {
Engine,
EventQuery,
QueryResultsType,
QueryType,
RecordingSegment,
RecordingSegmentsQuery,
RecordingSegmentsQueryResults,
ReviewQuery,
} from '../../../src/camera-manager/types';
import { FoldersManager } from '../../../src/card-controller/folders/manager';
import {
FolderPathComponent,
FolderQuery,
} from '../../../src/card-controller/folders/types';
import {
AdvancedCameraCardTimelineItem,
TimelineDataSource,
} from '../../../src/components-lib/timeline/source';
import { ConditionStateManagerReadonlyInterface } from '../../../src/condition-trigger/conditions/types';
import { QuerySource } from '../../../src/query-source';
import { ViewMediaType } from '../../../src/view/item';
import { QueryNode, UnifiedQuery } from '../../../src/view/unified-query';
import {
createCameraManager,
createFolder,
createStore,
TestViewMedia,
} from '../../test-utils';
const CAMERA_ID = 'camera-1';
const TEST_MEDIA_ID = 'TEST_MEDIA_ID';
const RECORDING_SEGMENT_ID = 'SEGMENT_ID';
const EXPECTED_RECORDING_ID = `recording-${CAMERA_ID}-${RECORDING_SEGMENT_ID}`;
const start = new Date('2025-09-21T19:31:06Z');
const end = new Date('2025-09-21T19:31:15Z');
const testCameraMedia = new TestViewMedia({
cameraID: CAMERA_ID,
id: TEST_MEDIA_ID,
startTime: start,
endTime: end,
});
const folder = createFolder({ id: 'folder-1', title: 'Folder Title' });
const testFolderMedia = new TestViewMedia({
folder,
id: TEST_MEDIA_ID,
startTime: start,
endTime: end,
});
const createTestCameraManager = (): CameraManager => {
const cameraManager = createCameraManager(
createStore([
{
cameraID: CAMERA_ID,
},
]),
);
vi.mocked(cameraManager.getCameraMetadata).mockReturnValue({
title: 'Camera Title',
icon: { icon: 'mdi:camera' },
});
const eventQuery: EventQuery = {
source: QuerySource.Camera,
type: QueryType.Event,
cameraIDs: new Set([CAMERA_ID]),
start: start,
end: end,
};
vi.mocked(cameraManager.generateDefaultEventQueries).mockReturnValue([eventQuery]);
vi.mocked(cameraManager.executeMediaQueries).mockResolvedValue([testCameraMedia]);
const recordingSegmentQuery: RecordingSegmentsQuery = {
type: QueryType.RecordingSegments,
cameraIDs: new Set([CAMERA_ID]),
start,
end,
};
vi.mocked(cameraManager.generateDefaultRecordingSegmentsQueries).mockReturnValue([
recordingSegmentQuery,
]);
const recordingSegment: RecordingSegment = {
start_time: 1695307866,
end_time: 1695307875,
id: RECORDING_SEGMENT_ID,
};
const recordingSegmentsQueryResults: RecordingSegmentsQueryResults = {
type: QueryResultsType.RecordingSegments,
engine: Engine.Generic,
segments: [recordingSegment],
};
vi.mocked(cameraManager.getRecordingSegments).mockResolvedValue(
new Map([[recordingSegmentQuery, recordingSegmentsQueryResults]]),
);
return cameraManager;
};
describe('TimelineDataSource', () => {
// Create camera-based query with events for two cameras
const cameraEventsQuery = new UnifiedQuery();
const eventQuery1: EventQuery = {
source: QuerySource.Camera,
type: QueryType.Event,
cameraIDs: new Set(['camera-1']),
hasClip: true,
};
cameraEventsQuery.addNode(eventQuery1);
const eventQuery2: EventQuery = {
source: QuerySource.Camera,
type: QueryType.Event,
cameraIDs: new Set(['camera-2']),
hasSnapshot: true,
};
cameraEventsQuery.addNode(eventQuery2);
// Create camera-based query with reviews for one camera
const reviewQuery = new UnifiedQuery();
const reviewQueryNode: ReviewQuery = {
source: QuerySource.Camera,
type: QueryType.Review,
cameraIDs: new Set(['camera-1']),
};
reviewQuery.addNode(reviewQueryNode);
// Create folder-based query
const folderQuery = new UnifiedQuery();
const folderPath: NonEmptyTuple<FolderPathComponent> = [{}];
const folderQueryNode: FolderQuery = {
source: QuerySource.Folder,
folder: folder,
path: folderPath,
};
folderQuery.addNode(folderQueryNode);
// Helper to create a TimelineDataSource with a shape query
const createSource = (
cameraManager: CameraManager,
foldersManager: FoldersManager,
conditionStateManager: ConditionStateManagerReadonlyInterface,
shape: UnifiedQuery,
showRecordings = true,
): TimelineDataSource => {
return new TimelineDataSource(
cameraManager,
foldersManager,
conditionStateManager,
shape,
showRecordings,
);
};
beforeEach(() => {
vi.clearAllMocks();
});
describe('should get groups', () => {
it('should get camera based groups', () => {
const source = createSource(
createTestCameraManager(),
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
cameraEventsQuery,
true,
);
expect(source.groups.length).toBe(2);
expect(source.groups.get('camera/camera-1')).toEqual({
content: 'Camera Title',
id: 'camera/camera-1',
});
expect(source.groups.get('camera/camera-2')).toEqual({
content: 'Camera Title',
id: 'camera/camera-2',
});
});
it('should get folder based groups', () => {
const source = createSource(
createTestCameraManager(),
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
folderQuery,
true,
);
expect(source.groups.length).toBe(1);
expect(source.groups.get('folder/folder-1')).toEqual({
content: 'Folder Title',
id: 'folder/folder-1',
});
});
it('should use camera id if camera has no title', () => {
const cameraManager = createTestCameraManager();
vi.mocked(cameraManager.getCameraMetadata).mockReturnValue(null);
const source = createSource(
cameraManager,
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
cameraEventsQuery,
true,
);
expect(source.groups.get('camera/camera-1')).toEqual({
content: 'camera-1',
id: 'camera/camera-1',
});
});
it('should use folder id if folder has no title', () => {
const folder = createFolder({ id: 'folder-1' });
const testQuery = new UnifiedQuery();
const folderPath: NonEmptyTuple<FolderPathComponent> = [{}];
testQuery.addNode({
source: QuerySource.Folder,
folder: folder,
path: folderPath,
});
const source = createSource(
createTestCameraManager(),
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
testQuery,
true,
);
expect(source.groups.length).toBe(1);
expect(source.groups.get('folder/folder-1')).toEqual({
content: 'folder/folder-1',
id: 'folder/folder-1',
});
});
});
describe('should update events from view', () => {
it('should add camera events to dataset', () => {
const startTime = new Date('2025-09-21T15:32:21Z');
const endTime = new Date('2025-09-21T15:35:28Z');
const id = 'EVENT_ID';
const media = new TestViewMedia({
cameraID: 'camera-1',
id,
startTime,
endTime,
});
const source = createSource(
createTestCameraManager(),
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
cameraEventsQuery,
true,
);
source.addMediaToDataset(cameraEventsQuery, [media]);
expect(source.dataset.length).toBe(1);
expect(source.dataset.get(id)).toEqual({
id,
start: startTime.getTime(),
end: endTime.getTime(),
media,
group: 'camera/camera-1',
content: '',
type: 'range',
query: cameraEventsQuery,
});
});
it('should add review media with severity to dataset', () => {
const startTime = new Date('2025-09-21T15:32:21Z');
const endTime = new Date('2025-09-21T15:35:28Z');
const id = 'REVIEW_ID';
const media = new TestViewMedia({
cameraID: 'camera-1',
id,
startTime,
endTime,
mediaType: ViewMediaType.Review,
severity: 'high',
});
const source = createSource(
createTestCameraManager(),
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
reviewQuery,
false,
);
source.addMediaToDataset(reviewQuery, [media]);
expect(source.dataset.length).toBe(1);
expect(source.dataset.get(id)).toEqual({
id,
start: startTime.getTime(),
end: endTime.getTime(),
media,
group: 'camera/camera-1',
content: '',
type: 'range',
query: reviewQuery,
severity: 'high',
});
});
it('should add review media with null severity to dataset', () => {
const startTime = new Date('2025-09-21T15:32:21Z');
const endTime = new Date('2025-09-21T15:35:28Z');
const id = 'REVIEW_ID';
const media = new TestViewMedia({
cameraID: 'camera-1',
id,
startTime,
endTime,
mediaType: ViewMediaType.Review,
severity: null,
});
const source = createSource(
createTestCameraManager(),
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
reviewQuery,
false,
);
source.addMediaToDataset(reviewQuery, [media]);
expect(source.dataset.length).toBe(1);
expect(source.dataset.get(id)).toEqual({
id,
start: startTime.getTime(),
end: endTime.getTime(),
media,
group: 'camera/camera-1',
content: '',
type: 'range',
query: reviewQuery,
severity: undefined,
});
});
it('should add folder events to dataset', () => {
const startTime = new Date('2025-09-21T15:32:21Z');
const endTime = new Date('2025-09-21T15:35:28Z');
const id = 'EVENT_ID';
const folderID = 'FOLDER_ID';
const folder = createFolder({ id: folderID });
const media = new TestViewMedia({
cameraID: null,
id,
startTime,
endTime,
folder,
});
const source = createSource(
createTestCameraManager(),
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
folderQuery,
true,
);
source.addMediaToDataset(folderQuery, [media]);
expect(source.dataset.length).toBe(1);
expect(source.dataset.get(id)).toEqual({
id,
start: startTime.getTime(),
end: endTime.getTime(),
media,
group: `folder/${folderID}`,
content: '',
type: 'range',
query: folderQuery,
});
});
it('should ignore non-events media', () => {
const source = createSource(
createTestCameraManager(),
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
cameraEventsQuery,
true,
);
source.addMediaToDataset(cameraEventsQuery, [
new TestViewMedia({
mediaType: ViewMediaType.Recording,
}),
]);
expect(source.dataset.length).toBe(0);
});
it('should ignore null results', () => {
const source = createSource(
createTestCameraManager(),
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
cameraEventsQuery,
true,
);
source.addMediaToDataset(cameraEventsQuery, null);
expect(source.dataset.length).toBe(0);
});
it('should ignore media without camera or folder ownership', () => {
const source = createSource(
createTestCameraManager(),
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
cameraEventsQuery,
true,
);
source.addMediaToDataset(cameraEventsQuery, [
new TestViewMedia({
cameraID: null,
folder: null,
mediaType: ViewMediaType.Snapshot,
}),
]);
expect(source.dataset.length).toBe(0);
});
});
describe('should refresh', () => {
const window: TimelineWindow = {
start: new Date('2025-09-21T19:31:06Z'),
end: new Date('2025-09-21T19:31:15Z'),
};
describe('should refresh events', () => {
beforeEach(() => {
vi.restoreAllMocks();
});
describe('should refresh events from camera', () => {
it('should refresh events successfully', async () => {
const source = createSource(
createTestCameraManager(),
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
cameraEventsQuery,
false,
);
await source.refresh(window);
expect(source.dataset.length).toBe(1);
expect(source.dataset.get('TEST_MEDIA_ID')).toEqual({
id: 'TEST_MEDIA_ID',
content: '',
start: new Date('2025-09-21T19:31:06Z').getTime(),
end: new Date('2025-09-21T19:31:15Z').getTime(),
media: testCameraMedia,
type: 'range',
query: expect.any(UnifiedQuery),
group: 'camera/camera-1',
});
});
it('should refresh events and handle exception', async () => {
const consoleSpy = vi.spyOn(global.console, 'warn').mockReturnValue(undefined);
const cameraManager = createTestCameraManager();
vi.mocked(cameraManager.executeMediaQueries).mockRejectedValue(
new Error('Error fetching events'),
);
const source = createSource(
cameraManager,
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
cameraEventsQuery,
false,
);
expect(source.dataset.length).toBe(0);
await source.refresh(window);
expect(source.dataset.length).toBe(0);
expect(consoleSpy).toHaveBeenCalledWith('Error fetching events');
});
it('should not refresh events when window is cached', async () => {
const cameraManager = createTestCameraManager();
const source = createSource(
cameraManager,
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
cameraEventsQuery,
false,
);
await source.refresh(window);
expect(source.dataset.length).toBe(1);
await source.refresh(window);
expect(source.dataset.length).toBe(1);
expect(cameraManager.executeMediaQueries).toHaveBeenCalledTimes(1);
});
});
describe('should refresh events from folder', () => {
it('should refresh events successfully', async () => {
const foldersManager = mock<FoldersManager>();
vi.mocked(foldersManager.getDefaultQueryParameters).mockReturnValue({
source: QuerySource.Folder,
folder,
path: [{}],
});
vi.mocked(foldersManager.expandFolder).mockResolvedValue([testFolderMedia]);
const source = createSource(
mock<CameraManager>(),
foldersManager,
mock<ConditionStateManagerReadonlyInterface>(),
folderQuery,
false,
);
await source.refresh(window);
expect(source.dataset.length).toBe(1);
expect(source.dataset.get('TEST_MEDIA_ID')).toEqual({
id: 'TEST_MEDIA_ID',
content: '',
start: new Date('2025-09-21T19:31:06Z').getTime(),
end: new Date('2025-09-21T19:31:15Z').getTime(),
media: testFolderMedia,
type: 'range',
group: 'folder/folder-1',
query: expect.any(UnifiedQuery),
});
});
describe('should refresh events from folder cached', () => {
beforeAll(() => {
vi.useFakeTimers();
});
afterAll(() => {
vi.useRealTimers();
});
it('should refresh events only when not cached', async () => {
const foldersManager = mock<FoldersManager>();
vi.mocked(foldersManager.getDefaultQueryParameters).mockReturnValue({
source: QuerySource.Folder,
folder,
path: [{}],
});
vi.mocked(foldersManager.expandFolder).mockResolvedValue([testFolderMedia]);
const source = createSource(
mock<CameraManager>(),
foldersManager,
mock<ConditionStateManagerReadonlyInterface>(),
folderQuery,
false,
);
await source.refresh(window);
await source.refresh(window);
await source.refresh(window);
expect(foldersManager.expandFolder).toHaveBeenCalledTimes(1);
});
it('should refresh events when cached expired', async () => {
const start = new Date();
vi.setSystemTime(start);
const foldersManager = mock<FoldersManager>();
vi.mocked(foldersManager.getDefaultQueryParameters).mockReturnValue({
source: QuerySource.Folder,
folder,
path: [{}],
});
vi.mocked(foldersManager.expandFolder).mockResolvedValue([testFolderMedia]);
const source = createSource(
mock<CameraManager>(),
foldersManager,
mock<ConditionStateManagerReadonlyInterface>(),
folderQuery,
false,
);
await source.refresh(window);
vi.setSystemTime(add(start, { hours: 1 }));
await source.refresh(window);
expect(foldersManager.expandFolder).toHaveBeenCalledTimes(2);
});
});
});
});
describe('should refresh reviews', () => {
beforeEach(() => {
vi.restoreAllMocks();
});
it('should refresh reviews successfully', async () => {
const cameraManager = createTestCameraManager();
const cameraReviewQuery: ReviewQuery = {
source: QuerySource.Camera,
type: QueryType.Review,
cameraIDs: new Set([CAMERA_ID]),
start,
end,
};
vi.mocked(cameraManager.generateDefaultReviewQueries).mockReturnValue([
cameraReviewQuery,
]);
const source = createSource(
cameraManager,
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
reviewQuery,
false,
);
await source.refresh(window);
expect(cameraManager.executeMediaQueries).toHaveBeenCalled();
expect(source.dataset.length).toBe(1);
});
it('should not refresh reviews when window is cached', async () => {
const cameraManager = createTestCameraManager();
const cameraReviewQuery: ReviewQuery = {
source: QuerySource.Camera,
type: QueryType.Review,
cameraIDs: new Set([CAMERA_ID]),
start,
end,
};
vi.mocked(cameraManager.generateDefaultReviewQueries).mockReturnValue([
cameraReviewQuery,
]);
const source = createSource(
cameraManager,
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
reviewQuery,
false,
);
await source.refresh(window);
await source.refresh(window);
expect(cameraManager.executeMediaQueries).toHaveBeenCalledTimes(1);
});
it('should not refresh reviews without review queries', async () => {
const cameraManager = createTestCameraManager();
vi.mocked(cameraManager.generateDefaultReviewQueries).mockReturnValue(null);
const source = createSource(
cameraManager,
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
new UnifiedQuery(),
false,
);
await source.refresh(window);
expect(cameraManager.executeMediaQueries).not.toHaveBeenCalled();
expect(source.dataset.length).toBe(0);
});
});
describe('should refresh recordings', () => {
const getRecordings = (
dataset: DataSet<AdvancedCameraCardTimelineItem>,
): AdvancedCameraCardTimelineItem[] => {
return dataset.get({ filter: (item) => item.type === 'background' });
};
it('should refresh recordings successfully', async () => {
const source = createSource(
createTestCameraManager(),
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
cameraEventsQuery,
true,
);
await source.refresh(window);
// 1 event and 1 recording == 2 total items.
expect(source.dataset.length).toBe(2);
expect(source.dataset.get(EXPECTED_RECORDING_ID)).toEqual({
content: '',
end: 1695307875000,
group: 'camera/camera-1',
id: EXPECTED_RECORDING_ID,
start: 1695307866000,
type: 'background',
});
});
it('should refresh recordings and handle exception', async () => {
const consoleSpy = vi.spyOn(global.console, 'warn').mockReturnValue(undefined);
const cameraManager = createTestCameraManager();
vi.mocked(cameraManager.getRecordingSegments).mockRejectedValue(
new Error('Error fetching recordings'),
);
const source = createSource(
cameraManager,
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
cameraEventsQuery,
true,
);
expect(getRecordings(source.dataset).length).toBe(0);
await source.refresh(window);
expect(getRecordings(source.dataset).length).toBe(0);
expect(consoleSpy).toHaveBeenCalledWith('Error fetching recordings');
});
it('should not refresh recordings when window is cached', async () => {
const cameraManager = createTestCameraManager();
const source = createSource(
cameraManager,
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
cameraEventsQuery,
true,
);
await source.refresh(window);
expect(getRecordings(source.dataset).length).toBe(1);
await source.refresh(window);
expect(getRecordings(source.dataset).length).toBe(1);
expect(cameraManager.getRecordingSegments).toHaveBeenCalledTimes(1);
});
it('should not refresh recordings when recordings disabled', async () => {
const source = createSource(
createTestCameraManager(),
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
cameraEventsQuery,
// Disable recordings.
false,
);
await source.refresh(window);
expect(source.dataset.get(EXPECTED_RECORDING_ID)).toBeNull();
});
it('should not refresh recordings with folders', async () => {
const source = createSource(
createTestCameraManager(),
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
folderQuery,
true,
);
await source.refresh(window);
expect(source.dataset.get(EXPECTED_RECORDING_ID)).toBeNull();
});
it('should not refresh recordings without cameras', async () => {
// Create an empty query with no slices
const emptyQuery = new UnifiedQuery();
const source = createSource(
createTestCameraManager(),
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
emptyQuery,
true,
);
await source.refresh(window);
expect(source.dataset.get(EXPECTED_RECORDING_ID)).toBeNull();
});
it('should not refresh recordings without recording queries', async () => {
const cameraManager = createTestCameraManager();
vi.mocked(cameraManager.generateDefaultRecordingSegmentsQueries).mockReturnValue(
null,
);
const source = createSource(
cameraManager,
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
cameraEventsQuery,
true,
);
await source.refresh(window);
expect(getRecordings(source.dataset).length).toBe(0);
expect(cameraManager.getRecordingSegments).toHaveBeenCalledTimes(0);
});
it('should compress recording segments', async () => {
const cameraManager = createTestCameraManager();
const recordingSegmentQuery: RecordingSegmentsQuery = {
type: QueryType.RecordingSegments,
cameraIDs: new Set([CAMERA_ID]),
start: new Date('2025-09-21T19:31:06Z'),
end: new Date('2025-09-21T19:31:15Z'),
};
const recordingSegmentsQueryResults: RecordingSegmentsQueryResults = {
type: QueryResultsType.RecordingSegments,
engine: Engine.Generic,
segments: [
{
start_time: 1695307866,
end_time: 1695307875,
id: RECORDING_SEGMENT_ID,
},
{
start_time: 1695307875,
end_time: 1695307885,
id: `${RECORDING_SEGMENT_ID}-2`,
},
],
};
vi.mocked(cameraManager.getRecordingSegments).mockResolvedValue(
new Map([
[recordingSegmentQuery, recordingSegmentsQueryResults],
[{ ...recordingSegmentQuery }, recordingSegmentsQueryResults],
]),
);
const source = createSource(
cameraManager,
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
cameraEventsQuery,
true,
);
await source.refresh(window);
expect(getRecordings(source.dataset)).toEqual([
{
content: '',
end: 1695307885000,
group: 'camera/camera-1',
id: 'recording-camera-1-SEGMENT_ID',
start: 1695307866000,
type: 'background',
},
]);
expect(cameraManager.getRecordingSegments).toHaveBeenCalledTimes(1);
});
it('should compress recording segments without an end', async () => {
const cameraManager = createTestCameraManager();
const recordingSegmentQuery: RecordingSegmentsQuery = {
type: QueryType.RecordingSegments,
cameraIDs: new Set([CAMERA_ID]),
start: new Date('2025-09-21T19:31:06Z'),
end: new Date('2025-09-21T19:31:15Z'),
};
const recordingSegmentsQueryResults: RecordingSegmentsQueryResults = {
type: QueryResultsType.RecordingSegments,
engine: Engine.Generic,
segments: [
{
start_time: 1695307866,
end_time: 1695307876,
id: RECORDING_SEGMENT_ID,
},
{
start_time: 1695307875,
end_time: 1695307885,
id: `${RECORDING_SEGMENT_ID}-2`,
},
],
};
vi.mocked(cameraManager.getRecordingSegments).mockResolvedValue(
new Map([[recordingSegmentQuery, recordingSegmentsQueryResults]]),
);
const source = createSource(
cameraManager,
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
cameraEventsQuery,
true,
);
source.dataset.add({
id: 'recording-camera-1-SEGMENT_ID',
start: 1695307866000,
// No end time.
end: undefined,
group: 'camera/camera-1',
content: '',
type: 'background',
});
await source.refresh(window);
expect(getRecordings(source.dataset)).toEqual([
{
content: '',
end: 1695307885000,
group: 'camera/camera-1',
id: 'recording-camera-1-SEGMENT_ID',
start: 1695307866000,
type: 'background',
},
]);
expect(cameraManager.getRecordingSegments).toHaveBeenCalledTimes(1);
});
it('should compress recording segments without mixing up cameras', async () => {
const cameraManager = createTestCameraManager();
vi.mocked(cameraManager.getRecordingSegments).mockResolvedValue(
new Map([
[
{
type: QueryType.RecordingSegments,
cameraIDs: new Set(['camera-1']),
start: new Date('2025-09-21T19:31:06Z'),
end: new Date('2025-09-21T19:31:15Z'),
},
{
type: QueryResultsType.RecordingSegments,
engine: Engine.Generic,
segments: [
{
start_time: 1695307866,
end_time: 1695307875,
id: 'segment-1',
},
],
},
],
[
{
type: QueryType.RecordingSegments,
cameraIDs: new Set(['camera-2']),
start: new Date('2025-09-21T19:31:06Z'),
end: new Date('2025-09-21T19:31:15Z'),
},
{
type: QueryResultsType.RecordingSegments,
engine: Engine.Generic,
segments: [
{
start_time: 1695307866,
end_time: 1695307875,
id: 'segment-2',
},
],
},
],
]),
);
const source = createSource(
cameraManager,
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
cameraEventsQuery,
true,
);
await source.refresh(window);
expect(getRecordings(source.dataset)).toEqual([
{
content: '',
end: 1695307875000,
group: 'camera/camera-1',
id: 'recording-camera-1-segment-1',
start: 1695307866000,
type: 'background',
},
{
content: '',
end: 1695307875000,
group: 'camera/camera-2',
id: 'recording-camera-2-segment-2',
start: 1695307866000,
type: 'background',
},
]);
expect(cameraManager.getRecordingSegments).toHaveBeenCalledTimes(1);
});
});
});
describe('should rewrite event', () => {
it('should not rewrite when item is not found', () => {
const source = createSource(
createTestCameraManager(),
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
cameraEventsQuery,
true,
);
source.rewriteEvent('UNKNOWN_ID');
expect(source.dataset.length).toBe(0);
});
it('should not rewrite when item is not found', () => {
const source = createSource(
createTestCameraManager(),
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
cameraEventsQuery,
true,
);
const item = {
id: 'id',
start: start.getTime(),
end: end.getTime(),
media: testCameraMedia,
group: 'camera/camera-1' as const,
content: '',
type: 'range' as const,
query: cameraEventsQuery,
};
source.dataset.add(item);
source.rewriteEvent('id');
expect(source.dataset.get('id')).toBe(item);
});
});
describe('shape getter', () => {
it('should return the shape query', () => {
const source = createSource(
createTestCameraManager(),
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
cameraEventsQuery,
true,
);
const shape = source.shape;
expect(shape).toBe(cameraEventsQuery);
});
});
describe('areResultsFresh', () => {
it('should delegate to runner', () => {
const cameraManager = createTestCameraManager();
vi.mocked(cameraManager.areMediaQueriesResultsFresh).mockReturnValue(true);
const source = createSource(
cameraManager,
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
cameraEventsQuery,
true,
);
const result = source.areResultsFresh(new Date(), cameraEventsQuery);
expect(result).toBe(true);
});
it('should return false when stale', () => {
const cameraManager = createTestCameraManager();
vi.mocked(cameraManager.areMediaQueriesResultsFresh).mockReturnValue(false);
const source = createSource(
cameraManager,
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
cameraEventsQuery,
true,
);
const result = source.areResultsFresh(new Date(), cameraEventsQuery);
expect(result).toBe(false);
});
});
describe('buildRecordingsWindowedQuery', () => {
it('should build recordings query with window', () => {
const source = createSource(
createTestCameraManager(),
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
cameraEventsQuery,
true,
);
const window = { start, end };
const query = source.buildRecordingsWindowedQuery(window);
expect(query).not.toBeNull();
const mediaQueries = query?.getMediaQueries({ type: QueryType.Recording });
expect(mediaQueries?.length).toBeGreaterThan(0);
expect(mediaQueries?.[0].start).toBe(start);
expect(mediaQueries?.[0].end).toBe(end);
});
it('should return null when no cameras', () => {
const emptyQuery = new UnifiedQuery();
const source = createSource(
createTestCameraManager(),
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
emptyQuery,
true,
);
const query = source.buildRecordingsWindowedQuery({ start, end });
expect(query).toBeNull();
});
});
describe('hasClip/hasSnapshot in refresh', () => {
beforeAll(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date('2024-01-15T12:00:00Z'));
});
afterAll(() => {
vi.useRealTimers();
});
const isEventQuery = (query: QueryNode): query is EventQuery =>
query.source === QuerySource.Camera && query.type === QueryType.Event;
it('should set hasClip for clips media type', async () => {
const cameraManager = createTestCameraManager();
vi.mocked(cameraManager.executeMediaQueries).mockResolvedValue([]);
const clipsQuery = new UnifiedQuery();
const queryNode: EventQuery = {
source: QuerySource.Camera,
type: QueryType.Event,
cameraIDs: new Set(['camera-1']),
hasClip: true,
};
clipsQuery.addNode(queryNode);
const source = createSource(
cameraManager,
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
clipsQuery,
true,
);
const window = { start, end };
await source.refresh(window);
expect(cameraManager.executeMediaQueries).toHaveBeenCalled();
const call = vi.mocked(cameraManager.executeMediaQueries).mock.calls[0];
const eventQuery = call[0][0];
assert(isEventQuery(eventQuery));
expect(eventQuery.hasClip).toBe(true);
expect(eventQuery.hasSnapshot).toBeUndefined();
});
it('should set hasSnapshot for snapshots media type', async () => {
const cameraManager = createTestCameraManager();
vi.mocked(cameraManager.executeMediaQueries).mockResolvedValue([]);
const snapshotsQuery = new UnifiedQuery();
const queryNode: EventQuery = {
source: QuerySource.Camera,
type: QueryType.Event,
cameraIDs: new Set(['camera-1']),
hasSnapshot: true,
};
snapshotsQuery.addNode(queryNode);
const source = createSource(
cameraManager,
mock<FoldersManager>(),
mock<ConditionStateManagerReadonlyInterface>(),
snapshotsQuery,
true,
);
const window = { start, end };
await source.refresh(window);
expect(cameraManager.executeMediaQueries).toHaveBeenCalled();
const call = vi.mocked(cameraManager.executeMediaQueries).mock.calls[0];
const eventQuery = call[0][0];
assert(isEventQuery(eventQuery));
expect(eventQuery.hasSnapshot).toBe(true);
expect(eventQuery.hasClip).toBeUndefined();
});
});
});