The personal finance app for everyone (by everyone)
Ruby
require "test_helper"
class InsightTest < ActiveSupport::TestCase
include InsightTestHelper
setup do
@insight = insights(:spending_anomaly_dining)
end
test "mark_read! transitions active insight and stamps read_at" do
assert @insight.active?
@insight.mark_read!
assert @insight.reload.read?
assert @insight.read_at.present?
end
test "mark_read! does not touch acknowledged insights" do
@insight.acknowledge!
@insight.mark_read!
assert @insight.reload.acknowledged?
assert_nil @insight.read_at
end
test "acknowledge! removes insight from visible scope" do
assert_includes Insight.visible, @insight
@insight.acknowledge!
assert_not_includes Insight.visible, @insight
assert @insight.dismissed_at.present?
end
test "unacknowledge! restores an acknowledged insight as read, not new" do
@insight.acknowledge!
@insight.unacknowledge!
assert @insight.reload.read?
assert_nil @insight.dismissed_at
assert @insight.read_at.present?
assert_includes Insight.visible, @insight
end
# A stale/replayed undo (e.g. an old toast link clicked after the insight
# has since expired or resurrected) must not force a non-acknowledged
# insight back to :read — that would wrongly pull an :expired insight back
# into view.
test "unacknowledge! is a no-op on an insight that isn't acknowledged" do
@insight.update!(status: :expired)
@insight.unacknowledge!
assert @insight.reload.expired?
end
test "duplicate dedup_key within a family is rejected" do
assert_raises ActiveRecord::RecordInvalid do
@insight.family.insights.create!(
insight_type: @insight.insight_type,
priority: "medium",
title: "Duplicate",
body: "Duplicate body",
dedup_key: @insight.dedup_key
)
end
end
test "same dedup_key is allowed across families" do
other_family = families(:empty)
assert_nothing_raised do
other_family.insights.create!(
insight_type: @insight.insight_type,
priority: "medium",
title: "Same key, other family",
body: "Body",
dedup_key: @insight.dedup_key
)
end
end
test "ordered puts high priority first, then most recent" do
high = insights(:cash_flow_warning)
assert_equal high, Insight.ordered.first
end
test "insight_type must be a known type" do
insight = Insight.new(
family: families(:empty),
insight_type: "bogus",
title: "t",
body: "b",
dedup_key: "bogus:key"
)
assert_not insight.valid?
assert insight.errors[:insight_type].any?
end
test "visible hides an insight about an account a member cannot see" do
family = families(:dylan_family)
shared = idle_cash_insight(family, accounts(:depository))
private_one = idle_cash_insight(family, accounts(:connected))
gone = idle_cash_insight(family, Struct.new(:id).new(SecureRandom.uuid))
visible = family.insights.visible
assert_includes visible, shared
assert_not_includes visible, private_one
assert_not_includes visible, gone, "an insight about a deleted account is hidden too"
assert_includes visible, @insight, "insights without an account are untouched"
assert private_one.reload.active?, "hiding writes nothing; the row returns as it was"
end
test "revoking a share hides an insight about that account right away" do
family = families(:dylan_family)
insight = idle_cash_insight(family, accounts(:depository))
accounts(:depository).unshare_with!(users(:family_member))
assert_not_includes family.insights.visible, insight
end
test "a member joining without access hides an insight about the account" do
family = families(:dylan_family)
insight = idle_cash_insight(family, accounts(:depository))
family.users.create!(email: "[email protected]", password: "password123!A", first_name: "New", last_name: "Member", role: "member")
assert_not_includes family.insights.visible, insight
end
test "an inactive member does not hide an insight" do
family = families(:dylan_family)
insight = idle_cash_insight(family, accounts(:connected))
users(:family_member).update_columns(active: false)
assert_includes family.insights.visible, insight
end
test "an insight about another family's account id is hidden" do
family = families(:dylan_family)
insight = idle_cash_insight(family, accounts(:connected))
insight.update_columns(family_id: families(:empty).id)
assert_not_includes families(:empty).insights.visible, insight
end
test "a malformed account id hides the insight instead of raising" do
family = families(:dylan_family)
insight = idle_cash_insight(family, Struct.new(:id).new("not-a-uuid"))
assert_not_includes family.insights.visible.to_a, insight
end
test "an insight about an account pending deletion is hidden" do
family = families(:dylan_family)
insight = idle_cash_insight(family, accounts(:depository))
accounts(:depository).update_columns(status: "pending_deletion")
assert_not_includes family.insights.visible, insight
end
test "an insight about a recurring series follows the series' current account" do
family = families(:dylan_family)
series = recurring_transactions(:netflix_subscription)
insight = subscription_audit_insight(family, series, account_id: series.account_id)
assert_includes family.insights.visible, insight
series.update_columns(account_id: accounts(:connected).id)
assert_not_includes family.insights.visible, insight
series.update_columns(account_id: nil)
assert_includes family.insights.visible, insight
end
test "an insight about a deleted recurring series is hidden" do
family = families(:dylan_family)
series = recurring_transactions(:netflix_subscription)
insight = subscription_audit_insight(family, series)
series.destroy!
assert_not_includes family.insights.visible, insight
end
# A total names no single account, so it carries the set it was computed
# from. Once that set is no longer the one every active member can open, the
# figures may come from an account someone lost, and the row hides until the
# next run rewrites it.
test "revoking a share hides a household total right away" do
family = families(:dylan_family)
total = household_total(family)
assert_includes family.insights.visible, total
accounts(:depository).unshare_with!(users(:family_member))
assert_not_includes family.insights.visible, total
assert_includes family.insights.visible, @insight, "a row without a digest is untouched"
end
test "restoring access shows the household total again as it was" do
family = families(:dylan_family)
total = household_total(family).tap(&:mark_read!)
before = total.reload.attributes
accounts(:depository).unshare_with!(users(:family_member))
accounts(:depository).share_with!(users(:family_member))
assert_includes family.insights.visible, total
assert_equal before, total.reload.attributes, "hiding writes nothing"
end
test "a member leaving hides a household total" do
family = families(:dylan_family)
total = household_total(family)
users(:family_member).deactivate
assert_not_includes family.insights.visible, total
end
test "a member joining hides a household total" do
family = families(:dylan_family)
total = household_total(family)
family.users.create!(email: "[email protected]", password: "password123!A", first_name: "New", last_name: "Member", role: "member")
assert_not_includes family.insights.visible, total
end
test "an access change leaves an insight about a still shared account visible" do
family = families(:dylan_family)
per_account = idle_cash_insight(family, accounts(:depository))
accounts(:credit_card).unshare_with!(users(:family_member))
assert_includes family.insights.visible, per_account
end
test "a household total is checked against its own family's accounts" do
total = household_total(families(:empty))
accounts(:depository).unshare_with!(users(:family_member))
assert_includes families(:empty).insights.visible, total
end
private
def subscription_audit_insight(family, series, account_id: nil)
family.insights.create!(
insight_type: "subscription_audit", priority: "medium", status: "active", title: "Audit", body: "body",
metadata: { "recurring_transaction_id" => series.id, "account_id" => account_id }.compact,
dedup_key: "subscription_audit:#{series.id}", generated_at: Time.current
)
end
def idle_cash_insight(family, account)
family.insights.create!(
insight_type: "idle_cash", priority: "low", status: "active", title: "Idle", body: "body",
metadata: { "account_id" => account.id }, dedup_key: "idle_cash:#{account.id}",
generated_at: Time.current
)
end
end