2026-02-14 15:26:42 +01:00
|
|
|
/**
|
|
|
|
|
* I-005: Assignment API — Preview vs Execute Parity
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
|
|
|
|
|
import { prisma } from '../setup'
|
|
|
|
|
import {
|
|
|
|
|
createTestUser,
|
|
|
|
|
createTestProgram,
|
|
|
|
|
createTestPipeline,
|
|
|
|
|
createTestTrack,
|
|
|
|
|
createTestStage,
|
|
|
|
|
createTestProject,
|
|
|
|
|
createTestPSS,
|
|
|
|
|
cleanupTestData,
|
|
|
|
|
} from '../helpers'
|
|
|
|
|
import { previewStageAssignment, executeStageAssignment } from '@/server/services/stage-assignment'
|
|
|
|
|
|
|
|
|
|
let programId: string
|
|
|
|
|
let userIds: string[] = []
|
|
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
|
const program = await createTestProgram({ name: 'Assignment Preview Test' })
|
|
|
|
|
programId = program.id
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
|
await cleanupTestData(programId, userIds)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('I-005: Assignment Preview vs Execute Parity', () => {
|
|
|
|
|
it('preview and execute produce matching assignment pairs', 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: 'Assignment Stage',
|
|
|
|
|
stageType: 'EVALUATION',
|
|
|
|
|
status: 'STAGE_ACTIVE',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Create 3 jurors
|
|
|
|
|
const juror1 = await createTestUser('JURY_MEMBER', { name: 'Juror Preview 1' })
|
|
|
|
|
const juror2 = await createTestUser('JURY_MEMBER', { name: 'Juror Preview 2' })
|
|
|
|
|
const juror3 = await createTestUser('JURY_MEMBER', { name: 'Juror Preview 3' })
|
|
|
|
|
userIds.push(juror1.id, juror2.id, juror3.id)
|
|
|
|
|
|
|
|
|
|
// Create 2 projects
|
|
|
|
|
const proj1 = await createTestProject(programId, { title: 'Preview P1' })
|
|
|
|
|
const proj2 = await createTestProject(programId, { title: 'Preview P2' })
|
|
|
|
|
await createTestPSS(proj1.id, track.id, stage.id, { state: 'PENDING' })
|
|
|
|
|
await createTestPSS(proj2.id, track.id, stage.id, { state: 'PENDING' })
|
|
|
|
|
|
|
|
|
|
const config = { requiredReviews: 2 }
|
|
|
|
|
|
|
|
|
|
// Step 1: Preview
|
|
|
|
|
const preview = await previewStageAssignment(stage.id, config, prisma)
|
|
|
|
|
|
|
|
|
|
expect(preview.assignments.length).toBeGreaterThan(0)
|
|
|
|
|
expect(preview.stats.totalProjects).toBe(2)
|
|
|
|
|
|
|
|
|
|
// Step 2: Execute with the same pairs from preview
|
|
|
|
|
const assignmentInputs = preview.assignments.map(a => ({
|
|
|
|
|
userId: a.userId,
|
|
|
|
|
projectId: a.projectId,
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
const execResult = await executeStageAssignment(
|
|
|
|
|
stage.id, assignmentInputs, admin.id, prisma
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(execResult.created).toBe(assignmentInputs.length)
|
|
|
|
|
expect(execResult.errors).toHaveLength(0)
|
|
|
|
|
|
|
|
|
|
// Step 3: Verify all assignments exist in database
|
|
|
|
|
const dbAssignments = await prisma.assignment.findMany({
|
|
|
|
|
where: { stageId: stage.id },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(dbAssignments.length).toBe(assignmentInputs.length)
|
|
|
|
|
|
|
|
|
|
// Verify each preview pair has a matching DB record
|
|
|
|
|
for (const input of assignmentInputs) {
|
|
|
|
|
const match = dbAssignments.find(
|
|
|
|
|
a => a.userId === input.userId && a.projectId === input.projectId
|
|
|
|
|
)
|
|
|
|
|
expect(match).toBeDefined()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|