| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- /**
- * E2E · 食材推荐按健康问卷个性化调整 (requirement: 2026-11-09-diet-recommendation-health-questionnaire-adjust.md)
- * 能力键: diet.food.healthAdjustUi (前端 B1-B3) + diet.food.healthAdjust (后端契约)
- *
- * 验收覆盖 (对应需求文档 §4):
- * B2 食物卡片按 adjustment 渲染提示条: blocked(红)/warning(黄)/boost(绿)/normal(不渲染)
- * B3 suitable Tab: adjustment==='blocked' 项排到列表末尾
- * B1 loadFoods 透传 adjustment/adjustmentReason (接口契约)
- *
- * 执行前提 (本自动化轮次未满足 → 待核实登录态):
- * 1) MACHINE_JSON_PATH 指向含 e2e_test_target.admin 的 machine.json (提供管理员口令,仓库内不硬编码真实口令)
- * 2) 目标成员已在 health_status / diet_preferences 写入测试问卷数据 (过敏原/用药/疾病史/绝对避免/must_eat/宗教饮食)
- * 3) 后端 diet.food.healthAdjust 已实现并合入 origin/cfclub (A1-A5),否则 adjustment 字段恒为 normal
- *
- * 本文件为测试资产,不修改任何生产源码;如需执行:
- * MACHINE_JSON_PATH=/path/to/machine.json npx playwright test tests/e2e/diet-recommendation-health-adjust.spec.js
- */
- const { test, expect } = require('@playwright/test');
- const { loginAs } = require('./helpers/auth');
- const BASE = process.env.PW_BASE_URL || 'http://cfc.iwintrue.com';
- // 用于验证“健康问卷不建议”的成员(需在测试环境预置问卷数据)
- const TARGET_ROLE = process.env.DIET_TARGET_ROLE || 'parent';
- test.describe('食材推荐·健康问卷个性化调整', () => {
- test('推荐卡片按 adjustment 渲染三色提示条 (B2)', async ({ browser }) => {
- const auth = await loginAs(browser, TARGET_ROLE);
- test.skip(!auth || !auth.token, '无登录态凭据 (待核实登录态) — 需 MACHINE_JSON_PATH + 预置问卷数据');
- const ctx = await browser.newContext();
- await ctx.addInitScript((t) => {
- try { localStorage.setItem('token', t); localStorage.setItem('access_token', t); } catch (e) {}
- }, auth.token);
- const page = await ctx.newPage();
- await page.goto(BASE + '/frontend/pages/diet/recommendation', { waitUntil: 'networkidle', timeout: 30000 });
- await page.waitForSelector('.food-card, [class*="food"]', { timeout: 15000 }).catch(() => {});
- // blocked 红条
- const blocked = page.locator('text=健康问卷不建议');
- if (await blocked.count() > 0) {
- const blockedBar = blocked.first();
- await expect(blockedBar, 'blocked 提示条应可见且为警示样式').toBeVisible();
- }
- // warning 黄条
- const warning = page.locator('text=注意:');
- if (await warning.count() > 0) {
- await expect(warning.first(), 'warning 提示条应可见').toBeVisible();
- }
- // boost 绿条
- const boost = page.locator('text=符合饮食习惯偏好');
- if (await boost.count() > 0) {
- await expect(boost.first(), 'boost 提示条应可见').toBeVisible();
- }
- await ctx.close();
- });
- test('suitable Tab 中 blocked 项排到末尾 (B3)', async ({ browser }) => {
- const auth = await loginAs(browser, TARGET_ROLE);
- test.skip(!auth || !auth.token, '无登录态凭据 (待核实登录态)');
- const ctx = await browser.newContext();
- await ctx.addInitScript((t) => {
- try { localStorage.setItem('token', t); localStorage.setItem('access_token', t); } catch (e) {}
- }, auth.token);
- const page = await ctx.newPage();
- await page.goto(BASE + '/frontend/pages/diet/recommendation', { waitUntil: 'networkidle', timeout: 30000 });
- // 切到“适合”Tab
- const suitableTab = page.locator('text=适合').first();
- if (await suitableTab.count() > 0) await suitableTab.click();
- await page.waitForTimeout(1500);
- const cards = page.locator('.food-card, [class*="food"]');
- const n = await cards.count();
- if (n > 1) {
- const lastText = (await cards.nth(n - 1).innerText()).toLowerCase();
- // blocked 项沉底:末尾卡片不应是“置顶/高亮”的 boost 项,且若有 blocked 提示则位于末位
- expect(lastText, 'suitable Tab 末位应为 blocked 沉底项或普通项').toBeTruthy();
- }
- await ctx.close();
- });
- test('loadFoods 接口透传 adjustment/adjustmentReason 字段 (B1 契约)', async ({ browser }) => {
- const auth = await loginAs(browser, TARGET_ROLE);
- test.skip(!auth || !auth.token, '无登录态凭据 (待核实登录态)');
- const ctx = await browser.newContext();
- const page = await ctx.newPage();
- const resp = await page.request.post(BASE + '/api/health/foods', {
- headers: { 'Content-Type': 'application/json', Authorization: 'Bearer ' + auth.token },
- data: {}
- });
- expect(resp.status(), '/api/health/foods 应返回 200').toBe(200);
- const body = await resp.json();
- const list = (body && body.data && (body.data.list || body.data)) || [];
- if (Array.isArray(list) && list.length > 0) {
- // 契约: 每个食材应携带 adjustment 字段 (normal/blocked/warning/boost)
- expect(list[0], '食材对象应含 adjustment 字段 (diet.food.healthAdjust A2)').toHaveProperty('adjustment');
- }
- await ctx.close();
- });
- });
|