/** * I-002: Stage Config — Invalid Config Schema */ import { describe, it, expect, beforeAll, afterAll } from 'vitest' import { prisma, createTestContext } from '../setup' import { createTestUser, createTestProgram, createTestPipeline, createTestTrack, createTestStage, cleanupTestData, } from '../helpers' import { stageRouter } from '@/server/routers/stage' let programId: string let userIds: string[] = [] beforeAll(async () => { const program = await createTestProgram({ name: 'Stage Config Test' }) programId = program.id }) afterAll(async () => { await cleanupTestData(programId, userIds) }) describe('I-002: Stage Config — Valid and Invalid Updates', () => { it('accepts valid stage config update', async () => { const admin = await createTestUser('SUPER_ADMIN') userIds.push(admin.id) const pipeline = await createTestPipeline(programId) const track = await createTestTrack(pipeline.id) const stage = await createTestStage(track.id, { name: 'Config Test Stage', stageType: 'EVALUATION', status: 'STAGE_ACTIVE', }) const ctx = createTestContext(admin) const caller = stageRouter.createCaller(ctx) const updated = await caller.updateConfig({ id: stage.id, configJson: { requiredReviews: 3, evaluationMode: 'CRITERIA_BASED', }, }) expect(updated).toBeDefined() // Verify the configJson was persisted const fetched = await prisma.stage.findUnique({ where: { id: stage.id } }) const config = fetched!.configJson as Record expect(config.requiredReviews).toBe(3) expect(config.evaluationMode).toBe('CRITERIA_BASED') }) it('updates stage name via updateConfig', async () => { const admin = await createTestUser('SUPER_ADMIN') userIds.push(admin.id) const pipeline = await createTestPipeline(programId) const track = await createTestTrack(pipeline.id) const stage = await createTestStage(track.id, { name: 'Original Name', stageType: 'EVALUATION', status: 'STAGE_ACTIVE', }) const ctx = createTestContext(admin) const caller = stageRouter.createCaller(ctx) const updated = await caller.updateConfig({ id: stage.id, name: 'New Name', }) expect(updated.name).toBe('New Name') const fetched = await prisma.stage.findUnique({ where: { id: stage.id } }) expect(fetched!.name).toBe('New Name') }) it('updates stage window dates via updateConfig', async () => { const admin = await createTestUser('SUPER_ADMIN') userIds.push(admin.id) const pipeline = await createTestPipeline(programId) const track = await createTestTrack(pipeline.id) const stage = await createTestStage(track.id, { name: 'Window Test Stage', stageType: 'EVALUATION', status: 'STAGE_ACTIVE', }) const ctx = createTestContext(admin) const caller = stageRouter.createCaller(ctx) const openAt = new Date('2026-03-01T00:00:00Z') const closeAt = new Date('2026-04-01T00:00:00Z') const updated = await caller.updateConfig({ id: stage.id, windowOpenAt: openAt, windowCloseAt: closeAt, }) expect(updated).toBeDefined() const fetched = await prisma.stage.findUnique({ where: { id: stage.id } }) expect(fetched!.windowOpenAt!.getTime()).toBe(openAt.getTime()) expect(fetched!.windowCloseAt!.getTime()).toBe(closeAt.getTime()) }) it('rejects invalid window dates (close before open)', async () => { const admin = await createTestUser('SUPER_ADMIN') userIds.push(admin.id) const pipeline = await createTestPipeline(programId) const track = await createTestTrack(pipeline.id) const stage = await createTestStage(track.id, { name: 'Invalid Window Stage', stageType: 'EVALUATION', status: 'STAGE_ACTIVE', }) const ctx = createTestContext(admin) const caller = stageRouter.createCaller(ctx) await expect( caller.updateConfig({ id: stage.id, windowOpenAt: new Date('2026-04-01T00:00:00Z'), windowCloseAt: new Date('2026-03-01T00:00:00Z'), // Before open }) ).rejects.toThrow() }) it('opens and closes stage window via openWindow/closeWindow', async () => { const admin = await createTestUser('SUPER_ADMIN') userIds.push(admin.id) const pipeline = await createTestPipeline(programId) const track = await createTestTrack(pipeline.id) const stage = await createTestStage(track.id, { name: 'Open/Close Window Stage', stageType: 'EVALUATION', status: 'STAGE_ACTIVE', }) const ctx = createTestContext(admin) const caller = stageRouter.createCaller(ctx) // Open the window const opened = await caller.openWindow({ id: stage.id }) expect(opened.windowOpenAt).not.toBeNull() // Close the window const closed = await caller.closeWindow({ id: stage.id }) expect(closed.windowCloseAt).not.toBeNull() }) })