diet-recommendation-health-adjust.spec.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * E2E · 食材推荐按健康问卷个性化调整 (requirement: 2026-11-09-diet-recommendation-health-questionnaire-adjust.md)
  3. * 能力键: diet.food.healthAdjustUi (前端 B1-B3) + diet.food.healthAdjust (后端契约)
  4. *
  5. * 验收覆盖 (对应需求文档 §4):
  6. * B2 食物卡片按 adjustment 渲染提示条: blocked(红)/warning(黄)/boost(绿)/normal(不渲染)
  7. * B3 suitable Tab: adjustment==='blocked' 项排到列表末尾
  8. * B1 loadFoods 透传 adjustment/adjustmentReason (接口契约)
  9. *
  10. * 执行前提 (本自动化轮次未满足 → 待核实登录态):
  11. * 1) MACHINE_JSON_PATH 指向含 e2e_test_target.admin 的 machine.json (提供管理员口令,仓库内不硬编码真实口令)
  12. * 2) 目标成员已在 health_status / diet_preferences 写入测试问卷数据 (过敏原/用药/疾病史/绝对避免/must_eat/宗教饮食)
  13. * 3) 后端 diet.food.healthAdjust 已实现并合入 origin/cfclub (A1-A5),否则 adjustment 字段恒为 normal
  14. *
  15. * 本文件为测试资产,不修改任何生产源码;如需执行:
  16. * MACHINE_JSON_PATH=/path/to/machine.json npx playwright test tests/e2e/diet-recommendation-health-adjust.spec.js
  17. */
  18. const { test, expect } = require('@playwright/test');
  19. const { loginAs } = require('./helpers/auth');
  20. const BASE = process.env.PW_BASE_URL || 'http://cfc.iwintrue.com';
  21. // 用于验证“健康问卷不建议”的成员(需在测试环境预置问卷数据)
  22. const TARGET_ROLE = process.env.DIET_TARGET_ROLE || 'parent';
  23. test.describe('食材推荐·健康问卷个性化调整', () => {
  24. test('推荐卡片按 adjustment 渲染三色提示条 (B2)', async ({ browser }) => {
  25. const auth = await loginAs(browser, TARGET_ROLE);
  26. test.skip(!auth || !auth.token, '无登录态凭据 (待核实登录态) — 需 MACHINE_JSON_PATH + 预置问卷数据');
  27. const ctx = await browser.newContext();
  28. await ctx.addInitScript((t) => {
  29. try { localStorage.setItem('token', t); localStorage.setItem('access_token', t); } catch (e) {}
  30. }, auth.token);
  31. const page = await ctx.newPage();
  32. await page.goto(BASE + '/frontend/pages/diet/recommendation', { waitUntil: 'networkidle', timeout: 30000 });
  33. await page.waitForSelector('.food-card, [class*="food"]', { timeout: 15000 }).catch(() => {});
  34. // blocked 红条
  35. const blocked = page.locator('text=健康问卷不建议');
  36. if (await blocked.count() > 0) {
  37. const blockedBar = blocked.first();
  38. await expect(blockedBar, 'blocked 提示条应可见且为警示样式').toBeVisible();
  39. }
  40. // warning 黄条
  41. const warning = page.locator('text=注意:');
  42. if (await warning.count() > 0) {
  43. await expect(warning.first(), 'warning 提示条应可见').toBeVisible();
  44. }
  45. // boost 绿条
  46. const boost = page.locator('text=符合饮食习惯偏好');
  47. if (await boost.count() > 0) {
  48. await expect(boost.first(), 'boost 提示条应可见').toBeVisible();
  49. }
  50. await ctx.close();
  51. });
  52. test('suitable Tab 中 blocked 项排到末尾 (B3)', async ({ browser }) => {
  53. const auth = await loginAs(browser, TARGET_ROLE);
  54. test.skip(!auth || !auth.token, '无登录态凭据 (待核实登录态)');
  55. const ctx = await browser.newContext();
  56. await ctx.addInitScript((t) => {
  57. try { localStorage.setItem('token', t); localStorage.setItem('access_token', t); } catch (e) {}
  58. }, auth.token);
  59. const page = await ctx.newPage();
  60. await page.goto(BASE + '/frontend/pages/diet/recommendation', { waitUntil: 'networkidle', timeout: 30000 });
  61. // 切到“适合”Tab
  62. const suitableTab = page.locator('text=适合').first();
  63. if (await suitableTab.count() > 0) await suitableTab.click();
  64. await page.waitForTimeout(1500);
  65. const cards = page.locator('.food-card, [class*="food"]');
  66. const n = await cards.count();
  67. if (n > 1) {
  68. const lastText = (await cards.nth(n - 1).innerText()).toLowerCase();
  69. // blocked 项沉底:末尾卡片不应是“置顶/高亮”的 boost 项,且若有 blocked 提示则位于末位
  70. expect(lastText, 'suitable Tab 末位应为 blocked 沉底项或普通项').toBeTruthy();
  71. }
  72. await ctx.close();
  73. });
  74. test('loadFoods 接口透传 adjustment/adjustmentReason 字段 (B1 契约)', async ({ browser }) => {
  75. const auth = await loginAs(browser, TARGET_ROLE);
  76. test.skip(!auth || !auth.token, '无登录态凭据 (待核实登录态)');
  77. const ctx = await browser.newContext();
  78. const page = await ctx.newPage();
  79. const resp = await page.request.post(BASE + '/api/health/foods', {
  80. headers: { 'Content-Type': 'application/json', Authorization: 'Bearer ' + auth.token },
  81. data: {}
  82. });
  83. expect(resp.status(), '/api/health/foods 应返回 200').toBe(200);
  84. const body = await resp.json();
  85. const list = (body && body.data && (body.data.list || body.data)) || [];
  86. if (Array.isArray(list) && list.length > 0) {
  87. // 契约: 每个食材应携带 adjustment 字段 (normal/blocked/warning/boost)
  88. expect(list[0], '食材对象应含 adjustment 字段 (diet.food.healthAdjust A2)').toHaveProperty('adjustment');
  89. }
  90. await ctx.close();
  91. });
  92. });