import { describe, it, expect, beforeAll, afterAll, beforeEach } from 'vitest' import { MongoClient, Db } from 'mongodb' import { DeviceId } from '../../../app/domain/device/device_id.js' const MONGO_URI = 'mongodb://adam:adam@localhost:27017/adam-acs-test?authSource=admin' let client: MongoClient let db: Db describe('Device Model (integration)', () => { beforeAll(async () => { client = new MongoClient(MONGO_URI) await client.connect() db = client.db('adam-acs-test') }) afterAll(async () => { await db.dropDatabase() await client.close() }) beforeEach(async () => { await db.collection('devices').deleteMany({}) }) it('creates and retrieves a device', async () => { const col = db.collection('devices') const device = { identifier: 'test-hash-001', deviceId: { manufacturer: 'Sagemcom', oui: 'F8B7FF', productClass: 'Livebox', serialNumber: 'SN001' }, properties: [{ name: 'Device.DeviceInfo.Manufacturer', value: 'Sagemcom', type: 'xsd:string' }], parameters: [], gpnStatus: 'unknown', dataModel: 'tr-181', createdAt: new Date(), updatedAt: new Date(), } await col.insertOne(device) const found = await col.findOne({ identifier: 'test-hash-001' }) expect(found).not.toBeNull() expect(found!.deviceId.manufacturer).toBe('Sagemcom') expect(found!.properties).toHaveLength(1) }) it('updates device properties', async () => { const col = db.collection('devices') await col.insertOne({ identifier: 'dev-002', deviceId: { manufacturer: 'Huawei', oui: '202BC1', productClass: 'BM632w', serialNumber: '000000' }, properties: [{ name: 'InternetGateway.DeviceInfo.SW', value: '1.0', type: 'xsd:string' }], parameters: [], gpnStatus: 'supported', createdAt: new Date(), updatedAt: new Date(), }) await col.updateOne( { identifier: 'dev-002' }, { $set: { 'properties.0.value': '2.0', updatedAt: new Date() } } ) const found = await col.findOne({ identifier: 'dev-002' }) expect(found!.properties[0].value).toBe('2.0') }) it('stores and retrieves references', async () => { const col = db.collection('devices') await col.insertOne({ identifier: 'dev-003', deviceId: { manufacturer: 'Test', oui: 'AABB', productClass: 'X', serialNumber: 'SN' }, properties: [], parameters: [], references: [ { provider: 'wassup', status: 'ok', resolvedAt: new Date(), resolvedBy: 'ip:10.0.0.1', properties: { sai: '123', spr: 'FR' } }, ], createdAt: new Date(), updatedAt: new Date(), }) const found = await col.findOne({ identifier: 'dev-003' }) expect(found!.references).toHaveLength(1) expect(found!.references[0].provider).toBe('wassup') expect(found!.references[0].properties.sai).toBe('123') }) it('deletes a device', async () => { const col = db.collection('devices') await col.insertOne({ identifier: 'dev-del', deviceId: {}, properties: [], parameters: [] }) const result = await col.deleteOne({ identifier: 'dev-del' }) expect(result.deletedCount).toBe(1) }) it('paginates devices', async () => { const col = db.collection('devices') await col.deleteMany({}) // explicit clean const docs = Array.from({ length: 15 }, (_, i) => ({ identifier: `dev-${i}`, deviceId: { manufacturer: 'M', oui: 'O', productClass: 'P', serialNumber: `S${i}` }, properties: [], parameters: [], })) await col.insertMany(docs) const total = await col.countDocuments({}) expect(total).toBe(15) const page1 = await col.find({}).skip(0).limit(10).toArray() expect(page1).toHaveLength(10) const page2 = await col.find({}).skip(10).limit(10).toArray() expect(page2).toHaveLength(5) }) it('finds device by deviceId fields', async () => { const col = db.collection('devices') await col.insertOne({ identifier: 'dev-find', deviceId: { manufacturer: 'Sagemcom', oui: 'F8B7FF', productClass: 'LB6', serialNumber: 'UNIQUE-SN' }, properties: [], parameters: [], }) const found = await col.findOne({ 'deviceId.serialNumber': 'UNIQUE-SN' }) expect(found).not.toBeNull() expect(found!.identifier).toBe('dev-find') }) it('updates references array', async () => { const col = db.collection('devices') await col.insertOne({ identifier: 'dev-ref', deviceId: {}, properties: [], parameters: [], references: [] }) const newRefs = [ { provider: 'wassup', status: 'ok', resolvedAt: new Date(), resolvedBy: 'ip:1.2.3.4', properties: { sai: '999' } }, ] await col.updateOne({ identifier: 'dev-ref' }, { $set: { references: newRefs } }) const found = await col.findOne({ identifier: 'dev-ref' }) expect(found!.references[0].properties.sai).toBe('999') }) })