Bucksley / app · bucksley.ai / Tests / BucksleyKitTests / AskToolsTests.swift
| 1 | import Foundation |
| 2 | import Testing |
| 3 | @testable import BucksleyKit |
| 4 | |
| 5 | @Suite struct AskToolsTests { |
| 6 | func ledger() -> Ledger { |
| 7 | var ws = Workspace() |
| 8 | ws.accounts = [Account(id: "chk", connectionID: "c", name: "Checking", institution: "Bank", kind: .checking, balance: 3000), |
| 9 | Account(id: "card", connectionID: "c", name: "Visa", institution: "Bank", kind: .credit, balance: -32400.55)] |
| 10 | let transactions = [ |
| 11 | tx("pay-out", "2026-11-04", -1500, "Mobile Banking Payment to Confirmation Qhal", account: "chk"), |
| 12 | tx("pay-in", "2026-11-05", 1500, "Payment From Checking:", account: "card"), |
| 13 | tx("mortgage-oct", "2026-10-05", -3939.75, "CMG MORTGAGE INC", account: "chk"), |
| 14 | tx("mortgage", "2026-11-05", -3939.75, "CMG MORTGAGE INC", account: "chk"), |
| 15 | tx("coffee", "2026-11-10", -6.5, "BLUE BOTTLE COFFEE", account: "card"), |
| 16 | tx("pay", "2026-11-13", 4267.93, "ACME PAYROLL DIRECT DEP", account: "chk") |
| 17 | ] |
| 18 | return Ledger(workspace: ws, transactions: transactions, edits: [:], balances: []) |
| 19 | } |
| 20 | |
| 21 | @Test func cardPaymentsAreTransfersNotSpendingOrIncome() { |
| 22 | let l = ledger() |
| 23 | #expect(l.entries.first { $0.id == "pay-out" }?.categoryID == "transfer") |
| 24 | #expect(l.entries.first { $0.id == "pay-in" }?.categoryID == "transfer") |
| 25 | #expect(l.income(month: "2026-11") == Decimal(string: "4267.93")) |
| 26 | #expect(l.spending(month: "2026-11").reduce(Decimal(0)) { $0 + $1.amount } == Decimal(string: "3946.25")) |
| 27 | #expect(Defaults.guessCategory(tx("x", "2026-11-01", -40, "MOBIL 0042 OAKLAND")) == "gas") |
| 28 | } |
| 29 | |
| 30 | @Test func anAnswerThatNamesChargesLinksThemWithoutALookup() { |
| 31 | let text = "Your largest charges: 1. Cmg Mortgage Inc: –$3,939.75 on Nov 5. 2. Blue Bottle Coffee: $6.50." |
| 32 | #expect(AskBucksley.linked(text, found: [], ledger: ledger(), today: "2026-11-25") == ["mortgage", "coffee"]) |
| 33 | #expect(AskBucksley.linked("Your card balance is $32,400.55.", found: ["coffee"], ledger: ledger(), today: "2026-11-25") == ["coffee"]) |
| 34 | } |
| 35 | |
| 36 | @Test func largestChargesNeverIncludeABalanceAndAreRemembered() { |
| 37 | let tools = AskTools(ledger: ledger(), today: "2026-11-25") |
| 38 | let text = tools.findTransactions(.init(month: "2026-11", direction: "spending", sort: "largest", limit: 2)) |
| 39 | #expect(text.lowercased().contains("mortgage")) |
| 40 | #expect(!text.contains("32,400")) |
| 41 | #expect(tools.transactionIDs == ["mortgage", "coffee"]) |
| 42 | #expect(tools.accounts().contains("not transactions")) |
| 43 | } |
| 44 | |
| 45 | @Test func claudeLooksUpTransactionsAndTheAnswerLinksThem() async throws { |
| 46 | Keychain.memory = Keychain.memory ?? [:] |
| 47 | try Keychain.set("test-key", for: .claudeKey) |
| 48 | let calls = Counter() |
| 49 | let http = StubHTTP { request in |
| 50 | let body = (try? JSONSerialization.jsonObject(with: request.httpBody ?? Data())) as? [String: Any] ?? [:] |
| 51 | #expect((body["tools"] as? [Any])?.count == 5) |
| 52 | if calls.next() == 1 { |
| 53 | return (200, #"{"stop_reason":"tool_use","content":[{"type":"tool_use","id":"u1","name":"find_transactions","input":{"month":"2026-11","direction":"spending","sort":"largest","limit":1}}]}"#) |
| 54 | } |
| 55 | let messages = body["messages"] as? [[String: Any]] ?? [] |
| 56 | let result = ((messages.last?["content"] as? [[String: Any]])?.first?["content"] as? String) ?? "" |
| 57 | #expect(result.contains("3,939.75")) |
| 58 | return (200, #"{"stop_reason":"end_turn","content":[{"type":"text","text":"Your biggest charge was the mortgage."}]}"#) |
| 59 | } |
| 60 | let answer = try await AskBucksley(http: http).ask("What was my biggest charge?", ledger: ledger(), provider: .claude) |
| 61 | #expect(answer.text == "Your biggest charge was the mortgage.") |
| 62 | #expect(answer.transactionIDs == ["mortgage"]) |
| 63 | } |
| 64 | } |
| 65 |