MockSettingsInterface.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System;
 6  using System.Collections.Generic;
 7  using Microsoft.CmdPal.Ext.WebSearch.Helpers;
 8  
 9  namespace Microsoft.CmdPal.Ext.WebSearch.UnitTests;
10  
11  public class MockSettingsInterface : ISettingsInterface
12  {
13      private readonly List<HistoryItem> _historyItems;
14  
15      public event EventHandler HistoryChanged;
16  
17      public bool GlobalIfURI { get; set; }
18  
19      public int HistoryItemCount { get; set; }
20  
21      public string CustomSearchUri { get; }
22  
23      public IReadOnlyList<HistoryItem> HistoryItems => _historyItems;
24  
25      public MockSettingsInterface(int historyItemCount = 0, bool globalIfUri = true, List<HistoryItem> mockHistory = null)
26      {
27          _historyItems = mockHistory ?? new List<HistoryItem>();
28          GlobalIfURI = globalIfUri;
29          HistoryItemCount = historyItemCount;
30      }
31  
32      public void AddHistoryItem(HistoryItem historyItem)
33      {
34          if (historyItem is null)
35          {
36              return;
37          }
38  
39          _historyItems.Add(historyItem);
40  
41          // Simulate the same logic as SettingsManager
42          if (HistoryItemCount > 0)
43          {
44              while (_historyItems.Count > HistoryItemCount)
45              {
46                  _historyItems.RemoveAt(0);
47              }
48          }
49  
50          HistoryChanged?.Invoke(this, EventArgs.Empty);
51      }
52  
53      // Helper method for testing
54      public void ClearHistory()
55      {
56          _historyItems.Clear();
57          HistoryChanged?.Invoke(this, EventArgs.Empty);
58      }
59  
60      // Helper method for testing
61      public int GetHistoryCount()
62      {
63          return _historyItems.Count;
64      }
65  }