Files
marqdo-view/tests/cli_surface.rs
T
marqdo-view dev 80ce564f09 feat(PR-02): DocumentModel, Analysis v2 projection, single renderer and Live browsing
- document.rs: comrak marqdo.md.v1 parse -> DocBlock/Inline with exact UTF-8 spans
- semantic.rs: typed Analysis v2 validation (schema/profile/root_id/source_digest,
  units/functions/statements/resources/imports/references/calls/diagnostics,
  per-span UTF-8 checks with explicit view.source_span_invalid warnings)
- presentation.rs: one PresentationModel/Outline with source status matrix
- render.rs: one ReaderRenderer; sanitized GFM, statement anchors, heading mode
  badges (display-only prefix strip), source pane, docs/Live|Static link policy,
  CSP meta, no inline scripts, no raw HTML execution
- server.rs: loopback-only Live reader with Host/DNS-rebinding checks, strict
  percent decoding, project path boundary, security headers, read-only APIs
- app.rs: AppContext degradation (core missing -> prose-only, check error ->
  diagnostics + revoked semantics, protocol mismatch -> hard error)
- assets: single reader.css/reader.js + embedded SHA-256 AssetBundle, copied
  legacy logo/blossom/feather with provenance
- examples + frozen-CLI Analysis goldens + rendering/HTTP/asset/no-coupling tests
- ADR-0003 heading prefix/anchor identity; docs updated
2026-11-22 11:19:11 +08:00

103 lines
3.7 KiB
Rust

//! 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_on_missing_path_fails_without_starting_a_server() {
let cwd = std::env::current_dir().unwrap();
let output = run_view(
&["definitely-missing.mq.md", "--no-open"],
&cwd,
&clean_env(),
);
assert_eq!(output.status.code(), Some(1));
assert!(String::from_utf8_lossy(&output.stderr).contains("view.io"));
}
#[test]
fn degradation_without_core_keeps_prose_readable() {
// The reader must degrade to prose-only with an explicit status, never
// invent semantic annotations or a fake run state.
let project = common::TempProject::new("cli-degrade-no-core");
project.marqdo_project("# @knowledge 标题\n\n正文 **加粗**。\n");
let context = marqdo_view::app::AppContext::open(&project.dir, None).unwrap();
let relative = context.project().entry().relative.clone();
let model = context.load(&relative).unwrap();
assert_eq!(model.source_status.data_value(), "core-unavailable");
assert!(model.semantic.is_none());
assert!(model.document.plain_text().contains("正文"));
assert!(model.status_note.is_some());
}
#[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"));
}