Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(search): cover paging, count and the query filters
Yudhi Armyndharis4dad1efc10a2
diff --git a/src/modules/search/providers/builtin-fts.provider.spec.ts b/src/modules/search/providers/builtin-fts.provider.spec.tsindex 7a0bab1b2..215ea45fb 100644--- a/src/modules/search/providers/builtin-fts.provider.spec.ts+++ b/src/modules/search/providers/builtin-fts.provider.spec.ts@@ -3,6 +3,7 @@ import { DataSource } from 'typeorm';import { Message, MessageDirection } from '../../../modules/message/entities/message.entity';import { Session } from '../../../modules/session/entities/session.entity';import { BuiltInFtsProvider } from './builtin-fts.provider';+import type { SearchQuery } from '../search.types';import { AddMessagesFts1782400000000 } from '../../../database/migrations/1782400000000-AddMessagesFts';describe('BuiltInFtsProvider (sqlite)', () => {@@ -55,7 +56,7 @@ describe('BuiltInFtsProvider (sqlite)', () => {});afterEach(() => ds.destroy());- it('matches by keyword and ranks + paginates', async () => {+ it('matches by keyword with a highlighted snippet', async () => {const res = await provider.search({ q: 'hello', limit: 10 });expect(res.provider).toBe('builtin-fts');expect(res.hits.length).toBe(2);@@ -63,6 +64,65 @@ describe('BuiltInFtsProvider (sqlite)', () => {expect(res.total).toBe(2);});+ it('pages one hit at a time in rank, timestamp DESC, id DESC order with the full total', async () => {+ // Same length as the other two hello rows, so every hit ranks alike, and the same timestamp as+ // 'hello again', so only the id tiebreak orders that pair. limit 1 with offset > 0 makes the+ // provider count instead of taking rows.length as the total.+ await ds.getRepository(Message).insert({+ sessionId: 's2',+ chatId: 'c2',+ from: '[email protected]',+ to: '[email protected]',+ body: 'hello there',+ type: 'text',+ direction: MessageDirection.INCOMING,+ timestamp: 3,+ });+ const all = await ds.getRepository(Message).find();+ const expected = all+ .filter(m => m.body?.startsWith('hello'))+ .sort((a, b) => b.timestamp - a.timestamp || (a.id < b.id ? 1 : -1))+ .map(m => m.id);++ const paged: string[] = [];+ for (let offset = 0; offset < 3; offset++) {+ const res = await provider.search({ q: 'hello', limit: 1, offset });+ expect(res.total).toBe(3);+ paged.push(...res.hits.map(h => h.messageId));+ }+ expect(paged).toEqual(expected);+ expect((await provider.search({ q: 'hello', limit: 1, offset: 3 })).hits).toEqual([]);+ });++ it('applies the chatId, from, direction, type and dateTo filters', async () => {+ await ds.getRepository(Message).insert({+ sessionId: 's1',+ chatId: 'c1',+ from: '[email protected]',+ to: '[email protected]',+ body: 'hello picture',+ type: 'image',+ direction: MessageDirection.OUTGOING,+ timestamp: 2,+ });+ const bodies = async (filter: Omit<SearchQuery, 'q'>): Promise<string[]> =>+ (await provider.search({ q: 'hello', ...filter })).hits.map(h => h.body).sort();++ expect(await bodies({ chatId: 'c2' })).toEqual(['hello again']);+ expect(await bodies({ from: '[email protected]' })).toEqual(['hello picture', 'hello world']);+ expect(await bodies({ direction: MessageDirection.INCOMING })).toEqual(['hello again']);+ expect(await bodies({ type: 'image' })).toEqual(['hello picture']);+ expect(await bodies({ type: ['text', 'image'] })).toEqual(['hello again', 'hello picture', 'hello world']);+ expect(await bodies({ dateTo: 2000 })).toEqual(['hello picture', 'hello world']);++ const combined = { chatId: 'c1', from: '[email protected]', direction: MessageDirection.OUTGOING, type: 'text' as const };+ expect(await bodies({ ...combined, dateTo: 1000 })).toEqual(['hello world']);+ // Counted rather than taken from rows.length, with every filter bound in the count query too.+ const page = await provider.search({ q: 'hello', ...combined, dateTo: 3000, limit: 1, offset: 1 });+ expect(page.hits).toEqual([]);+ expect(page.total).toBe(1);+ });+it('scopes by sessionIds (auth) and by sessionId filter', async () => {const scoped = await provider.search({ q: 'hello', sessionIds: ['s1'] });expect(scoped.hits.every(h => h.sessionId === 's1')).toBe(true);