Files
MetonaSqlark/tests/aria-cascade.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

203 lines
9.9 KiB
TypeScript
Raw Permalink 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.
/**
* v0.4.1 测试 — AriaEngine 外键级联 + clearAll 重置
*/
import { AriaEngine } from '../src/engine/aria/index';
import { resetOPFSMock } from './helpers/storage-harness';
beforeEach(() => { resetOPFSMock(); });
const mkEngine = async (name: string): Promise<AriaEngine> => {
const e = new AriaEngine({ storageBackend: 'opfs', walSyncMode: 'full' });
await e.open(`cascade-${name}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`, 1);
return e;
};
describe('[v0.4.1] AriaEngine 外键级联', () => {
test('CASCADE:删除主表行时级联删除引用行', async () => {
const e = await mkEngine('cas');
await e.createTable({ name: 'users', columns: { id: { type: 'string', primaryKey: true } } });
await e.createTable({ name: 'orders', columns: { id: { type: 'string', primaryKey: true }, user_id: { type: 'string', references: 'users.id', onDelete: 'CASCADE' } } });
await e.insert('users', [{ id: 'u1' }, { id: 'u2' }]);
await e.insert('orders', [{ id: 'o1', user_id: 'u1' }, { id: 'o2', user_id: 'u1' }, { id: 'o3', user_id: 'u2' }]);
const count = await e.delete('users', { table: 'users', where: { id: 'u1' } });
expect(count).toBe(3); // u1 + o1 + o2
expect(await e.count('users')).toBe(1);
expect(await e.count('orders')).toBe(1);
const rest = await e.find('orders', { table: 'orders' });
expect(rest[0].user_id).toBe('u2');
await e.close();
});
test('CASCADE:多层递归(users → orders → order_items', async () => {
const e = await mkEngine('cas2');
await e.createTable({ name: 'users', columns: { id: { type: 'string', primaryKey: true } } });
await e.createTable({ name: 'orders', columns: { id: { type: 'string', primaryKey: true }, user_id: { type: 'string', references: 'users.id', onDelete: 'CASCADE' } } });
await e.createTable({ name: 'order_items', columns: { id: { type: 'string', primaryKey: true }, order_id: { type: 'string', references: 'orders.id', onDelete: 'CASCADE' } } });
await e.insert('users', [{ id: 'u1' }]);
await e.insert('orders', [{ id: 'o1', user_id: 'u1' }]);
await e.insert('order_items', [{ id: 'i1', order_id: 'o1' }, { id: 'i2', order_id: 'o1' }]);
const count = await e.delete('users', { table: 'users', where: { id: 'u1' } });
expect(count).toBe(4); // u1 + o1 + i1 + i2
expect(await e.count('order_items')).toBe(0);
await e.close();
});
test('SET NULL:删除主表行时引用列置 null', async () => {
const e = await mkEngine('sn');
await e.createTable({ name: 'users', columns: { id: { type: 'string', primaryKey: true } } });
await e.createTable({ name: 'orders', columns: { id: { type: 'string', primaryKey: true }, user_id: { type: 'string', references: 'users.id', onDelete: 'SET NULL' } } });
await e.insert('users', [{ id: 'u1' }]);
await e.insert('orders', [{ id: 'o1', user_id: 'u1' }]);
const count = await e.delete('users', { table: 'users', where: { id: 'u1' } });
expect(count).toBe(1); // 仅 u1,引用行保留
const rows = await e.find('orders', { table: 'orders' });
expect(rows).toHaveLength(1);
expect(rows[0].user_id).toBeNull();
await e.close();
});
test('RESTRICT:存在引用行时禁止删除', async () => {
const e = await mkEngine('res');
await e.createTable({ name: 'users', columns: { id: { type: 'string', primaryKey: true } } });
await e.createTable({ name: 'orders', columns: { id: { type: 'string', primaryKey: true }, user_id: { type: 'string', references: 'users.id', onDelete: 'RESTRICT' } } });
await e.insert('users', [{ id: 'u1' }]);
await e.insert('orders', [{ id: 'o1', user_id: 'u1' }]);
await expect(e.delete('users', { table: 'users', where: { id: 'u1' } })).rejects.toThrow('Cannot delete');
// 数据未被删除
expect(await e.count('users')).toBe(1);
expect(await e.count('orders')).toBe(1);
await e.close();
});
test('CASCADE 同时清理二级索引(删除后按索引查不到)', async () => {
const e = await mkEngine('cidx');
await e.createTable({ name: 'users', columns: { id: { type: 'string', primaryKey: true } } });
await e.createTable({ name: 'orders', columns: { id: { type: 'string', primaryKey: true }, user_id: { type: 'string', index: true, references: 'users.id', onDelete: 'CASCADE' } } });
await e.insert('users', [{ id: 'u1' }]);
await e.insert('orders', [{ id: 'o1', user_id: 'u1' }]);
await e.delete('users', { table: 'users', where: { id: 'u1' } });
const rows = await e.find('orders', { table: 'orders', where: { user_id: 'u1' } });
expect(rows).toHaveLength(0);
await e.close();
});
test('事务内级联删除可提交/回滚', async () => {
const e = await mkEngine('ctx');
await e.createTable({ name: 'users', columns: { id: { type: 'string', primaryKey: true } } });
await e.createTable({ name: 'orders', columns: { id: { type: 'string', primaryKey: true }, user_id: { type: 'string', references: 'users.id', onDelete: 'CASCADE' } } });
await e.insert('users', [{ id: 'u1' }]);
await e.insert('orders', [{ id: 'o1', user_id: 'u1' }]);
// rollback:级联删除被回滚
await e.beginTransaction();
await e.delete('users', { table: 'users', where: { id: 'u1' } });
await e.rollbackTransaction();
expect(await e.count('users')).toBe(1);
expect(await e.count('orders')).toBe(1);
// commit:级联删除生效
await e.beginTransaction();
await e.delete('users', { table: 'users', where: { id: 'u1' } });
await e.commitTransaction();
expect(await e.count('users')).toBe(0);
expect(await e.count('orders')).toBe(0);
await e.close();
});
});
describe('[v0.4.1] AriaEngine clearAll', () => {
test('clearAll 清空全部表与数据,实例可继续使用', async () => {
const e = await mkEngine('clr');
await e.createTable({ name: 'users', columns: { id: { type: 'string', primaryKey: true }, name: { type: 'string', index: true } } });
await e.insert('users', [{ id: '1', name: 'Alice' }]);
await e.clearAll();
expect(await e.getTableNames()).toHaveLength(0);
// 实例可继续建表使用
await e.createTable({ name: 't2', columns: { id: { type: 'string', primaryKey: true } } });
await e.insert('t2', [{ id: 'x' }]);
expect(await e.count('t2')).toBe(1);
await e.close();
});
test('clearAll 后重启(模拟刷新)无残留数据', async () => {
const name = `clr2-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
let e = new AriaEngine({ storageBackend: 'opfs', walSyncMode: 'full' });
await e.open(name, 1);
await e.createTable({ name: 't', columns: { id: { type: 'string', primaryKey: true } } });
await e.insert('t', [{ id: '1' }]);
await e.clearAll();
await e.close();
// 重新打开(模拟页面刷新):不应有残留表
e = new AriaEngine({ storageBackend: 'opfs', walSyncMode: 'full' });
await e.open(name, 1);
expect(await e.getTableNames()).toHaveLength(0);
await e.close();
});
});
describe('[v0.4.1] AriaEngine ALTER TABLE', () => {
test('DROP COLUMN 真正清除存储中的列值(find 副本不再残留)', async () => {
const e = await mkEngine('alt');
await e.createTable({ name: 'users', columns: { id: { type: 'string', primaryKey: true }, name: { type: 'string' }, email: { type: 'string' }, age: { type: 'number', default: 0 } } });
await e.insert('users', [{ id: '1', name: 'Alice', email: 'a@x.com', age: 30 }]);
await e.alterTable('users', 'ADD', { name: 'phone', type: 'string' });
await e.insert('users', [{ id: '2', name: 'Frank', age: 33, phone: '123' }]);
await e.alterTable('users', 'DROP', { name: 'phone', type: 'string' });
const rows = await e.find('users', { table: 'users' });
for (const row of rows) {
expect('phone' in row).toBe(false);
}
await e.close();
});
test('ALTER 持久化:重启后 schema 与行一致', async () => {
const name = `alt2-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
let e = new AriaEngine({ storageBackend: 'opfs', walSyncMode: 'full' });
await e.open(name, 1);
await e.createTable({ name: 'users', columns: { id: { type: 'string', primaryKey: true }, name: { type: 'string' } } });
await e.insert('users', [{ id: '1', name: 'Alice' }]);
await e.alterTable('users', 'ADD', { name: 'phone', type: 'string' });
await e.insert('users', [{ id: '2', name: 'Frank', phone: '123' }]);
await e.alterTable('users', 'DROP', { name: 'phone', type: 'string' });
await e.close();
e = new AriaEngine({ storageBackend: 'opfs', walSyncMode: 'full' });
await e.open(name, 1);
const schema = await e.getTableSchema('users');
expect(Object.keys(schema!.columns)).not.toContain('phone');
const rows = await e.find('users', { table: 'users' });
for (const row of rows) expect('phone' in row).toBe(false);
await e.close();
});
test('ALTER 模拟崩溃(不 close)重启:schema 与行一致', async () => {
const name = `alt3-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
let e = new AriaEngine({ storageBackend: 'opfs', walSyncMode: 'full' });
await e.open(name, 1);
await e.createTable({ name: 'users', columns: { id: { type: 'string', primaryKey: true }, name: { type: 'string' } } });
await e.insert('users', [{ id: '1', name: 'Alice' }]);
await e.alterTable('users', 'ADD', { name: 'phone', type: 'string' });
await e.insert('users', [{ id: '2', name: 'Frank', phone: '123' }]);
await e.alterTable('users', 'DROP', { name: 'phone', type: 'string' });
// 不 close,模拟崩溃
e = new AriaEngine({ storageBackend: 'opfs', walSyncMode: 'full' });
await e.open(name, 1);
const schema = await e.getTableSchema('users');
expect(Object.keys(schema!.columns)).not.toContain('phone');
const rows = await e.find('users', { table: 'users' });
for (const row of rows) expect('phone' in row).toBe(false);
await e.close();
});
});