Files
sure/test/controllers/recurring_transactions/smart_fills_controller_test.rb
T
Brandon fe0d27471d feat(bills): the bills pages, calendar feed and in-page AI helpers (#3202)
* feat(bills): the bills pages, calendar feed and in-page AI helpers

Second of three chunks carved out of #3083, stacked on the schema and domain
core. This is everything a user sees and clicks. The whole surface sits behind
the preview flag, so it is unreachable until someone opts in.

Pages, all under one nav entry:

- the pay run, a month calendar, the full bills table, and the paycheck planner
- a detail drawer per bill, with payment history, price changes and cost
  analytics
- create and edit flows for bills, subscriptions, installment plans and income

The overview marks pay periods inside the month, so a weekly paycheck no longer
reads as one undifferentiated month of bills. Markers appear only when income
actually subdivides the month, which means monthly and undeclared income render
exactly as before and there is no new setting to configure.

Navigation and design system:

- one preview-gated nav item shared by the desktop rail and the mobile bar
- DS::Sparkline for payment-history charts, replacing raw SVG in views
- status badges render through DS::Pill rather than hand-rolled spans
- the suggestions panel is a disclosure that remembers being collapsed, per
  device, the way privacy mode and the sidebar width already do
- every surface reflows to phone widths without horizontal scroll

Calendar feed: a signed ICS feed per family, served sessionless by token, with a
reset that revokes previously shared URLs.

In-page AI helpers: smart fill on the bill form and a smart configuration
proposal on an existing bill, each reading a bounded slice of charge history.
Provider-side prompt assembly sits behind the existing LlmConcept interface,
with an implementation for each of the two providers. These belong here rather
than with the assistant tools because they are buttons on these pages and lean
on the provider suggester, not on the tool registry.

Suite 7,776 runs green apart from the pre-existing passkey-session flake, which passes standalone. Rubocop clean, eager loading verified. The hosting guide for the feature ships here rather than with the schema, since its instructions walk pages this PR introduces.

* Render the suggested strip through DS::Disclosure

The hand-rolled details pair predates the component. The card_inset
variant is the same shape, so the strip now inherits the design system
chrome, and the persisted-disclosure controller rides along unchanged.

* Route the remaining hand-rolled chips through the design system

The subscription-state chips, rule-match chips and match-reason chips
become DS::Pill, with the state chips extracted to one shared partial so
the drawer and the summary tab stop carrying copy-pasted markup. The AI
prompt chips become DS::Button and the bills-index filter becomes
DS::SearchInput, both of which this PR already uses elsewhere for the
same shapes.

* Fix erb_lint whitespace offenses in bills views

* Address the post-ready review round

* Require a writable destination account and gate the feed on preview

* Reject an unresolvable declared account out loud
2026-09-02 02:08:58 +02:00

99 lines
3.6 KiB
Ruby

require "test_helper"
class RecurringTransactions::SmartFillsControllerTest < ActionDispatch::IntegrationTest
RawSuggestion = Provider::LlmConcept::BillSetupSuggestion
setup do
sign_in @user = users(:family_admin)
@user.update!(preferences: (@user.preferences || {}).merge("preview_features_enabled" => true))
@family = @user.family
@entry = accounts(:depository).entries.create!(
date: Date.current, amount: 40, currency: "USD", name: "GYM MEMBERSHIP",
entryable: Transaction.new
)
end
test "applies suggested values to the form and says so" do
stub_provider(raw(name: "Gym Membership", amount: 42.0, frequency: "weekly", confidence: 0.9,
rationale: "Weekly gaps between charges"))
post smart_fill_recurring_transactions_url(entry_id: @entry.id),
headers: { "Turbo-Frame" => "modal" }
assert_response :success
assert_match I18n.t("recurring_transactions.new.smart_fill_applied"), response.body
assert_match "Weekly gaps between charges", response.body
assert_select "input[name=?][value=?]", "recurring_transaction[name]", "Gym Membership"
assert_select "input[name=?][value=?]", "recurring_transaction[amount]", "42.0"
end
test "a provider failure keeps the plain prefill and explains" do
provider = Object.new
provider.define_singleton_method(:suggest_bill_setup) do |**|
Provider::Response.new(success?: false, data: nil, error: StandardError.new("provider down"))
end
Provider::Registry.stubs(:preferred_llm_provider).returns(provider)
post smart_fill_recurring_transactions_url(entry_id: @entry.id),
headers: { "Turbo-Frame" => "modal" }
assert_response :success
assert_match "Could not analyze the charge history", response.body
assert_select "input[name=?][value=?]", "recurring_transaction[name]", "GYM MEMBERSHIP",
{ count: 1 }, "the entry's own prefill must survive a failed suggestion"
end
test "forbidden without an LLM provider" do
Provider::Registry.stubs(:preferred_llm_provider).returns(nil)
post smart_fill_recurring_transactions_url(entry_id: @entry.id)
assert_response :forbidden
end
test "forbidden without AI consent" do
stub_provider(raw)
@user.update!(ai_enabled: false)
post smart_fill_recurring_transactions_url(entry_id: @entry.id)
assert_response :forbidden
end
test "an inaccessible entry never becomes evidence" do
stub_provider(raw(name: "Should not appear"))
hidden = accounts(:investment).entries.create!(
date: Date.current, amount: 30, currency: "USD", name: "PRIVATE FEE",
entryable: Transaction.new
)
member = users(:family_member)
member.update!(preferences: (member.preferences || {}).merge("preview_features_enabled" => true))
sign_in member
post smart_fill_recurring_transactions_url(entry_id: hidden.id),
headers: { "Turbo-Frame" => "modal" }
assert_response :success
assert_match "Could not analyze the charge history", response.body
assert_no_match "Should not appear", response.body
end
private
def raw(**overrides)
RawSuggestion.new(**{
name: nil, amount: nil, frequency: nil, day_of_month: nil, weekday: nil,
month_of_year: nil, category_name: nil, bill_type: nil, autopay: nil,
confidence: nil, rationale: nil
}.merge(overrides))
end
def stub_provider(suggestion)
provider = Object.new
provider.define_singleton_method(:suggest_bill_setup) do |**|
Provider::Response.new(success?: true, data: suggestion, error: nil)
end
Provider::Registry.stubs(:preferred_llm_provider).returns(provider)
end
end