const BASE = 'http://tisachi.ddns.net:9002/api/v1'; async function api(path, { token, method = 'GET', body } = {}) { const res = await fetch(`${BASE}${path}`, { method, headers: { 'Content-Type': 'application/json', ...(token ? { Authorization: `Bearer ${token}` } : {}), }, body: body ? JSON.stringify(body) : undefined, }); const text = await res.text(); const json = text ? JSON.parse(text) : null; if (!res.ok) throw new Error(`${method} ${path} -> ${res.status}: ${text}`); return json; } function assert(cond, msg) { if (!cond) throw new Error(`ASSERT FAILED: ${msg}`); console.log(`ok: ${msg}`); } async function main() { // fresh user per run (unique email) const email = `smoke${Date.now()}@fitos.local`; const reg = await api('/auth/register', { method: 'POST', body: { email, password: 'testpass123', firstName: 'Smoke', lastName: 'Test' }, }); const { accessToken: tok } = reg; assert(tok, 'register returns access token'); // me const me = await api('/me', { token: tok }); assert(me.user.email === email, 'GET /me returns profile'); // body parameters await api('/me/body-parameters/weight', { token: tok, method: 'POST', body: { value: 80.5 } }); await api('/me/body-parameters/circumference', { token: tok, method: 'POST', body: { name: 'biceps', value: 40 }, }); const bp = await api('/me/body-parameters', { token: tok }); assert(bp.weight.length >= 1 && bp.circumferences.length >= 1, 'body parameters saved'); // profile update const upd = await api('/me', { token: tok, method: 'PATCH', body: { age: 30, heightCm: 180, priorities: 'chest, back', exclusions: 'knee injury', availableTime: '3x45min' }, }); assert(upd.user.age === 30 && upd.user.priorities === 'chest, back', 'profile updated'); // puzzles const pz = await api('/puzzles', { token: tok }); assert(pz.puzzles.length >= 20, `puzzle pool seeded (${pz.puzzles.length})`); const bench = pz.puzzles.find((p) => p.slug === 'bench-press'); const squat = pz.puzzles.find((p) => p.slug === 'squat'); assert(bench && squat, 'bench-press and squat exist'); // create plan (4 weeks, week 0 = Mon: bench + squat, other weeks copied) const week = (index) => ({ index, days: [ { dayOfWeek: 1, exercises: [ { puzzleId: bench.id, order: 0, sets: [{ index: 0, reps: 10, loadKg: 60 + index * 2.5, restSec: 90, rir: 2 }] }, { puzzleId: squat.id, order: 1, sets: [{ index: 0, reps: 8, loadKg: 80 + index * 2.5, restSec: 120, rir: 1 }] }, ], }, ], }); const plan = await api('/plans', { token: tok, method: 'POST', body: { title: 'MVP test plan', weeksCount: 4, weeks: [week(0), week(1), week(2), week(3)] }, }); assert(plan.plan.status === 'DRAFT', 'plan created as DRAFT'); const planId = plan.plan.id; // activate — a past Monday (2 weeks before the next Monday) so day 1 is in the past const now = new Date(); const daysUntilMonday = (8 - now.getDay()) % 7; const nextMonday = new Date(now.getFullYear(), now.getMonth(), now.getDate() + daysUntilMonday); const start = new Date(nextMonday); start.setDate(start.getDate() - 14); const dateStr = `${start.getFullYear()}-${String(start.getMonth() + 1).padStart(2, '0')}-${String( start.getDate(), ).padStart(2, '0')}`; const activate = await api(`/plans/${planId}/activate`, { token: tok, method: 'POST', body: { startDate: dateStr }, }); assert(activate.ok, 'plan activated (Monday start, in the past)'); // dashboard const dash = await api('/dashboard', { token: tok }); assert(dash.killstreak === 0, 'killstreak starts at 0'); assert(dash.activePlan.id === planId, 'active plan on dashboard'); assert(dash.progress.total === 4 && dash.progress.completed === 0, 'plan progress 0/4'); assert(dash.nextTraining && dash.nextTraining.exercises.length === 2, 'next training has 2 exercises'); // calendar (current month — plan may span into next month) const cal = await api('/calendar', { token: tok }); assert(cal.days.length >= 1, `calendar shows training days in current month (got ${cal.days.length})`); // total across the plan = 4 const planDetail = await api(`/plans/${planId}`, { token: tok }); const totalDays = planDetail.plan.weeks.reduce((n, w) => n + w.days.length, 0); assert(totalDays === 4, `plan has 4 training days total (got ${totalDays})`); // day details const dayId = cal.days[0].id; const dayInfo = await api(`/days/${dayId}`, { token: tok }); assert(dayInfo.day.status === 'PLANNED', 'GET /days/:id returns day'); assert(dayInfo.day.exercises.length === 2, 'day details include exercises'); // start session const sess = await api(`/days/${dayId}/sessions`, { token: tok, method: 'POST' }); assert(sess.sessionId, 'session started'); const sessionId = sess.sessionId; // duplicate active session -> 409 try { await api(`/days/${dayId}/sessions`, { token: tok, method: 'POST' }); throw new Error('duplicate session should fail'); } catch (e) { assert(String(e.message).includes('409'), 'one active session per day (409)'); } // session details const s = await api(`/sessions/${sessionId}`, { token: tok }); assert(s.session.exercises.length === 2, 'session has 2 planned exercises'); const ex0 = s.session.exercises[0]; assert(ex0.sets.length === 1 && ex0.sets[0].planSetId, 'session set references plan set'); assert(ex0.planExercise.sets[0].reps === 10, 'planned parameter visible as parent'); // record actual results const set0 = ex0.sets[0]; await api(`/sessions/sets/${set0.id}`, { token: tok, method: 'PATCH', body: { reps: 10, rir: 2, difficulty: 3 }, }); const ex1 = s.session.exercises[1]; await api(`/sessions/sets/${ex1.sets[0].id}`, { token: tok, method: 'PATCH', body: { reps: 8, rir: 1, difficulty: 4 }, }); // add exercise const add = await api(`/sessions/${sessionId}/exercises`, { token: tok, method: 'POST', body: { puzzleId: pz.puzzles.find((p) => p.slug === 'plank').id }, }); assert(add.exerciseId, 'exercise added to session'); const s2 = await api(`/sessions/${sessionId}`, { token: tok }); const added = s2.session.exercises.find((e) => e.id === add.exerciseId); assert(added && added.planExercise === null, 'added exercise has no plan reference'); const addedSet = await api(`/sessions/exercises/${added.id}/sets`, { token: tok, method: 'POST' }); await api(`/sessions/sets/${addedSet.setId}`, { token: tok, method: 'PATCH', body: { reps: 30, rir: 0 } }); // complete session await api(`/sessions/${sessionId}/complete`, { token: tok, method: 'POST' }); const s3 = await api(`/sessions/${sessionId}`, { token: tok }); assert(s3.session.status === 'COMPLETED', 'session completed'); // dashboard: killstreak = 1 const dash2 = await api('/dashboard', { token: tok }); assert(dash2.killstreak === 1, `killstreak = 1 after effective training (got ${dash2.killstreak})`); assert(dash2.progress.completed === 1, 'progress 1/4'); // refresh route exists (401 without cookie, not 404) const refreshRes = await fetch(`${BASE}/auth/refresh`, { method: 'POST' }); assert(refreshRes.status === 401, 'refresh route exists (401 without cookie)'); // admin: login as admin const adminLogin = await api('/auth/login', { method: 'POST', body: { email: 'admin@fitos.local', password: 'ChangeMe123!' }, }); const atok = adminLogin.accessToken; assert(adminLogin.user.isAdmin, 'admin user has isAdmin flag'); // admin: create gym const gym = await api('/admin/gyms', { token: atok, method: 'POST', body: { name: 'Gym Test', location: 'Warszawa', hours: '24h', capacity: 50 }, }); assert(gym.gym.name === 'Gym Test', 'admin created gym'); // admin: assign membership const meUser = await api('/me', { token: tok }); const mem = await api(`/admin/users/${meUser.user.id}/memberships`, { token: atok, method: 'POST', body: { gymId: gym.gym.id, role: 'MEMBER' }, }); assert(mem.membership.role === 'MEMBER', 'membership assigned'); // user sees gym const me2 = await api('/me', { token: tok }); assert(me2.memberships.length === 1 && me2.memberships[0].gym.name === 'Gym Test', 'user sees gym membership'); // admin: create user (unique email per run) const newEmail = `new${Date.now()}@fitos.local`; const newUser = await api('/admin/users', { token: atok, method: 'POST', body: { email: newEmail, password: 'newpass123', firstName: 'New' }, }); assert(newUser.user.email === newEmail, 'admin created user'); // non-admin cannot access admin routes try { await api('/admin/gyms', { token: tok }); throw new Error('non-admin should not access admin routes'); } catch (e) { assert(String(e.message).includes('403'), 'non-admin blocked from admin (403)'); } console.log('\nALL SMOKE TESTS PASSED'); } main().catch((e) => { console.error(e.message); process.exit(1); });