forked from EduCraft/curriculum-project-hub
546 lines
17 KiB
Rust
546 lines
17 KiB
Rust
//! Integration tests for `cph_model::load`, driven by static fixtures under
|
|
//! `tests/fixtures/`. The fixtures double as documentation of the ADR-0029
|
|
//! on-disk format (a nested outline manifest; supersedes ADR-0008's flat
|
|
//! `[[parts]]`).
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use cph_diag::DiagCode;
|
|
use cph_model::{load, OutlineEntry};
|
|
|
|
/// Absolute path to a fixture engineering-file root.
|
|
fn fixture(name: &str) -> PathBuf {
|
|
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
.join("tests/fixtures")
|
|
.join(name)
|
|
}
|
|
|
|
#[test]
|
|
fn valid_two_part_lesson_loads_in_order_with_no_errors() {
|
|
let (lesson, diags) = load(&fixture("valid"));
|
|
|
|
let lesson = lesson.expect("valid fixture must produce a Lesson");
|
|
assert!(
|
|
diags.is_empty(),
|
|
"valid fixture must have no diagnostics, got: {diags:?}"
|
|
);
|
|
|
|
assert_eq!(lesson.project.id, "fixture-valid");
|
|
assert_eq!(lesson.project.name, "valid-2-part");
|
|
assert_eq!(lesson.info.title, "测试课:两个部件");
|
|
// A single-string `author` loads as a one-element list.
|
|
assert_eq!(lesson.info.authors, vec!["范式教育教研组".to_string()]);
|
|
|
|
// Parts preserve declared order: segment first, lemma second.
|
|
assert_eq!(lesson.parts.len(), 2);
|
|
assert_eq!(lesson.parts[0].kind, "segment");
|
|
assert_eq!(lesson.parts[0].path, PathBuf::from("segments/intro"));
|
|
assert_eq!(
|
|
lesson.parts[0].notes.as_deref(),
|
|
Some("这一节补充一个直观例题")
|
|
);
|
|
let outline = lesson.outline_document();
|
|
assert_eq!(outline.children[0].title, "intro");
|
|
assert_eq!(
|
|
outline.children[0].notes.as_deref(),
|
|
Some("这一节补充一个直观例题")
|
|
);
|
|
assert!(outline.children[0].children.is_empty());
|
|
assert_eq!(lesson.parts[0].descriptor.kind, "segment");
|
|
assert_eq!(lesson.parts[1].kind, "lemma");
|
|
assert_eq!(lesson.parts[1].path, PathBuf::from("lemmas/young"));
|
|
assert_eq!(lesson.parts[1].descriptor.kind, "lemma");
|
|
|
|
// The outline is a flat sequence of elements-by-index when there are no
|
|
// containers.
|
|
assert_eq!(
|
|
lesson.outline,
|
|
vec![
|
|
OutlineEntry::Element {
|
|
part_index: 0,
|
|
depth: 0,
|
|
},
|
|
OutlineEntry::Element {
|
|
part_index: 1,
|
|
depth: 0,
|
|
},
|
|
]
|
|
);
|
|
|
|
// `source` scalar survives on the lemma descriptor; `kind` is removed.
|
|
let scalars = &lesson.parts[1].descriptor.scalars;
|
|
assert_eq!(
|
|
scalars.get("source").and_then(|v| v.as_str()),
|
|
Some("测试引理")
|
|
);
|
|
assert!(scalars.get("kind").is_none());
|
|
|
|
// 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:
|
|
// single-file artifact at build/<name>.pdf + one typst-compile step.
|
|
for target in &lesson.targets {
|
|
assert_eq!(
|
|
target.artifact,
|
|
cph_model::Artifact::SingleFile {
|
|
filepath: PathBuf::from(format!("build/{}.pdf", target.name)),
|
|
}
|
|
);
|
|
assert_eq!(
|
|
target.steps,
|
|
vec![cph_model::Step::TypstCompile {
|
|
template: PathBuf::from(format!("exports/{}.typ", target.name)),
|
|
}]
|
|
);
|
|
}
|
|
|
|
// Descriptor dir is absolute, anchored under the root.
|
|
assert!(lesson.parts[0].descriptor.dir.is_absolute());
|
|
assert_eq!(
|
|
lesson.parts[0].descriptor.dir,
|
|
lesson.root.join("segments/intro")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn nested_sections_flatten_depth_first_with_correct_depths() {
|
|
let (lesson, diags) = load(&fixture("nested"));
|
|
let lesson = lesson.expect("nested fixture must produce a Lesson");
|
|
assert!(
|
|
diags.is_empty(),
|
|
"nested fixture must have no diagnostics, got: {diags:?}"
|
|
);
|
|
|
|
// DFS pre-order element sequence (ADR-0029): containers contribute no
|
|
// element of their own.
|
|
let paths: Vec<_> = lesson.parts.iter().map(|p| p.path.clone()).collect();
|
|
assert_eq!(
|
|
paths,
|
|
vec![
|
|
PathBuf::from("segments/开场白"),
|
|
PathBuf::from("导言簇/segments/子段一"),
|
|
PathBuf::from("导言簇/嵌套子节/lemmas/子引理"),
|
|
PathBuf::from("导言簇/segments/子段二"),
|
|
],
|
|
"root-relative paths must accumulate through every nesting level"
|
|
);
|
|
|
|
// The outline interleaves section headings at their DFS-open position,
|
|
// with depth 1 for a section directly under the root and depth 2 for one
|
|
// nested inside another section.
|
|
assert_eq!(
|
|
lesson.outline,
|
|
vec![
|
|
OutlineEntry::Element {
|
|
part_index: 0,
|
|
depth: 0,
|
|
}, // segments/开场白
|
|
OutlineEntry::Section {
|
|
kind: "section".to_string(),
|
|
title: "导言簇".to_string(),
|
|
depth: 1,
|
|
notes: None,
|
|
path: PathBuf::from("导言簇"),
|
|
},
|
|
OutlineEntry::Element {
|
|
part_index: 1,
|
|
depth: 1,
|
|
}, // 导言簇/segments/子段一
|
|
OutlineEntry::Section {
|
|
kind: "section".to_string(),
|
|
title: "嵌套子节".to_string(),
|
|
depth: 2,
|
|
notes: None,
|
|
path: PathBuf::from("导言簇/嵌套子节"),
|
|
},
|
|
OutlineEntry::Element {
|
|
part_index: 2,
|
|
depth: 2,
|
|
}, // 导言簇/嵌套子节/lemmas/子引理
|
|
OutlineEntry::Element {
|
|
part_index: 3,
|
|
depth: 1,
|
|
}, // 导言簇/segments/子段二
|
|
]
|
|
);
|
|
|
|
// The outer section declares [group].title = "导言簇"; the inner section
|
|
// has no [group] at all, so its title falls back to the folder basename.
|
|
}
|
|
|
|
#[test]
|
|
fn missing_part_folder_yields_part_path_missing() {
|
|
let (lesson, diags) = load(&fixture("missing-part"));
|
|
|
|
// Still produces a Lesson (loader stays best-effort).
|
|
let lesson = lesson.expect("missing-part fixture must still produce a Lesson");
|
|
assert_eq!(lesson.parts.len(), 2, "both parts are recorded for order");
|
|
|
|
let missing: Vec<_> = diags
|
|
.iter()
|
|
.filter(|d| d.code == DiagCode::PartPathMissing)
|
|
.collect();
|
|
assert_eq!(
|
|
missing.len(),
|
|
1,
|
|
"exactly one PartPathMissing expected, got diags: {diags:?}"
|
|
);
|
|
assert_eq!(missing[0].severity, cph_diag::Severity::Error);
|
|
}
|
|
|
|
#[test]
|
|
fn kind_mismatch_yields_unknown_kind_diagnostic() {
|
|
let (lesson, diags) = load(&fixture("kind-mismatch"));
|
|
|
|
let lesson = lesson.expect("kind-mismatch fixture must still produce a Lesson");
|
|
assert_eq!(lesson.parts.len(), 1);
|
|
|
|
let mismatch: Vec<_> = diags
|
|
.iter()
|
|
.filter(|d| d.code == DiagCode::UnknownKind)
|
|
.collect();
|
|
assert_eq!(
|
|
mismatch.len(),
|
|
1,
|
|
"exactly one kind-mismatch diagnostic expected, got: {diags:?}"
|
|
);
|
|
assert!(
|
|
mismatch[0].message.contains("segment") && mismatch[0].message.contains("lemma"),
|
|
"message should name both kinds, got: {}",
|
|
mismatch[0].message
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn malformed_manifest_is_a_hard_failure() {
|
|
let (lesson, diags) = load(&fixture("malformed-manifest"));
|
|
|
|
assert!(
|
|
lesson.is_none(),
|
|
"malformed manifest must be a hard failure (None)"
|
|
);
|
|
assert_eq!(diags.len(), 1, "one hard-failure diagnostic expected");
|
|
assert_eq!(diags[0].code, DiagCode::ManifestMalformed);
|
|
assert_eq!(diags[0].severity, cph_diag::Severity::Error);
|
|
}
|
|
|
|
#[test]
|
|
fn missing_manifest_is_a_hard_failure() {
|
|
// A directory with no manifest.toml at all.
|
|
let (lesson, diags) = load(&fixture("does-not-exist-at-all"));
|
|
assert!(lesson.is_none());
|
|
assert_eq!(diags.len(), 1);
|
|
assert_eq!(diags[0].code, DiagCode::ManifestMalformed);
|
|
}
|
|
|
|
#[test]
|
|
fn folder_with_both_manifest_and_element_is_manifest_malformed() {
|
|
let (lesson, diags) = load(&fixture("both-manifest-and-element"));
|
|
let lesson = lesson.expect("must still produce a best-effort Lesson");
|
|
assert_eq!(
|
|
lesson.parts.len(),
|
|
1,
|
|
"the ambiguous child is a placeholder"
|
|
);
|
|
|
|
let malformed: Vec<_> = diags
|
|
.iter()
|
|
.filter(|d| d.code == DiagCode::ManifestMalformed)
|
|
.collect();
|
|
assert_eq!(
|
|
malformed.len(),
|
|
1,
|
|
"exactly one ManifestMalformed expected, got: {diags:?}"
|
|
);
|
|
assert!(
|
|
malformed[0]
|
|
.message
|
|
.contains("both manifest.toml and element.toml"),
|
|
"message should explain the ambiguity, got: {}",
|
|
malformed[0].message
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn folder_with_neither_manifest_nor_element_is_manifest_malformed() {
|
|
let (lesson, diags) = load(&fixture("neither-manifest-nor-element"));
|
|
let lesson = lesson.expect("must still produce a best-effort Lesson");
|
|
assert_eq!(
|
|
lesson.parts.len(),
|
|
1,
|
|
"the incomplete child is a placeholder"
|
|
);
|
|
|
|
let malformed: Vec<_> = diags
|
|
.iter()
|
|
.filter(|d| d.code == DiagCode::ManifestMalformed)
|
|
.collect();
|
|
assert_eq!(
|
|
malformed.len(),
|
|
1,
|
|
"exactly one ManifestMalformed expected, got: {diags:?}"
|
|
);
|
|
assert!(
|
|
malformed[0]
|
|
.message
|
|
.contains("neither manifest.toml nor element.toml"),
|
|
"message should explain the gap, got: {}",
|
|
malformed[0].message
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn container_declaring_root_only_tables_is_manifest_malformed() {
|
|
let (lesson, diags) = load(&fixture("container-root-tables"));
|
|
assert!(
|
|
lesson.is_some(),
|
|
"a container misplacing root tables is non-fatal"
|
|
);
|
|
|
|
let malformed: Vec<_> = diags
|
|
.iter()
|
|
.filter(|d| d.code == DiagCode::ManifestMalformed && d.message.contains("root-only"))
|
|
.collect();
|
|
assert_eq!(
|
|
malformed.len(),
|
|
1,
|
|
"exactly one root-only-table diagnostic expected, got: {diags:?}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn root_manifest_declaring_group_is_manifest_malformed() {
|
|
let (lesson, diags) = load(&fixture("root-group-declared"));
|
|
assert!(lesson.is_some(), "the root declaring [group] is non-fatal");
|
|
|
|
let malformed: Vec<_> = diags
|
|
.iter()
|
|
.filter(|d| d.code == DiagCode::ManifestMalformed && d.message.contains("[group]"))
|
|
.collect();
|
|
assert_eq!(
|
|
malformed.len(),
|
|
1,
|
|
"exactly one root-[group] diagnostic expected, got: {diags:?}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn structured_target_configs_parse_into_typed_builds() {
|
|
use cph_model::{Artifact, Step};
|
|
|
|
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:?}"
|
|
);
|
|
|
|
// An array `author` loads as the ordered author list.
|
|
assert_eq!(
|
|
lesson.info.authors,
|
|
vec!["张老师".to_string(), "李老师".to_string()]
|
|
);
|
|
|
|
// Declared order is preserved.
|
|
assert_eq!(lesson.target_names(), vec!["student", "archive", "teacher"]);
|
|
|
|
// student: explicit single-file artifact (with filepath) + one
|
|
// typst-compile step (with template).
|
|
let student = &lesson.targets[0];
|
|
assert_eq!(student.name, "student");
|
|
assert_eq!(
|
|
student.artifact,
|
|
Artifact::SingleFile {
|
|
filepath: PathBuf::from("build/student.pdf"),
|
|
}
|
|
);
|
|
assert_eq!(
|
|
student.steps,
|
|
vec![Step::TypstCompile {
|
|
template: PathBuf::from("exports/student.typ"),
|
|
}]
|
|
);
|
|
|
|
// archive: file-tree artifact (root + outputs glob) + two ordered steps,
|
|
// a typst-compile followed by a shell step (order preserved).
|
|
let archive = &lesson.targets[1];
|
|
assert_eq!(archive.name, "archive");
|
|
assert_eq!(
|
|
archive.artifact,
|
|
Artifact::FileTree {
|
|
root: PathBuf::from("build/archive"),
|
|
outputs: "**/*.{html,js,json}".to_string(),
|
|
}
|
|
);
|
|
assert_eq!(
|
|
archive.steps,
|
|
vec![
|
|
Step::TypstCompile {
|
|
template: PathBuf::from("exports/archive.typ"),
|
|
},
|
|
Step::Shell {
|
|
run: "npm run build".to_string(),
|
|
},
|
|
]
|
|
);
|
|
|
|
// teacher: empty body → all defaults.
|
|
let teacher = &lesson.targets[2];
|
|
assert_eq!(teacher.name, "teacher");
|
|
assert_eq!(
|
|
teacher.artifact,
|
|
Artifact::SingleFile {
|
|
filepath: PathBuf::from("build/teacher.pdf"),
|
|
}
|
|
);
|
|
assert_eq!(
|
|
teacher.steps,
|
|
vec![Step::TypstCompile {
|
|
template: PathBuf::from("exports/teacher.typ"),
|
|
}]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn malformed_target_config_is_non_fatal_with_schema_violations() {
|
|
use cph_model::{Artifact, Step};
|
|
|
|
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.type` → SchemaViolation, target kept with default artifact.
|
|
let student = &lesson.targets[0];
|
|
assert_eq!(
|
|
student.artifact,
|
|
Artifact::SingleFile {
|
|
filepath: PathBuf::from("build/student.pdf"),
|
|
}
|
|
);
|
|
|
|
// Bad step `type` → the step is skipped; the file-tree artifact survives and
|
|
// the empty step list falls back to the default step.
|
|
let teacher = &lesson.targets[1];
|
|
assert_eq!(
|
|
teacher.artifact,
|
|
Artifact::FileTree {
|
|
root: PathBuf::from("build/teacher"),
|
|
outputs: "**/*.html".to_string(),
|
|
}
|
|
);
|
|
assert_eq!(
|
|
teacher.steps,
|
|
vec![Step::TypstCompile {
|
|
template: PathBuf::from("exports/teacher.typ"),
|
|
}]
|
|
);
|
|
|
|
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 type, got: {diags:?}"
|
|
);
|
|
assert!(
|
|
violations
|
|
.iter()
|
|
.any(|d| d.message.contains("make-it-nice")),
|
|
"a diagnostic should name the bad step type, got: {diags:?}"
|
|
);
|
|
}
|
|
|
|
// --- `.cph-version` compatibility gate (ADR-0016) ------------------------------
|
|
|
|
/// The cph version the running CLI was built with — what `.cph-version` must
|
|
/// match exactly (ADR-0016's MVP rule).
|
|
const CPH_VERSION: &str = env!("CARGO_PKG_VERSION");
|
|
|
|
/// Write a one-part lesson into a uniquely-created temp dir, optionally with a
|
|
/// `.cph-version` file. `TempDir` owns cleanup so parallel tests cannot delete
|
|
/// one another's fixture after a timestamp collision.
|
|
fn tmp_lesson_with_version(version: Option<&str>) -> tempfile::TempDir {
|
|
let tmp = tempfile::Builder::new()
|
|
.prefix("cph-version-test-")
|
|
.tempdir()
|
|
.unwrap();
|
|
let p = tmp.path();
|
|
std::fs::write(
|
|
p.join("manifest.toml"),
|
|
"[project]\nid = \"v\"\nname = \"v\"\n[info]\ntitle = \"v\"\n[[children]]\nkind = \"segment\"\npath = \"segments/a\"\n",
|
|
)
|
|
.unwrap();
|
|
let seg = p.join("segments").join("a");
|
|
std::fs::create_dir_all(&seg).unwrap();
|
|
std::fs::write(seg.join("element.toml"), "kind = \"segment\"\n").unwrap();
|
|
std::fs::write(seg.join("textbook.typ"), "t.\n").unwrap();
|
|
if let Some(v) = version {
|
|
std::fs::write(p.join(".cph-version"), v).unwrap();
|
|
}
|
|
tmp
|
|
}
|
|
|
|
#[test]
|
|
fn cph_version_matching_is_not_a_diagnostic() {
|
|
let tmp = tmp_lesson_with_version(Some(CPH_VERSION));
|
|
let (_lesson, diags) = load(tmp.path());
|
|
assert!(
|
|
diags.iter().all(|d| d.code != DiagCode::CphVersionMismatch),
|
|
"a matching .cph-version must not warn, got {diags:?}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn cph_version_mismatch_is_an_error_diagnostic() {
|
|
let tmp = tmp_lesson_with_version(Some("99.99.99"));
|
|
let (lesson, diags) = load(tmp.path());
|
|
// The lesson still loads (so other defects could surface), but the
|
|
// mismatch is an error diagnostic — which alone makes it illegal.
|
|
assert!(lesson.is_some(), "a mismatch should not halt loading");
|
|
let mm: Vec<_> = diags
|
|
.iter()
|
|
.filter(|d| d.code == DiagCode::CphVersionMismatch)
|
|
.collect();
|
|
assert_eq!(
|
|
mm.len(),
|
|
1,
|
|
"expected one CphVersionMismatch, got {diags:?}"
|
|
);
|
|
assert!(
|
|
mm[0].message.contains("99.99.99") && mm[0].message.contains(CPH_VERSION),
|
|
"diagnostic should name both versions, got: {}",
|
|
mm[0].message
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn cph_version_missing_is_skipped() {
|
|
// No `.cph-version` file at all → skipped (ADR-0016 migration period; OPEN).
|
|
let tmp = tmp_lesson_with_version(None);
|
|
let (_lesson, diags) = load(tmp.path());
|
|
assert!(
|
|
diags.iter().all(|d| d.code != DiagCode::CphVersionMismatch),
|
|
"a missing .cph-version must be skipped, got {diags:?}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn cph_version_empty_is_an_error() {
|
|
let tmp = tmp_lesson_with_version(Some(" \n"));
|
|
let (_lesson, diags) = load(tmp.path());
|
|
assert!(
|
|
diags
|
|
.iter()
|
|
.any(|d| d.code == DiagCode::CphVersionMismatch && d.message.contains("empty")),
|
|
"an empty .cph-version should be an error, got {diags:?}"
|
|
);
|
|
}
|