From 205a01b7da07fff2cad9db904910818898d6d102 Mon Sep 17 00:00:00 2001 From: Igor Savin Date: Mon, 20 Nov 2023 23:31:14 +0200 Subject: [PATCH] Split test suites --- ...rmissionsConsumerMonoSchema.errors.spec.ts | 84 +++++++++++++++++++ .../SqsPermissionsConsumerMonoSchema.spec.ts | 36 -------- 2 files changed, 84 insertions(+), 36 deletions(-) create mode 100644 packages/sqs/test/consumers/SqsPermissionsConsumerMonoSchema.errors.spec.ts diff --git a/packages/sqs/test/consumers/SqsPermissionsConsumerMonoSchema.errors.spec.ts b/packages/sqs/test/consumers/SqsPermissionsConsumerMonoSchema.errors.spec.ts new file mode 100644 index 00000000..04964de3 --- /dev/null +++ b/packages/sqs/test/consumers/SqsPermissionsConsumerMonoSchema.errors.spec.ts @@ -0,0 +1,84 @@ +import type { SQSClient } from '@aws-sdk/client-sqs' +import { ReceiveMessageCommand } from '@aws-sdk/client-sqs' +import { waitAndRetry } from '@message-queue-toolkit/core' +import type { AwilixContainer } from 'awilix' +import { asClass } from 'awilix' +import { describe, beforeEach, afterEach, expect, it } from 'vitest' +import z from 'zod' + +import { FakeConsumerErrorResolver } from '../fakes/FakeConsumerErrorResolver' +import type { SqsPermissionPublisherMonoSchema } from '../publishers/SqsPermissionPublisherMonoSchema' +import { userPermissionMap } from '../repositories/PermissionRepository' +import { registerDependencies, SINGLETON_CONFIG } from '../utils/testContext' +import type { Dependencies } from '../utils/testContext' + +const perms: [string, ...string[]] = ['perm1', 'perm2'] + +describe('SqsPermissionsConsumerMonoSchema', () => { + describe('error handling', () => { + let diContainer: AwilixContainer + let publisher: SqsPermissionPublisherMonoSchema + let sqsClient: SQSClient + + beforeEach(async () => { + diContainer = await registerDependencies({ + consumerErrorResolver: asClass(FakeConsumerErrorResolver, SINGLETON_CONFIG), + }) + sqsClient = diContainer.cradle.sqsClient + publisher = diContainer.cradle.permissionPublisher + + delete userPermissionMap[100] + delete userPermissionMap[200] + delete userPermissionMap[300] + + const command = new ReceiveMessageCommand({ + QueueUrl: diContainer.cradle.permissionPublisher.queueUrl, + }) + const reply = await sqsClient.send(command) + expect(reply.Messages!.length).toBe(0) + + const fakeErrorResolver = diContainer.cradle + .consumerErrorResolver as FakeConsumerErrorResolver + fakeErrorResolver.clear() + }) + + afterEach(async () => { + const { awilixManager } = diContainer.cradle + await awilixManager.executeDispose() + await diContainer.dispose() + }) + + it('Invalid message in the queue', async () => { + const { consumerErrorResolver } = diContainer.cradle + + // @ts-ignore + publisher['messageSchema'] = z.any() + await publisher.publish({ + messageType: 'add', + permissions: perms, + } as any) + + const fakeResolver = consumerErrorResolver as FakeConsumerErrorResolver + await waitAndRetry(() => { + return fakeResolver.handleErrorCallsCount > 0 + }) + + expect(fakeResolver.handleErrorCallsCount).toBe(1) + }) + + it('Non-JSON message in the queue', async () => { + const { consumerErrorResolver } = diContainer.cradle + + // @ts-ignore + publisher['messageSchema'] = z.any() + await publisher.publish('dummy' as any) + + const fakeResolver = consumerErrorResolver as FakeConsumerErrorResolver + const errorCount = await waitAndRetry(() => { + return fakeResolver.handleErrorCallsCount + }) + + expect(errorCount).toBe(1) + }) + }) +}) diff --git a/packages/sqs/test/consumers/SqsPermissionsConsumerMonoSchema.spec.ts b/packages/sqs/test/consumers/SqsPermissionsConsumerMonoSchema.spec.ts index 3df83c62..948a7f04 100644 --- a/packages/sqs/test/consumers/SqsPermissionsConsumerMonoSchema.spec.ts +++ b/packages/sqs/test/consumers/SqsPermissionsConsumerMonoSchema.spec.ts @@ -4,7 +4,6 @@ import { waitAndRetry } from '@message-queue-toolkit/core' import type { AwilixContainer } from 'awilix' import { asClass } from 'awilix' import { describe, beforeEach, afterEach, expect, it, beforeAll } from 'vitest' -import z from 'zod' import { assertQueue, deleteQueue } from '../../lib/utils/sqsUtils' import { FakeConsumerErrorResolver } from '../fakes/FakeConsumerErrorResolver' @@ -193,40 +192,5 @@ describe('SqsPermissionsConsumerMonoSchema', () => { expect(usersPermissions[0]).toHaveLength(2) }) }) - - describe('error handling', () => { - it('Invalid message in the queue', async () => { - const { consumerErrorResolver } = diContainer.cradle - - // @ts-ignore - publisher['messageSchema'] = z.any() - await publisher.publish({ - messageType: 'add', - permissions: perms, - } as any) - - const fakeResolver = consumerErrorResolver as FakeConsumerErrorResolver - await waitAndRetry(() => { - return fakeResolver.handleErrorCallsCount > 0 - }) - - expect(fakeResolver.handleErrorCallsCount).toBe(1) - }) - - it('Non-JSON message in the queue', async () => { - const { consumerErrorResolver } = diContainer.cradle - - // @ts-ignore - publisher['messageSchema'] = z.any() - await publisher.publish('dummy' as any) - - const fakeResolver = consumerErrorResolver as FakeConsumerErrorResolver - const errorCount = await waitAndRetry(() => { - return fakeResolver.handleErrorCallsCount - }) - - expect(errorCount).toBe(1) - }) - }) }) })