File size: 5,610 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
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
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { enableHook, disableHook } from './hookSettings.js';
import { SettingScope, type LoadedSettings } from '../config/settings.js';

describe('hookSettings', () => {
  let mockSettings: LoadedSettings;
  let mockUser: {
    path: string;
    settings: { hooksConfig: { disabled: string[] } };
  };
  let mockWorkspace: {
    path: string;
    settings: { hooksConfig: { disabled: string[] } };
  };
  let mockSetValue: ReturnType<typeof vi.fn>;

  beforeEach(() => {
    mockUser = {
      path: '/mock/user.json',
      settings: { hooksConfig: { disabled: [] } },
    };
    mockWorkspace = {
      path: '/mock/workspace.json',
      settings: { hooksConfig: { disabled: [] } },
    };
    mockSetValue = vi.fn();

    mockSettings = {
      forScope: (scope: SettingScope) => {
        if (scope === SettingScope.User) return mockUser;
        if (scope === SettingScope.Workspace) return mockWorkspace;
        return mockUser; // Default/Fallback
      },
      setValue: mockSetValue,
    } as unknown as LoadedSettings;
  });

  describe('enableHook', () => {
    it('should return no-op if hook is not disabled in any scope', () => {
      const result = enableHook(mockSettings, 'test-hook');

      expect(result.status).toBe('no-op');
      expect(result.action).toBe('enable');
      expect(result.modifiedScopes).toHaveLength(0);
      expect(result.alreadyInStateScopes).toHaveLength(2); // User + Workspace
      expect(mockSetValue).not.toHaveBeenCalled();
    });

    it('should enable hook in User scope if disabled there', () => {
      mockUser.settings.hooksConfig.disabled = ['test-hook'];

      const result = enableHook(mockSettings, 'test-hook');

      expect(result.status).toBe('success');
      expect(result.modifiedScopes).toEqual([
        { scope: SettingScope.User, path: '/mock/user.json' },
      ]);
      expect(mockSetValue).toHaveBeenCalledWith(
        SettingScope.User,
        'hooksConfig.disabled',
        [],
      );
    });

    it('should enable hook in Workspace scope if disabled there', () => {
      mockWorkspace.settings.hooksConfig.disabled = ['test-hook'];

      const result = enableHook(mockSettings, 'test-hook');

      expect(result.status).toBe('success');
      expect(result.modifiedScopes).toEqual([
        { scope: SettingScope.Workspace, path: '/mock/workspace.json' },
      ]);
      expect(mockSetValue).toHaveBeenCalledWith(
        SettingScope.Workspace,
        'hooksConfig.disabled',
        [],
      );
    });

    it('should enable hook in BOTH scopes if disabled in both', () => {
      mockUser.settings.hooksConfig.disabled = ['test-hook', 'other'];
      mockWorkspace.settings.hooksConfig.disabled = ['test-hook'];

      const result = enableHook(mockSettings, 'test-hook');

      expect(result.status).toBe('success');
      expect(result.modifiedScopes).toHaveLength(2);
      expect(result.modifiedScopes).toContainEqual({
        scope: SettingScope.User,
        path: '/mock/user.json',
      });
      expect(result.modifiedScopes).toContainEqual({
        scope: SettingScope.Workspace,
        path: '/mock/workspace.json',
      });

      expect(mockSetValue).toHaveBeenCalledWith(
        SettingScope.Workspace,
        'hooksConfig.disabled',
        [],
      );
      expect(mockSetValue).toHaveBeenCalledWith(
        SettingScope.User,
        'hooksConfig.disabled',
        ['other'],
      );
    });
  });

  describe('disableHook', () => {
    it('should disable hook in the requested scope', () => {
      const result = disableHook(
        mockSettings,
        'test-hook',
        SettingScope.Workspace,
      );

      expect(result.status).toBe('success');
      expect(result.modifiedScopes).toEqual([
        { scope: SettingScope.Workspace, path: '/mock/workspace.json' },
      ]);
      expect(mockSetValue).toHaveBeenCalledWith(
        SettingScope.Workspace,
        'hooksConfig.disabled',
        ['test-hook'],
      );
    });

    it('should return no-op if already disabled in requested scope', () => {
      mockWorkspace.settings.hooksConfig.disabled = ['test-hook'];

      const result = disableHook(
        mockSettings,
        'test-hook',
        SettingScope.Workspace,
      );

      expect(result.status).toBe('no-op');
      expect(mockSetValue).not.toHaveBeenCalled();
    });

    it('should disable in requested scope and report if already disabled in other scope', () => {
      // User has it disabled
      mockUser.settings.hooksConfig.disabled = ['test-hook'];

      // We request disable in Workspace
      const result = disableHook(
        mockSettings,
        'test-hook',
        SettingScope.Workspace,
      );

      expect(result.status).toBe('success');
      expect(result.modifiedScopes).toEqual([
        { scope: SettingScope.Workspace, path: '/mock/workspace.json' },
      ]);
      expect(result.alreadyInStateScopes).toEqual([
        { scope: SettingScope.User, path: '/mock/user.json' },
      ]);
      expect(mockSetValue).toHaveBeenCalledWith(
        SettingScope.Workspace,
        'hooksConfig.disabled',
        ['test-hook'],
      );
    });

    it('should return error if invalid scope provided', () => {
      // @ts-expect-error - Testing runtime check
      const result = disableHook(mockSettings, 'test-hook', 'InvalidScope');

      expect(result.status).toBe('error');
      expect(result.error).toContain('Invalid settings scope');
    });
  });
});