Scrum4Me/__tests__/api/story-log.test.ts
Janpeter Visser 94f4f6ffd8
feat(PBI-33): chat-kanaal UI + lint cleanup (#145)
* feat(PBI-33): chat-kanaal UI — IdeaTimeline merge + UserChatInput

Voltooit de UI-laag van PLAN_CHAT (gebruikersvragen over plan, Claude
antwoordt async). Backend (UserQuestion model, createUserQuestionAction,
SSE-handling, server-side prop-passing) was al aanwezig — alleen de
UI-koppeling ontbrak waardoor userQuestions ongebruikt bleven.

- IdeaDetailLayout geeft userQuestions/planMd/ideaId/isDemo door aan
  IdeaTimeline en telt user-questions mee in de tab-count
- IdeaTimeline mergt user-questions chronologisch met logs+questions,
  rendert ze met MessageCircle-icoon en pending/answered status, en
  toont onderaan UserChatInput wanneer plan_md aanwezig is
- UserChatInput nieuw component met textarea + verzend-knop dat
  createUserQuestionAction aanroept en op success router.refresh()
  triggert zodat SSE de pending-state oppikt
- useNotificationsRealtime: router toegevoegd aan useEffect-deps zodat
  router.refresh() op user_question/idea-job events werkt zonder
  stale-closure waarschuwing

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(lint): unused vars/imports + react-hook-form watch incompatibility

Resolves de overige lint-warnings van de gefaalde sprint-build die los
staan van PBI-33. Eslint-config staat unused vars/args toe als ze met
'_' prefixen, dus required interface-params krijgen een prefix terwijl
losse dode constantes/imports verwijderd worden.

- sprint-header: productId is required prop maar nog niet gebruikt
  → prefix _productId i.p.v. verwijderen (caller passeert het door)
- agent-throughput: STATUSES-constante was dood — verwijderd, queries
  gebruiken hardcoded status-velden in de perDay-loop
- claude-jobs: productAccessFilter en enforceUserRateLimit waren
  dode imports — verwijderd
- story-log.test: ongebruikte 'data' binding vervangen door bare
  await res.json() zodat de stream nog wel geconsumeerd wordt
- product-dialog: form.watch('auto_pr') vervangen door useWatch met
  control-prop — useWatch is veilig voor React Compiler memoization

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-07 16:04:53 +02:00

185 lines
5.8 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
vi.mock('@/lib/prisma', () => ({
prisma: {
story: {
findFirst: vi.fn(),
},
storyLog: {
create: vi.fn(),
},
},
}))
vi.mock('@/lib/api-auth', () => ({
authenticateApiRequest: vi.fn(),
}))
import { prisma } from '@/lib/prisma'
import { authenticateApiRequest } from '@/lib/api-auth'
import { POST as postStoryLog } from '@/app/api/stories/[id]/log/route'
const mockPrisma = prisma as unknown as {
story: { findFirst: ReturnType<typeof vi.fn> }
storyLog: { create: ReturnType<typeof vi.fn> }
}
const mockAuth = authenticateApiRequest as ReturnType<typeof vi.fn>
const STORY = { id: 'story-1', product_id: 'prod-1' }
const LOG_RESULT = { id: 'log-1', created_at: new Date('2026-04-30T10:00:00Z') }
function makeRequest(body: unknown, storyId = 'story-1'): [Request, { params: Promise<{ id: string }> }] {
return [
new Request(`http://localhost/api/stories/${storyId}/log`, {
method: 'POST',
headers: { Authorization: 'Bearer test-token', 'Content-Type': 'application/json' },
body: JSON.stringify(body),
}),
{ params: Promise.resolve({ id: storyId }) },
]
}
describe('POST /api/stories/:id/log', () => {
beforeEach(() => {
vi.clearAllMocks()
mockAuth.mockResolvedValue({ userId: 'user-1', isDemo: false })
mockPrisma.story.findFirst.mockResolvedValue(STORY)
mockPrisma.storyLog.create.mockResolvedValue(LOG_RESULT)
})
// TC-L-06
it('returns 422 when type field is missing', async () => {
const res = await postStoryLog(...makeRequest({ content: 'Missing type' }))
expect(res.status).toBe(422)
})
// TC-L-07
it('returns 422 for unknown type value', async () => {
const res = await postStoryLog(...makeRequest({ type: 'UNKNOWN', content: 'test' }))
expect(res.status).toBe(422)
})
describe('type: IMPLEMENTATION_PLAN', () => {
// TC-L-08
it('returns 422 when content is missing', async () => {
const res = await postStoryLog(...makeRequest({ type: 'IMPLEMENTATION_PLAN' }))
expect(res.status).toBe(422)
})
it('returns 422 when content is empty string', async () => {
const res = await postStoryLog(...makeRequest({ type: 'IMPLEMENTATION_PLAN', content: '' }))
expect(res.status).toBe(422)
})
// TC-L-09
it('creates log entry and returns 201 with id and created_at', async () => {
const res = await postStoryLog(
...makeRequest({ type: 'IMPLEMENTATION_PLAN', content: 'Aanpak: stap 1 implementeer.' })
)
const data = await res.json()
expect(res.status).toBe(201)
expect(data).toHaveProperty('id', 'log-1')
expect(data).toHaveProperty('created_at')
expect(mockPrisma.storyLog.create).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
story_id: 'story-1',
type: 'IMPLEMENTATION_PLAN',
content: 'Aanpak: stap 1 implementeer.',
}),
})
)
})
})
describe('type: TEST_RESULT', () => {
// TC-L-10
it('returns 422 when status is missing', async () => {
const res = await postStoryLog(...makeRequest({ type: 'TEST_RESULT', content: 'Tests done' }))
expect(res.status).toBe(422)
})
// TC-L-11
it('returns 422 for invalid status value', async () => {
const res = await postStoryLog(
...makeRequest({ type: 'TEST_RESULT', content: 'Tests done', status: 'UNKNOWN' })
)
expect(res.status).toBe(422)
})
// TC-L-12
it('creates log entry with status PASSED and returns 201', async () => {
const res = await postStoryLog(
...makeRequest({ type: 'TEST_RESULT', content: 'Alle tests geslaagd.', status: 'PASSED' })
)
const data = await res.json()
expect(res.status).toBe(201)
expect(data).toHaveProperty('id', 'log-1')
expect(mockPrisma.storyLog.create).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({ type: 'TEST_RESULT', status: 'PASSED' }),
})
)
})
// TC-L-13
it('creates log entry with status FAILED and returns 201', async () => {
const res = await postStoryLog(
...makeRequest({ type: 'TEST_RESULT', content: 'Test gefaald.', status: 'FAILED' })
)
await res.json()
expect(res.status).toBe(201)
expect(mockPrisma.storyLog.create).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({ type: 'TEST_RESULT', status: 'FAILED' }),
})
)
})
})
describe('type: COMMIT', () => {
// TC-L-14
it('returns 422 when commit_hash is missing', async () => {
const res = await postStoryLog(
...makeRequest({ type: 'COMMIT', content: 'feat: done', commit_message: 'feat: ST-001' })
)
expect(res.status).toBe(422)
})
// TC-L-15
it('returns 422 when commit_message is missing', async () => {
const res = await postStoryLog(
...makeRequest({ type: 'COMMIT', content: 'feat: done', commit_hash: 'abc1234' })
)
expect(res.status).toBe(422)
})
// TC-L-16
it('creates log entry with commit fields and returns 201', async () => {
const res = await postStoryLog(
...makeRequest({
type: 'COMMIT',
content: 'feat: implementatie afgerond',
commit_hash: 'abc1234',
commit_message: 'feat(ST-001): account aanmaken',
})
)
const data = await res.json()
expect(res.status).toBe(201)
expect(data).toHaveProperty('id', 'log-1')
expect(mockPrisma.storyLog.create).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
type: 'COMMIT',
commit_hash: 'abc1234',
commit_message: 'feat(ST-001): account aanmaken',
}),
})
)
})
})
})