Files
trade-message-center/apps/server/test/onetalk-product-content-migration.test.ts
T

39 lines
1.5 KiB
TypeScript

// 验证 OneTalk 商品内容只扩展 JSONB kind 约束,不重写既有事实。
import assert from "node:assert/strict";
import { existsSync } from "node:fs";
import { readFile } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import test from "node:test";
import { migrationDirectory } from "../src/database/migration-config.ts";
const migrationPathUrl = new URL(
`../${migrationDirectory}/0013_nifty_thunderbird.sql`,
import.meta.url,
);
const migrationPath = fileURLToPath(
existsSync(migrationPathUrl)
? migrationPathUrl
: new URL(`../../${migrationDirectory}/0013_nifty_thunderbird.sql`, import.meta.url),
);
test("extends only the content-kind check with product while preserving existing constraints", async () => {
const sql = await readFile(migrationPath, "utf8");
assert.match(sql, /DROP CONSTRAINT\s+"onetalk_message_content_v1_chk"/i);
assert.match(sql, /ADD CONSTRAINT\s+"onetalk_message_content_v1_chk"/i);
assert.match(
sql,
/\('text', 'image', 'file', 'business_card', 'inquiry', 'order', 'product'\)/,
);
assert.match(
sql,
/<> 'business_card' or "onetalk_message"\."content" = '\{"version":1,"kind":"business_card"\}'::jsonb/,
);
assert.equal((sql.match(/\bALTER TABLE\b/gi) ?? []).length, 2);
assert.equal((sql.match(/\bUPDATE\b/gi) ?? []).length, 0);
assert.equal((sql.match(/\bDELETE\b/gi) ?? []).length, 0);
assert.equal(sql.includes("history_generation"), false);
});