Files
sure/test/models/category_test.rb
T
eb498cfa0c Feature/category hierarchy and account search (#2845)
* Add parent/child category hierarchy to all category selects; add search to account select

Category selection consistency:
- DS::Select (shared component powering the main transaction form,
  transaction edit, bulk-update, and transfer category pickers) now
  indents subcategories with a corner-down-right icon, matching the
  existing transaction-row category dropdown.
- Feed DS::Select-based category pickers with Category.alphabetically_by_hierarchy
  (parent name, then parent-before-children, then own name) so children
  render directly under their parent.
- Added Category::Group.select_options, a shared helper producing
  parent-then-child ordered options (with an indent marker) for plain
  HTML <select> elements. Used by:
  - Rule builder category condition/action selects
  - Bulk 'categorize transactions' select
  - CSV/QIF import category mapping select
- Grouped the splits category combobox and the transaction search
  category filter checklist the same way, both with the
  corner-down-right indent icon used elsewhere.

Account selection:
- Added searchable: true to the account select in the new/edit
  transaction form, matching the category and merchant selects next
  to it.

* Fix SyntaxError: 'for' is a Ruby reserved keyword

Category::Group.select_options called for(categories) as a bare method
call, but Ruby parses a bare 'for' as the start of a for..in loop
statement, not a method invocation. Qualify it as self.for(categories)
to call the class method explicitly.

Verified with 'ruby -c' on all touched .rb files and ERB.new(...).src
on all touched .erb files.

* Add test coverage for category hierarchy and account search

Model-level:
- Category::GroupTest (new): for() grouping and the new select_options
  helper (order + indent labels).
- CategoryTest: alphabetically_by_hierarchy scope ordering.
- Rule::ConditionFilter::TransactionCategoryTest (new)
- Rule::ActionExecutor::SetTransactionCategoryTest (new)
- Import::CategoryMappingTest (new): grouping + 'Add as new category'
  still prepends correctly.

Controller/integration-level (asserting actual rendered HTML order):
- SplitsControllerTest: category combobox data-value ordering.
- Transactions::CategorizesControllerTest: bulk-categorize <select>
  option ordering.
- TransactionsControllerTest:
  - search filter checkbox ordering (q[categories][])
  - new-transaction DS::Select category ordering (via trigger id +
    ancestor traversal)
  - new-transaction account select renders a search box

All new/modified test files verified with 'ruby -c' (syntax) and
cross-checked fixture names, family scoping, route helpers, and field
names against the actual fixtures/routes/views. Ruby/Bundler network
access to rubygems.org is unavailable in this sandbox, so the suite
itself has not been executed — run 'bin/rails test' before merging.

* Align with design-sure conventions: keep domain logic in component, not template

Per .cursor/rules/view_conventions.mdc ('keep domain logic out of the
views'), the parent/child hierarchy check for DS::Select items belongs
in the component class, not inline in the ERB template. DS::Select
already has this exact pattern for other per-item derived properties
(color_for, icon_for, logo_for) — added child? alongside them and
updated the template to call it instead of computing it inline.

Added test/components/DS/select_test.rb (ViewComponent::TestCase,
no rendering needed) covering child? directly: subcategory objects,
root-category objects, non-hierarchical objects (merchants), and the
include_blank placeholder item.

Also did a broader pass against the design-sure .cursor/rules to confirm
the rest of this branch's changes already comply:
- Uses Current.family (never current_family) throughout
- Uses the icon() helper exclusively, never lucide_icon directly
- No new/hardcoded colors; only existing semantic Tailwind tokens
  already used elsewhere in these same files
- No changes to sure-design-system.css / application.css
- Extended existing components/partials rather than creating new ones
  where one already existed (view_conventions.mdc component-vs-partial
  guidance)
- Test additions stay in Minitest + fixtures, avoid system tests,
  and test query-method output directly (testing.mdc)

* Address CodeRabbit review: sort category groups, tighten test assertion, move grouping out of view

* Address review: fix arrow leak in rule summaries, filter panel alignment, simplify splits ordering

* fix(pages): set breadcrumbs for changelog and feedback pages (#2889)

* fix(app): set breadcrumbs for changelog and feedback pages

* feat(test): add test to assert breadcrumbs

* fix(test): remove changes

* feat(app): update breadcrumbs to use semantic nav element

* feat(test): add breadcrumb assertions to changelog and feedback pages

* fix(app): replace breadcrumb nav element with div containing data-breadcrumbs attribute

* fix ci failures

* resolved failures

* Regenerate schema.rb from migrations

* Fix test

* Remove schema dump noise

---------

Signed-off-by: Shibu M <23173570+DataEnginr@users.noreply.github.com>
Signed-off-by: Juan José Mata <juanjo.mata@gmail.com>
Co-authored-by: Claude <claude@anthropic.com>
Co-authored-by: Kenrick Tandrian <60643640+KenTandrian@users.noreply.github.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
2026-08-26 07:59:00 +02:00

202 lines
6.5 KiB
Ruby

require "test_helper"
class CategoryTest < ActiveSupport::TestCase
def setup
@family = families(:dylan_family)
end
test "replacing and destroying" do
transactions = categories(:food_and_drink).transactions.to_a
categories(:food_and_drink).replace_and_destroy!(categories(:income))
assert_equal categories(:income), transactions.map { |t| t.reload.category }.uniq.first
end
test "replacing with nil should nullify the category" do
transactions = categories(:food_and_drink).transactions.to_a
categories(:food_and_drink).replace_and_destroy!(nil)
assert_nil transactions.map { |t| t.reload.category }.uniq.first
end
test "destroying parent category preserves subcategory transaction assignments" do
parent = @family.categories.create!(
name: "Parent With Child Transactions",
color: "#000000",
lucide_icon: "folder"
)
subcategory = @family.categories.create!(
name: "Child With Transactions",
color: "#111111",
lucide_icon: "folder",
parent: parent
)
transaction = Transaction.create!(category: subcategory)
assert_difference "Category.count", -1 do
parent.destroy!
end
assert_nil subcategory.reload.parent_id
assert_equal subcategory, transaction.reload.category
end
test "invalid parent_id does not raise during validation" do
category = Category.new(
name: "Orphan Subcategory",
color: "#000000",
lucide_icon: "folder",
family: @family,
parent_id: SecureRandom.uuid
)
assert_nothing_raised { category.valid? }
assert_not category.subcategory?
assert_nil category.parent
end
test "subcategory can only be one level deep" do
category = categories(:subcategory)
error = assert_raises(ActiveRecord::RecordInvalid) do
category.subcategories.create!(name: "Invalid category", family: @family)
end
assert_equal "Validation failed: Parent can't have more than 2 levels of subcategories", error.message
end
test "all_investment_contributions_names returns all locale variants" do
names = Category.all_investment_contributions_names
assert_includes names, "Investment Contributions" # English
assert_includes names, "Contributions aux investissements" # French
assert_includes names, "Investeringsbijdragen" # Dutch
assert names.all? { |name| name.is_a?(String) }
assert_equal names, names.uniq # No duplicates
end
test "display_name localizes default category names" do
I18n.with_locale(:"zh-CN") do
assert_equal "餐饮", categories(:food_and_drink).display_name
assert_equal "未分类", Category.uncategorized.display_name
end
end
test "display_name returns default category names in english" do
I18n.with_locale(:en) do
assert_equal "Food & Drink", categories(:food_and_drink).display_name
assert_equal "Uncategorized", Category.uncategorized.display_name
end
end
test "display_name preserves custom category names" do
category = Category.new(name: "School Supplies", color: "#123456", lucide_icon: "book", family: @family)
I18n.with_locale(:"zh-CN") do
assert_equal "School Supplies", category.display_name
end
end
test "display_name_with_parent localizes default parent and child names" do
category = Category.new(
name: "Groceries",
color: "#123456",
lucide_icon: "shopping-bag",
family: @family,
parent: categories(:food_and_drink)
)
I18n.with_locale(:"zh-CN") do
assert_equal "餐饮 > 杂货", category.display_name_with_parent
end
end
test "display_name_with_parent preserves custom child names" do
category = Category.new(
name: "Coffee Beans",
color: "#123456",
lucide_icon: "coffee",
family: @family,
parent: categories(:food_and_drink)
)
I18n.with_locale(:"zh-CN") do
assert_equal "餐饮 > Coffee Beans", category.display_name_with_parent
end
end
test "should accept valid 6-digit hex colors" do
[ "#FFFFFF", "#000000", "#123456", "#ABCDEF", "#abcdef" ].each do |color|
category = Category.new(name: "Category #{color}", color: color, lucide_icon: "shapes", family: @family)
assert category.valid?, "#{color} should be valid"
end
end
test "should reject invalid colors" do
[ "invalid", "#123", "#1234567", "#GGGGGG", "red", "ffffff", "#ffff", "" ].each do |color|
category = Category.new(name: "Category #{color}", color: color, lucide_icon: "shapes", family: @family)
assert_not category.valid?, "#{color} should be invalid"
assert_includes category.errors[:color], "is invalid"
end
end
test "ids_with_transactions returns a lookup hash for categorized transactions" do
category = categories(:food_and_drink)
transaction = Transaction.create!(category: category)
Entry.create!(
account: accounts(:depository),
entryable: transaction,
name: "Lookup transaction",
date: Date.current,
amount: 10,
currency: "USD"
)
lookup = Category.ids_with_transactions(family: @family, category_ids: [ category.id, 0 ])
assert lookup.key?(category.id)
assert_not lookup.key?(0)
end
test "alphabetically_by_hierarchy orders a category's subcategories immediately after it" do
ordered = @family.categories.alphabetically_by_hierarchy.to_a
parent_index = ordered.index(categories(:food_and_drink))
child_index = ordered.index(categories(:subcategory))
assert_not_nil parent_index
assert_not_nil child_index
assert_equal parent_index + 1, child_index
end
test "recently_used_for orders by last_used_at, most recent first" do
older = categories(:income)
newer = categories(:food_and_drink)
older.update!(last_used_at: 2.days.ago)
newer.update!(last_used_at: 1.day.ago)
assert_equal [ newer, older ], Category.recently_used_for(family: @family).to_a
end
test "recently_used_for excludes categories with no usage yet" do
categories(:food_and_drink).update!(last_used_at: 1.day.ago)
assert_not_includes Category.recently_used_for(family: @family).to_a, categories(:income)
end
test "recently_used_for excludes given categories and respects limit" do
a = categories(:income)
b = categories(:food_and_drink)
c = categories(:subcategory)
a.update!(last_used_at: 3.days.ago)
b.update!(last_used_at: 2.days.ago)
c.update!(last_used_at: 1.day.ago)
result = Category.recently_used_for(family: @family, excluding: b, limit: 1)
assert_equal [ c ], result.to_a
end
end