import { test, after } from 'node:test'; import assert from 'node:assert/strict'; import { newApp } from './helpers.js'; test('server-types: full CRUD', async (t) => { const app = await newApp(); after(() => app.close()); const created = await app.inject({ method: 'POST', url: '/api/server-types', payload: { name: 'Database' }, }); assert.equal(created.statusCode, 201); const t1 = JSON.parse(created.body); const dup = await app.inject({ method: 'POST', url: '/api/server-types', payload: { name: 'Database' }, }); assert.equal(dup.statusCode, 409); const upd = await app.inject({ method: 'PUT', url: `/api/server-types/${t1.id}`, payload: { name: 'DB' }, }); assert.equal(JSON.parse(upd.body).name, 'DB'); const del = await app.inject({ method: 'DELETE', url: `/api/server-types/${t1.id}` }); assert.equal(del.statusCode, 204); }); test('server-types: cannot delete one referenced by a host (409)', async (t) => { const app = await newApp(); after(() => app.close()); const site = JSON.parse((await app.inject({ method: 'POST', url: '/api/sites', payload: { name: 'S' } })).body); const room = JSON.parse((await app.inject({ method: 'POST', url: '/api/rooms', payload: { site_id: site.id, name: 'R' } })).body); const type = JSON.parse((await app.inject({ method: 'POST', url: '/api/server-types', payload: { name: 'Web' } })).body); await app.inject({ method: 'POST', url: '/api/hosts', payload: { hardware_id: 'HW', hostname: 'h', asset_id: 'A', room_id: room.id, position: '', server_type_id: type.id, }, }); const r = await app.inject({ method: 'DELETE', url: `/api/server-types/${type.id}` }); assert.equal(r.statusCode, 409); });