-
-
Notifications
You must be signed in to change notification settings - Fork 282
fix: no mutation for caller owned payloads #996
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| 'use strict' | ||
|
|
||
| type EventModule = typeof import('../internal/queue/event') | ||
| type QueueModule = typeof import('../internal/queue/queue') | ||
|
|
||
| type TestPayload = { | ||
| name: string | ||
| tenant: { | ||
| ref: string | ||
| host: string | ||
| } | ||
| reqId?: string | ||
| scheduleAt?: Date | ||
| } | ||
|
|
||
| async function loadQueueModules(opts?: { pgQueueEnable?: boolean }) { | ||
| jest.resetModules() | ||
|
|
||
| const configModule = await import('../config') | ||
| configModule.getConfig({ reload: true }) | ||
| configModule.mergeConfig({ | ||
| pgQueueEnable: opts?.pgQueueEnable ?? false, | ||
| }) | ||
|
|
||
| const eventModule = (await import('../internal/queue/event')) as EventModule | ||
| const queueModule = (await import('../internal/queue/queue')) as QueueModule | ||
|
|
||
| return { eventModule, queueModule } | ||
| } | ||
|
|
||
| function defineTestEvent(EventBase: EventModule['Event']) { | ||
| return class TestEvent extends EventBase<TestPayload> { | ||
| static readonly version = 'v-test' | ||
| protected static queueName = 'test-event' | ||
| } | ||
| } | ||
|
|
||
| function createPayload(overrides: Partial<TestPayload> = {}): TestPayload { | ||
| return { | ||
| name: 'test-object', | ||
| tenant: { | ||
| ref: 'test-tenant', | ||
| host: 'localhost', | ||
| }, | ||
| reqId: 'req-123', | ||
| ...overrides, | ||
| } | ||
| } | ||
|
|
||
| describe('Event payload versioning', () => { | ||
| afterEach(() => { | ||
| jest.restoreAllMocks() | ||
| jest.resetModules() | ||
| }) | ||
|
|
||
| it('does not mutate payloads passed to static send', async () => { | ||
| const { eventModule } = await loadQueueModules() | ||
| const TestEvent = defineTestEvent(eventModule.Event) | ||
| const payload = createPayload() | ||
|
|
||
| jest.spyOn(TestEvent.prototype, 'send').mockImplementation(function ( | ||
| this: InstanceType<typeof TestEvent> | ||
| ) { | ||
| expect(this.payload.$version).toBe('v-test') | ||
| return Promise.resolve('queued') | ||
| }) | ||
|
|
||
| await TestEvent.send(payload) | ||
|
|
||
| expect(payload).toEqual(createPayload()) | ||
| expect(payload).not.toHaveProperty('$version') | ||
| }) | ||
|
|
||
| it('does not mutate payloads passed to static invoke', async () => { | ||
| const { eventModule } = await loadQueueModules() | ||
| const TestEvent = defineTestEvent(eventModule.Event) | ||
| const payload = createPayload() | ||
|
|
||
| jest.spyOn(TestEvent.prototype, 'invoke').mockImplementation(function ( | ||
| this: InstanceType<typeof TestEvent> | ||
| ) { | ||
| expect(this.payload.$version).toBe('v-test') | ||
| return Promise.resolve(null) | ||
| }) | ||
|
|
||
| await TestEvent.invoke(payload) | ||
|
|
||
| expect(payload).toEqual(createPayload()) | ||
| expect(payload).not.toHaveProperty('$version') | ||
| }) | ||
|
|
||
| it('does not mutate payloads passed to static invokeOrSend', async () => { | ||
| const { eventModule } = await loadQueueModules() | ||
| const TestEvent = defineTestEvent(eventModule.Event) | ||
| const payload = createPayload() | ||
|
|
||
| jest.spyOn(TestEvent.prototype, 'invokeOrSend').mockImplementation(function ( | ||
| this: InstanceType<typeof TestEvent> | ||
| ) { | ||
| expect(this.payload.$version).toBe('v-test') | ||
| return Promise.resolve(null) | ||
| }) | ||
|
|
||
| await TestEvent.invokeOrSend(payload) | ||
|
|
||
| expect(payload).toEqual(createPayload()) | ||
| expect(payload).not.toHaveProperty('$version') | ||
| }) | ||
|
|
||
| it('does not mutate message payloads when batching', async () => { | ||
| const { eventModule, queueModule } = await loadQueueModules({ pgQueueEnable: true }) | ||
| const TestEvent = defineTestEvent(eventModule.Event) | ||
| const payload = createPayload({ scheduleAt: new Date('2026-04-07T10:00:00.000Z') }) | ||
| const insert = jest.fn().mockResolvedValue('job-id') | ||
|
|
||
| jest | ||
| .spyOn(queueModule.Queue, 'getInstance') | ||
| .mockReturnValue({ insert } as unknown as ReturnType<typeof queueModule.Queue.getInstance>) | ||
|
|
||
| const message = new TestEvent(payload) | ||
|
|
||
| await TestEvent.batchSend([message]) | ||
|
|
||
| expect(payload).toEqual(createPayload({ scheduleAt: new Date('2026-04-07T10:00:00.000Z') })) | ||
| expect(payload).not.toHaveProperty('$version') | ||
| expect(insert).toHaveBeenCalledWith([ | ||
| expect.objectContaining({ | ||
| name: 'test-event', | ||
| deadLetter: 'test-event-dead-letter', | ||
| data: expect.objectContaining({ | ||
| $version: 'v-test', | ||
| name: 'test-object', | ||
| tenant: expect.objectContaining({ | ||
| ref: 'test-tenant', | ||
| }), | ||
| }), | ||
| startAfter: new Date('2026-04-07T10:00:00.000Z'), | ||
| }), | ||
| ]) | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.