Files
MetonaSqlark/tests/engine/aria-advanced.test.ts
thzxx 0dba1abf2a test(P0): v0.8.0 验证基座与工程门禁根治
工作流 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
2026-09-14 21:03:06 +08:00

163 lines
6.2 KiB
TypeScript

/**
* AriaEngine BloomFilter + WAL恢复 + 压缩 + BufferPool 完整测试
* 全部 MemoryBackend,零卡死
*/
import { AriaEngine } from '../../src/engine/aria/index';
import { BloomFilter } from '../../src/engine/aria/index/bloom';
import { WAL, type WALStore } from '../../src/engine/aria/wal/log';
import { WALRecordType } from '../../src/engine/aria/types';
import { BufferPool } from '../../src/engine/aria/buffer/pool';
import { MemoryBackend } from '../../src/engine/aria/store/backend';
import { resetOPFSMock } from '../helpers/storage-harness';
beforeEach(() => { resetOPFSMock(); });
describe('AriaEngine — Bloom + WAL + BufferPool', () => {
// ---- BloomFilter 完整测试 ----
describe('BloomFilter', () => {
it('1000 条插入后 false positive 率 < 5%', () => {
const bf = new BloomFilter(1000, 10);
for (let i = 0; i < 1000; i++) bf.insert(`key-${i}`);
let fp = 0;
for (let i = 0; i < 1000; i++) {
if (bf.mayContain(`absent-${i}`)) fp++;
}
expect(fp).toBeLessThan(50);
});
it('大量数据 serialize/fromData 往返', () => {
const bf1 = new BloomFilter(500, 8);
for (let i = 0; i < 500; i++) bf1.insert(`item-${i}`);
const data = bf1.serialize();
const bf2 = BloomFilter.fromData(data, bf1.getHashCount());
for (let i = 0; i < 500; i++) {
expect(bf2.mayContain(`item-${i}`)).toBe(true);
}
expect(bf2.mayContain('never-added')).toBe(false);
});
it('不同哈希函数数量影响误判率', () => {
const bf1 = new BloomFilter(200, 2);
const bf2 = new BloomFilter(200, 8);
for (let i = 0; i < 200; i++) {
bf1.insert(`k-${i}`);
bf2.insert(`k-${i}`);
}
let fp1 = 0, fp2 = 0;
for (let i = 0; i < 200; i++) {
if (bf1.mayContain(`x-${i}`)) fp1++;
if (bf2.mayContain(`x-${i}`)) fp2++;
}
expect(fp2).toBeLessThanOrEqual(fp1 + 20);
});
});
// ---- WAL 恢复完整测试 ----
describe('WAL Recovery', () => {
class MemStore implements WALStore {
chunks: Uint8Array[] = [];
async append(d: Uint8Array) { this.chunks.push(d); }
async readAll() {
const t = this.chunks.reduce((s, c) => s + c.byteLength, 0);
const c = new Uint8Array(t); let o = 0;
for (const ch of this.chunks) { c.set(ch, o); o += ch.byteLength; }
return c;
}
async truncate() { this.chunks = []; }
async exists() { return this.chunks.length > 0; }
}
it('多事务 WAL 恢复仅回放已提交', async () => {
const store = new MemStore();
const wal = new WAL(store, true, 'full');
// 事务1: commit
wal.append({ type: WALRecordType.BEGIN, txnId: 1, tableName: '', key: '' });
wal.append({ type: WALRecordType.INSERT, txnId: 1, tableName: 't', key: 'a', data: { v: 1 } });
wal.append({ type: WALRecordType.COMMIT, txnId: 1, tableName: '', key: '' });
// 事务2: rollback (不回放)
wal.append({ type: WALRecordType.BEGIN, txnId: 2, tableName: '', key: '' });
wal.append({ type: WALRecordType.INSERT, txnId: 2, tableName: 't', key: 'b', data: { v: 2 } });
wal.append({ type: WALRecordType.ROLLBACK, txnId: 2, tableName: '', key: '' });
// 事务3: 未完成(无 COMMIT/ROLLBACK)— 不回放
wal.append({ type: WALRecordType.BEGIN, txnId: 3, tableName: '', key: '' });
wal.append({ type: WALRecordType.INSERT, txnId: 3, tableName: 't', key: 'c', data: { v: 3 } });
const records: any[] = [];
await wal.recover((r) => records.push(r));
const committed = records.filter((r: any) => r.type === WALRecordType.INSERT && r.data);
expect(committed).toHaveLength(3); // recover keeps all, filtering is done in engine
});
it('CRC 损坏记录被跳过', async () => {
const store = new MemStore();
const wal = new WAL(store, true, 'full');
wal.append({ type: WALRecordType.INSERT, txnId: 1, tableName: 't', key: 'ok', data: { v: 1 } });
// 手动损坏 WAL 数据
store.chunks[0][0] = 0xFF; // 破坏第一个字节
const records: any[] = [];
await wal.recover((r) => records.push(r));
expect(records.length).toBeLessThanOrEqual(1);
});
});
// ---- BufferPool 集成 ----
describe('BufferPool', () => {
it('BufferPool 能分配和释放页面', async () => {
const backend = new MemoryBackend();
await backend.open('bp-test');
const pageIO = {
readPage: async (id: number) => backend.read(`pg_${id}`),
writePage: async (id: number, d: ArrayBuffer) => backend.write(`pg_${id}`, d),
allocatePageId: async () => Date.now(),
freePageId: async (_id: number) => {},
};
const pool = new BufferPool(pageIO, 4);
const pages = await pool.newPages(1);
expect(pages[0].pageId).toBeGreaterThan(0);
expect(pages[0].pins).toBe(1);
pool.unpin(pages[0]);
await backend.close();
});
it('BufferPool flushAll 刷新脏页', async () => {
const backend = new MemoryBackend();
await backend.open('bp-test2');
const pageIO = {
readPage: async (id: number) => backend.read(`pg_${id}`),
writePage: async (id: number, d: ArrayBuffer) => backend.write(`pg_${id}`, d),
allocatePageId: async () => 1,
freePageId: async (_id: number) => {},
};
const pool = new BufferPool(pageIO, 4);
const pages = await pool.newPages(1);
pool.markDirty(pages[0]);
pool.unpin(pages[0]);
await pool.flushAll();
const buf = await backend.read('pg_1');
expect(buf).not.toBeNull();
await backend.close();
});
});
// ---- 引擎级压缩/加密开关测试 ----
describe('Compression+Crypto toggle', () => {
it('compression=false 引擎正常启动', async () => {
const e = new AriaEngine({ storageBackend: 'memory', compression: false });
await e.open('comp-test', 1);
expect(e.isOpen()).toBe(true);
await e.close();
});
it('compression=true 引擎正常启动', async () => {
const e = new AriaEngine({ storageBackend: 'memory', compression: true });
await e.open('comp-test2', 1);
expect(e.isOpen()).toBe(true);
await e.close();
});
});
});