using System; using System.Collections.Generic; using System.Linq; using Paca.Browser.Tabs; using Xunit; namespace Paca.Tests.Browser; public class ChromeOmniboxAutocompleteTests { [Fact] public void OmniboxAutocompleteScorer_Calculates_Scores_Correctly() { var now = DateTime.UtcNow; // 1. Exact URL match (e.g. typing "github.com") var scoreExactUrl = OmniboxAutocompleteScorer.CalculateScore("github.com", "GitHub: Where the world builds", "https://github.com", visitCount: 50, lastVisit: now); // 2. Substring match var scoreSub = OmniboxAutocompleteScorer.CalculateScore("hub", "GitHub: Where the world builds", "https://github.com", visitCount: 1, lastVisit: now.AddDays(-10)); // 3. No match var scoreNoMatch = OmniboxAutocompleteScorer.CalculateScore("banana", "GitHub", "https://github.com", visitCount: 50, lastVisit: now); Assert.True(scoreExactUrl > scoreSub, $"Exact URL score ({scoreExactUrl}) should be higher than substring score ({scoreSub})"); Assert.Equal(0, scoreNoMatch); } [Fact] public void OmniboxSuggestionAggregator_Combines_And_Ranks_Sources() { var aggregator = new OmniboxSuggestionAggregator(); var openTabs = new List { new("Google Search", "https://www.google.com", OmniboxSuggestionType.OpenTab, VisitCount: 10, LastVisited: DateTime.UtcNow) }; var bookmarks = new List { new("GitHub Main", "https://github.com", OmniboxSuggestionType.Bookmark, VisitCount: 5, LastVisited: DateTime.UtcNow.AddDays(-1)), new("Google Drive", "https://drive.google.com", OmniboxSuggestionType.Bookmark, VisitCount: 20, LastVisited: DateTime.UtcNow) }; var history = new List { new("Stack Overflow", "https://stackoverflow.com/questions", OmniboxSuggestionType.History, VisitCount: 2, LastVisited: DateTime.UtcNow.AddDays(-2)) }; var suggestions = aggregator.Aggregate("google", openTabs, bookmarks, history, maxCount: 5); Assert.Equal(2, suggestions.Count); Assert.All(suggestions, s => Assert.Contains("google", s.Url + s.Title, StringComparison.OrdinalIgnoreCase)); // Top match should have higher score Assert.True(suggestions[0].Score >= suggestions[1].Score); } [Fact] public void OmniboxSuggestionAggregator_Deduplicates_Url_Across_Sources() { var aggregator = new OmniboxSuggestionAggregator(); // Same URL in open tab and bookmark: OpenTab takes priority or higher score wins var openTabs = new List { new("Paca Official", "https://paca.app", OmniboxSuggestionType.OpenTab, 1, DateTime.UtcNow) }; var bookmarks = new List { new("Paca App", "https://paca.app", OmniboxSuggestionType.Bookmark, 10, DateTime.UtcNow) }; var results = aggregator.Aggregate("paca", openTabs, bookmarks, new List(), maxCount: 5); Assert.Single(results); Assert.Equal("https://paca.app", results[0].Url); } }