Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

feat(transactions): align Uncategorized category filter with No merchant/Untagged pattern (#3186)

GFR4e2084b662b7
diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb
index a3218a06c4..665cb2ec22 100644
--- a/app/controllers/pages_controller.rb
+++ b/app/controllers/pages_controller.rb
@@ -264,9 +264,9 @@ class PagesController < ApplicationController
links = []
node_indices = {}
- add_node = ->(unique_key, display_name, value, percentage, color) {
+ add_node = ->(unique_key, display_name, value, percentage, color, filter_value = nil) {
node_indices[unique_key] ||= begin
- nodes << { id: unique_key, name: display_name, value: value.to_f.round(2), percentage: percentage.to_f.round(1), color: color }
+ nodes << { id: unique_key, name: display_name, filter_value: filter_value, value: value.to_f.round(2), percentage: percentage.to_f.round(1), color: color }
nodes.size - 1
end
}
@@ -375,7 +375,7 @@ class PagesController < ApplicationController
opposite_subs = all_subs.select { |s| s[:net_direction] != matching_direction }
if same_side_subs.any?
- parent_idx = add_node.call(node_key, ct.category.name, val, percentage, color)
+ parent_idx = add_node.call(node_key, ct.category.name, val, percentage, color, ct.category.filter_value)
if flow_direction == :inbound
links << { source: parent_idx, target: cash_flow_idx, value: val, color: color, percentage: percentage }
@@ -388,7 +388,7 @@ class PagesController < ApplicationController
sub_pct = val.zero? ? 0 : (sub_val / val * 100).round(1)
sub_color = sub[:category].color.presence || color
sub_key = "#{prefix}_sub_#{sub[:category].id}"
- sub_idx = add_node.call(sub_key, sub[:category].name, sub_val, sub_pct, sub_color)
+ sub_idx = add_node.call(sub_key, sub[:category].name, sub_val, sub_pct, sub_color, sub[:category].filter_value)
if flow_direction == :inbound
links << { source: sub_idx, target: parent_idx, value: sub_val, color: sub_color, percentage: sub_pct }
@@ -397,7 +397,7 @@ class PagesController < ApplicationController
end
end
else
- idx = add_node.call(node_key, ct.category.name, val, percentage, color)
+ idx = add_node.call(node_key, ct.category.name, val, percentage, color, ct.category.filter_value)
if flow_direction == :inbound
links << { source: idx, target: cash_flow_idx, value: val, color: color, percentage: percentage }
@@ -415,7 +415,7 @@ class PagesController < ApplicationController
sub_pct = total.zero? ? 0 : (sub_val / total * 100).round(1)
sub_color = sub[:category].color.presence || color
sub_key = "#{opposite_prefix}_sub_#{sub[:category].id}"
- sub_idx = add_node.call(sub_key, sub[:category].name, sub_val, sub_pct, sub_color)
+ sub_idx = add_node.call(sub_key, sub[:category].name, sub_val, sub_pct, sub_color, sub[:category].filter_value)
# Opposite direction: if parent is outbound (expense), this sub is inbound (income)
if flow_direction == :inbound
@@ -438,6 +438,7 @@ class PagesController < ApplicationController
{
id: ct.category.id,
name: ct.category.name,
+ filter_value: ct.category.filter_value,
amount: ct.total.to_f.round(2),
currency: ct.currency,
percentage: ct.weight.round(1),
diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb
index dd498fb6b4..c91f49aad2 100644
--- a/app/controllers/reports_controller.rb
+++ b/app/controllers/reports_controller.rb
@@ -397,10 +397,11 @@ class ReportsController < ApplicationController
family_currency = Current.family.currency
# Helper to initialize a category group hash
- init_category_group = ->(id, name, color, icon, type) do
+ init_category_group = ->(id, name, color, icon, type, filter_value) do
{
category_id: id,
category_name: name,
+ category_filter_value: filter_value,
category_color: color,
category_icon: icon,
type: type,
@@ -416,6 +417,7 @@ class ReportsController < ApplicationController
{
category_id: category.id,
category_name: category.name,
+ category_filter_value: category.filter_value,
category_color: category.color,
category_icon: category.lucide_icon,
total: 0,
@@ -437,16 +439,16 @@ class ReportsController < ApplicationController
# Uncategorized or Other Investments (for trades)
if is_trade
parent_key = [ :other_investments, type ]
- grouped_data[parent_key] ||= init_category_group.call(:other_investments, Category.other_investments.name, Category.other_investments.color, Category.other_investments.lucide_icon, type)
+ grouped_data[parent_key] ||= init_category_group.call(:other_investments, Category.other_investments.name, Category.other_investments.color, Category.other_investments.lucide_icon, type, Category.other_investments.name)
else
parent_key = [ :uncategorized, type ]
- grouped_data[parent_key] ||= init_category_group.call(:uncategorized, Category.uncategorized.name, Category.uncategorized.color, Category.uncategorized.lucide_icon, type)
+ grouped_data[parent_key] ||= init_category_group.call(:uncategorized, Category.uncategorized.name, Category.uncategorized.color, Category.uncategorized.lucide_icon, type, Category.uncategorized.filter_value)
end
elsif category.parent_id.present?
# This is a subcategory - group under parent
parent = category.parent
parent_key = [ parent.id, type ]
- grouped_data[parent_key] ||= init_category_group.call(parent.id, parent.name, parent.color || Category::UNCATEGORIZED_COLOR, parent.lucide_icon, type)
+ grouped_data[parent_key] ||= init_category_group.call(parent.id, parent.name, parent.color || Category::UNCATEGORIZED_COLOR, parent.lucide_icon, type, parent.filter_value)
# Add to subcategory
grouped_data[parent_key][:subcategories][category.id] ||= init_subcategory.call(category)
@@ -456,7 +458,7 @@ class ReportsController < ApplicationController
else
# This is a root category (no parent)
parent_key = [ category.id, type ]
- grouped_data[parent_key] ||= init_category_group.call(category.id, category.name, category.color || Category::UNCATEGORIZED_COLOR, category.lucide_icon, type)
+ grouped_data[parent_key] ||= init_category_group.call(category.id, category.name, category.color || Category::UNCATEGORIZED_COLOR, category.lucide_icon, type, category.filter_value)
end
grouped_data[parent_key][:count] += 1
diff --git a/app/javascript/controllers/donut_chart_controller.js b/app/javascript/controllers/donut_chart_controller.js
index 9392d94590..7f0ef24d8e 100644
--- a/app/javascript/controllers/donut_chart_controller.js
+++ b/app/javascript/controllers/donut_chart_controller.js
@@ -251,11 +251,11 @@ export default class extends Controller {
// Handles click on segment (optional, controlled by enableClick value)
#handleClick(segment) {
- if (!segment.name) return;
+ if (!segment.filter_value) return;
Turbo.visit(
buildCategoryTransactionsUrl({
- name: segment.name,
+ filterValue: segment.filter_value,
startDate: this.startDateValue,
endDate: this.endDateValue,
}),
diff --git a/app/javascript/controllers/sankey_chart_controller.js b/app/javascript/controllers/sankey_chart_controller.js
index 21d1478cf5..5a25528218 100644
--- a/app/javascript/controllers/sankey_chart_controller.js
+++ b/app/javascript/controllers/sankey_chart_controller.js
@@ -178,7 +178,7 @@ export default class extends Controller {
Turbo.visit(
buildCategoryTransactionsUrl({
- name: d.name,
+ filterValue: d.filter_value,
startDate: this.startDateValue,
endDate: this.endDateValue,
}),
diff --git a/app/javascript/utils/transactions_filter_url.mjs b/app/javascript/utils/transactions_filter_url.mjs
index b10c9b01b5..d58d925247 100644
--- a/app/javascript/utils/transactions_filter_url.mjs
+++ b/app/javascript/utils/transactions_filter_url.mjs
@@ -6,17 +6,22 @@ export function isNavigableCategoryNode(id) {
}
// Builds a relative deep link to the transactions index, filtered by a single
-// category name and (optionally) a start/end date range. Mirrors the params
-// the transactions search form submits: q[categories][], q[start_date],
-// q[end_date].
+// category and (optionally) a start/end date range. Mirrors the params the
+// transactions search form submits: q[categories][], q[start_date], q[end_date].
+//
+// filterValue must be the category's stable filter value (Category#filter_value:
+// the persisted name for a real category, or Category::UNCATEGORIZED_FILTER_VALUE
+// for the synthetic "Uncategorized" node/segment) -- not the display name, since
+// the transactions search filter no longer matches the localized "Uncategorized"
+// label.
export function buildCategoryTransactionsUrl({
- name,
+ filterValue,
startDate,
endDate,
basePath = "/transactions",
}) {
const params = new URLSearchParams();
- params.append("q[categories][]", name);
+ params.append("q[categories][]", filterValue);
if (startDate) params.append("q[start_date]", startDate);
if (endDate) params.append("q[end_date]", endDate);
return `${basePath}?${params.toString()}`;
diff --git a/app/models/assistant/function/get_transactions.rb b/app/models/assistant/function/get_transactions.rb
index f873852c46..a084947360 100644
--- a/app/models/assistant/function/get_transactions.rb
+++ b/app/models/assistant/function/get_transactions.rb
@@ -145,6 +145,21 @@ class Assistant::Function::GetTransactions < Assistant::Function
search_params = params.except("order", "page", "page_size", "sort_by")
search_params["status"] = search_params.delete("statuses") if search_params.key?("statuses")
+ # The categories filter now matches Category::UNCATEGORIZED_FILTER_VALUE, a stable
+ # sentinel, rather than the localized display name. params_schema still documents
+ # (and the AI still passes) the literal "Uncategorized", so translate it here --
+ # unless the family has a real category with that exact name, which takes priority
+ # (get_categories never lists the synthetic option, so a real category by that
+ # name is what the AI actually meant).
+ if search_params["categories"].present?
+ uncategorized_aliases = [ "Uncategorized", Category.uncategorized_name ].uniq
+ real_names_matching_alias = family.categories.where(name: uncategorized_aliases).pluck(:name)
+ search_params["categories"] = search_params["categories"].map do |name|
+ next name if real_names_matching_alias.include?(name)
+ uncategorized_aliases.include?(name) ? Category::UNCATEGORIZED_FILTER_VALUE : name
+ end
+ end
+
search = Transaction::Search.new(
family,
filters: search_params,
diff --git a/app/models/category.rb b/app/models/category.rb
index bad3148a1f..8962600ae8 100644
--- a/app/models/category.rb
+++ b/app/models/category.rb
@@ -12,9 +12,19 @@ class Category < ApplicationRecord
dependent: :nullify
belongs_to :parent, class_name: "Category", optional: true
+ # Set by .uncategorized so #filter_value can identify the synthetic instance
+ # without a locale-sensitive name comparison. Not a DB column.
+ attr_writer :synthetic_uncategorized
+
+ # Stable, non-localized filter value for the synthetic "Uncategorized" option.
+ # Using an opaque sentinel (rather than the translated display name) means a real
+ # category can never collide with it, regardless of name or locale.
+ UNCATEGORIZED_FILTER_VALUE = "__uncategorized__"
+
validates :name, :color, :lucide_icon, :family, presence: true
validates :color, format: { with: /\A#[0-9A-Fa-f]{6}\z/ }
validates :name, uniqueness: { scope: :family_id }
+ validates :name, exclusion: { in: [ UNCATEGORIZED_FILTER_VALUE ] }
validate :category_level_limit
@@ -228,7 +238,7 @@ class Category < ApplicationRecord
name: I18n.t(UNCATEGORIZED_NAME_KEY),
color: UNCATEGORIZED_COLOR,
lucide_icon: "circle-dashed"
- )
+ ).tap { |category| category.synthetic_uncategorized = true }
end
def other_investments
@@ -373,6 +383,18 @@ class Category < ApplicationRecord
!persisted? && name == I18n.t(UNCATEGORIZED_NAME_KEY)
end
+ # The value the transactions-filter checkbox submits for this category: the
+ # persisted name for a real category, or the stable sentinel for the
+ # synthetic "Uncategorized" pseudo-category returned by .uncategorized.
+ #
+ # Uses the explicit synthetic_uncategorized marker set by .uncategorized, not
+ # uncategorized? -- that predicate compares name against I18n.t in the *current*
+ # locale, so it would give the wrong answer if the instance were built under one
+ # locale and read under another.
+ def filter_value
+ @synthetic_uncategorized ? UNCATEGORIZED_FILTER_VALUE : name
+ end
+
# Predicate: is this the synthetic "Other Investments" category?
def other_investments?
!persisted? && name == I18n.t(OTHER_INVESTMENTS_NAME_KEY)
diff --git a/app/models/transaction/search.rb b/app/models/transaction/search.rb
index dabd909624..1603c20383 100644
--- a/app/models/transaction/search.rb
+++ b/app/models/transaction/search.rb
@@ -131,10 +131,8 @@ class Transaction::Search
def apply_category_filter(query, categories)
return query unless categories.present?
- # Check for "Uncategorized" in any supported locale (handles URL params in different languages)
- all_uncategorized_names = Category.all_uncategorized_names
- include_uncategorized = (categories & all_uncategorized_names).any?
- real_categories = categories - all_uncategorized_names
+ include_uncategorized = categories.include?(Category::UNCATEGORIZED_FILTER_VALUE)
+ real_categories = categories - [ Category::UNCATEGORIZED_FILTER_VALUE ]
# Get parent category IDs for the given category names
parent_category_ids = family.categories.where(name: real_categories).pluck(:id)
diff --git a/app/views/budget_categories/show.html.erb b/app/views/budget_categories/show.html.erb
index 7bec0188f0..cf86853043 100644
--- a/app/views/budget_categories/show.html.erb
+++ b/app/views/budget_categories/show.html.erb
@@ -133,7 +133,7 @@
variant: "outline",
full_width: true,
href: transactions_path(q: {
- categories: [@budget_category.name],
+ categories: [@budget_category.category.filter_value],
start_date: @budget.start_date,
end_date: @budget.end_date
}),
diff --git a/app/views/pages/dashboard/_outflows_donut.html.erb b/app/views/pages/dashboard/_outflows_donut.html.erb
index 2e430da536..07f993314f 100644
--- a/app/views/pages/dashboard/_outflows_donut.html.erb
+++ b/app/views/pages/dashboard/_outflows_donut.html.erb
@@ -90,7 +90,7 @@
<% end %>
<% if category[:clickable] != false %>
- <%= link_to transactions_path(q: { categories: [category[:name]], start_date: period.date_range.first, end_date: period.date_range.last }),
+ <%= link_to transactions_path(q: { categories: [category[:filter_value]], start_date: period.date_range.first, end_date: period.date_range.last }),
class: "flex items-center justify-between mx-3 p-3 rounded-lg cursor-pointer group gap-3",
data: {
turbo_frame: "_top",
diff --git a/app/views/reports/_category_row.html.erb b/app/views/reports/_category_row.html.erb
index fcb04e1994..2b932d1068 100644
--- a/app/views/reports/_category_row.html.erb
+++ b/app/views/reports/_category_row.html.erb
@@ -7,7 +7,7 @@
# /transactions only lists Transaction rows. Skip trade-only buckets (e.g. Other
# Investments) so the destination is not empty relative to the report row.
clickable = item[:has_transactions] && row_start_date.present? && row_end_date.present?
- transactions_href = clickable ? transactions_path(q: { categories: [ item[:category_name] ], start_date: row_start_date, end_date: row_end_date }) : nil
+ transactions_href = clickable ? transactions_path(q: { categories: [ item[:category_filter_value] ], start_date: row_start_date, end_date: row_end_date }) : nil
%>
<tr data-category="category-<%= item[:category_id] %>"
diff --git a/app/views/transactions/searches/filters/_badge.html.erb b/app/views/transactions/searches/filters/_badge.html.erb
index feec464f1e..05933e8343 100644
--- a/app/views/transactions/searches/filters/_badge.html.erb
+++ b/app/views/transactions/searches/filters/_badge.html.erb
@@ -48,6 +48,10 @@
<div class="flex items-center gap-2">
<p><%= Tag.untagged_name %></p>
</div>
+ <% elsif param_key == "categories" && param_value == Category::UNCATEGORIZED_FILTER_VALUE %>
+ <div class="flex items-center gap-2">
+ <p><%= Category.uncategorized_name %></p>
+ </div>
<% else %>
<div class="flex items-center gap-2">
<p><%= param_value %></p>
diff --git a/app/views/transactions/searches/filters/_category_filter.html.erb b/app/views/transactions/searches/filters/_category_filter.html.erb
index 44284d518b..b5e687a9d4 100644
--- a/app/views/transactions/searches/filters/_category_filter.html.erb
+++ b/app/views/transactions/searches/filters/_category_filter.html.erb
@@ -16,7 +16,7 @@
<%= form.check_box :categories,
{
multiple: true,
- checked: @q[:categories]&.include?(category.name),
+ checked: @q[:categories]&.include?(category.filter_value),
class: "checkbox checkbox--light",
data: {
category_filter_target: "checkbox",
@@ -25,9 +25,9 @@
parent_id: category.parent_id
}
},
- category.name,
+ category.filter_value,
nil %>
- <%= form.label :categories, category.display_name, value: category.name, class: "text-sm text-primary cursor-pointer flex items-center gap-1" do %>
+ <%= form.label :categories, category.display_name, value: category.filter_value, class: "text-sm text-primary cursor-pointer flex items-center gap-1" do %>
<% if is_child %>
<%= icon("corner-down-right", size: "sm") %>
<% end %>
diff --git a/test/controllers/pages_controller_test.rb b/test/controllers/pages_controller_test.rb
index 46bbfccb81..a4f367c18a 100644
--- a/test/controllers/pages_controller_test.rb
+++ b/test/controllers/pages_controller_test.rb
@@ -141,6 +141,30 @@ class PagesControllerTest < ActionDispatch::IntegrationTest
assert sankey_data.fetch("nodes").any? { |node| node.fetch("id").start_with?("expense_") }
end
+ test "dashboard sankey nodes carry a stable filter_value, including opposite-direction subcategories" do
+ parent_category = @family.categories.create!(name: "Shopping", color: "#FF5733")
+ subcategory = @family.categories.create!(name: "Rebate Program", parent: parent_category, color: "#33FF57")
+
+ # Parent nets as an expense; the subcategory nets as income (more refunded than spent),
+ # which routes it into the "opposite_subs" branch as its own standalone node.
+ create_transaction(account: @family.accounts.first, name: "Shopping trip", amount: 100, category: parent_category)
+ create_transaction(account: @family.accounts.first, name: "Rebate refund", amount: -30, category: subcategory)
+
+ get root_path
+ assert_response :ok
+
+ chart = css_select("[data-controller='sankey-chart']").first
+ sankey_data = JSON.parse(chart["data-sankey-chart-data-value"])
+ nodes = sankey_data.fetch("nodes")
+
+ opposite_node = nodes.find { |node| node.fetch("id").start_with?("income_sub_") }
+ assert_not_nil opposite_node, "expected an opposite-direction subcategory node"
+ assert_equal subcategory.name, opposite_node["filter_value"]
+
+ parent_node = nodes.find { |node| node.fetch("id") == "expense_#{parent_category.id}" }
+ assert_equal parent_category.name, parent_node["filter_value"]
+ end
+
test "dashboard renders money flow widget" do
get root_path
diff --git a/test/controllers/reports_controller_test.rb b/test/controllers/reports_controller_test.rb
index d2729540b4..eb8b4cb610 100644
--- a/test/controllers/reports_controller_test.rb
+++ b/test/controllers/reports_controller_test.rb
@@ -436,7 +436,7 @@ class ReportsControllerTest < ActionDispatch::IntegrationTest
expense_href = transactions_path(q: { categories: [ expense_category.name ], start_date: start_date, end_date: end_date })
income_href = transactions_path(q: { categories: [ income_category.name ], start_date: start_date, end_date: end_date })
- uncategorized_href = transactions_path(q: { categories: [ Category.uncategorized.name ], start_date: start_date, end_date: end_date })
+ uncategorized_href = transactions_path(q: { categories: [ Category.uncategorized.filter_value ], start_date: start_date, end_date: end_date })
assert_select "tr[data-category='category-#{expense_category.id}'] a[href=?]", expense_href, text: expense_category.name
assert_select "tr[data-category='category-#{income_category.id}'] a[href=?]", income_href, text: income_category.name
@@ -449,7 +449,7 @@ class ReportsControllerTest < ActionDispatch::IntegrationTest
assert_select "tr.group\\/category-row[data-category='category-#{expense_category.id}'] td div.relative a[class*='before:absolute'][class*='before:inset-0']"
end
- test "index uncategorized category link uses localized name that Search accepts" do
+ test "index uncategorized category link stays on the stable filter value while the displayed text is localized" do
start_date = Date.current.beginning_of_month
end_date = Date.current.end_of_month
account = @family.accounts.first
@@ -457,12 +457,11 @@ class ReportsControllerTest < ActionDispatch::IntegrationTest
@user.update!(locale: "zh-CN")
localized_name = I18n.with_locale(:"zh-CN") { Category.uncategorized.name }
- assert_includes Category.all_uncategorized_names, localized_name
get reports_path(period_type: :monthly, start_date: start_date, end_date: end_date)
assert_response :ok
- href = transactions_path(q: { categories: [ localized_name ], start_date: start_date, end_date: end_date })
+ href = transactions_path(q: { categories: [ Category::UNCATEGORIZED_FILTER_VALUE ], start_date: start_date, end_date: end_date })
assert_select "tr[data-category='category-uncategorized'] a[href=?]", href, text: localized_name
end
diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml
index 2e2aa6d82c..dfdf4800f2 100644
--- a/test/fixtures/users.yml
+++ b/test/fixtures/users.yml
@@ -9,6 +9,11 @@ empty:
password_digest: $2a$04$deKu1NIqviMgrl.4NkXcieFkAmLa33RN9Tw/R/Llr22eMd4x/o/RO
onboarded_at: <%= 3.days.ago %>
+ # Explicit created_at so this admin sorts before the other admin fixture
+ # (sso_only) in family "empty" — without it, both rows get the same
+ # fixture-load timestamp and Account#assign_default_owner's
+ # order(:created_at).first becomes non-deterministic (test/models/account_test.rb).
+ created_at: <%= 4.days.ago %>
role: admin
ai_enabled: true
show_sidebar: true
diff --git a/test/javascript/utils/transactions_filter_url_test.mjs b/test/javascript/utils/transactions_filter_url_test.mjs
index c7d2109fa0..7406a9c051 100644
--- a/test/javascript/utils/transactions_filter_url_test.mjs
+++ b/test/javascript/utils/transactions_filter_url_test.mjs
@@ -20,7 +20,7 @@ test("category nodes are navigable, structural nodes are not", () => {
test("builds a transactions deep link with category and date range", () => {
const url = buildCategoryTransactionsUrl({
- name: "Groceries",
+ filterValue: "Groceries",
startDate: "2026-05-01",
endDate: "2026-05-31",
});
@@ -32,16 +32,25 @@ test("builds a transactions deep link with category and date range", () => {
test("encodes category names with special characters", () => {
const url = buildCategoryTransactionsUrl({
- name: "Food & Drink",
+ filterValue: "Food & Drink",
startDate: "2026-05-01",
endDate: "2026-05-31",
});
assert.match(url, /q%5Bcategories%5D%5B%5D=Food\+%26\+Drink/);
});
+test("passes the Uncategorized sentinel through unchanged, not a display name", () => {
+ const url = buildCategoryTransactionsUrl({
+ filterValue: "__uncategorized__",
+ startDate: "2026-05-01",
+ endDate: "2026-05-31",
+ });
+ assert.match(url, /q%5Bcategories%5D%5B%5D=__uncategorized__/);
+});
+
test("omits date params when dates are blank", () => {
const url = buildCategoryTransactionsUrl({
- name: "Groceries",
+ filterValue: "Groceries",
startDate: "",
endDate: "",
});
diff --git a/test/models/assistant/function/get_transactions_test.rb b/test/models/assistant/function/get_transactions_test.rb
index 9d5f144826..8f965e780b 100644
--- a/test/models/assistant/function/get_transactions_test.rb
+++ b/test/models/assistant/function/get_transactions_test.rb
@@ -42,6 +42,51 @@ class Assistant::Function::GetTransactionsTest < ActiveSupport::TestCase
assert_empty result[:transactions]
end
+ test "translates the documented Uncategorized category alias to the filter sentinel" do
+ uncategorized_entry = Entry.create!(
+ account: accounts(:depository),
+ name: "AI uncategorized lookup",
+ date: Date.current,
+ amount: 42,
+ currency: "USD",
+ entryable: Transaction.new
+ )
+
+ result = @function.call("categories" => [ "Uncategorized" ])
+ result_ids = result[:transactions].map { |t| t[:id] }
+
+ assert_includes result_ids, uncategorized_entry.entryable.id
+ end
+
+ test "a real category literally named Uncategorized takes priority over the alias translation" do
+ family = @user.family
+ lookalike_category = family.categories.create!(name: "Uncategorized", color: "#123456")
+
+ lookalike_entry = Entry.create!(
+ account: accounts(:depository),
+ name: "AI lookalike category lookup",
+ date: Date.current,
+ amount: 42,
+ currency: "USD",
+ entryable: Transaction.new(category: lookalike_category)
+ )
+
+ truly_uncategorized_entry = Entry.create!(
+ account: accounts(:depository),
+ name: "AI truly uncategorized lookup",
+ date: Date.current,
+ amount: 42,
+ currency: "USD",
+ entryable: Transaction.new
+ )
+
+ result = @function.call("categories" => [ "Uncategorized" ])
+ result_ids = result[:transactions].map { |t| t[:id] }
+
+ assert_includes result_ids, lookalike_entry.entryable.id
+ assert_not_includes result_ids, truly_uncategorized_entry.entryable.id
+ end
+
test "schema no longer inlines user data enums" do
schema = @function.params_schema
diff --git a/test/models/category_test.rb b/test/models/category_test.rb
index c227b1ebc3..803e08e3d0 100644
--- a/test/models/category_test.rb
+++ b/test/models/category_test.rb
@@ -91,6 +91,30 @@ class CategoryTest < ActiveSupport::TestCase
end
end
+ test "rejects the reserved Uncategorized filter sentinel as a name" do
+ category = Category.new(name: Category::UNCATEGORIZED_FILTER_VALUE, color: "#123456", lucide_icon: "folder", family: @family)
+
+ assert_not category.valid?
+ assert_includes category.errors[:name], "is reserved"
+ end
+
+ test "filter_value returns the sentinel for the synthetic Uncategorized category and the name for real categories" do
+ assert_equal Category::UNCATEGORIZED_FILTER_VALUE, Category.uncategorized.filter_value
+ assert_equal categories(:food_and_drink).name, categories(:food_and_drink).filter_value
+ end
+
+ test "filter_value stays the sentinel even when read under a different locale than it was built in" do
+ # Regression test: filter_value must not rely on a locale-sensitive name
+ # comparison (uncategorized? checks name against I18n.t in the *current*
+ # locale), or a synthetic instance built under one locale and read under
+ # another would silently stop matching.
+ synthetic = I18n.with_locale(:"zh-CN") { Category.uncategorized }
+
+ I18n.with_locale(:en) do
+ assert_equal Category::UNCATEGORIZED_FILTER_VALUE, synthetic.filter_value
+ end
+ end
+
test "display_name preserves custom category names" do
category = Category.new(name: "School Supplies", color: "#123456", lucide_icon: "book", family: @family)
diff --git a/test/models/transaction/search_test.rb b/test/models/transaction/search_test.rb
index 00b68f260a..06cfe06028 100644
--- a/test/models/transaction/search_test.rb
+++ b/test/models/transaction/search_test.rb
@@ -120,7 +120,7 @@ class Transaction::SearchTest < ActiveSupport::TestCase
)
# Search for uncategorized transactions
- uncategorized_results = Transaction::Search.new(@family, filters: { categories: [ Category.uncategorized.name ] }).transactions_scope
+ uncategorized_results = Transaction::Search.new(@family, filters: { categories: [ Category::UNCATEGORIZED_FILTER_VALUE ] }).transactions_scope
uncategorized_ids = uncategorized_results.pluck(:id)
# Should include standard uncategorized transactions
@@ -153,7 +153,7 @@ class Transaction::SearchTest < ActiveSupport::TestCase
loan_payment = create_transaction(account: @loan_account, amount: 300, kind: "loan_payment")
investment_contribution = create_transaction(account: @checking_account, amount: 400, kind: "investment_contribution")
- uncategorized_ids = Transaction::Search.new(@family, filters: { categories: [ Category.uncategorized.name ] })
+ uncategorized_ids = Transaction::Search.new(@family, filters: { categories: [ Category::UNCATEGORIZED_FILTER_VALUE ] })
.transactions_scope.pluck(:id)
assert_includes uncategorized_ids, loan_payment.entryable.id,
@@ -172,7 +172,7 @@ class Transaction::SearchTest < ActiveSupport::TestCase
create_transaction(account: account, amount: 100, kind: kind).entryable.id
end
- filter_ids = Transaction::Search.new(@family, filters: { categories: [ Category.uncategorized.name ] })
+ filter_ids = Transaction::Search.new(@family, filters: { categories: [ Category::UNCATEGORIZED_FILTER_VALUE ] })
.transactions_scope.pluck(:id).to_set
wizard_ids = @family.entries.uncategorized_transactions.pluck(:entryable_id).to_set
@@ -203,7 +203,7 @@ class Transaction::SearchTest < ActiveSupport::TestCase
)
# Filter for only uncategorized
- results = Transaction::Search.new(@family, filters: { categories: [ Category.uncategorized.name ] }).transactions_scope
+ results = Transaction::Search.new(@family, filters: { categories: [ Category::UNCATEGORIZED_FILTER_VALUE ] }).transactions_scope
result_ids = results.pluck(:id)
# Should only include uncategorized transaction
@@ -238,7 +238,7 @@ class Transaction::SearchTest < ActiveSupport::TestCase
)
# Filter for food category + uncategorized
- results = Transaction::Search.new(@family, filters: { categories: [ "Food & Drink", Category.uncategorized.name ] }).transactions_scope
+ results = Transaction::Search.new(@family, filters: { categories: [ "Food & Drink", Category::UNCATEGORIZED_FILTER_VALUE ] }).transactions_scope
result_ids = results.pluck(:id)
# Should include both food and uncategorized
@@ -597,8 +597,9 @@ class Transaction::SearchTest < ActiveSupport::TestCase
assert_not_includes result_ids, no_match.entryable.id
end
- test "uncategorized filter returns same results across all supported locales" do
- # Create uncategorized transactions
+ test "uncategorized filter works regardless of current locale since the filter value is a stable sentinel" do
+ # The category filter now matches on the stable Category::UNCATEGORIZED_FILTER_VALUE sentinel
+ # (not the translated display name), so the current locale can no longer affect matching.
uncategorized1 = create_transaction(
account: @checking_account,
amount: 100,
@@ -611,7 +612,6 @@ class Transaction::SearchTest < ActiveSupport::TestCase
kind: "standard"
)
- # Create a categorized transaction to ensure filter is working
categorized = create_transaction(
account: @checking_account,
amount: 300,
@@ -619,75 +619,45 @@ class Transaction::SearchTest < ActiveSupport::TestCase
kind: "standard"
)
- # Get the expected count using English locale (known working case)
- I18n.with_locale(:en) do
- english_uncategorized_name = Category.uncategorized.name
- english_results = Transaction::Search.new(@family, filters: { categories: [ english_uncategorized_name ] }).transactions_scope
- @expected_count = english_results.count
- assert_equal 2, @expected_count, "English locale should return 2 uncategorized transactions"
- end
-
- # Test every supported locale returns the same count when filtering by that locale's uncategorized name
LanguagesHelper::SUPPORTED_LOCALES.each do |locale|
I18n.with_locale(locale) do
- localized_uncategorized_name = Category.uncategorized.name
- results = Transaction::Search.new(@family, filters: { categories: [ localized_uncategorized_name ] }).transactions_scope
- result_count = results.count
+ results = Transaction::Search.new(@family, filters: { categories: [ Category::UNCATEGORIZED_FILTER_VALUE ] }).transactions_scope
+ result_ids = results.pluck(:id)
- assert_equal @expected_count, result_count,
- "Locale '#{locale}' with uncategorized name '#{localized_uncategorized_name}' should return #{@expected_count} transactions but got #{result_count}"
+ assert_includes result_ids, uncategorized1.entryable.id
+ assert_includes result_ids, uncategorized2.entryable.id
+ assert_not_includes result_ids, categorized.entryable.id
end
end
end
- test "uncategorized filter works with English parameter name regardless of current locale" do
- # This tests the bug where URL contains English "Uncategorized" but user's locale is different
- # Bug: /transactions/?q[categories][]=Uncategorized fails when locale is French
+ test "a real category literally named like the localized Uncategorized label remains selectable" do
+ # Regression test: filter values are opaque sentinels (Category::UNCATEGORIZED_FILTER_VALUE), not the
+ # translated display name, so a category a user happens to name "Uncategorized" can't be misdetected
+ # as the synthetic filter option.
+ lookalike_category = @family.categories.create!(
+ name: Category.uncategorized_name,
+ color: "#123456"
+ )
- # Create uncategorized transactions
- uncategorized1 = create_transaction(
+ lookalike_transaction = create_transaction(
account: @checking_account,
amount: 100,
+ category: lookalike_category,
kind: "standard"
)
- uncategorized2 = create_transaction(
+ uncategorized_transaction = create_transaction(
account: @checking_account,
amount: 200,
kind: "standard"
)
- # Create a categorized transaction to ensure filter is working
- categorized = create_transaction(
- account: @checking_account,
- amount: 300,
- category: categories(:food_and_drink),
- kind: "standard"
- )
-
- # Get the English uncategorized name (this is what URLs typically contain)
- english_uncategorized_name = I18n.t("models.category.uncategorized", locale: :en)
-
- # Get the expected count using English locale (known working case)
- expected_count = nil
- I18n.with_locale(:en) do
- results = Transaction::Search.new(@family, filters: { categories: [ english_uncategorized_name ] }).transactions_scope
- expected_count = results.count
- assert_equal 2, expected_count, "English locale should return 2 uncategorized transactions"
- end
-
- # Test that using the English parameter name works in every supported locale
- # This catches the bug where French locale fails with English "Uncategorized" parameter
- LanguagesHelper::SUPPORTED_LOCALES.each do |locale|
- I18n.with_locale(locale) do
- # Simulate URL parameter: q[categories][]=Uncategorized (English, regardless of user's locale)
- results = Transaction::Search.new(@family, filters: { categories: [ english_uncategorized_name ] }).transactions_scope
- result_count = results.count
+ results = Transaction::Search.new(@family, filters: { categories: [ lookalike_category.name ] }).transactions_scope
+ result_ids = results.pluck(:id)
- assert_equal expected_count, result_count,
- "Locale '#{locale}' should return #{expected_count} transactions when filtering with English 'Uncategorized' parameter, but got #{result_count}"
- end
- end
+ assert_includes result_ids, lookalike_transaction.entryable.id
+ assert_not_includes result_ids, uncategorized_transaction.entryable.id
end
test "filtering for only No merchant returns only transactions without a merchant" do
diff --git a/test/system/property_test.rb b/test/system/property_test.rb
index 7e905b645a..24610146e4 100644
--- a/test/system/property_test.rb
+++ b/test/system/property_test.rb
@@ -30,8 +30,11 @@ class PropertiesEditTest < ApplicationSystemTestCase
# racing the broadcast.
def open_account_edit_dialog
3.times do
- # A prior (slow) attempt may have already opened the edit form.
- return if has_selector?("#account_accountable_attributes_subtype", wait: 0)
+ # A prior (slow) attempt may have already opened the edit form. Check
+ # the field is enabled, not just present — the select briefly exists
+ # but disabled while the form finishes hydrating, and assert_field's
+ # default matcher (used by the caller) excludes disabled fields.
+ return if has_field?("account_accountable_attributes_subtype", wait: 0)
begin
within_testid("account-menu") do
@@ -50,9 +53,9 @@ class PropertiesEditTest < ApplicationSystemTestCase
)
next
end
- return if has_selector?("#account_accountable_attributes_subtype", wait: 2)
+ return if has_field?("account_accountable_attributes_subtype", wait: 2)
end
- assert_selector "#account_accountable_attributes_subtype"
+ assert_field "account_accountable_attributes_subtype"
end
def open_new_account_modal