工作流 C-1 / C-3 前半 + 测试代码类型检查。 【故障注入基座】新增 tests/helpers/storage-harness.ts + faulty-backend.ts - TransactionalFileStore:忠实 OPFS 提交语义(close 才可见)+ 字节级故障注入 (failNextWrite/Append/Delete、truncateAppendTo 撕裂写、crashPending 真崩溃) - 删除旧 opfs-mock:读返回内部引用、keepExistingData:false 不截断、close 空实现 导致"提交前可见"等真实缺陷无法被测出(31 个测试文件迁移至新 harness) - 删除 aria-opfs-backend 内的第三份重复 mock(含从未被断言使用的 writeCalls 死代码 与 entry.content.subarray 恒等分支) - FaultyBackend:包装任意 IStorageBackend 注入故障;crash() 明确区别于 close() (后者是优雅停机,会刷完写队列 —— 这正是此前所有"崩溃恢复"测试的真相) - 16 条基座自测证明注入真的生效(含 close 不能当崩溃的对照组) 【覆盖率口径】jest.config.cjs - 移除 '!src/**/index.ts'(该 glob 把 AriaEngine 主实现等 15 个实现文件整体 排除出统计,与 v0.2.6 曾承认过的问题同源),改为只排除纯类型声明文件并附理由 - 新增 coverageThreshold 门禁(此前完全不存在) - 真实基线:语句 90.66% / 分支 82.94% / 函数 94.36% / 行 93.43% - 修正 testMatch 使 tests/helpers 下的测试可被发现 【测试代码类型检查】tsconfig.test.json + npm run typecheck:tests - 修复 103 个测试代码类型错误(此前 babel 剥离类型 + tsconfig 排除 tests,全部隐藏) - 新增 tests/helpers/assertions.ts:nonNull/decode/rows/object/engineMethod/expectCode 以断言收窄替代 as any - 消除 21 个 lint warning(含 v043-hardening 中定义后从未调用的 mockOPFS 死代码) - parser.test.ts 12 处 toBeDefined() 空断言升级为结构断言(并新增 AND/OR 优先级用例, 当前红灯,对应总账第 11 项,将在工作流 A 修复) 【版本契约】新增 tests/version-contract.test.ts - 校验 src VERSION / package.json / dist 三者一致,替代两处硬编码版本字面量 【CI 门禁】.gitea/workflows/ci.yml - lint 去掉 continue-on-error(此前永远不让 CI 变红) - 新增 tests 类型检查、--coverage 覆盖率门禁、dist 与源码同步校验 - 版本 0.7.4 升至 0.8.0
124 lines
4.8 KiB
TypeScript
124 lines
4.8 KiB
TypeScript
/**
|
||
* AriaEngine MVCC 事务隔离 + Savepoint 测试
|
||
* 全部 MemoryBackend,零卡死
|
||
*/
|
||
import { AriaEngine } from '../../src/engine/aria/index';
|
||
import { createSchema } from '../../src/table/schema';
|
||
|
||
import { resetOPFSMock } from '../helpers/storage-harness';
|
||
|
||
beforeEach(() => { resetOPFSMock(); });
|
||
|
||
describe('AriaEngine — MVCC 事务 + Savepoint', () => {
|
||
let engine: AriaEngine;
|
||
|
||
beforeEach(async () => {
|
||
engine = new AriaEngine({ storageBackend: 'memory' });
|
||
await engine.open('mvcc-test', 1);
|
||
await engine.createTable(createSchema('users', {
|
||
id: { type: 'string', primaryKey: true },
|
||
name: { type: 'string', required: true },
|
||
balance: { type: 'number', default: 0 },
|
||
}));
|
||
});
|
||
|
||
afterEach(async () => { await engine.close(); });
|
||
|
||
// ---- MVCC 事务 ----
|
||
it('beginTransaction 分配唯一事务ID', () => {
|
||
const id1 = (engine as any).mvcc.beginTransaction();
|
||
const id2 = (engine as any).mvcc.beginTransaction();
|
||
expect(id1).not.toBe(id2);
|
||
});
|
||
|
||
it('commit 后清理本事务版本(undo 记录不保留,v0.6.3)', () => {
|
||
const txnId = (engine as any).mvcc.beginTransaction();
|
||
(engine as any).mvcc.writeVersion('users', '1', { name: 'Alice' }, txnId);
|
||
(engine as any).mvcc.commitTransaction(txnId);
|
||
const store = (engine as any).mvcc.versionStore;
|
||
// v0.6.3: 已提交版本随 commit 清理(快照读取已移除,版本链仅作事务内 undo)
|
||
expect(store.has('users.1')).toBe(false);
|
||
});
|
||
|
||
it('rollback 移除本事务版本', () => {
|
||
const txnId = (engine as any).mvcc.beginTransaction();
|
||
(engine as any).mvcc.writeVersion('users', '1', { name: 'Alice' }, txnId);
|
||
(engine as any).mvcc.rollbackTransaction(txnId);
|
||
const store = (engine as any).mvcc.versionStore;
|
||
expect(store.has('users.1')).toBe(false);
|
||
});
|
||
|
||
it('引擎层事务 commit 后数据可见', async () => {
|
||
await engine.beginTransaction();
|
||
await engine.insert('users', [{ id: '1', name: 'Alice', balance: 100 }]);
|
||
await engine.commitTransaction();
|
||
expect(await engine.count('users')).toBe(1);
|
||
});
|
||
|
||
it('引擎层事务 rollback 后数据消失', async () => {
|
||
await engine.insert('users', [{ id: '1', name: 'Alice' }]);
|
||
await engine.beginTransaction();
|
||
await engine.insert('users', [{ id: '2', name: 'Bob' }]);
|
||
await engine.rollbackTransaction();
|
||
expect(await engine.count('users')).toBe(1);
|
||
});
|
||
|
||
it('GC 清理过旧版本(未提交版本链保留最新 N 个)', () => {
|
||
for (let i = 0; i < 200; i++) {
|
||
const txnId = (engine as any).mvcc.beginTransaction();
|
||
(engine as any).mvcc.writeVersion('users', '1', { ver: i }, txnId);
|
||
// 不 commit:未提交版本保留在链上(活跃事务 undo)
|
||
}
|
||
(engine as any).mvcc.gc(50);
|
||
expect((engine as any).mvcc.versionStore.get('users.1').length).toBe(50);
|
||
});
|
||
|
||
it('多事务并发未提交 + 部分提交:commit 版本清理、未提交保留', () => {
|
||
const mvcc = (engine as any).mvcc;
|
||
const txnA = mvcc.beginTransaction();
|
||
mvcc.writeVersion('users', '1', { name: 'A1' }, txnA);
|
||
const txnB = mvcc.beginTransaction();
|
||
mvcc.writeVersion('users', '1', { name: 'B1' }, txnB);
|
||
mvcc.commitTransaction(txnA);
|
||
// txnA 版本已清理;txnB 未提交版本保留
|
||
const versions = mvcc.versionStore.get('users.1') as { txnId: number }[];
|
||
expect(versions.map((v) => v.txnId)).toEqual([txnB]);
|
||
mvcc.rollbackTransaction(txnB);
|
||
expect(mvcc.versionStore.has('users.1')).toBe(false);
|
||
});
|
||
|
||
// ---- Savepoint ----
|
||
it('savepoint 创建成功', async () => {
|
||
await engine.beginTransaction();
|
||
await (engine as any).savepoint('sp1');
|
||
expect((engine as any).savepoints.has('sp1')).toBe(true);
|
||
await engine.rollbackTransaction();
|
||
});
|
||
|
||
it('rollbackToSavepoint 恢复快照', async () => {
|
||
await engine.insert('users', [{ id: '1', name: 'Alice' }]);
|
||
await engine.beginTransaction();
|
||
await engine.insert('users', [{ id: '2', name: 'Bob' }]);
|
||
await (engine as any).savepoint('sp1');
|
||
await engine.insert('users', [{ id: '3', name: 'Charlie' }]);
|
||
await (engine as any).rollbackToSavepoint('sp1');
|
||
await engine.commitTransaction();
|
||
expect(await engine.count('users')).toBe(2);
|
||
});
|
||
|
||
it('releaseSavepoint 释放后不可回滚', async () => {
|
||
await engine.beginTransaction();
|
||
await (engine as any).savepoint('sp1');
|
||
await (engine as any).releaseSavepoint('sp1');
|
||
expect((engine as any).savepoints.has('sp1')).toBe(false);
|
||
await engine.rollbackTransaction();
|
||
});
|
||
|
||
it('重复 savepoint 名称抛出错误', async () => {
|
||
await engine.beginTransaction();
|
||
await (engine as any).savepoint('sp1');
|
||
await expect((engine as any).savepoint('sp1')).rejects.toThrow('already exists');
|
||
await engine.rollbackTransaction();
|
||
});
|
||
});
|