/ src / modules / cmdpal / Tests / Microsoft.CmdPal.UI.ViewModels.UnitTests / MainListPageResultFactoryTests.cs
MainListPageResultFactoryTests.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 System.Linq;
  8  using Microsoft.CmdPal.UI.ViewModels.Commands;
  9  using Microsoft.CommandPalette.Extensions;
 10  using Microsoft.CommandPalette.Extensions.Toolkit;
 11  using Microsoft.VisualStudio.TestTools.UnitTesting;
 12  using Windows.Foundation;
 13  
 14  namespace Microsoft.CmdPal.UI.ViewModels.UnitTests;
 15  
 16  [TestClass]
 17  public partial class MainListPageResultFactoryTests
 18  {
 19      private sealed partial class MockListItem : IListItem
 20      {
 21          public string Title { get; set; } = string.Empty;
 22  
 23          public string Subtitle { get; set; } = string.Empty;
 24  
 25          public ICommand Command => new NoOpCommand();
 26  
 27          public IDetails? Details => null;
 28  
 29          public IIconInfo? Icon => null;
 30  
 31          public string Section => throw new NotImplementedException();
 32  
 33          public ITag[] Tags => throw new NotImplementedException();
 34  
 35          public string TextToSuggest => throw new NotImplementedException();
 36  
 37          public IContextItem[] MoreCommands => throw new NotImplementedException();
 38  
 39  #pragma warning disable CS0067 // The event is never used
 40          public event TypedEventHandler<object, IPropChangedEventArgs>? PropChanged;
 41  #pragma warning restore CS0067 // The event is never used
 42  
 43          public override string ToString() => Title;
 44      }
 45  
 46      private static Scored<IListItem> S(string title, int score)
 47      {
 48          return new Scored<IListItem>
 49          {
 50              Score = score,
 51              Item = new MockListItem { Title = title },
 52          };
 53      }
 54  
 55      [TestMethod]
 56      public void Merge_PrioritizesListsCorrectly()
 57      {
 58          var filtered = new List<Scored<IListItem>>
 59          {
 60              S("F1", 100),
 61              S("F2", 50),
 62          };
 63  
 64          var scoredFallback = new List<Scored<IListItem>>
 65          {
 66              S("SF1", 100),
 67              S("SF2", 60),
 68          };
 69  
 70          var apps = new List<Scored<IListItem>>
 71          {
 72              S("A1", 100),
 73              S("A2", 55),
 74          };
 75  
 76          // Fallbacks are not scored.
 77          var fallbacks = new List<Scored<IListItem>>
 78          {
 79              S("FB1", 0),
 80              S("FB2", 0),
 81          };
 82  
 83          var result = MainListPageResultFactory.Create(
 84              filtered,
 85              scoredFallback,
 86              apps,
 87              fallbacks,
 88              appResultLimit: 10);
 89  
 90          // Expected order:
 91          // 100: F1, SF1, A1
 92          // 60: SF2
 93          // 55: A2
 94          // 50: F2
 95          // Then fallbacks in original order: FB1, FB2
 96          var titles = result.Select(r => r.Title).ToArray();
 97  #pragma warning disable CA1861 // Avoid constant arrays as arguments
 98          CollectionAssert.AreEqual(
 99              new[] { "F1", "SF1", "A1", "SF2", "A2", "F2", "Fallbacks", "FB1", "FB2" },
100              titles);
101  #pragma warning restore CA1861 // Avoid constant arrays as arguments
102      }
103  
104      [TestMethod]
105      public void Merge_AppliesAppLimit()
106      {
107          var apps = new List<Scored<IListItem>>
108          {
109              S("A1", 100),
110              S("A2", 90),
111              S("A3", 80),
112          };
113  
114          var result = MainListPageResultFactory.Create(
115              null,
116              null,
117              apps,
118              null,
119              2);
120  
121          Assert.AreEqual(2, result.Length);
122          Assert.AreEqual("A1", result[0].Title);
123          Assert.AreEqual("A2", result[1].Title);
124      }
125  
126      [TestMethod]
127      public void Merge_FiltersEmptyFallbacks()
128      {
129          var fallbacks = new List<Scored<IListItem>>
130          {
131              S("FB1", 0),
132              S("FB3", 0),
133          };
134  
135          var result = MainListPageResultFactory.Create(
136              null,
137              null,
138              null,
139              fallbacks,
140              appResultLimit: 10);
141  
142          Assert.AreEqual(3, result.Length);
143          Assert.AreEqual("Fallbacks", result[0].Title);
144          Assert.AreEqual("FB1", result[1].Title);
145          Assert.AreEqual("FB3", result[2].Title);
146      }
147  
148      [TestMethod]
149      public void Merge_HandlesNullLists()
150      {
151          var result = MainListPageResultFactory.Create(
152              null,
153              null,
154              null,
155              null,
156              appResultLimit: 10);
157  
158          Assert.IsNotNull(result);
159          Assert.AreEqual(0, result.Length);
160      }
161  }