Files
sure/test/models/assistant/function/bills_tools_schema_test.rb
T
Brandon ce92b36351 feat(bills): assistant and MCP tools for bills (#3203)
* feat(bills): assistant and MCP tools for bills

Last of three chunks carved out of #3083, stacked on the UI bundle. Exposes
bills to the builtin assistant and to MCP clients. Everything here is gated
behind preview features, so the tools are absent from tools/list until a user
opts in.

Seven tools:

- get_bills, get_bill_details and get_paycheck_plan for reads
- get_bill_audit, a deterministic review that surfaces likely duplicates, price
  changes, trials about to convert, upcoming renewals and long-overdue bills
- create_bill, update_bill and record_bill_payment for writes

Shared argument parsing, permission checks and error shapes live in
BillsSupport, so every tool answers with the same {error, hint} contract the
existing tools use, and a bad argument never aborts the turn.

The write tools mutate financial records on a model's say-so, so they refuse
rather than guess: a payment cannot exceed what its cycle still owes, a repeated
settle will not quietly close next month, an unrecognized frequency is an error
instead of a silent monthly default, and non-finite or negative amounts are
rejected before they reach the database.

The read tools say what they filtered. An empty result names the statuses that
do hold matches, the paycheck plan discloses the unconfirmed series it excluded
from spending headroom, and history and price-change windows report their real
totals rather than letting a caller sum a truncated list.

A not-found no longer returns the scoped relation's SQL, which handed any MCP
client the access-control schema for the cost of a guessed id.

The in-page AI helpers are not here. Smart fill and smart configuration are
buttons on the bills pages, so they ship with the UI bundle along with the
provider-side suggester they call.

Suite 7,854 runs, 0 failures. Rubocop clean, eager loading verified.

* Address the ready-review round

* Reject an out-of-range audit lookback out loud

* Speak the cycle remainder guard through the allocator locale
2026-09-02 07:06:13 +02:00

75 lines
2.7 KiB
Ruby

require "test_helper"
class Assistant::Function::BillsToolsSchemaTest < ActiveSupport::TestCase
BILLS_TOOLS = [
Assistant::Function::GetBills,
Assistant::Function::GetBillDetails,
Assistant::Function::GetPaycheckPlan,
Assistant::Function::GetBillAudit,
Assistant::Function::CreateBill,
Assistant::Function::UpdateBill,
Assistant::Function::RecordBillPayment
].freeze
# Strict function calling requires every declared property in `required`;
# a strict tool with an optional property is rejected wholesale by strict
# providers. Scoped to the bills tools: the pre-existing tools' strictness
# is reworked by the assistant-upgrade PR (#3064), not here.
test "strict bills tools declare every property as required" do
each_definition do |name, definition|
next unless definition[:strict]
schema = definition[:params_schema]
property_keys = schema[:properties].keys.map(&:to_s)
required_keys = Array(schema[:required]).map(&:to_s)
assert_equal property_keys.sort, required_keys.sort,
"#{name} is strict but properties #{property_keys - required_keys} are not required"
end
end
# Data-driven enums put per-family values into the schema, which breaks
# strict validators the moment a family has none (the empty-enum incident)
# and bloats every request. Bills tools use static enums only.
test "bills tool enums are static and never empty" do
each_definition do |name, definition|
definition[:params_schema][:properties].each do |key, property|
enums = [ property[:enum], property.dig(:items, :enum) ].compact
enums.each do |values|
assert values.any?, "#{name}.#{key} declares an empty enum"
end
end
end
end
# The registry is shared with the public /mcp endpoint: being listed makes a
# tool callable by external agents. Bills is a preview feature, so its tools
# ride the per-user preview flag -- present for an opted-in user, absent from
# the default set. This test documents both halves.
test "the bills tools are preview-gated in the registry" do
user_with_preview = users(:family_admin)
user_with_preview.update!(
preferences: (user_with_preview.preferences || {}).merge("preview_features_enabled" => true)
)
preview_classes = Assistant.function_classes(user_with_preview)
default_classes = Assistant.function_classes(nil)
BILLS_TOOLS.each do |tool|
assert_includes preview_classes, tool
assert_not_includes default_classes, tool
end
end
private
def each_definition
user = users(:family_admin)
BILLS_TOOLS.each do |fn_class|
fn = fn_class.new(user)
yield fn.name, fn.to_definition
end
end
end