import { DataSource } from 'typeorm'; import { AddAiChat1784780000000 } from '../migrations/1784780000000-AddAiChat'; describe('AddAiChat1784780000000', () => { let dataSource: DataSource; beforeEach(async () => { dataSource = new DataSource({ type: 'better-sqlite3', database: ':memory:', migrations: [AddAiChat1784780000000], }); await dataSource.initialize(); await dataSource.query( 'CREATE TABLE users (id integer PRIMARY KEY AUTOINCREMENT, username varchar(100) NOT NULL)', ); }); afterEach(async () => { if (dataSource.isInitialized) await dataSource.destroy(); }); it('创建会话、消息和工具记录表,并按会话级联删除', async () => { await dataSource.runMigrations(); for (const table of ['ai_conversations', 'ai_messages', 'ai_tool_runs']) { expect(await dataSource.createQueryRunner().hasTable(table)).toBe(true); } await dataSource.query("INSERT INTO users (username) VALUES ('tester')"); await dataSource.query( "INSERT INTO ai_conversations (user_id, title) VALUES (1, '测试会话')", ); await dataSource.query( "INSERT INTO ai_messages (conversation_id, role, content) VALUES (1, 'assistant', '回答')", ); await dataSource.query( "INSERT INTO ai_tool_runs (message_id, tool_call_id, tool_name, status) VALUES (1, 'call_1', 'search_students', 'success')", ); await dataSource.query('DELETE FROM ai_conversations WHERE id = 1'); expect(await dataSource.query('SELECT id FROM ai_messages')).toEqual([]); expect(await dataSource.query('SELECT id FROM ai_tool_runs')).toEqual([]); }); });