f2b9239d82
Adds the initial testing baseline for the project: Unit coverage: - Zod schemas for items, assignments, movements, categories, auth, recipients, users, and assets - password hashing and verification helpers - auth role helper functions Integration coverage with PostgreSQL Testcontainers: - item use-cases: create, duplicate names, delete constraints - assignment use-cases: create, insufficient stock, return, double return - asset use-cases: available/assigned creation and lifecycle transitions - user use-cases: create/update, uniqueness, admin safeguards, password reset - category use-cases: create/update/delete constraints - recipient use-cases: create/update and uniqueness constraints E2E smoke coverage with Playwright: - unauthenticated redirect to login - seeded admin login - dashboard load - admin users page - inventory items page - assignments page Also configures: - Vitest - Playwright - PostgreSQL Testcontainers helpers - deterministic E2E admin bootstrap - test artifact ignores Validation: - bun run test: 9 files / 37 tests passed - bun run test:e2e: 3 passed - bunx tsc --noEmit: passed - bunx prisma validate: passed
199 lines
6.0 KiB
TypeScript
199 lines
6.0 KiB
TypeScript
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest"
|
|
import type { PrismaClient } from "@/generated/prisma/client"
|
|
import {
|
|
createTestItem,
|
|
createTestRecipient,
|
|
createTestUser,
|
|
} from "../helpers/factories"
|
|
import {
|
|
resetIntegrationTestDatabase,
|
|
startIntegrationTestDatabase,
|
|
stopIntegrationTestDatabase,
|
|
} from "../helpers/test-db"
|
|
|
|
let prisma: PrismaClient
|
|
let createAssignmentUseCase: typeof import("@/use-cases/assignment.use-cases").createAssignmentUseCase
|
|
let returnAssignmentUseCase: typeof import("@/use-cases/assignment.use-cases").returnAssignmentUseCase
|
|
|
|
beforeAll(async () => {
|
|
await startIntegrationTestDatabase()
|
|
|
|
const prismaModule = await import("@/lib/prisma")
|
|
const assignmentUseCases = await import("@/use-cases/assignment.use-cases")
|
|
|
|
prisma = prismaModule.prisma
|
|
createAssignmentUseCase = assignmentUseCases.createAssignmentUseCase
|
|
returnAssignmentUseCase = assignmentUseCases.returnAssignmentUseCase
|
|
})
|
|
|
|
beforeEach(async () => {
|
|
await resetIntegrationTestDatabase(prisma)
|
|
})
|
|
|
|
afterAll(async () => {
|
|
await prisma?.$disconnect()
|
|
await stopIntegrationTestDatabase()
|
|
})
|
|
|
|
describe("assignment use-cases", () => {
|
|
it("creates an assignment, decrements stock, and records an ASSIGNMENT movement", async () => {
|
|
const actor = await createTestUser(prisma)
|
|
const recipient = await createTestRecipient(prisma)
|
|
const item = await createTestItem(prisma, { stock: 5 })
|
|
|
|
const assignmentDate = new Date("2026-01-01T00:00:00.000Z")
|
|
|
|
const result = await createAssignmentUseCase({
|
|
actorId: actor.id,
|
|
itemId: item.id,
|
|
recipientId: recipient.id,
|
|
quantity: 2,
|
|
assignmentDate,
|
|
notes: "Initial assignment",
|
|
})
|
|
|
|
expect(result.success).toBe(true)
|
|
if (!result.success) throw new Error("Expected assignment creation success")
|
|
|
|
const [updatedItem, assignment, movements] = await Promise.all([
|
|
prisma.item.findUniqueOrThrow({ where: { id: item.id } }),
|
|
prisma.assignment.findUniqueOrThrow({
|
|
where: { id: result.assignmentId },
|
|
}),
|
|
prisma.movement.findMany({ orderBy: [{ createdAt: "asc" }, { id: "asc" }] }),
|
|
])
|
|
|
|
expect(updatedItem.stock).toBe(3)
|
|
expect(assignment).toMatchObject({
|
|
itemId: item.id,
|
|
recipientId: recipient.id,
|
|
quantity: 2,
|
|
notes: "Initial assignment",
|
|
createdBy: actor.id,
|
|
returnDate: null,
|
|
})
|
|
expect(assignment.assignmentDate).toEqual(assignmentDate)
|
|
expect(movements).toHaveLength(1)
|
|
expect(movements[0]).toMatchObject({
|
|
type: "ASSIGNMENT",
|
|
itemId: item.id,
|
|
recipientId: recipient.id,
|
|
assignmentId: result.assignmentId,
|
|
quantity: 2,
|
|
userId: actor.id,
|
|
})
|
|
})
|
|
|
|
it("rejects assignment creation when item stock is insufficient", async () => {
|
|
const actor = await createTestUser(prisma)
|
|
const recipient = await createTestRecipient(prisma)
|
|
const item = await createTestItem(prisma, { stock: 1 })
|
|
|
|
const result = await createAssignmentUseCase({
|
|
actorId: actor.id,
|
|
itemId: item.id,
|
|
recipientId: recipient.id,
|
|
quantity: 2,
|
|
})
|
|
|
|
expect(result).toEqual({
|
|
success: false,
|
|
errors: {
|
|
quantity: ["Item does not have enough stock"],
|
|
},
|
|
})
|
|
|
|
await expect(
|
|
prisma.item.findUniqueOrThrow({ where: { id: item.id } }),
|
|
).resolves.toMatchObject({ stock: 1 })
|
|
await expect(prisma.assignment.count()).resolves.toBe(0)
|
|
await expect(prisma.movement.count()).resolves.toBe(0)
|
|
})
|
|
|
|
it("returns an assignment, restores stock, closes it, and records a RETURN movement", async () => {
|
|
const actor = await createTestUser(prisma)
|
|
const recipient = await createTestRecipient(prisma)
|
|
const item = await createTestItem(prisma, { stock: 4 })
|
|
|
|
const created = await createAssignmentUseCase({
|
|
actorId: actor.id,
|
|
itemId: item.id,
|
|
recipientId: recipient.id,
|
|
quantity: 3,
|
|
})
|
|
|
|
expect(created.success).toBe(true)
|
|
if (!created.success)
|
|
throw new Error("Expected assignment creation success")
|
|
|
|
const returned = await returnAssignmentUseCase({
|
|
id: created.assignmentId,
|
|
actorId: actor.id,
|
|
})
|
|
|
|
expect(returned).toEqual({ success: true })
|
|
|
|
const [updatedItem, assignment, movements] = await Promise.all([
|
|
prisma.item.findUniqueOrThrow({ where: { id: item.id } }),
|
|
prisma.assignment.findUniqueOrThrow({
|
|
where: { id: created.assignmentId },
|
|
}),
|
|
prisma.movement.findMany({ orderBy: [{ createdAt: "asc" }, { id: "asc" }] }),
|
|
])
|
|
|
|
expect(updatedItem.stock).toBe(4)
|
|
expect(assignment.returnDate).toBeInstanceOf(Date)
|
|
expect(assignment).toMatchObject({
|
|
itemId: null,
|
|
assetId: null,
|
|
recipientId: null,
|
|
quantity: null,
|
|
})
|
|
expect(movements).toHaveLength(2)
|
|
expect(movements[0]).toMatchObject({
|
|
type: "ASSIGNMENT",
|
|
itemId: item.id,
|
|
assignmentId: created.assignmentId,
|
|
quantity: 3,
|
|
userId: actor.id,
|
|
})
|
|
expect(movements[1]).toMatchObject({
|
|
type: "RETURN",
|
|
itemId: item.id,
|
|
assignmentId: created.assignmentId,
|
|
quantity: 3,
|
|
userId: actor.id,
|
|
})
|
|
})
|
|
|
|
it("rejects returning the same assignment twice", async () => {
|
|
const actor = await createTestUser(prisma)
|
|
const recipient = await createTestRecipient(prisma)
|
|
const item = await createTestItem(prisma, { stock: 2 })
|
|
|
|
const created = await createAssignmentUseCase({
|
|
actorId: actor.id,
|
|
itemId: item.id,
|
|
recipientId: recipient.id,
|
|
quantity: 1,
|
|
})
|
|
|
|
expect(created.success).toBe(true)
|
|
if (!created.success)
|
|
throw new Error("Expected assignment creation success")
|
|
|
|
await expect(
|
|
returnAssignmentUseCase({ id: created.assignmentId, actorId: actor.id }),
|
|
).resolves.toEqual({ success: true })
|
|
|
|
await expect(
|
|
returnAssignmentUseCase({ id: created.assignmentId, actorId: actor.id }),
|
|
).resolves.toEqual({
|
|
success: false,
|
|
errors: { id: ["Assignment already returned"] },
|
|
})
|
|
|
|
await expect(prisma.movement.count()).resolves.toBe(2)
|
|
})
|
|
})
|