Scrum4Me/__tests__/api/story-log.test.ts
Janpeter Visser 43a4294424
Todo description, entity codes, REST API extensions and Claude Code hardening (ST-509/511/512/513) (#2)
* docs(ST-511): add backlog entry for entity codes feature

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

* fix(ST-511): add createWithCodeRetry helper to handle P2002 race on auto codes

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

* fix(ST-511): retry on auto-code unique conflict in story and pbi create

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

* fix(ST-511): surface field errors for code and title in PBI dialog

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

* fix(ST-511): read create-state errors in Story dialog fieldError

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

* docs(ST-512): add backlog entry for REST API code/description/implementation_plan extensions; mark ST-511 done

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

* feat(ST-512): extend REST API with code, description and implementation_plan

- GET /api/products returns code, description and definition_of_done
- GET /api/products/:id/next-story returns story.code and per-task code + implementation_plan
- GET /api/sprints/:id/tasks returns description, implementation_plan, story_code and derived per-task code
- POST /api/todos accepts and returns optional description (max 2000)

All changes are additive — existing clients ignore unknown keys.

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

* docs(ST-512): mark ST-512 as done

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

* docs(ST-513): add backlog entry for API hardening for Claude Code

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

* feat(ST-513): add task and story status mappers for API boundary

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

* feat(ST-513): expose lowercase status on API and accept lowercase in PATCH /api/tasks

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

* feat(ST-513): add metadata JSONB column to StoryLog

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

* feat(ST-513): accept optional metadata in story log and switch validation errors to 422

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

* feat(ST-513): add GET /api/health endpoint with optional db ping

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

* feat(ST-513): add GET /api/products/:id/claude-context bundled endpoint

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

* docs(ST-513): add docs/API.md and link from CLAUDE.md specs table

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

* docs(ST-513): mark ST-513 as done

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

* fix(ST-513): split 400 (malformed JSON) from 422 (validation), reject 'review'

Codex review on PR #2:

- P2.1: routes treated JSON parse failures as 422 instead of 400, breaking
  the contract in docs/API.md. Replace `request.json().catch(() => null)`
  with try/catch in 4 routes (tasks, reorder, todos, story-log) so a
  malformed body returns 400 and only well-formed-but-invalid bodies
  return 422.

- P2.2: PATCH /api/tasks/:id accepted `status: "review"`, but the sprint
  task list UI does not render REVIEW (no label/color, the cycle helper
  falls back to TO_DO). Reject `review` at the API until the sprint UI
  is extended; document the subset in docs/API.md.

Tests in __tests__/api updated for the new contract (29 assertions:
zod-failures now expect 422, status payloads use lowercase API values,
sprint-tasks mocks include the new story relation).

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-04-26 23:40:54 +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' })
)
const data = 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',
}),
})
)
})
})
})