feat(PR-01): single Rust package, Tool Protocol v1 bundle and PublicCoreClient
- src/lib.rs module layout + thin main.rs dispatcher; CLI owns browse/export/verify - core_client: MARQDO_BIN only, explicit argv (no shell), 0700 scratch dirs, wall-clock timeout killing the whole process group, version/protocol negotiation - project/source_map: canonical path boundary, SafeRelativePath, exact UTF-8 byte <-> line/column <-> UTF-16 mapping, SHA-256 of exact bytes - stable view.* error codes with 0/1/2 exit classes; BindDenied for non-loopback - verify use case: Analysis v2 schema/profile/root_id/source_digest binding, no execution - tool.json + tools/package.sh + tools/install-smoke.sh; frozen-CLI install smoke PASS - contract tests with a real fake-core executable + architectural coupling gates - COMPATIBILITY/CHANGELOG/STATUS updated
This commit is contained in:
1 parent
ef546f2ca6
commit
8ba20a5c67
23 files changed
+3692
-8
No files matched your search
@@ -0,0 +1,89 @@
|
||||
//! CLI surface tests: this program owns its own help/subcommands and refuses
|
||||
//! unsafe defaults. Exit codes follow RFC-VIEW-0001 §10.2.
|
||||
|
||||
mod common;
|
||||
|
||||
use common::run_view;
|
||||
|
||||
fn clean_env() -> Vec<(&'static str, Option<&'static str>)> {
|
||||
vec![
|
||||
("MARQDO_BIN", None),
|
||||
("MARQDO_TOOL_PROTOCOL", None),
|
||||
("MARQDO_PROTOCOL_ANALYSIS", None),
|
||||
("MARQDO_PROTOCOL_TRACE", None),
|
||||
]
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn help_comes_from_view_not_core() {
|
||||
let cwd = std::env::current_dir().unwrap();
|
||||
let output = run_view(&["--help"], &cwd, &clean_env());
|
||||
assert_eq!(output.status.code(), Some(0));
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
for needle in ["marqdo-view", "export", "verify", "PATH", "--marqdo-bin"] {
|
||||
assert!(
|
||||
stdout.contains(needle),
|
||||
"missing {needle} in help:\n{stdout}"
|
||||
);
|
||||
}
|
||||
// The Core's own help wording must not leak here.
|
||||
assert!(!stdout.contains("Statically check a document"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn version_is_the_tool_version() {
|
||||
let cwd = std::env::current_dir().unwrap();
|
||||
let output = run_view(&["--version"], &cwd, &clean_env());
|
||||
assert_eq!(output.status.code(), Some(0));
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
assert!(stdout.contains("marqdo-view 0.1.0"), "{stdout}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn export_requires_an_output_directory() {
|
||||
let cwd = std::env::current_dir().unwrap();
|
||||
let output = run_view(&["export", "example.mq.md"], &cwd, &clean_env());
|
||||
assert_eq!(output.status.code(), Some(2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_loopback_bind_is_refused_by_default() {
|
||||
let cwd = std::env::current_dir().unwrap();
|
||||
let output = run_view(&["--host", "0.0.0.0"], &cwd, &clean_env());
|
||||
assert_eq!(output.status.code(), Some(2));
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
assert!(stderr.contains("view.bind_denied"), "{stderr}");
|
||||
assert!(stderr.contains("--unsafe-expose"), "{stderr}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn browse_without_core_reports_core_missing() {
|
||||
// Browse is not implemented yet, but the reader must still fail with a
|
||||
// stable View code and exit class rather than pretending to work.
|
||||
let cwd = std::env::current_dir().unwrap();
|
||||
let output = run_view(&[], &cwd, &clean_env());
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
assert_eq!(output.status.code(), Some(2), "stderr: {stderr}");
|
||||
assert!(
|
||||
stderr.contains("view.unsupported") || stderr.contains("view.core_missing"),
|
||||
"unexpected diagnostic: {stderr}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verify_without_core_is_an_argument_error() {
|
||||
let project = common::TempProject::new("cli-verify-no-core");
|
||||
project.marqdo_project("# @knowledge fixture\n");
|
||||
let output = run_view(&["verify", "index.mq.md"], &project.dir, &clean_env());
|
||||
assert_eq!(output.status.code(), Some(2));
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
assert!(stderr.contains("view.core_missing"), "{stderr}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_document_is_an_io_error() {
|
||||
let project = common::TempProject::new("cli-missing-doc");
|
||||
let output = run_view(&["verify", "missing.mq.md"], &project.dir, &clean_env());
|
||||
assert_eq!(output.status.code(), Some(1));
|
||||
assert!(String::from_utf8_lossy(&output.stderr).contains("view.io"));
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
//! Shared test helpers for integration tests.
|
||||
//!
|
||||
//! Fake cores are real executables (never mocked Rust traits at the process
|
||||
//! boundary) so argv, exit codes, stderr and temp artifacts are all exercised.
|
||||
|
||||
#![allow(dead_code)]
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Command, Output};
|
||||
|
||||
pub fn view_bin() -> &'static str {
|
||||
env!("CARGO_BIN_EXE_marqdo-view")
|
||||
}
|
||||
|
||||
pub fn temp_root(name: &str) -> PathBuf {
|
||||
let dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join(name);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).expect("create test temp dir");
|
||||
dir
|
||||
}
|
||||
|
||||
pub struct TempProject {
|
||||
pub dir: PathBuf,
|
||||
}
|
||||
|
||||
impl TempProject {
|
||||
pub fn new(name: &str) -> Self {
|
||||
Self {
|
||||
dir: temp_root(name),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write(&self, relative: &str, contents: &str) -> PathBuf {
|
||||
let path = self.dir.join(relative);
|
||||
if let Some(parent) = path.parent() {
|
||||
std::fs::create_dir_all(parent).expect("create parent");
|
||||
}
|
||||
std::fs::write(&path, contents).expect("write fixture");
|
||||
path
|
||||
}
|
||||
|
||||
pub fn path(&self, relative: &str) -> PathBuf {
|
||||
self.dir.join(relative)
|
||||
}
|
||||
|
||||
pub fn marqdo_project(&self, source: &str) {
|
||||
self.write(
|
||||
"marqdo.yaml",
|
||||
"project:\n name: fixture\n version: 0.1.0\n",
|
||||
);
|
||||
self.write("index.mq.md", source);
|
||||
}
|
||||
}
|
||||
|
||||
/// A scriptable shell-based fake core. Every behaviour is controlled by files
|
||||
/// in its directory, so tests never mutate global environment variables.
|
||||
pub struct FakeCore {
|
||||
pub dir: PathBuf,
|
||||
pub script: PathBuf,
|
||||
}
|
||||
|
||||
impl FakeCore {
|
||||
pub fn new(name: &str) -> Self {
|
||||
let dir = temp_root(name);
|
||||
let script = dir.join("marqdo");
|
||||
let body = r#"#!/bin/sh
|
||||
dir="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
|
||||
{
|
||||
printf 'invocation\n'
|
||||
for arg in "$@"; do printf 'arg=%s\n' "$arg"; done
|
||||
} >> "$dir/argv.txt"
|
||||
if [ -f "$dir/sleep" ]; then
|
||||
sleep "$(cat "$dir/sleep")"
|
||||
fi
|
||||
case "$1" in
|
||||
--version)
|
||||
if [ -f "$dir/version" ]; then cat "$dir/version"; else printf 'marqdo 3.0.0-beta.1\n'; fi
|
||||
exit 0
|
||||
;;
|
||||
check)
|
||||
if [ -f "$dir/check_stderr" ]; then cat "$dir/check_stderr" >&2; fi
|
||||
emit=""
|
||||
prev=""
|
||||
for a in "$@"; do
|
||||
if [ "$prev" = "--emit" ]; then emit="$a"; fi
|
||||
prev="$a"
|
||||
done
|
||||
if [ -f "$dir/analysis.json" ] && [ -n "$emit" ]; then cp "$dir/analysis.json" "$emit"; fi
|
||||
if [ -f "$dir/check_exit" ]; then exit "$(cat "$dir/check_exit")"; fi
|
||||
exit 0
|
||||
;;
|
||||
run)
|
||||
trace=""
|
||||
prev=""
|
||||
for a in "$@"; do
|
||||
if [ "$prev" = "--trace-file" ]; then trace="$a"; fi
|
||||
prev="$a"
|
||||
done
|
||||
if [ -f "$dir/trace.json" ] && [ -n "$trace" ]; then cp "$dir/trace.json" "$trace"; fi
|
||||
if [ -f "$dir/run.json" ]; then cat "$dir/run.json"; else printf '{"command":"run","success":true}\n'; fi
|
||||
if [ -f "$dir/run_exit" ]; then exit "$(cat "$dir/run_exit")"; fi
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
printf 'fake core: unknown command %s\n' "$1" >&2
|
||||
exit 64
|
||||
;;
|
||||
esac
|
||||
"#;
|
||||
std::fs::write(&script, body).expect("write fake core");
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755))
|
||||
.expect("chmod fake core");
|
||||
}
|
||||
Self { dir, script }
|
||||
}
|
||||
|
||||
pub fn set_version(&self, text: &str) {
|
||||
std::fs::write(self.dir.join("version"), text).expect("write version");
|
||||
}
|
||||
|
||||
pub fn set_check(&self, exit_code: i32, stderr: &str) {
|
||||
std::fs::write(self.dir.join("check_exit"), exit_code.to_string()).expect("write exit");
|
||||
std::fs::write(self.dir.join("check_stderr"), stderr).expect("write stderr");
|
||||
}
|
||||
|
||||
pub fn set_analysis(&self, analysis: &str) {
|
||||
std::fs::write(self.dir.join("analysis.json"), analysis).expect("write analysis");
|
||||
}
|
||||
|
||||
pub fn set_run(&self, exit_code: i32, json: &str, trace: Option<&str>) {
|
||||
std::fs::write(self.dir.join("run_exit"), exit_code.to_string()).expect("write run exit");
|
||||
std::fs::write(self.dir.join("run.json"), json).expect("write run json");
|
||||
match trace {
|
||||
Some(trace) => {
|
||||
std::fs::write(self.dir.join("trace.json"), trace).expect("write trace");
|
||||
}
|
||||
None => {
|
||||
let _ = std::fs::remove_file(self.dir.join("trace.json"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_sleep_seconds(&self, seconds: u32) {
|
||||
std::fs::write(self.dir.join("sleep"), seconds.to_string()).expect("write sleep");
|
||||
}
|
||||
|
||||
pub fn argv_lines(&self) -> Vec<String> {
|
||||
std::fs::read_to_string(self.dir.join("argv.txt"))
|
||||
.unwrap_or_default()
|
||||
.lines()
|
||||
.map(str::to_string)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// All argument values for the most recent invocation.
|
||||
pub fn last_args(&self) -> Vec<String> {
|
||||
self.argv_lines()
|
||||
.into_iter()
|
||||
.filter_map(|line| line.strip_prefix("arg=").map(str::to_string))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_view(args: &[&str], cwd: &Path, envs: &[(&str, Option<&str>)]) -> Output {
|
||||
let mut command = Command::new(view_bin());
|
||||
command.args(args).current_dir(cwd);
|
||||
for (key, value) in envs {
|
||||
match value {
|
||||
Some(value) => {
|
||||
command.env(key, value);
|
||||
}
|
||||
None => {
|
||||
command.env_remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
command.output().expect("run marqdo-view")
|
||||
}
|
||||
|
||||
/// Minimal valid Analysis v2 JSON for a single module, with a caller-provided
|
||||
/// source digest and schema version.
|
||||
pub fn minimal_analysis(relative_path: &str, source_digest: &str, schema_version: u32) -> String {
|
||||
serde_json::json!({
|
||||
"schema": "marqdo.analysis",
|
||||
"schema_version": schema_version,
|
||||
"producer": {
|
||||
"name": "marqdo",
|
||||
"version": "3.0.0-beta.1",
|
||||
"markdown_profile": "marqdo.md.v1"
|
||||
},
|
||||
"project": {
|
||||
"root_id": "root:fixture@0.1.0",
|
||||
"identity_kind": "manifest",
|
||||
"lock_digest": "",
|
||||
"stdlib": null
|
||||
},
|
||||
"modules": [{
|
||||
"id": "root:fixture@0.1.0::index.mq.md",
|
||||
"package_instance_id": "root:fixture@0.1.0",
|
||||
"relative_path": relative_path,
|
||||
"source_digest": source_digest,
|
||||
"units": [],
|
||||
"functions": [],
|
||||
"imports": [],
|
||||
"references": [],
|
||||
"calls": [],
|
||||
"resources": []
|
||||
}],
|
||||
"diagnostics": []
|
||||
})
|
||||
.to_string()
|
||||
}
|
||||
|
||||
pub fn sha256_hex(bytes: &[u8]) -> String {
|
||||
use sha2::{Digest, Sha256};
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(bytes);
|
||||
let digest = hasher.finalize();
|
||||
let mut out = String::with_capacity(64);
|
||||
for byte in digest {
|
||||
use std::fmt::Write as _;
|
||||
let _ = write!(out, "{byte:02x}");
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
//! Contract tests against a real fake-core executable: argv shape, exit codes,
|
||||
//! stderr handling, temp artifacts and process-tree cleanup.
|
||||
//!
|
||||
//! The fake core is a shell script, not a mocked Rust trait, so everything at
|
||||
//! the process boundary is exercised for real.
|
||||
|
||||
mod common;
|
||||
|
||||
use common::{minimal_analysis, sha256_hex, FakeCore, TempProject};
|
||||
use marqdo_view::core_client::{CheckOptions, CoreClient, RunOptions};
|
||||
use marqdo_view::project::Project;
|
||||
|
||||
fn fixture(name: &str) -> (TempProject, FakeCore) {
|
||||
let project = TempProject::new(name);
|
||||
project.marqdo_project("# @knowledge fixture\n");
|
||||
let core = FakeCore::new(&format!("{name}-core"));
|
||||
(project, core)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_returns_exact_analysis_bytes_and_digest() {
|
||||
let (project, core) = fixture("check_ok");
|
||||
let source = std::fs::read(project.path("index.mq.md")).unwrap();
|
||||
let analysis = minimal_analysis("index.mq.md", &sha256_hex(&source), 2);
|
||||
core.set_analysis(&analysis);
|
||||
|
||||
let client = CoreClient::with_bin(&core.script).unwrap();
|
||||
let project = Project::open(&project.path("index.mq.md")).unwrap();
|
||||
let entry = project.entry().relative.clone();
|
||||
let output = client
|
||||
.check(&project, &entry, &CheckOptions::default())
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.analysis_bytes, analysis.as_bytes());
|
||||
assert_eq!(output.analysis_sha256, sha256_hex(analysis.as_bytes()));
|
||||
assert_eq!(
|
||||
output.analysis_digest,
|
||||
format!("sha256:{}", sha256_hex(analysis.as_bytes()))
|
||||
);
|
||||
let args = core.last_args();
|
||||
assert_eq!(args[0], "check");
|
||||
assert!(args.contains(&"--emit".to_string()));
|
||||
// No `--refs` unless requested.
|
||||
assert!(!args.contains(&"--refs".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_refs_is_forwarded_as_a_stricter_flag() {
|
||||
let (project, core) = fixture("check_refs");
|
||||
let source = std::fs::read(project.path("index.mq.md")).unwrap();
|
||||
core.set_analysis(&minimal_analysis("index.mq.md", &sha256_hex(&source), 2));
|
||||
let client = CoreClient::with_bin(&core.script).unwrap();
|
||||
let project = Project::open(&project.path("index.mq.md")).unwrap();
|
||||
let entry = project.entry().relative.clone();
|
||||
client
|
||||
.check(
|
||||
&project,
|
||||
&entry,
|
||||
&CheckOptions {
|
||||
refs: true,
|
||||
timeout: None,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
assert!(core.last_args().contains(&"--refs".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_failure_preserves_core_stderr_and_code() {
|
||||
let (project, core) = fixture("check_fail");
|
||||
core.set_check(1, "index.mq.md:3:1: sem.unclosed_control\n");
|
||||
let client = CoreClient::with_bin(&core.script).unwrap();
|
||||
let project = Project::open(&project.path("index.mq.md")).unwrap();
|
||||
let entry = project.entry().relative.clone();
|
||||
let err = client
|
||||
.check(&project, &entry, &CheckOptions::default())
|
||||
.unwrap_err();
|
||||
assert_eq!(err.code(), "view.analysis_failed");
|
||||
assert_eq!(err.exit_code(), 1);
|
||||
let text = err.to_string();
|
||||
assert!(text.contains("sem.unclosed_control"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_returns_json_and_raw_trace() {
|
||||
let (project, core) = fixture("run_ok");
|
||||
core.set_run(
|
||||
0,
|
||||
r#"{"command":"run","success":true,"stdout":"ok","stderr":"","trace":[]}"#,
|
||||
Some(r#"{"schema":"marqdo.trace","schema_version":2,"events":[]}"#),
|
||||
);
|
||||
let client = CoreClient::with_bin(&core.script).unwrap();
|
||||
let project = Project::open(&project.path("index.mq.md")).unwrap();
|
||||
let entry = project.entry().relative.clone();
|
||||
let output = client
|
||||
.run(&project, &entry, &RunOptions::default())
|
||||
.unwrap();
|
||||
assert_eq!(output.exit_code, 0);
|
||||
assert_eq!(output.json["success"], true);
|
||||
let trace = output.trace_bytes.expect("trace bytes");
|
||||
assert_eq!(output.trace_sha256.unwrap(), sha256_hex(&trace));
|
||||
// Mode is always explicit; pure is the View default.
|
||||
let args = core.last_args();
|
||||
assert!(args.contains(&"--mode".to_string()));
|
||||
let mode_index = args.iter().position(|a| a == "--mode").unwrap();
|
||||
assert_eq!(args[mode_index + 1], "pure");
|
||||
assert!(args.contains(&"--json".to_string()));
|
||||
assert!(args.contains(&"--trace-file".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_without_trace_file_reports_no_trace() {
|
||||
let (project, core) = fixture("run_no_trace");
|
||||
core.set_run(
|
||||
0,
|
||||
r#"{"command":"run","success":true,"stdout":"","stderr":"","trace":[]}"#,
|
||||
None,
|
||||
);
|
||||
let client = CoreClient::with_bin(&core.script).unwrap();
|
||||
let project = Project::open(&project.path("index.mq.md")).unwrap();
|
||||
let entry = project.entry().relative.clone();
|
||||
let output = client
|
||||
.run(&project, &entry, &RunOptions::default())
|
||||
.unwrap();
|
||||
assert!(output.trace_bytes.is_none());
|
||||
assert!(output.trace_sha256.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_invalid_json_is_a_protocol_io_error() {
|
||||
let (project, core) = fixture("run_bad_json");
|
||||
core.set_run(0, "this is not json", None);
|
||||
let client = CoreClient::with_bin(&core.script).unwrap();
|
||||
let project = Project::open(&project.path("index.mq.md")).unwrap();
|
||||
let entry = project.entry().relative.clone();
|
||||
let err = client
|
||||
.run(&project, &entry, &RunOptions::default())
|
||||
.unwrap_err();
|
||||
assert_eq!(err.code(), "view.io");
|
||||
assert!(err.to_string().contains("exactly one JSON"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn timeout_kills_the_process_group_and_reports_timeout() {
|
||||
let (project, core) = fixture("run_timeout");
|
||||
core.set_sleep_seconds(5);
|
||||
core.set_run(0, r#"{"command":"run","success":true}"#, None);
|
||||
let client = CoreClient::with_bin(&core.script).unwrap();
|
||||
let project = Project::open(&project.path("index.mq.md")).unwrap();
|
||||
let entry = project.entry().relative.clone();
|
||||
let started = std::time::Instant::now();
|
||||
let err = client
|
||||
.run(
|
||||
&project,
|
||||
&entry,
|
||||
&RunOptions {
|
||||
view_timeout: Some(std::time::Duration::from_millis(300)),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.unwrap_err();
|
||||
let elapsed = started.elapsed();
|
||||
assert_eq!(err.code(), "view.core_timeout");
|
||||
assert!(
|
||||
elapsed < std::time::Duration::from_secs(3),
|
||||
"timeout took {elapsed:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incompatible_product_version_is_rejected_before_any_work() {
|
||||
let (project, core) = fixture("version_old");
|
||||
core.set_version("marqdo 2.9.1\n");
|
||||
let client = CoreClient::with_bin(&core.script).unwrap();
|
||||
let err = client.negotiate().unwrap_err();
|
||||
assert_eq!(err.code(), "view.protocol_incompatible");
|
||||
assert_eq!(err.exit_code(), 2);
|
||||
// Only `--version` was invoked; no check/run happened.
|
||||
assert_eq!(core.last_args(), vec!["--version".to_string()]);
|
||||
let _ = project;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_emit_after_success_is_an_analysis_failure() {
|
||||
// Fake core exits 0 but writes nothing: never accept a phantom analysis.
|
||||
let (project, core) = fixture("check_no_emit");
|
||||
let client = CoreClient::with_bin(&core.script).unwrap();
|
||||
let project = Project::open(&project.path("index.mq.md")).unwrap();
|
||||
let entry = project.entry().relative.clone();
|
||||
let err = client
|
||||
.check(&project, &entry, &CheckOptions::default())
|
||||
.unwrap_err();
|
||||
assert_eq!(err.code(), "view.analysis_failed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cli_verify_end_to_end_against_fake_core() {
|
||||
let (project, core) = fixture("verify_cli");
|
||||
let source = std::fs::read(project.path("index.mq.md")).unwrap();
|
||||
core.set_analysis(&minimal_analysis("index.mq.md", &sha256_hex(&source), 2));
|
||||
let script = core.script.to_str().unwrap().to_string();
|
||||
let dir = project.dir.to_str().unwrap().to_string();
|
||||
let output = common::run_view(
|
||||
&["verify", "index.mq.md", "--marqdo-bin", &script],
|
||||
std::path::Path::new(&dir),
|
||||
&[
|
||||
("MARQDO_BIN", None),
|
||||
("MARQDO_TOOL_PROTOCOL", None),
|
||||
("MARQDO_PROTOCOL_ANALYSIS", None),
|
||||
("MARQDO_PROTOCOL_TRACE", None),
|
||||
],
|
||||
);
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
assert_eq!(output.status.code(), Some(0), "stderr: {stderr}");
|
||||
assert!(stdout.contains("result: PASS"), "{stdout}");
|
||||
assert!(stdout.contains("marqdo 3.0.0-beta.1"), "{stdout}");
|
||||
assert!(!stdout.contains(&dir), "absolute path leaked: {stdout}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cli_verify_rejects_schema_v3_and_digest_mismatch() {
|
||||
let (project, core) = fixture("verify_negative");
|
||||
let source = std::fs::read(project.path("index.mq.md")).unwrap();
|
||||
let script = core.script.to_str().unwrap().to_string();
|
||||
let dir = project.dir.to_str().unwrap().to_string();
|
||||
|
||||
core.set_analysis(&minimal_analysis("index.mq.md", &sha256_hex(&source), 3));
|
||||
let output = common::run_view(
|
||||
&["verify", "index.mq.md", "--marqdo-bin", &script],
|
||||
std::path::Path::new(&dir),
|
||||
&[("MARQDO_BIN", None)],
|
||||
);
|
||||
assert_eq!(output.status.code(), Some(2));
|
||||
assert!(String::from_utf8_lossy(&output.stderr).contains("view.protocol_incompatible"));
|
||||
|
||||
core.set_analysis(&minimal_analysis("index.mq.md", &"0".repeat(64), 2));
|
||||
let output = common::run_view(
|
||||
&["verify", "index.mq.md", "--marqdo-bin", &script],
|
||||
std::path::Path::new(&dir),
|
||||
&[("MARQDO_BIN", None)],
|
||||
);
|
||||
assert_eq!(output.status.code(), Some(1));
|
||||
assert!(String::from_utf8_lossy(&output.stderr).contains("view.source_mismatch"));
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
//! Architectural gates (RFC-VIEW-0001 §17.2): no private Core crates, no
|
||||
//! source-tree reads of the legacy repositories, no second renderer, no shell
|
||||
//! execution, one CSS/JS entry point.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
fn manifest_dir() -> PathBuf {
|
||||
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
}
|
||||
|
||||
fn read(path: &Path) -> String {
|
||||
std::fs::read_to_string(path).unwrap_or_else(|err| panic!("read {}: {err}", path.display()))
|
||||
}
|
||||
|
||||
fn collect_files(dir: &Path, extension: &str, out: &mut Vec<PathBuf>) {
|
||||
let Ok(entries) = std::fs::read_dir(dir) else {
|
||||
return;
|
||||
};
|
||||
for entry in entries.flatten() {
|
||||
let path = entry.path();
|
||||
if path.is_dir() {
|
||||
collect_files(&path, extension, out);
|
||||
} else if path.extension().and_then(|e| e.to_str()) == Some(extension) {
|
||||
out.push(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_private_core_crate_dependencies() {
|
||||
let root = manifest_dir();
|
||||
let forbidden = [
|
||||
"marqdo",
|
||||
"marqdo-cli",
|
||||
"marqdo-ir",
|
||||
"marqdo-runtime",
|
||||
"marqdo-frontend",
|
||||
"marqdo-host",
|
||||
"marqdo-markdown",
|
||||
"marqdo-span",
|
||||
"marqdo-expression",
|
||||
"marqdo-sema",
|
||||
];
|
||||
let mut in_dependency_section = false;
|
||||
for line in read(&root.join("Cargo.toml")).lines() {
|
||||
let trimmed = line.trim();
|
||||
if trimmed.starts_with('[') {
|
||||
in_dependency_section = trimmed.ends_with("dependencies]")
|
||||
|| trimmed.contains("dependencies.")
|
||||
|| trimmed.contains("dependencies]");
|
||||
continue;
|
||||
}
|
||||
if !in_dependency_section || trimmed.is_empty() || trimmed.starts_with('#') {
|
||||
continue;
|
||||
}
|
||||
let Some((name, _)) = trimmed.split_once('=') else {
|
||||
continue;
|
||||
};
|
||||
let name = name.trim().trim_matches('"');
|
||||
assert!(
|
||||
!forbidden.contains(&name),
|
||||
"Cargo.toml declares forbidden Core dependency {name:?}"
|
||||
);
|
||||
}
|
||||
|
||||
let lock = read(&root.join("Cargo.lock"));
|
||||
for line in lock.lines() {
|
||||
let Some(rest) = line.trim().strip_prefix("name = \"") else {
|
||||
continue;
|
||||
};
|
||||
let name = rest.trim_end_matches('"');
|
||||
if name == "marqdo-view" {
|
||||
continue;
|
||||
}
|
||||
assert!(
|
||||
!name.starts_with("marqdo") || name == "marqdo",
|
||||
"Cargo.lock contains suspicious package {name:?}"
|
||||
);
|
||||
assert!(
|
||||
!forbidden.contains(&name),
|
||||
"Cargo.lock contains forbidden Core package {name:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn source_never_reads_or_links_the_legacy_repositories() {
|
||||
let root = manifest_dir();
|
||||
let mut sources = Vec::new();
|
||||
collect_files(&root.join("src"), "rs", &mut sources);
|
||||
assert!(!sources.is_empty(), "src/ has no Rust sources");
|
||||
let needles = [
|
||||
["marqdo", "_ir"].concat(),
|
||||
["marqdo", "-ir"].concat(),
|
||||
["marqdo", "_runtime"].concat(),
|
||||
["marqdo", "-runtime"].concat(),
|
||||
["marqdo", "_frontend"].concat(),
|
||||
["marqdo", "-frontend"].concat(),
|
||||
["marqdo", "::"].concat(),
|
||||
["..", "/marqdo"].concat(),
|
||||
["..", "/marqdo-cli"].concat(),
|
||||
["inspect", " --ir"].concat(),
|
||||
["Command::new", "(\"sh\")"].concat(),
|
||||
["Command::new", "(\"bash\")"].concat(),
|
||||
["sh", " -c"].concat(),
|
||||
["bash", " -c"].concat(),
|
||||
];
|
||||
for path in sources {
|
||||
let text = read(&path);
|
||||
for (line_number, line) in text.lines().enumerate() {
|
||||
let trimmed = line.trim_start();
|
||||
// Documentation may *name* forbidden APIs when explaining what is
|
||||
// not used; the gate only scans executable source lines.
|
||||
if trimmed.starts_with("//") {
|
||||
continue;
|
||||
}
|
||||
for needle in &needles {
|
||||
assert!(
|
||||
!line.contains(needle.as_str()),
|
||||
"{}:{} contains forbidden reference {:?}",
|
||||
path.display(),
|
||||
line_number + 1,
|
||||
needle
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exactly_one_reader_theme_and_script_entry() {
|
||||
let root = manifest_dir();
|
||||
let assets = root.join("assets");
|
||||
let mut css = Vec::new();
|
||||
let mut js = Vec::new();
|
||||
collect_files(&assets, "css", &mut css);
|
||||
collect_files(&assets, "js", &mut js);
|
||||
assert!(css.len() <= 1, "more than one CSS entry: {css:?}");
|
||||
assert!(js.len() <= 1, "more than one JS entry: {js:?}");
|
||||
for path in &css {
|
||||
assert_eq!(
|
||||
path.file_name().and_then(|n| n.to_str()),
|
||||
Some("reader.css"),
|
||||
"the only CSS entry must be assets/reader.css"
|
||||
);
|
||||
}
|
||||
for path in &js {
|
||||
assert_eq!(
|
||||
path.file_name().and_then(|n| n.to_str()),
|
||||
Some("reader.js"),
|
||||
"the only JS entry must be assets/reader.js"
|
||||
);
|
||||
}
|
||||
// The source tree must not reference legacy skin names at all.
|
||||
let mut sources = Vec::new();
|
||||
collect_files(&root.join("src"), "rs", &mut sources);
|
||||
for path in sources {
|
||||
let text = read(&path);
|
||||
for needle in ["notebook-page", "reader-page", "legacy.css", "new.css"] {
|
||||
assert!(
|
||||
!text.contains(needle),
|
||||
"{} references legacy skin marker {needle:?}",
|
||||
path.display()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_manifest_matches_package_and_protocols() {
|
||||
let root = manifest_dir();
|
||||
let raw = read(&root.join("tool.json"));
|
||||
let tool: serde_json::Value = serde_json::from_str(&raw).expect("tool.json is JSON");
|
||||
assert_eq!(tool["schema"], "marqdo.cli-tool");
|
||||
assert_eq!(tool["schema_version"], 1);
|
||||
assert_eq!(tool["name"], "marqdo-view");
|
||||
assert_eq!(tool["command"], "view");
|
||||
assert_eq!(tool["entrypoint"], "bin/marqdo-view");
|
||||
assert_eq!(
|
||||
tool["version"],
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
"tool.json version must match Cargo package version"
|
||||
);
|
||||
assert_eq!(tool["compatibility"]["marqdo"], ">=3.0.0-beta.1,<4.0.0");
|
||||
assert_eq!(tool["compatibility"]["analysis"], serde_json::json!([2]));
|
||||
assert_eq!(tool["compatibility"]["trace"], serde_json::json!([2]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn release_lockfile_is_committed() {
|
||||
let root = manifest_dir();
|
||||
assert!(
|
||||
root.join("Cargo.lock").is_file(),
|
||||
"Cargo.lock must be committed for --locked release builds"
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user