Files
novalon-website/src/lib/db.test.ts
T
zhangxiang a366bd1400 fix(qa): 第五轮系统性质量保障缺陷修复 + 单元/集成测试
安全/鉴权:登出清服务端 httpOnly 令牌、登录 IP 限流前置 bcrypt、停用账号
令牌在 6 个内联 role handler 失效、admin/items 存在性 oracle 认证前置、
同意偏好 sanitize fail-closed、api-crypto 按 HTTP 方法判体。

CMS:workflow 状态更新+审计收进交互式 $transaction(失败即回滚)、编辑器
richtext 走 textarea 防换行损毁、array/object 子字段按作用域读写防污染、
about/contact/erp-upgrade 补 ISR revalidate 与路由映射。

UI/a11y:AnimatedCounter 入视口前显起点、CLS 累计上报、MetricCard 动画入
effect 并卸载取消 rAF、后台状态四态映射/搜索防抖+序号守卫/媒体单 input/
吞错改横幅/表单 label-aria 关联。

新增对应判别性单测与 pagination/rate-limit/client-ip/sanitize/validate-content-data
等模块及 tests-integration 真库集成层。
2026-09-28 10:48:07 +08:00

71 lines
2.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, it, expect, jest, beforeEach, afterEach } from '@jest/globals';
// PrismaClient 由本文件的 mock 提供:只需暴露 $queryRaw,即可断言真实下发的 PRAGMA。
const mockQueryRaw = jest.fn<(parts: readonly string[]) => Promise<unknown>>();
jest.mock('@/generated/prisma/client', () => ({
PrismaClient: jest.fn().mockImplementation(() => ({
$queryRaw: mockQueryRaw,
})),
}));
type DbModule = typeof import('./db');
/** 重新求值模块(导入即触发一次 PRAGMA 下发),返回全新的 db 实例 */
async function loadFreshDb(): Promise<DbModule> {
delete (globalThis as unknown as { prisma?: unknown }).prisma;
jest.resetModules();
return (await import('./db')) as DbModule;
}
let consoleErrorSpy: jest.Spied<typeof console.error>;
beforeEach(() => {
jest.clearAllMocks();
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
mockQueryRaw.mockResolvedValue([]);
});
afterEach(() => {
consoleErrorSpy.mockRestore();
});
describe('SQLite 并发默认值(验收 B-5)', () => {
it('导入 src/lib/db 即把 journal_mode 切到 WAL 并设置 busy_timeout', async () => {
await loadFreshDb();
expect(mockQueryRaw).toHaveBeenCalledTimes(2);
expect(mockQueryRaw.mock.calls[0]![0][0]).toBe('PRAGMA journal_mode=WAL');
expect(mockQueryRaw.mock.calls[1]![0][0]).toBe('PRAGMA busy_timeout=5000');
});
it('applySqliteConcurrencyDefaults 幂等:重复调用不重复下发 PRAGMA', async () => {
const db = await loadFreshDb();
await db.applySqliteConcurrencyDefaults();
await db.applySqliteConcurrencyDefaults();
expect(mockQueryRaw).toHaveBeenCalledTimes(2);
});
it('PRAGMA 失败只记录日志,既不抛出也不掩盖', async () => {
mockQueryRaw.mockRejectedValueOnce(new Error('SQLITE_CANTOPEN: unable to open database file'));
const db = await loadFreshDb();
await expect(db.applySqliteConcurrencyDefaults()).resolves.toBeUndefined();
expect(consoleErrorSpy).toHaveBeenCalledTimes(1);
expect(consoleErrorSpy.mock.calls[0]![0]).toContain('WAL');
expect((consoleErrorSpy.mock.calls[0]![1] as Error).message).toContain('SQLITE_CANTOPEN');
// 第一条 PRAGMA 失败后不再尝试第二条(连接本身已不可用)
expect(mockQueryRaw).toHaveBeenCalledTimes(1);
});
it('导出的 prisma 仍是同一个客户端实例,且带 $queryRaw 能力', async () => {
const db = await loadFreshDb();
expect(db.prisma).toBeDefined();
expect(typeof db.prisma.$queryRaw).toBe('function');
});
});