//! 真实 CLI 拒绝合同漂移;只改实现或移动文件不改变稳定合同。 use super::*; #[test] fn api_baseline_is_stable_and_never_overwritten() { let root = directory("api-baseline"); let domain = root.join("module/demo/catalog"); fs::create_dir_all(&domain).unwrap(); let app = domain.join("app/main.dever"); let initial = "type Item {name:Text,private key:Int}\necho(value:Item) (out:Text) {out=value.name}\napply(callback:handler(value:Text) (out:Text), value:Text) (out:Text) {out=callback(value)}"; write_source(&app, initial).unwrap(); fs::write(domain.join("api.dever"), "public get echo = app.hello").unwrap(); write_source( &app, format!("{initial}\nhello() (out:Text) {{out=\"one\"}}"), ) .unwrap(); let source = root.to_str().unwrap(); success(cli(&["fmt", source])); let snapshot = success(cli(&["api", source])); assert!(snapshot.contains("demo.catalog.app.Item")); assert!(snapshot.contains("handler(value: Text)")); assert!(snapshot.contains("/demo/catalog/echo")); assert!(!snapshot.contains("key")); assert!(!snapshot.contains(source)); let baseline = root.join("dever.api"); success(cli(&[ "api", source, "--output", baseline.to_str().unwrap(), ])); assert_eq!(fs::read_to_string(&baseline).unwrap(), snapshot); assert!( !cli(&["api", source, "--output", baseline.to_str().unwrap()]) .status .success() ); success(cli(&["check", source])); // 新增不导出的 Domain 定义会移动类型编号,不应改变合同。 fs::write(domain.join("domain.dever"), "type Earlier {extra:Bool}").unwrap(); write_source( &app, format!("{initial}\nhello() (out:Text) {{out=\"two\"}}"), ) .unwrap(); success(cli(&["fmt", source])); assert_eq!(success(cli(&["api", source])), snapshot); success(cli(&["check", source])); fs::write( domain.join("api.dever"), "authenticated get echo = app.hello", ) .unwrap(); success(cli(&["fmt", source])); for command in ["check", "run", "build"] { let output = root.join("must-not-build"); let mut args = vec![command, source]; if command == "build" { args.extend(["--output", output.to_str().unwrap()]); } let result = cli(&args); assert!(!result.status.success()); assert!(String::from_utf8_lossy(&result.stderr).contains("public API differs")); assert!(!output.exists()); } assert_eq!(fs::read_to_string(&baseline).unwrap(), snapshot); let changed = success(cli(&["api", source])); assert_ne!(changed, snapshot); fs::write(&baseline, "dever-api 5\n").unwrap(); assert!( String::from_utf8_lossy(&cli(&["check", source]).stderr).contains("expected dever-api 6") ); } #[test] fn api_snapshot_tracks_model_defaults_and_nominal_ids() { let root = directory("api-model-contract"); let domain = root.join("module/demo/article"); fs::create_dir_all(&domain).unwrap(); let model = domain.join("data/main.dever"); write_source( &model, "type Article {title:Text(1,80) unique,score:Int default 7}", ) .unwrap(); let source = root.to_str().unwrap(); success(cli(&["fmt", source])); let first = success(cli(&["api", source])); assert!(first.contains("demo.article.data.id")); assert!(first.contains("default 7")); fs::write(root.join("dever.api"), &first).unwrap(); write_source( &model, "type Article {title:Text(1,80) unique,score:Int default 9}", ) .unwrap(); success(cli(&["fmt", source])); assert!(!cli(&["check", source]).status.success()); assert!(success(cli(&["api", source])).contains("default 9")); } #[test] fn api_snapshot_names_related_types_and_tracks_foreign_key_changes() { let root = directory("api-relations"); let domain = root.join("module/demo/catalog/data"); fs::create_dir_all(&domain).unwrap(); fs::write( domain.join("user.dever"), "type User {name:Text}\nrelation posts=data.post.author_id", ) .unwrap(); fs::write( domain.join("post.dever"), "type Post {title:Text,author_id:data.user.id,editor_id:data.user.id}", ) .unwrap(); let (loaded, program) = analyze(&root); let first = loaded.api_snapshot(&program); assert!( first.contains("Related>"), "{first}" ); assert!(first.contains("target=demo.catalog.data.post.Post key=author_id many=true")); assert!(!first.contains("Value_")); fs::write( domain.join("user.dever"), "type User {name:Text}\nrelation posts=data.post.editor_id", ) .unwrap(); let (loaded, program) = analyze(&root); let second = loaded.api_snapshot(&program); assert_ne!(first, second); assert!(second.contains("key=editor_id many=true")); let formatted = dever::compiler::format::format(&loaded.declarations); assert!( formatted.contains("relation posts = data.post.editor_id"), "{formatted}" ); } #[test] fn data_display_metadata_is_public_only_and_does_not_change_api_baseline() { let root = directory("api-field-metadata"); let domain = root.join("module/demo/article/data"); fs::create_dir_all(&domain).unwrap(); let file = domain.join("article.dv"); fs::write(&file, "type State { Draft, Published }\ntype Article { title: Text(1,80), state: State, private token: Text? }").unwrap(); let (project, program) = analyze(&root); let baseline = project.api_snapshot(&program); fs::write(&file, r#"type State { Draft name "草稿", Published name "发布" } type Article name "文章" { title: Text(1,80) name "标题" description "Editor’s title", state: State, private token: Text? name "私有" }"#).unwrap(); let (project, program) = analyze(&root); assert_eq!(project.api_snapshot(&program), baseline); let model = &project.data_metadata(&program)["demo.article.data.Article"]; assert_eq!(model["name"], "文章"); let fields = &model["fields"]; assert_eq!( fields["title"], serde_json::json!({"type":"Text","length":[1,80],"name":"标题","description":"Editor’s title"}) ); assert_eq!( fields["state"]["options"][0], serde_json::json!({"value":"Draft","label":"草稿"}) ); assert!(fields.get("token").is_none()); }