Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions packages/server/src/utils/getChatMessage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { beforeEach, describe, expect, it, jest } from '@jest/globals'

jest.mock('./getRunningExpressApp', () => ({
getRunningExpressApp: jest.fn()
}))

import { utilGetChatMessage } from './getChatMessage'
import { getRunningExpressApp } from './getRunningExpressApp'
import { ChatFlow } from '../database/entities/ChatFlow'

const mockFindOneBy = jest.fn() as jest.Mock
const mockFind = jest.fn() as jest.Mock

describe('utilGetChatMessage', () => {
beforeEach(() => {
jest.clearAllMocks()
;(getRunningExpressApp as jest.Mock).mockReturnValue({
AppDataSource: {
getRepository: jest.fn((entity: unknown) => {
if (entity === ChatFlow) {
return { findOneBy: mockFindOneBy }
}

return { find: mockFind }
})
}
})

mockFindOneBy.mockImplementation(() => Promise.resolve({ id: 'flow-1', workspaceId: 'ws-1' }))
mockFind.mockImplementation(() => Promise.resolve([]))
})

it('applies skip and take for standard paginated queries', async () => {
await utilGetChatMessage({
chatflowid: 'flow-1',
activeWorkspaceId: 'ws-1',
page: 2,
pageSize: 25
})

expect(mockFind).toHaveBeenCalledWith(
expect.objectContaining({
skip: 25,
take: 25,
order: { createdDate: 'ASC' }
})
)
})

it('does not apply pagination when page and pageSize are not provided', async () => {
await utilGetChatMessage({
chatflowid: 'flow-1',
activeWorkspaceId: 'ws-1'
})

expect(mockFind).toHaveBeenCalledWith(
expect.objectContaining({
skip: undefined,
take: undefined
})
)
})
})
2 changes: 2 additions & 0 deletions packages/server/src/utils/getChatMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ export const utilGetChatMessage = async ({
relations: {
execution: true
},
skip: page > 0 && pageSize > 0 ? pageSize * (page - 1) : undefined,
take: page > 0 && pageSize > 0 ? pageSize : undefined,
order: {
createdDate: sortOrder === 'DESC' ? 'DESC' : 'ASC'
}
Expand Down