UNPKG

delta-sync

Version:

A lightweight framework for bi-directional database synchronization with automatic version tracking and conflict resolution.

111 lines (110 loc) 3.85 kB
// tester/AdapterTester.ts export class AdapterTester { constructor(adapter, testStoreName = 'adapter_test') { this.testResults = {}; this.adapter = adapter; this.testStoreName = testStoreName; } async runTest(testName, testFn) { try { await testFn(); this.testResults[testName] = { success: true, message: 'Test passed successfully' }; } catch (error) { this.testResults[testName] = { success: false, message: error instanceof Error ? error.message : 'Unknown error' }; } } async runAllTests() { // 按顺序执行所有测试 await this.runTest('readStore', () => this.testReadStore()); await this.runTest('readBulk', () => this.testReadBulk()); await this.runTest('putBulk', () => this.testPutBulk()); await this.runTest('deleteBulk', () => this.testDeleteBulk()); await this.runTest('clearStore', () => this.testClearStore()); await this.runTest('getStores', () => this.testGetStores()); // 计算整体测试结果 const success = Object.values(this.testResults) .every(result => result.success); return { success, results: this.testResults }; } async testReadStore() { const result = await this.adapter.readStore(this.testStoreName); if (!result || typeof result.hasMore !== 'boolean') { throw new Error('readStore should return { items: any[], hasMore: boolean }'); } } async testReadBulk() { const testIds = ['test1', 'test2']; const items = await this.adapter.readBulk(this.testStoreName, testIds); if (!Array.isArray(items)) { throw new Error('readBulk should return an array'); } } async testPutBulk() { const testItems = [ { id: 'test1', data: { content: 'test content 1' } }, { id: 'test2', data: { content: 'test content 2' } } ]; const results = await this.adapter.putBulk(this.testStoreName, testItems); if (!Array.isArray(results)) { throw new Error('putBulk should return an array'); } } async testDeleteBulk() { const testIds = ['test1', 'test2']; await this.adapter.deleteBulk(this.testStoreName, testIds); // 验证删除后是否还能读取 const items = await this.adapter.readBulk(this.testStoreName, testIds); if (items.length > 0) { throw new Error('Items should be deleted'); } } async testClearStore() { const result = await this.adapter.clearStore(this.testStoreName); if (typeof result !== 'boolean') { throw new Error('clearStore should return a boolean'); } } async testGetStores() { const stores = await this.adapter.getStores(); if (!Array.isArray(stores)) { throw new Error('getStores should return an array of strings'); } } } export async function testAdapterFunctionality(adapter, testStoreName) { const tester = new AdapterTester(adapter, testStoreName); return await tester.runAllTests(); } /** * 使用示例: * * const adapter = new YourDatabaseAdapter(); * const results = await testAdapterFunctionality(adapter); * * console.log('Test Results:', results); * if (results.success) { * console.log('All tests passed!'); * } else { * console.log('Some tests failed:', * Object.entries(results.results) * .filter(([_, result]) => !result.success) * .map(([name, result]) => `${name}: ${result.message}`) * ); * } */