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

94 lines
3.8 KiB
TypeScript

import { expect, tap } from '@git.zone/tstest/tapbundle';
import { GtfsClient, GtfsConfigFlow, GtfsIntegration, GtfsMapper, HomeAssistantGtfsIntegration, createGtfsDiscoveryDescriptor, gtfsProfile, type IGtfsSnapshot, type TGtfsRawData } from '../../ts/integrations/gtfs/index.js';
const rawData: TGtfsRawData = {
device: {
id: 'gtfs-downtown-route',
name: 'GTFS Downtown Route',
manufacturer: 'GTFS',
model: 'Transit schedule feed',
},
entities: [
{
id: 'next_departure',
name: 'Next Departure',
platform: 'sensor',
state: '2026-05-11T14:35:00Z',
deviceClass: 'timestamp',
attributes: {
arrival: '2026-05-11T14:58:00Z',
day: 'today',
destination: 'DOWNTOWN',
origin: 'CENTRAL',
route_id: '10',
trip_id: 'weekday-10-1435',
},
},
],
query: {
data: 'city.zip',
destination: 'DOWNTOWN',
origin: 'CENTRAL',
},
};
tap.test('matches manual GTFS candidates and creates config flow output', async () => {
const descriptor = createGtfsDiscoveryDescriptor();
const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'gtfs-manual-match');
const result = await matcher!.matches({ source: 'manual', id: 'gtfs-downtown-route', name: 'Downtown GTFS', metadata: { rawData } }, {});
expect(result.matched).toBeTrue();
expect(result.candidate?.integrationDomain).toEqual('gtfs');
const validation = await descriptor.getValidators()[0].validate(result.candidate!, {});
expect(validation.matched).toBeTrue();
const done = await (await new GtfsConfigFlow().start(result.candidate!, {})).submit!({});
expect(done.kind).toEqual('done');
expect(done.config?.name).toEqual('Downtown GTFS');
expect(done.config?.rawData).toEqual(rawData);
});
tap.test('maps GTFS raw snapshots to runtime devices and entities', async () => {
const client = new GtfsClient({ name: 'GTFS Runtime', rawData });
const snapshot = await client.getSnapshot();
const mappedSnapshot = GtfsMapper.toSnapshotFromRaw({ name: 'GTFS Runtime' }, rawData);
const devices = GtfsMapper.toDevices(mappedSnapshot);
const entities = GtfsMapper.toEntities(mappedSnapshot);
expect(snapshot.online).toBeTrue();
expect(mappedSnapshot.source).toEqual('manual');
expect(devices[0].integrationDomain).toEqual('gtfs');
expect(devices[0].manufacturer).toEqual('GTFS');
expect(entities.some((entityArg) => entityArg.id === 'sensor.gtfs_downtown_route_next_departure')).toBeTrue();
expect(entities[0].attributes?.deviceClass).toEqual('timestamp');
});
tap.test('exposes GTFS read-only runtime, HA alias, and unsupported control', async () => {
const integration = new GtfsIntegration();
const alias = new HomeAssistantGtfsIntegration();
expect(alias instanceof GtfsIntegration).toBeTrue();
expect(alias.domain).toEqual('gtfs');
expect(integration.status).toEqual('read-only-runtime');
expect(gtfsProfile.metadata.configFlow).toEqual(false);
expect(gtfsProfile.metadata.qualityScale).toEqual('legacy');
expect(gtfsProfile.metadata.requirements).toEqual(['pygtfs==0.1.9']);
const runtime = await integration.setup({ name: 'GTFS Runtime', rawData }, {});
const status = await runtime.callService!({ domain: 'gtfs', service: 'status', target: {} });
const refresh = await runtime.callService!({ domain: 'gtfs', service: 'refresh', target: {} });
const snapshot = status.data as IGtfsSnapshot;
expect(status.success).toBeTrue();
expect(refresh.success).toBeTrue();
expect(snapshot.online).toBeTrue();
expect((await runtime.devices())[0].name).toEqual('GTFS Downtown Route');
const command = await runtime.callService!({ domain: 'gtfs', 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();