Add native local NAS and network service integrations
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||
import { HomeAssistantMikrotikIntegration, type IMikrotikCommand, type IMikrotikConfig } from '../../ts/integrations/mikrotik/index.js';
|
||||
|
||||
const config: IMikrotikConfig = {
|
||||
host: '192.168.88.1',
|
||||
snapshot: {
|
||||
connected: true,
|
||||
router: {
|
||||
name: 'API Router',
|
||||
host: '192.168.88.1',
|
||||
serialNumber: 'MK123456',
|
||||
actions: ['reboot'],
|
||||
},
|
||||
resources: {},
|
||||
devices: [],
|
||||
interfaces: [],
|
||||
sensors: {},
|
||||
},
|
||||
};
|
||||
|
||||
tap.test('does not fake RouterOS/API command success without injected executor', async () => {
|
||||
const runtime = await new HomeAssistantMikrotikIntegration().setup(config, {});
|
||||
const result = await runtime.callService!({ domain: 'mikrotik', service: 'reboot', target: {} });
|
||||
|
||||
expect(result.success).toBeFalse();
|
||||
expect(result.error || '').toInclude('not faked');
|
||||
await runtime.destroy();
|
||||
});
|
||||
|
||||
tap.test('executes explicit RouterOS/API commands through injected executor', async () => {
|
||||
let command: IMikrotikCommand | undefined;
|
||||
const runtime = await new HomeAssistantMikrotikIntegration().setup({
|
||||
...config,
|
||||
commandExecutor: async (commandArg) => {
|
||||
command = commandArg;
|
||||
return { success: true, data: { accepted: true } };
|
||||
},
|
||||
}, {});
|
||||
const result = await runtime.callService!({ domain: 'mikrotik', service: 'reboot', target: {} });
|
||||
|
||||
expect(result.success).toBeTrue();
|
||||
expect(command?.path).toEqual('/system/reboot');
|
||||
await runtime.destroy();
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
@@ -0,0 +1,56 @@
|
||||
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||
import { createMikrotikDiscoveryDescriptor, MikrotikConfigFlow } from '../../ts/integrations/mikrotik/index.js';
|
||||
|
||||
tap.test('matches and validates manual Mikrotik RouterOS/API entries', async () => {
|
||||
const descriptor = createMikrotikDiscoveryDescriptor();
|
||||
const matcher = descriptor.getMatchers()[0];
|
||||
const result = await matcher.matches({
|
||||
host: '192.168.88.1',
|
||||
name: 'RouterOS Lab',
|
||||
model: 'hAP ax3',
|
||||
macAddress: 'AA-BB-CC-DD-EE-FF',
|
||||
}, {});
|
||||
|
||||
expect(result.matched).toBeTrue();
|
||||
expect(result.candidate?.integrationDomain).toEqual('mikrotik');
|
||||
expect(result.candidate?.port).toEqual(8728);
|
||||
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?.liveRouterOsApiImplemented).toBeFalse();
|
||||
});
|
||||
|
||||
tap.test('accepts snapshot-only manual setup and rejects unrelated entries', async () => {
|
||||
const descriptor = createMikrotikDiscoveryDescriptor();
|
||||
const matcher = descriptor.getMatchers()[0];
|
||||
const snapshotResult = await matcher.matches({
|
||||
snapshot: {
|
||||
connected: true,
|
||||
router: { name: 'Snapshot Router', serialNumber: 'MK654321' },
|
||||
resources: {},
|
||||
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 Mikrotik config without claiming live API support', async () => {
|
||||
const flow = new MikrotikConfigFlow();
|
||||
const step = await flow.start({ source: 'manual', host: '192.168.88.1', metadata: { verifySsl: true } }, {});
|
||||
const done = await step.submit!({ host: '192.168.88.1', verifySsl: true, username: 'admin' });
|
||||
|
||||
expect(done.kind).toEqual('done');
|
||||
expect(done.config?.port).toEqual(8728);
|
||||
expect(done.config?.protocol).toEqual('routeros-api-ssl');
|
||||
expect(done.config?.metadata?.liveRouterOsApiImplemented).toBeFalse();
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
@@ -0,0 +1,102 @@
|
||||
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||
import { MikrotikMapper, type IMikrotikSnapshot } from '../../ts/integrations/mikrotik/index.js';
|
||||
|
||||
const snapshot: IMikrotikSnapshot = {
|
||||
connected: true,
|
||||
updatedAt: '2026-01-01T00:00:00.000Z',
|
||||
router: {
|
||||
host: '192.168.88.1',
|
||||
name: 'Core Router',
|
||||
model: 'hAP ax3',
|
||||
serialNumber: 'MK123456',
|
||||
firmware: '7.15.3',
|
||||
actions: ['reboot'],
|
||||
},
|
||||
resources: {
|
||||
cpuLoad: 12,
|
||||
freeMemory: 512 * 1048576,
|
||||
totalMemory: 1024 * 1048576,
|
||||
uptime: '1h2m3s',
|
||||
version: '7.15.3',
|
||||
boardName: 'hAP ax3',
|
||||
},
|
||||
devices: [
|
||||
{
|
||||
mac: '11:22:33:44:55:66',
|
||||
name: 'Kitchen Phone',
|
||||
ipAddress: '192.168.88.20',
|
||||
interface: 'bridge',
|
||||
ssid: 'Home WiFi',
|
||||
connected: true,
|
||||
signalStrength: -55,
|
||||
signalToNoise: 42,
|
||||
actions: ['arp_ping'],
|
||||
},
|
||||
],
|
||||
interfaces: [
|
||||
{
|
||||
id: '*1',
|
||||
name: 'ether1',
|
||||
label: 'WAN',
|
||||
type: 'ether',
|
||||
running: true,
|
||||
disabled: false,
|
||||
rxBytes: 2_000_000_000,
|
||||
txBytes: 1_000_000_000,
|
||||
rxBitsPerSecond: 2_000_000,
|
||||
txBitsPerSecond: 1_000_000,
|
||||
},
|
||||
],
|
||||
sensors: {},
|
||||
};
|
||||
|
||||
tap.test('maps Mikrotik router resources, clients, interfaces, and traffic sensors', async () => {
|
||||
const normalized = MikrotikMapper.toSnapshot({ snapshot });
|
||||
const devices = MikrotikMapper.toDevices(normalized);
|
||||
const entities = MikrotikMapper.toEntities(normalized);
|
||||
|
||||
expect(devices.some((deviceArg) => deviceArg.id === 'mikrotik.router.mk123456')).toBeTrue();
|
||||
expect(devices.some((deviceArg) => deviceArg.id === 'mikrotik.client.11_22_33_44_55_66')).toBeTrue();
|
||||
expect(entities.find((entityArg) => entityArg.id === 'sensor.core_router_cpu_load')?.state).toEqual(12);
|
||||
expect(entities.find((entityArg) => entityArg.id === 'sensor.core_router_memory_free')?.state).toEqual(512);
|
||||
expect(entities.find((entityArg) => entityArg.id === 'sensor.core_router_uptime')?.state).toEqual(3723);
|
||||
expect(entities.find((entityArg) => entityArg.id === 'sensor.core_router_wan_download')?.state).toEqual(2);
|
||||
expect(entities.find((entityArg) => entityArg.id === 'sensor.core_router_wan_download_speed')?.state).toEqual(2);
|
||||
expect(entities.find((entityArg) => entityArg.id === 'binary_sensor.kitchen_phone_connected')?.attributes?.nativePlatform).toEqual('device_tracker');
|
||||
expect(entities.find((entityArg) => entityArg.id === 'switch.core_router_wan')?.state).toEqual('on');
|
||||
});
|
||||
|
||||
tap.test('models only represented Mikrotik RouterOS/API commands safely', async () => {
|
||||
const normalized = MikrotikMapper.toSnapshot({ snapshot });
|
||||
const rebootCommand = MikrotikMapper.commandForService(normalized, {
|
||||
domain: 'mikrotik',
|
||||
service: 'reboot',
|
||||
target: {},
|
||||
});
|
||||
const interfaceCommand = MikrotikMapper.commandForService(normalized, {
|
||||
domain: 'switch',
|
||||
service: 'turn_off',
|
||||
target: { entityId: 'switch.core_router_wan' },
|
||||
});
|
||||
const arpPingCommand = MikrotikMapper.commandForService(normalized, {
|
||||
domain: 'mikrotik',
|
||||
service: 'arp_ping',
|
||||
target: {},
|
||||
data: { mac: '11-22-33-44-55-66', count: 1 },
|
||||
});
|
||||
const unsupportedDisconnect = MikrotikMapper.commandForService(normalized, {
|
||||
domain: 'mikrotik',
|
||||
service: 'disconnect_client',
|
||||
target: {},
|
||||
data: { mac: '11:22:33:44:55:66' },
|
||||
});
|
||||
|
||||
expect(rebootCommand?.path).toEqual('/system/reboot');
|
||||
expect(interfaceCommand?.path).toEqual('/interface/set');
|
||||
expect(interfaceCommand?.params.disabled).toEqual('yes');
|
||||
expect(arpPingCommand?.path).toEqual('/ping');
|
||||
expect(arpPingCommand?.params.address).toEqual('192.168.88.20');
|
||||
expect(unsupportedDisconnect).toBeUndefined();
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
Reference in New Issue
Block a user