173 lines
6.6 KiB
TypeScript
173 lines
6.6 KiB
TypeScript
import { describe, it, expect, beforeAll } from 'vitest';
|
|
import { prisma } from '../../src/lib/prisma.js';
|
|
import { buildApp, createEquipment, createUser, loginViaApi, rand, PASSWORD } from '../helpers.js';
|
|
|
|
describe('equipment catalog (kind + spec)', () => {
|
|
let adminToken: string;
|
|
let memberToken: string;
|
|
|
|
beforeAll(async () => {
|
|
const { user: admin } = await createUser({ isAdmin: true });
|
|
adminToken = (await loginViaApi(admin.email, PASSWORD)).token;
|
|
const { user: member } = await createUser();
|
|
memberToken = (await loginViaApi(member.email, PASSWORD)).token;
|
|
});
|
|
|
|
it('creates an equipment entry with a kind-specific spec', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/admin/equipment')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({
|
|
name: `Hantle ${rand()}`,
|
|
kind: 'DUMBBELL',
|
|
spec: { minKg: 2.5, maxKg: 25 },
|
|
description: 'Hantle wolne.',
|
|
});
|
|
expect(res.status).toBe(201);
|
|
expect(res.body.equipment).toMatchObject({
|
|
kind: 'DUMBBELL',
|
|
spec: { minKg: 2.5, maxKg: 25 },
|
|
});
|
|
});
|
|
|
|
it('rejects a missing spec for a load-bearing kind with 400', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/admin/equipment')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: `Sztanga ${rand()}`, kind: 'BARBELL' });
|
|
expect(res.status).toBe(400);
|
|
expect(res.body.error.code).toBe('BAD_REQUEST');
|
|
});
|
|
|
|
it('rejects an inconsistent load range spec with 400', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/admin/equipment')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: `Wyciąg ${rand()}`, kind: 'CABLE', spec: { minKg: 50, maxKg: 5 } });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects a spec payload for BODYWEIGHT with 400', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/admin/equipment')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: `Masa ciała ${rand()}`, kind: 'BODYWEIGHT', spec: { minKg: 1, maxKg: 10 } });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('creates a BODYWEIGHT entry without a spec', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/admin/equipment')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: `Drążek ${rand()}`, kind: 'BODYWEIGHT' });
|
|
expect(res.status).toBe(201);
|
|
expect(res.body.equipment.spec).toBeNull();
|
|
});
|
|
|
|
it('updates the kind and spec of an equipment entry', async () => {
|
|
const app = buildApp();
|
|
const created = await app
|
|
.post('/api/v1/admin/equipment')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: `Leg press ${rand()}`, kind: 'MACHINE', spec: { minKg: 20, maxKg: 120 } });
|
|
const id = created.body.equipment.id as string;
|
|
const res = await app
|
|
.patch(`/api/v1/admin/equipment/${id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ kind: 'CARDIO', spec: { maxSpeedKmh: 16 } });
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.equipment).toMatchObject({ kind: 'CARDIO', spec: { maxSpeedKmh: 16 } });
|
|
});
|
|
|
|
it('rejects an inconsistent spec after a kind change with 400', async () => {
|
|
const app = buildApp();
|
|
const created = await app
|
|
.post('/api/v1/admin/equipment')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: `Kettlebell ${rand()}`, kind: 'KETTLEBELL', spec: { minKg: 4, maxKg: 32 } });
|
|
const id = created.body.equipment.id as string;
|
|
const res = await app
|
|
.patch(`/api/v1/admin/equipment/${id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ kind: 'BARBELL' });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects a non-admin user with 403', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/admin/equipment')
|
|
.set('Authorization', `Bearer ${memberToken}`)
|
|
.send({ name: `Forbidden ${rand()}`, kind: 'OTHER' });
|
|
expect(res.status).toBe(403);
|
|
});
|
|
|
|
it('lists the catalog with kind and spec', async () => {
|
|
const app = buildApp();
|
|
const names = {
|
|
barbell: `List Barbell ${rand()}`,
|
|
dumbbell: `List Dumbbell ${rand()}`,
|
|
bodyweight: `List Bodyweight ${rand()}`,
|
|
};
|
|
await app
|
|
.post('/api/v1/admin/equipment')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: names.barbell, kind: 'BARBELL', spec: { maxLoadKg: 100 } });
|
|
await app
|
|
.post('/api/v1/admin/equipment')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: names.dumbbell, kind: 'DUMBBELL', spec: { minKg: 2.5, maxKg: 25 } });
|
|
await app
|
|
.post('/api/v1/admin/equipment')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: names.bodyweight, kind: 'BODYWEIGHT' });
|
|
|
|
const res = await app.get('/api/v1/admin/equipment').set('Authorization', `Bearer ${adminToken}`);
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.equipment).toEqual(
|
|
expect.arrayContaining([
|
|
expect.objectContaining({ name: names.barbell, kind: 'BARBELL', spec: { maxLoadKg: 100 } }),
|
|
expect.objectContaining({ name: names.dumbbell, kind: 'DUMBBELL', spec: { minKg: 2.5, maxKg: 25 } }),
|
|
expect.objectContaining({ name: names.bodyweight, kind: 'BODYWEIGHT', spec: null }),
|
|
]),
|
|
);
|
|
});
|
|
|
|
it('lets an admin add gym equipment with a quantity and change it', async () => {
|
|
const app = buildApp();
|
|
const gym = await prisma.gym.create({ data: { name: `Qty Gym ${rand()}` } });
|
|
const barbell = await createEquipment(`Qty Barbell ${rand()}`);
|
|
const dumbbells = await createEquipment(`Qty Dumbbell ${rand()}`);
|
|
|
|
const added = await app
|
|
.post(`/api/v1/admin/gyms/${gym.id}/equipment`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ equipmentId: barbell.id, quantity: 2 });
|
|
expect(added.status).toBe(200);
|
|
expect(added.body.gym.equipment).toEqual([
|
|
expect.objectContaining({ equipmentId: barbell.id, quantity: 2 }),
|
|
]);
|
|
|
|
const addedDumbbells = await app
|
|
.post(`/api/v1/admin/gyms/${gym.id}/equipment`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ equipmentId: dumbbells.id, quantity: 4 });
|
|
expect(addedDumbbells.status).toBe(200);
|
|
|
|
const updated = await app
|
|
.patch(`/api/v1/admin/gyms/${gym.id}/equipment/${dumbbells.id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ quantity: 6 });
|
|
expect(updated.status).toBe(200);
|
|
const link = updated.body.gym.equipment.find(
|
|
(e: { equipmentId: string }) => e.equipmentId === dumbbells.id,
|
|
);
|
|
expect(link.quantity).toBe(6);
|
|
});
|
|
});
|