Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(identity): run the shared lid table cases on PostgreSQL
Yudhi Armyndharis412155f4ead5
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex 1384b30bc..5219b4eb8 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -260,19 +260,20 @@ jobs:# failure recovery and media metadata updates hold on the real server, that a send# idempotency key is claimed by a unique-violation insert, and that a replace-all import runs (its# idempotency-key trim casts a varchar session id against a uuid). The PostgreSQL-specific- # suites self-skip unless DATABASE_TYPE=postgres. Session lifetime and two-node delete scenarios- # run against SQLite in the default test job and against the postgres:16 service here.+ # suites self-skip unless DATABASE_TYPE=postgres. Session lifetime, two-node delete and lid+ # mapping ownership scenarios run against SQLite in the default test job and against the+ # postgres:16 service here.## TZ is deliberately NOT UTC: the pin's whole job is to make a `timestamp` column mean the same# instant on a host that runs in another zone, and on a UTC runner every assertion about it# would pass for the wrong reason (the UTC-pin spec fails outright rather than pass vacuously).- - name: Postgres specs (FTS + migrations + ordering + schema + UTC + recovery + idempotency + import + lifetime)+ - name: Postgres specs (FTS + migrations + ordering + schema + UTC + recovery + idempotency + import + lifetime + lid owners)# --runInBand: the specs issue DDL against the one shared service database, and jest's# default file-parallelism races their catalog writes (pg_type duplicate-key). The lock# spec's own two-migrator race is in-file (Promise.all) and unaffected by runInBand. The# synchronize spec additionally CREATEs and DROPs a scratch database of its own, which is# only safe serialized.- run: npx jest --runInBand src/database/migrations/__tests__/1782400000000-AddMessagesFts.pg.spec.ts src/database/migrations/__tests__/pg-boot-migrations.pg.spec.ts src/modules/message/message-list-ordering.pg.spec.ts src/database/migrations/__tests__/pg-entity-synchronize.pg.spec.ts src/database/postgres-utc.pg.spec.ts src/modules/webhook/webhook-recovery.pg.spec.ts src/modules/message/idempotency/send-idempotency.service.pg.spec.ts src/modules/infra/infra-data.import.pg.spec.ts src/modules/session/session-content-lifetime.spec.ts src/modules/session/session-delete-claim.spec.ts+ run: npx jest --runInBand src/database/migrations/__tests__/1782400000000-AddMessagesFts.pg.spec.ts src/database/migrations/__tests__/pg-boot-migrations.pg.spec.ts src/modules/message/message-list-ordering.pg.spec.ts src/database/migrations/__tests__/pg-entity-synchronize.pg.spec.ts src/database/postgres-utc.pg.spec.ts src/modules/webhook/webhook-recovery.pg.spec.ts src/modules/message/idempotency/send-idempotency.service.pg.spec.ts src/modules/infra/infra-data.import.pg.spec.ts src/modules/session/session-content-lifetime.spec.ts src/modules/session/session-delete-claim.spec.ts src/engine/identity/lid-mapping-store.spec.tsenv:TZ: Asia/JakartaDATABASE_TYPE: postgresdiff --git a/.github/workflows/release.yml b/.github/workflows/release.ymlindex 8fc0bb514..b7bf657ef 100644--- a/.github/workflows/release.yml+++ b/.github/workflows/release.yml@@ -233,17 +233,17 @@ jobs:# retention window and a local-midnight count, that webhook failure recovery and media metadata# updates hold on the real server, that a send idempotency key is claimed by a unique-violation# insert, and that a replace-all import runs. The PostgreSQL-specific suites self-skip unless- # DATABASE_TYPE=postgres. Session lifetime and two-node delete scenarios use SQLite in the default- # test job and the postgres:16 service here.+ # DATABASE_TYPE=postgres. Session lifetime, two-node delete and lid mapping ownership scenarios use+ # SQLite in the default test job and the postgres:16 service here.# TZ is deliberately not UTC, for the reason ci.yml states.- - name: Postgres specs (FTS + migrations + ordering + schema + UTC + recovery + idempotency + import + lifetime)+ - name: Postgres specs (FTS + migrations + ordering + schema + UTC + recovery + idempotency + import + lifetime + lid owners)# --runInBand: same reason the CI job passes it — both specs issue DDL against the one# shared service database, and jest's default file-parallelism races their catalog writes# (the first v0.19.0 tag failed this step on `relation "messages" already exists` over a# tree CI had passed minutes earlier). The lock spec's own two-migrator race is in-file# (Promise.all) and unaffected by runInBand. The synchronize spec additionally CREATEs and# DROPs a scratch database of its own, which is only safe serialized.- run: npx jest --runInBand src/database/migrations/__tests__/1782400000000-AddMessagesFts.pg.spec.ts src/database/migrations/__tests__/pg-boot-migrations.pg.spec.ts src/modules/message/message-list-ordering.pg.spec.ts src/database/migrations/__tests__/pg-entity-synchronize.pg.spec.ts src/database/postgres-utc.pg.spec.ts src/modules/webhook/webhook-recovery.pg.spec.ts src/modules/message/idempotency/send-idempotency.service.pg.spec.ts src/modules/infra/infra-data.import.pg.spec.ts src/modules/session/session-content-lifetime.spec.ts src/modules/session/session-delete-claim.spec.ts+ run: npx jest --runInBand src/database/migrations/__tests__/1782400000000-AddMessagesFts.pg.spec.ts src/database/migrations/__tests__/pg-boot-migrations.pg.spec.ts src/modules/message/message-list-ordering.pg.spec.ts src/database/migrations/__tests__/pg-entity-synchronize.pg.spec.ts src/database/postgres-utc.pg.spec.ts src/modules/webhook/webhook-recovery.pg.spec.ts src/modules/message/idempotency/send-idempotency.service.pg.spec.ts src/modules/infra/infra-data.import.pg.spec.ts src/modules/session/session-content-lifetime.spec.ts src/modules/session/session-delete-claim.spec.ts src/engine/identity/lid-mapping-store.spec.tsenv:TZ: Asia/JakartaDATABASE_TYPE: postgresdiff --git a/docs/09-testing-strategy.md b/docs/09-testing-strategy.mdindex f80e12ffe..58a227200 100644--- a/docs/09-testing-strategy.md+++ b/docs/09-testing-strategy.md@@ -259,7 +259,7 @@ request's superseded run; push runs on `main` are never cancelled or queued behi| `lint` | backend ESLint, full-program TypeScript check, formatting, version consistency, .dockerignore context, OpenAPI snapshot, SDK routes and webhook events against the contract, contract coverage per SDK, SDK docs against the shipped client surface, client wire shapes (`check:contract-shapes` — the JavaScript SDK's, dashboard's, Python's, Go's and Java's hand-written types against the OpenAPI schemas; the PHP client returns untyped arrays and has no types layer to gate) || `audit` | dependency security audit of all three npm trees (root, `dashboard/` and `sdk/javascript/`); when npm's audit endpoint cannot answer, the root audit skips with a warning annotation here while the dashboard and SDK audits, a plain `npm audit`, fail closed, so the job still goes red until a re-run; the release gate and the weekly scan fail (`CHECK_AUDIT_REQUIRED=1`) || `test` | backend coverage run, repo-file drift gates (`test:docs`), script unit tests (node:test), e2e smoke tests |-| `test-postgres` | real PostgreSQL 16 service, backend build, migration smoke, and the PostgreSQL specs (FTS provider, boot-migration lock, message list ordering, entity synchronize, UTC pin, webhook recovery, send idempotency keys, replace-all import, session content lifetimes, two-node session delete) |+| `test-postgres` | real PostgreSQL 16 service, backend build, migration smoke, and the PostgreSQL specs (FTS provider, boot-migration lock, message list ordering, entity synchronize, UTC pin, webhook recovery, send idempotency keys, replace-all import, session content lifetimes, two-node session delete, lid mapping ownership) || `dashboard` | dashboard install, lint, formatting, type-check, i18n parity, build, unit tests || `scripts-smoke` | shellcheck on `docker-entrypoint.sh` and every `scripts/*.sh`, plus the backup/restore smoke test || `chart` | helm lint, helm template with default and fully-toggled values, kubeconform on both renders, the rendered-behaviour check, actionlint on the workflows |diff --git a/src/common/test-lane-partition.spec.ts b/src/common/test-lane-partition.spec.tsindex 7f61fcc3c..0e5267d79 100644--- a/src/common/test-lane-partition.spec.ts+++ b/src/common/test-lane-partition.spec.ts@@ -153,6 +153,7 @@ describe('Test lane partition', () => {const dialectSwitchedSpecs = ['src/modules/session/session-content-lifetime.spec.ts','src/modules/session/session-delete-claim.spec.ts',+ 'src/engine/identity/lid-mapping-store.spec.ts',];it.each(['ci.yml', 'release.yml'])('runs the dialect-switched specs in the %s test-postgres job', file => {diff --git a/src/engine/identity/lid-mapping-store.spec.ts b/src/engine/identity/lid-mapping-store.spec.tsindex 717ce9e3b..138106cb5 100644--- a/src/engine/identity/lid-mapping-store.spec.ts+++ b/src/engine/identity/lid-mapping-store.spec.ts@@ -1,3 +1,4 @@+import { randomUUID } from 'crypto';import { DataSource, FindOperator, In, Repository } from 'typeorm';import { LidMappingStoreService } from './lid-mapping-store.service';import { LidMapping } from './lid-mapping.entity';@@ -802,13 +803,34 @@ describe('LidMappingStoreService: a negative result on the shared table', () =>return { release, reached };}+ // SQLite in memory; on PostgreSQL (DATABASE_TYPE=postgres, the test-postgres job) a schema of its own, on+ // the search_path as the app sets it, since the statements differ by dialect.+ const pg = process.env.DATABASE_TYPE === 'postgres';+ const server = {+ type: 'postgres' as const,+ host: process.env.DATABASE_HOST || 'localhost',+ port: Number(process.env.DATABASE_PORT || 5432),+ username: process.env.DATABASE_USERNAME || 'openwa',+ password: process.env.DATABASE_PASSWORD || 'openwa',+ database: process.env.DATABASE_NAME || 'openwa',+ };+ let schema = '';+ const onServer = async (sql: string): Promise<void> => {+ const admin = await new DataSource(server).initialize();+ await admin.query(sql).finally(() => admin.destroy());+ };+beforeEach(async () => {- ds = await new DataSource({- type: 'better-sqlite3',- database: ':memory:',- entities: [LidMapping, Session],- synchronize: true,- }).initialize();+ const entities = [LidMapping, Session];+ if (pg) {+ schema = `lid_store_${randomUUID().replace(/-/g, '')}`;+ await onServer(`CREATE SCHEMA "${schema}"`);+ }+ ds = await new DataSource(+ pg+ ? { ...server, schema, extra: { options: `-c search_path=${schema},public` }, entities, synchronize: true }+ : { type: 'better-sqlite3', database: ':memory:', entities, synchronize: true },+ ).initialize();await ds.getRepository(Session).insert([s1, s2, s3, s9].map(id => ({ id, name: id })));repo = ds.getRepository(LidMapping);store = new LidMappingStoreService(repo);@@ -818,8 +840,9 @@ describe('LidMappingStoreService: a negative result on the shared table', () =>});afterEach(async () => {- expect(warn).not.toHaveBeenCalled();await ds.destroy();+ if (pg) await onServer(`DROP SCHEMA "${schema}" CASCADE`);+ expect(warn).not.toHaveBeenCalled();});it('records a negative for a lid with no row', async () => {