Skip to content

Commit 06d41e5

Browse files
authored
πŸ› fix: sloved the old removeSessionTopics not work (#11671)
* fix: sloved the old removeSessionTopics not work * fix: add the test
1 parent c0f9875 commit 06d41e5

File tree

5 files changed

+87
-2
lines changed

5 files changed

+87
-2
lines changed

β€Žsrc/server/routers/lambda/__tests__/integration/topic.integration.test.tsβ€Ž

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,80 @@ describe('Topic Router Integration Tests', () => {
285285
});
286286
});
287287

288+
describe('batchDeleteByAgentId', () => {
289+
it('should batch delete topics by agentId (new data)', async () => {
290+
const caller = topicRouter.createCaller(createTestContext(userId));
291+
292+
// Create topics with agentId directly (new data structure)
293+
const topicId1 = await caller.createTopic({
294+
title: 'Agent Topic 1',
295+
agentId: testAgentId,
296+
});
297+
const topicId2 = await caller.createTopic({
298+
title: 'Agent Topic 2',
299+
agentId: testAgentId,
300+
});
301+
302+
// Batch delete by agentId
303+
await caller.batchDeleteByAgentId({
304+
agentId: testAgentId,
305+
});
306+
307+
const remainingTopics = await serverDB.select().from(topics).where(eq(topics.userId, userId));
308+
309+
expect(remainingTopics).toHaveLength(0);
310+
});
311+
312+
it('should batch delete topics by agentId (legacy sessionId data)', async () => {
313+
const caller = topicRouter.createCaller(createTestContext(userId));
314+
315+
// Create topics with sessionId (legacy data structure)
316+
await caller.createTopic({
317+
title: 'Legacy Topic 1',
318+
sessionId: testSessionId,
319+
});
320+
await caller.createTopic({
321+
title: 'Legacy Topic 2',
322+
sessionId: testSessionId,
323+
});
324+
325+
// Batch delete by agentId should also delete legacy topics via sessionId mapping
326+
await caller.batchDeleteByAgentId({
327+
agentId: testAgentId,
328+
});
329+
330+
const remainingTopics = await serverDB
331+
.select()
332+
.from(topics)
333+
.where(eq(topics.sessionId, testSessionId));
334+
335+
expect(remainingTopics).toHaveLength(0);
336+
});
337+
338+
it('should batch delete topics by agentId (mixed data)', async () => {
339+
const caller = topicRouter.createCaller(createTestContext(userId));
340+
341+
// Create both new (agentId) and legacy (sessionId) topics
342+
await caller.createTopic({
343+
title: 'New Agent Topic',
344+
agentId: testAgentId,
345+
});
346+
await caller.createTopic({
347+
title: 'Legacy Session Topic',
348+
sessionId: testSessionId,
349+
});
350+
351+
// Batch delete by agentId should delete both
352+
await caller.batchDeleteByAgentId({
353+
agentId: testAgentId,
354+
});
355+
356+
const remainingTopics = await serverDB.select().from(topics).where(eq(topics.userId, userId));
357+
358+
expect(remainingTopics).toHaveLength(0);
359+
});
360+
});
361+
288362
describe('searchTopics', () => {
289363
it('should search topics using agentId', async () => {
290364
const caller = topicRouter.createCaller(createTestContext(userId));

β€Žsrc/server/routers/lambda/topic.tsβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ export const topicRouter = router({
7676
return ctx.topicModel.batchDelete(input.ids);
7777
}),
7878

79+
batchDeleteByAgentId: topicProcedure
80+
.input(z.object({ agentId: z.string() }))
81+
.mutation(async ({ input, ctx }) => {
82+
return ctx.topicModel.batchDeleteByAgentId(input.agentId);
83+
}),
84+
7985
batchDeleteBySessionId: topicProcedure
8086
.input(
8187
z.object({

β€Žsrc/services/topic/index.tsβ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ export class TopicService {
109109
return lambdaClient.topic.batchDeleteBySessionId.mutate({ id: this.toDbSessionId(sessionId) });
110110
};
111111

112+
removeTopicsByAgentId = (agentId: string) => {
113+
return lambdaClient.topic.batchDeleteByAgentId.mutate({ agentId });
114+
};
115+
112116
batchRemoveTopics = (topics: string[]) => {
113117
return lambdaClient.topic.batchDelete.mutate({ ids: topics });
114118
};

β€Žsrc/store/chat/slices/topic/action.test.tsβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ vi.mock('zustand/traditional');
2828
vi.mock('@/services/topic', () => ({
2929
topicService: {
3030
removeTopics: vi.fn(),
31+
removeTopicsByAgentId: vi.fn(),
3132
removeAllTopic: vi.fn(),
3233
removeTopic: vi.fn(),
3334
cloneTopic: vi.fn(),
@@ -570,7 +571,7 @@ describe('topic action', () => {
570571
await result.current.removeSessionTopics();
571572
});
572573

573-
expect(topicService.removeTopics).toHaveBeenCalledWith(activeAgentId);
574+
expect(topicService.removeTopicsByAgentId).toHaveBeenCalledWith(activeAgentId);
574575
expect(refreshTopicSpy).toHaveBeenCalled();
575576
expect(switchTopicSpy).toHaveBeenCalled();
576577
});

β€Žsrc/store/chat/slices/topic/action.tsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ export const chatTopic: StateCreator<
567567
const { switchTopic, activeAgentId, refreshTopic } = get();
568568
if (!activeAgentId) return;
569569

570-
await topicService.removeTopics(activeAgentId);
570+
await topicService.removeTopicsByAgentId(activeAgentId);
571571
await refreshTopic();
572572

573573
// switch to default topic

0 commit comments

Comments
Β (0)