feat(checker): cph-model — structured export-target build config (WU-B, ADR-0009)

manifest.toml [targets.*] upgraded from bare names to structured build configs.
Lesson.targets: Vec<String> → Vec<TargetConfig> { name, artifact, numbering }.
ArtifactKind { SingleFile, FileTree } mirrors Spec.Courseware.Artifact.
NumberingConfig.heading: Option<Vec<String>> — None means "use framework
default" (render package owns it), so absence is distinguishable from a value.
Empty [targets.x] body = all defaults, no diagnostics. Malformed config (bad
artifact value, non-array numbering.heading) → non-fatal SchemaViolation, target
kept with defaults. Enabled toml `preserve_order` so target declaration order is
real (the old Vec<String> never actually preserved order). target_names() helper
for call sites that only want names. 8 tests green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-22 08:42:15 +08:00
parent 09b026f598
commit b5bf9d60d1
6 changed files with 372 additions and 11 deletions
@@ -0,0 +1,16 @@
[project]
id = "fixture-bad-target-config"
name = "bad-target-config"
[info]
title = "测试课:非法导出目标配置"
# Invalid artifact value → SchemaViolation, falls back to default (single-file).
[targets.student]
artifact = "pdf-thing"
# numbering.heading is not an array of strings → SchemaViolation, heading None.
[targets.teacher]
artifact = "file-tree"
[targets.teacher.numbering]
heading = "not-an-array"
@@ -0,0 +1,19 @@
[project]
id = "fixture-target-configs"
name = "target-configs"
[info]
title = "测试课:导出目标的结构化 build 配置"
# Full config: explicit artifact + numbering.heading override.
[targets.student]
artifact = "single-file"
[targets.student.numbering]
heading = ["{1:一}、", "{1:1}.{2:1}", "{1:1}.{2:1}.{3:1}"]
# file-tree artifact, no presentation override.
[targets.archive]
artifact = "file-tree"
# Empty body → all defaults.
[targets.teacher]
+98 -4
View File
@@ -46,10 +46,13 @@ fn valid_two_part_lesson_loads_in_order_with_no_errors() {
);
assert!(scalars.get("kind").is_none());
// Targets are collected from [targets.*].
let mut targets = lesson.targets.clone();
targets.sort();
assert_eq!(targets, vec!["student".to_string(), "teacher".to_string()]);
// Targets are collected from [targets.*], in declared order.
assert_eq!(lesson.target_names(), vec!["student", "teacher"]);
// Empty `[targets.x]` bodies → all-default build config, no diagnostics.
for target in &lesson.targets {
assert_eq!(target.artifact, cph_model::ArtifactKind::SingleFile);
assert_eq!(target.numbering, None);
}
// Descriptor dir is absolute, anchored under the root.
assert!(lesson.parts[0].descriptor.dir.is_absolute());
@@ -123,3 +126,94 @@ fn missing_manifest_is_a_hard_failure() {
assert_eq!(diags.len(), 1);
assert_eq!(diags[0].code, DiagCode::SchemaViolation);
}
#[test]
fn structured_target_configs_parse_into_typed_builds() {
use cph_model::ArtifactKind;
let (lesson, diags) = load(&fixture("target-configs"));
let lesson = lesson.expect("target-configs fixture must produce a Lesson");
assert!(
diags.is_empty(),
"well-formed target configs must have no diagnostics, got: {diags:?}"
);
// Declared order is preserved.
assert_eq!(lesson.target_names(), vec!["student", "archive", "teacher"]);
// student: explicit single-file + heading numbering override, in order.
let student = &lesson.targets[0];
assert_eq!(student.name, "student");
assert_eq!(student.artifact, ArtifactKind::SingleFile);
let numbering = student
.numbering
.as_ref()
.expect("student declares a numbering override");
assert_eq!(
numbering.heading.as_deref(),
Some(
&[
"{1:一}、".to_string(),
"{1:1}.{2:1}".to_string(),
"{1:1}.{2:1}.{3:1}".to_string(),
][..]
)
);
// archive: file-tree artifact, no presentation override.
let archive = &lesson.targets[1];
assert_eq!(archive.name, "archive");
assert_eq!(archive.artifact, ArtifactKind::FileTree);
assert_eq!(archive.numbering, None);
// teacher: empty body → all defaults, numbering absent.
let teacher = &lesson.targets[2];
assert_eq!(teacher.name, "teacher");
assert_eq!(teacher.artifact, ArtifactKind::SingleFile);
assert_eq!(teacher.numbering, None);
}
#[test]
fn malformed_target_config_is_non_fatal_with_schema_violations() {
use cph_model::ArtifactKind;
let (lesson, diags) = load(&fixture("bad-target-config"));
let lesson = lesson.expect("bad-target-config must still produce a Lesson");
// Both targets survive despite their malformed fields.
assert_eq!(lesson.target_names(), vec!["student", "teacher"]);
// Bad `artifact` value → SchemaViolation, target kept with default artifact.
let student = &lesson.targets[0];
assert_eq!(student.artifact, ArtifactKind::SingleFile);
// Bad `numbering.heading` (not an array of strings) → heading stays None,
// but the numbering table itself is present.
let teacher = &lesson.targets[1];
assert_eq!(teacher.artifact, ArtifactKind::FileTree);
let teacher_numbering = teacher
.numbering
.as_ref()
.expect("teacher declares a numbering table");
assert_eq!(teacher_numbering.heading, None);
let violations: Vec<_> = diags
.iter()
.filter(|d| d.code == DiagCode::SchemaViolation)
.collect();
assert_eq!(
violations.len(),
2,
"expected one SchemaViolation per malformed field, got: {diags:?}"
);
assert!(
violations.iter().any(|d| d.message.contains("pdf-thing")),
"a diagnostic should name the bad artifact value, got: {diags:?}"
);
assert!(
violations
.iter()
.any(|d| d.message.contains("numbering.heading")),
"a diagnostic should name the malformed numbering.heading, got: {diags:?}"
);
}