mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-02 20:15:22 +02:00
Transaction rules engine V1 (#1900)
* Domain model sketch
* Scaffold out rules domain
* Migrations
* Remove existing data enrichment for clean slate
* Sketch out business logic and basic tests
* Simplify rule scope building and action executions
* Get generator working again
* Basic implementation + tests
* Remove manual merchant management (rules will replace)
* Revert "Remove manual merchant management (rules will replace)"
This reverts commit 83dcbd9ff0
.
* Family and Provider merchants model
* Fix brakeman warnings
* Fix notification loader
* Update notification position
* Add Rule action and condition registries
* Rule form with compound conditions and tests
* Split out notification types, add CTA type
* Rules form builder and Stimulus controller
* Clean up rule registry domain
* Clean up rules stimulus controller
* CTA message for rule when user changes transaction category
* Fix tests
* Lint updates
* Centralize notifications in Notifiable concern
* Implement category rule prompts with auto backoff and option to disable
* Fix layout bug caused by merge conflict
* Initialize rule with correct action for category CTA
* Add rule deletions, get rules working
* Complete dynamic rule form, split Stimulus controllers by resource
* Fix failing tests
* Change test password to avoid chromium conflicts
* Update integration tests
* Centralize all test password references
* Add re-apply rule action
* Rule confirm modal
* Run migrations
* Trigger rule notification after inline category updates
* Clean up rule styles
* Basic attribute locking for rules
* Apply attribute locks on user edits
* Log data enrichments, only apply rules to unlocked attributes
* Fix merge errors
* Additional merge conflict fixes
* Form UI improvements, ignore attribute locks on manual rule application
* Batch AI auto-categorization of transactions
* Auto merchant detection, ai enrichment in batches
* Fix Plaid merchant assignments
* Plaid category matching
* Cleanup 1
* Test cleanup
* Remove stale route
* Fix desktop chat UI issues
* Fix mobile nav styling issues
This commit is contained in:
parent
8edd7ecef0
commit
297a695d0f
152 changed files with 4502 additions and 612 deletions
|
@ -84,7 +84,7 @@ class CategoriesControllerTest < ActionDispatch::IntegrationTest
|
|||
end
|
||||
|
||||
test "bootstrap" do
|
||||
assert_difference "Category.count", 10 do
|
||||
assert_difference "Category.count", 12 do
|
||||
post bootstrap_categories_url
|
||||
end
|
||||
|
||||
|
|
39
test/controllers/family_merchants_controller_test.rb
Normal file
39
test/controllers/family_merchants_controller_test.rb
Normal file
|
@ -0,0 +1,39 @@
|
|||
require "test_helper"
|
||||
|
||||
class FamilyMerchantsControllerTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
sign_in @user = users(:family_admin)
|
||||
@merchant = merchants(:netflix)
|
||||
end
|
||||
|
||||
test "index" do
|
||||
get family_merchants_path
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "new" do
|
||||
get new_family_merchant_path
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should create merchant" do
|
||||
assert_difference("FamilyMerchant.count") do
|
||||
post family_merchants_url, params: { family_merchant: { name: "new merchant", color: "#000000" } }
|
||||
end
|
||||
|
||||
assert_redirected_to family_merchants_path
|
||||
end
|
||||
|
||||
test "should update merchant" do
|
||||
patch family_merchant_url(@merchant), params: { family_merchant: { name: "new name", color: "#000000" } }
|
||||
assert_redirected_to family_merchants_path
|
||||
end
|
||||
|
||||
test "should destroy merchant" do
|
||||
assert_difference("FamilyMerchant.count", -1) do
|
||||
delete family_merchant_url(@merchant)
|
||||
end
|
||||
|
||||
assert_redirected_to family_merchants_path
|
||||
end
|
||||
end
|
|
@ -1,39 +0,0 @@
|
|||
require "test_helper"
|
||||
|
||||
class MerchantsControllerTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
sign_in @user = users(:family_admin)
|
||||
@merchant = merchants(:netflix)
|
||||
end
|
||||
|
||||
test "index" do
|
||||
get merchants_path
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "new" do
|
||||
get new_merchant_path
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should create merchant" do
|
||||
assert_difference("Merchant.count") do
|
||||
post merchants_url, params: { merchant: { name: "new merchant", color: "#000000" } }
|
||||
end
|
||||
|
||||
assert_redirected_to merchants_path
|
||||
end
|
||||
|
||||
test "should update merchant" do
|
||||
patch merchant_url(@merchant), params: { merchant: { name: "new name", color: "#000000" } }
|
||||
assert_redirected_to merchants_path
|
||||
end
|
||||
|
||||
test "should destroy merchant" do
|
||||
assert_difference("Merchant.count", -1) do
|
||||
delete merchant_url(@merchant)
|
||||
end
|
||||
|
||||
assert_redirected_to merchants_path
|
||||
end
|
||||
end
|
165
test/controllers/rules_controller_test.rb
Normal file
165
test/controllers/rules_controller_test.rb
Normal file
|
@ -0,0 +1,165 @@
|
|||
require "test_helper"
|
||||
|
||||
class RulesControllerTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
sign_in @user = users(:family_admin)
|
||||
end
|
||||
|
||||
test "should get new" do
|
||||
get new_rule_url(resource_type: "transaction")
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should get edit" do
|
||||
get edit_rule_url(rules(:one))
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
# "Set all transactions with a name like 'starbucks' and an amount between 20 and 40 to the 'food and drink' category"
|
||||
test "creates rule with nested conditions" do
|
||||
post rules_url, params: {
|
||||
rule: {
|
||||
effective_date: 30.days.ago.to_date,
|
||||
resource_type: "transaction",
|
||||
conditions_attributes: {
|
||||
"0" => {
|
||||
condition_type: "transaction_name",
|
||||
operator: "like",
|
||||
value: "starbucks"
|
||||
},
|
||||
"1" => {
|
||||
condition_type: "compound",
|
||||
operator: "and",
|
||||
sub_conditions_attributes: {
|
||||
"0" => {
|
||||
condition_type: "transaction_amount",
|
||||
operator: ">",
|
||||
value: 20
|
||||
},
|
||||
"1" => {
|
||||
condition_type: "transaction_amount",
|
||||
operator: "<",
|
||||
value: 40
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
actions_attributes: {
|
||||
"0" => {
|
||||
action_type: "set_transaction_category",
|
||||
value: categories(:food_and_drink).id
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rule = @user.family.rules.order("created_at DESC").first
|
||||
|
||||
# Rule
|
||||
assert_equal "transaction", rule.resource_type
|
||||
assert_not rule.active # Not active by default
|
||||
assert_equal 30.days.ago.to_date, rule.effective_date
|
||||
|
||||
# Conditions assertions
|
||||
assert_equal 2, rule.conditions.count
|
||||
compound_condition = rule.conditions.find { |condition| condition.condition_type == "compound" }
|
||||
assert_equal "compound", compound_condition.condition_type
|
||||
assert_equal 2, compound_condition.sub_conditions.count
|
||||
|
||||
# Actions assertions
|
||||
assert_equal 1, rule.actions.count
|
||||
assert_equal "set_transaction_category", rule.actions.first.action_type
|
||||
assert_equal categories(:food_and_drink).id, rule.actions.first.value
|
||||
|
||||
assert_redirected_to confirm_rule_url(rule)
|
||||
end
|
||||
|
||||
test "can update rule" do
|
||||
rule = rules(:one)
|
||||
|
||||
assert_difference -> { Rule.count } => 0,
|
||||
-> { Rule::Condition.count } => 1,
|
||||
-> { Rule::Action.count } => 1 do
|
||||
patch rule_url(rule), params: {
|
||||
rule: {
|
||||
active: false,
|
||||
conditions_attributes: {
|
||||
"0" => {
|
||||
id: rule.conditions.first.id,
|
||||
value: "new_value"
|
||||
},
|
||||
"1" => {
|
||||
condition_type: "transaction_amount",
|
||||
operator: ">",
|
||||
value: 100
|
||||
}
|
||||
},
|
||||
actions_attributes: {
|
||||
"0" => {
|
||||
id: rule.actions.first.id,
|
||||
value: "new_value"
|
||||
},
|
||||
"1" => {
|
||||
action_type: "set_transaction_tags",
|
||||
value: tags(:one).id
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
rule.reload
|
||||
|
||||
assert_not rule.active
|
||||
assert_equal "new_value", rule.conditions.order("created_at ASC").first.value
|
||||
assert_equal "new_value", rule.actions.order("created_at ASC").first.value
|
||||
assert_equal tags(:one).id, rule.actions.order("created_at ASC").last.value
|
||||
assert_equal "100", rule.conditions.order("created_at ASC").last.value
|
||||
|
||||
assert_redirected_to rules_url
|
||||
end
|
||||
|
||||
test "can destroy conditions and actions while editing" do
|
||||
rule = rules(:one)
|
||||
|
||||
assert_equal 1, rule.conditions.count
|
||||
assert_equal 1, rule.actions.count
|
||||
|
||||
patch rule_url(rule), params: {
|
||||
rule: {
|
||||
conditions_attributes: {
|
||||
"0" => { id: rule.conditions.first.id, _destroy: true },
|
||||
"1" => {
|
||||
condition_type: "transaction_name",
|
||||
operator: "like",
|
||||
value: "new_condition"
|
||||
}
|
||||
},
|
||||
actions_attributes: {
|
||||
"0" => { id: rule.actions.first.id, _destroy: true },
|
||||
"1" => {
|
||||
action_type: "set_transaction_tags",
|
||||
value: tags(:one).id
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert_redirected_to rules_url
|
||||
|
||||
rule.reload
|
||||
|
||||
assert_equal 1, rule.conditions.count
|
||||
assert_equal 1, rule.actions.count
|
||||
end
|
||||
|
||||
test "can destroy rule" do
|
||||
rule = rules(:one)
|
||||
|
||||
assert_difference [ "Rule.count", "Rule::Condition.count", "Rule::Action.count" ], -1 do
|
||||
delete rule_url(rule)
|
||||
end
|
||||
|
||||
assert_redirected_to rules_url
|
||||
end
|
||||
end
|
3
test/fixtures/merchants.yml
vendored
3
test/fixtures/merchants.yml
vendored
|
@ -1,13 +1,16 @@
|
|||
one:
|
||||
type: FamilyMerchant
|
||||
name: Test
|
||||
family: empty
|
||||
|
||||
netflix:
|
||||
type: FamilyMerchant
|
||||
name: Netflix
|
||||
color: "#fd7f6f"
|
||||
family: dylan_family
|
||||
|
||||
amazon:
|
||||
type: FamilyMerchant
|
||||
name: Amazon
|
||||
color: "#fd7f6f"
|
||||
family: dylan_family
|
||||
|
|
4
test/fixtures/rule/actions.yml
vendored
Normal file
4
test/fixtures/rule/actions.yml
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
one:
|
||||
rule: one
|
||||
action_type: set_transaction_category
|
||||
value: "some_category_id"
|
5
test/fixtures/rule/conditions.yml
vendored
Normal file
5
test/fixtures/rule/conditions.yml
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
one:
|
||||
rule: one
|
||||
condition_type: transaction_name
|
||||
operator: like
|
||||
value: "starbucks"
|
3
test/fixtures/rules.yml
vendored
Normal file
3
test/fixtures/rules.yml
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
one:
|
||||
family: dylan_family
|
||||
resource_type: "transaction"
|
42
test/models/family/auto_categorizer_test.rb
Normal file
42
test/models/family/auto_categorizer_test.rb
Normal file
|
@ -0,0 +1,42 @@
|
|||
require "test_helper"
|
||||
|
||||
class Family::AutoCategorizerTest < ActiveSupport::TestCase
|
||||
include EntriesTestHelper, ProviderTestHelper
|
||||
|
||||
setup do
|
||||
@family = families(:dylan_family)
|
||||
@account = @family.accounts.create!(name: "Rule test", balance: 100, currency: "USD", accountable: Depository.new)
|
||||
@llm_provider = mock
|
||||
Provider::Registry.stubs(:get_provider).with(:openai).returns(@llm_provider)
|
||||
end
|
||||
|
||||
test "auto-categorizes transactions" do
|
||||
txn1 = create_transaction(account: @account, name: "McDonalds").transaction
|
||||
txn2 = create_transaction(account: @account, name: "Amazon purchase").transaction
|
||||
txn3 = create_transaction(account: @account, name: "Netflix subscription").transaction
|
||||
|
||||
test_category = @family.categories.create!(name: "Test category")
|
||||
|
||||
provider_response = provider_success_response([
|
||||
AutoCategorization.new(transaction_id: txn1.id, category_name: test_category.name),
|
||||
AutoCategorization.new(transaction_id: txn2.id, category_name: test_category.name),
|
||||
AutoCategorization.new(transaction_id: txn3.id, category_name: nil)
|
||||
])
|
||||
|
||||
@llm_provider.expects(:auto_categorize).returns(provider_response).once
|
||||
|
||||
assert_difference "DataEnrichment.count", 2 do
|
||||
Family::AutoCategorizer.new(@family, transaction_ids: [ txn1.id, txn2.id, txn3.id ]).auto_categorize
|
||||
end
|
||||
|
||||
assert_equal test_category, txn1.reload.category
|
||||
assert_equal test_category, txn2.reload.category
|
||||
assert_nil txn3.reload.category
|
||||
|
||||
# After auto-categorization, all transactions are locked and no longer enrichable
|
||||
assert_equal 0, @account.transactions.reload.enrichable(:category_id).count
|
||||
end
|
||||
|
||||
private
|
||||
AutoCategorization = Provider::LlmConcept::AutoCategorization
|
||||
end
|
42
test/models/family/auto_merchant_detector_test.rb
Normal file
42
test/models/family/auto_merchant_detector_test.rb
Normal file
|
@ -0,0 +1,42 @@
|
|||
require "test_helper"
|
||||
|
||||
class Family::AutoMerchantDetectorTest < ActiveSupport::TestCase
|
||||
include EntriesTestHelper, ProviderTestHelper
|
||||
|
||||
setup do
|
||||
@family = families(:dylan_family)
|
||||
@account = @family.accounts.create!(name: "Rule test", balance: 100, currency: "USD", accountable: Depository.new)
|
||||
@llm_provider = mock
|
||||
Provider::Registry.stubs(:get_provider).with(:openai).returns(@llm_provider)
|
||||
end
|
||||
|
||||
test "auto detects transaction merchants" do
|
||||
txn1 = create_transaction(account: @account, name: "McDonalds").transaction
|
||||
txn2 = create_transaction(account: @account, name: "Chipotle").transaction
|
||||
txn3 = create_transaction(account: @account, name: "generic").transaction
|
||||
|
||||
provider_response = provider_success_response([
|
||||
AutoDetectedMerchant.new(transaction_id: txn1.id, business_name: "McDonalds", business_url: "mcdonalds.com"),
|
||||
AutoDetectedMerchant.new(transaction_id: txn2.id, business_name: "Chipotle", business_url: "chipotle.com"),
|
||||
AutoDetectedMerchant.new(transaction_id: txn3.id, business_name: nil, business_url: nil)
|
||||
])
|
||||
|
||||
@llm_provider.expects(:auto_detect_merchants).returns(provider_response).once
|
||||
|
||||
assert_difference "DataEnrichment.count", 2 do
|
||||
Family::AutoMerchantDetector.new(@family, transaction_ids: [ txn1.id, txn2.id, txn3.id ]).auto_detect
|
||||
end
|
||||
|
||||
assert_equal "McDonalds", txn1.reload.merchant.name
|
||||
assert_equal "Chipotle", txn2.reload.merchant.name
|
||||
assert_equal "https://logo.synthfinance.com/mcdonalds.com", txn1.reload.merchant.logo_url
|
||||
assert_equal "https://logo.synthfinance.com/chipotle.com", txn2.reload.merchant.logo_url
|
||||
assert_nil txn3.reload.merchant
|
||||
|
||||
# After auto-detection, all transactions are locked and no longer enrichable
|
||||
assert_equal 0, @account.transactions.reload.enrichable(:merchant_id).count
|
||||
end
|
||||
|
||||
private
|
||||
AutoDetectedMerchant = Provider::LlmConcept::AutoDetectedMerchant
|
||||
end
|
|
@ -17,6 +17,103 @@ class Provider::OpenaiTest < ActiveSupport::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
test "auto categorizes transactions by various attributes" do
|
||||
VCR.use_cassette("openai/auto_categorize") do
|
||||
input_transactions = [
|
||||
{ id: "1", name: "McDonalds", amount: 20, classification: "expense", merchant: "McDonalds", hint: "Fast Food" },
|
||||
{ id: "2", name: "Amazon purchase", amount: 100, classification: "expense", merchant: "Amazon" },
|
||||
{ id: "3", name: "Netflix subscription", amount: 10, classification: "expense", merchant: "Netflix", hint: "Subscriptions" },
|
||||
{ id: "4", name: "paycheck", amount: 3000, classification: "income" },
|
||||
{ id: "5", name: "Italian dinner with friends", amount: 100, classification: "expense" },
|
||||
{ id: "6", name: "1212XXXBCaaa charge", amount: 2.99, classification: "expense" }
|
||||
]
|
||||
|
||||
response = @subject.auto_categorize(
|
||||
transactions: input_transactions,
|
||||
user_categories: [
|
||||
{ id: "shopping_id", name: "Shopping", is_subcategory: false, parent_id: nil, classification: "expense" },
|
||||
{ id: "subscriptions_id", name: "Subscriptions", is_subcategory: true, parent_id: nil, classification: "expense" },
|
||||
{ id: "restaurants_id", name: "Restaurants", is_subcategory: false, parent_id: nil, classification: "expense" },
|
||||
{ id: "fast_food_id", name: "Fast Food", is_subcategory: true, parent_id: "restaurants_id", classification: "expense" },
|
||||
{ id: "income_id", name: "Income", is_subcategory: false, parent_id: nil, classification: "income" }
|
||||
]
|
||||
)
|
||||
|
||||
assert response.success?
|
||||
assert_equal input_transactions.size, response.data.size
|
||||
|
||||
txn1 = response.data.find { |c| c.transaction_id == "1" }
|
||||
txn2 = response.data.find { |c| c.transaction_id == "2" }
|
||||
txn3 = response.data.find { |c| c.transaction_id == "3" }
|
||||
txn4 = response.data.find { |c| c.transaction_id == "4" }
|
||||
txn5 = response.data.find { |c| c.transaction_id == "5" }
|
||||
txn6 = response.data.find { |c| c.transaction_id == "6" }
|
||||
|
||||
assert_equal "Fast Food", txn1.category_name
|
||||
assert_equal "Shopping", txn2.category_name
|
||||
assert_equal "Subscriptions", txn3.category_name
|
||||
assert_equal "Income", txn4.category_name
|
||||
assert_equal "Restaurants", txn5.category_name
|
||||
assert_nil txn6.category_name
|
||||
end
|
||||
end
|
||||
|
||||
test "auto detects merchants" do
|
||||
VCR.use_cassette("openai/auto_detect_merchants") do
|
||||
input_transactions = [
|
||||
{ id: "1", name: "McDonalds", amount: 20, classification: "expense" },
|
||||
{ id: "2", name: "local pub", amount: 20, classification: "expense" },
|
||||
{ id: "3", name: "WMT purchases", amount: 20, classification: "expense" },
|
||||
{ id: "4", name: "amzn 123 abc", amount: 20, classification: "expense" },
|
||||
{ id: "5", name: "chaseX1231", amount: 2000, classification: "income" },
|
||||
{ id: "6", name: "check deposit 022", amount: 200, classification: "income" },
|
||||
{ id: "7", name: "shooters bar and grill", amount: 200, classification: "expense" },
|
||||
{ id: "8", name: "Microsoft Office subscription", amount: 200, classification: "expense" }
|
||||
]
|
||||
|
||||
response = @subject.auto_detect_merchants(
|
||||
transactions: input_transactions,
|
||||
user_merchants: [ { name: "Shooters" } ]
|
||||
)
|
||||
|
||||
assert response.success?
|
||||
assert_equal input_transactions.size, response.data.size
|
||||
|
||||
txn1 = response.data.find { |c| c.transaction_id == "1" }
|
||||
txn2 = response.data.find { |c| c.transaction_id == "2" }
|
||||
txn3 = response.data.find { |c| c.transaction_id == "3" }
|
||||
txn4 = response.data.find { |c| c.transaction_id == "4" }
|
||||
txn5 = response.data.find { |c| c.transaction_id == "5" }
|
||||
txn6 = response.data.find { |c| c.transaction_id == "6" }
|
||||
txn7 = response.data.find { |c| c.transaction_id == "7" }
|
||||
txn8 = response.data.find { |c| c.transaction_id == "8" }
|
||||
|
||||
assert_equal "McDonald's", txn1.business_name
|
||||
assert_equal "mcdonalds.com", txn1.business_url
|
||||
|
||||
assert_nil txn2.business_name
|
||||
assert_nil txn2.business_url
|
||||
|
||||
assert_equal "Walmart", txn3.business_name
|
||||
assert_equal "walmart.com", txn3.business_url
|
||||
|
||||
assert_equal "Amazon", txn4.business_name
|
||||
assert_equal "amazon.com", txn4.business_url
|
||||
|
||||
assert_nil txn5.business_name
|
||||
assert_nil txn5.business_url
|
||||
|
||||
assert_nil txn6.business_name
|
||||
assert_nil txn6.business_url
|
||||
|
||||
assert_equal "Shooters", txn7.business_name
|
||||
assert_nil txn7.business_url
|
||||
|
||||
assert_equal "Microsoft", txn8.business_name
|
||||
assert_equal "microsoft.com", txn8.business_url
|
||||
end
|
||||
end
|
||||
|
||||
test "basic chat response" do
|
||||
VCR.use_cassette("openai/chat/basic_response") do
|
||||
response = @subject.chat_response(
|
||||
|
|
136
test/models/provider/plaid/category_alias_matcher_test.rb
Normal file
136
test/models/provider/plaid/category_alias_matcher_test.rb
Normal file
|
@ -0,0 +1,136 @@
|
|||
require "test_helper"
|
||||
|
||||
class Provider::Plaid::CategoryAliasMatcherTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@family = families(:empty)
|
||||
|
||||
# User income categories
|
||||
@income = @family.categories.create!(name: "Income", classification: "income")
|
||||
@dividend_income = @family.categories.create!(name: "Dividend Income", parent: @income, classification: "income")
|
||||
@interest_income = @family.categories.create!(name: "Interest Income", parent: @income, classification: "income")
|
||||
|
||||
# User expense categories
|
||||
@loan_payments = @family.categories.create!(name: "Loan Payments")
|
||||
@fees = @family.categories.create!(name: "Fees")
|
||||
@entertainment = @family.categories.create!(name: "Entertainment")
|
||||
|
||||
@food_and_drink = @family.categories.create!(name: "Food & Drink")
|
||||
@groceries = @family.categories.create!(name: "Groceries", parent: @food_and_drink)
|
||||
@restaurant = @family.categories.create!(name: "Restaurant", parent: @food_and_drink)
|
||||
|
||||
@shopping = @family.categories.create!(name: "Shopping")
|
||||
@clothing = @family.categories.create!(name: "Clothing", parent: @shopping)
|
||||
|
||||
@home = @family.categories.create!(name: "Home")
|
||||
@medical = @family.categories.create!(name: "Medical")
|
||||
@personal_care = @family.categories.create!(name: "Personal Care")
|
||||
@transportation = @family.categories.create!(name: "Transportation")
|
||||
@trips = @family.categories.create!(name: "Trips")
|
||||
|
||||
@services = @family.categories.create!(name: "Services")
|
||||
@car = @family.categories.create!(name: "Car", parent: @services)
|
||||
|
||||
@giving = @family.categories.create!(name: "Giving")
|
||||
|
||||
@matcher = Provider::Plaid::CategoryAliasMatcher.new(@family.categories)
|
||||
end
|
||||
|
||||
test "matches expense categories" do
|
||||
assert_equal @loan_payments, @matcher.match("loan_payments_car_payment")
|
||||
assert_equal @loan_payments, @matcher.match("loan_payments_credit_card_payment")
|
||||
assert_equal @loan_payments, @matcher.match("loan_payments_personal_loan_payment")
|
||||
assert_equal @loan_payments, @matcher.match("loan_payments_mortgage_payment")
|
||||
assert_equal @loan_payments, @matcher.match("loan_payments_student_loan_payment")
|
||||
assert_equal @loan_payments, @matcher.match("loan_payments_other_payment")
|
||||
assert_equal @fees, @matcher.match("bank_fees_atm_fees")
|
||||
assert_equal @fees, @matcher.match("bank_fees_foreign_transaction_fees")
|
||||
assert_equal @fees, @matcher.match("bank_fees_insufficient_funds")
|
||||
assert_equal @fees, @matcher.match("bank_fees_interest_charge")
|
||||
assert_equal @fees, @matcher.match("bank_fees_overdraft_fees")
|
||||
assert_equal @fees, @matcher.match("bank_fees_other_bank_fees")
|
||||
assert_equal @entertainment, @matcher.match("entertainment_casinos_and_gambling")
|
||||
assert_equal @entertainment, @matcher.match("entertainment_music_and_audio")
|
||||
assert_equal @entertainment, @matcher.match("entertainment_sporting_events_amusement_parks_and_museums")
|
||||
assert_equal @entertainment, @matcher.match("entertainment_tv_and_movies")
|
||||
assert_equal @entertainment, @matcher.match("entertainment_video_games")
|
||||
assert_equal @entertainment, @matcher.match("entertainment_other_entertainment")
|
||||
assert_equal @food_and_drink, @matcher.match("food_and_drink_beer_wine_and_liquor")
|
||||
assert_equal @food_and_drink, @matcher.match("food_and_drink_coffee")
|
||||
assert_equal @food_and_drink, @matcher.match("food_and_drink_fast_food")
|
||||
assert_equal @groceries, @matcher.match("food_and_drink_groceries")
|
||||
assert_equal @restaurant, @matcher.match("food_and_drink_restaurant")
|
||||
assert_equal @food_and_drink, @matcher.match("food_and_drink_vending_machines")
|
||||
assert_equal @food_and_drink, @matcher.match("food_and_drink_other_food_and_drink")
|
||||
assert_equal @shopping, @matcher.match("general_merchandise_bookstores_and_newsstands")
|
||||
assert_equal @clothing, @matcher.match("general_merchandise_clothing_and_accessories")
|
||||
assert_equal @shopping, @matcher.match("general_merchandise_convenience_stores")
|
||||
assert_equal @shopping, @matcher.match("general_merchandise_department_stores")
|
||||
assert_equal @shopping, @matcher.match("general_merchandise_discount_stores")
|
||||
assert_equal @shopping, @matcher.match("general_merchandise_electronics")
|
||||
assert_equal @shopping, @matcher.match("general_merchandise_gifts_and_novelties")
|
||||
assert_equal @shopping, @matcher.match("general_merchandise_office_supplies")
|
||||
assert_equal @shopping, @matcher.match("general_merchandise_online_marketplaces")
|
||||
assert_equal @shopping, @matcher.match("general_merchandise_pet_supplies")
|
||||
assert_equal @shopping, @matcher.match("general_merchandise_sporting_goods")
|
||||
assert_equal @shopping, @matcher.match("general_merchandise_superstores")
|
||||
assert_equal @shopping, @matcher.match("general_merchandise_tobacco_and_vape")
|
||||
assert_equal @shopping, @matcher.match("general_merchandise_other_general_merchandise")
|
||||
assert_equal @home, @matcher.match("home_improvement_furniture")
|
||||
assert_equal @home, @matcher.match("home_improvement_hardware")
|
||||
assert_equal @home, @matcher.match("home_improvement_repair_and_maintenance")
|
||||
assert_equal @home, @matcher.match("home_improvement_security")
|
||||
assert_equal @home, @matcher.match("home_improvement_other_home_improvement")
|
||||
assert_equal @medical, @matcher.match("medical_dental_care")
|
||||
assert_equal @medical, @matcher.match("medical_eye_care")
|
||||
assert_equal @medical, @matcher.match("medical_nursing_care")
|
||||
assert_equal @medical, @matcher.match("medical_pharmacies_and_supplements")
|
||||
assert_equal @medical, @matcher.match("medical_primary_care")
|
||||
assert_equal @medical, @matcher.match("medical_veterinary_services")
|
||||
assert_equal @medical, @matcher.match("medical_other_medical")
|
||||
assert_equal @personal_care, @matcher.match("personal_care_gyms_and_fitness_centers")
|
||||
assert_equal @personal_care, @matcher.match("personal_care_hair_and_beauty")
|
||||
assert_equal @personal_care, @matcher.match("personal_care_laundry_and_dry_cleaning")
|
||||
assert_equal @personal_care, @matcher.match("personal_care_other_personal_care")
|
||||
assert_equal @services, @matcher.match("general_services_accounting_and_financial_planning")
|
||||
assert_equal @car, @matcher.match("general_services_automotive")
|
||||
assert_equal @services, @matcher.match("general_services_childcare")
|
||||
assert_equal @services, @matcher.match("general_services_consulting_and_legal")
|
||||
assert_equal @services, @matcher.match("general_services_education")
|
||||
assert_equal @services, @matcher.match("general_services_insurance")
|
||||
assert_equal @services, @matcher.match("general_services_postage_and_shipping")
|
||||
assert_equal @services, @matcher.match("general_services_storage")
|
||||
assert_equal @services, @matcher.match("general_services_other_general_services")
|
||||
assert_equal @giving, @matcher.match("government_and_non_profit_donations")
|
||||
assert_nil @matcher.match("government_and_non_profit_government_departments_and_agencies")
|
||||
assert_nil @matcher.match("government_and_non_profit_tax_payment")
|
||||
assert_nil @matcher.match("government_and_non_profit_other_government_and_non_profit")
|
||||
assert_equal @transportation, @matcher.match("transportation_bikes_and_scooters")
|
||||
assert_equal @transportation, @matcher.match("transportation_gas")
|
||||
assert_equal @transportation, @matcher.match("transportation_parking")
|
||||
assert_equal @transportation, @matcher.match("transportation_public_transit")
|
||||
assert_equal @transportation, @matcher.match("transportation_taxis_and_ride_shares")
|
||||
assert_equal @transportation, @matcher.match("transportation_tolls")
|
||||
assert_equal @transportation, @matcher.match("transportation_other_transportation")
|
||||
assert_equal @trips, @matcher.match("travel_flights")
|
||||
assert_equal @trips, @matcher.match("travel_lodging")
|
||||
assert_equal @trips, @matcher.match("travel_rental_cars")
|
||||
assert_equal @trips, @matcher.match("travel_other_travel")
|
||||
assert_equal @home, @matcher.match("rent_and_utilities_gas_and_electricity")
|
||||
assert_equal @home, @matcher.match("rent_and_utilities_internet_and_cable")
|
||||
assert_equal @home, @matcher.match("rent_and_utilities_rent")
|
||||
assert_equal @home, @matcher.match("rent_and_utilities_sewage_and_waste_management")
|
||||
assert_equal @home, @matcher.match("rent_and_utilities_telephone")
|
||||
assert_equal @home, @matcher.match("rent_and_utilities_water")
|
||||
assert_equal @home, @matcher.match("rent_and_utilities_other_utilities")
|
||||
end
|
||||
|
||||
test "matches income categories" do
|
||||
assert_equal @dividend_income, @matcher.match("income_dividends")
|
||||
assert_equal @interest_income, @matcher.match("income_interest_earned")
|
||||
assert_equal @income, @matcher.match("income_tax_refund")
|
||||
assert_equal @income, @matcher.match("income_retirement_pension")
|
||||
assert_equal @income, @matcher.match("income_unemployment")
|
||||
assert_equal @income, @matcher.match("income_wages")
|
||||
assert_equal @income, @matcher.match("income_other_income")
|
||||
end
|
||||
end
|
|
@ -23,21 +23,4 @@ class Provider::SynthTest < ActiveSupport::TestCase
|
|||
assert usage.plan.present?
|
||||
end
|
||||
end
|
||||
|
||||
test "enriches transaction" do
|
||||
VCR.use_cassette("synth/transaction_enrich") do
|
||||
response = @synth.enrich_transaction(
|
||||
"UBER EATS",
|
||||
amount: 25.50,
|
||||
date: Date.iso8601("2025-03-16"),
|
||||
city: "San Francisco",
|
||||
state: "CA",
|
||||
country: "US"
|
||||
)
|
||||
|
||||
data = response.data
|
||||
assert data.name.present?
|
||||
assert data.category.present?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
61
test/models/rule/action_test.rb
Normal file
61
test/models/rule/action_test.rb
Normal file
|
@ -0,0 +1,61 @@
|
|||
require "test_helper"
|
||||
|
||||
class Rule::ActionTest < ActiveSupport::TestCase
|
||||
include EntriesTestHelper
|
||||
|
||||
setup do
|
||||
@family = families(:dylan_family)
|
||||
@transaction_rule = rules(:one)
|
||||
@account = @family.accounts.create!(name: "Rule test", balance: 1000, currency: "USD", accountable: Depository.new)
|
||||
|
||||
@grocery_category = @family.categories.create!(name: "Grocery")
|
||||
@whole_foods_merchant = @family.merchants.create!(name: "Whole Foods", type: "FamilyMerchant")
|
||||
|
||||
# Some sample transactions to work with
|
||||
@txn1 = create_transaction(date: Date.current, account: @account, amount: 100, name: "Rule test transaction1", merchant: @whole_foods_merchant).transaction
|
||||
@txn2 = create_transaction(date: Date.current, account: @account, amount: -200, name: "Rule test transaction2").transaction
|
||||
@txn3 = create_transaction(date: 1.day.ago.to_date, account: @account, amount: 50, name: "Rule test transaction3").transaction
|
||||
|
||||
@rule_scope = @account.transactions
|
||||
end
|
||||
|
||||
test "set_transaction_category" do
|
||||
# Does not modify transactions that are locked (user edited them)
|
||||
@txn1.lock!(:category_id)
|
||||
|
||||
action = Rule::Action.new(
|
||||
rule: @transaction_rule,
|
||||
action_type: "set_transaction_category",
|
||||
value: @grocery_category.id
|
||||
)
|
||||
|
||||
action.apply(@rule_scope)
|
||||
|
||||
assert_nil @txn1.reload.category
|
||||
|
||||
[ @txn2, @txn3 ].each do |transaction|
|
||||
assert_equal @grocery_category.id, transaction.reload.category_id
|
||||
end
|
||||
end
|
||||
|
||||
test "set_transaction_tags" do
|
||||
tag = @family.tags.create!(name: "Rule test tag")
|
||||
|
||||
# Does not modify transactions that are locked (user edited them)
|
||||
@txn1.lock!(:tag_ids)
|
||||
|
||||
action = Rule::Action.new(
|
||||
rule: @transaction_rule,
|
||||
action_type: "set_transaction_tags",
|
||||
value: tag.id
|
||||
)
|
||||
|
||||
action.apply(@rule_scope)
|
||||
|
||||
assert_equal [], @txn1.reload.tags
|
||||
|
||||
[ @txn2, @txn3 ].each do |transaction|
|
||||
assert_equal [ tag ], transaction.reload.tags
|
||||
end
|
||||
end
|
||||
end
|
128
test/models/rule/condition_test.rb
Normal file
128
test/models/rule/condition_test.rb
Normal file
|
@ -0,0 +1,128 @@
|
|||
require "test_helper"
|
||||
|
||||
class Rule::ConditionTest < ActiveSupport::TestCase
|
||||
include EntriesTestHelper
|
||||
|
||||
setup do
|
||||
@family = families(:empty)
|
||||
@transaction_rule = rules(:one)
|
||||
@account = @family.accounts.create!(name: "Rule test", balance: 1000, currency: "USD", accountable: Depository.new)
|
||||
|
||||
@grocery_category = @family.categories.create!(name: "Grocery")
|
||||
@whole_foods_merchant = @family.merchants.create!(name: "Whole Foods", type: "FamilyMerchant")
|
||||
|
||||
# Some sample transactions to work with
|
||||
create_transaction(date: Date.current, account: @account, amount: 100, name: "Rule test transaction1", merchant: @whole_foods_merchant)
|
||||
create_transaction(date: Date.current, account: @account, amount: -200, name: "Rule test transaction2")
|
||||
create_transaction(date: 1.day.ago.to_date, account: @account, amount: 50, name: "Rule test transaction3")
|
||||
create_transaction(date: 1.year.ago.to_date, account: @account, amount: 10, name: "Rule test transaction4", merchant: @whole_foods_merchant)
|
||||
create_transaction(date: 1.year.ago.to_date, account: @account, amount: 1000, name: "Rule test transaction5")
|
||||
|
||||
@rule_scope = @account.transactions
|
||||
end
|
||||
|
||||
test "applies transaction_name condition" do
|
||||
scope = @rule_scope
|
||||
|
||||
condition = Rule::Condition.new(
|
||||
rule: @transaction_rule,
|
||||
condition_type: "transaction_name",
|
||||
operator: "=",
|
||||
value: "Rule test transaction1"
|
||||
)
|
||||
|
||||
scope = condition.prepare(scope)
|
||||
|
||||
assert_equal 5, scope.count
|
||||
|
||||
filtered = condition.apply(scope)
|
||||
|
||||
assert_equal 1, filtered.count
|
||||
end
|
||||
|
||||
test "applies transaction_amount condition using absolute values" do
|
||||
scope = @rule_scope
|
||||
|
||||
condition = Rule::Condition.new(
|
||||
rule: @transaction_rule,
|
||||
condition_type: "transaction_amount",
|
||||
operator: ">",
|
||||
value: "50"
|
||||
)
|
||||
|
||||
scope = condition.prepare(scope)
|
||||
|
||||
filtered = condition.apply(scope)
|
||||
assert_equal 3, filtered.count
|
||||
end
|
||||
|
||||
test "applies transaction_merchant condition" do
|
||||
scope = @rule_scope
|
||||
|
||||
condition = Rule::Condition.new(
|
||||
rule: @transaction_rule,
|
||||
condition_type: "transaction_merchant",
|
||||
operator: "=",
|
||||
value: @whole_foods_merchant.id
|
||||
)
|
||||
|
||||
scope = condition.prepare(scope)
|
||||
|
||||
filtered = condition.apply(scope)
|
||||
assert_equal 2, filtered.count
|
||||
end
|
||||
|
||||
test "applies compound and condition" do
|
||||
scope = @rule_scope
|
||||
|
||||
parent_condition = Rule::Condition.new(
|
||||
rule: @transaction_rule,
|
||||
condition_type: "compound",
|
||||
operator: "and",
|
||||
sub_conditions: [
|
||||
Rule::Condition.new(
|
||||
condition_type: "transaction_merchant",
|
||||
operator: "=",
|
||||
value: @whole_foods_merchant.id
|
||||
),
|
||||
Rule::Condition.new(
|
||||
condition_type: "transaction_amount",
|
||||
operator: "<",
|
||||
value: "50"
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
scope = parent_condition.prepare(scope)
|
||||
|
||||
filtered = parent_condition.apply(scope)
|
||||
assert_equal 1, filtered.count
|
||||
end
|
||||
|
||||
test "applies compound or condition" do
|
||||
scope = @rule_scope
|
||||
|
||||
parent_condition = Rule::Condition.new(
|
||||
rule: @transaction_rule,
|
||||
condition_type: "compound",
|
||||
operator: "or",
|
||||
sub_conditions: [
|
||||
Rule::Condition.new(
|
||||
condition_type: "transaction_merchant",
|
||||
operator: "=",
|
||||
value: @whole_foods_merchant.id
|
||||
),
|
||||
Rule::Condition.new(
|
||||
condition_type: "transaction_amount",
|
||||
operator: "<",
|
||||
value: "50"
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
scope = parent_condition.prepare(scope)
|
||||
|
||||
filtered = parent_condition.apply(scope)
|
||||
assert_equal 2, filtered.count
|
||||
end
|
||||
end
|
77
test/models/rule_test.rb
Normal file
77
test/models/rule_test.rb
Normal file
|
@ -0,0 +1,77 @@
|
|||
require "test_helper"
|
||||
|
||||
class RuleTest < ActiveSupport::TestCase
|
||||
include EntriesTestHelper
|
||||
|
||||
setup do
|
||||
@family = families(:empty)
|
||||
@account = @family.accounts.create!(name: "Rule test", balance: 1000, currency: "USD", accountable: Depository.new)
|
||||
@whole_foods_merchant = @family.merchants.create!(name: "Whole Foods", type: "FamilyMerchant")
|
||||
@groceries_category = @family.categories.create!(name: "Groceries")
|
||||
end
|
||||
|
||||
test "basic rule" do
|
||||
transaction_entry = create_transaction(date: Date.current, account: @account, merchant: @whole_foods_merchant)
|
||||
|
||||
rule = Rule.create!(
|
||||
family: @family,
|
||||
resource_type: "transaction",
|
||||
effective_date: 1.day.ago.to_date,
|
||||
conditions: [ Rule::Condition.new(condition_type: "transaction_merchant", operator: "=", value: @whole_foods_merchant.id) ],
|
||||
actions: [ Rule::Action.new(action_type: "set_transaction_category", value: @groceries_category.id) ]
|
||||
)
|
||||
|
||||
rule.apply
|
||||
|
||||
transaction_entry.reload
|
||||
|
||||
assert_equal @groceries_category, transaction_entry.transaction.category
|
||||
end
|
||||
|
||||
test "compound rule" do
|
||||
transaction_entry1 = create_transaction(date: Date.current, amount: 50, account: @account, merchant: @whole_foods_merchant)
|
||||
transaction_entry2 = create_transaction(date: Date.current, amount: 100, account: @account, merchant: @whole_foods_merchant)
|
||||
|
||||
# Assign "Groceries" to transactions with a merchant of "Whole Foods" and an amount greater than $60
|
||||
rule = Rule.create!(
|
||||
family: @family,
|
||||
resource_type: "transaction",
|
||||
effective_date: 1.day.ago.to_date,
|
||||
conditions: [
|
||||
Rule::Condition.new(condition_type: "compound", operator: "and", sub_conditions: [
|
||||
Rule::Condition.new(condition_type: "transaction_merchant", operator: "=", value: @whole_foods_merchant.id),
|
||||
Rule::Condition.new(condition_type: "transaction_amount", operator: ">", value: 60)
|
||||
])
|
||||
],
|
||||
actions: [ Rule::Action.new(action_type: "set_transaction_category", value: @groceries_category.id) ]
|
||||
)
|
||||
|
||||
rule.apply
|
||||
|
||||
transaction_entry1.reload
|
||||
transaction_entry2.reload
|
||||
|
||||
assert_nil transaction_entry1.transaction.category
|
||||
assert_equal @groceries_category, transaction_entry2.transaction.category
|
||||
end
|
||||
|
||||
# Artificial limitation put in place to prevent users from creating overly complex rules
|
||||
# Rules should be shallow and wide
|
||||
test "no nested compound conditions" do
|
||||
rule = Rule.new(
|
||||
family: @family,
|
||||
resource_type: "transaction",
|
||||
actions: [ Rule::Action.new(action_type: "set_transaction_category", value: @groceries_category.id) ],
|
||||
conditions: [
|
||||
Rule::Condition.new(condition_type: "compound", operator: "and", sub_conditions: [
|
||||
Rule::Condition.new(condition_type: "compound", operator: "and", sub_conditions: [
|
||||
Rule::Condition.new(condition_type: "transaction_name", operator: "=", value: "Starbucks")
|
||||
])
|
||||
])
|
||||
]
|
||||
)
|
||||
|
||||
assert_not rule.valid?
|
||||
assert_equal [ "Compound conditions cannot be nested" ], rule.errors.full_messages
|
||||
end
|
||||
end
|
|
@ -10,7 +10,7 @@ class SettingsTest < ApplicationSystemTestCase
|
|||
[ "Accounts", accounts_path ],
|
||||
[ "Tags", tags_path ],
|
||||
[ "Categories", categories_path ],
|
||||
[ "Merchants", merchants_path ],
|
||||
[ "Merchants", family_merchants_path ],
|
||||
[ "Imports", imports_path ],
|
||||
[ "What's new", changelog_path ],
|
||||
[ "Feedback", feedback_path ]
|
||||
|
|
194
test/vcr_cassettes/openai/auto_categorize.yml
Normal file
194
test/vcr_cassettes/openai/auto_categorize.yml
Normal file
|
@ -0,0 +1,194 @@
|
|||
---
|
||||
http_interactions:
|
||||
- request:
|
||||
method: post
|
||||
uri: https://api.openai.com/v1/responses
|
||||
body:
|
||||
encoding: UTF-8
|
||||
string: '{"model":"gpt-4.1-mini","input":[{"role":"developer","content":"Here
|
||||
are the user''s available categories in JSON format:\n\n```json\n[{\"id\":\"shopping_id\",\"name\":\"Shopping\",\"is_subcategory\":false,\"parent_id\":null,\"classification\":\"expense\"},{\"id\":\"subscriptions_id\",\"name\":\"Subscriptions\",\"is_subcategory\":true,\"parent_id\":null,\"classification\":\"expense\"},{\"id\":\"restaurants_id\",\"name\":\"Restaurants\",\"is_subcategory\":false,\"parent_id\":null,\"classification\":\"expense\"},{\"id\":\"fast_food_id\",\"name\":\"Fast
|
||||
Food\",\"is_subcategory\":true,\"parent_id\":\"restaurants_id\",\"classification\":\"expense\"},{\"id\":\"income_id\",\"name\":\"Income\",\"is_subcategory\":false,\"parent_id\":null,\"classification\":\"income\"}]\n```\n\nUse
|
||||
the available categories to auto-categorize the following transactions:\n\n```json\n[{\"id\":\"1\",\"name\":\"McDonalds\",\"amount\":20,\"classification\":\"expense\",\"merchant\":\"McDonalds\",\"hint\":\"Fast
|
||||
Food\"},{\"id\":\"2\",\"name\":\"Amazon purchase\",\"amount\":100,\"classification\":\"expense\",\"merchant\":\"Amazon\"},{\"id\":\"3\",\"name\":\"Netflix
|
||||
subscription\",\"amount\":10,\"classification\":\"expense\",\"merchant\":\"Netflix\",\"hint\":\"Subscriptions\"},{\"id\":\"4\",\"name\":\"paycheck\",\"amount\":3000,\"classification\":\"income\"},{\"id\":\"5\",\"name\":\"Italian
|
||||
dinner with friends\",\"amount\":100,\"classification\":\"expense\"},{\"id\":\"6\",\"name\":\"1212XXXBCaaa
|
||||
charge\",\"amount\":2.99,\"classification\":\"expense\"}]\n```\n"}],"text":{"format":{"type":"json_schema","name":"auto_categorize_personal_finance_transactions","strict":true,"schema":{"type":"object","properties":{"categorizations":{"type":"array","description":"An
|
||||
array of auto-categorizations for each transaction","items":{"type":"object","properties":{"transaction_id":{"type":"string","description":"The
|
||||
internal ID of the original transaction","enum":["1","2","3","4","5","6"]},"category_name":{"type":"string","description":"The
|
||||
matched category name of the transaction, or null if no match","enum":["Shopping","Subscriptions","Restaurants","Fast
|
||||
Food","Income","null"]}},"required":["transaction_id","category_name"],"additionalProperties":false}}},"required":["categorizations"],"additionalProperties":false}}},"instructions":"You
|
||||
are an assistant to a consumer personal finance app. You will be provided
|
||||
a list\nof the user''s transactions and a list of the user''s categories. Your
|
||||
job is to auto-categorize\neach transaction.\n\nClosely follow ALL the rules
|
||||
below while auto-categorizing:\n\n- Return 1 result per transaction\n- Correlate
|
||||
each transaction by ID (transaction_id)\n- Attempt to match the most specific
|
||||
category possible (i.e. subcategory over parent category)\n- Category and
|
||||
transaction classifications should match (i.e. if transaction is an \"expense\",
|
||||
the category must have classification of \"expense\")\n- If you don''t know
|
||||
the category, return \"null\"\n - You should always favor \"null\" over false
|
||||
positives\n - Be slightly pessimistic. Only match a category if you''re
|
||||
60%+ confident it is the correct one.\n- Each transaction has varying metadata
|
||||
that can be used to determine the category\n - Note: \"hint\" comes from
|
||||
3rd party aggregators and typically represents a category name that\n may
|
||||
or may not match any of the user-supplied categories\n"}'
|
||||
headers:
|
||||
Content-Type:
|
||||
- application/json
|
||||
Authorization:
|
||||
- Bearer <OPENAI_ACCESS_TOKEN>
|
||||
Accept-Encoding:
|
||||
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
||||
Accept:
|
||||
- "*/*"
|
||||
User-Agent:
|
||||
- Ruby
|
||||
response:
|
||||
status:
|
||||
code: 200
|
||||
message: OK
|
||||
headers:
|
||||
Date:
|
||||
- Wed, 16 Apr 2025 14:07:39 GMT
|
||||
Content-Type:
|
||||
- application/json
|
||||
Transfer-Encoding:
|
||||
- chunked
|
||||
Connection:
|
||||
- keep-alive
|
||||
Openai-Version:
|
||||
- '2020-10-01'
|
||||
Openai-Organization:
|
||||
- user-r6cwd3mn6iv6gn748b2xoajx
|
||||
X-Request-Id:
|
||||
- req_01b869bd9eb7b994a80e79f6de92e5a2
|
||||
Openai-Processing-Ms:
|
||||
- '2173'
|
||||
Strict-Transport-Security:
|
||||
- max-age=31536000; includeSubDomains; preload
|
||||
Cf-Cache-Status:
|
||||
- DYNAMIC
|
||||
Set-Cookie:
|
||||
- __cf_bm=xGkX7L6XeEFLp6ZPB2Y.LLHD_YSpzTH28MUro6fQG7Y-1744812459-1.0.1.1-uy8WQsFzGblq3h.u6WFs2vld_HM.5fveVAFBsQ6y.Za22DSEa22k3NS7.GAUbgAvoVjGvSQlkm8LkSZyU3wZfN70cUpZrg27orQt0Nfq91U;
|
||||
path=/; expires=Wed, 16-Apr-25 14:37:39 GMT; domain=.api.openai.com; HttpOnly;
|
||||
Secure; SameSite=None
|
||||
- _cfuvid=LicWzTMZxt1n1GLU6XQx3NnU0PbKnI0m97CH.p0895U-1744812459077-0.0.1.1-604800000;
|
||||
path=/; domain=.api.openai.com; HttpOnly; Secure; SameSite=None
|
||||
X-Content-Type-Options:
|
||||
- nosniff
|
||||
Server:
|
||||
- cloudflare
|
||||
Cf-Ray:
|
||||
- 93143ffeffe8cf6b-CMH
|
||||
Alt-Svc:
|
||||
- h3=":443"; ma=86400
|
||||
body:
|
||||
encoding: ASCII-8BIT
|
||||
string: |-
|
||||
{
|
||||
"id": "resp_67ffb9a8e530819290c5d3ec8aaf326d0e0f06e2ac13ae37",
|
||||
"object": "response",
|
||||
"created_at": 1744812456,
|
||||
"status": "completed",
|
||||
"error": null,
|
||||
"incomplete_details": null,
|
||||
"instructions": "You are an assistant to a consumer personal finance app. You will be provided a list\nof the user's transactions and a list of the user's categories. Your job is to auto-categorize\neach transaction.\n\nClosely follow ALL the rules below while auto-categorizing:\n\n- Return 1 result per transaction\n- Correlate each transaction by ID (transaction_id)\n- Attempt to match the most specific category possible (i.e. subcategory over parent category)\n- Category and transaction classifications should match (i.e. if transaction is an \"expense\", the category must have classification of \"expense\")\n- If you don't know the category, return \"null\"\n - You should always favor \"null\" over false positives\n - Be slightly pessimistic. Only match a category if you're 60%+ confident it is the correct one.\n- Each transaction has varying metadata that can be used to determine the category\n - Note: \"hint\" comes from 3rd party aggregators and typically represents a category name that\n may or may not match any of the user-supplied categories\n",
|
||||
"max_output_tokens": null,
|
||||
"model": "gpt-4.1-mini-2025-04-14",
|
||||
"output": [
|
||||
{
|
||||
"id": "msg_67ffb9a96b3c81928d9da130e889a9aa0e0f06e2ac13ae37",
|
||||
"type": "message",
|
||||
"status": "completed",
|
||||
"content": [
|
||||
{
|
||||
"type": "output_text",
|
||||
"annotations": [],
|
||||
"text": "{\"categorizations\":[{\"transaction_id\":\"1\",\"category_name\":\"Fast Food\"},{\"transaction_id\":\"2\",\"category_name\":\"Shopping\"},{\"transaction_id\":\"3\",\"category_name\":\"Subscriptions\"},{\"transaction_id\":\"4\",\"category_name\":\"Income\"},{\"transaction_id\":\"5\",\"category_name\":\"Restaurants\"},{\"transaction_id\":\"6\",\"category_name\":\"null\"}]}"
|
||||
}
|
||||
],
|
||||
"role": "assistant"
|
||||
}
|
||||
],
|
||||
"parallel_tool_calls": true,
|
||||
"previous_response_id": null,
|
||||
"reasoning": {
|
||||
"effort": null,
|
||||
"summary": null
|
||||
},
|
||||
"store": true,
|
||||
"temperature": 1.0,
|
||||
"text": {
|
||||
"format": {
|
||||
"type": "json_schema",
|
||||
"description": null,
|
||||
"name": "auto_categorize_personal_finance_transactions",
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"categorizations": {
|
||||
"type": "array",
|
||||
"description": "An array of auto-categorizations for each transaction",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"transaction_id": {
|
||||
"type": "string",
|
||||
"description": "The internal ID of the original transaction",
|
||||
"enum": [
|
||||
"1",
|
||||
"2",
|
||||
"3",
|
||||
"4",
|
||||
"5",
|
||||
"6"
|
||||
]
|
||||
},
|
||||
"category_name": {
|
||||
"type": "string",
|
||||
"description": "The matched category name of the transaction, or null if no match",
|
||||
"enum": [
|
||||
"Shopping",
|
||||
"Subscriptions",
|
||||
"Restaurants",
|
||||
"Fast Food",
|
||||
"Income",
|
||||
"null"
|
||||
]
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"transaction_id",
|
||||
"category_name"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"categorizations"
|
||||
],
|
||||
"additionalProperties": false
|
||||
},
|
||||
"strict": true
|
||||
}
|
||||
},
|
||||
"tool_choice": "auto",
|
||||
"tools": [],
|
||||
"top_p": 1.0,
|
||||
"truncation": "disabled",
|
||||
"usage": {
|
||||
"input_tokens": 659,
|
||||
"input_tokens_details": {
|
||||
"cached_tokens": 0
|
||||
},
|
||||
"output_tokens": 70,
|
||||
"output_tokens_details": {
|
||||
"reasoning_tokens": 0
|
||||
},
|
||||
"total_tokens": 729
|
||||
},
|
||||
"user": null,
|
||||
"metadata": {}
|
||||
}
|
||||
recorded_at: Wed, 16 Apr 2025 14:07:39 GMT
|
||||
recorded_with: VCR 6.3.1
|
203
test/vcr_cassettes/openai/auto_detect_merchants.yml
Normal file
203
test/vcr_cassettes/openai/auto_detect_merchants.yml
Normal file
|
@ -0,0 +1,203 @@
|
|||
---
|
||||
http_interactions:
|
||||
- request:
|
||||
method: post
|
||||
uri: https://api.openai.com/v1/responses
|
||||
body:
|
||||
encoding: UTF-8
|
||||
string: '{"model":"gpt-4.1-mini","input":[{"role":"developer","content":"Here
|
||||
are the user''s available merchants in JSON format:\n\n```json\n[{\"name\":\"Shooters\"}]\n```\n\nUse
|
||||
BOTH your knowledge AND the user-generated merchants to auto-detect the following
|
||||
transactions:\n\n```json\n[{\"id\":\"1\",\"name\":\"McDonalds\",\"amount\":20,\"classification\":\"expense\"},{\"id\":\"2\",\"name\":\"local
|
||||
pub\",\"amount\":20,\"classification\":\"expense\"},{\"id\":\"3\",\"name\":\"WMT
|
||||
purchases\",\"amount\":20,\"classification\":\"expense\"},{\"id\":\"4\",\"name\":\"amzn
|
||||
123 abc\",\"amount\":20,\"classification\":\"expense\"},{\"id\":\"5\",\"name\":\"chaseX1231\",\"amount\":2000,\"classification\":\"income\"},{\"id\":\"6\",\"name\":\"check
|
||||
deposit 022\",\"amount\":200,\"classification\":\"income\"},{\"id\":\"7\",\"name\":\"shooters
|
||||
bar and grill\",\"amount\":200,\"classification\":\"expense\"},{\"id\":\"8\",\"name\":\"Microsoft
|
||||
Office subscription\",\"amount\":200,\"classification\":\"expense\"}]\n```\n\nReturn
|
||||
\"null\" if you are not 80%+ confident in your answer.\n"}],"text":{"format":{"type":"json_schema","name":"auto_detect_personal_finance_merchants","strict":true,"schema":{"type":"object","properties":{"merchants":{"type":"array","description":"An
|
||||
array of auto-detected merchant businesses for each transaction","items":{"type":"object","properties":{"transaction_id":{"type":"string","description":"The
|
||||
internal ID of the original transaction","enum":["1","2","3","4","5","6","7","8"]},"business_name":{"type":["string","null"],"description":"The
|
||||
detected business name of the transaction, or `null` if uncertain"},"business_url":{"type":["string","null"],"description":"The
|
||||
URL of the detected business, or `null` if uncertain"}},"required":["transaction_id","business_name","business_url"],"additionalProperties":false}}},"required":["merchants"],"additionalProperties":false}}},"instructions":"You
|
||||
are an assistant to a consumer personal finance app.\n\nClosely follow ALL
|
||||
the rules below while auto-detecting business names and website URLs:\n\n-
|
||||
Return 1 result per transaction\n- Correlate each transaction by ID (transaction_id)\n-
|
||||
Do not include the subdomain in the business_url (i.e. \"amazon.com\" not
|
||||
\"www.amazon.com\")\n- User merchants are considered \"manual\" user-generated
|
||||
merchants and should only be used in 100% clear cases\n- Be slightly pessimistic. We
|
||||
favor returning \"null\" over returning a false positive.\n- NEVER return
|
||||
a name or URL for generic transaction names (e.g. \"Paycheck\", \"Laundromat\",
|
||||
\"Grocery store\", \"Local diner\")\n\nDetermining a value:\n\n- First attempt
|
||||
to determine the name + URL from your knowledge of global businesses\n- If
|
||||
no certain match, attempt to match one of the user-provided merchants\n- If
|
||||
no match, return \"null\"\n\nExample 1 (known business):\n\n```\nTransaction
|
||||
name: \"Some Amazon purchases\"\n\nResult:\n- business_name: \"Amazon\"\n-
|
||||
business_url: \"amazon.com\"\n```\n\nExample 2 (generic business):\n\n```\nTransaction
|
||||
name: \"local diner\"\n\nResult:\n- business_name: null\n- business_url: null\n```\n"}'
|
||||
headers:
|
||||
Content-Type:
|
||||
- application/json
|
||||
Authorization:
|
||||
- Bearer <OPENAI_ACCESS_TOKEN>
|
||||
Accept-Encoding:
|
||||
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
||||
Accept:
|
||||
- "*/*"
|
||||
User-Agent:
|
||||
- Ruby
|
||||
response:
|
||||
status:
|
||||
code: 200
|
||||
message: OK
|
||||
headers:
|
||||
Date:
|
||||
- Wed, 16 Apr 2025 15:41:50 GMT
|
||||
Content-Type:
|
||||
- application/json
|
||||
Transfer-Encoding:
|
||||
- chunked
|
||||
Connection:
|
||||
- keep-alive
|
||||
Openai-Version:
|
||||
- '2020-10-01'
|
||||
Openai-Organization:
|
||||
- user-r6cwd3mn6iv6gn748b2xoajx
|
||||
X-Request-Id:
|
||||
- req_77a41d32ae2c3dbd9081b34bc5e4ce61
|
||||
Openai-Processing-Ms:
|
||||
- '2152'
|
||||
Strict-Transport-Security:
|
||||
- max-age=31536000; includeSubDomains; preload
|
||||
Cf-Cache-Status:
|
||||
- DYNAMIC
|
||||
Set-Cookie:
|
||||
- __cf_bm=hCFJRspk322ZVvRasJGcux5mYDyfa5aO7EQOCAbnhjM-1744818110-1.0.1.1-.fRz_SYTG_PqZ3VCSDju7YeDaZwCyf5OGVvDvaN.h3aegNTlYtdPwbnZ5NNFxLRJhWFRY4vwHYkHm1DGTarK5NQ6UjA1sOrRpmS5eZ.zabw;
|
||||
path=/; expires=Wed, 16-Apr-25 16:11:50 GMT; domain=.api.openai.com; HttpOnly;
|
||||
Secure; SameSite=None
|
||||
- _cfuvid=At3dVxwug2seJ3Oa02PSnIoKhVSEvt6IPCLfhkULvac-1744818110064-0.0.1.1-604800000;
|
||||
path=/; domain=.api.openai.com; HttpOnly; Secure; SameSite=None
|
||||
X-Content-Type-Options:
|
||||
- nosniff
|
||||
Server:
|
||||
- cloudflare
|
||||
Cf-Ray:
|
||||
- 9314c9f5cef5efe9-CMH
|
||||
Alt-Svc:
|
||||
- h3=":443"; ma=86400
|
||||
body:
|
||||
encoding: ASCII-8BIT
|
||||
string: |-
|
||||
{
|
||||
"id": "resp_67ffcfbbddb48192a251a3c0f341941a04d20b39fa51ef90",
|
||||
"object": "response",
|
||||
"created_at": 1744818107,
|
||||
"status": "completed",
|
||||
"error": null,
|
||||
"incomplete_details": null,
|
||||
"instructions": "You are an assistant to a consumer personal finance app.\n\nClosely follow ALL the rules below while auto-detecting business names and website URLs:\n\n- Return 1 result per transaction\n- Correlate each transaction by ID (transaction_id)\n- Do not include the subdomain in the business_url (i.e. \"amazon.com\" not \"www.amazon.com\")\n- User merchants are considered \"manual\" user-generated merchants and should only be used in 100% clear cases\n- Be slightly pessimistic. We favor returning \"null\" over returning a false positive.\n- NEVER return a name or URL for generic transaction names (e.g. \"Paycheck\", \"Laundromat\", \"Grocery store\", \"Local diner\")\n\nDetermining a value:\n\n- First attempt to determine the name + URL from your knowledge of global businesses\n- If no certain match, attempt to match one of the user-provided merchants\n- If no match, return \"null\"\n\nExample 1 (known business):\n\n```\nTransaction name: \"Some Amazon purchases\"\n\nResult:\n- business_name: \"Amazon\"\n- business_url: \"amazon.com\"\n```\n\nExample 2 (generic business):\n\n```\nTransaction name: \"local diner\"\n\nResult:\n- business_name: null\n- business_url: null\n```\n",
|
||||
"max_output_tokens": null,
|
||||
"model": "gpt-4.1-mini-2025-04-14",
|
||||
"output": [
|
||||
{
|
||||
"id": "msg_67ffcfbc58bc8192bbcf4dc54759837c04d20b39fa51ef90",
|
||||
"type": "message",
|
||||
"status": "completed",
|
||||
"content": [
|
||||
{
|
||||
"type": "output_text",
|
||||
"annotations": [],
|
||||
"text": "{\"merchants\":[{\"transaction_id\":\"1\",\"business_name\":\"McDonald's\",\"business_url\":\"mcdonalds.com\"},{\"transaction_id\":\"2\",\"business_name\":null,\"business_url\":null},{\"transaction_id\":\"3\",\"business_name\":\"Walmart\",\"business_url\":\"walmart.com\"},{\"transaction_id\":\"4\",\"business_name\":\"Amazon\",\"business_url\":\"amazon.com\"},{\"transaction_id\":\"5\",\"business_name\":null,\"business_url\":null},{\"transaction_id\":\"6\",\"business_name\":null,\"business_url\":null},{\"transaction_id\":\"7\",\"business_name\":\"Shooters\",\"business_url\":null},{\"transaction_id\":\"8\",\"business_name\":\"Microsoft\",\"business_url\":\"microsoft.com\"}]}"
|
||||
}
|
||||
],
|
||||
"role": "assistant"
|
||||
}
|
||||
],
|
||||
"parallel_tool_calls": true,
|
||||
"previous_response_id": null,
|
||||
"reasoning": {
|
||||
"effort": null,
|
||||
"summary": null
|
||||
},
|
||||
"store": true,
|
||||
"temperature": 1.0,
|
||||
"text": {
|
||||
"format": {
|
||||
"type": "json_schema",
|
||||
"description": null,
|
||||
"name": "auto_detect_personal_finance_merchants",
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"merchants": {
|
||||
"type": "array",
|
||||
"description": "An array of auto-detected merchant businesses for each transaction",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"transaction_id": {
|
||||
"type": "string",
|
||||
"description": "The internal ID of the original transaction",
|
||||
"enum": [
|
||||
"1",
|
||||
"2",
|
||||
"3",
|
||||
"4",
|
||||
"5",
|
||||
"6",
|
||||
"7",
|
||||
"8"
|
||||
]
|
||||
},
|
||||
"business_name": {
|
||||
"type": [
|
||||
"string",
|
||||
"null"
|
||||
],
|
||||
"description": "The detected business name of the transaction, or `null` if uncertain"
|
||||
},
|
||||
"business_url": {
|
||||
"type": [
|
||||
"string",
|
||||
"null"
|
||||
],
|
||||
"description": "The URL of the detected business, or `null` if uncertain"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"transaction_id",
|
||||
"business_name",
|
||||
"business_url"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"merchants"
|
||||
],
|
||||
"additionalProperties": false
|
||||
},
|
||||
"strict": true
|
||||
}
|
||||
},
|
||||
"tool_choice": "auto",
|
||||
"tools": [],
|
||||
"top_p": 1.0,
|
||||
"truncation": "disabled",
|
||||
"usage": {
|
||||
"input_tokens": 635,
|
||||
"input_tokens_details": {
|
||||
"cached_tokens": 0
|
||||
},
|
||||
"output_tokens": 140,
|
||||
"output_tokens_details": {
|
||||
"reasoning_tokens": 0
|
||||
},
|
||||
"total_tokens": 775
|
||||
},
|
||||
"user": null,
|
||||
"metadata": {}
|
||||
}
|
||||
recorded_at: Wed, 16 Apr 2025 15:41:50 GMT
|
||||
recorded_with: VCR 6.3.1
|
Loading…
Add table
Add a link
Reference in a new issue