Add native local network integrations
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||
import { HomeAssistantAsuswrtIntegration, type IAsuswrtCommand, type IAsuswrtConfig } from '../../ts/integrations/asuswrt/index.js';
|
||||
|
||||
const config: IAsuswrtConfig = {
|
||||
host: '192.168.1.1',
|
||||
protocol: 'ssh',
|
||||
snapshot: {
|
||||
connected: true,
|
||||
router: {
|
||||
name: 'SSH Router',
|
||||
host: '192.168.1.1',
|
||||
protocol: 'ssh',
|
||||
labelMac: 'AA:BB:CC:DD:EE:FF',
|
||||
actions: ['reboot'],
|
||||
},
|
||||
devices: [],
|
||||
interfaces: [],
|
||||
sensors: {},
|
||||
},
|
||||
};
|
||||
|
||||
tap.test('does not fake SSH/Telnet command success without injected executor', async () => {
|
||||
const runtime = await new HomeAssistantAsuswrtIntegration().setup(config, {});
|
||||
const result = await runtime.callService!({ domain: 'asuswrt', service: 'reboot', target: {} });
|
||||
|
||||
expect(result.success).toBeFalse();
|
||||
expect(result.error || '').toInclude('not faked');
|
||||
await runtime.destroy();
|
||||
});
|
||||
|
||||
tap.test('executes explicit commands through injected executor', async () => {
|
||||
let command: IAsuswrtCommand | undefined;
|
||||
const runtime = await new HomeAssistantAsuswrtIntegration().setup({
|
||||
...config,
|
||||
commandExecutor: async (commandArg) => {
|
||||
command = commandArg;
|
||||
return { success: true, data: { accepted: true } };
|
||||
},
|
||||
}, {});
|
||||
const result = await runtime.callService!({ domain: 'asuswrt', service: 'reboot', target: {} });
|
||||
|
||||
expect(result.success).toBeTrue();
|
||||
expect(command?.type).toEqual('router.reboot');
|
||||
await runtime.destroy();
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
@@ -0,0 +1,55 @@
|
||||
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||
import { AsuswrtConfigFlow, createAsuswrtDiscoveryDescriptor } from '../../ts/integrations/asuswrt/index.js';
|
||||
|
||||
tap.test('matches and validates manual ASUSWRT router entries', async () => {
|
||||
const descriptor = createAsuswrtDiscoveryDescriptor();
|
||||
const matcher = descriptor.getMatchers()[0];
|
||||
const result = await matcher.matches({
|
||||
host: '192.168.1.1',
|
||||
name: 'Main Router',
|
||||
model: 'RT-AX88U',
|
||||
macAddress: 'AA-BB-CC-DD-EE-FF',
|
||||
}, {});
|
||||
|
||||
expect(result.matched).toBeTrue();
|
||||
expect(result.candidate?.integrationDomain).toEqual('asuswrt');
|
||||
expect(result.candidate?.port).toEqual(8443);
|
||||
expect(result.normalizedDeviceId).toEqual('aa:bb:cc:dd:ee:ff');
|
||||
|
||||
const validator = descriptor.getValidators()[0];
|
||||
const validation = await validator.validate(result.candidate!, {});
|
||||
expect(validation.matched).toBeTrue();
|
||||
expect(validation.metadata?.liveSshTelnetImplemented).toBeFalse();
|
||||
});
|
||||
|
||||
tap.test('accepts snapshot-only manual setup and rejects unrelated entries', async () => {
|
||||
const descriptor = createAsuswrtDiscoveryDescriptor();
|
||||
const matcher = descriptor.getMatchers()[0];
|
||||
const snapshotResult = await matcher.matches({
|
||||
snapshot: {
|
||||
connected: true,
|
||||
router: { name: 'Snapshot Router', labelMac: 'AABBCCDDEEFF' },
|
||||
devices: [],
|
||||
interfaces: [],
|
||||
sensors: {},
|
||||
},
|
||||
}, {});
|
||||
const unrelated = await matcher.matches({ name: 'Generic Switch', model: 'GS108' }, {});
|
||||
|
||||
expect(snapshotResult.matched).toBeTrue();
|
||||
expect(snapshotResult.confidence).toEqual('certain');
|
||||
expect(unrelated.matched).toBeFalse();
|
||||
});
|
||||
|
||||
tap.test('builds manual ASUSWRT config without claiming SSH/Telnet live support', async () => {
|
||||
const flow = new AsuswrtConfigFlow();
|
||||
const step = await flow.start({ source: 'manual', host: '192.168.1.1', metadata: { protocol: 'ssh' } }, {});
|
||||
const done = await step.submit!({ host: '192.168.1.1', protocol: 'ssh', username: 'admin', mode: 'ap' });
|
||||
|
||||
expect(done.kind).toEqual('done');
|
||||
expect(done.config?.protocol).toEqual('ssh');
|
||||
expect(done.config?.mode).toEqual('ap');
|
||||
expect(done.config?.metadata?.liveSshTelnetImplemented).toBeFalse();
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
@@ -0,0 +1,92 @@
|
||||
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||
import { AsuswrtMapper, type IAsuswrtSnapshot } from '../../ts/integrations/asuswrt/index.js';
|
||||
|
||||
const snapshot: IAsuswrtSnapshot = {
|
||||
connected: true,
|
||||
updatedAt: '2026-01-01T00:00:00.000Z',
|
||||
router: {
|
||||
host: '192.168.1.1',
|
||||
protocol: 'https',
|
||||
name: 'Main Router',
|
||||
model: 'RT-AX88U',
|
||||
firmware: '3.0.0.4',
|
||||
labelMac: 'AA:BB:CC:DD:EE:FF',
|
||||
actions: ['reboot'],
|
||||
},
|
||||
devices: [
|
||||
{
|
||||
mac: '11:22:33:44:55:66',
|
||||
name: 'Kitchen Phone',
|
||||
ipAddress: '192.168.1.40',
|
||||
connected: true,
|
||||
connectedTo: 'wl0.1',
|
||||
actions: ['reconnect'],
|
||||
},
|
||||
],
|
||||
interfaces: [
|
||||
{
|
||||
name: 'eth0',
|
||||
label: 'WAN',
|
||||
connected: true,
|
||||
rxBytes: 2_000_000_000,
|
||||
txBytes: 1_000_000_000,
|
||||
rxRate: 250_000,
|
||||
txRate: 125_000,
|
||||
},
|
||||
],
|
||||
sensors: {
|
||||
sensor_connected_device: 1,
|
||||
sensor_rx_bytes: 2_000_000_000,
|
||||
sensor_tx_bytes: 1_000_000_000,
|
||||
sensor_rx_rates: 250_000,
|
||||
sensor_tx_rates: 125_000,
|
||||
sensor_load_avg1: 0.12,
|
||||
'2.4GHz': 42,
|
||||
CPU: 61,
|
||||
mem_usage_perc: 35,
|
||||
mem_free: 262_144,
|
||||
sensor_uptime: 3600,
|
||||
},
|
||||
actions: [],
|
||||
};
|
||||
|
||||
tap.test('maps ASUSWRT router, tracker, interface, and traffic sensors', async () => {
|
||||
const devices = AsuswrtMapper.toDevices(snapshot);
|
||||
const entities = AsuswrtMapper.toEntities(snapshot);
|
||||
|
||||
expect(devices.some((deviceArg) => deviceArg.id === 'asuswrt.router.aa_bb_cc_dd_ee_ff')).toBeTrue();
|
||||
expect(devices.some((deviceArg) => deviceArg.id === 'asuswrt.client.11_22_33_44_55_66')).toBeTrue();
|
||||
expect(entities.find((entityArg) => entityArg.id === 'sensor.main_router_download')?.state).toEqual(2);
|
||||
expect(entities.find((entityArg) => entityArg.id === 'sensor.main_router_download_speed')?.state).toEqual(2);
|
||||
expect(entities.find((entityArg) => entityArg.id === 'sensor.main_router_wan_download')?.state).toEqual(2);
|
||||
expect(entities.find((entityArg) => entityArg.id === 'sensor.main_router_wan_upload_speed')?.state).toEqual(1);
|
||||
expect(entities.find((entityArg) => entityArg.id === 'binary_sensor.kitchen_phone_connected')?.state).toEqual('on');
|
||||
expect(entities.find((entityArg) => entityArg.id === 'sensor.main_router_memory_free')?.state).toEqual(256);
|
||||
});
|
||||
|
||||
tap.test('maps only explicitly represented ASUSWRT actions to commands', async () => {
|
||||
const rebootCommand = AsuswrtMapper.commandForService(snapshot, {
|
||||
domain: 'asuswrt',
|
||||
service: 'reboot',
|
||||
target: {},
|
||||
});
|
||||
const reconnectCommand = AsuswrtMapper.commandForService(snapshot, {
|
||||
domain: 'asuswrt',
|
||||
service: 'reconnect_device',
|
||||
target: {},
|
||||
data: { mac: '11-22-33-44-55-66' },
|
||||
});
|
||||
const unsupportedBlock = AsuswrtMapper.commandForService(snapshot, {
|
||||
domain: 'asuswrt',
|
||||
service: 'block_device',
|
||||
target: {},
|
||||
data: { mac: '11:22:33:44:55:66' },
|
||||
});
|
||||
|
||||
expect(rebootCommand?.type).toEqual('router.reboot');
|
||||
expect(reconnectCommand?.type).toEqual('client.action');
|
||||
expect(reconnectCommand?.mac).toEqual('11:22:33:44:55:66');
|
||||
expect(unsupportedBlock).toBeUndefined();
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
Reference in New Issue
Block a user