index-home.quickCompleteOverdue.test.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * 契约级单测(TDD)—— 能力键 task.complete.overdue-toast(2026-11-16 产品拍板)
  3. *
  4. * 溯源:
  5. * 需求:用户「马智强」反馈打卡提示不成功,根因=超期任务被 deadline 守卫拒绝(TC-C-02-E)。
  6. * 产品拍板:过期任务允许打卡完成,但零奖励(不发 CF 值)。后端 TaskService.completeTask
  7. * 已改为超期完成返回 { pointsEarned:0, overdue:true };本契约锁定前端首页
  8. * quickCompleteTask 对 overdue 的提示分支(避免用户看到"获得 0 CF")。
  9. * 设计:cfc-frontend/pages/index-home/index.vue quickCompleteTask → completeTaskApi → res.data.overdue
  10. * → uni.showToast({ title: '已过期,打卡成功但不计奖励', icon: 'none' })
  11. *
  12. * 说明:沿项目「源码静态契约」范式(对齐 __tests__/pages/tasks/taskHistory.view.test.js 静态装配),
  13. * 只读真实源码断言分支结构,不 mount 巨型 index.vue。
  14. */
  15. import fs from 'fs'
  16. import path from 'path'
  17. const INDEX_HOME_PATH = path.join(__dirname, '../../pages/index-home/index.vue')
  18. /** 提取 quickCompleteTask 方法体(4 空格缩进方法,到下一个方法边界 ` },` 为止) */
  19. function quickCompleteBody(src) {
  20. const m = src.match(/quickCompleteTask\s*:\s*function\s*\([^)]*\)\s*\{([\s\S]*?)\n \},/)
  21. if (!m) throw new Error('无法在 index.vue 中定位 quickCompleteTask 方法体')
  22. return m[1]
  23. }
  24. describe('task.complete.overdue-toast:首页过期打卡提示分支', function () {
  25. let body
  26. beforeAll(function () {
  27. const src = fs.readFileSync(INDEX_HOME_PATH, 'utf8')
  28. body = quickCompleteBody(src)
  29. })
  30. test('容纳后端 overdue 标记并提示「已过期,打卡成功但不计奖励」', function () {
  31. expect(body.indexOf('res.data.overdue') >= 0).toBe(true)
  32. expect(body.indexOf('已过期') >= 0).toBe(true)
  33. expect(body.indexOf('打卡成功但不计奖励') >= 0).toBe(true)
  34. })
  35. test('overdue 分支成功 toast 用 icon none(提示而非成功动画,避免误导"获得奖励")', function () {
  36. const segStart = body.indexOf('res.data.overdue')
  37. expect(segStart).toBeGreaterThanOrEqual(0)
  38. const seg = body.slice(segStart, segStart + 400)
  39. expect(seg.indexOf("icon: 'none'") >= 0).toBe(true)
  40. })
  41. test('overdue 分支前置(早于"获得 X CF"成功分支,避免被通用成功提示吞掉)', function () {
  42. const overdueIdx = body.indexOf('res.data.overdue')
  43. const earnIdx = body.indexOf('获得 ')
  44. expect(overdueIdx).toBeGreaterThanOrEqual(0)
  45. expect(earnIdx).toBeGreaterThan(overdueIdx)
  46. })
  47. })