From cc1e495976616c231df45759e5ed967794ee4265 Mon Sep 17 00:00:00 2001 From: Warren Lee <5959690+wrn14897@users.noreply.github.com> Date: Mon, 30 Nov 2026 12:27:03 +0800 Subject: [PATCH] test(api): fix flaky checkAlerts integration tests DocumentDB's documentdb_ttl_task pg_cron job purges expired documents every minute. checkAlerts tests evaluate alerts at fixed 2023/2024 dates, so their AlertHistory rows are already past the TTL; a purge landing between an evaluation and its assertion drops rows. Add an opt-in DOCDB_DISABLE_TTL to the DocumentDB entrypoint that unschedules the job, and set it only in docker-compose.ci.yml. Also reset the Slack webhook spy in the processAlert beforeEach: clearAllMocks keeps implementations, so a persistent mockRejectedValue would leak into later tests. --- docker-compose.ci.yml | 1 + docker/documentdb/entrypoint.sh | 7 +++++++ .../tasks/checkAlerts/__tests__/checkAlerts.int.test.ts | 3 +++ 3 files changed, 11 insertions(+) diff --git a/docker-compose.ci.yml b/docker-compose.ci.yml index c47ecf97c..cfb8a3a5d 100644 --- a/docker-compose.ci.yml +++ b/docker-compose.ci.yml @@ -40,6 +40,7 @@ services: environment: DOCDB_USER: hyperdx DOCDB_PASSWORD: hyperdx + DOCDB_DISABLE_TTL: 'true' ports: - '127.0.0.1:${HDX_CI_MONGO_PORT:-39999}:10260' networks: diff --git a/docker/documentdb/entrypoint.sh b/docker/documentdb/entrypoint.sh index d7b294b23..35c46c807 100755 --- a/docker/documentdb/entrypoint.sh +++ b/docker/documentdb/entrypoint.sh @@ -42,6 +42,13 @@ else start_postgres fi +# Integration tests write documents with timestamps far in the past, so the +# once-a-minute TTL purge can delete them mid-test. +if [ "${DOCDB_DISABLE_TTL:-false}" = true ]; then + psql -h /tmp -U postgres -d postgres -qtAX -v ON_ERROR_STOP=1 \ + -c "SELECT cron.unschedule(jobid) FROM cron.job WHERE jobname = 'documentdb_ttl_task';" >/dev/null +fi + echo "postgresql://postgres@localhost:5432/postgres" > /tmp/pg-url mkdir -p /tmp/tls export DOCUMENTDB_PG_URL_FILE=/tmp/pg-url DOCUMENTDB_TLS_STATE_DIR=/tmp/tls diff --git a/packages/api/src/tasks/checkAlerts/__tests__/checkAlerts.int.test.ts b/packages/api/src/tasks/checkAlerts/__tests__/checkAlerts.int.test.ts index 2fe606d23..7bb6cb4c2 100644 --- a/packages/api/src/tasks/checkAlerts/__tests__/checkAlerts.int.test.ts +++ b/packages/api/src/tasks/checkAlerts/__tests__/checkAlerts.int.test.ts @@ -2022,8 +2022,11 @@ describe('checkAlerts', () => { getMetadata: jest.fn().mockReturnValue(mockMetadata), })); + // clearAllMocks keeps implementations, so reset to stop one test's + // mockRejectedValue leaking into the next. jest .spyOn(slack, 'postMessageToWebhook') + .mockReset() .mockResolvedValueOnce(fromAny(null)); jest.spyOn(checkAlert, 'handleSendGenericWebhook');