File size: 6,107 Bytes
7421850
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect, vi } from 'vitest';
import {
  enableFeature,
  disableFeature,
  type FeatureToggleStrategy,
} from './featureToggleUtils.js';
import {
  SettingScope,
  type LoadedSettings,
  type LoadableSettingScope,
} from '../config/settings.js';

function createMockLoadedSettings(opts: {
  userSettings?: Record<string, unknown>;
  workspaceSettings?: Record<string, unknown>;
  userPath?: string;
  workspacePath?: string;
}): LoadedSettings {
  const scopes: Record<
    string,
    { settings: Record<string, unknown>; path: string }
  > = {
    [SettingScope.User]: {
      settings: opts.userSettings ?? {},
      path: opts.userPath ?? '/home/user/.gemini/settings.json',
    },
    [SettingScope.Workspace]: {
      settings: opts.workspaceSettings ?? {},
      path: opts.workspacePath ?? '/project/.gemini/settings.json',
    },
  };

  const mockSettings = {
    forScope: vi.fn((scope: LoadableSettingScope) => scopes[scope]),
    setValue: vi.fn(),
  } as unknown as LoadedSettings;

  return mockSettings;
}

function createMockStrategy(overrides?: {
  needsEnabling?: (
    settings: LoadedSettings,
    scope: LoadableSettingScope,
    featureName: string,
  ) => boolean;
  isExplicitlyDisabled?: (
    settings: LoadedSettings,
    scope: LoadableSettingScope,
    featureName: string,
  ) => boolean;
}): FeatureToggleStrategy {
  return {
    needsEnabling: vi.fn(overrides?.needsEnabling ?? (() => false)),
    enable: vi.fn(),
    isExplicitlyDisabled: vi.fn(
      overrides?.isExplicitlyDisabled ?? (() => false),
    ),
    disable: vi.fn(),
  };
}

describe('featureToggleUtils', () => {
  describe('enableFeature', () => {
    it('should return no-op when the feature is already enabled in all scopes', () => {
      const settings = createMockLoadedSettings({});
      const strategy = createMockStrategy({
        needsEnabling: () => false,
      });

      const result = enableFeature(settings, 'my-feature', strategy);

      expect(result.status).toBe('no-op');
      expect(result.action).toBe('enable');
      expect(result.featureName).toBe('my-feature');
      expect(result.modifiedScopes).toHaveLength(0);
      expect(result.alreadyInStateScopes).toHaveLength(2);
      expect(strategy.enable).not.toHaveBeenCalled();
    });

    it('should enable the feature when disabled in one scope', () => {
      const settings = createMockLoadedSettings({});
      const strategy = createMockStrategy({
        needsEnabling: (_s, scope) => scope === SettingScope.Workspace,
      });

      const result = enableFeature(settings, 'my-feature', strategy);

      expect(result.status).toBe('success');
      expect(result.action).toBe('enable');
      expect(result.modifiedScopes).toHaveLength(1);
      expect(result.modifiedScopes[0].scope).toBe(SettingScope.Workspace);
      expect(result.alreadyInStateScopes).toHaveLength(1);
      expect(result.alreadyInStateScopes[0].scope).toBe(SettingScope.User);
      expect(strategy.enable).toHaveBeenCalledTimes(1);
    });

    it('should enable the feature when disabled in both scopes', () => {
      const settings = createMockLoadedSettings({});
      const strategy = createMockStrategy({
        needsEnabling: () => true,
      });

      const result = enableFeature(settings, 'my-feature', strategy);

      expect(result.status).toBe('success');
      expect(result.action).toBe('enable');
      expect(result.modifiedScopes).toHaveLength(2);
      expect(result.alreadyInStateScopes).toHaveLength(0);
      expect(strategy.enable).toHaveBeenCalledTimes(2);
    });

    it('should include correct scope paths in the result', () => {
      const settings = createMockLoadedSettings({
        userPath: '/custom/user/path',
        workspacePath: '/custom/workspace/path',
      });
      const strategy = createMockStrategy({
        needsEnabling: () => true,
      });

      const result = enableFeature(settings, 'my-feature', strategy);

      const paths = result.modifiedScopes.map((s) => s.path);
      expect(paths).toContain('/custom/workspace/path');
      expect(paths).toContain('/custom/user/path');
    });
  });

  describe('disableFeature', () => {
    it('should return no-op when the feature is already disabled in the target scope', () => {
      const settings = createMockLoadedSettings({});
      const strategy = createMockStrategy({
        isExplicitlyDisabled: () => true,
      });

      const result = disableFeature(
        settings,
        'my-feature',
        SettingScope.User,
        strategy,
      );

      expect(result.status).toBe('no-op');
      expect(result.action).toBe('disable');
      expect(result.featureName).toBe('my-feature');
      expect(result.modifiedScopes).toHaveLength(0);
      expect(result.alreadyInStateScopes).toHaveLength(1);
      expect(strategy.disable).not.toHaveBeenCalled();
    });

    it('should disable the feature when it is enabled', () => {
      const settings = createMockLoadedSettings({});
      const strategy = createMockStrategy({
        isExplicitlyDisabled: () => false,
      });

      const result = disableFeature(
        settings,
        'my-feature',
        SettingScope.User,
        strategy,
      );

      expect(result.status).toBe('success');
      expect(result.action).toBe('disable');
      expect(result.modifiedScopes).toHaveLength(1);
      expect(result.modifiedScopes[0].scope).toBe(SettingScope.User);
      expect(strategy.disable).toHaveBeenCalledOnce();
    });

    it('should return error for an invalid  scope', () => {
      const settings = createMockLoadedSettings({});
      const strategy = createMockStrategy();

      const result = disableFeature(
        settings,
        'my-feature',
        SettingScope.Session,
        strategy,
      );

      expect(result.status).toBe('error');
      expect(result.action).toBe('disable');
      expect(result.error).toContain('Invalid settings scope');
      expect(strategy.disable).not.toHaveBeenCalled();
    });
  });
});