59 lines
2.9 KiB
JavaScript
59 lines
2.9 KiB
JavaScript
const { test, expect } = require('@playwright/test');
|
|
|
|
const png = Buffer.from(
|
|
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+/p9sAAAAASUVORK5CYII=',
|
|
'base64',
|
|
);
|
|
|
|
test.beforeEach(async ({ page }) => {
|
|
await page.goto('/');
|
|
await page.evaluate(() => localStorage.clear());
|
|
await page.reload();
|
|
});
|
|
|
|
test('loads the dashboard and validates missing meal input', async ({ page }) => {
|
|
await expect(page.getByRole('heading', { name: 'Calorie AI' })).toBeVisible();
|
|
await page.getByRole('button', { name: 'Analyze and save' }).click();
|
|
await expect(page.locator('#status')).toContainText('Describe the meal or upload an image.');
|
|
});
|
|
|
|
test('saves settings, uploads an image, logs nutrition, updates charts, and clears diary', async ({ page }) => {
|
|
let calls = 0;
|
|
await page.route('**/api/chat', async (route) => {
|
|
calls += 1;
|
|
const content = calls === 1
|
|
? '{"foods":["salmon","rice","broccoli","berries"],"calories":620,"proteinGrams":42,"carbsGrams":58,"fatGrams":22,"fruitServings":0.5,"vegetableServings":1.5}'
|
|
: '{"mealName":"Salmon rice bowl","calories":640,"proteinGrams":44,"carbsGrams":60,"fatGrams":23,"fruitServings":0.5,"vegetableServings":1.5,"foodGroups":"fish, grains, fruit, vegetables","notes":"Estimated from uploaded image and portion note."}';
|
|
|
|
await route.fulfill({
|
|
status: 200,
|
|
contentType: 'application/json',
|
|
body: JSON.stringify({ choices: [{ message: { content } }] }),
|
|
});
|
|
});
|
|
|
|
await page.locator('#baseUrl').fill('https://api.example.test/v1');
|
|
await page.locator('#visionModel').fill('vision-test-model');
|
|
await page.locator('#taskModel').fill('task-test-model');
|
|
await page.getByRole('button', { name: 'Save settings' }).click();
|
|
|
|
await page.locator('#description').fill('salmon rice bowl with broccoli and berries');
|
|
await page.locator('#measure').fill('one dinner bowl');
|
|
await page.locator('#image').setInputFiles({ name: 'meal.png', mimeType: 'image/png', buffer: png });
|
|
await expect(page.locator('#preview')).toBeVisible();
|
|
|
|
await page.getByRole('button', { name: 'Analyze and save' }).click();
|
|
await expect(page.locator('#status')).toContainText('Saved.');
|
|
await expect(page.locator('#todayCalories')).toHaveText('640 kcal');
|
|
await expect(page.locator('#todayMacroText')).toHaveText('P 44g | C 60g | F 23g');
|
|
await expect(page.locator('#todayProduceText')).toHaveText('Fruit 0.5 | Veg 1.5');
|
|
await expect(page.locator('.entry')).toContainText('Salmon rice bowl');
|
|
await expect(page.locator('#macroChart')).toBeVisible();
|
|
await expect(page.locator('#calorieChart')).toBeVisible();
|
|
await expect(page.locator('#produceChart')).toBeVisible();
|
|
|
|
page.once('dialog', dialog => dialog.accept());
|
|
await page.getByRole('button', { name: 'Clear local diary' }).click();
|
|
await expect(page.locator('#todayCalories')).toHaveText('0 kcal');
|
|
await expect(page.locator('#entries')).toContainText('No meals logged yet.');
|
|
});
|