fix: use type-only Baileys import and raw status code to fix Jest ESM issue

Replaces DisconnectReason enum import with type-only WASocket import and
uses 401 directly instead of DisconnectReason.loggedOut. Baileys is an ES
module that cannot be executed in Jest's CommonJS mode, so removing the
value import (keeping only type imports) prevents ts-jest from trying to
execute the module.

Also updated session-pool.test.ts to verify end() is called with the
expected Boom error object instead of undefined.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-27 17:40:24 +05:30
parent 06449acd96
commit d33b4e40b8
9 changed files with 4283 additions and 6 deletions
+75
View File
@@ -0,0 +1,75 @@
{
"permissions": {
"allow": [
"Bash(git init *)",
"Bash(git branch *)",
"Bash(pnpm install *)",
"Bash(git add *)",
"Bash(git commit *)",
"Bash(pnpm --filter @tower/types install)",
"Bash(pnpm --filter @tower/types build)",
"Bash(pnpm --filter @tower/config install)",
"Bash(pnpm --filter @tower/config test)",
"Bash(pnpm --filter @tower/config build)",
"Bash(pnpm --filter @tower/logger install)",
"Bash(pnpm --filter @tower/logger build)",
"Bash(pnpm --filter @tower/api install)",
"Bash(docker compose *)",
"Bash(DATABASE_URL=\"postgresql://tower:tower_dev@localhost:5432/tower_dev\" pnpm exec prisma db push)",
"Bash(DATABASE_URL=\"postgresql://tower:tower_dev@localhost:5433/tower_dev\" pnpm exec prisma migrate dev --name init_core_schema)",
"Bash(DATABASE_URL=\"postgresql://tower:tower_dev@localhost:5433/tower_dev\" pnpm test)",
"Bash(git -C /Users/maaz/Documents/insignia-work/tower add apps/api/src/prisma/prisma.service.spec.ts apps/api/prisma/migrations docker-compose.yml .env.example pnpm-lock.yaml)",
"Bash(git -C /Users/maaz/Documents/insignia-work/tower commit -m \"feat: add Prisma schema and PrismaService with integration tests \\(postgres on :5433\\)\")",
"Bash(DATABASE_URL=\"postgresql://tower:tower_dev@localhost:5433/tower_dev\" pnpm dev)",
"Bash(curl -s http://localhost:3001/health)",
"Bash(kill %1)",
"Bash(git -C /Users/maaz/Documents/insignia-work/tower add apps/api/src/modules/health/health.controller.spec.ts)",
"Bash(git -C /Users/maaz/Documents/insignia-work/tower commit -m \"feat: add health check module with unit tests\")",
"Bash(pnpm dlx *)",
"Bash(pnpm info *)",
"Bash(pnpm --filter @tower/web install)",
"Bash(pnpm --filter @tower/web test)",
"Bash(pnpm --filter @tower/web build)",
"Bash(pnpm --filter @tower/worker test)",
"Bash(pnpm --filter @tower/worker add @whiskeysockets/baileys)",
"Bash(git -C /Users/maaz/Documents/insignia-work/tower log --oneline -5)",
"Bash(pnpm ls *)",
"Bash(pnpm list *)",
"Bash(git -C /Users/maaz/Documents/insignia-work/tower add apps/worker/src/queues/)",
"Bash(git -C /Users/maaz/Documents/insignia-work/tower commit -m 'feat: add BullMQ ingest queue and processor *)",
"Bash(git -C /Users/maaz/Documents/insignia-work/tower commit -m ' *)",
"Bash(pnpm --filter @tower/worker build)",
"Bash(pnpm --filter @tower/worker add @hapi/boom)",
"Bash(git -C /Users/maaz/Documents/insignia-work/tower add apps/worker/src/whatsapp/session.ts apps/worker/package.json pnpm-lock.yaml)",
"Bash(pnpm --filter @tower/worker add @prisma/client)",
"Bash(pnpm --filter @tower/worker add --save-dev prisma)",
"Bash(pnpm --filter @tower/worker add --save-dev dotenv)",
"Bash(pnpm --filter @tower/worker generate)",
"Bash(pnpm build *)",
"Bash(pnpm test *)",
"Bash(git -C /Users/maaz/Documents/insignia-work/tower add apps/worker/src/queues/ingest.queue.ts apps/worker/src/queues/ingest.processor.ts)",
"Bash(git -C /Users/maaz/Documents/insignia-work/tower commit -m 'fix: rename queue from tower:ingest to tower-ingest \\(BullMQ v5 forbids colons\\) *)",
"Bash(pnpm --filter @tower/worker add qrcode-terminal)",
"Bash(pnpm --filter @tower/worker add -D @types/qrcode-terminal)",
"Bash(pnpm --filter @tower/worker test -- --testPathPattern=normalizer)",
"Bash(pnpm --filter @tower/worker test --testPathPattern=normalizer)",
"Bash(pnpm --filter @tower/worker test -- --testPathPattern normalizer)",
"Bash(xargs cat)",
"Bash(pnpm --filter @tower/worker test -- --testPathPattern 'normalizer')",
"Bash(DATABASE_URL=\"postgresql://tower:tower@localhost:5433/tower\" npx prisma migrate dev --name add-account-model)",
"Bash('../../../DATABASE_URL'=\"postgresql://tower:tower@localhost:5433/tower\" npx *)",
"Bash(psql \"postgresql://tower:tower_dev@localhost:5433/tower_dev\" -c '\\\\l')",
"Bash(env '../../../DATABASE_URL=postgresql://tower:tower_dev@localhost:5433/tower_dev' npx prisma migrate dev --name add-account-model)",
"Bash(npm run *)",
"Bash(npx tsc *)",
"Bash(pnpm --filter @tower/worker test -- --testPathPattern=session-pool)",
"Bash(pnpm --filter @tower/worker test session-pool)",
"Bash(pnpm --filter @tower/worker test -- --testPathPattern=approval)",
"Bash(pnpm --filter @tower/worker test approval.test.ts)",
"Bash(npx jest *)",
"Bash(pnpm --filter @tower/worker test -- --testPathPattern=forward)",
"Bash(pnpm --filter @tower/worker test -- forward.processor.test.ts)",
"Bash(npm test *)"
]
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
import "./.next/types/routes.d.ts";
import "./.next/dev/types/routes.d.ts";
// NOTE: This file should not be edited
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
+3 -1
View File
@@ -16,11 +16,13 @@
"@tower/types": "workspace:*",
"@whiskeysockets/baileys": "7.0.0-rc13",
"bullmq": "^5.0.0",
"ioredis": "^5.0.0"
"ioredis": "^5.0.0",
"qrcode-terminal": "^0.12.0"
},
"devDependencies": {
"@types/jest": "^29.0.0",
"@types/node": "^22.0.0",
"@types/qrcode-terminal": "^0.12.2",
"dotenv": "^17.4.2",
"jest": "^29.0.0",
"prisma": "^6.0.0",
@@ -66,8 +66,8 @@ describe('WhatsAppSessionPool', () => {
await pool.closeAll();
expect(sock1.end).toHaveBeenCalledWith(undefined);
expect(sock2.end).toHaveBeenCalledWith(undefined);
expect(sock1.end).toHaveBeenCalledWith(expect.objectContaining({ message: 'Shutdown' }));
expect(sock2.end).toHaveBeenCalledWith(expect.objectContaining({ message: 'Shutdown' }));
expect(pool.getAll().size).toBe(0);
});
+2 -2
View File
@@ -1,4 +1,4 @@
import { WASocket, DisconnectReason } from '@whiskeysockets/baileys';
import type { WASocket } from '@whiskeysockets/baileys';
import { Boom } from '@hapi/boom';
import { NormalizedMessage, NormalizedReaction } from '@tower/types';
import { createWhatsAppSession } from './session';
@@ -67,7 +67,7 @@ export class WhatsAppSessionPool {
logger.info({ count: this.sessions.size }, 'Closing all WhatsApp sessions');
for (const [accountId, sock] of this.sessions) {
try {
sock.end(new Boom('Shutdown', { statusCode: DisconnectReason.loggedOut }));
sock.end(new Boom('Shutdown', { statusCode: 401 }));
logger.info({ accountId }, 'Session closed');
} catch (err) {
logger.error({ accountId, err }, 'Error closing session');
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+17
View File
@@ -166,6 +166,9 @@ importers:
ioredis:
specifier: ^5.0.0
version: 5.11.0
qrcode-terminal:
specifier: ^0.12.0
version: 0.12.0
devDependencies:
'@types/jest':
specifier: ^29.0.0
@@ -173,6 +176,9 @@ importers:
'@types/node':
specifier: ^22.0.0
version: 22.19.19
'@types/qrcode-terminal':
specifier: ^0.12.2
version: 0.12.2
dotenv:
specifier: ^17.4.2
version: 17.4.2
@@ -1332,6 +1338,9 @@ packages:
'@types/node@22.19.19':
resolution: {integrity: sha512-dyh/xO2Fh5bYrfWaaqGrRQQGkNdmYw6AmaAUvYeUMNTWQtvb796ikLdmTchRmOlOiIJ1TDXfWgVx1QkUlQ6Hew==}
'@types/qrcode-terminal@0.12.2':
resolution: {integrity: sha512-v+RcIEJ+Uhd6ygSQ0u5YYY7ZM+la7GgPbs0V/7l/kFs2uO4S8BcIUEMoP7za4DNIqNnUD5npf0A/7kBhrCKG5Q==}
'@types/react-dom@19.2.3':
resolution: {integrity: sha512-jp2L/eY6fn+KgVVQAOqYItbF0VY/YApe5Mz2F0aykSO8gx31bYCZyvSeYxCHKvzHG5eZjc+zyaS5BrBWya2+kQ==}
peerDependencies:
@@ -3105,6 +3114,10 @@ packages:
resolution: {integrity: sha512-+Owyggi9IxT1ePKGafcI87ubSmxol6smwJ+RAHDQlx9+9cPwFWDiKFFCPuWhr9ignlGpZ9vDQLw67N4dcTVFEA==}
engines: {node: '>=20'}
qrcode-terminal@0.12.0:
resolution: {integrity: sha512-EXtzRZmC+YGmGlDFbXKxQiMZNwCLEO6BANKXG4iCtSIM0yqc/pappSx3RIKr4r0uh5JsBckOXeKrB3Iz7mdQpQ==}
hasBin: true
qs@6.15.2:
resolution: {integrity: sha512-Rzq0KEyX/w/tEybncDgdkZrJgVUsUMk3xjh3t5bv3S1HTAtg+uOYt72+ZfwiQwKdysThkTBdL/rTi6HDmX9Ddw==}
engines: {node: '>=0.6'}
@@ -4934,6 +4947,8 @@ snapshots:
dependencies:
undici-types: 6.21.0
'@types/qrcode-terminal@0.12.2': {}
'@types/react-dom@19.2.3(@types/react@19.2.15)':
dependencies:
'@types/react': 19.2.15
@@ -6948,6 +6963,8 @@ snapshots:
dependencies:
hookified: 2.2.0
qrcode-terminal@0.12.0: {}
qs@6.15.2:
dependencies:
side-channel: 1.1.0