-
Notifications
You must be signed in to change notification settings - Fork 7
fix(core): preserve type through offloading + reject removed messageTypeField #429
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
Open
kibertoad
wants to merge
3
commits into
main
Choose a base branch
from
types/reject-removed-message-type-field
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+263
−7
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
160 changes: 160 additions & 0 deletions
160
packages/core/test/queues/AbstractQueueService.offload.spec.ts
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,160 @@ | ||
| /** | ||
| * Regression tests for `AbstractQueueService.offloadMessagePayloadIfNeeded`. | ||
| * | ||
| * Identity fields (`messageIdField`, `messageTimestampField`, `messageDeduplicationIdField`, | ||
| * `messageDeduplicationOptionsField`) all have defaulted names ('id', 'timestamp', ...) and | ||
| * are unconditionally copied to the offloaded payload when present on the source message. | ||
| * The `type` field — which downstream subscriptions rely on for routing/filtering, e.g. SNS | ||
| * `FilterPolicyScope: 'MessageBody'` — must be handled the same way: present-on-source ⇒ | ||
| * present-on-offloaded, regardless of which `messageTypeResolver` mode (or absence) is | ||
| * configured. Otherwise large messages get silently dropped by SNS subscription filters. | ||
| */ | ||
| import type { CommonLogger, ErrorReporter } from '@lokalise/node-core' | ||
| import type { Either } from '@lokalise/node-core' | ||
| import { describe, expect, it } from 'vitest' | ||
| import type { ZodSchema } from 'zod/v4' | ||
| import type { MessageInvalidFormatError, MessageValidationError } from '../../lib/errors/Errors.ts' | ||
| import type { OffloadedPayloadPointerPayload } from '../../lib/payload-store/offloadedPayloadMessageSchemas.ts' | ||
| import type { PayloadStore } from '../../lib/payload-store/payloadStoreTypes.ts' | ||
| import { | ||
| AbstractQueueService, | ||
| type ResolvedMessage, | ||
| } from '../../lib/queues/AbstractQueueService.ts' | ||
| import type { MessageTypeResolverConfig } from '../../lib/queues/MessageTypeResolver.ts' | ||
| import type { QueueDependencies } from '../../lib/types/queueOptionsTypes.ts' | ||
|
|
||
| type TestMessage = { type?: string; id: string; timestamp: string; payload: unknown } | ||
|
|
||
| class TestQueueService extends AbstractQueueService< | ||
| TestMessage, | ||
| TestMessage, | ||
| QueueDependencies, | ||
| Record<string, never> | ||
| > { | ||
| protected resolveSchema(): Either<Error, ZodSchema<TestMessage>> { | ||
| throw new Error('not used in this test') | ||
| } | ||
| protected resolveMessage(): Either< | ||
| MessageInvalidFormatError | MessageValidationError, | ||
| ResolvedMessage | ||
| > { | ||
| throw new Error('not used in this test') | ||
| } | ||
| protected resolveNextFunction(): () => void { | ||
| throw new Error('not used in this test') | ||
| } | ||
| protected processPrehandlers(): Promise<undefined> { | ||
| throw new Error('not used in this test') | ||
| } | ||
| protected preHandlerBarrier<BarrierOutput>(): Promise<{ | ||
| isPassing: boolean | ||
| output?: BarrierOutput | ||
| }> { | ||
| throw new Error('not used in this test') | ||
| } | ||
| processMessage(): Promise<Either<'retryLater', 'success'>> { | ||
| throw new Error('not used in this test') | ||
| } | ||
| public close(): Promise<unknown> { | ||
| return Promise.resolve() | ||
| } | ||
|
|
||
| // Expose protected method for direct testing. | ||
| public callOffload(message: TestMessage, sizeFn: () => number) { | ||
| return this.offloadMessagePayloadIfNeeded(message, sizeFn) | ||
| } | ||
| } | ||
|
|
||
| const noopLogger: CommonLogger = { | ||
| level: 'silent', | ||
| fatal: () => undefined, | ||
| error: () => undefined, | ||
| warn: () => undefined, | ||
| info: () => undefined, | ||
| debug: () => undefined, | ||
| trace: () => undefined, | ||
| silent: () => undefined, | ||
| child: () => noopLogger, | ||
| } as unknown as CommonLogger | ||
|
|
||
| const noopReporter: ErrorReporter = { report: () => undefined } | ||
|
|
||
| const recordingStore = (storedKey: string): PayloadStore => ({ | ||
| storePayload: () => Promise.resolve(storedKey), | ||
| retrievePayload: () => Promise.resolve(null), | ||
| }) | ||
|
|
||
| const buildService = (messageTypeResolver?: MessageTypeResolverConfig) => | ||
| new TestQueueService( | ||
| { errorReporter: noopReporter, logger: noopLogger }, | ||
| { | ||
| messageTypeResolver, | ||
| // any threshold; we'll always force size > threshold via sizeFn | ||
| payloadStoreConfig: { | ||
| messageSizeThreshold: 1, | ||
| store: recordingStore('payload-id-1'), | ||
| storeName: 's3', | ||
| }, | ||
| }, | ||
| ) | ||
|
|
||
| const baseMessage: TestMessage = { | ||
| id: 'msg-1', | ||
| timestamp: '2026-01-01T00:00:00.000Z', | ||
| type: 'order.created', | ||
| payload: { large: 'data' }, | ||
| } | ||
|
|
||
| describe('AbstractQueueService.offloadMessagePayloadIfNeeded — `type` preservation', () => { | ||
| it('preserves `type` when no messageTypeResolver is configured', async () => { | ||
| const svc = buildService(undefined) | ||
| const result = (await svc.callOffload(baseMessage, () => 9999)) as OffloadedPayloadPointerPayload | ||
| expect((result as unknown as TestMessage).type).toBe('order.created') | ||
| expect(result.payloadRef?.id).toBe('payload-id-1') | ||
| expect(result.id).toBe('msg-1') | ||
| expect(result.timestamp).toBe('2026-01-01T00:00:00.000Z') | ||
| }) | ||
|
|
||
| it('preserves `type` when messageTypeResolver is `literal` mode', async () => { | ||
| const svc = buildService({ literal: 'order.created' }) | ||
| const result = (await svc.callOffload(baseMessage, () => 9999)) as OffloadedPayloadPointerPayload | ||
| expect((result as unknown as TestMessage).type).toBe('order.created') | ||
| }) | ||
|
|
||
| it('preserves `type` when messageTypeResolver is custom `resolver` mode', async () => { | ||
| const svc = buildService({ resolver: () => 'order.created' }) | ||
| const result = (await svc.callOffload(baseMessage, () => 9999)) as OffloadedPayloadPointerPayload | ||
| expect((result as unknown as TestMessage).type).toBe('order.created') | ||
| }) | ||
|
|
||
| it('preserves `type` at the configured path when messageTypeResolver is `messageTypePath`', async () => { | ||
| const svc = buildService({ messageTypePath: 'type' }) | ||
| const result = (await svc.callOffload(baseMessage, () => 9999)) as OffloadedPayloadPointerPayload | ||
| expect((result as unknown as TestMessage).type).toBe('order.created') | ||
| }) | ||
|
|
||
| it('preserves `type` at a non-default nested path when configured via `messageTypePath`', async () => { | ||
| const svc = buildService({ messageTypePath: 'metadata.eventName' }) | ||
| const message = { | ||
| id: 'msg-2', | ||
| timestamp: '2026-01-01T00:00:00.000Z', | ||
| payload: 'p', | ||
| metadata: { eventName: 'shipment.dispatched' }, | ||
| } as unknown as TestMessage | ||
| const result = (await svc.callOffload(message, () => 9999)) as OffloadedPayloadPointerPayload | ||
| expect((result as unknown as { metadata: { eventName: string } }).metadata.eventName).toBe( | ||
| 'shipment.dispatched', | ||
| ) | ||
| }) | ||
|
|
||
| it('does not invent a `type` when the source message has none', async () => { | ||
| const svc = buildService(undefined) | ||
| const message: TestMessage = { | ||
| id: 'msg-3', | ||
| timestamp: '2026-01-01T00:00:00.000Z', | ||
| payload: 'p', | ||
| } | ||
| const result = (await svc.callOffload(message, () => 9999)) as OffloadedPayloadPointerPayload | ||
| expect((result as unknown as TestMessage).type).toBeUndefined() | ||
| }) | ||
| }) |
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,29 @@ | ||
| /** | ||
| * Type-level tests for `CommonQueueOptions`. These guard against silent removals of legacy | ||
| * options. The lib historically exposed `messageTypeField`, removed in core 25.x in favor | ||
| * of `messageTypeResolver`. Callers that still pass the legacy option must get a compile-time | ||
| * error rather than silently broken runtime behavior (for example: payload offloading drops | ||
| * the message `type` field, then SNS subscriptions with `FilterPolicyScope: 'MessageBody'` | ||
| * filter on `type` fail to deliver to SQS). | ||
| * | ||
| * Run with: npx tsc --noEmit | ||
| */ | ||
| import { describe, it } from 'vitest' | ||
| import type { CommonQueueOptions } from '../../lib/types/queueOptionsTypes.ts' | ||
|
|
||
| describe('CommonQueueOptions — legacy `messageTypeField`', () => { | ||
| it('rejects passing the removed `messageTypeField` option', () => { | ||
| const _bad: CommonQueueOptions = { | ||
| // @ts-expect-error - `messageTypeField` was removed in core 25.x; use `messageTypeResolver` instead | ||
| messageTypeField: 'type', | ||
| } | ||
| }) | ||
|
|
||
| it('accepts the supported `messageTypeResolver` shape', () => { | ||
| const _ok1: CommonQueueOptions = { messageTypeResolver: { messageTypePath: 'type' } } | ||
| const _ok2: CommonQueueOptions = { messageTypeResolver: { literal: 'order.created' } } | ||
| const _ok3: CommonQueueOptions = { | ||
| messageTypeResolver: { resolver: () => 'order.created' }, | ||
| } | ||
| }) | ||
| }) |
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,44 @@ | ||
| /** | ||
| * Type-level guard for `SnsPublisherManager`. Reproduces the call shape that file-storage-service | ||
| * was using when payload offloading silently broke after the core 24.x → 25.x bump: | ||
| * | ||
| * ```ts | ||
| * new SnsPublisherManager(deps, { | ||
| * ... | ||
| * newPublisherOptions: { | ||
| * messageTypeField: 'type', // <- removed in core 25.x; ignored at runtime, dropped `type` from offloaded SNS body | ||
| * ... | ||
| * }, | ||
| * }) | ||
| * ``` | ||
| * | ||
| * This must now fail at compile time. | ||
| * | ||
| * Run with: npx tsc --noEmit | ||
| */ | ||
| import { describe, it } from 'vitest' | ||
| import type { SNSPublisherOptions } from '../lib/sns/AbstractSnsPublisher.ts' | ||
|
|
||
| type AnyMessage = { type: string } | ||
|
|
||
| type NewPublisherOptions = Omit< | ||
| SNSPublisherOptions<AnyMessage>, | ||
| 'messageSchemas' | 'creationConfig' | 'locatorConfig' | ||
| > | ||
|
|
||
| describe('SnsPublisherManager `newPublisherOptions` — legacy `messageTypeField`', () => { | ||
| it('rejects the removed `messageTypeField` option', () => { | ||
| const _bad: NewPublisherOptions = { | ||
| // @ts-expect-error - `messageTypeField` was removed in core 25.x; use `messageTypeResolver` instead | ||
| messageTypeField: 'type', | ||
| messageIdField: 'id', | ||
| } | ||
| }) | ||
|
|
||
| it('accepts `messageTypeResolver`', () => { | ||
| const _ok: NewPublisherOptions = { | ||
| messageTypeResolver: { messageTypePath: 'type' }, | ||
| messageIdField: 'id', | ||
| } | ||
| }) | ||
| }) | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use the expect type of option from vitest?