import { test, expect, type Page } from './fixtures'; import { mockApi, TEST_SHARED_PLAN, TEST_USER } from './mock-api'; /* Udostępnianie planów (PlanShare): sekcja „Udostępnione mi” (read-only) i dialog „Udostępnij plan” (właściciel → podopieczni/trenerzy). */ /** Detal planu właściciela (DRAFT, canManage, bez share'ów). */ const OWNER_PLAN = { id: 'plan-1', userId: 'user-test', canManage: true, title: 'Plan masowy', status: 'DRAFT', weeksCount: 1, startDate: null, gym: null, creator: null, shares: [], weeks: [], }; /** Właściciel = trener z podopiecznymi (dialog sharingu ma kogo pokazać). */ function mockOwnerPlans(page: Page) { mockApi(page, { me: { user: TEST_USER, memberships: [{ gymId: 'gym-1', roles: ['TRAINER'] }] }, }); // Stateful: udostępnienia widoczne w detalu po POST (jak w realnym API). const shares: { userId: string; user: { id: string; firstName: string; lastName: string } }[] = []; page.route('**/api/v1/plans/plan-1/share', (route) => { const req = route.request(); if (req.method() === 'POST') { const body = req.postDataJSON() as { userId: string }; if (!shares.some((s) => s.userId === body.userId)) { shares.push({ userId: body.userId, user: { id: body.userId, firstName: 'Anna', lastName: 'Nowak' }, }); } return route.fulfill({ status: 201, contentType: 'application/json', body: JSON.stringify({ ok: true }) }); } if (req.method() === 'DELETE') { const userId = req.url().split('/').slice(-2, -1)[0]; const i = shares.findIndex((s) => s.userId === userId); if (i >= 0) shares.splice(i, 1); } return route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ ok: true }) }); }); page.route('**/api/v1/plans/plan-1', (route) => route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ plan: { ...OWNER_PLAN, shares: [...shares] } }), }), ); page.route('**/api/v1/plans', (route) => { if (route.request().method() === 'POST') { return route.fulfill({ status: 201, contentType: 'application/json', body: JSON.stringify({ plan: { id: 'plan-1' } }), }); } return route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ plans: [OWNER_PLAN] }), }); }); } /** Odbiorca share'a: plan w sekcji „Udostępnione mi”, detal read-only. */ function mockRecipientPlans(page: Page) { mockApi(page, { sharedWithMe: { plans: [TEST_SHARED_PLAN] } }); page.route('**/api/v1/plans/shared-1', (route) => route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ plan: TEST_SHARED_PLAN }), }), ); } async function login(page: Page) { await page.goto('/login'); await page.locator('input[type="email"]').fill('test@fitos.pl'); await page.locator('input[type="password"]').fill('haslo123'); await page.getByRole('button', { name: 'Zaloguj' }).click(); await expect(page.getByRole('heading', { name: 'Dashboard' })).toBeVisible(); } test('„Udostępnione mi”: plan read-only — bez akcji statusowych, „Udostępnij” i tygodnia startu', async ({ page, }) => { mockRecipientPlans(page); await login(page); await page.getByRole('link', { name: 'Plany' }).click(); await expect(page.getByRole('heading', { name: 'Udostępnione mi' })).toBeVisible(); await expect(page.getByText('udostępnił: Jan Kowalski')).toBeVisible(); await page.getByRole('link', { name: /Plan udostępniony/ }).click(); await expect(page.getByRole('heading', { name: 'Plan udostępniony' })).toBeVisible(); // read-only: brak akcji w topbarze i wyboru tygodnia startu w karcie await expect(page.getByRole('button', { name: 'Aktywuj' })).toHaveCount(0); await expect(page.getByRole('button', { name: 'Edytuj' })).toHaveCount(0); await expect(page.getByRole('button', { name: 'Usuń' })).toHaveCount(0); await expect(page.getByRole('button', { name: 'Udostępnij' })).toHaveCount(0); await expect(page.getByText('Tydzień startu')).toHaveCount(0); }); test('właściciel: „Udostępnij” — dialog z podopiecznymi, share = POST /plans/:id/share', async ({ page, }) => { mockOwnerPlans(page); await login(page); await page.getByRole('link', { name: 'Plany' }).click(); await page.getByRole('link', { name: /Plan masowy/ }).click(); await expect(page.getByRole('heading', { name: 'Plan masowy' })).toBeVisible(); // DRAFT + canManage: wybór tygodnia startu + akcje w topbarze await expect(page.getByText('Tydzień startu', { exact: true })).toBeVisible(); await expect(page.getByRole('button', { name: 'Aktywuj' })).toBeVisible(); await expect(page.getByRole('button', { name: 'Udostępnij' })).toBeVisible(); await page.getByRole('button', { name: 'Udostępnij' }).click(); await expect(page.getByRole('dialog', { name: 'Udostępnij plan' })).toBeVisible(); await expect(page.getByText('Moje podopieczne')).toBeVisible(); // checkbox = natychmiastowy share (POST /plans/:id/share); // stan checkboxa i meta wracają z refetchu detalu (asynchronicznie) const checkbox = page.getByRole('checkbox'); await checkbox.click(); await expect(checkbox).toBeChecked(); // efekt share'a wycięty z detalu: odbiorca w meta planu (trwały, bez wyścigu z toastem) await expect(page.getByText('udostępnione: Anna Nowak')).toBeVisible(); });