File size: 4,654 Bytes
9332ccf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import type React from 'react';
import { Component, type ReactNode, act } from 'react';
import { renderHook, render } from '../../test-utils/render.js';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { SettingsContext, useSettingsStore } from './SettingsContext.js';
import {
  SettingScope,
  createTestMergedSettings,
  type LoadedSettings,
  type LoadedSettingsSnapshot,
  type SettingsFile,
} from '../../config/settings.js';

const createMockSettingsFile = (path: string): SettingsFile => ({
  path,
  settings: {},
  originalSettings: {},
});

const mockSnapshot: LoadedSettingsSnapshot = {
  system: createMockSettingsFile('/system'),
  systemDefaults: createMockSettingsFile('/defaults'),
  user: createMockSettingsFile('/user'),
  workspace: createMockSettingsFile('/workspace'),
  isTrusted: true,
  errors: [],
  merged: createTestMergedSettings({
    ui: { theme: 'default-theme' },
  }),
};

class ErrorBoundary extends Component<
  { children: ReactNode; onError: (error: Error) => void },
  { hasError: boolean }
> {
  constructor(props: { children: ReactNode; onError: (error: Error) => void }) {
    super(props);
    this.state = { hasError: false };
  }

  static getDerivedStateFromError(_error: Error) {
    return { hasError: true };
  }

  override componentDidCatch(error: Error) {
    this.props.onError(error);
  }

  override render() {
    if (this.state.hasError) {
      return null;
    }
    return this.props.children;
  }
}

const TestHarness = () => {
  useSettingsStore();
  return null;
};

describe('SettingsContext', () => {
  let mockLoadedSettings: LoadedSettings;
  let listeners: Array<() => void> = [];

  beforeEach(() => {
    listeners = [];

    mockLoadedSettings = {
      subscribe: vi.fn((listener: () => void) => {
        listeners.push(listener);
        return () => {
          listeners = listeners.filter((l) => l !== listener);
        };
      }),
      getSnapshot: vi.fn(() => mockSnapshot),
      setValue: vi.fn(),
    } as unknown as LoadedSettings;
  });

  const wrapper = ({ children }: { children: React.ReactNode }) => (
    <SettingsContext.Provider value={mockLoadedSettings}>
      {children}
    </SettingsContext.Provider>
  );

  it('should provide the correct initial state', async () => {
    const { result } = await renderHook(() => useSettingsStore(), { wrapper });

    expect(result.current.settings.merged).toEqual(mockSnapshot.merged);
    expect(result.current.settings.isTrusted).toBe(true);
  });

  it('should allow accessing settings for a specific scope', async () => {
    const { result } = await renderHook(() => useSettingsStore(), { wrapper });

    const userSettings = result.current.settings.forScope(SettingScope.User);
    expect(userSettings).toBe(mockSnapshot.user);

    const workspaceSettings = result.current.settings.forScope(
      SettingScope.Workspace,
    );
    expect(workspaceSettings).toBe(mockSnapshot.workspace);
  });

  it('should trigger re-renders when settings change (external event)', async () => {
    const { result } = await renderHook(() => useSettingsStore(), { wrapper });

    expect(result.current.settings.merged.ui?.theme).toBe('default-theme');

    const newSnapshot = {
      ...mockSnapshot,
      merged: { ui: { theme: 'new-theme' } },
    };
    (
      mockLoadedSettings.getSnapshot as ReturnType<typeof vi.fn>
    ).mockReturnValue(newSnapshot);

    // Trigger the listeners (simulate coreEvents emission)
    act(() => {
      listeners.forEach((l) => l());
    });

    expect(result.current.settings.merged.ui?.theme).toBe('new-theme');
  });

  it('should call store.setValue when setSetting is called', async () => {
    const { result } = await renderHook(() => useSettingsStore(), { wrapper });

    act(() => {
      result.current.setSetting(SettingScope.User, 'ui.theme', 'dark');
    });

    expect(mockLoadedSettings.setValue).toHaveBeenCalledWith(
      SettingScope.User,
      'ui.theme',
      'dark',
    );
  });

  it('should throw error if used outside provider', async () => {
    const onError = vi.fn();
    // Suppress console.error (React logs error boundary info)
    const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});

    await render(
      <ErrorBoundary onError={onError}>
        <TestHarness />
      </ErrorBoundary>,
    );

    expect(onError).toHaveBeenCalledWith(
      expect.objectContaining({
        message: 'useSettingsStore must be used within a SettingsProvider',
      }),
    );

    consoleSpy.mockRestore();
  });
});