feat: migrate View 0.3.0 to marqdo 3.0.0-rc.4 contracts
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline was successful
Replace the retired rc.2 Analysis v2/Trace v2/Execution Events v1 stack with the
single current contract family:
- Analysis v3 (ADR-0069 public boundary): one check --emit artifact carries the
exact source, Markdown AST, heading scopes (heading_span + complete span),
anchors, state-bearing references, calls, resources and diagnostics; the old
revision-3 shape is rejected with a rebuild hint.
- Observation v1: one run/call --output jsonl stream (+ atomic
--observation-file snapshot) decoded by one decoder and projected into the
reader/SSE/export layers; stale/malformed/truncated/incomplete never render
as success.
- CLI Tool revision 1: requires {api_revision:1, analysis:3, observation:1},
product range >=3.0.0-rc.3,<4.0.0, only MARQDO_BIN and
MARQDO_API_REVISION are consumed.
- Remove compatibility branches, retired flags/env vars and duplicate decoders;
re-record Analysis v3 and Observation v1 fixtures; rewrite contract,
integration and security tests (173 passing locally).
- Add check-no-legacy/check-docs/check-observation gates, rc.4 pins in the
CI pipelines, 0.3.0 Tool bundle and evidence manifest support.
See issues #4, #5 and #6; acceptance baseline is CLI source
452e9a32bd9e40dba427d4180691c58bab74449e, archive
9c3f166b5e5b8840d39d10019074e4e1c42432fc23e26c69670e64fdcf4e15d8.
This commit is contained in:
1 parent
7b34199c96
commit
340ecb035f
108 files changed
+13928
-6461
No files matched your search
+183
-180
@@ -2,7 +2,8 @@
|
||||
//! unique terminal state, SSE-ready events, dedupe, grants and cancellation.
|
||||
//!
|
||||
//! The fake Core is a real executable (never a mocked trait) so argv, process
|
||||
//! lifecycle and trace files are exercised at the process boundary.
|
||||
//! lifecycle and the Observation v1 stdout/snapshot destinations are exercised at
|
||||
//! the process boundary.
|
||||
|
||||
use crate::common::{sha256_hex, FakeCore};
|
||||
use marqdo_view::execution::controller::{ExecutionConfig, ExecutionController, SseSubscription};
|
||||
@@ -12,12 +13,19 @@ use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
const ANALYSIS: &str = "tests/golden/analysis-v3/control-flow.json";
|
||||
const OBS_JSONL: &str = "tests/golden/observation-v1/control-flow.jsonl";
|
||||
const OBS_SNAPSHOT: &str = "tests/golden/observation-v1/control-flow.snapshot.json";
|
||||
const OBS_TRUNCATED_JSONL: &str = "tests/golden/observation-v1/control-flow.truncated.jsonl";
|
||||
const OBS_TRUNCATED_SNAPSHOT: &str =
|
||||
"tests/golden/observation-v1/control-flow.truncated.snapshot.json";
|
||||
|
||||
fn repo_root() -> PathBuf {
|
||||
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
}
|
||||
|
||||
fn golden(name: &str) -> String {
|
||||
let path = repo_root().join("tests/golden/analysis-v2").join(name);
|
||||
let path = repo_root().join(name);
|
||||
std::fs::read_to_string(&path).unwrap_or_else(|err| panic!("read {path:?}: {err}"))
|
||||
}
|
||||
|
||||
@@ -35,14 +43,14 @@ fn fixture_with(
|
||||
name: &str,
|
||||
project_relative: &str,
|
||||
analysis: &str,
|
||||
run_json: &str,
|
||||
trace: Option<&str>,
|
||||
observation_jsonl: &str,
|
||||
observation_snapshot: Option<&str>,
|
||||
config: ExecutionConfig,
|
||||
) -> Fixture {
|
||||
let core = FakeCore::new(name);
|
||||
core.set_analysis(analysis);
|
||||
core.set_check_json(r#"{"command":"check","diagnostics":[],"errors":0,"success":true}"#);
|
||||
core.set_run(0, run_json, trace);
|
||||
core.set_observation(0, observation_jsonl, observation_snapshot);
|
||||
let project = repo_root().join(project_relative);
|
||||
let service = Arc::new(
|
||||
DocumentService::open(&project, Some(core.script.as_path()))
|
||||
@@ -57,19 +65,12 @@ fn fixture_with(
|
||||
}
|
||||
|
||||
fn control_flow(name: &str) -> Fixture {
|
||||
let analysis = golden("control-flow.json");
|
||||
let run =
|
||||
std::fs::read_to_string(repo_root().join("tests/golden/trace-v2/control-flow.run.json"))
|
||||
.expect("run golden");
|
||||
let trace =
|
||||
std::fs::read_to_string(repo_root().join("tests/golden/trace-v2/control-flow.trace.json"))
|
||||
.expect("trace golden");
|
||||
fixture_with(
|
||||
name,
|
||||
"examples/control-flow",
|
||||
&analysis,
|
||||
&run,
|
||||
Some(&trace),
|
||||
&golden(ANALYSIS),
|
||||
&golden(OBS_JSONL),
|
||||
Some(&golden(OBS_SNAPSHOT)),
|
||||
ExecutionConfig::default().with_timeout(10),
|
||||
)
|
||||
}
|
||||
@@ -106,7 +107,7 @@ fn is_terminal_data(data: &serde_json::Value) -> bool {
|
||||
.map(|state| {
|
||||
matches!(
|
||||
state,
|
||||
"succeeded" | "failed" | "cancelled" | "timed_out" | "unavailable"
|
||||
"succeeded" | "failed" | "cancelled" | "timed_out" | "unavailable" | "incomplete"
|
||||
)
|
||||
})
|
||||
.unwrap_or(false)
|
||||
@@ -159,9 +160,9 @@ fn wait_for_file(path: &Path) {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_entry_run_streams_accepted_running_and_succeeded_with_verified_trace() {
|
||||
fn explicit_entry_run_streams_accepted_running_and_verified_observation() {
|
||||
let fixture = control_flow("ctrl-success");
|
||||
let analysis = golden("control-flow.json");
|
||||
let analysis = golden(ANALYSIS);
|
||||
let body = request_body(
|
||||
&analysis,
|
||||
serde_json::json!({"kind": "entry"}),
|
||||
@@ -207,17 +208,26 @@ fn explicit_entry_run_streams_accepted_running_and_succeeded_with_verified_trace
|
||||
);
|
||||
assert_eq!(terminal["state"], "succeeded");
|
||||
assert_eq!(terminal["result"]["success"], true);
|
||||
assert_eq!(terminal["trace"]["status"], "verified");
|
||||
assert!(!terminal["trace"]["events"].as_array().unwrap().is_empty());
|
||||
assert_eq!(terminal["trace"]["stale"], false);
|
||||
// The fake Core's stderr/stdout were read through the adapter, not guessed.
|
||||
assert_eq!(terminal["result"]["observation_status"], "verified");
|
||||
let summary = terminal["observation_summary"]
|
||||
.as_object()
|
||||
.expect("observation summary");
|
||||
assert_eq!(summary["status"], "verified");
|
||||
assert!(!summary["events"].as_array().unwrap().is_empty());
|
||||
assert_eq!(terminal["result"]["stdout"], "0\n0\n2\n");
|
||||
let argv = fixture.core.last_args();
|
||||
assert!(argv.contains(&"--output".to_string()), "argv: {argv:?}");
|
||||
assert!(argv.contains(&"jsonl".to_string()), "argv: {argv:?}");
|
||||
assert!(
|
||||
argv.contains(&"--observation-file".to_string()),
|
||||
"argv: {argv:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn call_target_uses_the_call_subcommand_and_function_name() {
|
||||
let fixture = control_flow("ctrl-call");
|
||||
let analysis = golden("control-flow.json");
|
||||
let analysis = golden(ANALYSIS);
|
||||
let function_id = "root:control-flow@0.1.0::index.mq.md::演示.main";
|
||||
let body = request_body(
|
||||
&analysis,
|
||||
@@ -236,15 +246,22 @@ fn call_target_uses_the_call_subcommand_and_function_name() {
|
||||
.expect("subscribe");
|
||||
let (_events, terminal) = terminal_events(subscription);
|
||||
assert_eq!(terminal["state"], "succeeded");
|
||||
let argv = std::fs::read_to_string(fixture.core.dir.join("argv.txt")).unwrap_or_default();
|
||||
assert!(argv.contains("arg=call"), "argv: {argv}");
|
||||
assert!(argv.contains("arg=main"), "argv: {argv}");
|
||||
let argv = fixture.core.last_args();
|
||||
assert!(argv.iter().any(|arg| arg == "call"), "argv: {argv:?}");
|
||||
assert!(
|
||||
argv.iter().any(|arg| arg.contains("main")),
|
||||
"argv: {argv:?}"
|
||||
);
|
||||
assert!(
|
||||
!argv.iter().any(|arg| arg == "--trace-file"),
|
||||
"argv: {argv:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn same_client_request_id_is_idempotent_and_different_payload_conflicts() {
|
||||
let fixture = control_flow("ctrl-dedupe");
|
||||
let analysis = golden("control-flow.json");
|
||||
let analysis = golden(ANALYSIS);
|
||||
let body = request_body(
|
||||
&analysis,
|
||||
serde_json::json!({"kind": "entry"}),
|
||||
@@ -274,17 +291,9 @@ fn same_client_request_id_is_idempotent_and_different_payload_conflicts() {
|
||||
|
||||
#[test]
|
||||
fn second_run_is_busy_while_one_is_active_and_cancel_is_idempotent() {
|
||||
let analysis = golden("control-flow.json");
|
||||
let run = r#"{"command":"run","success":true,"value":{"type":"int","decimal":"1"}}"#;
|
||||
let fixture = fixture_with(
|
||||
"ctrl-cancel",
|
||||
"examples/control-flow",
|
||||
&analysis,
|
||||
run,
|
||||
None,
|
||||
ExecutionConfig::default().with_timeout(30),
|
||||
);
|
||||
let fixture = control_flow("ctrl-cancel");
|
||||
fixture.core.set_sleep_seconds(30);
|
||||
let analysis = golden(ANALYSIS);
|
||||
let body = request_body(
|
||||
&analysis,
|
||||
serde_json::json!({"kind": "entry"}),
|
||||
@@ -320,7 +329,6 @@ fn second_run_is_busy_while_one_is_active_and_cancel_is_idempotent() {
|
||||
state,
|
||||
RunState::CancelRequested | RunState::Cancelled
|
||||
));
|
||||
// Repeat cancel is idempotent.
|
||||
let _ = fixture
|
||||
.controller
|
||||
.cancel("session-a", &accepted.run_id)
|
||||
@@ -334,17 +342,7 @@ fn second_run_is_busy_while_one_is_active_and_cancel_is_idempotent() {
|
||||
assert_eq!(terminal["state"], "cancelled");
|
||||
let terminal_count = events
|
||||
.iter()
|
||||
.filter(|event| {
|
||||
event["state"]
|
||||
.as_str()
|
||||
.map(|state| {
|
||||
matches!(
|
||||
state,
|
||||
"succeeded" | "failed" | "cancelled" | "timed_out" | "unavailable"
|
||||
)
|
||||
})
|
||||
.unwrap_or(false)
|
||||
})
|
||||
.filter(|event| is_terminal_data(event))
|
||||
.count();
|
||||
assert_eq!(terminal_count, 1, "exactly one terminal state: {events:?}");
|
||||
assert!(started.elapsed() < Duration::from_secs(10));
|
||||
@@ -353,7 +351,7 @@ fn second_run_is_busy_while_one_is_active_and_cancel_is_idempotent() {
|
||||
#[test]
|
||||
fn cross_session_observation_is_rejected() {
|
||||
let fixture = control_flow("ctrl-session");
|
||||
let analysis = golden("control-flow.json");
|
||||
let analysis = golden(ANALYSIS);
|
||||
let body = request_body(
|
||||
&analysis,
|
||||
serde_json::json!({"kind": "entry"}),
|
||||
@@ -384,9 +382,9 @@ fn cross_session_observation_is_rejected() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stale_analysis_digest_is_rejected_before_any_process_starts() {
|
||||
let fixture = control_flow("ctrl-stale");
|
||||
let analysis = golden("control-flow.json");
|
||||
fn stale_request_digest_is_rejected_before_any_process_starts() {
|
||||
let fixture = control_flow("ctrl-stale-request");
|
||||
let analysis = golden(ANALYSIS);
|
||||
let mut body: serde_json::Value = serde_json::from_slice(&request_body(
|
||||
&analysis,
|
||||
serde_json::json!({"kind": "entry"}),
|
||||
@@ -401,13 +399,13 @@ fn stale_analysis_digest_is_rejected_before_any_process_starts() {
|
||||
.submit("session-a", body.to_string().as_bytes())
|
||||
.expect_err("stale digest must be rejected");
|
||||
assert_eq!(err.code(), "view.run_invalid");
|
||||
assert!(!fixture.core.dir.join("run-was-called").exists());
|
||||
assert!(!fixture.core.run_was_called());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn project_grants_are_mapped_to_explicit_cli_flags_and_pure_grants_are_rejected() {
|
||||
let fixture = control_flow("ctrl-grants");
|
||||
let analysis = golden("control-flow.json");
|
||||
let analysis = golden(ANALYSIS);
|
||||
let body = request_body(
|
||||
&analysis,
|
||||
serde_json::json!({"kind": "entry"}),
|
||||
@@ -425,19 +423,22 @@ fn project_grants_are_mapped_to_explicit_cli_flags_and_pure_grants_are_rejected(
|
||||
.expect("subscribe");
|
||||
let (_events, terminal) = terminal_events(subscription);
|
||||
assert_eq!(terminal["state"], "succeeded");
|
||||
let argv = std::fs::read_to_string(fixture.core.dir.join("argv.txt")).unwrap_or_default();
|
||||
assert!(argv.contains("arg=--mode\narg=project"), "argv: {argv}");
|
||||
let argv = fixture.core.last_args();
|
||||
assert!(
|
||||
argv.contains("arg=--allow-read\narg=data/**"),
|
||||
"argv: {argv}"
|
||||
argv.windows(2).any(|pair| pair == ["--mode", "project"]),
|
||||
"argv: {argv:?}"
|
||||
);
|
||||
assert!(
|
||||
argv.contains("arg=--allow-write\narg=out/*.txt"),
|
||||
"argv: {argv}"
|
||||
argv.windows(2)
|
||||
.any(|pair| pair == ["--allow-read", "data/**"]),
|
||||
"argv: {argv:?}"
|
||||
);
|
||||
assert!(
|
||||
argv.windows(2)
|
||||
.any(|pair| pair == ["--allow-write", "out/*.txt"]),
|
||||
"argv: {argv:?}"
|
||||
);
|
||||
assert!(!argv.contains("arg=--mode\narg=pure"));
|
||||
|
||||
// pure with grants is rejected by request validation before the Core runs.
|
||||
let pure_with_grants = request_body(
|
||||
&analysis,
|
||||
serde_json::json!({"kind": "entry"}),
|
||||
@@ -453,23 +454,18 @@ fn project_grants_are_mapped_to_explicit_cli_flags_and_pure_grants_are_rejected(
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_without_a_trace_file_succeeds_but_reports_unavailable_trace() {
|
||||
let analysis = golden("control-flow.json");
|
||||
let run = r#"{"command":"run","success":true,"value":{"type":"int","decimal":"7"},"stdout":"hi\n","stderr":"","diagnostics":[]}"#;
|
||||
let fixture = fixture_with(
|
||||
"ctrl-no-trace",
|
||||
"examples/control-flow",
|
||||
&analysis,
|
||||
run,
|
||||
None,
|
||||
ExecutionConfig::default().with_timeout(10),
|
||||
);
|
||||
fn missing_observation_stream_is_incomplete_never_success() {
|
||||
let fixture = control_flow("ctrl-no-observation");
|
||||
fixture
|
||||
.core
|
||||
.set_observation(0, "", Some(&golden(OBS_SNAPSHOT)));
|
||||
let analysis = golden(ANALYSIS);
|
||||
let body = request_body(
|
||||
&analysis,
|
||||
serde_json::json!({"kind": "entry"}),
|
||||
"pure",
|
||||
serde_json::json!({}),
|
||||
"req-no-trace",
|
||||
"req-no-observation",
|
||||
);
|
||||
let accepted = fixture
|
||||
.controller
|
||||
@@ -480,62 +476,26 @@ fn run_without_a_trace_file_succeeds_but_reports_unavailable_trace() {
|
||||
.subscribe("session-a", &accepted.run_id, Some(0))
|
||||
.expect("subscribe");
|
||||
let (_events, terminal) = terminal_events(subscription);
|
||||
assert_eq!(terminal["state"], "succeeded");
|
||||
assert_eq!(terminal["result"]["value_text"], "Int(7)");
|
||||
assert_eq!(terminal["trace"], serde_json::Value::Null);
|
||||
assert_eq!(terminal["result"]["trace_status"], "unavailable");
|
||||
assert!(!fixture.core.dir.join("trace.json").exists() || terminal["trace"].is_null());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn event_frames_use_the_view_run_events_schema_and_numeric_ids() {
|
||||
let fixture = control_flow("ctrl-frames");
|
||||
let analysis = golden("control-flow.json");
|
||||
let body = request_body(
|
||||
&analysis,
|
||||
serde_json::json!({"kind": "entry"}),
|
||||
"pure",
|
||||
serde_json::json!({}),
|
||||
"req-frames",
|
||||
assert_eq!(terminal["state"], "failed");
|
||||
assert_eq!(
|
||||
terminal["result"]["error"]["code"],
|
||||
"view.observation_invalid"
|
||||
);
|
||||
let accepted = fixture
|
||||
.controller
|
||||
.submit("session-a", &body)
|
||||
.expect("accepted");
|
||||
let subscription = fixture
|
||||
.controller
|
||||
.subscribe("session-a", &accepted.run_id, None)
|
||||
.expect("subscribe");
|
||||
let (events, terminal) = terminal_events(subscription);
|
||||
assert_eq!(terminal["schema"], "marqdo.view-run-events");
|
||||
assert_eq!(terminal["schema_version"], 1);
|
||||
assert_eq!(terminal["run_id"], accepted.run_id.as_str());
|
||||
assert!(!events.is_empty());
|
||||
assert_ne!(terminal["result"]["success"], true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn source_edit_during_a_run_marks_the_result_stale_and_drops_anchor_ids() {
|
||||
let project = crate::common::TempProject::new("exec-stale");
|
||||
let source = std::fs::read_to_string(repo_root().join("examples/control-flow/index.mq.md"))
|
||||
.expect("example source");
|
||||
project.write(
|
||||
"marqdo.yaml",
|
||||
"project:\n name: control-flow\n version: 0.1.0\n",
|
||||
);
|
||||
project.write("index.mq.md", &source);
|
||||
let analysis = golden("control-flow.json");
|
||||
let run =
|
||||
std::fs::read_to_string(repo_root().join("tests/golden/trace-v2/control-flow.run.json"))
|
||||
.expect("run golden");
|
||||
let trace =
|
||||
std::fs::read_to_string(repo_root().join("tests/golden/trace-v2/control-flow.trace.json"))
|
||||
.expect("trace golden");
|
||||
let core = FakeCore::new("exec-stale-core");
|
||||
let project = crate::common::TempProject::new("ctrl-stale-source");
|
||||
project.marqdo_project(&golden("examples/control-flow/index.mq.md"));
|
||||
let analysis = golden(ANALYSIS);
|
||||
let core = FakeCore::new("ctrl-stale-source");
|
||||
core.set_analysis(&analysis);
|
||||
core.set_run(0, &run, Some(&trace));
|
||||
core.set_check_json(r#"{"command":"check","diagnostics":[],"errors":0,"success":true}"#);
|
||||
core.set_observation(0, &golden(OBS_JSONL), Some(&golden(OBS_SNAPSHOT)));
|
||||
core.set_sleep_seconds(1);
|
||||
let service = Arc::new(
|
||||
DocumentService::open(&project.dir, Some(core.script.as_path())).expect("service"),
|
||||
DocumentService::open(&project.dir, Some(core.script.as_path())).expect("open service"),
|
||||
);
|
||||
let controller = Arc::new(ExecutionController::new(
|
||||
Arc::clone(&service),
|
||||
@@ -550,51 +510,93 @@ fn source_edit_during_a_run_marks_the_result_stale_and_drops_anchor_ids() {
|
||||
);
|
||||
let accepted = controller.submit("session-a", &body).expect("accepted");
|
||||
wait_for_file(&core.dir.join("run-was-called"));
|
||||
project.write("index.mq.md", &format!("{source}\n\n追加的编辑。\n"));
|
||||
project.write(
|
||||
"index.mq.md",
|
||||
&format!(
|
||||
"{}\n\n<!-- changed -->\n",
|
||||
golden("examples/control-flow/index.mq.md")
|
||||
),
|
||||
);
|
||||
let subscription = controller
|
||||
.subscribe("session-a", &accepted.run_id, Some(0))
|
||||
.expect("subscribe");
|
||||
let (_events, terminal) = terminal_events(subscription);
|
||||
assert_eq!(terminal["state"], "succeeded");
|
||||
assert_eq!(terminal["result"]["stale"], true);
|
||||
assert_eq!(terminal["trace"]["status"], "verified");
|
||||
assert_eq!(terminal["trace"]["stale"], true);
|
||||
let events = terminal["trace"]["events"].as_array().expect("events");
|
||||
assert!(!events.is_empty());
|
||||
assert!(
|
||||
events
|
||||
.iter()
|
||||
.all(|event| event["anchor"].is_null() && event["source_id"].is_null()),
|
||||
"stale runs must not anchor to the current text"
|
||||
assert_eq!(terminal["state"], "failed");
|
||||
assert_eq!(
|
||||
terminal["result"]["error"]["code"],
|
||||
"view.observation_stale"
|
||||
);
|
||||
assert_eq!(terminal["result"]["observation_status"], "stale");
|
||||
assert_ne!(terminal["result"]["success"], true);
|
||||
let summary = terminal["observation_summary"]
|
||||
.as_object()
|
||||
.expect("stale summary");
|
||||
assert_eq!(summary["status"], "stale");
|
||||
assert!(summary["events"].as_array().unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trace_with_a_mismatched_digest_is_reported_invalid_but_the_run_still_succeeds() {
|
||||
let analysis = golden("control-flow.json");
|
||||
let run =
|
||||
std::fs::read_to_string(repo_root().join("tests/golden/trace-v2/control-flow.run.json"))
|
||||
.expect("run golden");
|
||||
let mut trace: serde_json::Value = serde_json::from_str(
|
||||
&std::fs::read_to_string(repo_root().join("tests/golden/trace-v2/control-flow.trace.json"))
|
||||
.expect("trace golden"),
|
||||
)
|
||||
.expect("trace json");
|
||||
trace["source"]["analysis_digest"] = serde_json::json!(format!("sha256:{}", "0".repeat(64)));
|
||||
let fixture = fixture_with(
|
||||
"exec-trace-invalid",
|
||||
"examples/control-flow",
|
||||
&analysis,
|
||||
&run,
|
||||
Some(&trace.to_string()),
|
||||
ExecutionConfig::default().with_timeout(10),
|
||||
fn observation_with_a_mismatched_digest_is_rejected_as_stale() {
|
||||
let fixture = control_flow("ctrl-observation-mismatch");
|
||||
let mut lines: Vec<String> = golden(OBS_JSONL).lines().map(str::to_string).collect();
|
||||
for line in &mut lines {
|
||||
if line.contains("\"kind\":\"run.started\"") {
|
||||
*line = line.replace(
|
||||
&format!(
|
||||
"\"analysis_digest\":\"sha256:{}\"",
|
||||
sha256_hex(&golden(ANALYSIS).into_bytes())
|
||||
),
|
||||
&format!("\"analysis_digest\":\"sha256:{}\"", "0".repeat(64)),
|
||||
);
|
||||
}
|
||||
}
|
||||
fixture.core.set_observation(
|
||||
0,
|
||||
&format!("{}\n", lines.join("\n")),
|
||||
Some(&golden(OBS_SNAPSHOT)),
|
||||
);
|
||||
let analysis = golden(ANALYSIS);
|
||||
let body = request_body(
|
||||
&analysis,
|
||||
serde_json::json!({"kind": "entry"}),
|
||||
"pure",
|
||||
serde_json::json!({}),
|
||||
"req-trace-invalid",
|
||||
"req-observation-mismatch",
|
||||
);
|
||||
let accepted = fixture
|
||||
.controller
|
||||
.submit("session-a", &body)
|
||||
.expect("accepted");
|
||||
let subscription = fixture
|
||||
.controller
|
||||
.subscribe("session-a", &accepted.run_id, Some(0))
|
||||
.expect("subscribe");
|
||||
let (_events, terminal) = terminal_events(subscription);
|
||||
assert_eq!(terminal["state"], "failed");
|
||||
assert_eq!(
|
||||
terminal["result"]["error"]["code"],
|
||||
"view.observation_stale"
|
||||
);
|
||||
assert_ne!(terminal["result"]["observation_status"], "verified");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncated_observation_is_explicit_and_never_labelled_complete() {
|
||||
let fixture = fixture_with(
|
||||
"ctrl-truncated",
|
||||
"examples/control-flow",
|
||||
&golden(ANALYSIS),
|
||||
&golden(OBS_TRUNCATED_JSONL),
|
||||
Some(&golden(OBS_TRUNCATED_SNAPSHOT)),
|
||||
ExecutionConfig::default().with_timeout(10),
|
||||
);
|
||||
let analysis = golden(ANALYSIS);
|
||||
let body = request_body(
|
||||
&analysis,
|
||||
serde_json::json!({"kind": "entry"}),
|
||||
"pure",
|
||||
serde_json::json!({}),
|
||||
"req-truncated",
|
||||
);
|
||||
let accepted = fixture
|
||||
.controller
|
||||
@@ -606,35 +608,27 @@ fn trace_with_a_mismatched_digest_is_reported_invalid_but_the_run_still_succeeds
|
||||
.expect("subscribe");
|
||||
let (_events, terminal) = terminal_events(subscription);
|
||||
assert_eq!(terminal["state"], "succeeded");
|
||||
assert_eq!(terminal["trace"]["status"], "invalid");
|
||||
assert!(terminal["trace"]["events"].is_null());
|
||||
assert_eq!(terminal["result"]["observation_status"], "truncated");
|
||||
assert_eq!(terminal["result"]["observation_truncated"], true);
|
||||
assert_eq!(terminal["result"]["output_complete"], false);
|
||||
let summary = terminal["observation_summary"]
|
||||
.as_object()
|
||||
.expect("observation summary");
|
||||
assert_eq!(summary["state"], "truncated");
|
||||
assert_eq!(summary["truncated"]["reason"], "event_budget");
|
||||
assert_eq!(summary["truncated"]["limit"], 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncated_trace_is_marked_without_claiming_complete_coverage() {
|
||||
let analysis = golden("control-flow.json");
|
||||
let run = std::fs::read_to_string(
|
||||
repo_root().join("tests/golden/trace-v2/control-flow.truncated.run.json"),
|
||||
)
|
||||
.expect("run golden");
|
||||
let trace = std::fs::read_to_string(
|
||||
repo_root().join("tests/golden/trace-v2/control-flow.truncated.trace.json"),
|
||||
)
|
||||
.expect("trace golden");
|
||||
let fixture = fixture_with(
|
||||
"exec-trace-truncated",
|
||||
"examples/control-flow",
|
||||
&analysis,
|
||||
&run,
|
||||
Some(&trace),
|
||||
ExecutionConfig::default().with_timeout(10),
|
||||
);
|
||||
fn event_frames_use_the_view_run_events_schema_and_numeric_ids() {
|
||||
let fixture = control_flow("ctrl-events");
|
||||
let analysis = golden(ANALYSIS);
|
||||
let body = request_body(
|
||||
&analysis,
|
||||
serde_json::json!({"kind": "entry"}),
|
||||
"pure",
|
||||
serde_json::json!({}),
|
||||
"req-trace-truncated",
|
||||
"req-events",
|
||||
);
|
||||
let accepted = fixture
|
||||
.controller
|
||||
@@ -642,11 +636,20 @@ fn truncated_trace_is_marked_without_claiming_complete_coverage() {
|
||||
.expect("accepted");
|
||||
let subscription = fixture
|
||||
.controller
|
||||
.subscribe("session-a", &accepted.run_id, Some(0))
|
||||
.subscribe("session-a", &accepted.run_id, None)
|
||||
.expect("subscribe");
|
||||
let (_events, terminal) = terminal_events(subscription);
|
||||
let (events, terminal) = terminal_events(subscription);
|
||||
assert!(events.iter().all(|event| {
|
||||
event["schema"] == "marqdo.view-run-events" && event["schema_version"] == 1
|
||||
}));
|
||||
assert!(events
|
||||
.iter()
|
||||
.all(|event| event["run_id"] == accepted.run_id));
|
||||
assert!(events.iter().all(|event| event["seq"].as_u64().is_some()));
|
||||
let observation_frames = events
|
||||
.iter()
|
||||
.filter(|event| event.get("observation").is_some())
|
||||
.count();
|
||||
assert!(observation_frames > 0, "events: {events:?}");
|
||||
assert_eq!(terminal["state"], "succeeded");
|
||||
assert_eq!(terminal["trace"]["status"], "verified");
|
||||
assert_eq!(terminal["trace"]["truncated"]["reason"], "event_budget");
|
||||
assert_eq!(terminal["trace"]["truncated"]["limit"], 5);
|
||||
}
|
||||
@@ -25,7 +25,7 @@ fn exported_dir(name: &str) -> PathBuf {
|
||||
|
||||
fn fake_core_for_welcome(name: &str) -> FakeCore {
|
||||
let core = FakeCore::new(name);
|
||||
let golden = std::fs::read(repo_root().join("tests/golden/analysis-v2/welcome.json")).unwrap();
|
||||
let golden = std::fs::read(repo_root().join("tests/golden/analysis-v3/welcome.json")).unwrap();
|
||||
core.set_analysis(std::str::from_utf8(&golden).unwrap());
|
||||
core
|
||||
}
|
||||
@@ -104,6 +104,14 @@ fn exports_offline_site_with_shared_renderer_and_relative_links() {
|
||||
assert_eq!(manifest["schema_version"], 1);
|
||||
assert_eq!(manifest["executed"], false);
|
||||
assert_eq!(manifest["view"]["name"], "marqdo-view");
|
||||
assert_eq!(manifest["requires"]["api_revision"], 1);
|
||||
assert_eq!(manifest["requires"]["analysis"], 3);
|
||||
assert_eq!(manifest["requires"]["observation"], 1);
|
||||
assert_eq!(manifest["producers"]["view"]["name"], "marqdo-view");
|
||||
let view_sha = manifest["producers"]["view"]["binary_sha256"]
|
||||
.as_str()
|
||||
.expect("view binary sha");
|
||||
assert!(view_sha.len() == 64 || view_sha == "unavailable");
|
||||
assert!(manifest["pages"].as_array().unwrap().len() >= 2);
|
||||
assert!(manifest["assets"].as_array().unwrap().len() >= 5);
|
||||
let manifest_text = serde_json::to_string(&manifest).unwrap();
|
||||
@@ -141,7 +149,7 @@ fn export_never_executes_side_effects() {
|
||||
let out = exported_dir("export-no-exec");
|
||||
let core = FakeCore::new("export-core-noexec");
|
||||
let golden =
|
||||
std::fs::read(repo_root().join("tests/golden/analysis-v2/control-flow.json")).unwrap();
|
||||
std::fs::read(repo_root().join("tests/golden/analysis-v3/control-flow.json")).unwrap();
|
||||
core.set_analysis(std::str::from_utf8(&golden).unwrap());
|
||||
let output = export(&project, &out, &[], Some(&core));
|
||||
assert_eq!(
|
||||
@@ -168,7 +176,7 @@ fn no_annotations_keeps_prose_but_removes_anchors() {
|
||||
let out = exported_dir("export-no-annotations");
|
||||
let core = FakeCore::new("export-core-noannot");
|
||||
let golden =
|
||||
std::fs::read(repo_root().join("tests/golden/analysis-v2/mixed-modes.json")).unwrap();
|
||||
std::fs::read(repo_root().join("tests/golden/analysis-v3/mixed-modes.json")).unwrap();
|
||||
core.set_analysis(std::str::from_utf8(&golden).unwrap());
|
||||
let output = export(&project, &out, &["--no-annotations"], Some(&core));
|
||||
assert_eq!(
|
||||
@@ -291,7 +299,7 @@ fn exported_html_has_no_remote_subresources() {
|
||||
let out = exported_dir("export-offline");
|
||||
let core = FakeCore::new("export-core-offline");
|
||||
let golden =
|
||||
std::fs::read(repo_root().join("tests/golden/analysis-v2/tables-math.json")).unwrap();
|
||||
std::fs::read(repo_root().join("tests/golden/analysis-v3/tables-math.json")).unwrap();
|
||||
core.set_analysis(std::str::from_utf8(&golden).unwrap());
|
||||
let output = export(&project, &out, &[], Some(&core));
|
||||
assert_eq!(output.status.code(), Some(0));
|
||||
|
||||
@@ -10,24 +10,22 @@ use crate::common::http::{
|
||||
};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
const RUN_GOLDEN: &str = "tests/golden/trace-v2/control-flow.run.json";
|
||||
const TRACE_GOLDEN: &str = "tests/golden/trace-v2/control-flow.trace.json";
|
||||
const ANALYSIS_GOLDEN: &str = "tests/golden/analysis-v2/control-flow.json";
|
||||
const OBS_JSONL: &str = "tests/golden/observation-v1/control-flow.jsonl";
|
||||
const OBS_SNAPSHOT: &str = "tests/golden/observation-v1/control-flow.snapshot.json";
|
||||
const ANALYSIS_GOLDEN: &str = "tests/golden/analysis-v3/control-flow.json";
|
||||
|
||||
fn control_flow() -> TestServer {
|
||||
http::start(
|
||||
"examples/control-flow",
|
||||
ANALYSIS_GOLDEN,
|
||||
Some(RUN_GOLDEN),
|
||||
Some(TRACE_GOLDEN),
|
||||
Some((OBS_JSONL, OBS_SNAPSHOT)),
|
||||
)
|
||||
}
|
||||
|
||||
fn welcome() -> TestServer {
|
||||
http::start(
|
||||
"examples/welcome",
|
||||
"tests/golden/analysis-v2/welcome.json",
|
||||
None,
|
||||
"tests/golden/analysis-v3/welcome.json",
|
||||
None,
|
||||
)
|
||||
}
|
||||
@@ -160,8 +158,11 @@ fn explicit_run_streams_sse_accepted_running_and_terminal_with_verified_trace()
|
||||
assert_eq!(data["run_id"], run_id.as_str());
|
||||
assert_eq!(data["state"], "succeeded");
|
||||
assert_eq!(data["result"]["success"], true);
|
||||
assert_eq!(data["trace"]["status"], "verified");
|
||||
assert!(!data["trace"]["events"].as_array().unwrap().is_empty());
|
||||
assert_eq!(data["observation_summary"]["status"], "verified");
|
||||
assert!(!data["observation_summary"]["events"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.is_empty());
|
||||
let states: Vec<_> = frames
|
||||
.iter()
|
||||
.filter_map(|frame| frame.data.as_ref().and_then(|data| data["state"].as_str()))
|
||||
@@ -277,8 +278,7 @@ fn cancel_is_idempotent_and_yields_one_terminal_state() {
|
||||
let server = http::start_with_env(
|
||||
"examples/control-flow",
|
||||
ANALYSIS_GOLDEN,
|
||||
Some(RUN_GOLDEN),
|
||||
Some(TRACE_GOLDEN),
|
||||
Some((OBS_JSONL, OBS_SNAPSHOT)),
|
||||
&[("MARQDO_VIEW_SSE_HEARTBEAT_SECS", "1")],
|
||||
);
|
||||
server.core.set_sleep_seconds(10);
|
||||
@@ -333,7 +333,12 @@ fn cancel_is_idempotent_and_yields_one_terminal_state() {
|
||||
.map(|state| {
|
||||
matches!(
|
||||
state,
|
||||
"succeeded" | "failed" | "cancelled" | "timed_out" | "unavailable"
|
||||
"succeeded"
|
||||
| "failed"
|
||||
| "cancelled"
|
||||
| "timed_out"
|
||||
| "unavailable"
|
||||
| "incomplete"
|
||||
)
|
||||
})
|
||||
.unwrap_or(false)
|
||||
@@ -352,8 +357,7 @@ fn sse_heartbeats_keep_a_slow_job_connected() {
|
||||
let server = http::start_with_env(
|
||||
"examples/control-flow",
|
||||
ANALYSIS_GOLDEN,
|
||||
Some(RUN_GOLDEN),
|
||||
Some(TRACE_GOLDEN),
|
||||
Some((OBS_JSONL, OBS_SNAPSHOT)),
|
||||
&[("MARQDO_VIEW_SSE_HEARTBEAT_SECS", "1")],
|
||||
);
|
||||
server.core.set_sleep_seconds(3);
|
||||
@@ -386,8 +390,7 @@ fn sse_heartbeats_keep_a_slow_job_connected() {
|
||||
fn document_without_main_is_rejected_without_running_another_function() {
|
||||
let server = http::start(
|
||||
"examples/tables-math",
|
||||
"tests/golden/analysis-v2/tables-math.json",
|
||||
None,
|
||||
"tests/golden/analysis-v3/tables-math.json",
|
||||
None,
|
||||
);
|
||||
let (session, _) = open_session(&server, "/");
|
||||
@@ -408,8 +411,7 @@ fn at_most_two_sse_observers_per_run() {
|
||||
let server = http::start_with_env(
|
||||
"examples/control-flow",
|
||||
ANALYSIS_GOLDEN,
|
||||
Some(RUN_GOLDEN),
|
||||
Some(TRACE_GOLDEN),
|
||||
Some((OBS_JSONL, OBS_SNAPSHOT)),
|
||||
&[("MARQDO_VIEW_SSE_HEARTBEAT_SECS", "1")],
|
||||
);
|
||||
server.core.set_sleep_seconds(5);
|
||||
|
||||
+102
-58
@@ -1,47 +1,81 @@
|
||||
//! Rendering contract tests: one renderer, safe Markdown, exact source view,
|
||||
//! verified semantic annotations and Live/Export link differences.
|
||||
//! verified Analysis v3 annotations and Live/Export link differences.
|
||||
|
||||
use marqdo_view::config::ViewConfig;
|
||||
use marqdo_view::protocol::analysis_v2 as semantic;
|
||||
use marqdo_view::reader::assemble::{self as assemble, BuildInput};
|
||||
use marqdo_view::protocol::analysis_v3;
|
||||
use marqdo_view::reader::assemble::{build, BuildInput};
|
||||
use marqdo_view::reader::markdown::{DocumentIdentity, DocumentModel};
|
||||
use marqdo_view::reader::model::{Navigation, SourceStatus};
|
||||
use marqdo_view::reader::render::{ReaderRenderer, RenderContext};
|
||||
use marqdo_view::reader::source::{Project, SafeRelativePath};
|
||||
|
||||
fn example(name: &str) -> (marqdo_view::reader::model::PresentationModel, String) {
|
||||
let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("examples")
|
||||
.join(name);
|
||||
let project = Project::open(&root).unwrap();
|
||||
let entry = project.entry().relative.clone();
|
||||
let source = project.read_source_text(&entry).unwrap();
|
||||
let document = DocumentModel::parse(source.clone(), DocumentIdentity::placeholder(&entry));
|
||||
let golden_name = name;
|
||||
let golden = std::fs::read(
|
||||
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("tests/golden/analysis-v2")
|
||||
.join(format!("{golden_name}.json")),
|
||||
)
|
||||
.unwrap_or_else(|err| panic!("missing golden for {name}: {err}"));
|
||||
let verified =
|
||||
semantic::verify_analysis(&golden, &source, &document.source_index, entry.as_str())
|
||||
.unwrap_or_else(|err| panic!("golden does not verify for {name}: {err}"));
|
||||
fn repo_root() -> std::path::PathBuf {
|
||||
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
}
|
||||
|
||||
fn example(
|
||||
name: &str,
|
||||
entry: &str,
|
||||
golden: &str,
|
||||
) -> (marqdo_view::reader::model::PresentationModel, String) {
|
||||
let project = Project::open(&repo_root().join("examples").join(name)).unwrap();
|
||||
let analysis = std::fs::read(repo_root().join(golden)).unwrap();
|
||||
let verified = analysis_v3::verify_analysis(&analysis, entry, None)
|
||||
.unwrap_or_else(|err| panic!("golden does not verify for {name}: {err}"));
|
||||
let source = verified.projection.source.clone();
|
||||
assert_eq!(
|
||||
verified.projection.source_digest,
|
||||
marqdo_view::reader::source::sha256_hex(source.as_bytes())
|
||||
);
|
||||
let entry_path = SafeRelativePath::parse(entry).unwrap();
|
||||
let document = DocumentModel::parse(source.clone(), DocumentIdentity::placeholder(&entry_path));
|
||||
let config = ViewConfig::load(project.root()).unwrap_or_default();
|
||||
let navigation = Navigation::from_project(&project, &config, &entry);
|
||||
let model = assemble::build(BuildInput {
|
||||
let navigation = Navigation::from_project(&project, &config, &entry_path);
|
||||
let model = build(BuildInput {
|
||||
document,
|
||||
analysis: Some(&verified),
|
||||
status: SourceStatus::StaticOk,
|
||||
status_note: None,
|
||||
diagnostics: Vec::new(),
|
||||
navigation,
|
||||
core_version: Some("3.0.0-beta.1".to_string()),
|
||||
core_version: Some("3.0.0-rc.4".to_string()),
|
||||
run: None,
|
||||
});
|
||||
(model, source)
|
||||
}
|
||||
|
||||
fn control_flow() -> (marqdo_view::reader::model::PresentationModel, String) {
|
||||
example(
|
||||
"control-flow",
|
||||
"index.mq.md",
|
||||
"tests/golden/analysis-v3/control-flow.json",
|
||||
)
|
||||
}
|
||||
|
||||
fn mixed_modes() -> (marqdo_view::reader::model::PresentationModel, String) {
|
||||
example(
|
||||
"mixed-modes",
|
||||
"index.mq.md",
|
||||
"tests/golden/analysis-v3/mixed-modes.json",
|
||||
)
|
||||
}
|
||||
|
||||
fn tables_math() -> (marqdo_view::reader::model::PresentationModel, String) {
|
||||
example(
|
||||
"tables-math",
|
||||
"index.mq.md",
|
||||
"tests/golden/analysis-v3/tables-math.json",
|
||||
)
|
||||
}
|
||||
|
||||
fn welcome() -> (marqdo_view::reader::model::PresentationModel, String) {
|
||||
example(
|
||||
"welcome",
|
||||
"00-welcome.mq.md",
|
||||
"tests/golden/analysis-v3/welcome.json",
|
||||
)
|
||||
}
|
||||
|
||||
fn render_live(model: &marqdo_view::reader::model::PresentationModel) -> String {
|
||||
ReaderRenderer::new()
|
||||
.render(model, &RenderContext::live())
|
||||
@@ -60,8 +94,7 @@ fn heading_ids(html: &str) -> Vec<String> {
|
||||
let Some(after_tag) = part.split_once(' ') else {
|
||||
continue;
|
||||
};
|
||||
let rest = after_tag.1;
|
||||
if let Some(id_part) = rest.split_once("id=\"") {
|
||||
if let Some(id_part) = after_tag.1.split_once("id=\"") {
|
||||
if let Some((id, _)) = id_part.1.split_once('"') {
|
||||
ids.push(id.to_string());
|
||||
}
|
||||
@@ -72,7 +105,7 @@ fn heading_ids(html: &str) -> Vec<String> {
|
||||
|
||||
#[test]
|
||||
fn page_has_exactly_one_theme_and_one_script() {
|
||||
let (model, _) = example("mixed-modes");
|
||||
let (model, _) = mixed_modes();
|
||||
let html = render_live(&model);
|
||||
assert_eq!(html.matches("<link rel=\"stylesheet\"").count(), 1);
|
||||
assert_eq!(
|
||||
@@ -84,14 +117,13 @@ fn page_has_exactly_one_theme_and_one_script() {
|
||||
assert!(html.contains("Content-Security-Policy"));
|
||||
assert!(html.contains("default-src 'none'"));
|
||||
assert!(html.contains("data-live=\"true\""));
|
||||
// The one source of prose is the Markdown document itself.
|
||||
assert!(html.contains("计算笔记"));
|
||||
assert!(html.contains("静态阅读"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn semantic_annotations_come_from_analysis_and_are_unique() {
|
||||
let (model, _) = example("mixed-modes");
|
||||
let (model, _) = mixed_modes();
|
||||
let html = render_live(&model);
|
||||
assert!(html.contains("data-source-status=\"static-ok\""));
|
||||
assert!(html.contains("data-statement-kind=\"return\""));
|
||||
@@ -109,31 +141,29 @@ fn semantic_annotations_come_from_analysis_and_are_unique() {
|
||||
|
||||
#[test]
|
||||
fn tables_math_and_code_are_inert_and_readable() {
|
||||
let (model, source) = example("tables-math");
|
||||
let (model, source) = tables_math();
|
||||
let html = render_live(&model);
|
||||
assert!(html.contains("<table>"));
|
||||
assert!(html.contains("mq-table-wrap"));
|
||||
assert!(html.contains("mq-math-block"));
|
||||
assert!(html.contains("代码围栏永远是惰性资源"));
|
||||
assert!(html.contains("mq-codeblock"));
|
||||
// Inert resources are never executed: no run controls are enabled.
|
||||
assert!(html.contains("data-live=\"true\""));
|
||||
assert!(!html.contains("RUNNING"));
|
||||
// Source is preserved exactly in the textarea (escaped only).
|
||||
assert!(source.contains("```python"));
|
||||
assert!(html.contains("mq-raw-source"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn welcome_hero_only_when_configured() {
|
||||
let (welcome, _) = example("welcome");
|
||||
let (welcome, _) = welcome();
|
||||
let html = render_live(&welcome);
|
||||
assert!(welcome.navigation.welcome);
|
||||
assert!(html.contains("mq-welcome"));
|
||||
assert!(html.contains("WELCOME TO"));
|
||||
assert!(html.contains("代码即文档 · 文档即知识库 · 知识即可执行"));
|
||||
|
||||
let (ordinary, _) = example("tables-math");
|
||||
let (ordinary, _) = tables_math();
|
||||
let html = render_live(&ordinary);
|
||||
assert!(!ordinary.navigation.welcome);
|
||||
assert!(!html.contains("mq-welcome"));
|
||||
@@ -141,25 +171,23 @@ fn welcome_hero_only_when_configured() {
|
||||
|
||||
#[test]
|
||||
fn live_and_static_share_content_but_rewrite_links() {
|
||||
let (model, _) = example("welcome");
|
||||
let (model, _) = welcome();
|
||||
let live = render_live(&model);
|
||||
let static_site = render_static(&model, 0);
|
||||
assert!(live.contains("/file?path=guide.mq.md"));
|
||||
assert!(static_site.contains("pages/guide.mq.md.html"));
|
||||
assert!(!static_site.contains("/file?path="));
|
||||
// Same prose and semantics.
|
||||
for needle in ["欢迎来到 Marqdo", "代码即文档", "mq-document"] {
|
||||
assert!(live.contains(needle));
|
||||
assert!(static_site.contains(needle));
|
||||
assert!(live.contains(needle), "live misses {needle}");
|
||||
assert!(static_site.contains(needle), "static misses {needle}");
|
||||
}
|
||||
// Assets are relative from the site root.
|
||||
assert!(static_site.contains("href=\"assets/reader."));
|
||||
assert!(live.contains("href=\"/assets/reader."));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn static_pages_use_parent_relative_assets_and_links() {
|
||||
let (model, _) = example("welcome");
|
||||
let (model, _) = welcome();
|
||||
let html = render_static(&model, 1);
|
||||
assert!(html.contains("href=\"../assets/reader."));
|
||||
assert!(html.contains("../pages/guide.mq.md.html"));
|
||||
@@ -193,17 +221,13 @@ fn malicious_markdown_cannot_inject_markup_or_scripts() {
|
||||
source_sha256: String::new(),
|
||||
},
|
||||
);
|
||||
let project = Project::open(
|
||||
&std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("examples/tables-math"),
|
||||
)
|
||||
.unwrap();
|
||||
let config = ViewConfig::default();
|
||||
let project = Project::open(&repo_root().join("examples/tables-math")).unwrap();
|
||||
let navigation = Navigation::from_project(
|
||||
&project,
|
||||
&config,
|
||||
&ViewConfig::default(),
|
||||
&SafeRelativePath::parse("index.mq.md").unwrap(),
|
||||
);
|
||||
let model = assemble::build(BuildInput {
|
||||
let model = build(BuildInput {
|
||||
document,
|
||||
analysis: None,
|
||||
status: SourceStatus::CoreUnavailable,
|
||||
@@ -219,10 +243,7 @@ fn malicious_markdown_cannot_inject_markup_or_scripts() {
|
||||
assert!(!html.contains("<svg onload"));
|
||||
assert!(!html.contains("href=\"javascript:"));
|
||||
assert!(!html.contains("href=\"data:text/html"));
|
||||
// Ordinary page chrome (logo, dock icons) is allowed, but no tag carries an
|
||||
// attacker-supplied event handler.
|
||||
assert!(!html.contains("onerror=\"") && !html.contains("onload=\""));
|
||||
// The exact source is still available in the source pane, safely escaped.
|
||||
assert!(html.contains("<script>alert"));
|
||||
assert!(html.contains("https://example.com/ok"));
|
||||
assert!(html.contains("已忽略原始 HTML"));
|
||||
@@ -231,29 +252,26 @@ fn malicious_markdown_cannot_inject_markup_or_scripts() {
|
||||
|
||||
#[test]
|
||||
fn rendering_is_deterministic() {
|
||||
let (model, _) = example("control-flow");
|
||||
let first = render_live(&model);
|
||||
let second = render_live(&model);
|
||||
assert_eq!(first, second);
|
||||
let (model, _) = control_flow();
|
||||
assert_eq!(render_live(&model), render_live(&model));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_controls_are_explicitly_unavailable_before_pr04() {
|
||||
let (model, _) = example("control-flow");
|
||||
fn run_controls_are_explicitly_unavailable_before_an_explicit_run() {
|
||||
let (model, _) = control_flow();
|
||||
let html = render_live(&model);
|
||||
assert!(html.contains("data-live=\"true\""));
|
||||
assert!(
|
||||
html.contains("data-disabled=\"true\"") || html.contains("aria-disabled"),
|
||||
"run control must have an explicit disabled marker"
|
||||
);
|
||||
// No fake RUNNING/SUCCEEDED marker on a freshly opened document.
|
||||
assert!(!html.contains("data-run-state=\"running\""));
|
||||
assert!(!html.contains("data-run-state=\"succeeded\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reader_exposes_simple_theme_toggle_and_no_variable_or_memory_tabs() {
|
||||
let (model, _source) = example("welcome");
|
||||
let (model, _) = welcome();
|
||||
let html = render_live(&model);
|
||||
assert!(html.contains("data-theme=\"light\""));
|
||||
assert!(html.contains("data-action=\"theme\""));
|
||||
@@ -261,3 +279,29 @@ fn reader_exposes_simple_theme_toggle_and_no_variable_or_memory_tabs() {
|
||||
assert!(!html.contains("data-action=\"memory\""));
|
||||
assert!(html.contains("data-action=\"unsupported\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn headings_use_heading_span_while_sections_use_the_complete_span() {
|
||||
// ADR-0069: `heading_span` locates the heading node; the complete `span`
|
||||
// includes child sections. The renderer must not use the complete span as the
|
||||
// heading id source (that would shift ids when a section grows).
|
||||
let (model, _) = control_flow();
|
||||
let projection = model.semantic.as_ref().unwrap();
|
||||
for unit in &projection.units {
|
||||
if let Some(heading) = unit.heading_span {
|
||||
if unit.span != heading {
|
||||
assert!(unit.span.contains(heading));
|
||||
}
|
||||
}
|
||||
}
|
||||
let function = &projection.functions[0];
|
||||
let html = render_live(&model);
|
||||
let expected = marqdo_view::reader::model::anchor_for_span(
|
||||
function.heading_span.start,
|
||||
function.heading_span.end,
|
||||
);
|
||||
assert!(
|
||||
html.contains(&format!("id=\"{expected}\"")),
|
||||
"{expected} missing"
|
||||
);
|
||||
}
|
||||
@@ -1,11 +1,12 @@
|
||||
//! Unicode/CRLF/duplicate-text span contract (RFC-VIEW-0001 S-04).
|
||||
//! Goldens are emitted by the frozen CLI from the exact fixture bytes.
|
||||
//! Unicode/CRLF/duplicate-text span contract. The only source is the exact
|
||||
//! `modules[].source` carried by the Analysis v3 artifact; all spans are
|
||||
//! half-open UTF-8 byte ranges into it.
|
||||
|
||||
use marqdo_view::config::ViewConfig;
|
||||
use marqdo_view::protocol::analysis_v2 as semantic;
|
||||
use marqdo_view::reader::assemble::{self as assemble, BuildInput};
|
||||
use marqdo_view::protocol::analysis_v3;
|
||||
use marqdo_view::reader::assemble::{build, BuildInput};
|
||||
use marqdo_view::reader::markdown::{DocumentIdentity, DocumentModel};
|
||||
use marqdo_view::reader::model::{Navigation, SourceStatus};
|
||||
use marqdo_view::reader::model::{anchor_for_span, Navigation, SourceStatus};
|
||||
use marqdo_view::reader::render::{ReaderRenderer, RenderContext};
|
||||
use marqdo_view::reader::source::sha256_hex;
|
||||
use marqdo_view::reader::source::{Project, SafeRelativePath};
|
||||
@@ -14,34 +15,33 @@ fn repo_root() -> std::path::PathBuf {
|
||||
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
}
|
||||
|
||||
fn load(path: &str, golden: &str) -> (String, Vec<u8>, semantic::VerifiedAnalysis) {
|
||||
let root = repo_root();
|
||||
let source = std::fs::read_to_string(root.join(path)).unwrap();
|
||||
let index = marqdo_view::reader::source::SourceIndex::new(&source);
|
||||
let analysis = std::fs::read(root.join(golden)).unwrap();
|
||||
let relative = std::path::Path::new(path)
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
let verified = semantic::verify_analysis(&analysis, &source, &index, &relative).unwrap();
|
||||
(source, analysis, verified)
|
||||
fn load(golden: &str, relative: &str) -> analysis_v3::VerifiedAnalysis {
|
||||
let bytes = std::fs::read(repo_root().join(golden)).unwrap();
|
||||
let verified = analysis_v3::verify_analysis(&bytes, relative, None)
|
||||
.unwrap_or_else(|err| panic!("{golden} does not verify: {err}"));
|
||||
let source = &verified.projection.source;
|
||||
assert_eq!(
|
||||
verified.projection.source_digest,
|
||||
sha256_hex(source.as_bytes())
|
||||
);
|
||||
verified
|
||||
}
|
||||
|
||||
fn render(verified: &semantic::VerifiedAnalysis, source: &str, relative: &str) -> String {
|
||||
let root = repo_root().join("examples/unicode-spans");
|
||||
fn render(verified: &analysis_v3::VerifiedAnalysis, project_dir: &str, relative: &str) -> String {
|
||||
let root = repo_root().join(project_dir);
|
||||
let project = Project::open(&root).unwrap();
|
||||
let entry = SafeRelativePath::parse(relative).unwrap();
|
||||
let document = DocumentModel::parse(source.to_string(), DocumentIdentity::placeholder(&entry));
|
||||
let source = verified.projection.source.clone();
|
||||
let document = DocumentModel::parse(source, DocumentIdentity::placeholder(&entry));
|
||||
let navigation = Navigation::from_project(&project, &ViewConfig::default(), &entry);
|
||||
let model = assemble::build(BuildInput {
|
||||
let model = build(BuildInput {
|
||||
document,
|
||||
analysis: Some(verified),
|
||||
status: SourceStatus::StaticOk,
|
||||
status_note: None,
|
||||
diagnostics: Vec::new(),
|
||||
navigation,
|
||||
core_version: Some("3.0.0-beta.1".to_string()),
|
||||
core_version: Some("3.0.0-rc.4".to_string()),
|
||||
run: None,
|
||||
});
|
||||
ReaderRenderer::new()
|
||||
@@ -51,35 +51,25 @@ fn render(verified: &semantic::VerifiedAnalysis, source: &str, relative: &str) -
|
||||
|
||||
#[test]
|
||||
fn unicode_spans_are_exact_and_duplicate_returns_get_distinct_anchors() {
|
||||
let (source, analysis, verified) = load(
|
||||
"examples/unicode-spans/index.mq.md",
|
||||
"tests/golden/analysis-v2/unicode-spans.json",
|
||||
);
|
||||
assert_eq!(
|
||||
verified.projection.source_digest,
|
||||
sha256_hex(source.as_bytes())
|
||||
);
|
||||
let verified = load("tests/golden/analysis-v3/unicode-spans.json", "index.mq.md");
|
||||
let source = verified.projection.source.clone();
|
||||
let function = &verified.projection.functions[0];
|
||||
let returns: Vec<_> = function
|
||||
.flattened_statements()
|
||||
.into_iter()
|
||||
.filter(|statement| statement.kind == semantic::StatementKind::Return)
|
||||
.filter(|statement| statement.kind == analysis_v3::StatementKind::Return)
|
||||
.collect();
|
||||
assert_eq!(returns.len(), 2);
|
||||
let first = returns[0].span;
|
||||
let second = returns[1].span;
|
||||
assert_ne!(first, second);
|
||||
let index = marqdo_view::reader::source::SourceIndex::new(&source);
|
||||
// Core statement spans cover the whole carrier node (including markers).
|
||||
assert_eq!(index.source_text(&source, first), Some("`return 1`"));
|
||||
assert_eq!(index.source_text(&source, second), Some("`return 1`"));
|
||||
// The carrier text is identical, so only spans can distinguish the two.
|
||||
assert_eq!(first.start, source.find("`return 1`").unwrap() as u64);
|
||||
assert_ne!(first.start, second.start);
|
||||
|
||||
let html = render(&verified, &source, "index.mq.md");
|
||||
let statement_ids: Vec<&str> = html.matches("data-source-id=\"").map(|_| "").collect();
|
||||
let _ = statement_ids;
|
||||
let html = render(&verified, "examples/unicode-spans", "index.mq.md");
|
||||
// 3 anchored statements: branch header + two returns.
|
||||
assert_eq!(html.matches("class=\"mq-anchor\"").count(), 3, "{html}");
|
||||
let ids: Vec<&str> = html
|
||||
@@ -91,30 +81,28 @@ fn unicode_spans_are_exact_and_duplicate_returns_get_distinct_anchors() {
|
||||
unique.sort();
|
||||
unique.dedup();
|
||||
assert_eq!(ids.len(), unique.len(), "duplicate source ids: {ids:?}");
|
||||
// Emoji and CJK survive both rendering and source mapping.
|
||||
assert!(html.contains("中文 🙂 标题"));
|
||||
assert!(html.contains("甲🙂"));
|
||||
let _ = analysis;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn crlf_source_keeps_exact_digest_and_line_mapping() {
|
||||
let (source, _analysis, verified) = load(
|
||||
"examples/unicode-spans/crlf.mq.md",
|
||||
"tests/golden/analysis-v2/unicode-spans-crlf.json",
|
||||
let verified = load(
|
||||
"tests/golden/analysis-v3/unicode-spans-crlf.json",
|
||||
"crlf.mq.md",
|
||||
);
|
||||
let source = verified.projection.source.clone();
|
||||
assert!(source.contains("\r\n"), "fixture must stay CRLF");
|
||||
assert_eq!(
|
||||
verified.projection.source_digest,
|
||||
sha256_hex(source.as_bytes())
|
||||
);
|
||||
let index = marqdo_view::reader::source::SourceIndex::new(&source);
|
||||
let function = &verified.projection.functions[0];
|
||||
for (_, statement) in verified
|
||||
.projection
|
||||
.all_statements()
|
||||
.into_iter()
|
||||
.filter(|(_, statement)| statement.kind == semantic::StatementKind::Return)
|
||||
.filter(|(_, statement)| statement.kind == analysis_v3::StatementKind::Return)
|
||||
{
|
||||
assert!(
|
||||
index
|
||||
@@ -125,42 +113,20 @@ fn crlf_source_keeps_exact_digest_and_line_mapping() {
|
||||
statement.span
|
||||
);
|
||||
}
|
||||
let html = render(&verified, &source, "crlf.mq.md");
|
||||
let html = render(&verified, "examples/unicode-spans", "crlf.mq.md");
|
||||
assert!(html.contains("CRLF 文档"));
|
||||
// One list item per source line (SourceIndex counts the trailing line too).
|
||||
let source_lines = index.line_count();
|
||||
assert_eq!(html.matches("class=\"mq-line-text\"").count(), source_lines);
|
||||
let _ = function;
|
||||
assert_eq!(
|
||||
html.matches("class=\"mq-line-text\"").count(),
|
||||
index.line_count()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn knowledge_emphasis_never_becomes_an_execution_anchor() {
|
||||
let root = repo_root();
|
||||
let project = Project::open(&root.join("examples/mixed-modes")).unwrap();
|
||||
let project = Project::open(&repo_root().join("examples/mixed-modes")).unwrap();
|
||||
let entry = project.entry().relative.clone();
|
||||
let source = project.read_source_text(&entry).unwrap();
|
||||
let index = marqdo_view::reader::source::SourceIndex::new(&source);
|
||||
let analysis = std::fs::read(root.join("tests/golden/analysis-v2/mixed-modes.json")).unwrap();
|
||||
let verified = semantic::verify_analysis(&analysis, &source, &index, entry.as_str()).unwrap();
|
||||
assert_eq!(
|
||||
verified.projection.source_digest,
|
||||
sha256_hex(source.as_bytes())
|
||||
);
|
||||
let document = DocumentModel::parse(source.clone(), DocumentIdentity::placeholder(&entry));
|
||||
let navigation = Navigation::from_project(&project, &ViewConfig::default(), &entry);
|
||||
let model = assemble::build(BuildInput {
|
||||
document,
|
||||
analysis: Some(&verified),
|
||||
status: SourceStatus::StaticOk,
|
||||
status_note: None,
|
||||
diagnostics: Vec::new(),
|
||||
navigation,
|
||||
core_version: Some("3.0.0-beta.1".to_string()),
|
||||
run: None,
|
||||
});
|
||||
let html = ReaderRenderer::new()
|
||||
.render(&model, &RenderContext::live())
|
||||
.html;
|
||||
let verified = load("tests/golden/analysis-v3/mixed-modes.json", entry.as_str());
|
||||
let html = render(&verified, "examples/mixed-modes", entry.as_str());
|
||||
// Exactly the four Exec statements are anchored (branch + assign + 2 returns).
|
||||
assert_eq!(html.matches("class=\"mq-anchor\"").count(), 4, "{html}");
|
||||
let knowledge_bold = "此处加粗是知识内容,不执行。";
|
||||
@@ -173,3 +139,50 @@ fn knowledge_emphasis_never_becomes_an_execution_anchor() {
|
||||
"knowledge emphasis received an execution anchor"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn heading_ui_uses_heading_span_and_section_span_includes_children() {
|
||||
// ADR-0069: heading_span is the heading AST node; the complete span runs
|
||||
// through child sections. A section heading id must be derived from its own
|
||||
// heading span, so adding a child never moves it.
|
||||
let verified = load("tests/golden/analysis-v3/control-flow.json", "index.mq.md");
|
||||
let projection = &verified.projection;
|
||||
let mut sections_with_children = 0;
|
||||
for unit in &projection.units {
|
||||
let Some(heading) = unit.heading_span else {
|
||||
assert_eq!(unit.kind, analysis_v3::UnitKind::Root);
|
||||
continue;
|
||||
};
|
||||
assert!(unit.span.contains(heading));
|
||||
let anchor = anchor_for_span(heading.start, heading.end);
|
||||
// A heading id is derived from the heading itself, not the section body.
|
||||
assert_ne!(
|
||||
anchor,
|
||||
anchor_for_span(unit.span.start, unit.span.end),
|
||||
"unit {:?} used the complete section span as its heading id",
|
||||
unit.id
|
||||
);
|
||||
if projection
|
||||
.units
|
||||
.iter()
|
||||
.any(|child| child.parent_id.as_deref() == Some(unit.id.as_str()))
|
||||
{
|
||||
sections_with_children += 1;
|
||||
assert!(
|
||||
unit.span.end > heading.end,
|
||||
"section {:?} complete span does not include its children",
|
||||
unit.id
|
||||
);
|
||||
}
|
||||
}
|
||||
assert!(
|
||||
sections_with_children > 0,
|
||||
"fixture needs a section with children"
|
||||
);
|
||||
let html = render(&verified, "examples/control-flow", "index.mq.md");
|
||||
let function_anchor = anchor_for_span(
|
||||
projection.functions[0].heading_span.start,
|
||||
projection.functions[0].heading_span.end,
|
||||
);
|
||||
assert!(html.contains(&format!("id=\"{function_anchor}\"")));
|
||||
}
|
||||
Reference in new issue
Block a user