Files
sure/test/models/assistant/function/upload_account_statement_test.rb
Claude ec38a89d0c Expose the Statement Vault to external agents over MCP
A user wants to manage patrimonial history — a document-backed record of a
family's wealth where every figure traces back to the statement it came from —
by pointing an external agent harness at Sure. That model belongs in the
harness, not in Sure: it needs numbered build deltas, golden tests and closed
periods that a mutable Postgres row cannot provide.

What Sure was missing was the seam. The Statement Vault already does most of
the work — original bytes retained, SHA-256 dedup, period detection, account
matching with a confidence score, reconciliation against ledger balances, and a
month-by-month coverage map — but it is reachable only from the web UI. An
agent could not archive a document, cite one, or check for gaps.

Adds five preview MCP tools over what already exists, plus a citation grammar
for values the agent writes:

- upload_account_statement, list_account_statements, get_account_statement,
  get_statement_coverage
- record_valuation, whose source citation is parsed rather than trusted:
  ["estimated: "] citation [" (grade: A|B|C)"]. An uncited or free-styled
  value is rejected at the write boundary instead of landing in the ledger
  looking authoritative.

link and reject are deliberately not exposed. Attaching a statement to an
account is the human's decision, and the vault UI is where it is made; the
agent reports the suggested match and stops there.

Assistant.function_classes now takes a user so preview tools stay out of the
default surface. They are hidden from tools/list and not callable by name
without the preference enabled, and the vault tools re-check the manager role
and per-account permissions, since MCP calls never pass through a controller.

Docs: the blueprint this implements, and a guide covering which side owns which
layer, the vocabulary map between the two, the monthly runbook, and the gaps
(non-user holders, non-statement documents, one value per date).

No migrations, no API endpoints, no UI.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JFDp9HhXDeswadu4cxFojn
2026-08-01 00:00:12 -07:00

115 lines
3.9 KiB
Ruby

require "test_helper"
class Assistant::Function::UploadAccountStatementTest < ActiveSupport::TestCase
setup do
@user = users(:family_admin)
@account = accounts(:depository)
@function = Assistant::Function::UploadAccountStatement.new(@user)
@content = "date,amount\n2024-01-01,1\n"
end
test "has correct name and is not strict" do
assert_equal "upload_account_statement", @function.name
assert_not @function.strict_mode?
assert_includes @function.params_schema[:required], "content_base64"
end
test "stores a statement in the vault" do
result = nil
assert_difference "AccountStatement.count", 1 do
result = @function.call(params(filename: "statement.csv"))
end
assert result[:success]
assert_not result[:duplicate]
assert_equal Digest::SHA256.hexdigest(@content), result[:statement][:content_sha256]
assert_equal "statement.csv", result[:statement][:filename]
end
test "re-uploading identical bytes returns the existing statement without creating a row" do
first = @function.call(params(filename: "statement.csv"))
assert_no_difference "AccountStatement.count" do
second = @function.call(params(filename: "different-name.csv"))
assert second[:success]
assert second[:duplicate]
assert_equal first[:statement][:id], second[:statement][:id]
end
end
test "links to an account when one is given" do
result = @function.call(params(filename: "statement.csv", account_id: @account.id))
assert result[:success]
assert_equal @account.id, result[:statement][:account][:id]
assert_equal "linked", result[:statement][:review_status]
end
test "leaves the statement unmatched when no account is given" do
result = @function.call(params(filename: "statement.csv"))
assert_equal "unmatched", result[:statement][:review_status]
assert_nil result[:statement][:account]
end
test "reports a duplicate without disclosing a statement filed against a hidden account" do
@function.call(params(filename: "statement.csv", account_id: accounts(:other_asset).id))
result = Assistant::Function::UploadAccountStatement.new(users(:family_member))
.call(params(filename: "statement.csv"))
assert result[:success]
assert result[:duplicate]
assert_equal Digest::SHA256.hexdigest(@content), result[:statement][:content_sha256]
assert_nil result[:statement][:account]
assert_nil result[:statement][:filename]
end
test "refuses a user who cannot manage the vault" do
result = Assistant::Function::UploadAccountStatement.new(family_guest).call(params(filename: "statement.csv"))
assert_not result[:success]
assert_equal "forbidden", result[:error]
end
test "rejects an unsupported file type" do
result = @function.call(params(filename: "notes.txt"))
assert_not result[:success]
assert_equal "unsupported_file_type", result[:error]
end
test "rejects content that is not base64" do
result = @function.call("filename" => "statement.csv", "content_base64" => "not base64 @@@")
assert_not result[:success]
assert_equal "invalid_content", result[:error]
end
test "rejects an unknown account_id rather than silently uploading unlinked" do
result = @function.call(params(filename: "statement.csv", account_id: SecureRandom.uuid))
assert_not result[:success]
assert_equal "account_not_found", result[:error]
end
test "rejects a file whose contents do not match its extension" do
result = @function.call(
"filename" => "statement.pdf",
"content_base64" => Base64.strict_encode64("this is not a pdf")
)
assert_not result[:success]
assert_equal "invalid_file", result[:error]
end
private
def params(filename:, account_id: nil, content: @content)
{ "filename" => filename, "content_base64" => Base64.strict_encode64(content) }.tap do |p|
p["account_id"] = account_id if account_id
end
end
end