File size: 3,315 Bytes
84aa3bf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { SettingScope, type LoadedSettings } from '../config/settings.js';
import {
getScopeItems,
getScopeMessageForSetting,
} from './dialogScopeUtils.js';
import { isInSettingsScope } from './settingsUtils.js';
vi.mock('../config/settings', () => ({
SettingScope: {
User: 'user',
Workspace: 'workspace',
System: 'system',
},
isLoadableSettingScope: (scope: string) =>
['user', 'workspace', 'system'].includes(scope),
}));
vi.mock('./settingsUtils', () => ({
isInSettingsScope: vi.fn(),
}));
describe('dialogScopeUtils', () => {
beforeEach(() => {
vi.resetAllMocks();
});
describe('getScopeItems', () => {
it('should return scope items with correct labels and values', () => {
const items = getScopeItems();
expect(items).toEqual([
{ label: 'User Settings', value: SettingScope.User },
{ label: 'Workspace Settings', value: SettingScope.Workspace },
{ label: 'System Settings', value: SettingScope.System },
]);
});
});
describe('getScopeMessageForSetting', () => {
let mockSettings: { forScope: ReturnType<typeof vi.fn> };
beforeEach(() => {
mockSettings = {
forScope: vi.fn().mockReturnValue({ settings: {} }),
};
});
it('should return empty string if not modified in other scopes', () => {
vi.mocked(isInSettingsScope).mockReturnValue(false);
const message = getScopeMessageForSetting(
'key',
SettingScope.User,
mockSettings as unknown as LoadedSettings,
);
expect(message).toBe('');
});
it('should return message indicating modification in other scopes', () => {
vi.mocked(isInSettingsScope).mockReturnValue(true);
const message = getScopeMessageForSetting(
'key',
SettingScope.User,
mockSettings as unknown as LoadedSettings,
);
expect(message).toMatch(/Also modified in/);
expect(message).toMatch(/workspace/);
expect(message).toMatch(/system/);
});
it('should return message indicating modification in other scopes but not current', () => {
const workspaceSettings = { scope: 'workspace' };
const systemSettings = { scope: 'system' };
const userSettings = { scope: 'user' };
mockSettings.forScope.mockImplementation((scope: string) => {
if (scope === SettingScope.Workspace)
return { settings: workspaceSettings };
if (scope === SettingScope.System) return { settings: systemSettings };
if (scope === SettingScope.User) return { settings: userSettings };
return { settings: {} };
});
vi.mocked(isInSettingsScope).mockImplementation(
(_key, settings: unknown) => {
if (settings === workspaceSettings) return true;
if (settings === systemSettings) return false;
if (settings === userSettings) return false;
return false;
},
);
const message = getScopeMessageForSetting(
'key',
SettingScope.User,
mockSettings as unknown as LoadedSettings,
);
expect(message).toBe('(Modified in workspace)');
});
});
});
|