1d6e1fb4da
Replace `as any` cast with `as unknown as ReturnType<typeof createMeiliClient>` in the mock client factory. This preserves type safety without requiring the mock to implement the full SDK interface. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
68 lines
2.3 KiB
TypeScript
68 lines
2.3 KiB
TypeScript
import {
|
|
MESSAGES_INDEX,
|
|
configureIndex,
|
|
indexMessage,
|
|
deleteMessage,
|
|
MeiliDocument,
|
|
createMeiliClient,
|
|
} from './index';
|
|
|
|
function makeMockClient() {
|
|
const mockUpdateSettings = jest.fn().mockResolvedValue({});
|
|
const mockAddDocuments = jest.fn().mockResolvedValue({});
|
|
const mockDeleteDocument = jest.fn().mockResolvedValue({});
|
|
const mockIndex = jest.fn().mockReturnValue({
|
|
updateSettings: mockUpdateSettings,
|
|
addDocuments: mockAddDocuments,
|
|
deleteDocument: mockDeleteDocument,
|
|
});
|
|
return { client: { index: mockIndex } as unknown as ReturnType<typeof createMeiliClient>, mockIndex, mockUpdateSettings, mockAddDocuments, mockDeleteDocument };
|
|
}
|
|
|
|
describe('MESSAGES_INDEX', () => {
|
|
it('is the string tower-messages', () => {
|
|
expect(MESSAGES_INDEX).toBe('tower-messages');
|
|
});
|
|
});
|
|
|
|
describe('configureIndex', () => {
|
|
it('sets searchable, filterable, and sortable attributes on the correct index', async () => {
|
|
const { client, mockIndex, mockUpdateSettings } = makeMockClient();
|
|
await configureIndex(client);
|
|
expect(mockIndex).toHaveBeenCalledWith('tower-messages');
|
|
expect(mockUpdateSettings).toHaveBeenCalledWith({
|
|
searchableAttributes: ['content', 'senderName', 'sourceGroupName'],
|
|
filterableAttributes: ['sourceGroupId', 'tags', 'platform'],
|
|
sortableAttributes: ['approvedAt'],
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('indexMessage', () => {
|
|
it('adds the document to the messages index', async () => {
|
|
const { client, mockIndex, mockAddDocuments } = makeMockClient();
|
|
const doc: MeiliDocument = {
|
|
id: 'msg-1',
|
|
content: 'Hello community',
|
|
senderName: 'Alice',
|
|
sourceGroupId: 'grp-1',
|
|
sourceGroupName: 'UP Parivar',
|
|
tags: ['#important'],
|
|
platform: 'whatsapp',
|
|
approvedAt: 1716825600000,
|
|
};
|
|
await indexMessage(client, doc);
|
|
expect(mockIndex).toHaveBeenCalledWith('tower-messages');
|
|
expect(mockAddDocuments).toHaveBeenCalledWith([doc]);
|
|
});
|
|
});
|
|
|
|
describe('deleteMessage', () => {
|
|
it('deletes document by id from the messages index', async () => {
|
|
const { client, mockIndex, mockDeleteDocument } = makeMockClient();
|
|
await deleteMessage(client, 'msg-1');
|
|
expect(mockIndex).toHaveBeenCalledWith('tower-messages');
|
|
expect(mockDeleteDocument).toHaveBeenCalledWith('msg-1');
|
|
});
|
|
});
|