Skip to main content

btctax_cli/
return_inputs.rs

1//! Per-year `return_inputs(year, inputs_json)` side-table — the full-return v1 input surface (line
2//! items + PII + payments). A projection input, **not** ledger state (NFR6). Mirrors `tax_profile.rs`
3//! exactly (idempotent DDL, robust-to-older-vaults guard, typed error on bad JSON) — one JSON-encoded
4//! [`ReturnInputs`] per tax year, stored inside the encrypted vault.
5use crate::CliError;
6use btctax_core::tax::return_inputs::ReturnInputs;
7use rusqlite::{Connection, OptionalExtension};
8use std::collections::BTreeMap;
9
10/// The current row schema.
11///
12/// - **0** — pre-D-8. `can_be_claimed_as_dependent_*` were bare `bool`s (unanswered indistinguishable
13///   from "No").
14/// - **1** — those flags became tri-state `Option<bool>`.
15/// - **2** — P9: `Person.blind` and `ScheduleAInputs.salt_use_sales_tax` became tri-state; `hsa_present`
16///   was renamed `hsa_activity` (a *different* question); `dual_status_alien` and the mixed-use-mortgage
17///   box were added.
18///
19/// ★ **P9 §2.6 — there is no migration.** The owner confirmed no real tax data has ever been entered, so a
20/// row at any version other than the current one **REFUSES** (`row_to_inputs`) rather than being read or
21/// per-key unlaundered. A version check cannot forget a key; a hand-written unlaunder list can, and did
22/// (Fable r4 I-4). Retire this the moment real data exists — the first real return needs true migrations,
23/// and prior-year carryforwards are exactly what a filer cannot reconstruct (FOLLOWUPS, release gate).
24pub const SCHEMA_VERSION: i64 = 2;
25
26/// Create the `return_inputs` side-table if it does not exist, and bring an OLDER vault's table up to the
27/// current schema. Idempotent — it runs on every `get`/`set`, so it must be safe to call repeatedly.
28pub fn init_table(conn: &Connection) -> Result<(), CliError> {
29    conn.execute_batch(
30        "CREATE TABLE IF NOT EXISTS return_inputs \
31         (year INTEGER PRIMARY KEY, inputs_json TEXT NOT NULL, schema_version INTEGER NOT NULL DEFAULT 0);",
32    )?;
33    // A vault created BEFORE this column existed still has the 2-column table — `CREATE TABLE IF NOT
34    // EXISTS` is a no-op on it and adds nothing. SQLite has no `ADD COLUMN IF NOT EXISTS`, and this runs
35    // on every command, so: attempt the ALTER and tolerate EXACTLY the already-applied error. Any other
36    // error is real and propagates.
37    if let Err(e) = conn.execute_batch(
38        "ALTER TABLE return_inputs ADD COLUMN schema_version INTEGER NOT NULL DEFAULT 0;",
39    ) {
40        let msg = e.to_string();
41        if !msg.contains("duplicate column name") {
42            return Err(e.into());
43        }
44    }
45    Ok(())
46}
47
48/// ★ The ONE read boundary. Every path that turns a stored blob into a [`ReturnInputs`] goes through
49/// here — `get` AND `all` — so the version gate cannot be applied on one path and forgotten on the other.
50///
51/// **P9 §2.6 — refuse-and-reimport, not migrate.** A row whose `version` is anything other than the
52/// current [`SCHEMA_VERSION`] REFUSES (`StaleReturnInputs`). This is fail-closed in both directions: an
53/// OLDER row would deserialize its now-`Option` fields' stored `false` as `Some(false)` — a never-asked
54/// default ratified as the filer's answer, the D-8 laundering — and a NEWER row would be half-read. The
55/// remedy (named in the error) is `income clear` → `income import` → `report --write-carryover`; `clear`
56/// discards the row's computed carryover, so the rebuild step is not optional. There is no per-key
57/// migration to forget a key (Fable r4 I-4), because there is no real data to migrate yet.
58fn row_to_inputs(year: i32, json: &str, version: i64) -> Result<ReturnInputs, CliError> {
59    if version != SCHEMA_VERSION {
60        return Err(CliError::StaleReturnInputs {
61            year,
62            found: version,
63            expected: SCHEMA_VERSION,
64        });
65    }
66    serde_json::from_str(json).map_err(|e| CliError::BadConfigValue {
67        key: format!("return_inputs[{year}]"),
68        value: format!("invalid JSON: {e}"),
69    })
70}
71
72/// Return the stored [`ReturnInputs`] for `year`, or `None` if none has been set.
73/// Robust to older vaults (ensures the table first); typed error on malformed JSON.
74pub fn get(conn: &Connection, year: i32) -> Result<Option<ReturnInputs>, CliError> {
75    init_table(conn)?;
76    let json: Option<(String, i64)> = conn
77        .query_row(
78            "SELECT inputs_json, schema_version FROM return_inputs WHERE year=?1",
79            [year],
80            |r| Ok((r.get(0)?, r.get(1)?)),
81        )
82        .optional()?;
83    match json {
84        None => Ok(None),
85        Some((j, v)) => Ok(Some(row_to_inputs(year, &j, v)?)),
86    }
87}
88
89/// Persist `ri` as the [`ReturnInputs`] for `year` (upsert — replaces any prior value).
90pub fn set(conn: &Connection, year: i32, ri: &ReturnInputs) -> Result<(), CliError> {
91    init_table(conn)?;
92    let j = serde_json::to_string(ri).map_err(|e| CliError::BadConfigValue {
93        key: format!("return_inputs[{year}]"),
94        value: e.to_string(),
95    })?;
96    // ★ The DO-UPDATE branch MUST stamp the version too. The shipped upsert named `inputs_json` alone —
97    // so writing an answer onto a version-0 row would have left it at version 0, the read-time fixup would
98    // RE-FIRE on the very next `get`, and the filer's answered `false` would be laundered straight back to
99    // `None`. The bug would have reconstituted itself out of its own fix.
100    conn.execute(
101        "INSERT INTO return_inputs(year,inputs_json,schema_version) VALUES(?1,?2,?3) \
102         ON CONFLICT(year) DO UPDATE SET inputs_json=excluded.inputs_json, \
103                                         schema_version=excluded.schema_version",
104        rusqlite::params![year, j, SCHEMA_VERSION],
105    )?;
106    Ok(())
107}
108
109/// Whether a `ReturnInputs` exists for `year` (used by the `tax-profile set` guard — SPEC §4.12/D-4).
110/// A `SELECT 1` existence probe (does not deserialize the blob — review M5).
111pub fn exists(conn: &Connection, year: i32) -> Result<bool, CliError> {
112    init_table(conn)?;
113    let found: Option<i64> = conn
114        .query_row("SELECT 1 FROM return_inputs WHERE year=?1", [year], |r| {
115            r.get(0)
116        })
117        .optional()?;
118    Ok(found.is_some())
119}
120
121/// Delete the stored `ReturnInputs` for `year` (used by `income clear`). Returns `true` if a row existed.
122pub fn delete(conn: &Connection, year: i32) -> Result<bool, CliError> {
123    init_table(conn)?;
124    let n = conn.execute("DELETE FROM return_inputs WHERE year=?1", [year])?;
125    Ok(n > 0)
126}
127
128/// The years that have stored inputs, ascending — WITHOUT deserializing any blob, so a single corrupt
129/// row cannot break enumeration (review N3: one bad blob must not brick the read-only viewer).
130pub fn years(conn: &Connection) -> Result<Vec<i32>, CliError> {
131    init_table(conn)?;
132    let mut stmt = conn.prepare("SELECT year FROM return_inputs ORDER BY year")?;
133    let rows = stmt.query_map([], |r| r.get::<_, i32>(0))?;
134    Ok(rows.collect::<Result<Vec<_>, _>>()?)
135}
136
137/// Return all stored inputs, sorted by year ascending.
138pub fn all(conn: &Connection) -> Result<BTreeMap<i32, ReturnInputs>, CliError> {
139    init_table(conn)?;
140    let mut stmt = conn
141        .prepare("SELECT year, inputs_json, schema_version FROM return_inputs ORDER BY year")?;
142    let rows = stmt.query_map([], |r| {
143        Ok((r.get::<_, i32>(0)?, r.get::<_, String>(1)?, r.get::<_, i64>(2)?))
144    })?;
145    let mut out = BTreeMap::new();
146    for row in rows {
147        let (y, j, v) = row?;
148        // Same read boundary as `get` — NOT a second `from_str` (that is how the two paths drift apart).
149        out.insert(y, row_to_inputs(y, &j, v)?);
150    }
151    Ok(out)
152}
153
154#[cfg(test)]
155mod tests {
156    use super::*;
157    use btctax_core::tax::return_inputs::{Owner, W2};
158    use btctax_core::FilingStatus;
159    use rust_decimal_macros::dec;
160
161    fn mem() -> Connection {
162        let c = Connection::open_in_memory().unwrap();
163        init_table(&c).unwrap();
164        c
165    }
166
167    fn inputs() -> ReturnInputs {
168        ReturnInputs {
169            filing_status: FilingStatus::Mfj,
170            w2s: vec![W2 {
171                owner: Owner::Taxpayer,
172                employer: "ACME".into(),
173                box1_wages: dec!(82000),
174                box2_fed_withheld: dec!(9100),
175                ..Default::default()
176            }],
177            ..Default::default()
178        }
179    }
180
181    #[test]
182    fn set_then_get_round_trips() {
183        let c = mem();
184        set(&c, 2024, &inputs()).unwrap();
185        assert_eq!(get(&c, 2024).unwrap().unwrap(), inputs());
186        assert_eq!(get(&c, 2025).unwrap(), None);
187        assert!(exists(&c, 2024).unwrap());
188        assert!(!exists(&c, 2025).unwrap());
189    }
190
191    #[test]
192    fn get_on_tableless_vault_is_ok_none() {
193        let c = Connection::open_in_memory().unwrap(); // no init_table
194        assert_eq!(get(&c, 2024).unwrap(), None);
195    }
196
197    #[test]
198    fn bad_json_is_a_typed_error_not_a_panic() {
199        let c = mem();
200        // At the CURRENT schema version (so the stale-row gate passes and we reach the JSON parse): a
201        // malformed blob must be a typed error, not a panic. (A row omitting `schema_version` defaults to
202        // 0 and would refuse as stale — a different, correct path tested in `p9_stale_row_refuses`.)
203        c.execute(
204            "INSERT INTO return_inputs(year,inputs_json,schema_version) VALUES(2024,'not json',?1)",
205            [SCHEMA_VERSION],
206        )
207        .unwrap();
208        assert!(matches!(
209            get(&c, 2024).unwrap_err(),
210            CliError::BadConfigValue { .. }
211        ));
212    }
213
214    #[test]
215    fn all_returns_sorted_by_year() {
216        let c = mem();
217        set(&c, 2025, &inputs()).unwrap();
218        set(&c, 2024, &inputs()).unwrap();
219        assert_eq!(
220            all(&c).unwrap().keys().copied().collect::<Vec<_>>(),
221            vec![2024, 2025]
222        );
223    }
224
225    #[test]
226    fn delete_removes_the_row() {
227        let c = mem();
228        set(&c, 2024, &inputs()).unwrap();
229        assert!(exists(&c, 2024).unwrap());
230        assert!(delete(&c, 2024).unwrap()); // existed
231        assert!(!exists(&c, 2024).unwrap());
232        assert!(!delete(&c, 2024).unwrap()); // idempotent — nothing to remove
233    }
234}
235
236
237#[cfg(test)]
238mod p9_stale_row_refuses {
239    //! ★ P9 §2.6 — there is NO migration. A stored row whose `schema_version` is not the current one
240    //! REFUSES (`StaleReturnInputs`) rather than being silently read or per-key unlaundered. The owner
241    //! confirmed no real data has ever been entered, so refuse-and-reimport is lawful — and a version check
242    //! cannot forget a key, unlike the hand-written unlaunder list this replaces (whose `blind ×2`
243    //! mutation-check went vacuous — Fable r4 I-4). This module replaces the old `p8a_migration_tests`,
244    //! which tested the now-deleted v0→v1 unlaunder.
245    use super::*;
246    use rusqlite::Connection;
247
248    /// A vault holding a row at an OLD schema version, in the current 3-column table.
249    fn vault_with_row_at_version(year: i32, version: i64) -> Connection {
250        let conn = Connection::open_in_memory().unwrap();
251        init_table(&conn).unwrap();
252        // A well-formed blob (valid JSON) — the point is the VERSION, not a parse failure.
253        let blob = serde_json::to_string(&ReturnInputs::default()).unwrap();
254        conn.execute(
255            "INSERT INTO return_inputs(year,inputs_json,schema_version) VALUES(?1,?2,?3)",
256            rusqlite::params![year, blob, version],
257        )
258        .unwrap();
259        conn
260    }
261
262    /// ★ THE POINT. A pre-P9 row (v0, the pre-D-8 schema) must REFUSE — never be read. Its `blind`/`salt`
263    /// bools would deserialize to `Some(false)` (a never-asked default ratified as an answer), which is the
264    /// D-8 laundering; refusing is the fail-closed reading.
265    #[test]
266    fn a_version_0_row_refuses_stale() {
267        let conn = vault_with_row_at_version(2024, 0);
268        assert!(
269            matches!(get(&conn, 2024), Err(CliError::StaleReturnInputs { year: 2024, found: 0, expected }) if expected == SCHEMA_VERSION),
270            "a v0 row must refuse as stale, naming the version"
271        );
272    }
273
274    /// A v1 row (the post-D-8, pre-P9 schema) is equally stale — the `blind`/`salt` type flips landed in P9.
275    #[test]
276    fn a_version_1_row_refuses_stale() {
277        let conn = vault_with_row_at_version(2024, 1);
278        assert!(
279            matches!(get(&conn, 2024), Err(CliError::StaleReturnInputs { found: 1, .. })),
280            "a v1 row must refuse as stale"
281        );
282    }
283
284    /// ★ `all()` is the module's OTHER deserializer — it must refuse identically, or a reader (the TUI's
285    /// per-year resolution, `income show --all`) sees a laundered row `get` would have refused.
286    #[test]
287    fn all_refuses_a_stale_row_identically_to_get() {
288        let conn = vault_with_row_at_version(2024, 1);
289        assert!(
290            matches!(all(&conn), Err(CliError::StaleReturnInputs { found: 1, .. })),
291            "`all()` must apply the same version gate as `get()`"
292        );
293    }
294
295    /// ★ FORWARD guard (r3 Nit-2): a row written by a NEWER build (version > current) is also stale — the
296    /// same `!=` covers it. P9 creates the first-ever version skew, and a half-read future row is exactly
297    /// the class this spec closes.
298    #[test]
299    fn a_future_version_row_refuses_too() {
300        let conn = vault_with_row_at_version(2024, SCHEMA_VERSION + 1);
301        assert!(
302            matches!(get(&conn, 2024), Err(CliError::StaleReturnInputs { .. })),
303            "a future-version row must refuse, not be half-read"
304        );
305    }
306
307    /// A row at the CURRENT version reads normally — the gate is exact, not a blanket refusal.
308    #[test]
309    fn a_current_version_row_reads() {
310        let conn = vault_with_row_at_version(2024, SCHEMA_VERSION);
311        assert!(get(&conn, 2024).unwrap().is_some(), "a current-version row must read");
312    }
313
314    /// The stale-row refusal's message names all THREE remedy commands, in order (§2.6 / r6 I-1): `clear`
315    /// discards the computed carryover, so `import` alone is not a complete recovery. Mutation: drop the
316    /// rebuild clause from the `#[error(...)]` string ⇒ this fails.
317    #[test]
318    fn the_stale_message_names_the_full_three_command_remedy() {
319        let msg = CliError::StaleReturnInputs { year: 2024, found: 1, expected: 2 }.to_string();
320        assert!(msg.contains("income clear 2024"), "names clear");
321        assert!(msg.contains("income import"), "names import");
322        assert!(
323            msg.contains("--write-carryover"),
324            "names the rebuild — disclosure is not restoration (r6 I-1)"
325        );
326        assert!(msg.contains("2023"), "the rebuild targets the PRIOR year (year-1)");
327    }
328}