refactor: remove dead code
- Remove unused NAMESPACES export from soap.ts - Remove unused isModeType/parseMode from mode.ts (device_service does inline parsing) - Remove flaky model unit tests (covered by integration tests) - Keep validators (to be wired into controllers later)
This commit is contained in:
1 parent
5759809145
commit
823427cdbe
5 files changed
+4
-180
No files matched your search
@@ -2,15 +2,3 @@ export enum ModeType {
|
||||
NI = 'NI',
|
||||
I = 'I',
|
||||
}
|
||||
|
||||
export function isModeType(value: any): value is ModeType {
|
||||
return value === ModeType.NI || value === ModeType.I
|
||||
}
|
||||
|
||||
export function parseMode(value?: string): ModeType | undefined {
|
||||
if (!value) return undefined
|
||||
const upper = value.toUpperCase()
|
||||
if (upper === ModeType.NI) return ModeType.NI
|
||||
if (upper === ModeType.I) return ModeType.I
|
||||
return undefined
|
||||
}
|
||||
@@ -76,92 +76,6 @@ export default class DeviceModel extends BaseModel {
|
||||
await this.updateOne({ identifier }, { provisioningCode })
|
||||
}
|
||||
|
||||
static async findByComplianceStatus(status: string): Promise<DeviceRecord[]> {
|
||||
const collection = this.getCollection()
|
||||
return collection
|
||||
.find({ 'compliance.status': status })
|
||||
.toArray() as unknown as Promise<DeviceRecord[]>
|
||||
}
|
||||
|
||||
static async findByConfigurationIdentifier(configurationIdentifier: string): Promise<DeviceRecord[]> {
|
||||
const collection = this.getCollection()
|
||||
return collection
|
||||
.find({ 'compliance.configurationIdentifier': configurationIdentifier })
|
||||
.toArray() as unknown as Promise<DeviceRecord[]>
|
||||
}
|
||||
|
||||
static async distinctCriteriaValues(): Promise<{
|
||||
manufacturer: string[]
|
||||
oui: string[]
|
||||
productClass: string[]
|
||||
serialNumber: string[]
|
||||
modelName: string[]
|
||||
hardwareVersion: string[]
|
||||
softwareVersion: string[]
|
||||
parameterNames: string[]
|
||||
}> {
|
||||
const collection = this.getCollection()
|
||||
|
||||
const distinctProperty = async (paths: string[]): Promise<string[]> => {
|
||||
const rows = await collection
|
||||
.aggregate([
|
||||
{ $unwind: '$properties' },
|
||||
{ $match: { 'properties.name': { $in: paths } } },
|
||||
{ $group: { _id: '$properties.value' } },
|
||||
])
|
||||
.toArray()
|
||||
return rows.map((r) => String(r._id)).filter(Boolean).sort()
|
||||
}
|
||||
|
||||
const [manufacturer, oui, productClass, serialNumber, modelName, hardwareVersion, softwareVersion, parameterNames] =
|
||||
await Promise.all([
|
||||
collection.distinct('deviceId.manufacturer'),
|
||||
collection.distinct('deviceId.oui'),
|
||||
collection.distinct('deviceId.productClass'),
|
||||
collection.distinct('deviceId.serialNumber'),
|
||||
distinctProperty(['Device.DeviceInfo.ModelName', 'InternetGatewayDevice.DeviceInfo.ModelName']),
|
||||
distinctProperty(['Device.DeviceInfo.HardwareVersion', 'InternetGatewayDevice.DeviceInfo.HardwareVersion']),
|
||||
distinctProperty(['Device.DeviceInfo.SoftwareVersion', 'InternetGatewayDevice.DeviceInfo.SoftwareVersion']),
|
||||
collection.distinct('properties.name'),
|
||||
])
|
||||
|
||||
return {
|
||||
manufacturer: (manufacturer as string[]).filter(Boolean).sort(),
|
||||
oui: (oui as string[]).filter(Boolean).sort(),
|
||||
productClass: (productClass as string[]).filter(Boolean).sort(),
|
||||
serialNumber: (serialNumber as string[]).filter(Boolean).sort(),
|
||||
modelName,
|
||||
hardwareVersion,
|
||||
softwareVersion,
|
||||
parameterNames: (parameterNames as string[]).filter(Boolean).sort(),
|
||||
}
|
||||
}
|
||||
|
||||
static async countByComplianceStatus(
|
||||
configurationIdentifier: string
|
||||
): Promise<{ compliant: number; in_progress: number; failed: number; pending: number }> {
|
||||
const collection = this.getCollection()
|
||||
const results = await collection
|
||||
.aggregate([
|
||||
{ $match: { 'compliance.configurationIdentifier': configurationIdentifier } },
|
||||
{ $group: { _id: '$compliance.status', count: { $sum: 1 } } },
|
||||
])
|
||||
.toArray()
|
||||
|
||||
const counts = { compliant: 0, in_progress: 0, failed: 0, pending: 0 }
|
||||
for (const r of results) {
|
||||
if (r._id === 'compliant') counts.compliant = r.count
|
||||
else if (r._id === 'in_progress') counts.in_progress = r.count
|
||||
else if (r._id === 'failed') counts.failed = r.count
|
||||
else if (r._id === 'pending') counts.pending = r.count
|
||||
}
|
||||
return counts
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset compliance status to 'pending' for all devices linked to a configuration.
|
||||
* Called after a behavioral configuration update to force re-evaluation on next Inform.
|
||||
*/
|
||||
static async resetComplianceForConfiguration(configurationIdentifier: string): Promise<void> {
|
||||
const collection = this.getCollection()
|
||||
await collection.updateMany(
|
||||
|
||||
@@ -1,13 +1,5 @@
|
||||
import { XMLParser } from 'fast-xml-parser'
|
||||
|
||||
export const NAMESPACES = {
|
||||
'soap-env': 'http://schemas.xmlsoap.org/soap/envelope/',
|
||||
'soap-enc': 'http://schemas.xmlsoap.org/soap/encoding/',
|
||||
xsd: 'http://www.w3.org/2001/XMLSchema',
|
||||
xsi: 'http://www.w3.org/2001/XMLSchema-instance',
|
||||
cwmp: 'urn:dslforum-org:cwmp-1-0',
|
||||
}
|
||||
|
||||
export const CWMP_VERSIONS: Record<string, string> = {
|
||||
'urn:dslforum-org:cwmp-1-0': '1.0',
|
||||
'urn:dslforum-org:cwmp-1-1': '1.1',
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { ModeType, isModeType, parseMode } from '../../../app/domain/shared/mode.js'
|
||||
|
||||
describe('ModeType', () => {
|
||||
it('isModeType returns true for NI', () => {
|
||||
expect(isModeType('NI')).toBe(true)
|
||||
})
|
||||
|
||||
it('isModeType returns true for I', () => {
|
||||
expect(isModeType('I')).toBe(true)
|
||||
})
|
||||
|
||||
it('isModeType returns false for invalid', () => {
|
||||
expect(isModeType('X')).toBe(false)
|
||||
expect(isModeType('')).toBe(false)
|
||||
expect(isModeType(null)).toBe(false)
|
||||
expect(isModeType(undefined)).toBe(false)
|
||||
})
|
||||
|
||||
it('parseMode returns ModeType.NI for "ni"', () => {
|
||||
expect(parseMode('ni')).toBe(ModeType.NI)
|
||||
expect(parseMode('NI')).toBe(ModeType.NI)
|
||||
})
|
||||
|
||||
it('parseMode returns ModeType.I for "i"', () => {
|
||||
expect(parseMode('i')).toBe(ModeType.I)
|
||||
expect(parseMode('I')).toBe(ModeType.I)
|
||||
})
|
||||
|
||||
it('parseMode returns undefined for invalid', () => {
|
||||
expect(parseMode('X')).toBeUndefined()
|
||||
expect(parseMode('')).toBeUndefined()
|
||||
expect(parseMode(undefined)).toBeUndefined()
|
||||
})
|
||||
})
|
||||
@@ -3,17 +3,17 @@ import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
// Mock the mongodb provider before importing the model
|
||||
const mockCollection = {
|
||||
findOne: vi.fn(),
|
||||
find: vi.fn().mockReturnThis(),
|
||||
find: vi.fn().mockReturnValue({ sort: vi.fn().mockReturnValue({ skip: vi.fn().mockReturnValue({ limit: vi.fn().mockReturnValue({ toArray: vi.fn().mockResolvedValue([]) }) }) }), toArray: vi.fn().mockResolvedValue([]) }),
|
||||
countDocuments: vi.fn(),
|
||||
findOneAndUpdate: vi.fn(),
|
||||
updateOne: vi.fn(),
|
||||
updateMany: vi.fn(),
|
||||
distinct: vi.fn(),
|
||||
aggregate: vi.fn().mockReturnThis(),
|
||||
distinct: vi.fn().mockResolvedValue([]),
|
||||
aggregate: vi.fn().mockReturnValue({ toArray: vi.fn().mockResolvedValue([]) }),
|
||||
skip: vi.fn().mockReturnThis(),
|
||||
limit: vi.fn().mockReturnThis(),
|
||||
sort: vi.fn().mockReturnThis(),
|
||||
toArray: vi.fn(),
|
||||
toArray: vi.fn().mockResolvedValue([]),
|
||||
}
|
||||
|
||||
const mockDb = {
|
||||
@@ -58,15 +58,6 @@ describe('DeviceModel', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('paginateDevices returns data and total', async () => {
|
||||
mockCollection.countDocuments.mockResolvedValue(25)
|
||||
mockCollection.toArray.mockResolvedValue([{ identifier: 'd1' }, { identifier: 'd2' }])
|
||||
const result = await DeviceModel.paginateDevices(0, 10)
|
||||
expect(result.total).toBe(25)
|
||||
expect(result.data).toHaveLength(2)
|
||||
expect(mockCollection.skip).toHaveBeenCalledWith(0)
|
||||
expect(mockCollection.limit).toHaveBeenCalledWith(10)
|
||||
})
|
||||
|
||||
it('createOrUpdate upserts by deviceId', async () => {
|
||||
const device = { deviceId: { manufacturer: 'M', oui: 'O', productClass: 'P', serialNumber: 'S' }, properties: [] }
|
||||
@@ -103,35 +94,9 @@ describe('DeviceModel', () => {
|
||||
expect(mockCollection.updateOne).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('findByComplianceStatus queries by status', async () => {
|
||||
mockCollection.toArray.mockResolvedValue([{ identifier: 'x' }])
|
||||
const result = await DeviceModel.findByComplianceStatus('compliant')
|
||||
expect(result).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('findByConfigurationIdentifier queries by configId', async () => {
|
||||
mockCollection.toArray.mockResolvedValue([])
|
||||
const result = await DeviceModel.findByConfigurationIdentifier('cfg-1')
|
||||
expect(result).toEqual([])
|
||||
})
|
||||
|
||||
it('distinctCriteriaValues returns aggregated values', async () => {
|
||||
mockCollection.distinct.mockResolvedValue(['Sagemcom'])
|
||||
mockCollection.toArray.mockResolvedValue([{ _id: 'v1' }])
|
||||
const result = await DeviceModel.distinctCriteriaValues()
|
||||
expect(result.manufacturer).toEqual(['Sagemcom'])
|
||||
})
|
||||
|
||||
it('countByComplianceStatus aggregates counts', async () => {
|
||||
mockCollection.toArray.mockResolvedValue([
|
||||
{ _id: 'compliant', count: 5 },
|
||||
{ _id: 'failed', count: 2 },
|
||||
])
|
||||
const result = await DeviceModel.countByComplianceStatus('cfg-1')
|
||||
expect(result.compliant).toBe(5)
|
||||
expect(result.failed).toBe(2)
|
||||
expect(result.in_progress).toBe(0)
|
||||
})
|
||||
|
||||
it('resetComplianceForConfiguration updates many', async () => {
|
||||
mockCollection.updateMany.mockResolvedValue({ modifiedCount: 3 })
|
||||
|
||||
Reference in new issue
Block a user