Skip to content

Commit 18a4fd1

Browse files
committed
tests
1 parent bff36c5 commit 18a4fd1

3 files changed

Lines changed: 100 additions & 1 deletion

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import type { IRouter } from '@kbn/core/server';
9+
import { kibanaResponseFactory } from '@kbn/core/server';
10+
import { httpServerMock, loggingSystemMock } from '@kbn/core/server/mocks';
11+
import { registerInternalConversationRoutes } from './conversations';
12+
import type { RouteDependencies } from '../types';
13+
import { internalApiPath } from '../../../common/constants';
14+
15+
const MARK_READ_PATH = `${internalApiPath}/conversations/{conversation_id}/_mark_read`;
16+
17+
describe('registerInternalConversationRoutes - _mark_read', () => {
18+
let routeHandler: (ctx: any, req: any, res: any) => Promise<any>;
19+
let update: jest.Mock;
20+
21+
const createMockContext = () => ({
22+
core: Promise.resolve({}),
23+
licensing: Promise.resolve({
24+
license: { status: 'active', hasAtLeast: jest.fn().mockReturnValue(true) },
25+
}),
26+
});
27+
28+
const createRequest = (overrides: { params?: object; body?: object } = {}) =>
29+
httpServerMock.createKibanaRequest({
30+
method: 'post',
31+
path: MARK_READ_PATH,
32+
params: { conversation_id: 'conv-1' },
33+
body: { read: true },
34+
...overrides,
35+
});
36+
37+
beforeEach(() => {
38+
jest.clearAllMocks();
39+
40+
update = jest.fn().mockResolvedValue({ id: 'conv-1', read: true });
41+
42+
const getInternalServices = jest.fn().mockReturnValue({
43+
conversations: {
44+
getScopedClient: jest.fn().mockResolvedValue({ update }),
45+
},
46+
});
47+
48+
const routeHandlers: Record<string, (ctx: any, req: any, res: any) => Promise<any>> = {};
49+
50+
const router = {
51+
post: jest
52+
.fn()
53+
.mockImplementation(
54+
(config: { path: string }, handler: (ctx: any, req: any, res: any) => Promise<any>) => {
55+
routeHandlers[config.path] = handler;
56+
}
57+
),
58+
} as unknown as IRouter;
59+
60+
registerInternalConversationRoutes({
61+
router,
62+
getInternalServices,
63+
logger: loggingSystemMock.createLogger(),
64+
} as unknown as RouteDependencies);
65+
66+
routeHandler = routeHandlers[MARK_READ_PATH];
67+
});
68+
69+
it('calls client.update and returns id and read on success', async () => {
70+
const response = await routeHandler(
71+
createMockContext() as any,
72+
createRequest(),
73+
kibanaResponseFactory
74+
);
75+
76+
expect(update).toHaveBeenCalledWith({ id: 'conv-1', read: true });
77+
expect(response.status).toBe(200);
78+
expect(response.payload).toMatchObject({ id: 'conv-1', read: true });
79+
});
80+
81+
it('returns 500 when the persisted read value does not match the requested value', async () => {
82+
// Simulates ES returning a stale/legacy doc where `read` is undefined
83+
update.mockResolvedValue({ id: 'conv-1', read: undefined });
84+
85+
const response = await routeHandler(
86+
createMockContext() as any,
87+
createRequest(),
88+
kibanaResponseFactory
89+
);
90+
91+
expect(response.status).toBe(500);
92+
});
93+
});

x-pack/platform/plugins/shared/agent_builder/server/routes/internal/conversations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export function registerInternalConversationRoutes({
6565
path: `${internalApiPath}/conversations/{conversation_id}/_mark_read`,
6666
validate: {
6767
params: schema.object({
68-
conversation_id: schema.string(),
68+
conversation_id: schema.string({ maxLength: 256 }),
6969
}),
7070
body: schema.object({
7171
read: schema.boolean(),

x-pack/platform/plugins/shared/agent_builder/server/services/execution/utils/conversations.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ describe('conversations utils', () => {
111111
expect(conversationClient.update).toHaveBeenCalledWith(
112112
expect.objectContaining({
113113
rounds: [newRound],
114+
read: false,
115+
status: newRound.status,
114116
})
115117
);
116118
});
@@ -147,6 +149,8 @@ describe('conversations utils', () => {
147149
expect(conversationClient.update).toHaveBeenCalledWith(
148150
expect.objectContaining({
149151
rounds: [existingRound, newRound],
152+
read: false,
153+
status: newRound.status,
150154
})
151155
);
152156
});
@@ -184,6 +188,8 @@ describe('conversations utils', () => {
184188
expect(conversationClient.update).toHaveBeenCalledWith(
185189
expect.objectContaining({
186190
rounds: [newRound],
191+
read: false,
192+
status: newRound.status,
187193
})
188194
);
189195
});

0 commit comments

Comments
 (0)