The claim on BS-partner-2 gains a marked CHOICE (the owner, 2026-11-21: shapes on free text, strict on party fields, masking on bank imports) and the product rule WB-partner-15 with the downsides accepted; the tool interface, the handbook sections an assistant reads (parties and TINs, files in and out, attestations and audit) and the two import tools' descriptions say what is refused, what is masked and what the audit names. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
584 lines
19 KiB
Rust
584 lines
19 KiB
Rust
//! Free text never holds a tax id (BS-partner-2): the one detector free
|
|
//! text meets (`ledger_core::settings::holds_printed_tin`), the masking a
|
|
//! bank's description gets, and the engine commands that take free text.
|
|
//!
|
|
//! Oracle: the owner's decision of 2026-11-21 (docs/spec/claims/partner.md,
|
|
//! the CHOICE on BS-partner-2): free text refuses what looks like a tax id,
|
|
//! that is nine digits standing alone, an SSN's 3-2-4 and an EIN's 2-7, with
|
|
//! any dash, dot or space between the groups; it lets an invoice number, a
|
|
//! phone number, an order number, a reference of ten digits or more, an
|
|
//! amount and a date through. The fake ids are `000-00-0000` and
|
|
//! `00-0000000`; no real one appears here.
|
|
|
|
use ledger_core::settings::{
|
|
check_free_text, count_printed_tins, holds_printed_tin, mask_printed_tins, tin_refusal,
|
|
};
|
|
use proptest::prelude::*;
|
|
|
|
/// Every separator the detector reads between a TIN's groups.
|
|
const SEPARATORS: &[&str] = &[
|
|
"-",
|
|
"\u{2010}",
|
|
"\u{2011}",
|
|
"\u{2012}",
|
|
"\u{2013}",
|
|
"\u{2014}",
|
|
"\u{2015}",
|
|
"\u{2212}",
|
|
"\u{fe58}",
|
|
"\u{fe63}",
|
|
"\u{ff0d}",
|
|
".",
|
|
"\u{ff0e}",
|
|
" ",
|
|
" ",
|
|
"\t",
|
|
"\u{a0}",
|
|
"\u{2002}",
|
|
"\u{2003}",
|
|
"\u{2009}",
|
|
"\u{202f}",
|
|
"\u{3000}",
|
|
" - ",
|
|
" \u{2013} ",
|
|
"\u{a0}\u{2014}\u{a0}",
|
|
];
|
|
|
|
/// Text around a number that has no digit at its edge.
|
|
const CONTEXTS: &[(&str, &str)] = &[
|
|
("", ""),
|
|
("paid ", ""),
|
|
("", " paid"),
|
|
("SSN ", "."),
|
|
("(", ")"),
|
|
("ref: ", ", thanks"),
|
|
("a\u{e9}", "z"),
|
|
];
|
|
|
|
fn digits(n: usize) -> impl Strategy<Value = String> {
|
|
prop::collection::vec(0u8..10, n)
|
|
.prop_map(|d| d.into_iter().map(|x| char::from(b'0' + x)).collect())
|
|
}
|
|
|
|
proptest! {
|
|
#![proptest_config(ProptestConfig::with_cases(512))]
|
|
|
|
/// Every 3-2-4 grouping with any listed separator is found.
|
|
#[test]
|
|
fn every_ssn_grouping_is_found(
|
|
a in digits(3), b in digits(2), c in digits(4),
|
|
s1 in 0..SEPARATORS.len(), s2 in 0..SEPARATORS.len(), ctx in 0..CONTEXTS.len(),
|
|
) {
|
|
let text = format!("{}{a}{}{b}{}{c}{}", CONTEXTS[ctx].0, SEPARATORS[s1], SEPARATORS[s2], CONTEXTS[ctx].1);
|
|
prop_assert!(holds_printed_tin(&text), "{text:?}");
|
|
}
|
|
|
|
/// Every 2-7 grouping with any listed separator is found.
|
|
#[test]
|
|
fn every_ein_grouping_is_found(
|
|
a in digits(2), b in digits(7), s in 0..SEPARATORS.len(), ctx in 0..CONTEXTS.len(),
|
|
) {
|
|
let text = format!("{}{a}{}{b}{}", CONTEXTS[ctx].0, SEPARATORS[s], CONTEXTS[ctx].1);
|
|
prop_assert!(holds_printed_tin(&text), "{text:?}");
|
|
}
|
|
|
|
/// Nine digits standing alone are found.
|
|
#[test]
|
|
fn nine_digits_alone_are_found(a in digits(9), ctx in 0..CONTEXTS.len()) {
|
|
let text = format!("{}{a}{}", CONTEXTS[ctx].0, CONTEXTS[ctx].1);
|
|
prop_assert!(holds_printed_tin(&text), "{text:?}");
|
|
}
|
|
|
|
/// Ten digits or more are a reference, never a tax id: one digit more
|
|
/// before, after or inside a shape takes it out of the reading.
|
|
#[test]
|
|
fn ten_or_more_digits_are_a_reference(a in digits(10), extra in 0usize..6, ctx in 0..CONTEXTS.len()) {
|
|
let tail: String = "7".repeat(extra);
|
|
let text = format!("{}{a}{tail}{}", CONTEXTS[ctx].0, CONTEXTS[ctx].1);
|
|
prop_assert!(!holds_printed_tin(&text), "{text:?}");
|
|
}
|
|
|
|
/// Masking leaves nothing for the detector to find, keeps the last four
|
|
/// digits of each number and its length, and changes nothing else.
|
|
#[test]
|
|
fn masking_keeps_the_last_four_and_the_rest(
|
|
a in digits(3), b in digits(2), c in digits(4), s1 in 0..SEPARATORS.len(),
|
|
s2 in 0..SEPARATORS.len(), ctx in 0..CONTEXTS.len(),
|
|
) {
|
|
let text = format!("{}{a}{}{b}{}{c}{}", CONTEXTS[ctx].0, SEPARATORS[s1], SEPARATORS[s2], CONTEXTS[ctx].1);
|
|
let masked = mask_printed_tins(&text).into_owned();
|
|
prop_assert!(!holds_printed_tin(&masked), "{masked:?}");
|
|
prop_assert_eq!(masked.chars().count(), text.chars().count());
|
|
prop_assert_eq!(count_printed_tins(&text), 1);
|
|
let expect = format!("{}\u{2022}\u{2022}\u{2022}{}\u{2022}\u{2022}{}{c}{}", CONTEXTS[ctx].0, SEPARATORS[s1], SEPARATORS[s2], CONTEXTS[ctx].1);
|
|
prop_assert_eq!(masked, expect);
|
|
}
|
|
}
|
|
|
|
/// What must go through: the allow-list of the owner's table.
|
|
#[test]
|
|
fn the_allow_list_passes() {
|
|
for fine in [
|
|
// invoice numbers
|
|
"INV-2026-11-0042",
|
|
"2026-11-0042",
|
|
"invoice 2026-0042",
|
|
"INV 20261121",
|
|
// phone numbers
|
|
"801-555-0142",
|
|
"(801) 555-0142",
|
|
"+1 801 555 0142",
|
|
"+44 20 7946 0958",
|
|
// order numbers
|
|
"ORD-48213-7",
|
|
"order 4821-3377-12",
|
|
"PO 12-345",
|
|
// references of ten digits or more
|
|
"1234567890",
|
|
"REF 202611210042",
|
|
"000000000000",
|
|
"12345678901234567890",
|
|
// amounts
|
|
"$1,234,567.89",
|
|
"1,234,567",
|
|
"$12,345,678.00",
|
|
"USD 1 234 567,89",
|
|
// dates
|
|
"2026-11-21",
|
|
"11/21/2026",
|
|
"21.11.2026",
|
|
"2026-11-21T10:30:00Z",
|
|
// plain words
|
|
"Office rent, November",
|
|
"",
|
|
] {
|
|
assert!(!holds_printed_tin(fine), "{fine:?} must pass");
|
|
assert!(check_free_text("memo", fine).is_ok(), "{fine:?} must pass");
|
|
}
|
|
}
|
|
|
|
/// What must be found, written out: the shapes with the fake ids.
|
|
#[test]
|
|
fn the_shapes_are_found() {
|
|
for caught in [
|
|
"000-00-0000",
|
|
"00-0000000",
|
|
"000000000",
|
|
"tin 000 00 0000",
|
|
"ein 00 0000000",
|
|
"000\u{2013}00\u{2013}0000",
|
|
"000\u{a0}00\u{a0}0000",
|
|
"\u{ff10}\u{ff10}\u{ff10}-\u{ff10}\u{ff10}-\u{ff10}\u{ff10}\u{ff10}\u{ff10}",
|
|
"\u{660}\u{660}\u{660}-\u{660}\u{660}-\u{660}\u{660}\u{660}\u{660}",
|
|
"pay 00-0000000 now",
|
|
"SSN: 000.00.0000.",
|
|
] {
|
|
assert!(holds_printed_tin(caught), "{caught:?} must be found");
|
|
let why = check_free_text("the memo", caught).unwrap_err();
|
|
assert!(
|
|
why.starts_with("the memo looks like it holds a tax id"),
|
|
"{why}"
|
|
);
|
|
assert!(why.contains("books.set_tin"), "{why}");
|
|
assert!(why.contains("W-9"), "{why}");
|
|
assert!(
|
|
!why.contains("0000"),
|
|
"a refusal never repeats the number: {why}"
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Two numbers in one text are two finds, each masked to its last four.
|
|
#[test]
|
|
fn masking_masks_each_number() {
|
|
let text = "ACH 000-00-0000 and ref 00-0000000, then 1234567890";
|
|
assert_eq!(count_printed_tins(text), 2);
|
|
assert_eq!(
|
|
mask_printed_tins(text),
|
|
"ACH \u{2022}\u{2022}\u{2022}-\u{2022}\u{2022}-0000 and ref \u{2022}\u{2022}-\u{2022}\u{2022}\u{2022}0000, then 1234567890"
|
|
);
|
|
// Nothing to mask: the text comes back as it was.
|
|
assert!(matches!(
|
|
mask_printed_tins("Whole Foods #1234"),
|
|
std::borrow::Cow::Borrowed(_)
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn the_refusal_names_the_field_and_never_a_number() {
|
|
let why = tin_refusal("books.add_class name");
|
|
assert!(why.starts_with("books.add_class name looks like it holds a tax id"));
|
|
assert!(why.contains("books.set_tin") && why.contains("books.set_w9"));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// The engine: a caller other than the MCP layer meets the same check.
|
|
// ---------------------------------------------------------------------
|
|
|
|
mod engine {
|
|
use chrono::NaiveDate;
|
|
use ledger_core::arap::{BillFields, DocumentLine, InvoiceFields, PartySnapshot};
|
|
use ledger_core::schedules::{
|
|
Frequency, NewRecurring, NewSchedule, ScheduleConvention, ScheduleKind,
|
|
};
|
|
use ledger_core::{
|
|
AccountId, AccountType, Currency, Ledger, LedgerError, Money, NewAsset, Side,
|
|
};
|
|
use rust_decimal::Decimal;
|
|
use std::str::FromStr;
|
|
|
|
const TIN: &str = "000-00-0000";
|
|
|
|
fn day(d: u32) -> NaiveDate {
|
|
NaiveDate::from_ymd_opt(2026, 3, d).unwrap()
|
|
}
|
|
|
|
fn usd(s: &str) -> Money {
|
|
Money::new(Decimal::from_str(s).unwrap(), Currency::USD)
|
|
}
|
|
|
|
struct Fx {
|
|
l: Ledger,
|
|
cash: AccountId,
|
|
revenue: AccountId,
|
|
expense: AccountId,
|
|
asset: AccountId,
|
|
accum: AccountId,
|
|
liability: AccountId,
|
|
}
|
|
|
|
fn fx() -> Fx {
|
|
let mut l = Ledger::new(Currency::USD);
|
|
let mut add = |code: &str, name: &str, t: AccountType| {
|
|
l.add_account(code, name, t, None, false).unwrap()
|
|
};
|
|
let cash = add("1000", "Cash", AccountType::Asset);
|
|
let asset = add("1500", "Equipment", AccountType::Asset);
|
|
let accum = add("1590", "Accumulated depreciation", AccountType::Asset);
|
|
let liability = add("2500", "Deferred", AccountType::Liability);
|
|
let revenue = add("4000", "Sales", AccountType::Revenue);
|
|
let expense = add("6000", "Supplies", AccountType::Expense);
|
|
Fx {
|
|
l,
|
|
cash,
|
|
revenue,
|
|
expense,
|
|
asset,
|
|
accum,
|
|
liability,
|
|
}
|
|
}
|
|
|
|
fn line(desc: &str) -> DocumentLine {
|
|
DocumentLine {
|
|
description: desc.into(),
|
|
amount: Decimal::from(10),
|
|
class_id: None,
|
|
account: None,
|
|
quantity: None,
|
|
unit_price: None,
|
|
}
|
|
}
|
|
|
|
fn holds_tin(r: Result<impl std::fmt::Debug, LedgerError>, field: &str) {
|
|
match r {
|
|
Err(LedgerError::FreeTextHoldsTin(why)) => {
|
|
assert!(
|
|
why.starts_with(&format!("{field} looks like it holds a tax id")),
|
|
"{why}"
|
|
);
|
|
assert!(
|
|
why.contains("books.set_tin") && why.contains("W-9"),
|
|
"{why}"
|
|
);
|
|
assert!(!why.contains("0000"), "never the number: {why}");
|
|
}
|
|
other => panic!("{field}: expected FreeTextHoldsTin, got {other:?}"),
|
|
}
|
|
}
|
|
|
|
/// An entry's description, a reversal's, a write-off's and a void's
|
|
/// reason, a period's name, an account's code.
|
|
#[test]
|
|
fn entries_periods_and_codes() {
|
|
let mut f = fx();
|
|
holds_tin(
|
|
f.l.try_create_entry(day(1), &format!("paid {TIN}")),
|
|
"the entry description",
|
|
);
|
|
// Nothing was made: the next entry is still the first.
|
|
let e =
|
|
f.l.try_create_entry(day(1), "inv 2026-11-0042, ref 1234567890")
|
|
.unwrap();
|
|
f.l.add_line(e, f.cash, Side::Debit, usd("5.00")).unwrap();
|
|
f.l.add_line(e, f.revenue, Side::Credit, usd("5.00"))
|
|
.unwrap();
|
|
f.l.post_entry(e).unwrap();
|
|
let before = f.l.clone();
|
|
holds_tin(
|
|
f.l.reverse_entry(e, day(2), &format!("oops {TIN}")),
|
|
"the reversal's description",
|
|
);
|
|
assert_eq!(f.l, before, "a refusal changes nothing");
|
|
holds_tin(
|
|
f.l.create_period(&format!("P {TIN}"), day(1), day(31)),
|
|
"the period name",
|
|
);
|
|
holds_tin(
|
|
f.l.add_account(
|
|
&TIN.replace('-', ""),
|
|
"Odd",
|
|
AccountType::Asset,
|
|
None,
|
|
false,
|
|
),
|
|
"the account code",
|
|
);
|
|
f.l.create_period("2026-03", day(1), day(31)).unwrap();
|
|
}
|
|
|
|
/// A document's memo, references, duplicate reason, lines and the
|
|
/// customer's printed details.
|
|
#[test]
|
|
fn documents() {
|
|
let mut f = fx();
|
|
let c = f.l.add_customer("Acme").unwrap();
|
|
let v = f.l.add_vendor("Beta").unwrap();
|
|
let inv = |l: &mut Ledger, lines: Vec<DocumentLine>, fields: InvoiceFields| {
|
|
l.create_invoice_with(c, day(1), day(31), lines, fields)
|
|
};
|
|
holds_tin(
|
|
inv(
|
|
&mut f.l,
|
|
vec![line(&format!("work {TIN}"))],
|
|
InvoiceFields::default(),
|
|
),
|
|
"a line's description",
|
|
);
|
|
holds_tin(
|
|
inv(
|
|
&mut f.l,
|
|
vec![line("ok")],
|
|
InvoiceFields {
|
|
memo: Some(format!("see {TIN}")),
|
|
..Default::default()
|
|
},
|
|
),
|
|
"the memo",
|
|
);
|
|
holds_tin(
|
|
inv(
|
|
&mut f.l,
|
|
vec![line("ok")],
|
|
InvoiceFields {
|
|
customer_ref: Some("PO 00-0000000".into()),
|
|
..Default::default()
|
|
},
|
|
),
|
|
"the reference",
|
|
);
|
|
let snap = |name: &str| PartySnapshot {
|
|
name: name.into(),
|
|
address: None,
|
|
email: None,
|
|
phone: None,
|
|
contact: None,
|
|
billing_address: None,
|
|
shipping_address: None,
|
|
billing_email: None,
|
|
};
|
|
// The customer's printed details are a party's: any nine digits.
|
|
let strict = inv(
|
|
&mut f.l,
|
|
vec![line("ok")],
|
|
InvoiceFields {
|
|
bill_to: Some(snap("Acme 1234 56789")),
|
|
..Default::default()
|
|
},
|
|
);
|
|
assert!(
|
|
matches!(strict, Err(LedgerError::InvalidParty(_))),
|
|
"{strict:?}"
|
|
);
|
|
// The company's own details are free text: its shapes only.
|
|
holds_tin(
|
|
inv(
|
|
&mut f.l,
|
|
vec![line("ok")],
|
|
InvoiceFields {
|
|
seller: Some(snap(&format!("Us {TIN}"))),
|
|
..Default::default()
|
|
},
|
|
),
|
|
"the seller name",
|
|
);
|
|
// Neither a 10-digit PO nor a ZIP+4 in the company's address is a tax id.
|
|
let mut us = snap("Us");
|
|
us.address = Some("1 Main St, Midvale UT 84047-1234".into());
|
|
inv(
|
|
&mut f.l,
|
|
vec![line("ok")],
|
|
InvoiceFields {
|
|
customer_ref: Some("1234567890".into()),
|
|
memo: Some("2026-11-0042".into()),
|
|
seller: Some(us),
|
|
..Default::default()
|
|
},
|
|
)
|
|
.unwrap();
|
|
let bill = |l: &mut Ledger, fields: BillFields| {
|
|
l.create_bill_with(v, day(1), day(31), vec![line("ok")], fields)
|
|
};
|
|
holds_tin(
|
|
bill(
|
|
&mut f.l,
|
|
BillFields {
|
|
vendor_ref: Some("000000000".into()),
|
|
..Default::default()
|
|
},
|
|
),
|
|
"the reference",
|
|
);
|
|
holds_tin(
|
|
bill(
|
|
&mut f.l,
|
|
BillFields {
|
|
memo: Some(TIN.into()),
|
|
..Default::default()
|
|
},
|
|
),
|
|
"the memo",
|
|
);
|
|
holds_tin(
|
|
bill(
|
|
&mut f.l,
|
|
BillFields {
|
|
duplicate_reason: Some(format!("again {TIN}")),
|
|
..Default::default()
|
|
},
|
|
),
|
|
"the duplicate reason",
|
|
);
|
|
bill(
|
|
&mut f.l,
|
|
BillFields {
|
|
vendor_ref: Some("INV-2026-11-0042".into()),
|
|
..Default::default()
|
|
},
|
|
)
|
|
.unwrap();
|
|
}
|
|
|
|
/// Assets, schedules, recurring entries and payroll names.
|
|
#[test]
|
|
fn assets_schedules_and_payroll() {
|
|
let mut f = fx();
|
|
let new = |d: &str| NewAsset {
|
|
description: d.into(),
|
|
in_service_date: day(1),
|
|
cost: usd("1200.00"),
|
|
salvage_value: usd("0"),
|
|
useful_life_months: 12,
|
|
cost_account: f.asset,
|
|
accum_depr_account: f.accum,
|
|
expense_account: f.expense,
|
|
};
|
|
holds_tin(
|
|
f.l.register_asset(new(&format!("van {TIN}"))),
|
|
"the asset description",
|
|
);
|
|
let a = f.l.register_asset(new("van 1234567890")).unwrap();
|
|
holds_tin(
|
|
f.l.set_asset_serial(a, Some("SN 00-0000000")),
|
|
"the serial number",
|
|
);
|
|
holds_tin(
|
|
f.l.set_asset_description(a, "000000000"),
|
|
"the asset description",
|
|
);
|
|
holds_tin(f.l.set_asset_tag(a, "T-000-00-0000"), "the asset tag");
|
|
f.l.set_asset_serial(a, Some("C02XK1ABCDEF")).unwrap();
|
|
let sched = |d: &str| NewSchedule {
|
|
description: d.into(),
|
|
kind: ScheduleKind::PrepaidExpense,
|
|
total: usd("1200.00"),
|
|
start: day(1),
|
|
months: 12,
|
|
balance_account: f.asset,
|
|
pl_account: f.expense,
|
|
convention: ScheduleConvention::Daily,
|
|
};
|
|
holds_tin(
|
|
f.l.register_schedule(sched(&format!("rent {TIN}"))),
|
|
"the schedule's description",
|
|
);
|
|
let _ = f.liability;
|
|
let rec = |d: &str| NewRecurring {
|
|
description: d.into(),
|
|
lines: vec![
|
|
(f.expense, Side::Debit, usd("5.00")),
|
|
(f.cash, Side::Credit, usd("5.00")),
|
|
],
|
|
frequency: Frequency::Monthly,
|
|
start: day(1),
|
|
end: None,
|
|
};
|
|
holds_tin(
|
|
f.l.register_recurring(rec(&format!("fee {TIN}"))),
|
|
"the recurring entry's description",
|
|
);
|
|
f.l.register_recurring(rec("fee 2026-11-0042")).unwrap();
|
|
}
|
|
|
|
/// A draft's note, a discard's reason and a no-party reason: free
|
|
/// text now, no longer the strict nine-digit reading.
|
|
#[test]
|
|
fn notes_and_reasons_use_the_shapes() {
|
|
let mut f = fx();
|
|
let e = f.l.try_create_entry(day(1), "x").unwrap();
|
|
let wait = |n: &str| ledger_core::DraftState::AwaitingEvidence { note: n.into() };
|
|
assert!(
|
|
f.l.set_draft_state(e, wait(&format!("waiting {TIN}")))
|
|
.is_err()
|
|
);
|
|
f.l.set_draft_state(e, wait("waiting on ref 1234567890, inv 2026-11-0042"))
|
|
.unwrap();
|
|
assert!(Ledger::check_no_party_reason(&format!("own account {TIN}")).is_err());
|
|
Ledger::check_no_party_reason("own account 1234567890").unwrap();
|
|
assert!(f.l.discard_draft(e, &format!("dup {TIN}")).is_err());
|
|
f.l.discard_draft(e, "entered twice, ref 1234567890")
|
|
.unwrap();
|
|
}
|
|
|
|
/// BS-entry-4: a journal line an earlier version took replays, whatever
|
|
/// it holds; the books open on it. (A replay is the only way such a
|
|
/// line is ever applied: every command refuses it.)
|
|
#[test]
|
|
fn a_journaled_line_replays_whatever_it_holds() {
|
|
let mut f = fx();
|
|
let e = f.l.create_entry(day(1), &format!("paid {TIN}"));
|
|
f.l.add_line(e, f.cash, Side::Debit, usd("5.00")).unwrap();
|
|
f.l.add_line(e, f.revenue, Side::Credit, usd("5.00"))
|
|
.unwrap();
|
|
f.l.post_entry(e).unwrap();
|
|
let r =
|
|
f.l.replay_reverse_entry(e, day(2), &format!("undo {TIN}"))
|
|
.unwrap();
|
|
assert_eq!(
|
|
f.l.get_entry(r).unwrap().description(),
|
|
format!("undo {TIN}")
|
|
);
|
|
let ann = ledger_core::Annotation {
|
|
attachment_id: 1,
|
|
fields: ledger_core::AnnotationFields {
|
|
payee: Some(format!("Pat {TIN}")),
|
|
..Default::default()
|
|
},
|
|
note: Some(format!("see {TIN}")),
|
|
};
|
|
f.l.replay_annotate_attachment(ann.clone()).unwrap();
|
|
// The same annotation as a command is refused.
|
|
holds_tin(f.l.annotate_attachment(ann), "the payee");
|
|
}
|
|
}
|