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
+104
-59
@@ -17,9 +17,25 @@ import { checkSeedTarget } from '../src/lib/seed-target';
|
||||
import { METHODOLOGY } from '../src/lib/constants/methodology';
|
||||
import { NAVIGATION_V2, MEGA_DROPDOWN_DATA } from '../src/lib/constants/navigation';
|
||||
import { INQUIRY_RESPONSE_COMMITMENT, FREE_FIRST_CONSULT } from '../src/lib/constants/commitments';
|
||||
import {
|
||||
parseSeedMode,
|
||||
decideSeedWrite,
|
||||
createSeedWriteCounts,
|
||||
recordSeedWrite,
|
||||
formatSeedSummary,
|
||||
formatSeedMode,
|
||||
shouldPruneSeededRows,
|
||||
} from '../src/lib/seed-write-policy';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
// 写入模式与计数(2026-11-25,qa-tracker §17.2 的根治)。
|
||||
// 默认**补缺**:库里已有同 slug 的行一律不碰,保护 CMS 人工编辑;
|
||||
// 只有显式 `--force` 才覆盖/删除。判据只认「行是否存在 + 是否 --force」,
|
||||
// 详见 src/lib/seed-write-policy.ts 头注释(为什么不做内容指纹比对)。
|
||||
const SEED_MODE = parseSeedMode(process.argv.slice(2));
|
||||
const SEED_COUNTS = createSeedWriteCounts();
|
||||
|
||||
async function getModelId(code: string): Promise<string> {
|
||||
const model = await prisma.contentModel.findUnique({ where: { code } });
|
||||
if (!model) throw new Error(`模型 ${code} 未找到`);
|
||||
@@ -41,28 +57,34 @@ async function seedItems(
|
||||
const title = String(item[titleField] || `未命名 ${i + 1}`);
|
||||
const data = item as Record<string, unknown>;
|
||||
|
||||
await prisma.contentItem.upsert({
|
||||
where: { modelCode_slug_locale: { modelCode, slug, locale: 'zh-CN' } },
|
||||
update: {
|
||||
title,
|
||||
data: JSON.stringify(data),
|
||||
sortOrder: i,
|
||||
status: 'published',
|
||||
publishedAt: new Date(),
|
||||
},
|
||||
create: {
|
||||
modelId,
|
||||
modelCode,
|
||||
title,
|
||||
slug,
|
||||
data: JSON.stringify(data),
|
||||
sortOrder: i,
|
||||
status: 'published',
|
||||
publishedAt: new Date(),
|
||||
createdBy: 'system',
|
||||
updatedBy: 'system',
|
||||
},
|
||||
});
|
||||
const where = { modelCode_slug_locale: { modelCode, slug, locale: 'zh-CN' } } as const;
|
||||
const existing = await prisma.contentItem.findUnique({ where, select: { id: true } });
|
||||
const decision = decideSeedWrite({ exists: Boolean(existing), force: SEED_MODE.force });
|
||||
recordSeedWrite(SEED_COUNTS, decision.action);
|
||||
if (decision.action === 'skip') continue;
|
||||
|
||||
const payload = {
|
||||
title,
|
||||
data: JSON.stringify(data),
|
||||
sortOrder: i,
|
||||
status: 'published',
|
||||
publishedAt: new Date(),
|
||||
};
|
||||
|
||||
if (decision.action === 'update') {
|
||||
await prisma.contentItem.update({ where, data: payload });
|
||||
} else {
|
||||
await prisma.contentItem.create({
|
||||
data: {
|
||||
modelId,
|
||||
modelCode,
|
||||
slug,
|
||||
...payload,
|
||||
createdBy: 'system',
|
||||
updatedBy: 'system',
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,29 +185,41 @@ async function seedHomeZones() {
|
||||
];
|
||||
|
||||
for (const zone of zones) {
|
||||
await prisma.contentZone.upsert({
|
||||
// 区域同属「后台可改」的内容:ADR-0005 起布局由 CMS 管理,seed 不再是唯一真相源。
|
||||
const existingZone = await prisma.contentZone.findUnique({
|
||||
where: { code: zone.code },
|
||||
update: {
|
||||
name: zone.name,
|
||||
pageCode: zone.pageCode,
|
||||
zoneKey: zone.zoneKey,
|
||||
allowedModels: JSON.stringify(zone.allowedModels),
|
||||
items: JSON.stringify(zone.items),
|
||||
},
|
||||
create: {
|
||||
code: zone.code,
|
||||
name: zone.name,
|
||||
pageCode: zone.pageCode,
|
||||
zoneKey: zone.zoneKey,
|
||||
allowedModels: JSON.stringify(zone.allowedModels),
|
||||
items: JSON.stringify(zone.items),
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
console.log(` ✅ 区域: ${zone.name}`);
|
||||
const decision = decideSeedWrite({ exists: Boolean(existingZone), force: SEED_MODE.force });
|
||||
recordSeedWrite(SEED_COUNTS, decision.action);
|
||||
|
||||
const payload = {
|
||||
name: zone.name,
|
||||
pageCode: zone.pageCode,
|
||||
zoneKey: zone.zoneKey,
|
||||
allowedModels: JSON.stringify(zone.allowedModels),
|
||||
items: JSON.stringify(zone.items),
|
||||
};
|
||||
|
||||
if (decision.action === 'update') {
|
||||
await prisma.contentZone.update({ where: { code: zone.code }, data: payload });
|
||||
} else if (decision.action === 'create') {
|
||||
await prisma.contentZone.create({ data: { code: zone.code, ...payload } });
|
||||
}
|
||||
|
||||
console.log(` ✅ 区域: ${zone.name}${decision.action === 'skip' ? '(已存在,跳过)' : ''}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
// 未知参数必须先报错再写任何一行:`--forced` 这类手滑若被静默忽略,
|
||||
// 结果就是「以为重置了、其实一条没写」,比直接失败更难发现。
|
||||
if (SEED_MODE.unknown.length > 0) {
|
||||
throw new Error(
|
||||
`seed 中止(未写任何一行):未知参数 ${SEED_MODE.unknown.join(' ')};当前只支持 --force`,
|
||||
);
|
||||
}
|
||||
|
||||
// 整库写入前的沙盒检查:`DATABASE_URL` 常是**绝对路径**,而它不随 git worktree 走 ——
|
||||
// 把 `.env` 拷进另一棵树跑 seed,写的就是原来那棵树的库(2026-10-17 真实自伤,
|
||||
// 事故后所有门禁都跑在被旧代码覆盖的数据上出数)。详见 src/lib/seed-target.ts。
|
||||
@@ -198,6 +232,7 @@ async function main() {
|
||||
throw new Error(`seed 中止(未写任何一行):${seedTarget.reason}`);
|
||||
}
|
||||
console.log(` 🗂️ seed 目标库:${seedTarget.resolved ?? process.env.DATABASE_URL}(${seedTarget.reason})`);
|
||||
console.log(` ⚙️ 写入模式:${formatSeedMode(SEED_MODE)}`);
|
||||
console.log('🌱 开始数据库初始化...');
|
||||
|
||||
// ============ 1. 创建管理员用户 ============
|
||||
@@ -656,9 +691,13 @@ async function main() {
|
||||
console.log(' ✅ 首页 Hero Banner');
|
||||
|
||||
console.log('📝 导入案例研究...');
|
||||
// 当前为首批客户共创阶段,案例种子为空;清理历史种子遗留的虚构案例
|
||||
if (CASE_STUDIES.length === 0) {
|
||||
await prisma.contentItem.deleteMany({ where: { modelCode: 'case-study' } });
|
||||
// 当前为首批客户共创阶段,案例种子为空;清理历史种子遗留的虚构案例。
|
||||
// 删除是破坏性动作 ⇒ 只在 --force 下成立(非 force 时宁可留旧行,也不代你决定「哪些该没」)。
|
||||
if (shouldPruneSeededRows({ force: SEED_MODE.force, desiredCount: CASE_STUDIES.length })) {
|
||||
const pruned = await prisma.contentItem.deleteMany({ where: { modelCode: 'case-study' } });
|
||||
console.log(` 🧹 清理历史案例种子 ${pruned.count} 条(--force)`);
|
||||
} else if (CASE_STUDIES.length === 0) {
|
||||
console.log(' ℹ️ 案例种子为空:保留库中已有案例(未带 --force,不做删除)');
|
||||
}
|
||||
await seedItems('case-study', CASE_STUDIES as unknown as Record<string, unknown>[]);
|
||||
console.log(` ✅ ${CASE_STUDIES.length} 个案例`);
|
||||
@@ -758,28 +797,33 @@ async function main() {
|
||||
const slug = `stat-${i}`;
|
||||
const title = String(s.label || `指标 ${i + 1}`);
|
||||
const { id: _id, ...data } = s;
|
||||
await prisma.contentItem.upsert({
|
||||
where: { modelCode_slug_locale: { modelCode: 'stat-item', slug, locale: 'zh-CN' } },
|
||||
update: {
|
||||
const where = { modelCode_slug_locale: { modelCode: 'stat-item', slug, locale: 'zh-CN' } } as const;
|
||||
const existing = await prisma.contentItem.findUnique({ where, select: { id: true } });
|
||||
const decision = decideSeedWrite({ exists: Boolean(existing), force: SEED_MODE.force });
|
||||
recordSeedWrite(SEED_COUNTS, decision.action);
|
||||
if (decision.action !== 'skip') {
|
||||
const payload = {
|
||||
title,
|
||||
data: JSON.stringify({ ...data, accentColor: s.color }),
|
||||
sortOrder: i,
|
||||
status: 'published',
|
||||
publishedAt: new Date(),
|
||||
},
|
||||
create: {
|
||||
modelId: statModelId,
|
||||
modelCode: 'stat-item',
|
||||
title,
|
||||
slug,
|
||||
data: JSON.stringify({ ...data, accentColor: s.color }),
|
||||
sortOrder: i,
|
||||
status: 'published',
|
||||
publishedAt: new Date(),
|
||||
createdBy: 'system',
|
||||
updatedBy: 'system',
|
||||
},
|
||||
});
|
||||
};
|
||||
if (decision.action === 'update') {
|
||||
await prisma.contentItem.update({ where, data: payload });
|
||||
} else {
|
||||
await prisma.contentItem.create({
|
||||
data: {
|
||||
modelId: statModelId,
|
||||
modelCode: 'stat-item',
|
||||
slug,
|
||||
...payload,
|
||||
createdBy: 'system',
|
||||
updatedBy: 'system',
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log(` ✅ ${statList.length} 个数据指标`);
|
||||
|
||||
@@ -1089,6 +1133,7 @@ async function main() {
|
||||
const modelCount = await prisma.contentModel.count();
|
||||
const zoneCount = await prisma.contentZone.count();
|
||||
|
||||
console.log(`\n${formatSeedSummary(SEED_COUNTS)}`);
|
||||
console.log('\n🎉 数据库初始化完成!');
|
||||
console.log(` 📊 内容模型: ${modelCount}`);
|
||||
console.log(` 📄 内容条目: ${itemCount}`);
|
||||
|
||||
Reference in new issue
Block a user