CI / Static checks (push) Has been cancelled
CI / Production build (push) Has been cancelled
CI / API integration (push) Has been cancelled
CI / Taro builds (acheng-campus-food) (push) Has been cancelled
CI / Taro builds (atelier-solopreneur) (push) Has been cancelled
CI / Taro builds (linjing-editorial) (push) Has been cancelled
CI / Browser regression (push) Has been cancelled
138 lines
5.3 KiB
JavaScript
138 lines
5.3 KiB
JavaScript
const assert = require("node:assert/strict");
|
|
const { randomBytes } = require("node:crypto");
|
|
const { existsSync } = require("node:fs");
|
|
const path = require("node:path");
|
|
const { spawnSync } = require("node:child_process");
|
|
const { PrismaClient } = require("@prisma/client");
|
|
|
|
const databaseUrl = process.env.DATABASE_URL;
|
|
if (!databaseUrl) throw new Error("DATABASE_URL is required for integration tests");
|
|
|
|
const schema = `pb_test_${randomBytes(8).toString("hex")}`;
|
|
const testUrl = new URL(databaseUrl);
|
|
testUrl.searchParams.set("schema", schema);
|
|
const testEnvironment = { ...process.env, DATABASE_URL: testUrl.toString(), NODE_ENV: "test" };
|
|
const administrator = new PrismaClient();
|
|
const fixtureDatabase = new PrismaClient({ datasourceUrl: testUrl.toString() });
|
|
|
|
function run(command, args) {
|
|
const result = spawnSync(command, args, { stdio: "inherit", env: testEnvironment });
|
|
if (result.error) throw result.error;
|
|
return result.status ?? 1;
|
|
}
|
|
|
|
async function main() {
|
|
let created = false;
|
|
let status = 1;
|
|
try {
|
|
await administrator.$executeRawUnsafe(`CREATE SCHEMA "${schema}"`);
|
|
created = true;
|
|
if (run("pnpm", ["exec", "prisma", "migrate", "deploy"]) !== 0) return status;
|
|
if (run(process.execPath, ["test/prepare-integration-template-packages.mjs"]) !== 0)
|
|
return status;
|
|
if (run("pnpm", ["db:seed"]) !== 0) return status;
|
|
const initialTrustCount = await fixtureDatabase.trustContent.count();
|
|
const initialFormCount = await fixtureDatabase.inquiryForm.count();
|
|
const initialReleases = await fixtureDatabase.brandRelease.findMany({ orderBy: { id: "asc" } });
|
|
// 模拟旧模板遗留数据:数组 config、失效封面和媒体引用。重复 seed 必须修复结构,
|
|
// 同时保留记录本身和停用状态。
|
|
const doorsBrand = await fixtureDatabase.brandProfile.findUniqueOrThrow({
|
|
where: { slug: "linjing-doors" },
|
|
});
|
|
const legacyOfferingName = "legacy-offering-repair-fixture";
|
|
await fixtureDatabase.offering.create({
|
|
data: {
|
|
brandId: doorsBrand.id,
|
|
kind: "service",
|
|
name: legacyOfferingName,
|
|
coverUrl: "/media/seed/seed-02.jpg",
|
|
isEnabled: false,
|
|
config: [{ url: "/media/seed/seed-02.jpg" }],
|
|
},
|
|
});
|
|
if (run("pnpm", ["db:seed"]) !== 0) return status;
|
|
const repairedLegacyOffering = await fixtureDatabase.offering.findUniqueOrThrow({
|
|
where: { brandId_name: { brandId: doorsBrand.id, name: legacyOfferingName } },
|
|
});
|
|
assert.equal(
|
|
Array.isArray(repairedLegacyOffering.config),
|
|
false,
|
|
"Repeated seed must repair legacy array config",
|
|
);
|
|
assert.ok(
|
|
repairedLegacyOffering.config &&
|
|
typeof repairedLegacyOffering.config === "object" &&
|
|
Array.isArray(repairedLegacyOffering.config.media),
|
|
"Repeated seed must rebuild legacy media config",
|
|
);
|
|
assert.notEqual(
|
|
repairedLegacyOffering.coverUrl,
|
|
"/media/seed/seed-02.jpg",
|
|
"Repeated seed must replace a missing local cover",
|
|
);
|
|
assert.equal(
|
|
repairedLegacyOffering.isEnabled,
|
|
false,
|
|
"Repeated seed must preserve disabled offering state",
|
|
);
|
|
assert.equal(
|
|
await fixtureDatabase.trustContent.count(),
|
|
initialTrustCount,
|
|
"Repeated seed must not duplicate trust content",
|
|
);
|
|
assert.equal(
|
|
await fixtureDatabase.inquiryForm.count(),
|
|
initialFormCount,
|
|
"Repeated seed must not duplicate forms",
|
|
);
|
|
assert.deepEqual(
|
|
await fixtureDatabase.brandRelease.findMany({ orderBy: { id: "asc" } }),
|
|
initialReleases,
|
|
"Repeated seed must preserve release history and snapshots",
|
|
);
|
|
const seededOfferings = await fixtureDatabase.offering.findMany({
|
|
select: { name: true, coverUrl: true, config: true },
|
|
});
|
|
assert.ok(seededOfferings.length > 0, "Seed must create offering fixtures");
|
|
for (const offering of seededOfferings) {
|
|
assert.ok(
|
|
offering.config && typeof offering.config === "object" && !Array.isArray(offering.config),
|
|
`Seed offering config must be an object: ${offering.name}`,
|
|
);
|
|
const config = offering.config;
|
|
const mediaUrls = Array.isArray(config.media)
|
|
? config.media.flatMap((item) =>
|
|
item && typeof item === "object" && typeof item.url === "string" ? [item.url] : [],
|
|
)
|
|
: [];
|
|
const localMedia = [offering.coverUrl, ...mediaUrls].filter(
|
|
(url) => typeof url === "string" && url.startsWith("/media/seed/"),
|
|
);
|
|
for (const mediaPath of new Set(localMedia)) {
|
|
assert.equal(
|
|
existsSync(path.join(__dirname, "prisma/assets", path.basename(mediaPath))),
|
|
true,
|
|
`Seed offering references missing media: ${offering.name} -> ${mediaPath}`,
|
|
);
|
|
}
|
|
}
|
|
await fixtureDatabase.adminUser.updateMany({
|
|
where: { username: "admin" },
|
|
data: { mustChangePassword: false },
|
|
});
|
|
console.log("Seed idempotency verified: content, forms and release history preserved");
|
|
status = run(process.execPath, ["--test", "test/http.integration.test.js"]);
|
|
return status;
|
|
} finally {
|
|
await fixtureDatabase.$disconnect();
|
|
if (created) await administrator.$executeRawUnsafe(`DROP SCHEMA "${schema}" CASCADE`);
|
|
await administrator.$disconnect();
|
|
process.exitCode = status;
|
|
}
|
|
}
|
|
|
|
main().catch((error) => {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
});
|