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