Files
integrations/test/folder_watcher/test.folder_watcher.node.ts
T

77 lines
3.8 KiB
TypeScript

import { expect, tap } from '@git.zone/tstest/tapbundle';
import { FolderWatcherClient, FolderWatcherConfigFlow, FolderWatcherIntegration, FolderWatcherMapper, HomeAssistantFolderWatcherIntegration, createFolderWatcherDiscoveryDescriptor, folderWatcherProfile, type IFolderWatcherSnapshot, type TFolderWatcherRawData } from '../../ts/integrations/folder_watcher/index.js';
const fixtureFolder = 'test/folder_watcher';
const rawData: TFolderWatcherRawData = {
folder: fixtureFolder,
patterns: ['*.yaml'],
watched_files: 1,
last_event: {
event_type: 'created',
path: `${fixtureFolder}/event.yaml`,
file: 'event.yaml',
folder: fixtureFolder,
},
};
tap.test('matches manual Folder Watcher candidates and creates config flow output', async () => {
const descriptor = createFolderWatcherDiscoveryDescriptor();
const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'folder_watcher-manual-match');
const result = await matcher!.matches({ source: 'manual', name: 'Folder watcher', metadata: { rawData, folder: fixtureFolder, patterns: ['*.yaml'] } }, {});
expect(result.matched).toBeTrue();
expect(result.candidate?.integrationDomain).toEqual('folder_watcher');
const validation = await descriptor.getValidators()[0].validate(result.candidate!, {});
expect(validation.matched).toBeTrue();
const done = await (await new FolderWatcherConfigFlow().start(result.candidate!, {})).submit!({});
expect(done.kind).toEqual('done');
expect(done.config?.rawData).toEqual(rawData);
expect(done.config?.metadata?.folder).toEqual(fixtureFolder);
});
tap.test('maps Folder Watcher raw snapshots to devices and entities', async () => {
const client = new FolderWatcherClient({ rawData });
const snapshot = await client.getSnapshot();
const devices = FolderWatcherMapper.toDevices(snapshot);
const entities = FolderWatcherMapper.toEntities(snapshot);
expect(snapshot.online).toBeTrue();
expect(devices[0].integrationDomain).toEqual('folder_watcher');
expect(devices[0].model).toEqual('Watchdog folder observer');
expect(entities.length).toEqual(1);
expect(entities[0].state).toEqual('created');
expect(entities[0].attributes?.haPlatform).toEqual('event');
});
tap.test('reads local Folder Watcher snapshots and exposes read-only runtime with unsupported control', async () => {
const integration = new FolderWatcherIntegration();
const alias = new HomeAssistantFolderWatcherIntegration();
expect(alias instanceof FolderWatcherIntegration).toBeTrue();
expect(alias.domain).toEqual('folder_watcher');
expect(integration.status).toEqual('read-only-runtime');
expect(folderWatcherProfile.metadata.configFlow).toEqual(true);
expect(folderWatcherProfile.metadata.qualityScale).toEqual('internal');
expect(folderWatcherProfile.metadata.requirements).toEqual(['watchdog==6.0.0']);
const runtime = await integration.setup({ folder: fixtureFolder, patterns: ['*.yaml'] }, {});
const status = await runtime.callService!({ domain: 'folder_watcher', service: 'status', target: {} });
const refresh = await runtime.callService!({ domain: 'folder_watcher', service: 'refresh', target: {} });
const snapshot = status.data as IFolderWatcherSnapshot;
expect(status.success).toBeTrue();
expect(refresh.success).toBeTrue();
expect(snapshot.online).toBeTrue();
expect(snapshot.entities[0].state).toEqual('idle');
expect(snapshot.entities[0].attributes?.watched_files).toEqual(1);
expect((await runtime.devices())[0].name).toEqual(`Folder Watcher ${fixtureFolder}`);
const command = await runtime.callService!({ domain: 'folder_watcher', service: 'turn_on', target: {} });
expect(command.success).toBeFalse();
expect(command.error!).toContain('requires an injected client.execute() or commandExecutor');
await runtime.destroy();
});
export default tap.start();