-
Notifications
You must be signed in to change notification settings - Fork 495
fix: target URL filter #4735
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
fix: target URL filter #4735
Changes from all commits
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,234 @@ | ||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||
| buildFilterClickHouse, | ||||||||||||||||||||||||||||||
| } from '../../filters/filters'; | ||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||
| FilterLeaf, | ||||||||||||||||||||||||||||||
| } from '../../filters/filterDefs'; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| describe('Target URL Filter', () => { | ||||||||||||||||||||||||||||||
| describe('buildFilterClickHouse with target_url', () => { | ||||||||||||||||||||||||||||||
| test('should build target_url equals filter correctly', () => { | ||||||||||||||||||||||||||||||
| const filter: FilterLeaf = { | ||||||||||||||||||||||||||||||
| request_response_rmt: { | ||||||||||||||||||||||||||||||
| target_url: { equals: 'https://api.openai.com/v1/chat/completions' } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const result = buildFilterClickHouse({ filter, argsAcc: [] }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| expect(result.filter).toBe('request_response_rmt.target_url = {val_0 : String}'); | ||||||||||||||||||||||||||||||
| expect(result.argsAcc).toEqual(['https://api.openai.com/v1/chat/completions']); | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| test('should build target_url like filter correctly', () => { | ||||||||||||||||||||||||||||||
| const filter: FilterLeaf = { | ||||||||||||||||||||||||||||||
| request_response_rmt: { | ||||||||||||||||||||||||||||||
| target_url: { like: '%openai.com%' } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const result = buildFilterClickHouse({ filter, argsAcc: [] }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| expect(result.filter).toBe('request_response_rmt.target_url LIKE {val_0 : String}'); | ||||||||||||||||||||||||||||||
| expect(result.argsAcc).toEqual(['%openai.com%']); | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| test('should build target_url ilike filter correctly', () => { | ||||||||||||||||||||||||||||||
| const filter: FilterLeaf = { | ||||||||||||||||||||||||||||||
| request_response_rmt: { | ||||||||||||||||||||||||||||||
| target_url: { ilike: '%OPENAI.COM%' } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const result = buildFilterClickHouse({ filter, argsAcc: [] }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| expect(result.filter).toBe('request_response_rmt.target_url ILIKE {val_0 : String}'); | ||||||||||||||||||||||||||||||
| expect(result.argsAcc).toEqual(['%OPENAI.COM%']); | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| test('should build target_url contains filter correctly', () => { | ||||||||||||||||||||||||||||||
| const filter: FilterLeaf = { | ||||||||||||||||||||||||||||||
| request_response_rmt: { | ||||||||||||||||||||||||||||||
| target_url: { contains: 'anthropic' } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const result = buildFilterClickHouse({ filter, argsAcc: [] }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| expect(result.filter).toBe("request_response_rmt.target_url ILIKE '%' || {val_0 : String}::text || '%'"); | ||||||||||||||||||||||||||||||
| expect(result.argsAcc).toEqual(['anthropic']); | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| test('should build target_url not-equals filter correctly', () => { | ||||||||||||||||||||||||||||||
| const filter: FilterLeaf = { | ||||||||||||||||||||||||||||||
| request_response_rmt: { | ||||||||||||||||||||||||||||||
| target_url: { 'not-equals': 'https://api.openai.com/v1/embeddings' } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const result = buildFilterClickHouse({ filter, argsAcc: [] }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| expect(result.filter).toBe('request_response_rmt.target_url != {val_0 : String}'); | ||||||||||||||||||||||||||||||
| expect(result.argsAcc).toEqual(['https://api.openai.com/v1/embeddings']); | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| test('should handle target_url with null value', () => { | ||||||||||||||||||||||||||||||
| const filter: FilterLeaf = { | ||||||||||||||||||||||||||||||
| request_response_rmt: { | ||||||||||||||||||||||||||||||
| target_url: { equals: 'null' } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const result = buildFilterClickHouse({ filter, argsAcc: [] }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| expect(result.filter).toBe('request_response_rmt.target_url is null'); | ||||||||||||||||||||||||||||||
| expect(result.argsAcc).toEqual([]); | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| test('should work with target_url alongside other basic fields', () => { | ||||||||||||||||||||||||||||||
| // Test that target_url doesn't break when used with a simple status filter | ||||||||||||||||||||||||||||||
| const simpleFilter: FilterLeaf = { | ||||||||||||||||||||||||||||||
| request_response_rmt: { | ||||||||||||||||||||||||||||||
| target_url: { equals: 'https://api.openai.com/v1/chat/completions' } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const result = buildFilterClickHouse({ filter: simpleFilter, argsAcc: [] }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Should successfully build target_url filter | ||||||||||||||||||||||||||||||
| expect(result.argsAcc).toEqual(['https://api.openai.com/v1/chat/completions']); | ||||||||||||||||||||||||||||||
| expect(result.filter).toBe('request_response_rmt.target_url = {val_0 : String}'); | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| describe('Target URL Filter Mapping Verification', () => { | ||||||||||||||||||||||||||||||
| test('should verify target_url mapping exists in request_response_rmt', () => { | ||||||||||||||||||||||||||||||
| // This test verifies that our fix is working by checking that target_url | ||||||||||||||||||||||||||||||
| // filters can be built without throwing errors | ||||||||||||||||||||||||||||||
| const filter: FilterLeaf = { | ||||||||||||||||||||||||||||||
| request_response_rmt: { | ||||||||||||||||||||||||||||||
| target_url: { equals: 'https://api.anthropic.com/v1/messages' } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // This should not throw an error now that target_url mapping is added | ||||||||||||||||||||||||||||||
| expect(() => { | ||||||||||||||||||||||||||||||
| buildFilterClickHouse({ filter, argsAcc: [] }); | ||||||||||||||||||||||||||||||
| }).not.toThrow(); | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| test('should handle target_url with different URL formats', () => { | ||||||||||||||||||||||||||||||
| const testCases = [ | ||||||||||||||||||||||||||||||
| 'https://api.openai.com/v1/chat/completions', | ||||||||||||||||||||||||||||||
| 'https://api.anthropic.com/v1/messages', | ||||||||||||||||||||||||||||||
| 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent', | ||||||||||||||||||||||||||||||
| 'https://api.cohere.ai/v1/generate', | ||||||||||||||||||||||||||||||
| 'http://localhost:3000/api/proxy', | ||||||||||||||||||||||||||||||
| 'https://api.mistral.ai/v1/chat/completions', | ||||||||||||||||||||||||||||||
| 'wss://api.openai.com/v1/realtime', | ||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| testCases.forEach((url) => { | ||||||||||||||||||||||||||||||
| const filter: FilterLeaf = { | ||||||||||||||||||||||||||||||
| request_response_rmt: { | ||||||||||||||||||||||||||||||
| target_url: { equals: url } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const result = buildFilterClickHouse({ filter, argsAcc: [] }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| expect(result.filter).toBe('request_response_rmt.target_url = {val_0 : String}'); | ||||||||||||||||||||||||||||||
| expect(result.argsAcc).toEqual([url]); | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| test('should handle target_url with special characters in URL', () => { | ||||||||||||||||||||||||||||||
| const urlWithSpecialChars = 'https://api.example.com/v1/test?param=value&other=test%20string'; | ||||||||||||||||||||||||||||||
| const filter: FilterLeaf = { | ||||||||||||||||||||||||||||||
| request_response_rmt: { | ||||||||||||||||||||||||||||||
| target_url: { equals: urlWithSpecialChars } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const result = buildFilterClickHouse({ filter, argsAcc: [] }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| expect(result.filter).toBe('request_response_rmt.target_url = {val_0 : String}'); | ||||||||||||||||||||||||||||||
| expect(result.argsAcc).toEqual([urlWithSpecialChars]); | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| describe('Target URL Filter Edge Cases', () => { | ||||||||||||||||||||||||||||||
| test('should handle empty target_url value', () => { | ||||||||||||||||||||||||||||||
| const filter: FilterLeaf = { | ||||||||||||||||||||||||||||||
| request_response_rmt: { | ||||||||||||||||||||||||||||||
| target_url: { equals: '' } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const result = buildFilterClickHouse({ filter, argsAcc: [] }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| expect(result.filter).toBe('request_response_rmt.target_url = {val_0 : String}'); | ||||||||||||||||||||||||||||||
| expect(result.argsAcc).toEqual(['']); | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| test('should handle very long target_url values', () => { | ||||||||||||||||||||||||||||||
| const longUrl = 'https://api.example.com/v1/very/long/path/that/goes/on/and/on/' + 'segment/'.repeat(50) + '?param=value'; | ||||||||||||||||||||||||||||||
| const filter: FilterLeaf = { | ||||||||||||||||||||||||||||||
| request_response_rmt: { | ||||||||||||||||||||||||||||||
| target_url: { equals: longUrl } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const result = buildFilterClickHouse({ filter, argsAcc: [] }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| expect(result.filter).toBe('request_response_rmt.target_url = {val_0 : String}'); | ||||||||||||||||||||||||||||||
| expect(result.argsAcc).toEqual([longUrl]); | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| test('should handle target_url with unicode characters', () => { | ||||||||||||||||||||||||||||||
| const unicodeUrl = 'https://api.example.com/v1/测试/пример/🚀?param=тест'; | ||||||||||||||||||||||||||||||
| const filter: FilterLeaf = { | ||||||||||||||||||||||||||||||
| request_response_rmt: { | ||||||||||||||||||||||||||||||
| target_url: { equals: unicodeUrl } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const result = buildFilterClickHouse({ filter, argsAcc: [] }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| expect(result.filter).toBe('request_response_rmt.target_url = {val_0 : String}'); | ||||||||||||||||||||||||||||||
| expect(result.argsAcc).toEqual([unicodeUrl]); | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| describe('Target URL Filter Integration', () => { | ||||||||||||||||||||||||||||||
| test('should work with existing argument arrays', () => { | ||||||||||||||||||||||||||||||
| const filter: FilterLeaf = { | ||||||||||||||||||||||||||||||
| request_response_rmt: { | ||||||||||||||||||||||||||||||
| target_url: { equals: 'https://api.openai.com/v1/chat/completions' } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const existingArgs = ['existing_value_1', 'existing_value_2']; | ||||||||||||||||||||||||||||||
| const result = buildFilterClickHouse({ filter, argsAcc: [...existingArgs] }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| expect(result.filter).toBe('request_response_rmt.target_url = {val_2 : String}'); | ||||||||||||||||||||||||||||||
| expect(result.argsAcc).toEqual([...existingArgs, 'https://api.openai.com/v1/chat/completions']); | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| test('should demonstrate the fix prevents previous error', () => { | ||||||||||||||||||||||||||||||
| // Before the fix, this would cause an error because target_url was not mapped | ||||||||||||||||||||||||||||||
| const filter: FilterLeaf = { | ||||||||||||||||||||||||||||||
| request_response_rmt: { | ||||||||||||||||||||||||||||||
| target_url: { contains: 'anthropic' } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // This should not throw an error now that target_url mapping is fixed | ||||||||||||||||||||||||||||||
| expect(() => { | ||||||||||||||||||||||||||||||
| const result = buildFilterClickHouse({ filter, argsAcc: [] }); | ||||||||||||||||||||||||||||||
| expect(result.filter).toContain("request_response_rmt.target_url ILIKE '%' || {val_0 : String}::text || '%'"); | ||||||||||||||||||||||||||||||
| expect(result.argsAcc).toEqual(['anthropic']); | ||||||||||||||||||||||||||||||
| }).not.toThrow(); | ||||||||||||||||||||||||||||||
|
Comment on lines
+227
to
+231
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Consider using toThrow() without the inner expectation, as the inner expects may not run if an error is thrown earlier
Suggested change
|
||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
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.
style: This test is redundant with the basic equals test on lines 10-21 and doesn't add new validation