343 lines
12 KiB
TypeScript
343 lines
12 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
|
|
// Mock logger
|
|
vi.mock('@adonisjs/core/services/logger', () => ({
|
|
default: { info: vi.fn(), debug: vi.fn(), warn: vi.fn(), error: vi.fn() },
|
|
}))
|
|
|
|
// Mock DeviceModel
|
|
vi.mock('../../../../app/models/device.js', () => ({
|
|
default: {
|
|
createOrUpdate: vi.fn().mockResolvedValue({}),
|
|
findByDeviceId: vi.fn().mockResolvedValue(null),
|
|
updateOne: vi.fn().mockResolvedValue({}),
|
|
},
|
|
}))
|
|
|
|
// Mock ConfigurationModel
|
|
vi.mock('#models/configuration', () => ({
|
|
default: {
|
|
find: vi.fn().mockResolvedValue([]),
|
|
findOne: vi.fn().mockResolvedValue(null),
|
|
},
|
|
}))
|
|
|
|
// Mock FirmwareCatalogService
|
|
vi.mock('../../../../app/services/firmware_catalog_service.js', () => ({
|
|
default: class { async resolveUpgradePath() { return [] } },
|
|
FirmwareCatalogService: class { async resolveUpgradePath() { return [] } },
|
|
}))
|
|
|
|
// Mock schedule_window
|
|
vi.mock('../../../../app/utils/schedule_window.js', () => ({
|
|
isWithinSchedule: vi.fn().mockReturnValue(true),
|
|
}))
|
|
|
|
// Mock parameter_writability
|
|
vi.mock('../../../../app/services/parameter_writability.js', () => ({
|
|
reclassifyDeviceFromCatalog: vi.fn(),
|
|
}))
|
|
|
|
// Mock CommandModel
|
|
vi.mock('../../../../app/models/command.js', () => ({
|
|
default: {
|
|
getCollection: vi.fn().mockReturnValue({
|
|
find: vi.fn().mockReturnValue({ sort: vi.fn().mockReturnValue({ toArray: vi.fn().mockResolvedValue([]) }) }),
|
|
countDocuments: vi.fn().mockResolvedValue(0),
|
|
}),
|
|
updateCommand: vi.fn().mockResolvedValue({}),
|
|
},
|
|
}))
|
|
|
|
import { ConfigurationEvaluationState, FirmwareUpgradeState, ComplianceUpdateState } from '../../../../app/services/cwmp/configuration_evaluation_state.js'
|
|
import { Device } from '../../../../app/domain/device/device.js'
|
|
import { DeviceId } from '../../../../app/domain/device/device_id.js'
|
|
import { DeviceParameter } from '../../../../app/domain/device/device_parameter.js'
|
|
import { CWMPTaskQueue } from '../../../../app/services/cwmp/task_queue.js'
|
|
import { DeviceActionExecutor } from '../../../../app/services/cwmp/device_action_executor.js'
|
|
import { ParameterType } from '../../../../app/domain/shared/types.js'
|
|
import ConfigurationModel from '#models/configuration'
|
|
|
|
function makeDevice(): Device {
|
|
const device = new Device(new DeviceId('Sagemcom', 'F8B7FF', 'Livebox 4', 'LB4-TEST-001'))
|
|
device.setDataModel('tr-181')
|
|
device.setGpnStatus('unsupported')
|
|
return device
|
|
}
|
|
|
|
function makeContext(device: Device) {
|
|
const taskQueue = new CWMPTaskQueue()
|
|
return {
|
|
device,
|
|
session: 'test-session',
|
|
taskQueue,
|
|
services: { deviceAction: new DeviceActionExecutor(taskQueue) },
|
|
}
|
|
}
|
|
|
|
function makeConfig(overrides: any = {}) {
|
|
return {
|
|
identifier: 'test-config',
|
|
name: 'Test Config',
|
|
version: '1.0.0',
|
|
priority: 1,
|
|
policy: 'maintain',
|
|
enabled: true,
|
|
criteria: { manufacturer: 'Sagemcom', productClass: 'Livebox 4' },
|
|
inventory: [{ path: 'Device.ManagementServer.' }],
|
|
desiredParameters: [
|
|
{ name: 'Device.ManagementServer.PeriodicInformInterval', value: '300', type: 'unsignedInt' },
|
|
],
|
|
firmwareTarget: null,
|
|
schedule: null,
|
|
tags: [],
|
|
...overrides,
|
|
}
|
|
}
|
|
|
|
describe('ConfigurationEvaluationState — inventory and diffs flow', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('sends SPV when parameterDiffs exist', async () => {
|
|
const config = makeConfig()
|
|
vi.mocked(ConfigurationModel.find).mockResolvedValue([config])
|
|
|
|
const device = makeDevice()
|
|
// Device has the param but with wrong value
|
|
device.setParameter(new DeviceParameter(
|
|
'Device.ManagementServer.PeriodicInformInterval', '60', ParameterType.UNSIGNED_INT
|
|
))
|
|
const context = makeContext(device)
|
|
|
|
const state = new ConfigurationEvaluationState()
|
|
const result = await state.handle(context as any)
|
|
|
|
// Since gpnStatus=unsupported, inventory GPN is skipped
|
|
// Should produce SPV for the diff
|
|
expect(result.response).toContain('SetParameterValues')
|
|
expect(result.response).toContain('PeriodicInformInterval')
|
|
expect(result.response).toContain('300')
|
|
})
|
|
|
|
it('skips inventory GPN when device gpnStatus is unsupported', async () => {
|
|
const config = makeConfig({ desiredParameters: [] })
|
|
vi.mocked(ConfigurationModel.find).mockResolvedValue([config])
|
|
|
|
const device = makeDevice()
|
|
const context = makeContext(device)
|
|
|
|
const state = new ConfigurationEvaluationState()
|
|
const result = await state.handle(context as any)
|
|
|
|
// No diffs, inventory skipped → should go to compliance-update
|
|
expect(result.nextState).toBeDefined()
|
|
// Should NOT send GPN
|
|
expect(result.response || '').not.toContain('GetParameterNames')
|
|
})
|
|
|
|
it('handles evaluation error gracefully with PartialOperationalState', async () => {
|
|
// Force evaluate to throw
|
|
vi.mocked(ConfigurationModel.find).mockRejectedValue(new Error('DB connection lost'))
|
|
|
|
const device = makeDevice()
|
|
const context = makeContext(device)
|
|
|
|
const state = new ConfigurationEvaluationState()
|
|
const result = await state.handle(context as any)
|
|
|
|
// Should transition to PartialOperationalState
|
|
expect(result.nextState).toBeDefined()
|
|
expect((result.nextState as any).name).toBe('partial_operational')
|
|
})
|
|
})
|
|
|
|
describe('FirmwareUpgradeState', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('sends Download RPC for firmware step', async () => {
|
|
const device = makeDevice()
|
|
const context = makeContext(device)
|
|
|
|
const steps = [
|
|
{ fromVersion: '4.0', toVersion: '5.0', downloadUrl: 'http://fw.example.com/5.0.bin', fileSize: 1024, checksum: 'abc' },
|
|
]
|
|
const engineResult = { configuration: makeConfig(), isAlreadyCompliant: false } as any
|
|
|
|
const state = new FirmwareUpgradeState(steps, 0, engineResult)
|
|
const result = await state.handle(context as any)
|
|
|
|
expect(result.response).toContain('Download')
|
|
expect(result.response).toContain('http://fw.example.com/5.0.bin')
|
|
expect(result.response).toContain('1 Firmware Upgrade Image')
|
|
})
|
|
|
|
it('transitions to ComplianceUpdateState when no more steps', async () => {
|
|
const device = makeDevice()
|
|
const context = makeContext(device)
|
|
|
|
const engineResult = { configuration: makeConfig(), isAlreadyCompliant: false } as any
|
|
const state = new FirmwareUpgradeState([], 0, engineResult) // empty steps
|
|
|
|
const result = await state.handle(context as any)
|
|
expect(result.nextState).toBeDefined()
|
|
expect((result.nextState as any).name).toBe('compliance-update')
|
|
})
|
|
})
|
|
|
|
describe('ComplianceUpdateState — provCode write when different', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('sends SPV for provisioning code when device has different value', async () => {
|
|
const device = makeDevice()
|
|
// Set a DIFFERENT provisioning code on the device
|
|
device.setParameter(new DeviceParameter(
|
|
'Device.DeviceInfo.ProvisioningCode', 'old-code', ParameterType.STRING
|
|
))
|
|
|
|
const context = makeContext(device)
|
|
const engineResult = {
|
|
configuration: makeConfig(),
|
|
isAlreadyCompliant: true,
|
|
} as any
|
|
|
|
const state = new ComplianceUpdateState(engineResult)
|
|
const result = await state.handle(context as any)
|
|
|
|
// Should produce an SPV for the provisioning code
|
|
expect(result.response).toContain('SetParameterValues')
|
|
expect(result.response).toContain('ProvisioningCode')
|
|
})
|
|
|
|
it('transitions to ExecutingCommandsState when no configuration', async () => {
|
|
const device = makeDevice()
|
|
const context = makeContext(device)
|
|
const engineResult = { configuration: null, isAlreadyCompliant: true } as any
|
|
|
|
const state = new ComplianceUpdateState(engineResult)
|
|
const result = await state.handle(context as any)
|
|
|
|
expect(result.nextState).toBeDefined()
|
|
expect((result.nextState as any).name).toBe('executing-commands')
|
|
})
|
|
})
|
|
|
|
describe('ConfigurationEvaluationState — inventory GPN phase (device supports GPN)', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('sends GPN for inventory paths when device supports GPN', async () => {
|
|
const config = makeConfig({
|
|
desiredParameters: [{ name: 'Device.ManagementServer.PeriodicInformInterval', value: '300', type: 'unsignedInt' }],
|
|
inventory: [{ path: 'Device.ManagementServer.' }],
|
|
})
|
|
vi.mocked(ConfigurationModel.find).mockResolvedValue([config])
|
|
|
|
const device = makeDevice()
|
|
device.setGpnStatus('supported') // Device supports GPN
|
|
// Set a different value so isAlreadyCompliant = false (triggers inventory)
|
|
device.setParameter(new DeviceParameter(
|
|
'Device.ManagementServer.PeriodicInformInterval', '60', ParameterType.UNSIGNED_INT
|
|
))
|
|
const context = makeContext(device)
|
|
|
|
const state = new ConfigurationEvaluationState()
|
|
const result = await state.handle(context as any)
|
|
|
|
// Should send GetParameterNames for the inventory path
|
|
expect(result.response).toBeDefined()
|
|
expect(result.response).toContain('GetParameterNames')
|
|
expect(result.response).toContain('Device.ManagementServer.')
|
|
})
|
|
|
|
it('inventory GPN response then sends SPV for diffs after inventory completes', async () => {
|
|
const config = makeConfig({
|
|
desiredParameters: [{ name: 'Device.ManagementServer.PeriodicInformInterval', value: '300', type: 'unsignedInt' }],
|
|
inventory: [{ path: 'Device.ManagementServer.' }],
|
|
})
|
|
vi.mocked(ConfigurationModel.find).mockResolvedValue([config])
|
|
|
|
const device = makeDevice()
|
|
device.setGpnStatus('supported')
|
|
device.setParameter(new DeviceParameter(
|
|
'Device.ManagementServer.PeriodicInformInterval', '60', ParameterType.UNSIGNED_INT
|
|
))
|
|
const context = makeContext(device)
|
|
|
|
const state = new ConfigurationEvaluationState()
|
|
// First call: sends GPN for inventory
|
|
const result1 = await state.handle(context as any)
|
|
expect(result1.response).toContain('GetParameterNames')
|
|
|
|
// Second call: GPN response received, inventory done, should send SPV for diffs
|
|
context.message = { success: true, data: {} }
|
|
const result2 = await state.handle(context as any)
|
|
// After inventory finishes (no more paths), should send SPV for the diff
|
|
expect(result2.response).toContain('SetParameterValues')
|
|
})
|
|
})
|
|
|
|
describe('FirmwareUpgradeState — error handling', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('returns isComplete on download error', async () => {
|
|
const device = makeDevice()
|
|
const taskQueue = new CWMPTaskQueue()
|
|
const context = {
|
|
device,
|
|
session: 'test-session',
|
|
taskQueue,
|
|
services: {
|
|
deviceAction: {
|
|
applyDownload: vi.fn().mockRejectedValue(new Error('Network timeout')),
|
|
},
|
|
},
|
|
}
|
|
|
|
const steps = [{ fromVersion: '4.0', toVersion: '5.0', downloadUrl: 'http://fw.bin', fileSize: 1024, checksum: '' }]
|
|
const engineResult = { configuration: makeConfig() } as any
|
|
|
|
const state = new FirmwareUpgradeState(steps, 0, engineResult)
|
|
const result = await state.handle(context as any)
|
|
|
|
expect(result.isComplete).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('ComplianceUpdateState — second call after provCode write', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('transitions to ExecutingCommandsState on second call', async () => {
|
|
const device = makeDevice()
|
|
// Device has a DIFFERENT provCode so first call sends SPV
|
|
device.setParameter(new DeviceParameter(
|
|
'Device.DeviceInfo.ProvisioningCode', 'old-code', ParameterType.STRING
|
|
))
|
|
|
|
const context = makeContext(device)
|
|
const engineResult = { configuration: makeConfig(), isAlreadyCompliant: true } as any
|
|
|
|
const state = new ComplianceUpdateState(engineResult)
|
|
|
|
// First call: sends SPV for provisioning code
|
|
const result1 = await state.handle(context as any)
|
|
expect(result1.response).toContain('SetParameterValues')
|
|
|
|
// Second call: after SPV response, transitions to ExecutingCommandsState
|
|
const result2 = await state.handle(context as any)
|
|
expect(result2.nextState).toBeDefined()
|
|
expect((result2.nextState as any).name).toBe('executing-commands')
|
|
})
|
|
})
|
|
|