Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(infra): run the replace-all import against PostgreSQL
Yudhi Armyndharis36ec1c14119d
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex fbe3dcb90..84833b46f 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -271,7 +271,7 @@ jobs:# 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+ 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.tsenv:TZ: Asia/JakartaDATABASE_TYPE: postgresdiff --git a/.github/workflows/release.yml b/.github/workflows/release.ymlindex b156ba041..fc3731a9f 100644--- a/.github/workflows/release.yml+++ b/.github/workflows/release.yml@@ -242,7 +242,7 @@ jobs:# 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+ 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.tsenv:TZ: Asia/JakartaDATABASE_TYPE: postgresdiff --git a/src/modules/infra/infra-data.import.pg.spec.ts b/src/modules/infra/infra-data.import.pg.spec.tsnew file mode 100644index 000000000..bf22b5e47--- /dev/null+++ b/src/modules/infra/infra-data.import.pg.spec.ts@@ -0,0 +1,139 @@+/* istanbul ignore file: PG-gated, only runs under DATABASE_TYPE=postgres (test-postgres CI job);+ skipped in the default test job, so its lines would be unread and skew the global coverage gate. */+import 'reflect-metadata';+import { randomBytes, randomUUID } from 'crypto';+import { DataSource } from 'typeorm';+import { InfraDataController } from './infra-data.controller';+import { InfraDataService } from './infra-data.service';+import { Session, SessionStatus } from '../session/entities/session.entity';+import { Webhook } from '../webhook/entities/webhook.entity';+import { Message } from '../message/entities/message.entity';+import { MessageBatch } from '../message/entities/message-batch.entity';+import { Template } from '../template/entities/template.entity';+import { BaileysStoredMessage } from '../../engine/adapters/baileys-stored-message.entity';+import { LidMapping } from '../../engine/identity/lid-mapping.entity';+import { ChatState } from '../../engine/adapters/baileys-chat-state.entity';+import { PluginInstance } from '../integration/entities/plugin-instance.entity';+import { ConversationMapping } from '../integration/entities/conversation-mapping.entity';+import { IngressEvent } from '../integration/entities/ingress-event.entity';+import { WebhookDeliveryFailure } from '../webhook/entities/webhook-delivery-failure.entity';+import { WebhookOutboxEvent } from '../webhook/entities/webhook-outbox-event.entity';+import { IntegrationDeliveryFailure } from '../integration/entities/integration-delivery-failure.entity';+import { StatusUpdate } from '../status-store/entities/status-update.entity';+import { SendIdempotencyKey } from '../message/entities/send-idempotency-key.entity';+import { AutomationRule } from '../automation/entities/automation-rule.entity';+import { postgresUtcExtra } from '../../database/postgres-utc';++// StorageService (imported transitively by the infra controllers) pulls in `archiver`, which is+// ESM-only and cannot be parsed by ts-jest; the import never touches it.+jest.mock('archiver', () => ({ default: jest.fn() }));++/**+ * The replace-all import trims send_idempotency_keys with a statement that compares the claim's+ * varchar sessionId with sessions.id, a uuid on PostgreSQL. The comparison only holds because the+ * statement casts, and the cast is invisible on SQLite, where both are text: without it every import+ * on PostgreSQL answers imported:false with a warning about idempotency claims.+ */+const describePostgres = process.env.DATABASE_TYPE === 'postgres' ? describe : describe.skip;++describePostgres('InfraDataController.importData (postgres)', () => {+ const schema = `infra_import_${process.pid}_${randomBytes(4).toString('hex')}`;+ let admin: DataSource;+ let ds: DataSource;+ let controller: InfraDataController;++ beforeAll(async () => {+ const options = {+ 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',+ };+ admin = await new DataSource(options).initialize();+ await admin.query(`CREATE SCHEMA "${schema}"`);+ ds = await new DataSource({+ ...options,+ schema,+ extra: { ...postgresUtcExtra(), options: `-c search_path=${schema},public` },+ // The full data-connection entity set: exportData validates its table registry against it.+ entities: [+ Session,+ Webhook,+ Message,+ MessageBatch,+ Template,+ BaileysStoredMessage,+ LidMapping,+ ChatState,+ PluginInstance,+ ConversationMapping,+ IngressEvent,+ WebhookDeliveryFailure,+ WebhookOutboxEvent,+ IntegrationDeliveryFailure,+ StatusUpdate,+ AutomationRule,+ SendIdempotencyKey,+ ],+ synchronize: true,+ }).initialize();+ const cfg = { get: (key: string, def?: unknown) => (key === 'dataDatabase.type' ? 'postgres' : def) };+ controller = new InfraDataController(new InfraDataService(cfg as never, ds, undefined, undefined, undefined));+ });++ afterAll(async () => {+ await ds?.destroy();+ if (admin?.isInitialized) {+ await admin.query(`DROP SCHEMA IF EXISTS "${schema}" CASCADE`);+ await admin.destroy();+ }+ });++ it('drops the claims of a session the restore removes and keeps those of one it restores', async () => {+ const sessions = ds.getRepository(Session);+ const claims = ds.getRepository(SendIdempotencyKey);+ const [kept, removed] = [randomUUID(), randomUUID()];+ for (const id of [kept, removed]) {+ await sessions.save(+ sessions.create({+ id,+ name: `session-${id.slice(0, 8)}`,+ status: SessionStatus.READY,+ phone: null,+ pushName: null,+ config: {},+ proxyUrl: null,+ proxyType: null,+ connectedAt: null,+ lastActiveAt: null,+ }),+ );+ await claims.save(+ claims.create({+ sessionId: id,+ idempotencyKey: 'order-42',+ route: 'send-text',+ requestHash: 'h',+ state: 'completed',+ responseBody: { messageId: `sent-by-${id}` },+ expiresAt: new Date(Date.now() + 3_600_000),+ }),+ );+ }+ const dump = await controller.exportData();+ const onlyKept = {+ tables: {+ ...dump.tables,+ sessions: (dump.tables.sessions as unknown as Array<{ id: string }>).filter(row => row.id === kept),+ },+ };++ const result = await controller.importData(onlyKept as never);++ expect(result).toMatchObject({ imported: true });+ expect(result.warnings).toEqual([]);+ expect((await claims.find()).map(claim => claim.sessionId)).toEqual([kept]);+ });+});