Skip to main content

max / makenotwork

8.0 KB · 211 lines History Blame Raw
1 //! Coverage ratchet for the two areas where being wrong is most expensive:
2 //! anything that moves money, and anything that holds a creator's data.
3 //!
4 //! The same idea as `test_hygiene.rs`, turned on the source rather than on the
5 //! tests. It counts files in those areas that contain no test of any kind and
6 //! fails when the number goes up. It cannot make anyone write a good test; it
7 //! can stop a new payment handler or sync table from landing with none at all,
8 //! which is how the current 34 accumulated, one reasonable-looking file at a
9 //! time.
10 //!
11 //! Deliberately dumb. A file counts as covered if it holds a test attribute, if
12 //! its directory has a sibling `tests.rs`, or if a file under `tests/` names it
13 //! as the subject of a contract test. No coverage instrumentation, no AST walk:
14 //! a rule cheap enough to state as
15 //! "how many files look like this" is a rule that stays honest, and mutation
16 //! testing (`.cargo/mutants.toml`) is where the harder question of whether the
17 //! tests are any *good* gets asked.
18 //!
19 //! Method, tiers and the full file list: wiki `testing-posture`.
20 //!
21 //! Run with: cargo test --test untested_money_paths
22
23 use std::collections::HashSet;
24 use std::fs;
25 use std::path::{Path, PathBuf};
26
27 /// Money and user-data files with no test at all: 34 on 2026-08-07, down from
28 /// 41, which was down from 50 on 2026-08-04.
29 ///
30 /// The drop from 41 is not seven new tests. It is `declared_contract_subjects`
31 /// below finally crediting the eight files whose tests live in a
32 /// `tests/workflows/db_*.rs` contract file, which the count had been reporting
33 /// as untested all along. The seal got more accurate, not looser.
34 ///
35 /// Lower it when you cover one. Never raise it: a new untested file in these
36 /// areas is the thing this seal exists to refuse. If you genuinely need to add
37 /// one, the honest move is to write the test, not to bump the constant.
38 const UNTESTED_HIGH_WATER: usize = 34;
39
40 /// Anything that moves money or decides what someone is entitled to.
41 const MONEY: &[&str] = &[
42 "src/payments/",
43 "src/routes/stripe/",
44 "src/pricing",
45 "src/tier_prices.rs",
46 "src/synckit_billing.rs",
47 "src/db/transactions/",
48 "src/db/subscriptions.rs",
49 "src/db/promo_codes.rs",
50 "src/db/fan_plus.rs",
51 "src/db/creator_tiers/",
52 "src/helpers/billing.rs",
53 ];
54
55 /// Anything that stores, serves or synchronises a creator's own data.
56 const USER_DATA: &[&str] = &[
57 "src/storage.rs",
58 "src/routes/storage/",
59 "src/db/items/",
60 "src/db/users.rs",
61 "src/db/synckit/",
62 "src/routes/synckit/",
63 "src/import/",
64 "src/db/pending_",
65 ];
66
67 #[test]
68 fn untested_money_and_data_files_do_not_increase() {
69 let mut money = Vec::new();
70 let mut data = Vec::new();
71 let subjects = declared_contract_subjects();
72
73 for path in rs_files(Path::new("src")) {
74 let rel = path.to_string_lossy().replace('\\', "/");
75 let in_money = MONEY.iter().any(|p| rel.starts_with(p));
76 let in_data = USER_DATA.iter().any(|p| rel.starts_with(p));
77 if !in_money && !in_data {
78 continue;
79 }
80 if has_test(&path) || sibling_tests_file(&path) || subjects.contains(&module_path_of(&rel))
81 {
82 continue;
83 }
84 if in_money { &mut money } else { &mut data }.push(rel);
85 }
86
87 money.sort();
88 data.sort();
89 let total = money.len() + data.len();
90
91 assert!(
92 total <= UNTESTED_HIGH_WATER,
93 "untested money/data files rose from {UNTESTED_HIGH_WATER} to {total} \
94 (money {}, data {}).\n\
95 A new file on these paths needs a test before it lands. If the logic is \
96 async and database-bound, that is a contract test in \
97 `tests/workflows/db_*.rs` whose header reads \"contract tests for \
98 `your::module`\", not a unit test.\n\
99 Money: {money:#?}\nUser data: {data:#?}",
100 money.len(),
101 data.len(),
102 );
103
104 assert_eq!(
105 total, UNTESTED_HIGH_WATER,
106 "untested money/data files fell to {total}. Lower UNTESTED_HIGH_WATER to {total}.",
107 );
108 }
109
110 /// Whether the file carries any test of its own.
111 ///
112 /// Matches the attribute rather than `#[cfg(test)]`, because a `#[cfg(test)]`
113 /// block is not a test. `db/subscriptions.rs` held 811 lines of subscription
114 /// logic behind a `#[cfg(test)]` that contained one test-only constructor and
115 /// no test, and counting modules instead of tests is what hid it.
116 fn has_test(path: &Path) -> bool {
117 let Ok(text) = fs::read_to_string(path) else {
118 return false;
119 };
120 text.contains("#[test]") || text.contains("#[tokio::test") || text.contains("#[sqlx::test")
121 }
122
123 /// Whether the file's own directory has a `tests.rs` covering it.
124 ///
125 /// `db/creator_tiers/` keeps its 25 tests in a sibling file, which is the only
126 /// place in the crate that does. Without this, the seal would report three
127 /// well-covered files as untested and the number would stop meaning anything.
128 fn sibling_tests_file(path: &Path) -> bool {
129 path.parent()
130 .is_some_and(|dir| dir.join("tests.rs").exists())
131 }
132
133 /// Every module named as the subject of a contract-test file under `tests/`.
134 ///
135 /// The convention is a doc header reading "contract tests for `db::foo::bar`",
136 /// which twenty-odd files in `tests/workflows/` already follow. Crediting it
137 /// closes a hole that made this seal contradict its own advice: the failure
138 /// message tells you a database-bound module wants a contract test in `tests/`
139 /// rather than a unit test, and then the count refused to see the file you
140 /// wrote. `db/synckit/invitations.rs` landed on 2026-08-07 with eleven such
141 /// tests and was still reported untested.
142 ///
143 /// Deliberately as dumb as the rest of the seal: a declared subject, not an
144 /// inferred one. A test file that does not say what it covers is not credited,
145 /// which keeps the rule cheap to state and hard to satisfy by accident.
146 fn declared_contract_subjects() -> HashSet<String> {
147 let mut subjects = HashSet::new();
148 for path in rs_files(Path::new("tests")) {
149 let Ok(text) = fs::read_to_string(&path) else {
150 continue;
151 };
152 let header: String = text
153 .lines()
154 .take_while(|l| l.starts_with("//!") || l.trim().is_empty())
155 .collect::<Vec<_>>()
156 .join(" ");
157 if !header.contains("contract tests for") {
158 continue;
159 }
160 // Every backticked path in the header, so a file covering several
161 // modules ("`db::tips`, `db::license_keys`, `db::pending_refunds`")
162 // credits all of them.
163 //
164 // Underscores are part of a module name, not a separator: without them
165 // `db::pending_refunds` reads as prose and goes uncredited, which is
166 // how the first cut of this seal landed on 35 instead of 34.
167 for chunk in header.split('`').skip(1).step_by(2) {
168 let path_shaped = chunk.contains("::")
169 && !chunk.ends_with(':')
170 && chunk
171 .chars()
172 .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_' || c == ':');
173 if path_shaped {
174 subjects.insert(chunk.to_string());
175 }
176 }
177 }
178 subjects
179 }
180
181 /// The module path a source file defines, as a contract-test header would spell
182 /// it: `src/db/synckit/invitations.rs` -> `db::synckit::invitations`, and a
183 /// `mod.rs` names its directory rather than itself.
184 fn module_path_of(rel: &str) -> String {
185 rel.trim_start_matches("src/")
186 .trim_end_matches(".rs")
187 .replace('/', "::")
188 .trim_end_matches("::mod")
189 .to_string()
190 }
191
192 fn rs_files(dir: &Path) -> Vec<PathBuf> {
193 let mut out = Vec::new();
194 let mut stack = vec![dir.to_path_buf()];
195 while let Some(d) = stack.pop() {
196 let Ok(entries) = fs::read_dir(&d) else {
197 continue;
198 };
199 for entry in entries.flatten() {
200 let p = entry.path();
201 if p.is_dir() {
202 stack.push(p);
203 } else if p.extension().is_some_and(|e| e == "rs") {
204 out.push(p);
205 }
206 }
207 }
208 out.sort();
209 out
210 }
211