Repository navigation
fix: refresh AI tool availability after provider preference changes #10748
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
Shubham-Padkonde
wants to merge
1
commit into
pgadmin-org:master
Choose a base branch
from
Shubham-Padkonde:fix/10688-ai-preferences
base: master
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.
+154
−23
Open
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
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
35 changes: 35 additions & 0 deletions
35
web/pgadmin/tools/sqleditor/tests/test_panel_ai_availability.py
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,35 @@ | ||
| ########################################################################## | ||
| # | ||
| # pgAdmin 4 - PostgreSQL Tools | ||
| # | ||
| # Copyright (C) 2013 - 2026, The pgAdmin Development Team | ||
| # This software is released under the PostgreSQL Licence | ||
| # | ||
| ########################################################################## | ||
|
|
||
| import json | ||
| from types import SimpleNamespace | ||
| from unittest.mock import patch | ||
|
|
||
| from pgadmin.utils.route import BaseTestGenerator | ||
|
|
||
|
|
||
| class TestPanelAIAvailability(BaseTestGenerator): | ||
| """Each new Query Tool layout gets the current server-side AI status.""" | ||
|
|
||
| @patch('pgadmin.tools.sqleditor.render_template', return_value='') | ||
| @patch('pgadmin.tools.sqleditor.get_server') | ||
| @patch('pgadmin.llm.utils.is_llm_enabled') | ||
| def runTest(self, enabled, get_server, render): | ||
| get_server.return_value = SimpleNamespace( | ||
| shared=False, bgcolor=None, name='test', username='test', role='') | ||
| for value in (True, False): | ||
| with self.subTest(enabled=value): | ||
| enabled.return_value = value | ||
| response = self.tester.post('/sqleditor/panel/1', query_string={ | ||
| 'sid': 1, 'title': 'Query Tool', 'selectedNodeInfo': '{}', | ||
| 'llm_enabled': str(not value).lower(), | ||
| }) | ||
| self.assertEqual(response.status_code, 200) | ||
| params = json.loads(render.call_args.kwargs['params']) | ||
| self.assertIs(params['llm_enabled'], value) |
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,83 @@ | ||
| ///////////////////////////////////////////////////////////// | ||
| // | ||
| // pgAdmin 4 - PostgreSQL Tools | ||
| // | ||
| // Copyright (C) 2013 - 2026, The pgAdmin Development Team | ||
| // This software is released under the PostgreSQL Licence | ||
| // | ||
| ////////////////////////////////////////////////////////////// | ||
|
|
||
| import {waitFor} from '@testing-library/react'; | ||
|
|
||
| jest.unmock('zustand'); | ||
| jest.mock('../../../pgadmin/llm/static/js/AIReport', () => jest.fn()); | ||
| jest.mock('../../../pgadmin/static/js/api_instance', () => () => ({get: mockGet})); | ||
| jest.mock('sources/url_for', () => (name) => name); | ||
| jest.mock('sources/pgadmin', () => ({})); | ||
| jest.mock('../../../pgadmin/browser/static/js/MainMenuFactory', () => ({ | ||
| createMainMenus: jest.fn(), enableDisableMenus: jest.fn(), | ||
| })); | ||
|
|
||
| const mockGet = jest.fn(); | ||
| const status = (enabled) => ({data: {success: true, data: {enabled, system_enabled: true}}}); | ||
|
|
||
| describe('AI tools after preferences are saved', () => { | ||
| let browser, pgAdmin, preferences, tools; | ||
|
|
||
| beforeEach(() => { | ||
| jest.resetModules(); | ||
| mockGet.mockReset(); | ||
| browser = {add_menus: jest.fn(), add_menu_category: jest.fn()}; | ||
| pgAdmin = require('sources/pgadmin'); | ||
| preferences = require('../../../pgadmin/preferences/static/js/store').default; | ||
| global.define = (_deps, factory) => factory((s) => s, browser); | ||
| try { | ||
| require('../../../pgadmin/llm/static/js/ai_tools'); | ||
| } finally { | ||
| delete global.define; | ||
| } | ||
| tools = browser.AITools; | ||
| }); | ||
|
|
||
| // PreferencesComponent calls cache() after a successful save. | ||
| async function refreshPreferences(provider) { | ||
| mockGet.mockResolvedValueOnce({data: [{module: 'ai', name: 'default_provider', value: provider}]}); | ||
| await preferences.getState().cache(); | ||
| } | ||
|
|
||
| it('updates report menus and the flag for new Query Tools without registering duplicate menus', async () => { | ||
| await refreshPreferences(''); | ||
| mockGet.mockResolvedValue(status(false)); | ||
| tools.init(); | ||
| await waitFor(() => expect(tools.llmStatusChecked).toBe(true)); | ||
| expect(tools.checkLLMEnabled()).toBe(false); | ||
| mockGet.mockResolvedValue(status(true)); | ||
| await refreshPreferences('ollama'); | ||
| await waitFor(() => expect(pgAdmin.llm_enabled).toBe(true)); | ||
| expect(tools.checkLLMEnabled()).toBe(true); | ||
|
|
||
| tools.init(); | ||
| const calls = mockGet.mock.calls.length; | ||
| await refreshPreferences('ollama'); | ||
| expect(mockGet.mock.calls.slice(calls)).toEqual([['preferences.get_all']]); | ||
| mockGet.mockResolvedValue(status(false)); | ||
| await refreshPreferences(''); | ||
| await waitFor(() => expect(pgAdmin.llm_enabled).toBe(false)); | ||
| expect(tools.checkLLMEnabled()).toBe(false); | ||
| expect(browser.add_menus).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('keeps a failed lookup disabled even if an older successful request finishes later', async () => { | ||
| let resolveFirst; | ||
| mockGet.mockReturnValueOnce(new Promise(resolve => { resolveFirst = resolve; })); | ||
| tools.init(); | ||
| mockGet.mockRejectedValue(new Error('status unavailable')); | ||
| await refreshPreferences('openai'); | ||
| await waitFor(() => expect(tools.llmStatusChecked).toBe(true)); | ||
| resolveFirst(status(true)); | ||
| await new Promise(resolve => setTimeout(resolve, 0)); | ||
| expect(tools.checkLLMEnabled()).toBe(false); | ||
| expect(pgAdmin.llm_enabled).toBe(false); | ||
| expect(browser.add_menus).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
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.