code-moniker 0.1.0

Standalone CLI / linter for the code-moniker symbol graph: per-file probe, directory summary, project-wide architecture rules.
Documentation
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
//! End-to-end CLI tests. Each test writes a fixture file via `tempfile`,
//! drives `cli::run` directly with captured writers (no subprocess), and
//! asserts on stdout/stderr/exit. Black-box on the public CLI surface.

use std::io::Write;

use clap::Parser;
use code_moniker_cli::{self as cli, Cli, Exit};

const TS_FIXTURE: &str = r#"// header comment
export class Foo {
    bar(s: string): void {
        // inside method
    }
    baz(): void {}
}

class Bar extends Foo {}
"#;

fn run_with(argv: Vec<&str>) -> (Exit, String, String) {
	let cli = Cli::try_parse_from(argv).expect("parse argv");
	let mut out = Vec::new();
	let mut err = Vec::new();
	let exit = cli::run(&cli, &mut out, &mut err);
	(
		exit,
		String::from_utf8(out).unwrap(),
		String::from_utf8(err).unwrap(),
	)
}

fn write_fixture(name: &str, body: &str) -> tempfile::TempDir {
	let dir = tempfile::tempdir().unwrap();
	let p = dir.path().join(name);
	let mut f = std::fs::File::create(&p).unwrap();
	f.write_all(body.as_bytes()).unwrap();
	dir
}

#[test]
fn no_predicate_dumps_full_graph_as_tsv() {
	let dir = write_fixture("a.ts", TS_FIXTURE);
	let path = dir.path().join("a.ts");
	let (exit, out, err) = run_with(vec!["code-moniker", path.to_str().unwrap()]);
	assert_eq!(exit, Exit::Match, "stderr={err}");
	assert!(out.lines().any(|l| l.starts_with("def\t")), "{out}");
	assert!(out.contains("class:Foo"), "{out}");
	assert!(out.contains("class:Bar"), "{out}");
}

#[test]
fn count_only_prints_an_integer() {
	let dir = write_fixture("a.ts", TS_FIXTURE);
	let path = dir.path().join("a.ts");
	let (exit, out, _) = run_with(vec!["code-moniker", path.to_str().unwrap(), "--count"]);
	assert_eq!(exit, Exit::Match);
	let trimmed = out.trim();
	let n: usize = trimmed.parse().expect("expected integer, got {trimmed}");
	assert!(n > 0);
}

#[test]
fn quiet_emits_nothing_on_match() {
	let dir = write_fixture("a.ts", TS_FIXTURE);
	let path = dir.path().join("a.ts");
	let (exit, out, _) = run_with(vec![
		"code-moniker",
		path.to_str().unwrap(),
		"--kind",
		"comment",
		"--quiet",
	]);
	assert_eq!(exit, Exit::Match);
	assert!(out.is_empty(), "expected silent stdout, got {out}");
}

#[test]
fn no_match_returns_exit_one() {
	let dir = write_fixture("a.ts", TS_FIXTURE);
	let path = dir.path().join("a.ts");
	let (exit, _, _) = run_with(vec![
		"code-moniker",
		path.to_str().unwrap(),
		"--kind",
		"enum_constant",
		"--quiet",
	]);
	assert_eq!(exit, Exit::NoMatch);
}

#[test]
fn class_kind_filter_finds_class_foo() {
	let dir = write_fixture("a.ts", TS_FIXTURE);
	let path = dir.path().join("a.ts");
	let (exit, out, err) = run_with(vec![
		"code-moniker",
		path.to_str().unwrap(),
		"--kind",
		"method",
	]);
	assert_eq!(exit, Exit::Match, "stderr={err} stdout={out}");
	let lines: Vec<&str> = out.lines().collect();
	assert!(!lines.is_empty(), "no methods matched");
	for line in &lines {
		assert!(line.contains("class:Foo"), "{line}");
		assert!(line.starts_with("def\t"), "{line}");
	}
}

#[test]
fn json_format_produces_parsable_document() {
	let dir = write_fixture("a.ts", TS_FIXTURE);
	let path = dir.path().join("a.ts");
	let (exit, out, _) = run_with(vec![
		"code-moniker",
		path.to_str().unwrap(),
		"--format",
		"json",
	]);
	assert_eq!(exit, Exit::Match);
	let v: serde_json::Value = serde_json::from_str(&out).expect("valid JSON");
	assert_eq!(v["lang"].as_str(), Some("ts"));
	assert!(v["matches"]["defs"].as_array().unwrap().len() > 1);
}

#[test]
fn comment_kind_filter_finds_comments() {
	let dir = write_fixture("a.ts", TS_FIXTURE);
	let path = dir.path().join("a.ts");
	let (exit, out, _) = run_with(vec![
		"code-moniker",
		path.to_str().unwrap(),
		"--kind",
		"comment",
		"--count",
	]);
	assert_eq!(exit, Exit::Match);
	let n: usize = out.trim().parse().unwrap();
	assert_eq!(n, 2, "expected two comments, got {n}");
}

#[test]
fn with_text_attaches_comment_source() {
	let dir = write_fixture("a.ts", TS_FIXTURE);
	let path = dir.path().join("a.ts");
	let (exit, out, _) = run_with(vec![
		"code-moniker",
		path.to_str().unwrap(),
		"--kind",
		"comment",
		"--with-text",
		"--format",
		"json",
	]);
	assert_eq!(exit, Exit::Match);
	let v: serde_json::Value = serde_json::from_str(&out).unwrap();
	let texts: Vec<&str> = v["matches"]["defs"]
		.as_array()
		.unwrap()
		.iter()
		.filter_map(|d| d["text"].as_str())
		.collect();
	assert!(
		texts.iter().any(|t| t.contains("header comment")),
		"missing top-level comment text: {texts:?}"
	);
	assert!(
		texts.iter().any(|t| t.contains("inside method")),
		"missing nested comment text: {texts:?}"
	);
}

#[test]
fn unknown_extension_is_usage_error() {
	let dir = write_fixture("a.txt", "hello");
	let path = dir.path().join("a.txt");
	let (exit, _, err) = run_with(vec!["code-moniker", path.to_str().unwrap()]);
	assert_eq!(exit, Exit::UsageError);
	assert!(err.contains("unsupported"), "{err}");
}

#[test]
fn malformed_predicate_uri_is_usage_error() {
	let dir = write_fixture("a.ts", TS_FIXTURE);
	let path = dir.path().join("a.ts");
	let (exit, _, err) = run_with(vec![
		"code-moniker",
		path.to_str().unwrap(),
		"--where",
		"= not a uri",
	]);
	assert_eq!(exit, Exit::UsageError);
	assert!(err.contains("--where"), "{err}");
}

const TS_BAD_NAMING: &str = "class lower_case_class {}\n";

#[test]
fn check_clean_file_returns_match() {
	let dir = write_fixture("a.ts", "class GoodName {}\n");
	let path = dir.path().join("a.ts");
	let (exit, out, err) = run_with(vec![
		"code-moniker",
		"check",
		path.to_str().unwrap(),
		"--rules",
		"/no/such/file.toml",
	]);
	assert_eq!(exit, Exit::Match, "stdout={out} stderr={err}");
	assert!(out.is_empty(), "no violations expected: {out}");
}

#[test]
fn check_violation_reports_rule_id_and_lines() {
	let dir = write_fixture("a.ts", TS_BAD_NAMING);
	let path = dir.path().join("a.ts");
	let (exit, out, _) = run_with(vec![
		"code-moniker",
		"check",
		path.to_str().unwrap(),
		"--rules",
		"/no/such/file.toml",
	]);
	assert_eq!(exit, Exit::NoMatch);
	assert!(out.contains("ts.class.name-pascalcase"), "{out}");
	assert!(out.contains("L1-L1"), "{out}");
}

#[test]
fn check_json_format_is_structured() {
	let dir = write_fixture("a.ts", TS_BAD_NAMING);
	let path = dir.path().join("a.ts");
	let (exit, out, _) = run_with(vec![
		"code-moniker",
		"check",
		path.to_str().unwrap(),
		"--rules",
		"/no/such/file.toml",
		"--format",
		"json",
	]);
	assert_eq!(exit, Exit::NoMatch);
	let v: serde_json::Value = serde_json::from_str(&out).expect("json output");
	assert_eq!(v["summary"]["files_scanned"], 1);
	assert_eq!(v["summary"]["files_with_violations"], 1);
	let files = v["files"].as_array().unwrap();
	assert_eq!(files.len(), 1);
	assert!(files[0]["file"].as_str().unwrap().ends_with("a.ts"));
	let viols = files[0]["violations"].as_array().unwrap();
	assert_eq!(viols[0]["rule_id"], "ts.class.name-pascalcase");
}

#[test]
fn check_project_walks_directory_and_aggregates() {
	let dir = tempfile::tempdir().expect("tmpdir");
	std::fs::write(dir.path().join("good.ts"), "class GoodName {}\n").unwrap();
	std::fs::write(dir.path().join("bad.ts"), TS_BAD_NAMING).unwrap();
	std::fs::write(dir.path().join("README.md"), "not a source file\n").unwrap();
	let (exit, out, _) = run_with(vec![
		"code-moniker",
		"check",
		dir.path().to_str().unwrap(),
		"--rules",
		"/no/such/file.toml",
	]);
	assert_eq!(exit, Exit::NoMatch, "stdout={out}");
	assert!(out.contains("bad.ts"), "{out}");
	assert!(!out.contains("good.ts"), "{out}");
	assert!(out.contains("ts.class.name-pascalcase"), "{out}");
	assert!(out.contains("violation(s) across 1 file(s)"), "{out}");
}

#[test]
fn check_project_respects_gitignore() {
	let dir = tempfile::tempdir().expect("tmpdir");
	std::fs::write(dir.path().join(".gitignore"), "ignored.ts\n").unwrap();
	std::fs::write(dir.path().join("scanned.ts"), TS_BAD_NAMING).unwrap();
	std::fs::write(dir.path().join("ignored.ts"), TS_BAD_NAMING).unwrap();
	// `ignore` only honors .gitignore inside a git repo, so init one.
	std::fs::create_dir(dir.path().join(".git")).unwrap();
	let (exit, out, _) = run_with(vec![
		"code-moniker",
		"check",
		dir.path().to_str().unwrap(),
		"--rules",
		"/no/such/file.toml",
	]);
	assert_eq!(exit, Exit::NoMatch);
	assert!(out.contains("scanned.ts"), "{out}");
	assert!(!out.contains("ignored.ts"), "{out}");
}

#[test]
fn check_project_continues_on_per_file_errors() {
	let dir = tempfile::tempdir().expect("tmpdir");
	std::fs::write(dir.path().join("bad.ts"), TS_BAD_NAMING).unwrap();
	// Non-UTF-8 bytes: `std::fs::read_to_string` rejects this.
	std::fs::write(dir.path().join("broken.ts"), [0xff, 0xfe, 0xff, 0xfe]).unwrap();
	std::fs::write(dir.path().join("good.ts"), "class GoodName {}\n").unwrap();
	let (exit, out, err) = run_with(vec![
		"code-moniker",
		"check",
		dir.path().to_str().unwrap(),
		"--rules",
		"/no/such/file.toml",
	]);
	// Exit 1: violations + errors both signal an unclean run; exit 2 stays
	// reserved for global usage errors.
	assert_eq!(exit, Exit::NoMatch, "out={out} err={err}");
	assert!(
		out.contains("bad.ts"),
		"bad.ts violation still emitted: {out}"
	);
	assert!(out.contains("ts.class.name-pascalcase"), "{out}");
	assert!(
		err.contains("broken.ts"),
		"broken.ts error on stderr: {err}"
	);
	assert!(
		out.contains("1 file(s) errored"),
		"footer mentions errors: {out}"
	);
}

#[test]
fn check_project_json_includes_errors_array() {
	let dir = tempfile::tempdir().expect("tmpdir");
	std::fs::write(dir.path().join("broken.ts"), [0xff, 0xfe, 0xff]).unwrap();
	std::fs::write(dir.path().join("good.ts"), "class GoodName {}\n").unwrap();
	let (exit, out, _) = run_with(vec![
		"code-moniker",
		"check",
		dir.path().to_str().unwrap(),
		"--rules",
		"/no/such/file.toml",
		"--format",
		"json",
	]);
	assert_eq!(exit, Exit::NoMatch);
	let v: serde_json::Value = serde_json::from_str(&out).expect("json output");
	assert_eq!(v["summary"]["files_with_errors"], 1);
	let errors = v["errors"].as_array().unwrap();
	assert_eq!(errors.len(), 1);
	assert!(errors[0]["file"].as_str().unwrap().ends_with("broken.ts"));
}

#[test]
fn check_project_path_in_moniker_gates_a_rule() {
	let dir = tempfile::tempdir().expect("tmpdir");
	std::fs::create_dir(dir.path().join("strict")).unwrap();
	std::fs::create_dir(dir.path().join("lax")).unwrap();
	std::fs::write(dir.path().join("strict/a.ts"), "class lower_case {}\n").unwrap();
	std::fs::write(dir.path().join("lax/b.ts"), "class lower_case {}\n").unwrap();
	let rules_path = dir.path().join("rules.toml");
	// Replace the preset's name-pascalcase rule with one that only fires
	// inside the `strict` directory — keyed off the path-derived moniker
	// (`**/dir:strict/**` segment).
	std::fs::write(
		&rules_path,
		r#"
		[[ts.class.where]]
		id      = "name-pascalcase"
		expr    = "moniker ~ '**/dir:strict/**' => name =~ ^[A-Z][A-Za-z0-9]*$"
		message = "Strict layer requires PascalCase."
		"#,
	)
	.unwrap();
	let (exit, out, _) = run_with(vec![
		"code-moniker",
		"check",
		dir.path().to_str().unwrap(),
		"--rules",
		rules_path.to_str().unwrap(),
	]);
	assert_eq!(exit, Exit::NoMatch, "strict/a.ts should violate: {out}");
	assert!(out.contains("strict/a.ts"), "{out}");
	assert!(
		!out.contains("lax/b.ts"),
		"lax/ exempt by path-in-moniker gate: {out}"
	);
}

#[test]
fn check_project_cross_layer_import_violation() {
	let dir = tempfile::tempdir().expect("tmpdir");
	std::fs::create_dir_all(dir.path().join("src/core")).unwrap();
	std::fs::write(
		dir.path().join("src/core/bad.rs"),
		"use pgrx::prelude::*;\npub fn foo() {}\n",
	)
	.unwrap();
	let rules_path = dir.path().join("rules.toml");
	std::fs::write(
		&rules_path,
		r#"
		[aliases]
		core = "moniker ~ '**/dir:src/dir:core/**'"

		[[refs.where]]
		id   = "core-no-pgrx"
		expr = "$core AND kind = 'imports_symbol' => NOT target ~ '**/external_pkg:pgrx/**'"
		"#,
	)
	.unwrap();
	let (exit, out, _) = run_with(vec![
		"code-moniker",
		"check",
		dir.path().to_str().unwrap(),
		"--rules",
		rules_path.to_str().unwrap(),
	]);
	assert_eq!(exit, Exit::NoMatch, "core/bad.rs imports pgrx: {out}");
	assert!(out.contains("core-no-pgrx"), "{out}");
	assert!(out.contains("bad.rs"), "{out}");
}

#[test]
fn check_project_clean_returns_match() {
	let dir = tempfile::tempdir().expect("tmpdir");
	std::fs::write(dir.path().join("a.ts"), "class GoodName {}\n").unwrap();
	std::fs::write(dir.path().join("b.ts"), "class AlsoGood {}\n").unwrap();
	let (exit, out, _) = run_with(vec![
		"code-moniker",
		"check",
		dir.path().to_str().unwrap(),
		"--rules",
		"/no/such/file.toml",
	]);
	assert_eq!(exit, Exit::Match);
	assert!(out.contains("0 violation(s)"), "{out}");
}

#[test]
fn check_project_json_has_summary_and_files() {
	let dir = tempfile::tempdir().expect("tmpdir");
	std::fs::write(dir.path().join("bad.ts"), TS_BAD_NAMING).unwrap();
	std::fs::write(dir.path().join("good.ts"), "class GoodName {}\n").unwrap();
	let (exit, out, _) = run_with(vec![
		"code-moniker",
		"check",
		dir.path().to_str().unwrap(),
		"--rules",
		"/no/such/file.toml",
		"--format",
		"json",
	]);
	assert_eq!(exit, Exit::NoMatch);
	let v: serde_json::Value = serde_json::from_str(&out).expect("json output");
	assert_eq!(v["summary"]["files_scanned"], 2);
	assert_eq!(v["summary"]["files_with_violations"], 1);
	assert_eq!(v["summary"]["total_violations"], 1);
}

#[test]
fn check_require_doc_comment_flags_undocumented_public_class() {
	let dir = write_fixture("a.ts", "export class Foo {}\n");
	let rules_path = dir.path().join("rules.toml");
	std::fs::write(
		&rules_path,
		r#"
		[ts.class]
		require_doc_comment = "public"
		"#,
	)
	.unwrap();
	let path = dir.path().join("a.ts");
	let (exit, out, _) = run_with(vec![
		"code-moniker",
		"check",
		path.to_str().unwrap(),
		"--rules",
		rules_path.to_str().unwrap(),
	]);
	assert_eq!(exit, Exit::NoMatch);
	assert!(out.contains("ts.class.require_doc_comment"), "{out}");
}

#[test]
fn check_require_doc_comment_passes_when_docblock_precedes() {
	let dir = write_fixture("a.ts", "/** doc */\nexport class Foo {}\n");
	let rules_path = dir.path().join("rules.toml");
	std::fs::write(
		&rules_path,
		r#"
		[ts.class]
		require_doc_comment = "public"
		"#,
	)
	.unwrap();
	let path = dir.path().join("a.ts");
	let (exit, out, _) = run_with(vec![
		"code-moniker",
		"check",
		path.to_str().unwrap(),
		"--rules",
		rules_path.to_str().unwrap(),
	]);
	assert_eq!(exit, Exit::Match, "stdout={out}");
}

#[test]
fn check_default_preset_flags_helper_function_name() {
	let dir = write_fixture("a.ts", "function helper() {}\n");
	let path = dir.path().join("a.ts");
	let (exit, out, _) = run_with(vec![
		"code-moniker",
		"check",
		path.to_str().unwrap(),
		"--rules",
		"/no/such/file.toml",
	]);
	assert_eq!(exit, Exit::NoMatch);
	assert!(
		out.contains("ts.function.no-placeholder-names"),
		"expected forbid_name_patterns violation: {out}"
	);
}

#[test]
fn check_user_overlay_keeps_default_forbid_when_changing_max_lines() {
	let dir = write_fixture("a.ts", "function helper() {}\n");
	let rules_path = dir.path().join("rules.toml");
	// Override the preset's max-lines rule by reusing its id; the preset's
	// no-placeholder-names rule must survive the override.
	std::fs::write(
		&rules_path,
		r#"
		[[ts.function.where]]
		id   = "max-lines"
		expr = "lines <= 999"
		"#,
	)
	.unwrap();
	let path = dir.path().join("a.ts");
	let (exit, out, _) = run_with(vec![
		"code-moniker",
		"check",
		path.to_str().unwrap(),
		"--rules",
		rules_path.to_str().unwrap(),
	]);
	assert_eq!(exit, Exit::NoMatch);
	assert!(
		out.contains("ts.function.no-placeholder-names"),
		"merge regression — preset forbid_name_patterns lost when max_lines override applied: {out}"
	);
}

#[test]
fn check_unknown_kind_in_user_config_is_a_usage_error() {
	let dir = write_fixture("a.ts", "class GoodName {}\n");
	let rules_path = dir.path().join("rules.toml");
	std::fs::write(
		&rules_path,
		r#"
		[[ts.classs.where]]
		expr = "name =~ ^X"
		"#,
	)
	.unwrap();
	let path = dir.path().join("a.ts");
	let (exit, _, err) = run_with(vec![
		"code-moniker",
		"check",
		path.to_str().unwrap(),
		"--rules",
		rules_path.to_str().unwrap(),
	]);
	assert_eq!(exit, Exit::UsageError);
	assert!(err.contains("classs"), "{err}");
}

#[test]
fn check_invalid_regex_in_user_config_is_a_usage_error() {
	let dir = write_fixture("a.ts", "class GoodName {}\n");
	let rules_path = dir.path().join("rules.toml");
	std::fs::write(
		&rules_path,
		r#"
		[[ts.class.where]]
		expr = "name =~ [unclosed"
		"#,
	)
	.unwrap();
	let path = dir.path().join("a.ts");
	let (exit, _, err) = run_with(vec![
		"code-moniker",
		"check",
		path.to_str().unwrap(),
		"--rules",
		rules_path.to_str().unwrap(),
	]);
	assert_eq!(exit, Exit::UsageError);
	assert!(
		err.to_lowercase().contains("regex") || err.to_lowercase().contains("invalid"),
		"{err}"
	);
}

#[test]
fn check_explanation_appears_in_text_and_json() {
	let dir = write_fixture("a.ts", TS_BAD_NAMING);
	let rules_path = dir.path().join("rules.toml");
	std::fs::write(
		&rules_path,
		r#"
		[[ts.class.where]]
		id      = "name-pascalcase"
		expr    = "name =~ ^[A-Z][A-Za-z0-9]*$"
		message = "Rename `{name}`. See CLAUDE.md §naming."
		"#,
	)
	.unwrap();
	let path = dir.path().join("a.ts");

	let (exit, out, _) = run_with(vec![
		"code-moniker",
		"check",
		path.to_str().unwrap(),
		"--rules",
		rules_path.to_str().unwrap(),
	]);
	assert_eq!(exit, Exit::NoMatch);
	assert!(
		out.contains("  → Rename `lower_case_class`. See CLAUDE.md §naming."),
		"text format missing indented explanation: {out}"
	);

	let (_, out_json, _) = run_with(vec![
		"code-moniker",
		"check",
		path.to_str().unwrap(),
		"--rules",
		rules_path.to_str().unwrap(),
		"--format",
		"json",
	]);
	let v: serde_json::Value = serde_json::from_str(&out_json).expect("valid JSON");
	let arr = v["files"][0]["violations"].as_array().unwrap();
	let exp = arr[0]["explanation"]
		.as_str()
		.expect("json carries explanation");
	assert!(exp.contains("CLAUDE.md"), "explanation in JSON: {exp}");
}

#[test]
fn check_user_overlay_relaxes_default_rule() {
	let dir = write_fixture("a.ts", TS_BAD_NAMING);
	let rules_path = dir.path().join("rules.toml");
	std::fs::write(
		&rules_path,
		r#"
		[[ts.class.where]]
		id   = "name-pascalcase"
		expr = "name =~ ^[a-zA-Z_][a-zA-Z0-9_]*$"
		"#,
	)
	.unwrap();
	let path = dir.path().join("a.ts");
	let (exit, out, _) = run_with(vec![
		"code-moniker",
		"check",
		path.to_str().unwrap(),
		"--rules",
		rules_path.to_str().unwrap(),
	]);
	assert_eq!(exit, Exit::Match, "user override permits the name: {out}");
}