k8-contract-check.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/usr/bin/env node
  2. /**
  3. * K8 终身会员权益 — 需求契约静态校验(只读 / 幂等 / 无需编译 Java)
  4. *
  5. * 背景:2026-11-19 窗口引入《还原水机 K8 终身会员权益 spec》与实现(迁移499 + 维护提醒 +
  6. * 滤芯置顶 + 文章降权)。主干此时不可编译(C-311-1),Java 契约测试无法执行,
  7. * 故本脚本以「源码文本断言」形式把 spec 条款固化为可执行证据。
  8. *
  9. * 用法:node tests/scripts/k8-contract-check.js [--json]
  10. * 退出码:0 全部通过;1 存在失败项
  11. *
  12. * 判据文件:
  13. * cfc-backend/src/main/resources/schema.sql
  14. * cfc-backend/src/main/java/com/etotem/cfc/config/DatabaseInitializer.java
  15. * cfc-backend/src/main/java/com/etotem/cfc/service/K8MaintenanceCalculator.java
  16. * cfc-backend/src/main/java/com/etotem/cfc/controller/DeviceMaintenanceController.java
  17. * cfc-backend/src/main/java/com/etotem/cfc/service/ContentRecommendService.java
  18. * docs/superpowers/specs/2026-11-19-k8-lifetime-benefits-design.md
  19. */
  20. 'use strict';
  21. const fs = require('fs');
  22. const path = require('path');
  23. const ROOT = path.resolve(__dirname, '..', '..');
  24. const read = (rel) => {
  25. const p = path.join(ROOT, rel);
  26. return fs.existsSync(p) ? fs.readFileSync(p, 'utf8') : '';
  27. };
  28. const schema = read('cfc-backend/src/main/resources/schema.sql');
  29. const initializer = read('cfc-backend/src/main/java/com/etotem/cfc/config/DatabaseInitializer.java');
  30. const calc = read('cfc-backend/src/main/java/com/etotem/cfc/service/K8MaintenanceCalculator.java');
  31. const ctrl = read('cfc-backend/src/main/java/com/etotem/cfc/controller/DeviceMaintenanceController.java');
  32. const content = read('cfc-backend/src/main/java/com/etotem/cfc/service/ContentRecommendService.java');
  33. const spec = read('docs/superpowers/specs/2026-11-19-k8-lifetime-benefits-design.md');
  34. const results = [];
  35. function check(id, clause, desc, fn) {
  36. let ok = false;
  37. let detail = '';
  38. try {
  39. const r = fn();
  40. ok = r === true || (r && r.ok === true);
  41. detail = (r && r.detail) || '';
  42. } catch (e) {
  43. ok = false;
  44. detail = 'EXCEPTION ' + e.message;
  45. }
  46. results.push({ id, clause, desc, ok, detail });
  47. }
  48. /* ---------- §4.3 数据模型 ---------- */
  49. check('K8-C01', '§4.3', 'schema.sql 含 device_maintenance_reminders 表', () =>
  50. ({ ok: /CREATE TABLE IF NOT EXISTS device_maintenance_reminders/.test(schema) }));
  51. check('K8-C02', '§4.3', '唯一键 uk_reminder = (buyer_id, product_id, service_type, due_date)', () => {
  52. const m = schema.match(/UNIQUE KEY uk_reminder \(([^)]*)\)/);
  53. const cols = m ? m[1].split(',').map((s) => s.trim()) : [];
  54. return { ok: cols.length === 4 && cols.join(',') === 'buyer_id,product_id,service_type,due_date', detail: cols.join('|') };
  55. });
  56. check('K8-C03', '§4.3', 'schema.sql 含 idx_family_status (family_id, status)', () =>
  57. ({ ok: /INDEX idx_family_status \(family_id, status\)/.test(schema) }));
  58. check('K8-C04', '§4.3', 'status 默认 pending 且白名单含 pending/done/dismissed', () =>
  59. ({ ok: /status VARCHAR\(20\) NOT NULL DEFAULT 'pending'/.test(schema) && /pending\/done\/dismissed/.test(schema) }));
  60. /* ---------- §4.1 交易补录幂等 ---------- */
  61. check('K8-C05', '§4.1', 'DatabaseInitializer 含迁移499 幂等方法 migrateK8LifetimeBenefits', () =>
  62. ({ ok: /migrateK8LifetimeBenefits/.test(initializer) }));
  63. check('K8-C06', '§4.1', 'order_no 幂等守卫(INSERT IGNORE + K8-LIFETIME 前缀)', () => {
  64. const hasIgnore = /INSERT IGNORE INTO product_orders/.test(initializer);
  65. const hasNo = /orderNo = "K8-LIFETIME-"/.test(initializer);
  66. return { ok: hasIgnore && hasNo, detail: `INSERT_IGNORE=${hasIgnore} ORDER_NO_PREFIX=${hasNo}` };
  67. });
  68. check('K8-C07', '§4.1', '3 名终身会员 (53/71, 54/57, 55/59) 全部登记', () => {
  69. const hit = ['{53L, 71L', '{54L, 57L', '{55L, 59L'].filter((s) => initializer.includes(s)).length;
  70. return { ok: hit === 3, detail: `${hit}/3` };
  71. });
  72. check('K8-C08', '§4.1', '迁移499 与 schema.sql 双处同步(表 DDL 同时存在于两处)', () => {
  73. const inInit = /CREATE TABLE IF NOT EXISTS device_maintenance_reminders/.test(initializer);
  74. return { ok: inInit && /CREATE TABLE IF NOT EXISTS device_maintenance_reminders/.test(schema) };
  75. });
  76. /* ---------- §4.2 日期推算 ---------- */
  77. check('K8-C09', '§4.2', 'deriveMaintenance 返回 4 个日期(wash/filter 到期 + 两窗口起点)', () => {
  78. const need = ['washDueDate', 'filterDueDate', 'washWindowStart', 'filterWindowStart'];
  79. const miss = need.filter((n) => !calc.includes(n));
  80. return { ok: miss.length === 0, detail: miss.length ? 'MISSING ' + miss.join(',') : '' };
  81. });
  82. // 说明:实现走 LocalDate#plusMonths(自带月末截断),javadoc 已声明「8/31+6月=2/28」;
  83. // 早期版本按 lengthOfMonth/lastDay 断言属用例错(假红),已按实现语义修正。
  84. check('K8-C10', '§4.2', '月份加法月末截断(plusMonths 截断语义 + 声明可查)', () => {
  85. const plus = /plusMonths\(WASH_MONTHS\)/.test(calc) && /plusMonths\(FILTER_MONTHS\)/.test(calc);
  86. const decl = /截断/.test(calc) || /月末/.test(calc);
  87. return { ok: plus && decl, detail: `plusMonths=${plus} truncation_documented=${decl}` };
  88. });
  89. check('K8-C11', '§4.2', '窗口 = [到期-7d, 到期] 含首尾(8 天)', () => {
  90. const k = /WINDOW_DAYS\s*=\s*7/.test(calc);
  91. const sub = /minusDays\(WINDOW_DAYS\)/.test(calc);
  92. const incl = /isBefore\(windowStart\)/.test(calc);
  93. return { ok: k && sub && incl, detail: `const7=${k} minus_D7=${sub} inclusive=${incl}` };
  94. });
  95. /* ---------- §4.3 接口契约 ---------- */
  96. check('K8-C12', '§4.3', 'spec 约定查询入口 POST /api/device/maintenance/reminders(根路径)', () => {
  97. const specSays = /POST \/api\/device\/maintenance\/reminders/.test(spec);
  98. const implRoot = /@PostMapping\(\s*\)/.test(ctrl) || /@PostMapping$/.test(ctrl);
  99. return { ok: implRoot, detail: `spec_documented=${specSays} impl_root_mapping=${implRoot}` };
  100. });
  101. check('K8-C13', '§4.3', 'spec 约定后台接口 /api/admin/device/maintenance/reminders(独立前缀)', () => {
  102. const implPath = /@RequestMapping\("\/api\/admin\/device\/maintenance\/reminders"\)/.test(ctrl);
  103. const actual = (/@RequestMapping\("([^"]+)"\)/.exec(ctrl) || [])[1] || '';
  104. return { ok: implPath, detail: `actual_class_mapping=${actual}` };
  105. });
  106. check('K8-C14', '§4.3', '后台查询支持分页(page/size 参数)', () =>
  107. ({ ok: /(page|size|Page<|IPage|PageHelper)/.test(ctrl) }));
  108. check('K8-C15', '§4.3', '状态更新缺 status 时应报错而非静默按 done 处理', () => {
  109. const silentDone = /s == null \? "done"/.test(ctrl);
  110. return { ok: !silentDone, detail: silentDone ? 'DETECTED silent default done' : '' };
  111. });
  112. check('K8-C16', '§4.3', '状态更新校验归属(familyId 或 buyerId 匹配)', () =>
  113. ({ ok: /无权操作他人提醒/.test(ctrl) }));
  114. check('K8-C17', '§4.3', '后台查询需 admin 角色', () =>
  115. ({ ok: /!"admin"\.equals\(role\)/.test(ctrl) }));
  116. check('K8-C18', '§4.3', '查询仅返回 pending 且 today >= window_start(逾期仍返回)', () =>
  117. ({ ok: /isBefore\(r\.getWindowStartDate/.test(ctrl) && /"pending"\.equals\(r\.getStatus\(\)\)/.test(ctrl) }));
  118. /* ---------- §4.4 / §4.5 推荐 ---------- */
  119. check('K8-C19', '§4.4', 'ContentRecommendService 商品分支接入 K8 滤芯置顶', () =>
  120. ({ ok: /K8Recommendation|k8/i.test(content) }));
  121. check('K8-C20', '§4.5', 'ContentRecommendService 文章分支接入 K8 降权', () => {
  122. const k8 = (content.match(/[Kk]8/g) || []).length;
  123. return { ok: k8 >= 1, detail: `k8_refs=${k8}` };
  124. });
  125. /* ---------- 输出 ---------- */
  126. const failed = results.filter((r) => !r.ok);
  127. if (process.argv.includes('--json')) {
  128. console.log(JSON.stringify({ total: results.length, passed: results.length - failed.length, failed: failed.length, results }, null, 2));
  129. } else {
  130. console.log('K8 需求契约静态校验 —— spec 2026-11-19-k8-lifetime-benefits');
  131. console.log('='.repeat(74));
  132. for (const r of results) {
  133. console.log(`${r.ok ? 'PASS' : 'FAIL'} ${r.id} ${r.clause} ${r.desc}${r.detail ? ' [' + r.detail + ']' : ''}`);
  134. }
  135. console.log('-'.repeat(74));
  136. console.log(`TOTAL ${results.length} PASS ${results.length - failed.length} FAIL ${failed.length}`);
  137. }
  138. process.exit(failed.length === 0 ? 0 : 1);