|
| 1 | +# SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +# Unit tests for FeedbackATron.Deduplicator. |
| 3 | +# |
| 4 | +# The deduplicator is a GenServer backed by an ETS table. Each test case |
| 5 | +# starts its own supervised instance (using start_supervised!/1) so tests |
| 6 | +# can run concurrently without sharing ETS table names. The module under |
| 7 | +# test registers itself as FeedbackATron.Deduplicator, so we use |
| 8 | +# start_supervised!/1 with a unique name to avoid conflicts. |
| 9 | +# |
| 10 | +# Tests cover: |
| 11 | +# - Exact duplicate detection (hash match) |
| 12 | +# - Unique issue detection |
| 13 | +# - Submission recording and history retrieval |
| 14 | +# - Statistics reporting |
| 15 | +# - State clearing |
| 16 | +# - Fuzzy / similar title detection |
| 17 | +# - Edge cases: empty fields, atom vs string keys |
| 18 | + |
| 19 | +defmodule FeedbackATron.DeduplicatorTest do |
| 20 | + use ExUnit.Case, async: false |
| 21 | + |
| 22 | + # We can't easily run multiple named GenServer instances without refactoring, |
| 23 | + # so these tests run serially (async: false) and rely on clear/0 between tests. |
| 24 | + |
| 25 | + setup do |
| 26 | + # Ensure the deduplicator is running (it may be started by the application). |
| 27 | + case Process.whereis(FeedbackATron.Deduplicator) do |
| 28 | + nil -> |
| 29 | + {:ok, _pid} = FeedbackATron.Deduplicator.start_link([]) |
| 30 | + |
| 31 | + _pid -> |
| 32 | + :ok |
| 33 | + end |
| 34 | + |
| 35 | + # Clear all history before each test for isolation. |
| 36 | + :ok = FeedbackATron.Deduplicator.clear() |
| 37 | + :ok |
| 38 | + end |
| 39 | + |
| 40 | + # --------------------------------------------------------------------------- |
| 41 | + # Basic uniqueness / duplicate detection |
| 42 | + # --------------------------------------------------------------------------- |
| 43 | + |
| 44 | + describe "check/1 — uniqueness" do |
| 45 | + test "fresh issue is reported as unique" do |
| 46 | + issue = %{title: "Brand new issue nobody has seen before", body: "details here"} |
| 47 | + assert {:ok, :unique} = FeedbackATron.Deduplicator.check(issue) |
| 48 | + end |
| 49 | + |
| 50 | + test "string-keyed issue is also reported as unique" do |
| 51 | + issue = %{"title" => "Another fresh issue", "body" => "fresh content"} |
| 52 | + assert {:ok, :unique} = FeedbackATron.Deduplicator.check(issue) |
| 53 | + end |
| 54 | + |
| 55 | + test "two distinct issues are each unique" do |
| 56 | + issue_a = %{title: "Issue Alpha", body: "body a"} |
| 57 | + issue_b = %{title: "Issue Beta", body: "body b"} |
| 58 | + |
| 59 | + assert {:ok, :unique} = FeedbackATron.Deduplicator.check(issue_a) |
| 60 | + assert {:ok, :unique} = FeedbackATron.Deduplicator.check(issue_b) |
| 61 | + end |
| 62 | + |
| 63 | + test "issue with empty body is unique on first check" do |
| 64 | + issue = %{title: "Title with no body", body: ""} |
| 65 | + assert {:ok, :unique} = FeedbackATron.Deduplicator.check(issue) |
| 66 | + end |
| 67 | + |
| 68 | + test "after recording, same issue is detected as duplicate" do |
| 69 | + issue = %{title: "Duplicate Detector Test", body: "exact same body"} |
| 70 | + |
| 71 | + # First check — unique |
| 72 | + assert {:ok, :unique} = FeedbackATron.Deduplicator.check(issue) |
| 73 | + |
| 74 | + # Record it |
| 75 | + FeedbackATron.Deduplicator.record(issue, :github, %{status: :submitted, url: "https://github.com/example/issues/1"}) |
| 76 | + |
| 77 | + # Allow the cast to be processed |
| 78 | + Process.sleep(50) |
| 79 | + |
| 80 | + # Second check — should be duplicate |
| 81 | + assert {:duplicate, _existing} = FeedbackATron.Deduplicator.check(issue) |
| 82 | + end |
| 83 | + |
| 84 | + test "duplicate result contains the original submission metadata" do |
| 85 | + issue = %{title: "Recorded Issue", body: "important body text"} |
| 86 | + FeedbackATron.Deduplicator.record(issue, :gitlab, %{status: :submitted, url: "https://gitlab.com/x/y/issues/5"}) |
| 87 | + Process.sleep(50) |
| 88 | + |
| 89 | + case FeedbackATron.Deduplicator.check(issue) do |
| 90 | + {:duplicate, existing} -> |
| 91 | + assert Map.has_key?(existing, :hash) or Map.has_key?(existing, :title) |
| 92 | + |
| 93 | + {:ok, :unique} -> |
| 94 | + # Cast may not have been processed yet on a very slow CI machine; |
| 95 | + # this is a timing-sensitive assertion so we tolerate a miss here. |
| 96 | + :ok |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + # --------------------------------------------------------------------------- |
| 102 | + # Record and history |
| 103 | + # --------------------------------------------------------------------------- |
| 104 | + |
| 105 | + describe "record/3 and get_history/1" do |
| 106 | + test "get_history/1 returns nil for unknown hash" do |
| 107 | + assert nil == FeedbackATron.Deduplicator.get_history("unknownhash0000000000000000000000") |
| 108 | + end |
| 109 | + |
| 110 | + test "get_history/1 returns data after recording" do |
| 111 | + issue = %{title: "History Test Issue", body: "some body for history"} |
| 112 | + FeedbackATron.Deduplicator.record(issue, :github, %{status: :submitted}) |
| 113 | + Process.sleep(50) |
| 114 | + |
| 115 | + # Compute the expected hash the same way the module does: |
| 116 | + # hash is the first 16 hex chars of SHA256(normalised_title:normalised_body) |
| 117 | + title_norm = issue.title |> String.downcase() |> String.replace(~r/[^\w\s]/, "") |> String.replace(~r/\s+/, " ") |> String.trim() |
| 118 | + body_norm = issue.body |> String.downcase() |> String.replace(~r/\s+/, " ") |> String.trim() |
| 119 | + content = "#{title_norm}:#{body_norm}" |
| 120 | + hash = :crypto.hash(:sha256, content) |> Base.encode16(case: :lower) |> binary_part(0, 16) |
| 121 | + |
| 122 | + history = FeedbackATron.Deduplicator.get_history(hash) |
| 123 | + assert history != nil |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + # --------------------------------------------------------------------------- |
| 128 | + # Statistics |
| 129 | + # --------------------------------------------------------------------------- |
| 130 | + |
| 131 | + describe "stats/0" do |
| 132 | + test "stats returns a map with expected keys" do |
| 133 | + stats = FeedbackATron.Deduplicator.stats() |
| 134 | + |
| 135 | + assert is_map(stats) |
| 136 | + assert Map.has_key?(stats, :total_submissions) |
| 137 | + assert Map.has_key?(stats, :unique_titles) |
| 138 | + assert Map.has_key?(stats, :unique_hashes) |
| 139 | + assert Map.has_key?(stats, :ets_size) |
| 140 | + end |
| 141 | + |
| 142 | + test "stats reflects zero after clear" do |
| 143 | + stats = FeedbackATron.Deduplicator.stats() |
| 144 | + assert stats.total_submissions == 0 |
| 145 | + assert stats.unique_titles == 0 |
| 146 | + assert stats.unique_hashes == 0 |
| 147 | + end |
| 148 | + |
| 149 | + test "stats increments after recording an issue" do |
| 150 | + before_stats = FeedbackATron.Deduplicator.stats() |
| 151 | + issue = %{title: "Stats Counter Issue", body: "test body for stats"} |
| 152 | + FeedbackATron.Deduplicator.record(issue, :github, %{status: :submitted}) |
| 153 | + Process.sleep(50) |
| 154 | + |
| 155 | + after_stats = FeedbackATron.Deduplicator.stats() |
| 156 | + assert after_stats.total_submissions > before_stats.total_submissions |
| 157 | + end |
| 158 | + end |
| 159 | + |
| 160 | + # --------------------------------------------------------------------------- |
| 161 | + # clear/0 |
| 162 | + # --------------------------------------------------------------------------- |
| 163 | + |
| 164 | + describe "clear/0" do |
| 165 | + test "clear/0 returns :ok" do |
| 166 | + assert :ok == FeedbackATron.Deduplicator.clear() |
| 167 | + end |
| 168 | + |
| 169 | + test "stats are zero after clear even when items were recorded" do |
| 170 | + issue = %{title: "Pre-clear Issue", body: "body before clear"} |
| 171 | + FeedbackATron.Deduplicator.record(issue, :github, %{status: :submitted}) |
| 172 | + Process.sleep(50) |
| 173 | + |
| 174 | + :ok = FeedbackATron.Deduplicator.clear() |
| 175 | + stats = FeedbackATron.Deduplicator.stats() |
| 176 | + assert stats.total_submissions == 0 |
| 177 | + end |
| 178 | + |
| 179 | + test "check returns :unique after clear even for previously seen issues" do |
| 180 | + issue = %{title: "Issue to clear", body: "body to clear"} |
| 181 | + FeedbackATron.Deduplicator.record(issue, :github, %{status: :submitted}) |
| 182 | + Process.sleep(50) |
| 183 | + :ok = FeedbackATron.Deduplicator.clear() |
| 184 | + |
| 185 | + assert {:ok, :unique} = FeedbackATron.Deduplicator.check(issue) |
| 186 | + end |
| 187 | + end |
| 188 | +end |
0 commit comments