import { describe, it, expect, beforeEach } from 'vitest'; import { createApp } from '../../src/app'; import { authToken, bearer, resetDb } from '../helpers'; describe('activities lifecycle (admin create → reorder → worker sees order → delete)', () => { beforeEach(resetDb); it('creates, reorders, reflects the order to a worker, then removes one', async () => { const app = createApp(); const adminToken = await authToken(app, 'act-lc-admin@example.com', 'admin'); const workerToken = await authToken(app, 'act-lc-worker@example.com'); // Admin creates three activities; they append in creation order. const names = ['Zagen', 'Frezen', 'Slijpen']; const created: Array<{ id: number; name: string }> = []; for (const name of names) { const res = await app.request('/api/activities', { method: 'POST', headers: bearer(adminToken), body: JSON.stringify({ name, insole_types: ['Kurk'] }), }); expect(res.status).toBe(200); created.push(await res.json()); } expect(created.map((a) => a.name)).toEqual(names); // Admin reverses the order. const reversedIds = created.map((a) => a.id).reverse(); const reorderRes = await app.request('/api/activities/reorder', { method: 'PUT', headers: bearer(adminToken), body: JSON.stringify({ ids: reversedIds }), }); expect(reorderRes.status).toBe(200); // A worker reads the list and sees the new order (sorted by sort_order). const workerListRes = await app.request('/api/activities', { headers: bearer(workerToken) }); expect(workerListRes.status).toBe(200); const workerList: Array<{ id: number; name: string; sort_order: number }> = await workerListRes.json(); expect(workerList.map((a) => a.id)).toEqual(reversedIds); expect(workerList.map((a) => a.name)).toEqual(['Slijpen', 'Frezen', 'Zagen']); for (let i = 1; i < workerList.length; i++) { expect(workerList[i].sort_order).toBeGreaterThanOrEqual(workerList[i - 1].sort_order); } // Admin deletes the middle activity; it disappears from the list. const toDelete = created[1]; // Frezen const delRes = await app.request(`/api/activities/${toDelete.id}`, { method: 'DELETE', headers: bearer(adminToken), }); expect(delRes.status).toBe(200); expect(await delRes.json()).toEqual({ success: true }); const afterRes = await app.request('/api/activities', { headers: bearer(workerToken) }); const after: Array<{ id: number }> = await afterRes.json(); expect(after.map((a) => a.id)).toEqual([created[2].id, created[0].id]); expect(after.some((a) => a.id === toDelete.id)).toBe(false); }); });