From 39b4293910e4d0e08a0fd5fb1031f6fe7b75e81d Mon Sep 17 00:00:00 2001 From: dimakis Date: Sat, 4 Jul 2026 11:47:19 +0100 Subject: [PATCH] fix: workload promote test and status code issues - Fix vacuous broadcast test: use setWorkloadBroadcast() instead of monkey-patching non-existent app._workloadBroadcast property - Add missing test for promote with description but no title - Return 400 (not 404) when item exists but has no title Co-Authored-By: Claude Opus 4.6 --- server/__tests__/workload-routes.test.ts | 16 +++++++++++++--- server/app.ts | 3 ++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/server/__tests__/workload-routes.test.ts b/server/__tests__/workload-routes.test.ts index 13caa804..928f21c3 100644 --- a/server/__tests__/workload-routes.test.ts +++ b/server/__tests__/workload-routes.test.ts @@ -518,6 +518,15 @@ describe('workload routes', () => { expect(res.status).toBe(404); }); + it('POST /api/workload/items/:id/promote — returns 404 for missing item with description but no title', async () => { + const res = await request(app) + .post('/api/workload/items/nonexistent-id/promote') + .set('Cookie', authCookie) + .send({ description: 'has context but no title' }); + + expect(res.status).toBe(404); + }); + it('POST /api/workload/items/:id/promote — creates task from body fallback (Telos item)', async () => { const res = await request(app) .post('/api/workload/items/telos-item-123/promote') @@ -546,9 +555,9 @@ describe('workload routes', () => { }); it('POST /api/workload/items/:id/promote — fallback does not broadcast workload update', async () => { + const { setWorkloadBroadcast } = await import('../app.js'); const broadcasts: unknown[] = []; - const origBroadcast = (app as any)._workloadBroadcast; - (app as any)._workloadBroadcast = (msg: unknown) => broadcasts.push(msg); + setWorkloadBroadcast((msg: Record) => broadcasts.push(msg)); await request(app) .post('/api/workload/items/telos-no-broadcast/promote') @@ -558,7 +567,8 @@ describe('workload routes', () => { const workloadBroadcasts = broadcasts.filter((b: any) => b.type === 'workload_item_updated'); expect(workloadBroadcasts).toHaveLength(0); - (app as any)._workloadBroadcast = origBroadcast; + // Reset — no broadcast callback in test environment + setWorkloadBroadcast(() => {}); }); it('POST /api/workload/items/:id/promote — broadcasts workload_item_updated', async () => { diff --git a/server/app.ts b/server/app.ts index 72bd0f9e..400ebce1 100644 --- a/server/app.ts +++ b/server/app.ts @@ -1921,7 +1921,8 @@ app.post('/api/workload/items/:id/promote', (req, res) => { // Resolve title and context from workloadStore item or fallback body data (Telos items) const title = item?.title ?? body.data.title; if (!title) { - res.status(404).json({ error: 'Item not found and no title provided' }); + const status = item ? 400 : 404; + res.status(status).json({ error: item ? 'No title provided' : 'Item not found and no title provided' }); return; }