import { afterAll, beforeAll, describe, expect, it } from 'vitest' import { createTestContext } from '../test/helpers.ts' let ctx: Awaited> beforeAll(async () => { ctx = await createTestContext() }) afterAll(() => ctx.close()) describe('sites and areas', () => { it('builds a location tree', async () => { const site = await ctx.createSite('North Campus') const building = await ctx.admin.post(`/api/sites/${site.id}/areas`, { name: 'Warehouse 04', kind: 'building' }) expect(building.status).toBe(201) const bay = await ctx.admin.post(`/api/sites/${site.id}/areas`, { name: 'Loading bay C', parentId: building.body.area.id }) expect(bay.body.area).toMatchObject({ kind: 'area', parentId: building.body.area.id }) const list = await ctx.admin.get('/api/sites') const north = list.body.sites.find((s: { name: string }) => s.name === 'North Campus') expect(north.areas.map((a: { name: string }) => a.name)).toEqual(['Loading bay C', 'Warehouse 04']) }) it('rejects a parent area from another site', async () => { const a = await ctx.createSite('Site A') const b = await ctx.createSite('Site B') const areaInA = await ctx.admin.post(`/api/sites/${a.id}/areas`, { name: 'Line 1' }) const res = await ctx.admin.post(`/api/sites/${b.id}/areas`, { name: 'Line 2', parentId: areaInA.body.area.id }) expect(res.status).toBe(400) }) it('rejects duplicate site names', async () => { await ctx.createSite('Duplicate') expect((await ctx.admin.post('/api/sites', { name: 'Duplicate' })).status).toBe(409) }) it('hides archived sites unless asked', async () => { const site = await ctx.createSite('Old Plant') await ctx.admin.patch(`/api/sites/${site.id}`, { archived: true }) const names = (await ctx.admin.get('/api/sites')).body.sites.map((s: { name: string }) => s.name) expect(names).not.toContain('Old Plant') const all = (await ctx.admin.get('/api/sites?includeArchived=true')).body.sites.map((s: { name: string }) => s.name) expect(all).toContain('Old Plant') }) it('lets everyone read but only safety managers change locations', async () => { const reporter = await ctx.createUser('reporter') const supervisor = await ctx.createUser('supervisor') expect((await reporter.get('/api/sites')).status).toBe(200) expect((await supervisor.post('/api/sites', { name: 'Nope' })).status).toBe(403) }) })