mirror of
https://github.com/we-promise/sure.git
synced 2026-04-09 15:24:48 +00:00
* SimpleFin: metadata + merge fixes; holdings (incl. crypto) + Day Change; Sync Summary; ops rakes; lint # Conflicts: # db/schema.rb # Conflicts: # app/controllers/simplefin_items_controller.rb * fix testing * fix linting * xfix linting x2 * Review PR #267 on we-promise/sure (SimpleFin enhancements v2). Address all 15 actionable CodeRabbit comments: Add UUID validations in rakes (e.g., simplefin_unlink), swap Ruby pattern matching/loops for efficient DB queries (e.g., where LOWER(name) LIKE ?), generate docstrings for low-coverage areas (31%), consolidate routes for simplefin_items, move view logic to helpers (e.g., format_transaction_extra), strengthen tests with exact assertions/fixtures for dedup/relink failures. Also, check for overlaps with merged #262 (merchants fix): Ensure merchant creation in simplefin_entry/processor.rb aligns with new payee-based flow and MD5 IDs; add tests for edge cases like empty payees or over-merging pendings. Prioritize security (PII redaction in logs, no hardcoded secrets). * SimpleFin: address CodeRabbit comments (batch 1) - Consolidate simplefin_items routes under a single resources block; keep URLs stable - Replace inline JS with Stimulus auto-relink controller; auto-load relink modal via global modal frame - Improve a11y in relink modal by wrapping rows in labels - Harden unlink rake: default dry_run=true, UUID validation, redact PII in outputs, clearer errors - Backfill rake: default dry_run=true, UUID validation; groundwork for per-SFA counters - Fix-was-merged rake: default dry_run=true, UUID validation; clearer outputs - Idempotent transfer auto-match (find_or_create_by! + RecordNotUnique rescue) - Extract SimpleFin error tooltip assembly into helper and use it in view RuboCop: maintain 2-space indentation, spaces inside array brackets, spaces after commas, and no redundant returns * Linter noise * removed filed commited by mistake. * manual relink flow and tighten composite matching * enforce manual relink UI; fix adapter keywords; guarantee extra.simplefin hash * refactor(simplefin): extract relink service; enforce manual relink UI; tighten composite match; migration 7.2 * add provider date parser; refactor rake; move view queries; partial resilience * run balances-only import in background job. make update flow enqueue balances-only job * persists across all update redirects and initialize used_manual_ids to prevent NameError in relink candidate computation. * SimpleFin: metadata + merge fixes; holdings (incl. crypto) + Day Change; Sync Summary; ops rakes; lint * Fixed failed test after rebase. * scan_ruby fix * Calming the rabbit: Fix AccountProvider linking when accounts change Drop the legacy unique index instead of duplicating it Fix dynamic constant assignment Use fixtures consistently; avoid rescue for control flow. Replace bare rescue with explicit exception class. Move business logic out of the view. Critical: Transaction boundary excludes recompute phase, risking data loss. Inconsistency between documentation and implementation for zero-error case. Refactor to use the compute_unlinked_count helper for consistency. Fix cleanup task default: it deletes by default. Move sync stats computation to controller to avoid N+1 queries. Consolidate duplicate sync query. Clarify the intent of setting flash notice on the error path. Fix Date/Time comparison in should_be_inactive?. Move stats retrieval logic to controller. Remove duplicate Sync summary section. Remove the unnecessary sleep statement; use Capybara's built-in waiting. Add label wrappers for accessibility and consistency. * FIX SimpleFIN new account modal Now new account properly loads as a Modal, instead of new page. Fixes also form showing dashboard instead of settings page. * Remove SimpleFin legacy UI components, migrate schema, and refine linking behavior. # Conflicts: # app/helpers/settings_helper.rb * Extract SimpleFin-related logic to `prepare_show_context` helper and refactor for consistency. Adjust conditional checks and ensure controller variables are properly initialized. * Remove unused SimpleFin maps from prepare_show_context; select IDs to avoid N+1 Replace Tailwind bg-green-500 with semantic bg-success in _simplefin_panel/_provider_form Add f.label :setup_token in simplefin_items/new for a11y Remove duplicate require in AccountsControllerSimplefinCtaTest * Remove unnecessary blank lines * Reduce unnecessary changes This reduces the diff against main * Simplefin Account Setup: Display in modal This fixes an issue with the `X` dismiss button in the top right corner * Removed unnecessary comment. * removed unnecessary function. * fixed broken links * Removed unnecessary file * changed to database query * set to use UTC and gaurd against null * set dry_run=true * Fixed comment * Changed to use a database-level query * matched test name to test behavior. * Eliminate code duplication and Time.zone dependency * make final summary surface failures * lint fix * Revised timezone comment. better handle missing selectors. * sanitized LIKE wildcards * Fixed SimpleFin import to avoid “Currency can’t be blank” validation failures when providers return an empty currency string. * Added helper methods for admin and self-hosted checks * Specify exception types in rescue clauses. * Refined logic to determine transaction dates for credit accounts. * Refined stats calculation for `total_accounts` to track the maximum unique accounts per run instead of accumulating totals. * Moved `unlink_all!` logic to `SimplefinItem::Unlinking` concern and deprecated `SimplefinItem::Unlinker`. Updated related references. * Refined legacy unlinking logic, improved `current_holdings` formatting, and added ENV-based overrides for self-hosted checks. * Enhanced `unlink_all!` with explicit error handling, improved transaction safety, and refined ENV-based self-hosted checks. Adjusted exception types and cleaned up private method handling. * Improved currency assignment logic by adding fallback to `current_account` and `family` currencies. * Enhanced error tracking during SimpleFin account imports by adding categorized error buckets, limiting stored errors to the last 5, and improving `stats` calculations. * typo fix * Didn't realize rabbit was still mad... Refactored SimpleFin error handling and CTA logic: centralized duplicate detection and relink visibility into controller, improved task counters, adjusted redirect notices, and fixed form indexing. * Dang rabbit never stops... Centralized SimpleFin maps logic into `MapsHelper` concern and integrated it into relevant controllers and rake tasks. Optimized queries, reduced redundancy, and improved unlinked counts and manual account checks with batch processing. Adjusted task arguments for clarity. * Persistent rabbit. Optimized SimpleFin maps logic by implementing batch queries for manual account and unlinked count checks, reducing N+1 issues. Improved clarity of rake task argument descriptions and error messages for better usability. * Lost a commit somehow, resolved here. Refactored transaction extra details logic by introducing `build_transaction_extra_details` helper to improve clarity, reusability, and reduce view complexity. Enhanced rake tasks with strict dry-run validation and better error handling. Updated schema to allow nullable `merchant_id` and added conditional unique indexes for recurring transactions. * Refactored sensitive data redaction in `simplefin_unlink` task for recursive handling, optimized SQL sanitization in `simplefin_holdings_backfill`, improved error handling in `transactions_helper`, and streamlined day change calculation logic in `Holding` model. * Lint fix * Removed per PR comments. * Also removing per PR comment. * git commit -m "SimpleFIN polish: preserve #manual-accounts wrapper, unify \"manual\" scope, and correct unlinked counts - Preserve #manual-accounts wrapper: switch non-empty updates to turbo_stream.update and background broadcast_update_to; keep empty-path replace to render <div id=\"manual-accounts\"></div> - Unify definition of manual accounts via Account.visible_manual (visible + legacy-nil + no AccountProvider); reuse in controllers, jobs, and helper - Correct setup/unlinked counts: SimplefinItem::Syncer#finalize_setup_counts and maps now consider AccountProvider links (legacy account AND provider must be absent) Deleted: - app/models/simplefin_item/relink_service.rb - app/controllers/concerns/simplefin_items/relink_helpers.rb - app/javascript/controllers/auto_relink_controller.js - app/views/simplefin_items/_relink_modal.html.erb - app/views/simplefin_items/manual_relink.html.erb - app/views/simplefin_items/relink.html.erb - test/services/simplefin_item/relink_service_test.rb Refs: PR #318 unified link/unlink; PR #267 SimpleFIN; follow-up to fix wrapper ID loss and counting drift." * Extend unlinked account check to include "Investment" type * set SimpleFIN item for `balances`, remove redundant unpacking, and improve holdings task error * SimpleFIN: add `errors` action + modal; do not reintroduce legacy relink actions; removed dead helper * FIX simpleFIN linking * Add delay back, tests benefit from it * Put cache back in * Remove empty `rake` task * Small spelling fixes. --------- Signed-off-by: soky srm <sokysrm@gmail.com> Co-authored-by: Josh Waldrep <joshua.waldrep5+github@gmail.com> Co-authored-by: Juan José Mata <juanjo.mata@gmail.com> Co-authored-by: sokie <sokysrm@gmail.com> Co-authored-by: Dylan Corrales <deathcamel58@gmail.com>
616 lines
20 KiB
Ruby
616 lines
20 KiB
Ruby
require "test_helper"
|
|
|
|
class RecurringTransactionTest < ActiveSupport::TestCase
|
|
def setup
|
|
@family = families(:dylan_family)
|
|
@merchant = merchants(:netflix)
|
|
# Clear any existing recurring transactions
|
|
@family.recurring_transactions.destroy_all
|
|
end
|
|
|
|
test "identify_patterns_for creates recurring transactions for patterns with 3+ occurrences" do
|
|
# Create a series of transactions with same merchant and amount on similar days
|
|
# Use dates within the last 3 months: today, 1 month ago, 2 months ago
|
|
account = @family.accounts.first
|
|
[ 0, 1, 2 ].each do |months_ago|
|
|
transaction = Transaction.create!(
|
|
merchant: @merchant,
|
|
category: categories(:food_and_drink)
|
|
)
|
|
account.entries.create!(
|
|
date: months_ago.months.ago.beginning_of_month + 5.days,
|
|
amount: 15.99,
|
|
currency: "USD",
|
|
name: "Netflix Subscription",
|
|
entryable: transaction
|
|
)
|
|
end
|
|
|
|
assert_difference "@family.recurring_transactions.count", 1 do
|
|
RecurringTransaction.identify_patterns_for(@family)
|
|
end
|
|
|
|
recurring = @family.recurring_transactions.last
|
|
assert_equal @merchant, recurring.merchant
|
|
assert_equal 15.99, recurring.amount
|
|
assert_equal "USD", recurring.currency
|
|
assert_equal "active", recurring.status
|
|
assert_equal 3, recurring.occurrence_count
|
|
end
|
|
|
|
test "identify_patterns_for does not create recurring transaction for less than 3 occurrences" do
|
|
# Create only 2 transactions
|
|
account = @family.accounts.first
|
|
2.times do |i|
|
|
transaction = Transaction.create!(
|
|
merchant: @merchant,
|
|
category: categories(:food_and_drink)
|
|
)
|
|
account.entries.create!(
|
|
date: (i + 1).months.ago.beginning_of_month + 5.days,
|
|
amount: 15.99,
|
|
currency: "USD",
|
|
name: "Netflix Subscription",
|
|
entryable: transaction
|
|
)
|
|
end
|
|
|
|
assert_no_difference "@family.recurring_transactions.count" do
|
|
RecurringTransaction.identify_patterns_for(@family)
|
|
end
|
|
end
|
|
|
|
test "calculate_next_expected_date handles end of month correctly" do
|
|
recurring = @family.recurring_transactions.create!(
|
|
merchant: @merchant,
|
|
amount: 29.99,
|
|
currency: "USD",
|
|
expected_day_of_month: 31,
|
|
last_occurrence_date: Date.new(2025, 1, 31),
|
|
next_expected_date: Date.new(2025, 2, 28),
|
|
status: "active"
|
|
)
|
|
|
|
# February doesn't have 31 days, should return last day of February
|
|
next_date = recurring.calculate_next_expected_date(Date.new(2025, 1, 31))
|
|
assert_equal Date.new(2025, 2, 28), next_date
|
|
end
|
|
|
|
test "should_be_inactive? returns true when last occurrence is over 2 months ago" do
|
|
recurring = @family.recurring_transactions.create!(
|
|
merchant: merchants(:amazon),
|
|
amount: 19.99,
|
|
currency: "USD",
|
|
expected_day_of_month: 5,
|
|
last_occurrence_date: 3.months.ago.to_date,
|
|
next_expected_date: 2.months.ago.to_date,
|
|
status: "active"
|
|
)
|
|
|
|
assert recurring.should_be_inactive?
|
|
end
|
|
|
|
test "should_be_inactive? returns false when last occurrence is within 2 months" do
|
|
recurring = @family.recurring_transactions.create!(
|
|
merchant: merchants(:amazon),
|
|
amount: 25.99,
|
|
currency: "USD",
|
|
expected_day_of_month: 5,
|
|
last_occurrence_date: 1.month.ago.to_date,
|
|
next_expected_date: Date.current,
|
|
status: "active"
|
|
)
|
|
|
|
assert_not recurring.should_be_inactive?
|
|
end
|
|
|
|
test "cleanup_stale_for marks inactive when no recent occurrences" do
|
|
recurring = @family.recurring_transactions.create!(
|
|
merchant: merchants(:amazon),
|
|
amount: 35.99,
|
|
currency: "USD",
|
|
expected_day_of_month: 5,
|
|
last_occurrence_date: 3.months.ago.to_date,
|
|
next_expected_date: 2.months.ago.to_date,
|
|
status: "active"
|
|
)
|
|
|
|
RecurringTransaction.cleanup_stale_for(@family)
|
|
|
|
assert_equal "inactive", recurring.reload.status
|
|
end
|
|
|
|
test "record_occurrence! updates recurring transaction with new occurrence" do
|
|
recurring = @family.recurring_transactions.create!(
|
|
merchant: merchants(:amazon),
|
|
amount: 45.99,
|
|
currency: "USD",
|
|
expected_day_of_month: 5,
|
|
last_occurrence_date: 1.month.ago.to_date,
|
|
next_expected_date: Date.current,
|
|
status: "active",
|
|
occurrence_count: 3
|
|
)
|
|
|
|
new_date = Date.current
|
|
recurring.record_occurrence!(new_date)
|
|
|
|
assert_equal new_date, recurring.last_occurrence_date
|
|
assert_equal 4, recurring.occurrence_count
|
|
assert_equal "active", recurring.status
|
|
assert recurring.next_expected_date > new_date
|
|
end
|
|
|
|
test "identify_patterns_for preserves sign for income transactions" do
|
|
# Create recurring income transactions (negative amounts)
|
|
account = @family.accounts.first
|
|
[ 0, 1, 2 ].each do |months_ago|
|
|
transaction = Transaction.create!(
|
|
merchant: @merchant,
|
|
category: categories(:income)
|
|
)
|
|
account.entries.create!(
|
|
date: months_ago.months.ago.beginning_of_month + 15.days,
|
|
amount: -1000.00,
|
|
currency: "USD",
|
|
name: "Monthly Salary",
|
|
entryable: transaction
|
|
)
|
|
end
|
|
|
|
assert_difference "@family.recurring_transactions.count", 1 do
|
|
RecurringTransaction.identify_patterns_for(@family)
|
|
end
|
|
|
|
recurring = @family.recurring_transactions.last
|
|
assert_equal @merchant, recurring.merchant
|
|
assert_equal(-1000.00, recurring.amount)
|
|
assert recurring.amount.negative?, "Income should have negative amount"
|
|
assert_equal "USD", recurring.currency
|
|
assert_equal "active", recurring.status
|
|
end
|
|
|
|
test "identify_patterns_for creates name-based recurring transactions for transactions without merchants" do
|
|
# Create transactions without merchants (e.g., from CSV imports or standard accounts)
|
|
account = @family.accounts.first
|
|
[ 0, 1, 2 ].each do |months_ago|
|
|
transaction = Transaction.create!(
|
|
category: categories(:food_and_drink)
|
|
)
|
|
account.entries.create!(
|
|
date: months_ago.months.ago.beginning_of_month + 10.days,
|
|
amount: 25.00,
|
|
currency: "USD",
|
|
name: "Local Coffee Shop",
|
|
entryable: transaction
|
|
)
|
|
end
|
|
|
|
assert_difference "@family.recurring_transactions.count", 1 do
|
|
RecurringTransaction.identify_patterns_for(@family)
|
|
end
|
|
|
|
recurring = @family.recurring_transactions.last
|
|
assert_nil recurring.merchant
|
|
assert_equal "Local Coffee Shop", recurring.name
|
|
assert_equal 25.00, recurring.amount
|
|
assert_equal "USD", recurring.currency
|
|
assert_equal "active", recurring.status
|
|
assert_equal 3, recurring.occurrence_count
|
|
end
|
|
|
|
test "identify_patterns_for creates separate patterns for same merchant but different names" do
|
|
# Create two different recurring transactions from the same merchant
|
|
account = @family.accounts.first
|
|
|
|
# First pattern: Netflix Standard
|
|
[ 0, 1, 2 ].each do |months_ago|
|
|
transaction = Transaction.create!(
|
|
merchant: @merchant,
|
|
category: categories(:food_and_drink)
|
|
)
|
|
account.entries.create!(
|
|
date: months_ago.months.ago.beginning_of_month + 5.days,
|
|
amount: 15.99,
|
|
currency: "USD",
|
|
name: "Netflix Standard",
|
|
entryable: transaction
|
|
)
|
|
end
|
|
|
|
# Second pattern: Netflix Premium
|
|
[ 0, 1, 2 ].each do |months_ago|
|
|
transaction = Transaction.create!(
|
|
merchant: @merchant,
|
|
category: categories(:food_and_drink)
|
|
)
|
|
account.entries.create!(
|
|
date: months_ago.months.ago.beginning_of_month + 10.days,
|
|
amount: 19.99,
|
|
currency: "USD",
|
|
name: "Netflix Premium",
|
|
entryable: transaction
|
|
)
|
|
end
|
|
|
|
# Should create 2 patterns - one for each amount
|
|
assert_difference "@family.recurring_transactions.count", 2 do
|
|
RecurringTransaction.identify_patterns_for(@family)
|
|
end
|
|
end
|
|
|
|
test "matching_transactions works with name-based recurring transactions" do
|
|
# Skip when schema enforces NOT NULL merchant_id (branch-specific behavior)
|
|
unless RecurringTransaction.columns_hash["merchant_id"].null
|
|
skip "merchant_id is NOT NULL in this schema; name-based patterns disabled"
|
|
end
|
|
|
|
account = @family.accounts.first
|
|
|
|
# Create transactions for pattern
|
|
[ 0, 1, 2 ].each do |months_ago|
|
|
transaction = Transaction.create!(
|
|
category: categories(:food_and_drink)
|
|
)
|
|
account.entries.create!(
|
|
date: months_ago.months.ago.beginning_of_month + 15.days,
|
|
amount: 50.00,
|
|
currency: "USD",
|
|
name: "Gym Membership",
|
|
entryable: transaction
|
|
)
|
|
end
|
|
|
|
RecurringTransaction.identify_patterns_for(@family)
|
|
recurring = @family.recurring_transactions.last
|
|
|
|
# Verify matching transactions finds the correct entries
|
|
matches = recurring.matching_transactions
|
|
assert_equal 3, matches.size
|
|
assert matches.all? { |entry| entry.name == "Gym Membership" }
|
|
end
|
|
|
|
test "validation requires either merchant or name" do
|
|
recurring = @family.recurring_transactions.build(
|
|
amount: 25.00,
|
|
currency: "USD",
|
|
expected_day_of_month: 5,
|
|
last_occurrence_date: Date.current,
|
|
next_expected_date: 1.month.from_now.to_date
|
|
)
|
|
|
|
assert_not recurring.valid?
|
|
assert_includes recurring.errors[:base], "Either merchant or name must be present"
|
|
end
|
|
|
|
test "both merchant-based and name-based patterns can coexist" do
|
|
# Skip when schema enforces NOT NULL merchant_id (branch-specific behavior)
|
|
unless RecurringTransaction.columns_hash["merchant_id"].null
|
|
skip "merchant_id is NOT NULL in this schema; name-based patterns disabled"
|
|
end
|
|
account = @family.accounts.first
|
|
|
|
# Create merchant-based pattern
|
|
[ 0, 1, 2 ].each do |months_ago|
|
|
transaction = Transaction.create!(
|
|
merchant: @merchant,
|
|
category: categories(:food_and_drink)
|
|
)
|
|
account.entries.create!(
|
|
date: months_ago.months.ago.beginning_of_month + 5.days,
|
|
amount: 15.99,
|
|
currency: "USD",
|
|
name: "Netflix Subscription",
|
|
entryable: transaction
|
|
)
|
|
end
|
|
|
|
# Create name-based pattern (no merchant)
|
|
[ 0, 1, 2 ].each do |months_ago|
|
|
transaction = Transaction.create!(
|
|
category: categories(:one)
|
|
)
|
|
account.entries.create!(
|
|
date: months_ago.months.ago.beginning_of_month + 1.days,
|
|
amount: 1200.00,
|
|
currency: "USD",
|
|
name: "Monthly Rent",
|
|
entryable: transaction
|
|
)
|
|
end
|
|
|
|
assert_difference "@family.recurring_transactions.count", 2 do
|
|
RecurringTransaction.identify_patterns_for(@family)
|
|
end
|
|
|
|
# Verify both types exist
|
|
merchant_based = @family.recurring_transactions.where.not(merchant_id: nil).first
|
|
name_based = @family.recurring_transactions.where(merchant_id: nil).first
|
|
|
|
assert merchant_based.present?
|
|
assert_equal @merchant, merchant_based.merchant
|
|
|
|
assert name_based.present?
|
|
assert_equal "Monthly Rent", name_based.name
|
|
end
|
|
|
|
# Manual recurring transaction tests
|
|
test "create_from_transaction creates a manual recurring transaction" do
|
|
account = @family.accounts.first
|
|
transaction = Transaction.create!(
|
|
merchant: @merchant,
|
|
category: categories(:food_and_drink)
|
|
)
|
|
entry = account.entries.create!(
|
|
date: 2.months.ago,
|
|
amount: 50.00,
|
|
currency: "USD",
|
|
name: "Test Transaction",
|
|
entryable: transaction
|
|
)
|
|
|
|
recurring = nil
|
|
assert_difference "@family.recurring_transactions.count", 1 do
|
|
recurring = RecurringTransaction.create_from_transaction(transaction)
|
|
end
|
|
|
|
assert recurring.present?
|
|
assert recurring.manual?
|
|
assert_equal @merchant, recurring.merchant
|
|
assert_equal 50.00, recurring.amount
|
|
assert_equal "USD", recurring.currency
|
|
assert_equal 2.months.ago.day, recurring.expected_day_of_month
|
|
assert_equal "active", recurring.status
|
|
assert_equal 1, recurring.occurrence_count
|
|
# Next expected date should be in the future (either this month or next month)
|
|
assert recurring.next_expected_date >= Date.current
|
|
end
|
|
|
|
test "create_from_transaction automatically calculates amount variance from history" do
|
|
account = @family.accounts.first
|
|
|
|
# Create multiple historical transactions with varying amounts on the same day of month
|
|
amounts = [ 90.00, 100.00, 110.00, 120.00 ]
|
|
amounts.each_with_index do |amount, i|
|
|
transaction = Transaction.create!(
|
|
merchant: @merchant,
|
|
category: categories(:food_and_drink)
|
|
)
|
|
account.entries.create!(
|
|
date: (amounts.size - i).months.ago.beginning_of_month + 14.days, # Day 15
|
|
amount: amount,
|
|
currency: "USD",
|
|
name: "Test Transaction",
|
|
entryable: transaction
|
|
)
|
|
end
|
|
|
|
# Mark the most recent one as recurring
|
|
most_recent_entry = account.entries.order(date: :desc).first
|
|
recurring = RecurringTransaction.create_from_transaction(most_recent_entry.transaction)
|
|
|
|
assert recurring.manual?
|
|
assert_equal 90.00, recurring.expected_amount_min
|
|
assert_equal 120.00, recurring.expected_amount_max
|
|
assert_equal 105.00, recurring.expected_amount_avg # (90 + 100 + 110 + 120) / 4
|
|
assert_equal 4, recurring.occurrence_count
|
|
# Next expected date should be in the future
|
|
assert recurring.next_expected_date >= Date.current
|
|
end
|
|
|
|
test "create_from_transaction with single transaction sets fixed amount" do
|
|
account = @family.accounts.first
|
|
transaction = Transaction.create!(
|
|
merchant: @merchant,
|
|
category: categories(:food_and_drink)
|
|
)
|
|
entry = account.entries.create!(
|
|
date: 1.month.ago,
|
|
amount: 50.00,
|
|
currency: "USD",
|
|
name: "Test Transaction",
|
|
entryable: transaction
|
|
)
|
|
|
|
recurring = RecurringTransaction.create_from_transaction(transaction)
|
|
|
|
assert recurring.manual?
|
|
assert_equal 50.00, recurring.expected_amount_min
|
|
assert_equal 50.00, recurring.expected_amount_max
|
|
assert_equal 50.00, recurring.expected_amount_avg
|
|
assert_equal 1, recurring.occurrence_count
|
|
# Next expected date should be in the future
|
|
assert recurring.next_expected_date >= Date.current
|
|
end
|
|
|
|
test "matching_transactions with amount variance matches within range" do
|
|
account = @family.accounts.first
|
|
|
|
# Create manual recurring with variance for day 15 of the month
|
|
recurring = @family.recurring_transactions.create!(
|
|
merchant: @merchant,
|
|
amount: 100.00,
|
|
currency: "USD",
|
|
expected_day_of_month: 15,
|
|
last_occurrence_date: 1.month.ago,
|
|
next_expected_date: Date.current.next_month.beginning_of_month + 14.days,
|
|
status: "active",
|
|
manual: true,
|
|
expected_amount_min: 80.00,
|
|
expected_amount_max: 120.00,
|
|
expected_amount_avg: 100.00
|
|
)
|
|
|
|
# Create transactions with varying amounts on day 14 (within ±2 days of day 15)
|
|
transaction_within_range = Transaction.create!(merchant: @merchant, category: categories(:food_and_drink))
|
|
entry_within = account.entries.create!(
|
|
date: Date.current.next_month.beginning_of_month + 13.days, # Day 14
|
|
amount: 90.00,
|
|
currency: "USD",
|
|
name: "Test Transaction",
|
|
entryable: transaction_within_range
|
|
)
|
|
|
|
transaction_outside_range = Transaction.create!(merchant: @merchant, category: categories(:food_and_drink))
|
|
entry_outside = account.entries.create!(
|
|
date: Date.current.next_month.beginning_of_month + 14.days, # Day 15
|
|
amount: 150.00,
|
|
currency: "USD",
|
|
name: "Test Transaction",
|
|
entryable: transaction_outside_range
|
|
)
|
|
|
|
matches = recurring.matching_transactions
|
|
assert_includes matches, entry_within
|
|
assert_not_includes matches, entry_outside
|
|
end
|
|
|
|
test "should_be_inactive? has longer threshold for manual recurring" do
|
|
# Manual recurring - 6 months threshold
|
|
manual_recurring = @family.recurring_transactions.create!(
|
|
merchant: @merchant,
|
|
amount: 50.00,
|
|
currency: "USD",
|
|
expected_day_of_month: 15,
|
|
last_occurrence_date: 5.months.ago,
|
|
next_expected_date: 15.days.from_now,
|
|
status: "active",
|
|
manual: true
|
|
)
|
|
|
|
# Auto recurring - 2 months threshold with different amount to avoid unique constraint
|
|
auto_recurring = @family.recurring_transactions.create!(
|
|
merchant: @merchant,
|
|
amount: 60.00,
|
|
currency: "USD",
|
|
expected_day_of_month: 15,
|
|
last_occurrence_date: 3.months.ago,
|
|
next_expected_date: 15.days.from_now,
|
|
status: "active",
|
|
manual: false
|
|
)
|
|
|
|
assert_not manual_recurring.should_be_inactive?
|
|
assert auto_recurring.should_be_inactive?
|
|
end
|
|
|
|
test "update_amount_variance updates min/max/avg correctly" do
|
|
recurring = @family.recurring_transactions.create!(
|
|
merchant: @merchant,
|
|
amount: 100.00,
|
|
currency: "USD",
|
|
expected_day_of_month: 15,
|
|
last_occurrence_date: Date.current,
|
|
next_expected_date: 1.month.from_now,
|
|
status: "active",
|
|
manual: true,
|
|
occurrence_count: 1
|
|
)
|
|
|
|
# Record first occurrence with amount variance
|
|
recurring.record_occurrence!(Date.current, 100.00)
|
|
assert_equal 100.00, recurring.expected_amount_min.to_f
|
|
assert_equal 100.00, recurring.expected_amount_max.to_f
|
|
assert_equal 100.00, recurring.expected_amount_avg.to_f
|
|
|
|
# Record second occurrence with different amount
|
|
recurring.record_occurrence!(1.month.from_now, 120.00)
|
|
assert_equal 100.00, recurring.expected_amount_min.to_f
|
|
assert_equal 120.00, recurring.expected_amount_max.to_f
|
|
assert_in_delta 110.00, recurring.expected_amount_avg.to_f, 0.01
|
|
|
|
# Record third occurrence with lower amount
|
|
recurring.record_occurrence!(2.months.from_now, 90.00)
|
|
assert_equal 90.00, recurring.expected_amount_min.to_f
|
|
assert_equal 120.00, recurring.expected_amount_max.to_f
|
|
assert_in_delta 103.33, recurring.expected_amount_avg.to_f, 0.01
|
|
end
|
|
|
|
test "identify_patterns_for updates variance for manual recurring transactions" do
|
|
account = @family.accounts.first
|
|
|
|
# Create a manual recurring transaction with initial variance
|
|
manual_recurring = @family.recurring_transactions.create!(
|
|
merchant: @merchant,
|
|
amount: 50.00,
|
|
currency: "USD",
|
|
expected_day_of_month: 15,
|
|
last_occurrence_date: 3.months.ago,
|
|
next_expected_date: 1.month.from_now,
|
|
status: "active",
|
|
manual: true,
|
|
occurrence_count: 1,
|
|
expected_amount_min: 50.00,
|
|
expected_amount_max: 50.00,
|
|
expected_amount_avg: 50.00
|
|
)
|
|
|
|
# Create new transactions with varying amounts that would match the pattern
|
|
amounts = [ 45.00, 55.00, 60.00 ]
|
|
amounts.each_with_index do |amount, i|
|
|
transaction = Transaction.create!(
|
|
merchant: @merchant,
|
|
category: categories(:food_and_drink)
|
|
)
|
|
account.entries.create!(
|
|
date: (amounts.size - i).months.ago.beginning_of_month + 14.days,
|
|
amount: amount,
|
|
currency: "USD",
|
|
name: "Test Transaction",
|
|
entryable: transaction
|
|
)
|
|
end
|
|
|
|
# Run pattern identification
|
|
assert_no_difference "@family.recurring_transactions.count" do
|
|
RecurringTransaction.identify_patterns_for(@family)
|
|
end
|
|
|
|
# Manual recurring should be updated with new variance
|
|
manual_recurring.reload
|
|
assert manual_recurring.manual?
|
|
assert_equal 45.00, manual_recurring.expected_amount_min
|
|
assert_equal 60.00, manual_recurring.expected_amount_max
|
|
assert_in_delta 53.33, manual_recurring.expected_amount_avg.to_f, 0.01 # (45 + 55 + 60) / 3
|
|
assert manual_recurring.occurrence_count > 1
|
|
end
|
|
|
|
test "cleaner does not delete manual recurring transactions" do
|
|
# Create inactive manual recurring
|
|
manual_recurring = @family.recurring_transactions.create!(
|
|
merchant: @merchant,
|
|
amount: 50.00,
|
|
currency: "USD",
|
|
expected_day_of_month: 15,
|
|
last_occurrence_date: 1.year.ago,
|
|
next_expected_date: 1.year.ago + 1.month,
|
|
status: "inactive",
|
|
manual: true,
|
|
occurrence_count: 1
|
|
)
|
|
# Set updated_at to be old enough for cleanup
|
|
manual_recurring.update_column(:updated_at, 1.year.ago)
|
|
|
|
# Create inactive auto recurring with different merchant
|
|
auto_recurring = @family.recurring_transactions.create!(
|
|
merchant: merchants(:amazon),
|
|
amount: 30.00,
|
|
currency: "USD",
|
|
expected_day_of_month: 10,
|
|
last_occurrence_date: 1.year.ago,
|
|
next_expected_date: 1.year.ago + 1.month,
|
|
status: "inactive",
|
|
manual: false,
|
|
occurrence_count: 1
|
|
)
|
|
# Set updated_at to be old enough for cleanup
|
|
auto_recurring.update_column(:updated_at, 1.year.ago)
|
|
|
|
cleaner = RecurringTransaction::Cleaner.new(@family)
|
|
cleaner.remove_old_inactive_transactions
|
|
|
|
assert RecurringTransaction.exists?(manual_recurring.id)
|
|
assert_not RecurringTransaction.exists?(auto_recurring.id)
|
|
end
|
|
end
|