Files
gymflow/tests/history-export.spec.ts
2025-12-17 11:04:54 +02:00

99 lines
3.7 KiB
TypeScript

import { test, expect } from './fixtures';
import * as fs from 'fs';
import * as path from 'path';
test.describe('History Export', () => {
test.beforeEach(async ({ page }) => {
// Console logs for debugging
page.on('console', msg => console.log('PAGE LOG:', msg.text()));
page.on('pageerror', exception => console.log(`PAGE ERROR: ${exception}`));
await page.setViewportSize({ width: 1280, height: 720 });
await page.goto('/');
});
// Helper to handle first login
async function handleFirstLogin(page: any) {
try {
const heading = page.getByRole('heading', { name: /Change Password/i });
const dashboard = page.getByText('Free Workout');
// Wait for Change Password or Dashboard
await expect(heading).toBeVisible({ timeout: 10000 });
// If we are here, Change Password is visible
await page.getByLabel('New Password').fill('StrongNewPass123!');
await page.getByRole('button', { name: /Save|Change/i }).click();
// Now expect dashboard
await expect(dashboard).toBeVisible();
} catch (e) {
// Check if already at dashboard
if (await page.getByText('Free Workout').isVisible()) {
return;
}
throw e;
}
}
test('should export workout history as CSV', async ({ page, createUniqueUser, request }) => {
const user = await createUniqueUser();
// 1. Seed an exercise
const exName = 'Bench Press Test';
await request.post('/api/exercises', {
data: { name: exName, type: 'STRENGTH' },
headers: { 'Authorization': `Bearer ${user.token}` }
});
// 2. Log in
await page.getByLabel('Email').fill(user.email);
await page.getByLabel('Password').fill(user.password);
await page.getByRole('button', { name: 'Login' }).click();
await handleFirstLogin(page);
// 3. Log a workout
// We are likely already on Tracker, but let's be sure or just proceed
// If we want to navigate:
// await page.getByRole('button', { name: 'Tracker' }).first().click();
// Since handleFirstLogin confirms 'Free Workout' is visible, we are on Tracker.
const freeWorkoutBtn = page.getByRole('button', { name: 'Free Workout' });
await expect(freeWorkoutBtn).toBeVisible();
await freeWorkoutBtn.click();
await expect(page.getByRole('button', { name: 'Finish' })).toBeVisible();
// Log a set
await page.getByPlaceholder('Select Exercise').click();
await page.getByText(exName).first().click();
await page.getByPlaceholder('Weight').fill('100');
await page.getByPlaceholder('Reps').fill('10');
await page.getByRole('button', { name: 'Log Set' }).click();
// Finish session
await page.getByRole('button', { name: 'Finish' }).click();
await page.getByRole('button', { name: 'Confirm' }).click();
// 3. Navigate to History
await page.getByText('History', { exact: true }).click();
// 4. Setup download listener
const downloadPromise = page.waitForEvent('download');
// 5. Click Export button (Using the title we added)
// Note: The title comes from t('export_csv', lang), defaulting to 'Export CSV' in English
const exportBtn = page.getByRole('button', { name: 'Export CSV' });
await expect(exportBtn).toBeVisible();
await exportBtn.click();
const download = await downloadPromise;
// 6. Verify download
expect(download.suggestedFilename()).toContain('gymflow_history');
expect(download.suggestedFilename()).toContain('.csv');
});
});