Files
cflmy 5c9a313424 refactor(PR-02): protocol/reader architecture, /api/documents and reader IA
- migrate crate to RFC-0002 §5.3 Reader layout: src/protocol/{analysis_v2,
  trace_v2,tool_v1}.rs, src/reader/{source,markdown,model,assemble,render,
  assets}.rs, src/config.rs; lib.rs is the composition root; delete app/project/
  semantic/trace/document/presentation/source_map/render/assets top-level files
- split presentation into model (types) and assemble (merge/stale/DocumentService)
- merge controlled project root/path boundary and UTF-8 byte spans into
  reader/source.rs; move view.json config to config.rs
- replace GET /api/document with read-only GET /api/documents (documents list +
  current outline/function/analysis summary) and add contract tests
- reading UI: drop variable/memory placeholder actions, keep one explicit
  unsupported explanation, add simple dark theme toggle over the single token
  layer; assert via rendering test
- reorganize tests into Cargo [[test]] suites tests/contract and
  tests/integration; record docs/evidence/PR-02
- unit 41 + contract 41 + integration 33 all pass; clippy clean
2026-11-22 13:08:09 +08:00

109 lines
4.0 KiB
Rust

//! CLI surface tests: this program owns its own help/subcommands and refuses
//! unsafe defaults. Exit codes follow RFC-VIEW-0001 §10.2.
use crate::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.2.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");
// Explicitly point at a missing core so the test is independent of the
// caller environment (acceptance scripts export MARQDO_BIN).
let context = marqdo_view::reader::assemble::DocumentService::open(
&project.dir,
Some(std::path::Path::new("/definitely/missing/marqdo-frozen")),
)
.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"));
}