Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

test(message): pin the plan of the message-time read of a user chat

Yudhi Armyndharisea3ace8820fd
diff --git a/src/modules/message/message-list-ordering.pg.spec.ts b/src/modules/message/message-list-ordering.pg.spec.ts
index 7658cbb93..7682a6926 100644
--- a/src/modules/message/message-list-ordering.pg.spec.ts
+++ b/src/modules/message/message-list-ordering.pg.spec.ts
@@ -2,7 +2,7 @@
skipped in the default test job, so its lines would be unread and skew the global coverage gate. */
import 'reflect-metadata';
import { randomUUID } from 'crypto';
-import { DataSource, Repository } from 'typeorm';
+import { DataSource, Repository, SelectQueryBuilder } from 'typeorm';
import { MessageService } from './message.service';
import { Message, MessageDirection } from './entities/message.entity';
import type { EngineRegistry } from '../../engine/engine-registry.service';
@@ -249,6 +249,65 @@ const POSTGRES_ENABLED = process.env.DATABASE_TYPE === 'postgres';
expect(served).toEqual(unfiltered);
});
+ /**
+ * The message-time order of a user chat keeps the plain sort key here, so the planner may read the
+ * (sessionId, timestamp, id) index in order. The SQLite key, an expression over the column, leaves it
+ * no ordered path: it must sort. The cost-based choice between the two depends on table statistics,
+ * so the plan is read with sorting disabled for the planner, which makes the answer exact: a plain
+ * key plans with no Sort node, an expression over the column cannot.
+ */
+ it('plans the message-time order of a user chat on the time index, with no sort step', async () => {
+ const sessionId = 'sess-chat-plan';
+ type PlanNode = { 'Node Type': string; 'Index Name'?: string; Plans?: PlanNode[] };
+ const planNodes = (node: PlanNode): PlanNode[] => [node, ...(node.Plans ?? []).flatMap(planNodes)];
+ const stored: Array<[string, number]> = [
+ ['[email protected]', 1000],
+ ['[email protected]', 1001],
+ ['[email protected]', 1002],
+ ['[email protected]', 1003],
+ ];
+ const builders = jest.spyOn(repository, 'createQueryBuilder');
+ const runner = ds.createQueryRunner();
+ try {
+ await new AddMessageWindowIndexes1790812800000().up(runner);
+ await repository.insert(
+ stored.map(([chatId, timestamp]) => ({
+ id: randomUUID(),
+ sessionId,
+ chatId,
+ from: chatId,
+ timestamp,
+ })),
+ );
+ builders.mockClear();
+ const page = await service.getMessages(sessionId, {
+ chatId: '[email protected]',
+ orderBy: 'timestamp',
+ since: 1000_000,
+ until: 1060_000,
+ });
+ expect(page.messages.map(m => m.timestamp)).toEqual([1002, 1001, 1000]);
+
+ // The first builder is the page statement itself, left in the state it was executed in.
+ const [sql, params] = (builders.mock.results[0].value as SelectQueryBuilder<Message>).getQueryAndParameters();
+ await runner.startTransaction();
+ await runner.query('SET LOCAL enable_sort = off');
+ const [{ 'QUERY PLAN': plans }] = (await runner.query(`EXPLAIN (FORMAT JSON) ${sql}`, params)) as Array<{
+ 'QUERY PLAN': Array<{ Plan: PlanNode }>;
+ }>;
+ const nodes = planNodes(plans[0].Plan);
+
+ expect(nodes.map(node => node['Index Name'])).toContain('IDX_messages_session_timestamp_id');
+ expect(nodes.map(node => node['Node Type'])).not.toContain('Sort');
+ } finally {
+ builders.mockRestore();
+ if (runner.isTransactionActive) await runner.rollbackTransaction();
+ await runner.release();
+ await repository.delete({ sessionId });
+ }
+ });
+
/**
* The tiebreaker gives the list a total order; it does not stop the WINDOW drifting, because
* `offset` addresses a position by count. A message arriving mid-walk pushes every older row down
diff --git a/src/modules/message/message-window.spec.ts b/src/modules/message/message-window.spec.ts
index ff7330974..1466ee4cb 100644
--- a/src/modules/message/message-window.spec.ts
+++ b/src/modules/message/message-window.spec.ts
@@ -198,6 +198,18 @@ describe('message-time selection', () => {
expect(plan.map((r: { detail: string }) => r.detail).join(' ')).not.toContain('TEMP B-TREE');
});
+ // A user chat expands to several id candidates (one per dialect). SQLite has no planner statistics
+ // here, so a plain sort key makes it walk the session's whole time index and filter the chat row by
+ // row; the unary plus on the key makes it seek the chat index and sort only that chat's rows.
+ it('seeks the chat index, not the session time index, for a user chat with several id candidates', async () => {
+ await service.getMessages('s1', { orderBy: 'timestamp', chatId: '[email protected]', since: 1000_000, until: 1060_000 });
+ const page = queries.find(q => q.sql.includes('ORDER BY') && q.sql.includes('LIMIT'))!;
+ const plan = await ds.query<{ detail: string }[]>(`EXPLAIN QUERY PLAN ${page.sql}`, page.params);
+ const details = plan.map((r: { detail: string }) => r.detail).join(' ');
+ expect(details).toContain('IDX_messages_session_chat_timestamp_id');
+ expect(details).not.toContain('IDX_messages_session_timestamp_id');
+ });
+
it('migration preserves rows when applied and reverted', async () => {
const runner = ds.createQueryRunner();
const migration = new AddMessageWindowIndexes1790812800000();