1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! GAP-SG-205: every document ships in two languages, and nothing held them
//! level.
//!
//! `tests/i18n_bilingual_integration.rs` proves the ERROR MESSAGES are paired.
//! `tests/docs_consistency.rs` guards `gaps.md`. Neither looks at the
//! `X.md` / `X.pt-BR.md` pairs, so a section could be added, corrected or
//! retired on one side and quietly skipped on the other. Nine pairs had
//! drifted when this was written, and one of them was not cosmetic:
//! `CROSS_PLATFORM.pt-BR.md` still described an active CI job in a project
//! that forbids CI, because the English side had been corrected and the
//! Portuguese side had not.
//!
//! # Why the count and not the text
//!
//! The headings are TRANSLATED — "Platform contracts" against "Contratos de
//! plataforma" — so a textual diff is noise by construction. What must match is
//! the STRUCTURE: same number of `##` sections, in the same order, so a section
//! present in one language has a counterpart in the other. That is mechanical,
//! it has no opinion about translation quality, and it catches exactly the
//! failure mode above.
use std::path::{Path, PathBuf};
/// Repository root.
fn root() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).to_path_buf()
}
/// Counts the `##` headings of a markdown file, ignoring fenced code blocks.
///
/// A `## ` inside a fence is shell output or sample markdown, not a section of
/// this document. Counting it would make the guard fail on a correct pair.
fn section_count(markdown: &str) -> usize {
let mut inside_fence = false;
let mut count = 0;
for line in markdown.lines() {
if line.trim_start().starts_with("```") {
inside_fence = !inside_fence;
continue;
}
if inside_fence {
continue;
}
// `##` exactly: `###` is a subsection and pairs are allowed to differ
// in how deeply they subdivide a section.
if line.starts_with("## ") {
count += 1;
}
}
count
}
/// Pairs whose section counts are allowed to differ, each with its reason.
///
/// An allowlist entry is a claim that has to survive being read out loud, and
/// it has to survive the repository changing under it. The earlier reason here
/// said the English COOKBOOK duplicated 68 sections verbatim, which was true
/// when written and stopped being true the moment those duplicates were
/// removed. A stale justification is worse than no allowlist: the hole stays
/// open while the sentence explaining it no longer describes anything.
///
/// Re-measured after the deduplication: English carries 96 unique sections in
/// 146 184 bytes, Portuguese 82 sections in 152 983 bytes. The Portuguese file
/// is the LARGER of the two while holding fourteen fewer headings, so the gap
/// is how each side partitions the same material, not material the Portuguese
/// side lacks.
const KNOWN_ASYMMETRIES: &[(&str, &str)] = &[(
"docs/COOKBOOK",
"Portuguese holds 14 fewer headings over MORE bytes (152 983 against \
146 184), so the two sides partition the same material differently; the \
English duplication that once explained this entry was removed and the \
asymmetry survived it",
)];
/// Document stems that ship in both languages.
fn bilingual_stems() -> Vec<String> {
let mut out = Vec::new();
let candidates = [
"README",
"CHANGELOG",
"CONTRIBUTING",
"SECURITY",
"INTEGRATIONS",
"docs/AGENTS",
"docs/COOKBOOK",
"docs/CROSS_PLATFORM",
"docs/HEADLESS_INVOCATION",
"docs/HOW_TO_USE",
"docs/MIGRATION",
"docs/TESTING",
];
for stem in candidates {
let en = root().join(format!("{stem}.md"));
let pt = root().join(format!("{stem}.pt-BR.md"));
if en.exists() && pt.exists() {
out.push(stem.to_string());
}
}
out
}
#[test]
fn the_gate_found_the_pairs_it_is_supposed_to_read() {
let stems = bilingual_stems();
assert!(
stems.len() >= 10,
"only {} bilingual pairs found, so the list below went stale and every \
assertion would pass by not looking: {stems:?}",
stems.len()
);
}
#[test]
fn every_bilingual_pair_has_the_same_section_count() {
let mut drifted = Vec::new();
for stem in bilingual_stems() {
if KNOWN_ASYMMETRIES.iter().any(|(known, _)| *known == stem) {
continue;
}
let en = std::fs::read_to_string(root().join(format!("{stem}.md")))
.unwrap_or_else(|e| panic!("{stem}.md unreadable: {e}"));
let pt = std::fs::read_to_string(root().join(format!("{stem}.pt-BR.md")))
.unwrap_or_else(|e| panic!("{stem}.pt-BR.md unreadable: {e}"));
let (en_count, pt_count) = (section_count(&en), section_count(&pt));
if en_count != pt_count {
drifted.push(format!(
"{stem}: EN has {en_count} sections, pt-BR has {pt_count}"
));
}
}
assert!(
drifted.is_empty(),
"these documents ship in two languages and no longer describe the same \
product. A section added, corrected or retired on one side and skipped \
on the other is how `CROSS_PLATFORM.pt-BR.md` kept advertising a CI job \
this project forbids, months after the English side was fixed.\n\
Translate the missing section, or add the pair to KNOWN_ASYMMETRIES \
with a reason that survives being read out loud.\n{}",
drifted.join("\n")
);
}
#[test]
fn every_allowlisted_asymmetry_still_names_a_real_pair() {
// An allowlist that outlives its subject is worse than no allowlist: it
// silently exempts a file that may have been renamed onto a real gap.
for (stem, reason) in KNOWN_ASYMMETRIES {
assert!(
root().join(format!("{stem}.md")).exists()
&& root().join(format!("{stem}.pt-BR.md")).exists(),
"KNOWN_ASYMMETRIES names `{stem}`, which is not a bilingual pair \
anymore; remove the entry"
);
assert!(
reason.len() > 40,
"the exemption for `{stem}` needs a reason someone can check, not a \
label"
);
}
}
#[test]
fn the_section_counter_ignores_headings_inside_a_fence() {
let sample = "## Real\ntext\n```\n## Not a section\n```\n## Also real\n";
assert_eq!(
section_count(sample),
2,
"a `##` inside a code fence is sample output, not a section of the \
document; counting it would fail a correct pair"
);
}
#[test]
fn the_section_counter_ignores_deeper_headings() {
let sample = "## Section\n### Subsection\n#### Deeper\n## Second\n";
assert_eq!(section_count(sample), 2);
}