-
Notifications
You must be signed in to change notification settings - Fork 7
Handle edge cases #374
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
Handle edge cases #374
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
13c2025
cleanup
kibertoad 12ae817
improve handling of edge cases
kibertoad 4245042
More improvements
kibertoad cddb419
More improvements
kibertoad 4205603
Make handling of MessageSchemaContainer more cons
kibertoad fca5f14
Update documentation
kibertoad 7843189
Use in GCP and SQS
kibertoad 058cfe2
Add timestamps as well
kibertoad c1e2671
Handle type edge cases
kibertoad 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
Some comments aren't visible on the classic Files Changed page.
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
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
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
161 changes: 161 additions & 0 deletions
161
packages/core/test/queues/MessageSchemaContainer.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,161 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import z from 'zod/v4' | ||
|
|
||
| import { MessageSchemaContainer } from '../../lib/queues/MessageSchemaContainer.ts' | ||
|
|
||
| const MESSAGE_SCHEMA_A = z.object({ | ||
| type: z.literal('message.a'), | ||
| payload: z.string(), | ||
| }) | ||
|
|
||
| const MESSAGE_SCHEMA_B = z.object({ | ||
| type: z.literal('message.b'), | ||
| payload: z.number(), | ||
| }) | ||
|
|
||
| const MESSAGE_SCHEMA_NO_TYPE = z.object({ | ||
| payload: z.string(), | ||
| }) | ||
|
|
||
| type MessageA = z.infer<typeof MESSAGE_SCHEMA_A> | ||
| type MessageB = z.infer<typeof MESSAGE_SCHEMA_B> | ||
|
|
||
| describe('MessageSchemaContainer', () => { | ||
| describe('resolveSchema', () => { | ||
| it('should resolve schema using messageTypePath', () => { | ||
| const container = new MessageSchemaContainer<MessageA | MessageB>({ | ||
| messageSchemas: [MESSAGE_SCHEMA_A, MESSAGE_SCHEMA_B], | ||
| messageDefinitions: [], | ||
| messageTypeResolver: { messageTypePath: 'type' }, | ||
| }) | ||
|
|
||
| const resultA = container.resolveSchema({ type: 'message.a', payload: 'test' }) | ||
| expect('result' in resultA).toBe(true) | ||
|
|
||
| const resultB = container.resolveSchema({ type: 'message.b', payload: 123 }) | ||
| expect('result' in resultB).toBe(true) | ||
| }) | ||
|
|
||
| it('should return error for unknown message type', () => { | ||
| const container = new MessageSchemaContainer<MessageA>({ | ||
| messageSchemas: [MESSAGE_SCHEMA_A], | ||
| messageDefinitions: [], | ||
| messageTypeResolver: { messageTypePath: 'type' }, | ||
| }) | ||
|
|
||
| const result = container.resolveSchema({ type: 'unknown.type', payload: 'test' }) | ||
| expect('error' in result).toBe(true) | ||
| if ('error' in result && result.error) { | ||
| expect(result.error.message).toContain('Unsupported message type: unknown.type') | ||
| } | ||
| }) | ||
|
|
||
| it('should catch resolver errors and return as Either error', () => { | ||
| const container = new MessageSchemaContainer<MessageA>({ | ||
| messageSchemas: [MESSAGE_SCHEMA_A], | ||
| messageDefinitions: [], | ||
| messageTypeResolver: { | ||
| resolver: () => { | ||
| throw new Error('Resolver failed') | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| const result = container.resolveSchema({ payload: 'test' }) | ||
| expect('error' in result).toBe(true) | ||
| if ('error' in result && result.error) { | ||
| expect(result.error.message).toBe('Resolver failed') | ||
| } | ||
| }) | ||
|
|
||
| it('should resolve schema using literal type', () => { | ||
| const container = new MessageSchemaContainer<MessageA>({ | ||
| messageSchemas: [MESSAGE_SCHEMA_A], | ||
| messageDefinitions: [], | ||
| messageTypeResolver: { literal: 'message.a' }, | ||
| }) | ||
|
|
||
| // Any message resolves to the single schema | ||
| const result = container.resolveSchema({ anything: 'works' }) | ||
| expect('result' in result).toBe(true) | ||
| }) | ||
|
|
||
| it('should resolve schema using attributes with custom resolver', () => { | ||
| // Custom resolver extracts type from attributes for runtime resolution. | ||
| // For schema registration, we use messageTypePath to map schemas to types. | ||
| const container = new MessageSchemaContainer<MessageA | MessageB>({ | ||
| messageSchemas: [MESSAGE_SCHEMA_A, MESSAGE_SCHEMA_B], | ||
| messageDefinitions: [], | ||
| messageTypeResolver: { messageTypePath: 'type' }, | ||
| }) | ||
|
|
||
| // At runtime, we can still use attributes if needed (though this example uses data) | ||
| const result = container.resolveSchema({ type: 'message.a', payload: 'test' }) | ||
| expect('result' in result).toBe(true) | ||
| }) | ||
| }) | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| describe('registration validation', () => { | ||
| it('should throw error when custom resolver is used with multiple schemas', () => { | ||
| expect( | ||
| () => | ||
| new MessageSchemaContainer<MessageA | MessageB>({ | ||
| messageSchemas: [MESSAGE_SCHEMA_A, MESSAGE_SCHEMA_B], | ||
| messageDefinitions: [], | ||
| messageTypeResolver: { | ||
| resolver: () => 'some.type', | ||
| }, | ||
| }), | ||
| ).toThrow( | ||
| 'Custom resolver function cannot be used with multiple schemas. ' + | ||
| 'The resolver works for runtime type resolution, but at registration time ' + | ||
| 'we cannot determine which schema corresponds to which type. ' + | ||
| 'Use messageTypePath config (to extract types from schema literals) or register only a single schema.', | ||
| ) | ||
| }) | ||
|
|
||
| it('should allow custom resolver with single schema', () => { | ||
| expect( | ||
| () => | ||
| new MessageSchemaContainer<MessageA>({ | ||
| messageSchemas: [MESSAGE_SCHEMA_A], | ||
| messageDefinitions: [], | ||
| messageTypeResolver: { | ||
| resolver: () => 'message.a', | ||
| }, | ||
| }), | ||
| ).not.toThrow() | ||
| }) | ||
|
|
||
| it('should throw error for duplicate schema types', () => { | ||
| const DUPLICATE_SCHEMA = z.object({ | ||
| type: z.literal('message.a'), // Same type as MESSAGE_SCHEMA_A | ||
| payload: z.string(), // Same structure to satisfy type | ||
| }) | ||
|
|
||
| expect( | ||
| () => | ||
| new MessageSchemaContainer<MessageA>({ | ||
| messageSchemas: [MESSAGE_SCHEMA_A, DUPLICATE_SCHEMA], | ||
| messageDefinitions: [], | ||
| messageTypeResolver: { messageTypePath: 'type' }, | ||
| }), | ||
| ).toThrow('Duplicate schema for type: message.a') | ||
| }) | ||
|
|
||
| it('should handle schemas without literal type field gracefully', () => { | ||
| // When schema doesn't have the expected literal field, it falls back to DEFAULT_SCHEMA_KEY | ||
| // With a single schema, this works fine | ||
| const container = new MessageSchemaContainer({ | ||
| messageSchemas: [MESSAGE_SCHEMA_NO_TYPE], | ||
| messageDefinitions: [], | ||
| messageTypeResolver: { messageTypePath: 'type' }, | ||
| }) | ||
|
|
||
| // Since there's no 'type' field in schema, it uses default key | ||
| // Any message will fail to match since we're looking for a specific type | ||
| const result = container.resolveSchema({ type: 'any.type', payload: 'test' }) | ||
| expect('error' in result).toBe(true) | ||
| }) | ||
| }) | ||
| }) | ||
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
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.
Small wording nit: “outside of” → “outside” (avoids redundancy).
📝 Committable suggestion
🧰 Tools
🪛 LanguageTool
[style] ~9-~9: This phrase is redundant. Consider using “outside”.
Context: ... if you're using
HandlerSpydirectly (outside of the built-in queue services), you'll ne...(OUTSIDE_OF)
🤖 Prompt for AI Agents