A2a admin setting (#17868)

This commit is contained in:
David Pierce
2026-02-03 20:16:20 +00:00
committed by GitHub
parent 675ca07c8b
commit 75dbf9022c
4 changed files with 317 additions and 30 deletions

View File

@@ -15,6 +15,7 @@ import {
} from 'vitest';
import {
fetchAdminControls,
fetchAdminControlsOnce,
sanitizeAdminSettings,
stopAdminControlsPolling,
getAdminErrorMessage,
@@ -248,6 +249,71 @@ describe('Admin Controls', () => {
});
});
describe('fetchAdminControlsOnce', () => {
it('should return empty object if server is missing', async () => {
const result = await fetchAdminControlsOnce(undefined, true);
expect(result).toEqual({});
expect(mockServer.fetchAdminControls).not.toHaveBeenCalled();
});
it('should return empty object if project ID is missing', async () => {
mockServer = {
fetchAdminControls: vi.fn(),
} as unknown as CodeAssistServer;
const result = await fetchAdminControlsOnce(mockServer, true);
expect(result).toEqual({});
expect(mockServer.fetchAdminControls).not.toHaveBeenCalled();
});
it('should return empty object if admin controls are disabled', async () => {
const result = await fetchAdminControlsOnce(mockServer, false);
expect(result).toEqual({});
expect(mockServer.fetchAdminControls).not.toHaveBeenCalled();
});
it('should fetch from server and sanitize the response', async () => {
const serverResponse = {
strictModeDisabled: true,
unknownField: 'should be removed',
};
(mockServer.fetchAdminControls as Mock).mockResolvedValue(serverResponse);
const result = await fetchAdminControlsOnce(mockServer, true);
expect(result).toEqual({ strictModeDisabled: true });
expect(mockServer.fetchAdminControls).toHaveBeenCalledTimes(1);
});
it('should return empty object on 403 fetch error', async () => {
const error403 = new Error('Forbidden');
Object.assign(error403, { status: 403 });
(mockServer.fetchAdminControls as Mock).mockRejectedValue(error403);
const result = await fetchAdminControlsOnce(mockServer, true);
expect(result).toEqual({});
expect(mockServer.fetchAdminControls).toHaveBeenCalledTimes(1);
});
it('should return empty object on any other fetch error', async () => {
(mockServer.fetchAdminControls as Mock).mockRejectedValue(
new Error('Network error'),
);
const result = await fetchAdminControlsOnce(mockServer, true);
expect(result).toEqual({});
expect(mockServer.fetchAdminControls).toHaveBeenCalledTimes(1);
});
it('should not start or stop any polling timers', async () => {
const setIntervalSpy = vi.spyOn(global, 'setInterval');
const clearIntervalSpy = vi.spyOn(global, 'clearInterval');
(mockServer.fetchAdminControls as Mock).mockResolvedValue({});
await fetchAdminControlsOnce(mockServer, true);
expect(setIntervalSpy).not.toHaveBeenCalled();
expect(clearIntervalSpy).not.toHaveBeenCalled();
});
});
describe('polling', () => {
it('should poll and emit changes', async () => {
// Initial fetch

View File

@@ -89,6 +89,40 @@ export async function fetchAdminControls(
}
}
/**
* Fetches the admin controls from the server a single time.
* This function does not start or stop any polling.
*
* @param server The CodeAssistServer instance.
* @param adminControlsEnabled Whether admin controls are enabled.
* @returns The fetched settings if enabled and successful, otherwise undefined.
*/
export async function fetchAdminControlsOnce(
server: CodeAssistServer | undefined,
adminControlsEnabled: boolean,
): Promise<FetchAdminControlsResponse> {
if (!server || !server.projectId || !adminControlsEnabled) {
return {};
}
try {
const rawSettings = await server.fetchAdminControls({
project: server.projectId,
});
return sanitizeAdminSettings(rawSettings);
} catch (e) {
// Non-enterprise users don't have access to fetch settings.
if (isGaxiosError(e) && e.status === 403) {
return {};
}
debugLogger.error(
'Failed to fetch admin controls: ',
e instanceof Error ? e.message : e,
);
return {};
}
}
/**
* Starts polling for admin controls.
*/