mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-10 21:30:40 -07:00
refactor(cli): Reactive useSettingsStore hook (#14915)
This commit is contained in:
@@ -2546,6 +2546,50 @@ describe('Settings Loading and Merging', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Reactivity & Snapshots', () => {
|
||||
let loadedSettings: LoadedSettings;
|
||||
|
||||
beforeEach(() => {
|
||||
const emptySettingsFile: SettingsFile = {
|
||||
path: '/mock/path',
|
||||
settings: {},
|
||||
originalSettings: {},
|
||||
};
|
||||
|
||||
loadedSettings = new LoadedSettings(
|
||||
{ ...emptySettingsFile, path: getSystemSettingsPath() },
|
||||
{ ...emptySettingsFile, path: getSystemDefaultsPath() },
|
||||
{ ...emptySettingsFile, path: USER_SETTINGS_PATH },
|
||||
{ ...emptySettingsFile, path: MOCK_WORKSPACE_SETTINGS_PATH },
|
||||
true, // isTrusted
|
||||
[],
|
||||
);
|
||||
});
|
||||
|
||||
it('getSnapshot() should return stable reference if no changes occur', () => {
|
||||
const snap1 = loadedSettings.getSnapshot();
|
||||
const snap2 = loadedSettings.getSnapshot();
|
||||
expect(snap1).toBe(snap2);
|
||||
});
|
||||
|
||||
it('setValue() should create a new snapshot reference and emit event', () => {
|
||||
const oldSnapshot = loadedSettings.getSnapshot();
|
||||
const oldUserRef = oldSnapshot.user.settings;
|
||||
|
||||
loadedSettings.setValue(SettingScope.User, 'ui.theme', 'high-contrast');
|
||||
|
||||
const newSnapshot = loadedSettings.getSnapshot();
|
||||
|
||||
expect(newSnapshot).not.toBe(oldSnapshot);
|
||||
expect(newSnapshot.user.settings).not.toBe(oldUserRef);
|
||||
expect(newSnapshot.user.settings.ui?.theme).toBe('high-contrast');
|
||||
|
||||
expect(newSnapshot.system.settings).not.toBe(oldSnapshot.system.settings);
|
||||
|
||||
expect(mockCoreEvents.emitSettingsChanged).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Security and Sandbox', () => {
|
||||
let originalArgv: string[];
|
||||
let originalEnv: NodeJS.ProcessEnv;
|
||||
|
||||
@@ -10,6 +10,7 @@ import { platform } from 'node:os';
|
||||
import * as dotenv from 'dotenv';
|
||||
import process from 'node:process';
|
||||
import {
|
||||
CoreEvent,
|
||||
FatalConfigError,
|
||||
GEMINI_DIR,
|
||||
getErrorMessage,
|
||||
@@ -284,6 +285,20 @@ export function createTestMergedSettings(
|
||||
) as MergedSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
* An immutable snapshot of settings state.
|
||||
* Used with useSyncExternalStore for reactive updates.
|
||||
*/
|
||||
export interface LoadedSettingsSnapshot {
|
||||
system: SettingsFile;
|
||||
systemDefaults: SettingsFile;
|
||||
user: SettingsFile;
|
||||
workspace: SettingsFile;
|
||||
isTrusted: boolean;
|
||||
errors: SettingsError[];
|
||||
merged: MergedSettings;
|
||||
}
|
||||
|
||||
export class LoadedSettings {
|
||||
constructor(
|
||||
system: SettingsFile,
|
||||
@@ -303,6 +318,7 @@ export class LoadedSettings {
|
||||
: this.createEmptyWorkspace(workspace);
|
||||
this.errors = errors;
|
||||
this._merged = this.computeMergedSettings();
|
||||
this._snapshot = this.computeSnapshot();
|
||||
}
|
||||
|
||||
readonly system: SettingsFile;
|
||||
@@ -314,6 +330,7 @@ export class LoadedSettings {
|
||||
|
||||
private _workspaceFile: SettingsFile;
|
||||
private _merged: MergedSettings;
|
||||
private _snapshot: LoadedSettingsSnapshot;
|
||||
private _remoteAdminSettings: Partial<Settings> | undefined;
|
||||
|
||||
get merged(): MergedSettings {
|
||||
@@ -368,6 +385,36 @@ export class LoadedSettings {
|
||||
return merged;
|
||||
}
|
||||
|
||||
private computeSnapshot(): LoadedSettingsSnapshot {
|
||||
const cloneSettingsFile = (file: SettingsFile): SettingsFile => ({
|
||||
path: file.path,
|
||||
rawJson: file.rawJson,
|
||||
settings: structuredClone(file.settings),
|
||||
originalSettings: structuredClone(file.originalSettings),
|
||||
});
|
||||
return {
|
||||
system: cloneSettingsFile(this.system),
|
||||
systemDefaults: cloneSettingsFile(this.systemDefaults),
|
||||
user: cloneSettingsFile(this.user),
|
||||
workspace: cloneSettingsFile(this.workspace),
|
||||
isTrusted: this.isTrusted,
|
||||
errors: [...this.errors],
|
||||
merged: structuredClone(this._merged),
|
||||
};
|
||||
}
|
||||
|
||||
// Passing this along with getSnapshot to useSyncExternalStore allows for idiomatic reactivity on settings changes
|
||||
// React will pass a listener fn into this subscribe fn
|
||||
// that listener fn will perform an object identity check on the snapshot and trigger a React re render if the snapshot has changed
|
||||
subscribe(listener: () => void): () => void {
|
||||
coreEvents.on(CoreEvent.SettingsChanged, listener);
|
||||
return () => coreEvents.off(CoreEvent.SettingsChanged, listener);
|
||||
}
|
||||
|
||||
getSnapshot(): LoadedSettingsSnapshot {
|
||||
return this._snapshot;
|
||||
}
|
||||
|
||||
forScope(scope: LoadableSettingScope): SettingsFile {
|
||||
switch (scope) {
|
||||
case SettingScope.User:
|
||||
@@ -409,6 +456,7 @@ export class LoadedSettings {
|
||||
}
|
||||
|
||||
this._merged = this.computeMergedSettings();
|
||||
this._snapshot = this.computeSnapshot();
|
||||
coreEvents.emitSettingsChanged();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user