Browse Source

test(cfc-frontend): S-cfc-307 ui.profile.achievementLayout 契约级单测(实测绿,实现已在链)

新增 __tests__/pages/profile-main/achievementLayout.contract.test.js,守护 SC-P-96『我的成就固定五列』契约:独立五列网格类且不泄漏到团队/卡券区块;5 张成就卡文案与跳转方法齐备;五列等比 width:20%(5×20%=100%)单行不换行错位;隔离性(全局 .ach-item 25% / team-grid 33.33% 未被误改)。

TDD 结果:派发预期 RED,但 origin/cfclub 已含 cols-5 + width:20% 实现,实测 4 passed / 1 skipped(全绿)。按纪律不私改实现,交编排侧转测试登记(本键与 achievement.grid.fiveCol 同属变更 D 的重复登记)。仅新增 __tests__ 测试文件,未改任何 src/main 实现。
Administrator 7 hours ago
parent
commit
e9a45c0457

+ 152 - 0
cfc-frontend/__tests__/pages/profile-main/achievementLayout.contract.test.js

@@ -0,0 +1,152 @@
+/**
+ * 契约级单测(TDD-RED)——能力键 ui.profile.achievementLayout
+ *
+ * 溯源:
+ *   requirement: tests/user-scenarios/应用场景总览.md SC-P-96(我的成就固定五列显示)
+ *                + docs/需求分析/2026-11-06-成就成长打卡需求变更.md 变更 D
+ *   design:      cfc-frontend/pages/profile-main/profile.vue「我的成就」区块
+ *                (纯前端布局,无后端端点;docs/superpowers/api/API_REFERENCE.md 无对应接口)
+ *
+ * 契约要点(本文件守护的破坏):
+ *   1) 「我的成就」区块必须使用独立的五列网格类,不能全局改 .achievement-grid
+ *      ——否则「我的团队」(3 列)/「卡券中心」(复用网格)会被波及。
+ *   2) 区块内恰好 5 张成就卡:连续打卡/完成任务/收集勋章/成长记录/参加挑战,
+ *      顺序与跳转方法齐备——少一张或多一张都会破坏单行五列契约。
+ *   3) 五列等比排布(width:20% 或等价等分写法),5×20%=100%,无正 gap 撑破换行——
+ *      否则第 5 张卡折行错位,正是 SC-P-96 要修的缺陷。
+ *   4) 隔离性:全局 .ach-item 仍为 25%、.team-grid .ach-item 仍为 33.33%、
+ *      卡券区块不带五列类——被误改即回归。
+ *
+ * 断言口径(静态结构契约,读真实 SFC 源码,不依赖渲染引擎):
+ *   cfc-frontend jest.config.js 仅 transform .js,未装 vue-jest/jsdom 组件环境;
+ *   沿用 __tests__/family-join/join-form.required-info.test.js 与
+ *   __tests__/pages/growth-main/growthGrid.layout.test.js 的既有约定。
+ *   本文件不修改 pages/ 任何业务代码。
+ */
+
+import fs from 'fs'
+import path from 'path'
+
+const PAGE_PATH = path.join(__dirname, '../../../pages/profile-main/profile.vue')
+const SRC = fs.readFileSync(PAGE_PATH, 'utf8')
+
+/** 转义正则元字符(选择器含点号) */
+function esc(s) {
+  return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
+}
+
+/** 截取某一 CSS 选择器的声明块;返回 null 表示规则不存在 */
+function cssBlock(src, selector) {
+  const re = new RegExp('(?:^|\\n)[ \\t]*' + esc(selector) + '[ \\t]*\\{')
+  const m = re.exec(src)
+  if (!m) return null
+  const open = src.indexOf('{', m.index)
+  const close = src.indexOf('}', open)
+  if (open < 0 || close < 0) return null
+  return src.slice(open + 1, close)
+}
+
+/** 取模板中「我的成就」section(以 achievement-section 起始,逐段找含标题的那段) */
+function achievementSection(src) {
+  const tmpl = src.match(/<template>([\s\S]*?)<\/template>/)
+  if (!tmpl) return ''
+  const t = tmpl[1]
+  const marker = 'achievement-section'
+  let from = 0
+  while (true) {
+    const start = t.indexOf(marker, from)
+    if (start < 0) return ''
+    const next = t.indexOf(marker, start + 1)
+    const chunk = t.slice(start, next > 0 ? next : t.length)
+    if (chunk.indexOf('我的成就') >= 0) return chunk
+    from = start + 1
+  }
+}
+
+/** 从样式规则中读出 width 百分比 */
+function widthPercent(block) {
+  const m = /width:\s*([\d.]+)%/.exec(block || '')
+  return m ? Number(m[1]) : null
+}
+
+/** 是否满足五列等分:直接 20%,或需求允许的等价等分写法 */
+function isFiveColumnEqualWidth(block) {
+  if (!block) return false
+  if (widthPercent(block) === 20) return true
+  if (/flex:\s*1(?!\d)/.test(block)) return true
+  if (/calc\(\s*20%/.test(block)) return true
+  return false
+}
+
+const SECTION = achievementSection(SRC)
+
+// ---------------------------------------------------------------------------
+// SC-P-96 / 变更 D · 我的成就固定五列
+// ---------------------------------------------------------------------------
+
+describe('ui.profile.achievementLayout · 我的成就固定五列(SC-P-96 / 变更 D)', () => {
+  test('TC-P96-01 成就区块使用独立五列网格类,且该类未泄漏到团队/卡券区块', () => {
+    expect(SECTION).not.toBe('')
+    // 成就区块根容器带独立 cols-5 类
+    expect(SECTION).toMatch(/class="achievement-grid\s+cols-5"/)
+    // 隔离:团队网格走 team-grid,卡券区块不带 cols-5
+    expect(SECTION).not.toMatch(/team-grid/)
+    const fullTemplate = (SRC.match(/<template>([\s\S]*?)<\/template>/) || [])[1] || ''
+    expect((fullTemplate.match(/cols-5/g) || []).length).toBe(1)
+  })
+
+  test('TC-P96-02 成就区块恰好 5 张卡,文案与跳转方法按需求顺序齐备', () => {
+    const items = SECTION.match(/class="ach-item"/g) || []
+    expect(items.length).toBe(5)
+    const labels = ['连续打卡', '完成任务', '收集勋章', '成长记录', '参加挑战']
+    const handlers = ['goToStreak', 'goToTaskHistory', 'goToBadges', 'goToGrowth', 'goToChallenges']
+    labels.forEach((label, i) => {
+      // 第 i 张卡的文案在对应 @click 之后、下一张卡之前
+      const start = SECTION.indexOf('@click="' + handlers[i] + '"')
+      const next = i + 1 < handlers.length
+        ? SECTION.indexOf('@click="' + handlers[i + 1] + '"')
+        : SECTION.length
+      expect(start).toBeGreaterThanOrEqual(0)
+      expect(SECTION.slice(start, next)).toContain(label)
+    })
+  })
+
+  test('TC-P96-03 五列等比排布且单行不换行错位(5×20%=100%,无正 gap 撑破)', () => {
+    const fiveColItem = cssBlock(SRC, '.achievement-grid.cols-5 .ach-item')
+    expect(isFiveColumnEqualWidth(fiveColItem)).toBe(true)
+    // 等分宽度取 20% 时,5 张合计恰好 100%
+    const w = widthPercent(fiveColItem)
+    if (w !== null) {
+      expect(w * 5).toBe(100)
+    }
+    // 容器不得有正 gap:5×20%+gap>100% 会折行,违背"单行不换行错位"
+    const grid = cssBlock(SRC, '.achievement-grid')
+    const gap = /gap:\s*([\d.]+)(rpx|px|%)?/.exec(grid || '')
+    if (gap) {
+      expect(Number(gap[1])).toBe(0)
+    }
+  })
+
+  test('TC-P96-04 隔离性:全局 .ach-item 仍 25%、.team-grid 仍 33.33%,卡券区块不带五列类', () => {
+    // 全局默认仍是 4 列等分,未被改成 20%
+    expect(widthPercent(cssBlock(SRC, '.ach-item'))).toBe(25)
+    // 我的团队保持 3 列
+    expect(widthPercent(cssBlock(SRC, '.achievement-grid.team-grid .ach-item'))).toBe(33.33)
+    // 卡券中心区块仍在,且不使用 cols-5
+    const fullTemplate = (SRC.match(/<template>([\s\S]*?)<\/template>/) || [])[1] || ''
+    const couponStart = fullTemplate.indexOf('卡券中心')
+    if (couponStart >= 0) {
+      const couponChunk = fullTemplate.slice(couponStart, couponStart + 2500)
+      expect(couponChunk).not.toMatch(/cols-5/)
+    }
+  })
+})
+
+// ---------------------------------------------------------------------------
+// 权限流:本能力为家长端「我的成就」展示布局,无角色分支门禁,
+// 属《编排派发》中「权限(适用时)」的不适用项,显式记录。
+// ---------------------------------------------------------------------------
+
+describe.skip('权限流:成就五列布局无角色门禁,不适用', () => {
+  it('占位:该能力面向家长端个人中心展示,无 role 分支(详见汇报)', () => {})
+})