mirror of
https://github.com/lobehub/lobehub.git
synced 2026-03-27 13:29:15 +07:00
🐛 fix: sloved the old removeSessionTopics not work (#11671)
* fix: sloved the old removeSessionTopics not work * fix: add the test
This commit is contained in:
@@ -285,6 +285,80 @@ describe('Topic Router Integration Tests', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('batchDeleteByAgentId', () => {
|
||||
it('should batch delete topics by agentId (new data)', async () => {
|
||||
const caller = topicRouter.createCaller(createTestContext(userId));
|
||||
|
||||
// Create topics with agentId directly (new data structure)
|
||||
const topicId1 = await caller.createTopic({
|
||||
title: 'Agent Topic 1',
|
||||
agentId: testAgentId,
|
||||
});
|
||||
const topicId2 = await caller.createTopic({
|
||||
title: 'Agent Topic 2',
|
||||
agentId: testAgentId,
|
||||
});
|
||||
|
||||
// Batch delete by agentId
|
||||
await caller.batchDeleteByAgentId({
|
||||
agentId: testAgentId,
|
||||
});
|
||||
|
||||
const remainingTopics = await serverDB.select().from(topics).where(eq(topics.userId, userId));
|
||||
|
||||
expect(remainingTopics).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('should batch delete topics by agentId (legacy sessionId data)', async () => {
|
||||
const caller = topicRouter.createCaller(createTestContext(userId));
|
||||
|
||||
// Create topics with sessionId (legacy data structure)
|
||||
await caller.createTopic({
|
||||
title: 'Legacy Topic 1',
|
||||
sessionId: testSessionId,
|
||||
});
|
||||
await caller.createTopic({
|
||||
title: 'Legacy Topic 2',
|
||||
sessionId: testSessionId,
|
||||
});
|
||||
|
||||
// Batch delete by agentId should also delete legacy topics via sessionId mapping
|
||||
await caller.batchDeleteByAgentId({
|
||||
agentId: testAgentId,
|
||||
});
|
||||
|
||||
const remainingTopics = await serverDB
|
||||
.select()
|
||||
.from(topics)
|
||||
.where(eq(topics.sessionId, testSessionId));
|
||||
|
||||
expect(remainingTopics).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('should batch delete topics by agentId (mixed data)', async () => {
|
||||
const caller = topicRouter.createCaller(createTestContext(userId));
|
||||
|
||||
// Create both new (agentId) and legacy (sessionId) topics
|
||||
await caller.createTopic({
|
||||
title: 'New Agent Topic',
|
||||
agentId: testAgentId,
|
||||
});
|
||||
await caller.createTopic({
|
||||
title: 'Legacy Session Topic',
|
||||
sessionId: testSessionId,
|
||||
});
|
||||
|
||||
// Batch delete by agentId should delete both
|
||||
await caller.batchDeleteByAgentId({
|
||||
agentId: testAgentId,
|
||||
});
|
||||
|
||||
const remainingTopics = await serverDB.select().from(topics).where(eq(topics.userId, userId));
|
||||
|
||||
expect(remainingTopics).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('searchTopics', () => {
|
||||
it('should search topics using agentId', async () => {
|
||||
const caller = topicRouter.createCaller(createTestContext(userId));
|
||||
|
||||
@@ -76,6 +76,12 @@ export const topicRouter = router({
|
||||
return ctx.topicModel.batchDelete(input.ids);
|
||||
}),
|
||||
|
||||
batchDeleteByAgentId: topicProcedure
|
||||
.input(z.object({ agentId: z.string() }))
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
return ctx.topicModel.batchDeleteByAgentId(input.agentId);
|
||||
}),
|
||||
|
||||
batchDeleteBySessionId: topicProcedure
|
||||
.input(
|
||||
z.object({
|
||||
|
||||
@@ -109,6 +109,10 @@ export class TopicService {
|
||||
return lambdaClient.topic.batchDeleteBySessionId.mutate({ id: this.toDbSessionId(sessionId) });
|
||||
};
|
||||
|
||||
removeTopicsByAgentId = (agentId: string) => {
|
||||
return lambdaClient.topic.batchDeleteByAgentId.mutate({ agentId });
|
||||
};
|
||||
|
||||
batchRemoveTopics = (topics: string[]) => {
|
||||
return lambdaClient.topic.batchDelete.mutate({ ids: topics });
|
||||
};
|
||||
|
||||
@@ -28,6 +28,7 @@ vi.mock('zustand/traditional');
|
||||
vi.mock('@/services/topic', () => ({
|
||||
topicService: {
|
||||
removeTopics: vi.fn(),
|
||||
removeTopicsByAgentId: vi.fn(),
|
||||
removeAllTopic: vi.fn(),
|
||||
removeTopic: vi.fn(),
|
||||
cloneTopic: vi.fn(),
|
||||
@@ -570,7 +571,7 @@ describe('topic action', () => {
|
||||
await result.current.removeSessionTopics();
|
||||
});
|
||||
|
||||
expect(topicService.removeTopics).toHaveBeenCalledWith(activeAgentId);
|
||||
expect(topicService.removeTopicsByAgentId).toHaveBeenCalledWith(activeAgentId);
|
||||
expect(refreshTopicSpy).toHaveBeenCalled();
|
||||
expect(switchTopicSpy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -567,7 +567,7 @@ export const chatTopic: StateCreator<
|
||||
const { switchTopic, activeAgentId, refreshTopic } = get();
|
||||
if (!activeAgentId) return;
|
||||
|
||||
await topicService.removeTopics(activeAgentId);
|
||||
await topicService.removeTopicsByAgentId(activeAgentId);
|
||||
await refreshTopic();
|
||||
|
||||
// switch to default topic
|
||||
|
||||
Reference in New Issue
Block a user