report-upload-guide-config.test.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /**
  2. * 验收逻辑守卫(TDD-RED):报告上传页「支持的报告类型」配置化改版(后台可配置 guide-list)
  3. *
  4. * 背景(2026-11-27 报告上传说明区配置化改版,四条用户需求):
  5. * ① 去掉页面最下面的「关联商品」区;
  6. * ② 素质测评/菌群检测等条目里的「商品+购买按钮」缩小、与类型标题放同一行;
  7. * ③ 菌群检测报告、体检报告等所有报告类型一致;
  8. * ④ 该区域改为后台可配置:标题、内容、关联商品都通过后台配置。
  9. * 数据源 = utils/api.js 的 getReportTypeLinks()(POST /api/report-parser/types/links),
  10. * 返回全部 is_active=1 类型,字段含 typeId/displayName/description/icon/sortOrder/
  11. * linkedProductId/productName,按 sortOrder 升序 + id 升序稳定排序。
  12. *
  13. * 说明(沿用 cfc-frontend/__tests__/aiyunyi/report-upload.uploadCard.contract.test.js 的
  14. * 「加载真实 SFC 源码求值」约定):
  15. * jest 配置为 node 环境、无 vue-jest 挂载组件;本文件直接读取真实 report-upload.vue 的
  16. * <script>/<template> 源码——
  17. * - <script>:把 import 语句替换为注入桩后求值,得到真实组件的
  18. * computed(sortedGuideTypes) 与 methods(guideIcon/guideIconClass) 做行为级断言;
  19. * - <template>:读取模板文本做结构契约断言(guide-title-row 标题与购买按钮同行、
  20. * 购买按钮仅在 linkedProductId 时出现、无 linked-products 残留、无 guide-link 残留)。
  21. * 同时镜像三个纯决策函数(sortedGuideTypes/guideIcon/hasBuyButton)做边界测试,
  22. * 与组件实现逐位对齐(排序兜底/图标回退/按钮可见性)。
  23. * 若 GREEN 阶段实现方补齐上述契约,本文件断言即翻绿;当前实现缺失 → 预期 RED。
  24. * 本文件不修改 pages/、pages.json、utils/ 任何业务代码。
  25. */
  26. import fs from 'fs'
  27. import path from 'path'
  28. var REPORT_UPLOAD_PATH = path.join(__dirname, '../../pages/health/report-upload.vue')
  29. /** 把 ES import 语句替换为从 globalThis.__reportUploadStubs 取桩的声明(不解析模块,避免污染业务实现) */
  30. function stubImports(script, stubMap) {
  31. return script.replace(/import\s+([^;]+?)\s+from\s+['"]([^'"]+)['"];?/g, function (m, clause, id) {
  32. var names = []
  33. var c = clause.trim()
  34. var braceIdx = c.indexOf('{')
  35. if (braceIdx >= 0) {
  36. var def = c.slice(0, braceIdx).replace(/,\s*$/, '').trim()
  37. if (def) names.push(def)
  38. var named = c.slice(braceIdx + 1, c.lastIndexOf('}'))
  39. named.split(',').forEach(function (part) {
  40. var p = part.trim()
  41. if (!p) return
  42. var parts = p.split(/\s+as\s+/)
  43. var local = parts.length > 1 ? parts[1].trim() : parts[0].trim()
  44. if (local) names.push(local)
  45. })
  46. } else if (c.indexOf('*') === 0) {
  47. var nsMatch = c.match(/\*\s+as\s+(\w+)/)
  48. if (nsMatch) names.push(nsMatch[1])
  49. } else {
  50. names.push(c)
  51. }
  52. return names.map(function (n) {
  53. return 'var ' + n + ' = (globalThis.__reportUploadStubs[' + JSON.stringify(id) + '] || {})[' + JSON.stringify(n) + '];'
  54. }).join('\n')
  55. })
  56. }
  57. /** 加载真实 SFC:返回 { component, template }(component = 真实 <script> 的 export default 对象) */
  58. function loadReportUpload() {
  59. var src = fs.readFileSync(REPORT_UPLOAD_PATH, 'utf8')
  60. var tmplMatch = src.match(/<template>([\s\S]*?)<\/template>/)
  61. var scriptMatch = src.match(/<script>([\s\S]*?)<\/script>/)
  62. if (!scriptMatch) throw new Error('无法在 report-upload.vue 中定位 <script> 块')
  63. var script = stubImports(scriptMatch[1], globalThis.__reportUploadStubs || {})
  64. script = script.replace(/export default\s*\{/, 'module.exports = {')
  65. var mod = { exports: {} }
  66. // eslint-disable-next-line no-new-func
  67. var fn = new Function('module', 'exports', script)
  68. fn(mod, mod.exports)
  69. return { component: mod.exports, template: tmplMatch ? tmplMatch[1] : '' }
  70. }
  71. /** 依据真实组件的 data()/computed/methods 构造可驱动上下文 */
  72. function instantiate(component) {
  73. var vm = { $set: function (obj, key, val) { obj[key] = val } }
  74. var data = typeof component.data === 'function' ? component.data.call(vm) : (component.data || {})
  75. Object.keys(data).forEach(function (k) { vm[k] = data[k] })
  76. var methods = component.methods || {}
  77. Object.keys(methods).forEach(function (k) { vm[k] = methods[k] })
  78. return vm
  79. }
  80. /** 取真实组件的 computed 定义(未被 instantiate 展开到 vm 上,需单独调用求值) */
  81. function getComputed(component) {
  82. return component.computed || {}
  83. }
  84. // ---------------------------------------------------------------------------
  85. // 测试夹具
  86. // ---------------------------------------------------------------------------
  87. beforeEach(function () {
  88. globalThis.__reportUploadStubs = {
  89. '../../utils/memberNormalize.js': {
  90. normalizeMembers: function (list) { return list || [] },
  91. memberIdOf: function (m) { return m && m.id }
  92. },
  93. '../../utils/api.js': {
  94. uploadReportOnly: jest.fn(function () { return Promise.resolve({ data: {} }) }),
  95. getReportCollectStatus: jest.fn(function () { return Promise.resolve({ data: {} }) }),
  96. getFamilyMemberList: jest.fn(function () { return Promise.resolve({ data: [] }) }),
  97. requestReportTransfer: jest.fn(function () { return Promise.resolve({ code: 200 }) }),
  98. getReportTypeLinks: jest.fn(function () { return Promise.resolve({ data: [] }) })
  99. },
  100. '../../utils/image-picker.js': { pickImage: jest.fn() },
  101. '@/mixins/font-scale.js': {
  102. fontScaleMixin: { data: function () { return { fontScale: 1 } }, onShow: function () {} }
  103. }
  104. }
  105. })
  106. afterEach(function () {
  107. delete globalThis.__reportUploadStubs
  108. })
  109. // ---------------------------------------------------------------------------
  110. // AC1 sortedGuideTypes computed:sortOrder 升序 + typeId 稳定兜底(§配置化改版)
  111. // ---------------------------------------------------------------------------
  112. describe('AC1 sortedGuideTypes computed', function () {
  113. it('组件 computed 中定义 sortedGuideTypes', function () {
  114. var component = loadReportUpload().component
  115. expect(typeof getComputed(component).sortedGuideTypes).toBe('function')
  116. })
  117. it('sortOrder 升序排列', function () {
  118. var component = loadReportUpload().component
  119. var vm = instantiate(component)
  120. vm.reportTypeLinks = [
  121. { typeId: 'dan_a2', sortOrder: 20, displayName: '素质测评' },
  122. { typeId: 'gut_flora', sortOrder: 10, displayName: '菌群检测' },
  123. { typeId: 'physical_exam', sortOrder: 30, displayName: '体检报告' }
  124. ]
  125. var out = getComputed(component).sortedGuideTypes.call(vm)
  126. expect(out.map(function (x) { return x.typeId })).toEqual(['gut_flora', 'dan_a2', 'physical_exam'])
  127. })
  128. it('sortOrder 空(null/undefined)按 100 兜底排序,相等按 typeId 字符串稳定', function () {
  129. var component = loadReportUpload().component
  130. var vm = instantiate(component)
  131. vm.reportTypeLinks = [
  132. { typeId: 'tongue', sortOrder: null, displayName: '舌象照片' },
  133. { typeId: 'body_fat', displayName: '体脂秤报告' }, // undefined sortOrder
  134. { typeId: 'dan_a2', sortOrder: 10, displayName: '素质测评' }
  135. ]
  136. var out = getComputed(component).sortedGuideTypes.call(vm)
  137. expect(out.map(function (x) { return x.typeId })).toEqual(['dan_a2', 'body_fat', 'tongue'])
  138. })
  139. it('reportTypeLinks 为空/非数组 → 返回 [](不抛错)', function () {
  140. var component = loadReportUpload().component
  141. var vm = instantiate(component)
  142. vm.reportTypeLinks = null
  143. expect(getComputed(component).sortedGuideTypes.call(vm)).toEqual([])
  144. vm.reportTypeLinks = 'not-array'
  145. expect(getComputed(component).sortedGuideTypes.call(vm)).toEqual([])
  146. })
  147. it('不修改原数组(深比较)', function () {
  148. var component = loadReportUpload().component
  149. var vm = instantiate(component)
  150. vm.reportTypeLinks = [
  151. { typeId: 'dan_a2', sortOrder: 20 },
  152. { typeId: 'gut_flora', sortOrder: 10 }
  153. ]
  154. var before = JSON.stringify(vm.reportTypeLinks)
  155. getComputed(component).sortedGuideTypes.call(vm)
  156. expect(JSON.stringify(vm.reportTypeLinks)).toBe(before)
  157. })
  158. })
  159. // ---------------------------------------------------------------------------
  160. // AC2 guideIcon(item):icon 优先,空回退 typeId 映射,未知回退 📄
  161. // ---------------------------------------------------------------------------
  162. describe('AC2 guideIcon(item) 图标回退', function () {
  163. it('组件 methods 中定义 guideIcon', function () {
  164. var component = loadReportUpload().component
  165. expect(typeof instantiate(component).guideIcon).toBe('function')
  166. })
  167. it('有 icon 字段 → 优先使用', function () {
  168. var vm = instantiate(loadReportUpload().component)
  169. expect(vm.guideIcon({ typeId: 'gut_flora', icon: '🦠' })).toBe('🦠')
  170. expect(vm.guideIcon({ typeId: 'unknown_xyz', icon: '🌟' })).toBe('🌟')
  171. })
  172. it('icon 为空 → 回退 typeId 默认映射(gut_flora/dan_a2/physical_exam/tongue/body_fat)', function () {
  173. var vm = instantiate(loadReportUpload().component)
  174. expect(vm.guideIcon({ typeId: 'gut_flora' })).toBe('📄')
  175. expect(vm.guideIcon({ typeId: 'dan_a2', icon: '' })).toBe('🧠')
  176. expect(vm.guideIcon({ typeId: 'physical_exam', icon: null })).toBe('📋')
  177. expect(vm.guideIcon({ typeId: 'tongue' })).toBe('👅')
  178. expect(vm.guideIcon({ typeId: 'body_fat' })).toBe('⚖️')
  179. })
  180. it('未知 typeId / 无 typeId / item 为 null → 回退 📄', function () {
  181. var vm = instantiate(loadReportUpload().component)
  182. expect(vm.guideIcon({ typeId: 'some_new_type' })).toBe('📄')
  183. expect(vm.guideIcon({})).toBe('📄')
  184. expect(vm.guideIcon(null)).toBe('📄')
  185. })
  186. })
  187. // ---------------------------------------------------------------------------
  188. // AC3 guideIconClass(idx):循环四色视觉(blue/purple/orange/teal 按位取模)
  189. // ---------------------------------------------------------------------------
  190. describe('AC3 guideIconClass(idx) 循环四色', function () {
  191. it('组件 methods 中定义 guideIconClass', function () {
  192. var component = loadReportUpload().component
  193. expect(typeof instantiate(component).guideIconClass).toBe('function')
  194. })
  195. it('按 idx 取模返回四色 class', function () {
  196. var vm = instantiate(loadReportUpload().component)
  197. expect(vm.guideIconClass(0)).toBe('guide-icon-blue')
  198. expect(vm.guideIconClass(1)).toBe('guide-icon-purple')
  199. expect(vm.guideIconClass(2)).toBe('guide-icon-orange')
  200. expect(vm.guideIconClass(3)).toBe('guide-icon-teal')
  201. expect(vm.guideIconClass(4)).toBe('guide-icon-blue') // 循环
  202. })
  203. })
  204. // ---------------------------------------------------------------------------
  205. // AC4 模板结构契约:标题与购买按钮同行、购买按钮仅在 linkedProductId 时出现、
  206. // 无 linked-products 残留、无 guide-link 残留(§配置化改版 ①②③)
  207. // ---------------------------------------------------------------------------
  208. describe('AC4 模板结构契约', function () {
  209. var template
  210. beforeAll(function () {
  211. globalThis.__reportUploadStubs = {}
  212. template = loadReportUpload().template
  213. })
  214. it('guide-item 为 v-for 配置驱动(含 sortedGuideTypes 数据源,§④后台可配置)', function () {
  215. expect(template).toMatch(/v-for="[^"]*\bsortedGuideTypes\b/)
  216. })
  217. it('标题与购买按钮同一行:guide-title-row 内含 guide-name 与 guide-buy(§②同行)', function () {
  218. var start = template.indexOf('guide-title-row')
  219. expect(start).toBeGreaterThan(-1)
  220. var rowStart = template.lastIndexOf('<view class="guide-title-row"', start)
  221. expect(rowStart).toBeGreaterThan(-1)
  222. var rowEnd = template.indexOf('</view>', start)
  223. var block = template.slice(rowStart, rowEnd)
  224. expect(block).toMatch(/class="guide-name"/)
  225. expect(block).toMatch(/class="guide-buy"/)
  226. })
  227. it('购买按钮仅在 linkedProductId 时出现(v-if="item.linkedProductId",§②缩小+一致)', function () {
  228. expect(template).toMatch(/class="guide-buy"[^>]*v-if="item\.linkedProductId"|v-if="item\.linkedProductId"[^>]*class="guide-buy"/)
  229. })
  230. it('已删除重复的 guide-link(查看商品)块(§②去重复,只留一个小购买按钮)', function () {
  231. expect(template).not.toMatch(/class="guide-link"/)
  232. })
  233. it('已删除「关联商品」区(linked-products-section,§①去掉底部关联商品区)', function () {
  234. expect(template).not.toMatch(/linked-products-section/)
  235. expect(template).not.toMatch(/linked-product-card/)
  236. })
  237. })
  238. // ---------------------------------------------------------------------------
  239. // 镜像纯决策函数(边界守卫,与组件实现逐位对齐)
  240. // ---------------------------------------------------------------------------
  241. function sortedGuideTypesMirror(links) {
  242. if (!Array.isArray(links)) return []
  243. return links.slice().sort(function (a, b) {
  244. var sa = (a && a.sortOrder !== null && a.sortOrder !== undefined) ? a.sortOrder : 100
  245. var sb = (b && b.sortOrder !== null && b.sortOrder !== undefined) ? b.sortOrder : 100
  246. if (sa !== sb) return sa - sb
  247. var ta = String((a && a.typeId) || '')
  248. var tb = String((b && b.typeId) || '')
  249. return ta < tb ? -1 : (ta > tb ? 1 : 0)
  250. })
  251. }
  252. function hasBuyButtonMirror(item) {
  253. if (!item) return false
  254. var pid = item.linkedProductId
  255. if (pid === null || pid === undefined || pid === '') return false
  256. if (pid === 0) return false
  257. return true
  258. }
  259. describe('镜像守卫 sortedGuideTypesMirror', function () {
  260. it('非数组/空 → []', function () {
  261. expect(sortedGuideTypesMirror(null)).toEqual([])
  262. expect(sortedGuideTypesMirror(undefined)).toEqual([])
  263. expect(sortedGuideTypesMirror('not-array')).toEqual([])
  264. expect(sortedGuideTypesMirror([])).toEqual([])
  265. })
  266. it('sortOrder 升序;空按 100 兜底;相等按 typeId 稳定', function () {
  267. var links = [
  268. { typeId: 'tongue', sortOrder: null },
  269. { typeId: 'body_fat' },
  270. { typeId: 'dan_a2', sortOrder: 10 }
  271. ]
  272. expect(sortedGuideTypesMirror(links).map(function (x) { return x.typeId })).toEqual(['dan_a2', 'body_fat', 'tongue'])
  273. })
  274. it('不修改原数组', function () {
  275. var links = [{ typeId: 'dan_a2', sortOrder: 20 }, { typeId: 'gut_flora', sortOrder: 10 }]
  276. var before = JSON.stringify(links)
  277. sortedGuideTypesMirror(links)
  278. expect(JSON.stringify(links)).toBe(before)
  279. })
  280. })
  281. describe('镜像守卫 hasBuyButtonMirror', function () {
  282. it('linkedProductId 非空(正数/字符串数字) → true', function () {
  283. expect(hasBuyButtonMirror({ linkedProductId: 100 })).toBe(true)
  284. expect(hasBuyButtonMirror({ linkedProductId: '200' })).toBe(true)
  285. })
  286. it('linkedProductId = 0 / null / undefined / 空串 → false;item null → false', function () {
  287. expect(hasBuyButtonMirror({ linkedProductId: 0 })).toBe(false)
  288. expect(hasBuyButtonMirror({ linkedProductId: null })).toBe(false)
  289. expect(hasBuyButtonMirror({})).toBe(false)
  290. expect(hasBuyButtonMirror({ linkedProductId: '' })).toBe(false)
  291. expect(hasBuyButtonMirror(null)).toBe(false)
  292. })
  293. })