// tests/api/devices.test.js import { describe, it, expect, beforeAll, beforeEach } from 'vitest'; import request from 'supertest'; import { createApp } from '../../server.js'; import { resetDb } from '../helpers/db.js'; import { migrateUp } from '../../lib/db/migrate.js'; let app; const owner = r => r.set('Authorization', 'Bearer test-token'); beforeAll(async () => { process.env.OWNER_TOKEN = 'test-token'; app = createApp(); }); beforeEach(async () => { await resetDb(); await migrateUp(); }); describe('/api/devices', () => { it('GET / returns known devices grouped', async () => { const res = await request(app).get('/api/devices'); expect(res.status).toBe(200); const names = res.body.groups.map(g => g.name); expect(names).toContain('Network'); const net = res.body.groups.find(g => g.name === 'Network'); expect(net.devices.some(d => d.name === 'Orbi Satellite')).toBe(true); }); it('GET /discovered requires owner and lists new devices', async () => { expect((await request(app).get('/api/devices/discovered')).status).toBe(401); const res = await owner(request(app).get('/api/devices/discovered')); expect(res.status).toBe(200); expect(res.body.some(d => d.mac === '24:4b:fe:8e:09:a4')).toBe(true); }); it('PATCH /:mac promotes + names (owner)', async () => { const res = await owner(request(app).patch('/api/devices/24:4b:fe:8e:09:a4')) .send({ name: 'ASUS Router', grp: 'Network', status: 'known' }); expect(res.status).toBe(200); expect(res.body.name).toBe('ASUS Router'); expect((await owner(request(app).get('/api/devices/discovered'))).body).toHaveLength(0); }); it('PATCH rejects a bad MAC', async () => { expect((await owner(request(app).patch('/api/devices/not-a-mac')).send({ name: 'x' })).status).toBe(400); }); });