🐛 fix: sloved the old removeSessionTopics not work (#11671)

* fix: sloved the old removeSessionTopics not work

* fix: add the test
This commit is contained in:
Shinji-Li
2026-01-21 11:58:01 +08:00
committed by GitHub
parent c0f9875195
commit 06d41e5153
5 changed files with 87 additions and 2 deletions

View File

@@ -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));

View File

@@ -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({

View File

@@ -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 });
};

View File

@@ -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();
});

View File

@@ -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