fix(defaults): seed 改补缺、.prose 层真生效、写库 E2E 走临时库
三件事的共同点:默认值本来就在鼓励做错事。
1) seed 不再无条件覆盖(OPEN-08 / §17.2 根治)
`seedItems()` 原为 upsert 全字段覆盖、案例还无条件 deleteMany ⇒
「跑一次 seed」= 「冲掉 CMS 人工编辑」。改为默认补缺:
已存在同 slug 的行跳过并计入汇总(全跳过时日志明写「未写任何一行」),
只有显式 --force 才覆盖/删除,未知参数直接中止。
- src/lib/seed-write-policy.ts(纯函数)+ 14 例单测
- 临时库三向负对照:人工编辑存活 / --force 还原 / --forced 零写入
2) .prose 排版层从「从未定义」变成真规则(OPEN-09 / §18.6-1)
privacy/terms 写了两年 `prose prose-lg`,而仓里没装
@tailwindcss/typography ⇒ 两个类从未生成过任何 CSS。手写一份(ADR-0010),
整层用 :where() 保持零特异性 —— 库里已存的法律页 HTML 自带 inline utility,
层若按 `.prose p`(0,1,1) 写就会盖掉它们(实测:/terms 呼出块标签被染灰 329px、
日期行高 1.65→1.85 共 1222px)。行高与节奏刻意留在标记上。
- src/app/globals.css + 12 例判据(含「层内不许裸选择器」反方向守卫)
- 列表选择器钉死为 `:where(.prose) ul:where(:not(.list-none))`:
特异性 0 会输给基座 `ul`(0,0,1)(点静默消失、padding 却生效)
- 等价性:新种子库(无 inline 类)vs 已入库基线 = 0 像素差异、同高
- 视觉只读复核:/privacy /terms × 5 project = 10/10 通过,未写基线
3) 写库型 E2E 从「未授权」变成「可随时跑」(OPEN-07 / AUTH-D4 闭合)
11 例改为指向 /tmp 一次性 SQLite(migrate → seed → 路由预热 → next dev),
`npm run test:e2e:write-db` ⇒ 11 passed / 0 failed,prisma/dev.db 的
inode+size+mtime 跑前跑后不变。三个坑记入 lessons §5.66
(cookie domain 字面量、revalidate 密钥要同源、dev 懒编译需预热)。
文档:qa-tracker §19 新建 OPEN-* 登记表(15 行,只增不改名)、lessons §5.65–5.67、
ADR-0010、CONTEXT 决策表 3 行、quality-gates / testing / CLAUDE / README 同步。
门禁:type-check / type-check:e2e / lint(0 error, 88 warning) / jest(162 套件 1904 例)
/ check:grammar(21/21) / selftest / check:accents / check:dead / check:motion
/ check:a11y 全部 EXIT=0。
已知:check:visual 整段红是 pre-existing(check:accents:selftest 在 HEAD 纯净树上
同样 8/10 红),登记 OPEN-15;写库 E2E 有残差抖动,宣称前须连续两次 EXIT=0。
This commit is contained in:
1 parent
683f693b48
commit
9d144f623f
20 files changed
+1091
-250
No files matched your search
@@ -0,0 +1,107 @@
|
||||
/**
|
||||
* seed 的写策略(2026-11-25)。
|
||||
*
|
||||
* 背景:2026-11-24 那次 seed 事故(qa-tracker §17.2)不是「seed 写错了值」,而是
|
||||
* **seed 依据「种子文件是唯一真相」这一隐含假设无条件覆盖** —— `upsert` 的 update 分支
|
||||
* 把 CMS 里人工改过的正文冲回种子版本,`deleteMany` 更是无条件删。事故前没有任何一道闸
|
||||
* 能区分「我想初始化」与「我想重置」。
|
||||
*
|
||||
* 本模块把这条边界变成**显式的、可单测的纯函数**:
|
||||
* - 默认 = 补缺(缺行才写,已有行不碰)
|
||||
* - `--force` = 覆盖(明确表达「我要把库重置成种子状态」)
|
||||
*
|
||||
* 刻意**不做**内容指纹/时间戳比对:仓里没有「人工编辑」这一状态源,凭 `updatedAt` 猜
|
||||
* 会把「上一次 seed 自己写的行」误判成人工编辑,等于造出第二个真相源。
|
||||
* 判据只认两件可观测的事实:行是否存在、命令行是否显式 --force。
|
||||
*/
|
||||
|
||||
export type SeedWriteAction = 'create' | 'update' | 'skip';
|
||||
|
||||
export interface SeedMode {
|
||||
force: boolean;
|
||||
/** 未识别的参数。调用方必须报错:`--forced` 这类手滑不能退化成「看起来生效了」。 */
|
||||
unknown: string[];
|
||||
}
|
||||
|
||||
export interface SeedWriteDecision {
|
||||
action: SeedWriteAction;
|
||||
reason: string;
|
||||
}
|
||||
|
||||
export interface SeedWriteCounts {
|
||||
created: number;
|
||||
updated: number;
|
||||
skipped: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析 seed 的 CLI 参数。`npm run db:seed -- --force` 在 npm 层会把 `--` 保留在
|
||||
* `process.argv` 里,故显式忽略它(否则每次带 --force 都变成「未知参数」)。
|
||||
*/
|
||||
export function parseSeedMode(argv: readonly string[]): SeedMode {
|
||||
let force = false;
|
||||
const unknown: string[] = [];
|
||||
|
||||
for (const arg of argv) {
|
||||
if (arg === '--') continue;
|
||||
if (arg === '--force') {
|
||||
force = true;
|
||||
continue;
|
||||
}
|
||||
unknown.push(arg);
|
||||
}
|
||||
|
||||
return { force, unknown };
|
||||
}
|
||||
|
||||
/** 单行该新建、覆盖还是跳过。纯函数:同输入同输出。 */
|
||||
export function decideSeedWrite(input: { exists: boolean; force: boolean }): SeedWriteDecision {
|
||||
if (!input.exists) {
|
||||
return { action: 'create', reason: '库中缺行 ⇒ 新建' };
|
||||
}
|
||||
if (input.force) {
|
||||
return { action: 'update', reason: '--force ⇒ 覆盖已有行' };
|
||||
}
|
||||
return {
|
||||
action: 'skip',
|
||||
reason: '已存在且未带 --force ⇒ 保留库中内容(如需重置请显式 --force)',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否清理「种子集合已为空」的历史遗留行(如当前 `case-study`)。
|
||||
* 删除是破坏性动作,故只在 --force 下成立;非 force 时宁可留旧行也不猜。
|
||||
*/
|
||||
export function shouldPruneSeededRows(input: { force: boolean; desiredCount: number }): boolean {
|
||||
return input.force && input.desiredCount === 0;
|
||||
}
|
||||
|
||||
export function createSeedWriteCounts(): SeedWriteCounts {
|
||||
return { created: 0, updated: 0, skipped: 0 };
|
||||
}
|
||||
|
||||
export function recordSeedWrite(counts: SeedWriteCounts, action: SeedWriteAction): void {
|
||||
if (action === 'create') counts.created += 1;
|
||||
else if (action === 'update') counts.updated += 1;
|
||||
else counts.skipped += 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 收尾汇总。刻意在「一行都没写」时显式说出来 —— 否则读日志的人会把
|
||||
* 「全部跳过」误读成「seed 成功执行」。
|
||||
*/
|
||||
export function formatSeedSummary(counts: SeedWriteCounts): string {
|
||||
const total = counts.created + counts.updated + counts.skipped;
|
||||
const head = `种子写入汇总:新建 ${counts.created} / 覆盖 ${counts.updated} / 跳过 ${counts.skipped}(共 ${total} 行)`;
|
||||
if (counts.created + counts.updated === 0) {
|
||||
return `${head} ⇒ 未写任何一行(库中已有全部种子行;要重置请显式 --force)`;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
/** 启动日志用的一行模式说明。 */
|
||||
export function formatSeedMode(mode: SeedMode): string {
|
||||
return mode.force
|
||||
? '--force 覆盖模式(会改写库中已有同 slug 行)'
|
||||
: '补缺模式(已存在的行不覆盖,保护 CMS 人工编辑)';
|
||||
}
|
||||
Reference in new issue
Block a user