diff --git a/.changeset/quiet-papayas-greet.md b/.changeset/quiet-papayas-greet.md new file mode 100644 index 00000000..c0e00ffd --- /dev/null +++ b/.changeset/quiet-papayas-greet.md @@ -0,0 +1,5 @@ +--- +"@sebspark/pubsub": patch +--- + +Fixed bug that broke topic.initiate, leading to subscription creation failing. diff --git a/packages/pubsub/src/lib/subscriber.spec.ts b/packages/pubsub/src/lib/subscriber.spec.ts index 1c52b066..153b6f1b 100644 --- a/packages/pubsub/src/lib/subscriber.spec.ts +++ b/packages/pubsub/src/lib/subscriber.spec.ts @@ -1,6 +1,7 @@ +import { beforeEach } from 'node:test' import { PubSub, type Subscription, type Topic } from '@google-cloud/pubsub' import type { Schema } from 'avsc' -import { type MockedObject, afterAll, describe, expect, it, vi } from 'vitest' +import { type MockedObject, beforeAll, describe, expect, it, vi } from 'vitest' import { createSubscriber } from './subscriber' type ExampleMessage = { @@ -8,11 +9,6 @@ type ExampleMessage = { message: string } -const message = { - messageType: 'type of message', - message: 'message data', -} satisfies ExampleMessage - type ExamplePubsubChannels = { example: ExampleMessage } @@ -77,25 +73,62 @@ describe('subscriber', () => { const topicName = 'example' const subscriptionName = 'example-subscription' - it('uses an existing subscription if it exists', async () => { - const topicMock = new PubSub().topic(topicName) as MockedObject - const subscriptionMock = topicMock.subscription( + let topicMock: MockedObject + let subscriptionMock: MockedObject + + beforeAll(() => { + topicMock = new PubSub().topic(topicName) as MockedObject + subscriptionMock = topicMock.subscription( subscriptionName ) as MockedObject + }) + + beforeEach(() => {}) - subscriptionMock.exists.mockImplementation(() => [true]) + describe('subscribe', () => { + it('uses an existing subscription if it exists', async () => { + const subscriber = createSubscriber({ + projectId: 'test', + }) - const subscriber = createSubscriber({ - projectId: 'test', + topicMock.createSubscription.mockClear() + + await subscriber.topic('example').subscribe('existing-subscription', { + onMessage: () => Promise.resolve(), + }) + + expect(topicMock.subscription).toHaveBeenCalled() + expect(topicMock.createSubscription.mock.calls.length).toBe(0) }) + }) + + describe('initiate', () => { + it('does not create a subscription if it exists', async () => { + subscriptionMock.exists.mockImplementationOnce(() => [true]) - topicMock.createSubscription.mockClear() + const subscriber = createSubscriber({ + projectId: 'test', + }) - await subscriber.topic('example').subscribe('existing-subscription', { - onMessage: () => Promise.resolve(), + topicMock.createSubscription.mockClear() + + await subscriber.topic('example').initiate('existing-subscription') + + expect(topicMock.createSubscription.mock.calls.length).toBe(0) }) - expect(topicMock.subscription).toHaveBeenCalled() - expect(topicMock.createSubscription.mock.calls.length).toBe(0) + it('creates a subscription if it does not exist', async () => { + subscriptionMock.exists.mockImplementationOnce(() => [false]) + + const subscriber = createSubscriber({ + projectId: 'test', + }) + + topicMock.createSubscription.mockClear() + + await subscriber.topic('example').initiate('example-subscription') + + expect(topicMock.createSubscription).toHaveBeenCalled() + }) }) }) diff --git a/packages/pubsub/src/lib/subscriber.ts b/packages/pubsub/src/lib/subscriber.ts index 0b112e4a..9255a03e 100644 --- a/packages/pubsub/src/lib/subscriber.ts +++ b/packages/pubsub/src/lib/subscriber.ts @@ -7,7 +7,7 @@ import { type Topic, } from '@google-cloud/pubsub' -const makeSureSubacriptionExists = async ( +const makeSureSubscriptionExists = async ( topic: Topic, name: string, options?: PubSubOptions @@ -68,16 +68,13 @@ export const createSubscriber = >( const typedClient: SubscriptionClient = { topic: (name) => { - let _topic: Topic + const _topic: Topic = client.topic(name as string) return { initiate: async (subscriptionName, options) => { - await makeSureSubacriptionExists(_topic, subscriptionName, options) + await makeSureSubscriptionExists(_topic, subscriptionName, options) }, subscribe: async (subscriptionName, callbacks, options) => { - if (!_topic) { - _topic = client.topic(name as string) - } const subscription = _topic.subscription(subscriptionName) subscription.on('message', async (msg) => { const data = JSON.parse(msg.data.toString('utf8'))