Merge pull request #3 from madhura68/feat/story-promotion-filter
feat: story-promotion + done-stories filter in get_claude_context
This commit is contained in:
commit
fd452d229e
8 changed files with 1583 additions and 9 deletions
102
__tests__/get-claude-context-filter.test.ts
Normal file
102
__tests__/get-claude-context-filter.test.ts
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
|
||||
const {
|
||||
mockProductFindFirst,
|
||||
mockSprintFindFirst,
|
||||
mockStoryFindFirst,
|
||||
mockTodoFindMany,
|
||||
} = vi.hoisted(() => ({
|
||||
mockProductFindFirst: vi.fn(),
|
||||
mockSprintFindFirst: vi.fn(),
|
||||
mockStoryFindFirst: vi.fn(),
|
||||
mockTodoFindMany: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('../src/auth.js', () => ({
|
||||
getAuth: vi.fn().mockResolvedValue({ userId: 'user-1', isDemo: false }),
|
||||
}))
|
||||
|
||||
vi.mock('../src/prisma.js', () => ({
|
||||
prisma: {
|
||||
product: { findFirst: mockProductFindFirst },
|
||||
sprint: { findFirst: mockSprintFindFirst },
|
||||
story: { findFirst: mockStoryFindFirst },
|
||||
todo: { findMany: mockTodoFindMany },
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('../src/errors.js', () => ({
|
||||
toolError: vi.fn((msg: string) => ({ isError: true, content: [{ type: 'text', text: msg }] })),
|
||||
toolJson: vi.fn((data: unknown) => ({ content: [{ type: 'text', text: JSON.stringify(data) }] })),
|
||||
withToolErrors: vi.fn(async (fn: () => Promise<unknown>) => fn()),
|
||||
}))
|
||||
|
||||
import { registerGetClaudeContextTool } from '../src/tools/get-claude-context.js'
|
||||
|
||||
// Minimal McpServer stub that captures the registered handler
|
||||
function makeServer() {
|
||||
let handler: ((args: Record<string, unknown>) => Promise<unknown>) | null = null
|
||||
const server = {
|
||||
registerTool: vi.fn((_name: string, _def: unknown, h: typeof handler) => {
|
||||
handler = h
|
||||
}),
|
||||
call: async (args: Record<string, unknown>) => {
|
||||
if (!handler) throw new Error('tool not registered')
|
||||
return handler(args)
|
||||
},
|
||||
}
|
||||
return server
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
mockProductFindFirst.mockResolvedValue({
|
||||
id: 'prod-1', code: 'P1', name: 'Test', description: null, repo_url: null, definition_of_done: null,
|
||||
})
|
||||
mockSprintFindFirst.mockResolvedValue({ id: 'sprint-1', sprint_goal: 'Goal', status: 'ACTIVE' })
|
||||
mockStoryFindFirst.mockResolvedValue(null)
|
||||
mockTodoFindMany.mockResolvedValue([])
|
||||
})
|
||||
|
||||
describe('get_claude_context safety-net filter', () => {
|
||||
it('passes OR tasks filter in next-story query', async () => {
|
||||
const server = makeServer()
|
||||
registerGetClaudeContextTool(server as never)
|
||||
await server.call({ product_id: 'prod-1' })
|
||||
|
||||
expect(mockStoryFindFirst).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: expect.objectContaining({
|
||||
OR: [
|
||||
{ tasks: { none: {} } },
|
||||
{ tasks: { some: { status: { not: 'DONE' } } } },
|
||||
],
|
||||
}),
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('still filters on sprint_id and status', async () => {
|
||||
const server = makeServer()
|
||||
registerGetClaudeContextTool(server as never)
|
||||
await server.call({ product_id: 'prod-1' })
|
||||
|
||||
expect(mockStoryFindFirst).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: expect.objectContaining({
|
||||
sprint_id: 'sprint-1',
|
||||
status: { in: ['OPEN', 'IN_SPRINT'] },
|
||||
}),
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('does not query next story when no active sprint', async () => {
|
||||
mockSprintFindFirst.mockResolvedValue(null)
|
||||
const server = makeServer()
|
||||
registerGetClaudeContextTool(server as never)
|
||||
await server.call({ product_id: 'prod-1' })
|
||||
|
||||
expect(mockStoryFindFirst).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
136
__tests__/tasks-status-update.test.ts
Normal file
136
__tests__/tasks-status-update.test.ts
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
|
||||
vi.mock('../src/prisma.js', () => ({
|
||||
prisma: {
|
||||
task: {
|
||||
update: vi.fn(),
|
||||
findMany: vi.fn(),
|
||||
},
|
||||
story: {
|
||||
findUniqueOrThrow: vi.fn(),
|
||||
update: vi.fn(),
|
||||
},
|
||||
$transaction: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
import { prisma } from '../src/prisma.js'
|
||||
import { updateTaskStatusWithStoryPromotion } from '../src/lib/tasks-status-update.js'
|
||||
|
||||
const mockPrisma = prisma as unknown as {
|
||||
task: { update: ReturnType<typeof vi.fn>; findMany: ReturnType<typeof vi.fn> }
|
||||
story: { findUniqueOrThrow: ReturnType<typeof vi.fn>; update: ReturnType<typeof vi.fn> }
|
||||
$transaction: ReturnType<typeof vi.fn>
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
mockPrisma.$transaction.mockImplementation(
|
||||
async (run: (tx: typeof prisma) => Promise<unknown>) => run(prisma),
|
||||
)
|
||||
})
|
||||
|
||||
const TASK_BASE = {
|
||||
id: 'task-1',
|
||||
title: 'Task',
|
||||
story_id: 'story-1',
|
||||
implementation_plan: null,
|
||||
}
|
||||
|
||||
describe('updateTaskStatusWithStoryPromotion', () => {
|
||||
it('promotes story to DONE when last sibling task transitions to DONE', async () => {
|
||||
mockPrisma.task.update.mockResolvedValue({ ...TASK_BASE, status: 'DONE' })
|
||||
mockPrisma.task.findMany.mockResolvedValue([{ status: 'DONE' }, { status: 'DONE' }])
|
||||
mockPrisma.story.findUniqueOrThrow.mockResolvedValue({ status: 'IN_SPRINT' })
|
||||
|
||||
const result = await updateTaskStatusWithStoryPromotion('task-1', 'DONE')
|
||||
|
||||
expect(result.storyStatusChange).toBe('promoted')
|
||||
expect(result.storyId).toBe('story-1')
|
||||
expect(mockPrisma.story.update).toHaveBeenCalledWith({
|
||||
where: { id: 'story-1' },
|
||||
data: { status: 'DONE' },
|
||||
})
|
||||
})
|
||||
|
||||
it('does not promote when story is already DONE (idempotent)', async () => {
|
||||
mockPrisma.task.update.mockResolvedValue({ ...TASK_BASE, status: 'DONE' })
|
||||
mockPrisma.task.findMany.mockResolvedValue([{ status: 'DONE' }])
|
||||
mockPrisma.story.findUniqueOrThrow.mockResolvedValue({ status: 'DONE' })
|
||||
|
||||
const result = await updateTaskStatusWithStoryPromotion('task-1', 'DONE')
|
||||
|
||||
expect(result.storyStatusChange).toBe(null)
|
||||
expect(mockPrisma.story.update).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('does not promote when not all siblings are DONE', async () => {
|
||||
mockPrisma.task.update.mockResolvedValue({ ...TASK_BASE, status: 'DONE' })
|
||||
mockPrisma.task.findMany.mockResolvedValue([{ status: 'DONE' }, { status: 'IN_PROGRESS' }])
|
||||
mockPrisma.story.findUniqueOrThrow.mockResolvedValue({ status: 'IN_SPRINT' })
|
||||
|
||||
const result = await updateTaskStatusWithStoryPromotion('task-1', 'DONE')
|
||||
|
||||
expect(result.storyStatusChange).toBe(null)
|
||||
expect(mockPrisma.story.update).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('demotes story to IN_SPRINT when a task moves out of DONE on a DONE story', async () => {
|
||||
mockPrisma.task.update.mockResolvedValue({ ...TASK_BASE, status: 'IN_PROGRESS' })
|
||||
mockPrisma.task.findMany.mockResolvedValue([{ status: 'IN_PROGRESS' }, { status: 'DONE' }])
|
||||
mockPrisma.story.findUniqueOrThrow.mockResolvedValue({ status: 'DONE' })
|
||||
|
||||
const result = await updateTaskStatusWithStoryPromotion('task-1', 'IN_PROGRESS')
|
||||
|
||||
expect(result.storyStatusChange).toBe('demoted')
|
||||
expect(mockPrisma.story.update).toHaveBeenCalledWith({
|
||||
where: { id: 'story-1' },
|
||||
data: { status: 'IN_SPRINT' },
|
||||
})
|
||||
})
|
||||
|
||||
it('does not demote when story is not DONE', async () => {
|
||||
mockPrisma.task.update.mockResolvedValue({ ...TASK_BASE, status: 'IN_PROGRESS' })
|
||||
mockPrisma.task.findMany.mockResolvedValue([{ status: 'IN_PROGRESS' }])
|
||||
mockPrisma.story.findUniqueOrThrow.mockResolvedValue({ status: 'IN_SPRINT' })
|
||||
|
||||
const result = await updateTaskStatusWithStoryPromotion('task-1', 'IN_PROGRESS')
|
||||
|
||||
expect(result.storyStatusChange).toBe(null)
|
||||
expect(mockPrisma.story.update).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('updates the task regardless of story-status change', async () => {
|
||||
mockPrisma.task.update.mockResolvedValue({ ...TASK_BASE, status: 'IN_PROGRESS' })
|
||||
mockPrisma.task.findMany.mockResolvedValue([{ status: 'IN_PROGRESS' }])
|
||||
mockPrisma.story.findUniqueOrThrow.mockResolvedValue({ status: 'IN_SPRINT' })
|
||||
|
||||
await updateTaskStatusWithStoryPromotion('task-1', 'IN_PROGRESS')
|
||||
|
||||
expect(mockPrisma.task.update).toHaveBeenCalledWith({
|
||||
where: { id: 'task-1' },
|
||||
data: { status: 'IN_PROGRESS' },
|
||||
select: expect.any(Object),
|
||||
})
|
||||
})
|
||||
|
||||
it('uses the provided transaction client when passed', async () => {
|
||||
const tx = {
|
||||
task: { update: vi.fn(), findMany: vi.fn() },
|
||||
story: { findUniqueOrThrow: vi.fn(), update: vi.fn() },
|
||||
}
|
||||
tx.task.update.mockResolvedValue({ ...TASK_BASE, status: 'DONE' })
|
||||
tx.task.findMany.mockResolvedValue([{ status: 'DONE' }])
|
||||
tx.story.findUniqueOrThrow.mockResolvedValue({ status: 'IN_SPRINT' })
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const result = await updateTaskStatusWithStoryPromotion('task-1', 'DONE', tx as any)
|
||||
|
||||
expect(result.storyStatusChange).toBe('promoted')
|
||||
expect(mockPrisma.$transaction).not.toHaveBeenCalled()
|
||||
expect(tx.story.update).toHaveBeenCalledWith({
|
||||
where: { id: 'story-1' },
|
||||
data: { status: 'DONE' },
|
||||
})
|
||||
})
|
||||
})
|
||||
1251
package-lock.json
generated
1251
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -18,7 +18,8 @@
|
|||
"prisma:generate": "prisma generate",
|
||||
"postinstall": "prisma generate || true",
|
||||
"typecheck": "tsc --noEmit",
|
||||
"sync-schema": "bash scripts/sync-schema.sh"
|
||||
"sync-schema": "bash scripts/sync-schema.sh",
|
||||
"test": "vitest run"
|
||||
},
|
||||
"keywords": [
|
||||
"mcp",
|
||||
|
|
@ -39,6 +40,7 @@
|
|||
"@types/pg": "^8.11.10",
|
||||
"prisma": "^7.8.0",
|
||||
"tsx": "^4.19.0",
|
||||
"typescript": "^5.7.0"
|
||||
"typescript": "^5.7.0",
|
||||
"vitest": "^4.1.5"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
72
src/lib/tasks-status-update.ts
Normal file
72
src/lib/tasks-status-update.ts
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
import type { Prisma, TaskStatus } from '@prisma/client'
|
||||
import { prisma } from '../prisma.js'
|
||||
|
||||
export type StoryStatusChange = 'promoted' | 'demoted' | null
|
||||
|
||||
export interface UpdateTaskStatusResult {
|
||||
task: {
|
||||
id: string
|
||||
title: string
|
||||
status: TaskStatus
|
||||
story_id: string
|
||||
implementation_plan: string | null
|
||||
}
|
||||
storyStatusChange: StoryStatusChange
|
||||
storyId: string
|
||||
}
|
||||
|
||||
// Update task.status atomically and auto-promote/demote the parent story:
|
||||
// - All sibling tasks DONE → story.status = DONE
|
||||
// - Story was DONE and a task moves out of DONE → story.status = IN_SPRINT
|
||||
// Demote target is IN_SPRINT (not OPEN): OPEN means "back in product backlog",
|
||||
// which is a sprint-management action, not a status side-effect.
|
||||
export async function updateTaskStatusWithStoryPromotion(
|
||||
taskId: string,
|
||||
newStatus: TaskStatus,
|
||||
client?: Prisma.TransactionClient,
|
||||
): Promise<UpdateTaskStatusResult> {
|
||||
const run = async (tx: Prisma.TransactionClient): Promise<UpdateTaskStatusResult> => {
|
||||
const task = await tx.task.update({
|
||||
where: { id: taskId },
|
||||
data: { status: newStatus },
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
status: true,
|
||||
story_id: true,
|
||||
implementation_plan: true,
|
||||
},
|
||||
})
|
||||
|
||||
const siblings = await tx.task.findMany({
|
||||
where: { story_id: task.story_id },
|
||||
select: { status: true },
|
||||
})
|
||||
const allDone = siblings.every((s) => s.status === 'DONE')
|
||||
|
||||
const story = await tx.story.findUniqueOrThrow({
|
||||
where: { id: task.story_id },
|
||||
select: { status: true },
|
||||
})
|
||||
|
||||
let storyStatusChange: StoryStatusChange = null
|
||||
if (newStatus === 'DONE' && allDone && story.status !== 'DONE') {
|
||||
await tx.story.update({
|
||||
where: { id: task.story_id },
|
||||
data: { status: 'DONE' },
|
||||
})
|
||||
storyStatusChange = 'promoted'
|
||||
} else if (newStatus !== 'DONE' && story.status === 'DONE') {
|
||||
await tx.story.update({
|
||||
where: { id: task.story_id },
|
||||
data: { status: 'IN_SPRINT' },
|
||||
})
|
||||
storyStatusChange = 'demoted'
|
||||
}
|
||||
|
||||
return { task, storyStatusChange, storyId: task.story_id }
|
||||
}
|
||||
|
||||
if (client) return run(client)
|
||||
return prisma.$transaction(run)
|
||||
}
|
||||
|
|
@ -58,6 +58,10 @@ export function registerGetClaudeContextTool(server: McpServer) {
|
|||
where: {
|
||||
sprint_id: activeSprint.id,
|
||||
status: { in: ['OPEN', 'IN_SPRINT'] },
|
||||
OR: [
|
||||
{ tasks: { none: {} } },
|
||||
{ tasks: { some: { status: { not: 'DONE' } } } },
|
||||
],
|
||||
},
|
||||
orderBy: [{ priority: 'asc' }, { sort_order: 'asc' }],
|
||||
select: {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
import { z } from 'zod'
|
||||
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
|
||||
import { prisma } from '../prisma.js'
|
||||
import { requireWriteAccess } from '../auth.js'
|
||||
import { userCanAccessTask } from '../access.js'
|
||||
import { toolError, toolJson, withToolErrors } from '../errors.js'
|
||||
import { TASK_STATUS_API_VALUES, taskStatusFromApi, taskStatusToApi } from '../status.js'
|
||||
import { updateTaskStatusWithStoryPromotion } from '../lib/tasks-status-update.js'
|
||||
|
||||
const inputSchema = z.object({
|
||||
task_id: z.string().min(1),
|
||||
|
|
@ -31,15 +31,15 @@ export function registerUpdateTaskStatusTool(server: McpServer) {
|
|||
if (!(await userCanAccessTask(task_id, auth.userId))) {
|
||||
return toolError(`Task ${task_id} not found or not accessible`)
|
||||
}
|
||||
const task = await prisma.task.update({
|
||||
where: { id: task_id },
|
||||
data: { status: dbStatus },
|
||||
select: { id: true, status: true, implementation_plan: true },
|
||||
})
|
||||
const { task, storyStatusChange } = await updateTaskStatusWithStoryPromotion(
|
||||
task_id,
|
||||
dbStatus,
|
||||
)
|
||||
return toolJson({
|
||||
id: task.id,
|
||||
status: taskStatusToApi(task.status),
|
||||
implementation_plan: task.implementation_plan,
|
||||
story_status_change: storyStatusChange,
|
||||
})
|
||||
}),
|
||||
)
|
||||
|
|
|
|||
9
vitest.config.ts
Normal file
9
vitest.config.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import { defineConfig } from 'vitest/config'
|
||||
|
||||
export default defineConfig({
|
||||
test: {
|
||||
environment: 'node',
|
||||
include: ['__tests__/**/*.test.ts'],
|
||||
exclude: ['vendor/**', 'node_modules/**'],
|
||||
},
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue