-
Notifications
You must be signed in to change notification settings - Fork 451
Add support for Feature Flags #4439
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 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fb4d03f
Add a feature flags message to reduce bandwidth
guill ff27ea7
Convert client feature flags to a function
guill fd6f877
Remove commented out code
guill 0d4889f
Remove unnecessary getter/setters.
guill dfb7c4d
Changed typing from `any` to `unknown`.
guill 2b6269c
Add end-to-end browser tests
guill 4828ccd
Merge remote-tracking branch 'origin/main' into js/feature_flags
guill 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
Next
Next commit
Add a feature flags message to reduce bandwidth
- Loading branch information
commit fb4d03fee096df63f2610a8a95192abf1a1e7ce5
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,2 @@ | ||
| { | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import { api } from '@/scripts/api' | ||
|
|
||
| describe('API Feature Flags', () => { | ||
| let mockWebSocket: any | ||
| const wsEventHandlers: { [key: string]: (event: any) => void } = {} | ||
|
|
||
| beforeEach(() => { | ||
| // Use fake timers | ||
| vi.useFakeTimers() | ||
|
|
||
| // Mock WebSocket | ||
| mockWebSocket = { | ||
| readyState: 1, // WebSocket.OPEN | ||
| send: vi.fn(), | ||
| close: vi.fn(), | ||
| addEventListener: vi.fn( | ||
| (event: string, handler: (event: any) => void) => { | ||
| wsEventHandlers[event] = handler | ||
| } | ||
| ), | ||
| removeEventListener: vi.fn() | ||
| } | ||
|
|
||
| // Mock WebSocket constructor | ||
| global.WebSocket = vi.fn().mockImplementation(() => mockWebSocket) as any | ||
|
|
||
| // Reset API state | ||
| api.feature_flags = {} | ||
| api.clientFeatureFlags = { | ||
| supports_preview_metadata: true, | ||
| api_version: '1.0.0', | ||
| capabilities: ['bulk_operations', 'async_nodes'] | ||
| } | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers() | ||
| vi.restoreAllMocks() | ||
| }) | ||
|
|
||
| describe('Feature flags negotiation', () => { | ||
| it('should send client feature flags as first message on connection', async () => { | ||
| // Initialize API connection | ||
| const initPromise = api.init() | ||
|
|
||
| // Simulate connection open | ||
| wsEventHandlers['open'](new Event('open')) | ||
|
|
||
| // Check that feature flags were sent as first message | ||
| expect(mockWebSocket.send).toHaveBeenCalledTimes(1) | ||
| const sentMessage = JSON.parse(mockWebSocket.send.mock.calls[0][0]) | ||
| expect(sentMessage).toEqual({ | ||
| type: 'feature_flags', | ||
| data: { | ||
| supports_preview_metadata: true, | ||
| api_version: '1.0.0', | ||
| capabilities: ['bulk_operations', 'async_nodes'] | ||
| } | ||
| }) | ||
|
|
||
| // Simulate server response with status message | ||
| wsEventHandlers['message']({ | ||
| data: JSON.stringify({ | ||
| type: 'status', | ||
| data: { | ||
| status: { exec_info: { queue_remaining: 0 } }, | ||
| sid: 'test-sid' | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| // Simulate server feature flags response | ||
| wsEventHandlers['message']({ | ||
| data: JSON.stringify({ | ||
| type: 'feature_flags', | ||
| data: { | ||
| supports_preview_metadata: true, | ||
| async_execution: true, | ||
| supported_formats: ['webp', 'jpeg', 'png'], | ||
| api_version: '1.0.0', | ||
| max_upload_size: 104857600, | ||
| capabilities: ['isolated_nodes', 'dynamic_models'] | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| await initPromise | ||
|
|
||
| // Check that server features were stored | ||
| expect(api.feature_flags).toEqual({ | ||
| supports_preview_metadata: true, | ||
| async_execution: true, | ||
| supported_formats: ['webp', 'jpeg', 'png'], | ||
| api_version: '1.0.0', | ||
| max_upload_size: 104857600, | ||
| capabilities: ['isolated_nodes', 'dynamic_models'] | ||
| }) | ||
| }) | ||
|
|
||
| it('should handle server without feature flags support', async () => { | ||
| // Initialize API connection | ||
| const initPromise = api.init() | ||
|
|
||
| // Simulate connection open | ||
| wsEventHandlers['open'](new Event('open')) | ||
|
|
||
| // Clear the send mock to reset | ||
| mockWebSocket.send.mockClear() | ||
|
|
||
| // Simulate server response with status but no feature flags | ||
| wsEventHandlers['message']({ | ||
| data: JSON.stringify({ | ||
| type: 'status', | ||
| data: { | ||
| status: { exec_info: { queue_remaining: 0 } }, | ||
| sid: 'test-sid' | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| // Simulate some other message (not feature flags) | ||
| wsEventHandlers['message']({ | ||
| data: JSON.stringify({ | ||
| type: 'execution_start', | ||
| data: {} | ||
| }) | ||
| }) | ||
|
|
||
| await initPromise | ||
|
|
||
| // Server features should remain empty | ||
| expect(api.feature_flags).toEqual({}) | ||
| }) | ||
| }) | ||
|
|
||
| describe('Feature checking methods', () => { | ||
| beforeEach(() => { | ||
| // Set up some test features | ||
| api.feature_flags = { | ||
| supports_preview_metadata: true, | ||
| async_execution: false, | ||
| capabilities: ['isolated_nodes', 'dynamic_models'] | ||
| } | ||
| }) | ||
|
|
||
| it('should check if server supports a boolean feature', () => { | ||
| expect(api.serverSupportsFeature('supports_preview_metadata')).toBe(true) | ||
| expect(api.serverSupportsFeature('async_execution')).toBe(false) | ||
| expect(api.serverSupportsFeature('non_existent_feature')).toBe(false) | ||
| }) | ||
|
|
||
| it('should get server feature value', () => { | ||
| expect(api.getServerFeature('supports_preview_metadata')).toBe(true) | ||
| expect(api.getServerFeature('capabilities')).toEqual([ | ||
| 'isolated_nodes', | ||
| 'dynamic_models' | ||
| ]) | ||
| expect(api.getServerFeature('non_existent_feature')).toBeUndefined() | ||
| }) | ||
| }) | ||
|
|
||
| describe('Client feature flags configuration', () => { | ||
| it('should use default client feature flags', () => { | ||
| // Verify default flags are loaded from config | ||
| expect(api.clientFeatureFlags).toHaveProperty( | ||
| 'supports_preview_metadata', | ||
| true | ||
| ) | ||
| expect(api.clientFeatureFlags).toHaveProperty('api_version', '1.0.0') | ||
| expect(api.clientFeatureFlags).toHaveProperty('capabilities') | ||
| expect(api.clientFeatureFlags.capabilities).toEqual([ | ||
| 'bulk_operations', | ||
| 'async_nodes' | ||
| ]) | ||
| }) | ||
| }) | ||
|
|
||
| describe('Integration with preview messages', () => { | ||
| it('should affect preview message handling based on feature support', () => { | ||
| // Test with metadata support | ||
| api.feature_flags = { supports_preview_metadata: true } | ||
| expect(api.serverSupportsFeature('supports_preview_metadata')).toBe(true) | ||
|
|
||
| // Test without metadata support | ||
| api.feature_flags = {} | ||
| expect(api.serverSupportsFeature('supports_preview_metadata')).toBe(false) | ||
| }) | ||
| }) | ||
| }) |
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.
What does this do? If it's removed and the refs in the unit test replaced with
serverFeatureFlags, everything still passes.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.
I've removed it. Claude suggested it and I assumed it was related to how TypeScript stubs out functionality in unit tests. Not really sure though now 🤔