health-knowledge-base.spec.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * 健康知识库 E2E 测试(health knowledge base)
  3. *
  4. * 后端模块 HealthKnowledgeBaseController
  5. * - POST /api/health/knowledge/query 单条查询
  6. * - POST /api/health/knowledge/batch-query 批量查询
  7. * - POST /api/health/knowledge/import 一键导入预置 JSON
  8. *
  9. * 前端路由:通过 admin 端的 /api/admin/* 接口触发同样后端
  10. * 测试场景:直接走后端 API + 简单 admin token,验证接口联通即可
  11. */
  12. const { test, expect } = require('@playwright/test');
  13. function base() { return 'http' + String.fromCharCode(58,47,47) + 'cfc.iwintrue.com'; }
  14. function auth(t) { return t ? { Authorization: 'Bearer ' + t } : {}; }
  15. async function login(page) {
  16. const r = await page.request.post(base() + '/api/admin-auth/login-by-password', {
  17. data: { phone: '13800138000', password: 'admin123' }
  18. });
  19. if (!r.ok()) return null;
  20. const j = await r.json();
  21. return j?.data?.token || null;
  22. }
  23. test.describe('health knowledge base', () => {
  24. test('login + reachable', async ({ page }) => {
  25. const t = await login(page);
  26. expect(t).toBeTruthy();
  27. const r = await page.request.post(base() + '/api/health/knowledge/query', {
  28. data: { itemType: 'food', itemName: '苹果' },
  29. headers: auth(t)
  30. });
  31. expect(r).toBeTruthy();
  32. });
  33. test('query requires itemType + itemName', async ({ page }) => {
  34. const t = await login(page);
  35. const r = await page.request.post(base() + '/api/health/knowledge/query', {
  36. data: {},
  37. headers: auth(t)
  38. });
  39. const j = await r.json();
  40. expect(j.code).not.toBe(200);
  41. });
  42. test('batch-query empty -> error', async ({ page }) => {
  43. const t = await login(page);
  44. const r = await page.request.post(base() + '/api/health/knowledge/batch-query', {
  45. data: { queries: [] },
  46. headers: auth(t)
  47. });
  48. const j = await r.json();
  49. expect(j.code).not.toBe(200);
  50. });
  51. test('import endpoint accessible', async ({ page }) => {
  52. const t = await login(page);
  53. const r = await page.request.post(base() + '/api/health/knowledge/import', {
  54. headers: auth(t)
  55. });
  56. expect(r).toBeTruthy();
  57. });
  58. });