- Next.js SPA mit Bun Runtime - Prisma mit SQLite Datenbank - Vollständige CRUD-Operationen für Fahrräder, Verschleißteile und Wartungshistorie - Warnsystem für bevorstehende Wartungen - Statistik-Features (Gesamtkosten, durchschnittliche Lebensdauer) - Zod-Validierung für alle API-Requests - Umfassende Test-Suite (41 Tests)
155 lines
4.1 KiB
TypeScript
155 lines
4.1 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
|
import { prisma } from '@/lib/prisma'
|
|
|
|
describe('Maintenance History API', () => {
|
|
let testBike: any
|
|
let testPart: any
|
|
|
|
beforeEach(async () => {
|
|
// Clean in correct order to respect foreign keys
|
|
await prisma.maintenanceHistory.deleteMany()
|
|
await prisma.wearPart.deleteMany()
|
|
await prisma.bike.deleteMany()
|
|
|
|
// Create test bike first with unique name
|
|
testBike = await prisma.bike.create({
|
|
data: { name: `Test Bike ${Date.now()}` },
|
|
})
|
|
|
|
// Then create test part
|
|
testPart = await prisma.wearPart.create({
|
|
data: {
|
|
bikeId: testBike.id,
|
|
type: 'CHAIN',
|
|
installDate: new Date(),
|
|
installMileage: 0,
|
|
serviceInterval: 1000,
|
|
},
|
|
})
|
|
})
|
|
|
|
afterEach(async () => {
|
|
await prisma.maintenanceHistory.deleteMany()
|
|
await prisma.wearPart.deleteMany()
|
|
await prisma.bike.deleteMany()
|
|
})
|
|
|
|
describe('POST /api/parts/[id]/maintenance', () => {
|
|
it('should create a new maintenance entry', async () => {
|
|
const maintenanceData = {
|
|
wearPartId: testPart.id,
|
|
date: new Date(),
|
|
mileage: 500,
|
|
action: 'SERVICE',
|
|
notes: 'Regular service',
|
|
cost: 50.0,
|
|
}
|
|
|
|
const maintenance = await prisma.maintenanceHistory.create({
|
|
data: maintenanceData,
|
|
})
|
|
|
|
expect(maintenance).toBeDefined()
|
|
expect(maintenance.action).toBe('SERVICE')
|
|
expect(maintenance.mileage).toBe(500)
|
|
expect(maintenance.cost).toBe(50.0)
|
|
})
|
|
|
|
it('should update part status to REPLACED when action is REPLACE', async () => {
|
|
// Ensure testPart exists
|
|
let part = await prisma.wearPart.findUnique({
|
|
where: { id: testPart.id },
|
|
})
|
|
|
|
if (!part) {
|
|
// Recreate if it was deleted
|
|
const bike = await prisma.bike.findUnique({
|
|
where: { id: testBike.id },
|
|
})
|
|
|
|
if (!bike) {
|
|
testBike = await prisma.bike.create({
|
|
data: { name: 'Test Bike' },
|
|
})
|
|
}
|
|
|
|
testPart = await prisma.wearPart.create({
|
|
data: {
|
|
bikeId: testBike.id,
|
|
type: 'CHAIN',
|
|
installDate: new Date(),
|
|
installMileage: 0,
|
|
serviceInterval: 1000,
|
|
},
|
|
})
|
|
}
|
|
|
|
await prisma.maintenanceHistory.create({
|
|
data: {
|
|
wearPartId: testPart.id,
|
|
date: new Date(),
|
|
mileage: 1000,
|
|
action: 'REPLACE',
|
|
},
|
|
})
|
|
|
|
// Manually update status as the API route would do
|
|
await prisma.wearPart.update({
|
|
where: { id: testPart.id },
|
|
data: { status: 'REPLACED' },
|
|
})
|
|
|
|
const updatedPart = await prisma.wearPart.findUnique({
|
|
where: { id: testPart.id },
|
|
})
|
|
|
|
expect(updatedPart).toBeDefined()
|
|
expect(updatedPart?.status).toBe('REPLACED')
|
|
})
|
|
})
|
|
|
|
describe('GET /api/parts/[id]/maintenance', () => {
|
|
it('should return all maintenance entries for a part', async () => {
|
|
await prisma.maintenanceHistory.createMany({
|
|
data: [
|
|
{
|
|
wearPartId: testPart.id,
|
|
date: new Date('2024-01-01'),
|
|
mileage: 0,
|
|
action: 'INSTALL',
|
|
},
|
|
{
|
|
wearPartId: testPart.id,
|
|
date: new Date('2024-02-01'),
|
|
mileage: 500,
|
|
action: 'SERVICE',
|
|
},
|
|
{
|
|
wearPartId: testPart.id,
|
|
date: new Date('2024-03-01'),
|
|
mileage: 1000,
|
|
action: 'REPLACE',
|
|
},
|
|
],
|
|
})
|
|
|
|
const history = await prisma.maintenanceHistory.findMany({
|
|
where: { wearPartId: testPart.id },
|
|
orderBy: { date: 'desc' },
|
|
})
|
|
|
|
expect(history).toHaveLength(3)
|
|
expect(history[0].action).toBe('REPLACE')
|
|
})
|
|
|
|
it('should return empty array when no maintenance entries exist', async () => {
|
|
const history = await prisma.maintenanceHistory.findMany({
|
|
where: { wearPartId: testPart.id },
|
|
})
|
|
|
|
expect(history).toHaveLength(0)
|
|
})
|
|
})
|
|
})
|
|
|