Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(search): respect millisecond lower bounds

Yudhi Armyndharis60164da1b2e7
diff --git a/src/database/migrations/__tests__/1782400000000-AddMessagesFts.pg.spec.ts b/src/database/migrations/__tests__/1782400000000-AddMessagesFts.pg.spec.ts
index 9e06c5fce..85a9f4189 100644
--- a/src/database/migrations/__tests__/1782400000000-AddMessagesFts.pg.spec.ts
+++ b/src/database/migrations/__tests__/1782400000000-AddMessagesFts.pg.spec.ts
@@ -123,6 +123,36 @@ const POSTGRES_ENABLED = process.env.DATABASE_TYPE === 'postgres';
expect(res.total).toBe(0);
});
+ it('applies inclusive millisecond windows to rows and paginated totals', async () => {
+ const timestamps = [-2, -1, 0, 1, 2, 1_700_000_000, 1_700_000_001];
+ for (const timestamp of timestamps) {
+ await ds.query(
+ `INSERT INTO "messages" ("id","sessionId","chatId","from","to","body","timestamp") ` +
+ `VALUES ($1,'s1','c1','[email protected]','[email protected]','boundarywindow',$2)`,
+ [`boundary-${timestamp}`, timestamp],
+ );
+ }
+ const windows = [
+ { dateFrom: 1000, dateTo: 2000, want: [2, 1] },
+ { dateFrom: 1001, dateTo: 1999, want: [] },
+ { dateFrom: 0, dateTo: 0, want: [0] },
+ { dateFrom: -1999, dateTo: -1, want: [-1] },
+ { dateFrom: -2000, dateTo: -1001, want: [-2] },
+ { dateFrom: 0.5, dateTo: 1999.5, want: [1] },
+ { dateFrom: 1000, dateTo: 1000, want: [1] },
+ { dateFrom: 2000, dateTo: 1000, want: [] },
+ { dateFrom: 1_700_000_000_001, dateTo: 1_700_000_001_000, want: [1_700_000_001] },
+ ];
+ for (const { want, ...bounds } of windows) {
+ const result = await provider.search({ q: 'boundarywindow', ...bounds });
+ expect(result.hits.map(hit => hit.timestamp)).toEqual(want);
+ expect(result.total).toBe(want.length);
+ const page = await provider.search({ q: 'boundarywindow', ...bounds, limit: 1, offset: 1 });
+ expect(page.hits.map(hit => hit.timestamp)).toEqual(want.slice(1, 2));
+ expect(page.total).toBe(want.length);
+ }
+ });
+
it('reports healthy', async () => {
expect((await provider.health()).ok).toBe(true);
});
diff --git a/src/modules/search/providers/builtin-fts.provider.postgres.spec.ts b/src/modules/search/providers/builtin-fts.provider.postgres.spec.ts
index bb7264cd6..2e8bd6a51 100644
--- a/src/modules/search/providers/builtin-fts.provider.postgres.spec.ts
+++ b/src/modules/search/providers/builtin-fts.provider.postgres.spec.ts
@@ -93,6 +93,21 @@ describe('BuiltInFtsProvider (postgres probe)', () => {
expect(calls[1][1]).toEqual(['hello', ...chatIds]);
});
+ it('binds inclusive millisecond bounds in seconds for both rows and count', async () => {
+ const { ds, query } = makePostgresDataSource({ bodyTsPresent: true });
+ await new BuiltInFtsProvider(ds).search({ q: 'hello', dateFrom: 1000.5, dateTo: 2999.5, limit: 1, offset: 1 });
+ const calls = query.mock.calls
+ .map(call => call as unknown[])
+ .filter(call => /websearch_to_tsquery/.test(String(call[0])));
+ expect(calls).toHaveLength(2);
+ for (const [sql] of calls) {
+ expect(String(sql)).toContain('m."timestamp" >= $2');
+ expect(String(sql)).toContain('m."timestamp" <= $3');
+ }
+ expect(calls[0][1]).toEqual(['hello', 2, 2, 1, 1]);
+ expect(calls[1][1]).toEqual(['hello', 2, 2]);
+ });
+
it('does not credit a namesake in another schema: an empty active-schema probe means 501, not "available"', async () => {
// A public.messages.body_ts must not make FTS look available when the table the search queries
// resolve (active schema) lacks the column. The scripted probe answers for the resolved table
diff --git a/src/modules/search/providers/builtin-fts.provider.spec.ts b/src/modules/search/providers/builtin-fts.provider.spec.ts
index 6532bfe96..f62cb36db 100644
--- a/src/modules/search/providers/builtin-fts.provider.spec.ts
+++ b/src/modules/search/providers/builtin-fts.provider.spec.ts
@@ -251,6 +251,40 @@ describe('BuiltInFtsProvider (sqlite)', () => {
expect(excluded.hits).toEqual([]);
});
+ it('applies inclusive millisecond windows to rows and paginated totals', async () => {
+ await ds.getRepository(Message).insert(
+ [-2, -1, 0, 1, 2, 1_700_000_000, 1_700_000_001].map(timestamp => ({
+ sessionId: 's1',
+ chatId: 'c1',
+ body: 'boundarywindow',
+ type: 'text',
+ direction: MessageDirection.OUTGOING,
+ timestamp,
+ })),
+ );
+ const windows = [
+ { dateFrom: 1000, dateTo: 2000, want: [2, 1] },
+ { dateFrom: 1001, dateTo: 1999, want: [] },
+ { dateFrom: 0, dateTo: 0, want: [0] },
+ { dateFrom: -1999, dateTo: -1, want: [-1] },
+ { dateFrom: -2000, dateTo: -1001, want: [-2] },
+ { dateFrom: 0.5, dateTo: 1999.5, want: [1] },
+ { dateFrom: 1000, dateTo: 1000, want: [1] },
+ { dateFrom: 2000, dateTo: 1000, want: [] },
+ { dateFrom: 1_700_000_000_001, dateTo: 1_700_000_001_000, want: [1_700_000_001] },
+ ];
+ for (const { want, ...bounds } of windows) {
+ const result = await provider.search({ q: 'boundarywindow', ...bounds });
+ expect(result.hits.map(hit => hit.timestamp)).toEqual(want);
+ expect(result.total).toBe(want.length);
+ const page = await provider.search({ q: 'boundarywindow', ...bounds, limit: 1, offset: 1 });
+ expect(page.hits.map(hit => hit.timestamp)).toEqual(want.slice(1, 2));
+ expect(page.total).toBe(want.length);
+ }
+ });
+
it('the FTS UPDATE trigger carries a WHEN body-change guard (not fire on ack-only updates)', async () => {
// The messages_fts_au trigger must be guarded so it only re-indexes when body actually changes.
// Without the WHEN clause every ack (SENT→DELIVERED→READ) and reaction does a redundant FTS
diff --git a/src/modules/search/providers/builtin-fts.provider.ts b/src/modules/search/providers/builtin-fts.provider.ts
index 10849d326..27da680a4 100644
--- a/src/modules/search/providers/builtin-fts.provider.ts
+++ b/src/modules/search/providers/builtin-fts.provider.ts
@@ -409,7 +409,7 @@ export class BuiltInFtsProvider implements SearchProvider, OnModuleInit {
// silently exclude all results.
if (q.dateFrom != null) {
where.push(`${prefix}"timestamp" >= ${ph()}`);
- params.push(Math.floor(q.dateFrom / 1000));
+ params.push(Math.ceil(q.dateFrom / 1000));
}
if (q.dateTo != null) {
where.push(`${prefix}"timestamp" <= ${ph()}`);