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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! TDD tests for TDG git hooks integration (Sprint 66 Phase 3)
//!
//! Following Toyota Way Extreme TDD approach:
//! 1. RED: Write failing tests first
//! 2. GREEN: Implement minimum code to pass
//! 3. REFACTOR: Keep complexity ≤30 cyclomatic, ≤25 cognitive
use anyhow::Result;
use std::fs;
use std::path::PathBuf;
use tempfile::TempDir;
/// Test fixture for TDG hooks testing
struct TdgHooksFixture {
#[allow(dead_code)]
temp_dir: TempDir,
#[allow(dead_code)]
project_root: PathBuf,
#[allow(dead_code)]
git_dir: PathBuf,
hooks_dir: PathBuf,
pmat_dir: PathBuf,
tdg_rules_path: PathBuf,
}
impl TdgHooksFixture {
/// Create test fixture with git repository and TDG configuration
fn new() -> Result<Self> {
let temp_dir = tempfile::tempdir()?;
let project_root = temp_dir.path().to_path_buf();
let git_dir = project_root.join(".git");
let hooks_dir = git_dir.join("hooks");
let pmat_dir = project_root.join(".pmat");
let tdg_rules_path = pmat_dir.join("tdg-rules.toml");
// Create directories
fs::create_dir_all(&hooks_dir)?;
fs::create_dir_all(&pmat_dir)?;
// Create sample TDG rules configuration
let sample_config = r#"
[quality_gates]
rust_min_grade = "B+"
typescript_min_grade = "B+"
python_min_grade = "B"
max_score_drop = 5.0
allow_grade_drop = false
mode = "strict"
block_on_regression = true
block_on_new_files_below_threshold = true
[baseline]
auto_update_on_commit = true
auto_update_on_merge = true
baseline_path = ".pmat/baseline.json"
store_in_git = true
[ci_cd]
fail_fast = false
generate_reports = true
comment_on_pr = true
"#;
fs::write(&tdg_rules_path, sample_config)?;
Ok(Self {
temp_dir,
project_root,
git_dir,
hooks_dir,
pmat_dir,
tdg_rules_path,
})
}
/// Get path to pre-commit hook
fn pre_commit_hook(&self) -> PathBuf {
self.hooks_dir.join("pre-commit")
}
/// Get path to post-commit hook
fn post_commit_hook(&self) -> PathBuf {
self.hooks_dir.join("post-commit")
}
/// Get path to TDG baseline
fn baseline_path(&self) -> PathBuf {
self.pmat_dir.join("baseline.json")
}
}
// =============================================================================
// TDD TESTS (RED PHASE) - Sprint 66 Phase 3
// =============================================================================
#[tokio::test]
#[ignore = "RED phase TDD - Sprint 66 Phase 3 not yet implemented"]
async fn test_tdg_hooks_install_creates_pre_commit() -> Result<()> {
// Test that --tdg-enforcement flag creates pre-commit hook
// ARRANGE
let fixture = TdgHooksFixture::new()?;
// ACT
// This will be implemented by hooks_handlers.rs
// pmat hooks install --tdg-enforcement
// let result = install_tdg_hooks(&fixture.project_root).await?;
// ASSERT
assert!(fixture.pre_commit_hook().exists());
// Hook should contain TDG check commands
let hook_content = fs::read_to_string(fixture.pre_commit_hook())?;
assert!(hook_content.contains("pmat tdg check-regression"));
assert!(hook_content.contains("pmat tdg check-quality"));
assert!(hook_content.contains("PMAT TDG Enforcement"));
Ok(())
}
#[tokio::test]
#[ignore = "RED phase TDD - Sprint 66 Phase 3 not yet implemented"]
async fn test_tdg_hooks_install_creates_post_commit() -> Result<()> {
// Test that --tdg-enforcement flag creates post-commit hook
// ARRANGE
let fixture = TdgHooksFixture::new()?;
// ACT
// pmat hooks install --tdg-enforcement
// let result = install_tdg_hooks(&fixture.project_root).await?;
// ASSERT
assert!(fixture.post_commit_hook().exists());
// Hook should contain baseline update commands
let hook_content = fs::read_to_string(fixture.post_commit_hook())?;
assert!(hook_content.contains("pmat tdg baseline update"));
assert!(hook_content.contains("PMAT TDG Baseline Auto-Update"));
Ok(())
}
#[tokio::test]
#[ignore = "RED phase TDD - Sprint 66 Phase 3 not yet implemented"]
async fn test_tdg_hooks_uses_config_from_tdg_rules() -> Result<()> {
// Test that hooks use configuration from .pmat/tdg-rules.toml
// ARRANGE
let fixture = TdgHooksFixture::new()?;
// ACT
// pmat hooks install --tdg-enforcement
// let result = install_tdg_hooks(&fixture.project_root).await?;
// ASSERT
let hook_content = fs::read_to_string(fixture.pre_commit_hook())?;
// Hook should use config values from tdg-rules.toml
assert!(hook_content.contains("--min-grade B+") || hook_content.contains("B+"));
assert!(hook_content.contains("--max-score-drop 5.0") || hook_content.contains("5.0"));
assert!(hook_content.contains("--fail-on-regression"));
Ok(())
}
#[tokio::test]
#[ignore = "RED phase TDD - Sprint 66 Phase 3 not yet implemented"]
async fn test_tdg_hooks_pre_commit_blocks_on_regression() -> Result<()> {
// Test that pre-commit hook blocks commits when regression detected
// ARRANGE
let _fixture = TdgHooksFixture::new()?;
// Install hooks
// pmat hooks install --tdg-enforcement
// Create a baseline
// pmat tdg baseline create
// Simulate regression (lower quality code)
// ... create poor quality file ...
// ACT
// Run pre-commit hook
// let result = run_hook(&fixture.pre_commit_hook()).await;
// ASSERT
// assert!(result.is_err()); // Hook should fail
// assert!(result.unwrap_err().to_string().contains("Quality regression detected"));
Ok(())
}
#[tokio::test]
#[ignore = "RED phase TDD - Sprint 66 Phase 3 not yet implemented"]
async fn test_tdg_hooks_pre_commit_allows_improvement() -> Result<()> {
// Test that pre-commit hook allows commits when quality improves
// ARRANGE
let _fixture = TdgHooksFixture::new()?;
// Install hooks
// pmat hooks install --tdg-enforcement
// Create baseline
// pmat tdg baseline create
// Improve code quality
// ... create high quality file ...
// ACT
// Run pre-commit hook
// let result = run_hook(&fixture.pre_commit_hook()).await;
// ASSERT
// assert!(result.is_ok()); // Hook should pass
Ok(())
}
#[tokio::test]
#[ignore = "RED phase TDD - Sprint 66 Phase 3 not yet implemented"]
async fn test_tdg_hooks_post_commit_updates_baseline() -> Result<()> {
// Test that post-commit hook updates baseline after successful commit
// ARRANGE
let fixture = TdgHooksFixture::new()?;
// Install hooks
// pmat hooks install --tdg-enforcement
// Create initial baseline
let _baseline_path = fixture.baseline_path();
// pmat tdg baseline create --output baseline_path
// Get initial modification time
// let initial_mtime = fs::metadata(&baseline_path)?.modified()?;
// ACT
// Simulate commit (run post-commit hook)
// run_hook(&fixture.post_commit_hook()).await?;
// ASSERT
// Baseline should be updated (newer modification time)
// let new_mtime = fs::metadata(&baseline_path)?.modified()?;
// assert!(new_mtime > initial_mtime);
Ok(())
}
#[tokio::test]
#[ignore = "RED phase TDD - Sprint 66 Phase 3 not yet implemented"]
async fn test_tdg_hooks_respects_mode_warning() -> Result<()> {
// Test that hooks respect "warning" mode (don't block commits)
// ARRANGE
let fixture = TdgHooksFixture::new()?;
// Change config to warning mode
let warning_config = r#"
[quality_gates]
mode = "warning"
rust_min_grade = "A+"
block_on_regression = false
"#;
fs::write(&fixture.tdg_rules_path, warning_config)?;
// Install hooks
// pmat hooks install --tdg-enforcement
// Create baseline
// pmat tdg baseline create
// Add poor quality code (would fail in strict mode)
// ... create D-grade file ...
// ACT
// Run pre-commit hook
// let result = run_hook(&fixture.pre_commit_hook()).await;
// ASSERT
// Hook should pass but show warning
// assert!(result.is_ok());
// let output = result.unwrap();
// assert!(output.contains("WARNING") || output.contains("⚠️"));
Ok(())
}
#[tokio::test]
#[ignore = "RED phase TDD - Sprint 66 Phase 3 not yet implemented"]
async fn test_tdg_hooks_respects_mode_disabled() -> Result<()> {
// Test that hooks respect "disabled" mode (no checks run)
// ARRANGE
let fixture = TdgHooksFixture::new()?;
// Change config to disabled mode
let disabled_config = r#"
[quality_gates]
mode = "disabled"
"#;
fs::write(&fixture.tdg_rules_path, disabled_config)?;
// Install hooks
// pmat hooks install --tdg-enforcement
// ACT
// Run pre-commit hook
// let result = run_hook(&fixture.pre_commit_hook()).await;
// ASSERT
// Hook should immediately pass without running checks
// assert!(result.is_ok());
// let output = result.unwrap();
// assert!(output.contains("TDG enforcement disabled") || output.is_empty());
Ok(())
}
#[tokio::test]
#[ignore = "RED phase TDD - Sprint 66 Phase 3 not yet implemented"]
async fn test_tdg_hooks_handles_missing_baseline_gracefully() -> Result<()> {
// Test that hooks handle missing baseline without crashing
// ARRANGE
let _fixture = TdgHooksFixture::new()?;
// Install hooks
// pmat hooks install --tdg-enforcement
// DO NOT create baseline (simulate first-time use)
// ACT
// Run pre-commit hook
// let result = run_hook(&fixture.pre_commit_hook()).await;
// ASSERT
// Hook should pass (or warn) but not crash
// assert!(result.is_ok());
// let output = result.unwrap();
// assert!(output.contains("No baseline found") || output.contains("Creating initial baseline"));
Ok(())
}
#[tokio::test]
#[ignore = "RED phase TDD - Sprint 66 Phase 3 not yet implemented"]
async fn test_tdg_hooks_language_specific_thresholds() -> Result<()> {
// Test that hooks enforce language-specific minimum grades
// ARRANGE
let fixture = TdgHooksFixture::new()?;
// Config has different thresholds per language
let config = r#"
[quality_gates]
rust_min_grade = "A-"
typescript_min_grade = "B+"
python_min_grade = "B"
mode = "strict"
"#;
fs::write(&fixture.tdg_rules_path, config)?;
// Install hooks
// pmat hooks install --tdg-enforcement
// ACT
let hook_content = fs::read_to_string(fixture.pre_commit_hook())?;
// ASSERT
// Hook should contain language-specific grade checks
// This might be in the hook logic or passed as parameters
assert!(hook_content.contains("A-") || hook_content.contains("rust_min_grade"));
assert!(hook_content.contains("B+") || hook_content.contains("typescript_min_grade"));
Ok(())
}
// =============================================================================
// PROPERTY-BASED TESTS
// =============================================================================
#[tokio::test]
#[ignore = "RED phase TDD - Sprint 66 Phase 3 not yet implemented"]
async fn test_tdg_hooks_idempotent_installation() -> Result<()> {
// Property: Installing TDG hooks multiple times should be idempotent
// ARRANGE
let fixture = TdgHooksFixture::new()?;
// ACT
// pmat hooks install --tdg-enforcement (first time)
let hook_content_1 = fs::read_to_string(fixture.pre_commit_hook())?;
// pmat hooks install --tdg-enforcement (second time)
let hook_content_2 = fs::read_to_string(fixture.pre_commit_hook())?;
// pmat hooks install --tdg-enforcement (third time)
let hook_content_3 = fs::read_to_string(fixture.pre_commit_hook())?;
// ASSERT
// All installations should produce identical hooks
assert_eq!(hook_content_1, hook_content_2);
assert_eq!(hook_content_2, hook_content_3);
Ok(())
}