|
| 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 | +}); |
0 commit comments