- 数据库表规范化与原子事务版本迁移机制 (_schema_migrations) - 增加 SQLite 复合主键、PRAGMA busy_timeout 与索引覆盖 - 构建统一两级协同缓存 (L1 Memory LRU + L2 SQLite 持久化) - withCache 增加 In-flight Promise 去重并发防击穿 (单飞机制) - 收敛 dimension、role、bill 与 token-store 缓存逻辑 - 100% 保持现有 API 契约与零功能删除
73 lines
2.8 KiB
TypeScript
73 lines
2.8 KiB
TypeScript
import { Database } from "bun:sqlite";
|
|
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
import { closeDatabase, getDatabase } from "./connection.js";
|
|
import { runMigrations } from "./migrations.js";
|
|
|
|
describe("Core DB - 数据库连接与结构化迁移机制 (加固测试)", () => {
|
|
let db: Database;
|
|
|
|
beforeEach(() => {
|
|
db = getDatabase(":memory:");
|
|
});
|
|
|
|
afterEach(() => {
|
|
closeDatabase();
|
|
});
|
|
|
|
test("数据库初始化时自动应用所有版本迁移并记录到 _schema_migrations", () => {
|
|
const rows = db.query("SELECT version, name FROM _schema_migrations ORDER BY version ASC").all() as Array<{
|
|
version: number;
|
|
name: string;
|
|
}>;
|
|
expect(rows.length).toBeGreaterThanOrEqual(1);
|
|
expect(rows[0].version).toBe(1);
|
|
expect(rows[0].name).toBe("001_core_schema_and_approval_tables");
|
|
});
|
|
|
|
test("核心数据表与新增的多级缓存表结构完备且索引有效", () => {
|
|
// 检查表是否存在
|
|
const tables = db
|
|
.query("SELECT name FROM sqlite_master WHERE type = 'table'")
|
|
.all() as Array<{ name: string }>;
|
|
const tableNames = new Set(tables.map((t) => t.name));
|
|
|
|
expect(tableNames.has("rules")).toBe(true);
|
|
expect(tableNames.has("preferences")).toBe(true);
|
|
expect(tableNames.has("approval_runs")).toBe(true);
|
|
expect(tableNames.has("approval_findings")).toBe(true);
|
|
expect(tableNames.has("aipro_logs")).toBe(true);
|
|
expect(tableNames.has("cache_entries")).toBe(true);
|
|
|
|
// 检查加固索引:UPPER(short_id)、findings(rule_id)
|
|
const indices = db
|
|
.query("SELECT name FROM sqlite_master WHERE type = 'index'")
|
|
.all() as Array<{ name: string }>;
|
|
const indexNames = new Set(indices.map((i) => i.name));
|
|
|
|
expect(indexNames.has("idx_runs_corp_id")).toBe(true);
|
|
expect(indexNames.has("idx_runs_expires_at")).toBe(true);
|
|
expect(indexNames.has("idx_runs_flow_id")).toBe(true);
|
|
expect(indexNames.has("idx_runs_short_id_upper")).toBe(true);
|
|
expect(indexNames.has("idx_findings_rule_id")).toBe(true);
|
|
expect(indexNames.has("idx_cache_namespace")).toBe(true);
|
|
expect(indexNames.has("idx_cache_expires_at")).toBe(true);
|
|
});
|
|
|
|
test("cache_entries 具备复合主键 (namespace, key) 约束", () => {
|
|
db.run(
|
|
"INSERT INTO cache_entries (namespace, key, value, expires_at, updated_at) VALUES ('n1', 'k1', 'v1', 9999, 1000)"
|
|
);
|
|
// 相同 key 但不同 namespace 可以成功插入
|
|
db.run(
|
|
"INSERT INTO cache_entries (namespace, key, value, expires_at, updated_at) VALUES ('n2', 'k1', 'v2', 9999, 1000)"
|
|
);
|
|
|
|
const countRow = db.query("SELECT COUNT(*) AS c FROM cache_entries WHERE key = 'k1'").get() as { c: number };
|
|
expect(countRow.c).toBe(2);
|
|
});
|
|
|
|
test("再次执行 runMigrations 保证幂等且不抛错", () => {
|
|
expect(() => runMigrations(db)).not.toThrow();
|
|
});
|
|
});
|