use crate::graph::Graph;
use crate::projection::flows::{collect_flows, model_namespace};
use crate::projection::ids::{sanitize_filename, NameRegistrar};
use crate::projection::sink::ArtifactSink;
use std::collections::BTreeMap;
pub fn emit(
graph: &Graph,
model_ref: &str,
created_at: Option<String>,
sink: &mut ArtifactSink,
) -> Result<Vec<String>, String> {
let created_at = created_at.unwrap_or_else(|| chrono::Utc::now().to_rfc3339());
let ns = model_namespace(graph)?;
let file = format!("{}.spec", sanitize_filename(&ns));
let body = build_spec(graph, &ns, model_ref, &created_at)?;
sink.write(&file, &body)?;
Ok(vec![file])
}
pub fn project_gauge_in_memory(
graph: &Graph,
model_ref: &str,
created_at: Option<String>,
) -> Result<BTreeMap<String, String>, String> {
let mut map = BTreeMap::new();
let mut sink = ArtifactSink::Memory {
prefix: String::new(),
map: &mut map,
};
emit(graph, model_ref, created_at, &mut sink)?;
Ok(map)
}
fn build_spec(
graph: &Graph,
ns: &str,
model_ref: &str,
created_at: &str,
) -> Result<String, String> {
let flows = collect_flows(graph)?;
let mut s = String::new();
s.push_str(&format!(
"# {ns} flows\n\n\
// Gauge spec projected by DomainForge from {model_ref} at {created_at}.\n\
// One scenario per SEA Flow. Wire step implementations to these step\n\
// texts; parameters are quoted.\n\n"
));
if flows.is_empty() {
s.push_str("// No flows declared — nothing to verify.\n");
}
let mut reg = NameRegistrar::new();
for f in &flows {
let heading = format!(
"Issue {} from {} to {}",
gauge_escape(&f.resource),
gauge_escape(&f.from),
gauge_escape(&f.to)
);
let unique_heading = reg.register("slug", &heading);
let h_resource = gauge_escape(&f.resource);
let h_from = gauge_escape(&f.from);
let h_to = gauge_escape(&f.to);
let h_qty = gauge_escape(&f.quantity);
let display = if unique_heading != slug(&heading) {
let suffix = unique_heading.strip_prefix(&slug(&heading)).unwrap_or("");
format!("{heading}{suffix}")
} else {
heading
};
s.push_str(&format!(
"## {display}\n\n\
* Given a \"{h_from}\" entity exists\n\
* When \"{h_from}\" issues \"{h_resource}\" (quantity \"{h_qty}\") to \"{h_to}\"\n\
* Then \"{h_to}\" should hold \"{h_qty}\" \"{h_resource}\"\n\n"
));
}
Ok(s)
}
fn gauge_escape(s: &str) -> String {
s.chars()
.map(|c| match c {
'"' | '<' | '>' => '_',
_ => c,
})
.collect()
}
fn slug(s: &str) -> String {
crate::projection::ids::slug(s)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::parser::parse_to_graph;
const FIXED_TS: &str = "2026-07-02T00:00:00+00:00";
const SOURCE: &str = r#"
@namespace "procurement"
Entity "Buyer" in procurement
Entity "Supplier" in procurement
Resource "PurchaseOrder" units in procurement
Flow "PurchaseOrder" from "Buyer" to "Supplier" quantity 1
"#;
fn project(source: &str) -> BTreeMap<String, String> {
let graph = parse_to_graph(source).expect("fixture parses");
project_gauge_in_memory(&graph, "test.sea", Some(FIXED_TS.to_string()))
.expect("projection succeeds")
}
#[test]
fn emits_single_namespaced_spec_file() {
let files = project(SOURCE);
assert_eq!(files.keys().collect::<Vec<_>>(), vec!["procurement.spec"]);
}
#[test]
fn spec_has_heading_and_one_scenario_per_flow() {
let files = project(SOURCE);
let body = &files["procurement.spec"];
assert!(body.starts_with("# procurement flows\n"));
assert_eq!(body.matches("## ").count(), 1, "one scenario per flow");
assert!(body.contains("## Issue PurchaseOrder from Buyer to Supplier"));
assert!(body.contains("* Given a \"Buyer\" entity exists"));
assert!(body.contains(
"* When \"Buyer\" issues \"PurchaseOrder\" (quantity \"1\") to \"Supplier\""
));
assert!(body.contains("* Then \"Supplier\" should hold \"1\" \"PurchaseOrder\""));
}
#[test]
fn step_text_uses_no_reserved_angle_brackets() {
let files = project(SOURCE);
let body = &files["procurement.spec"];
for line in body.lines().filter(|l| l.starts_with("* ")) {
assert!(
!line.contains('<') && !line.contains('>'),
"reserved char in step: {line}"
);
}
}
#[test]
fn output_is_deterministic() {
assert_eq!(project(SOURCE), project(SOURCE));
}
#[test]
fn empty_model_emits_no_scenarios() {
let files = project("@namespace \"empty\"\n");
let body = &files["default.spec"];
assert!(body.starts_with("# default flows\n"));
assert_eq!(body.matches("## ").count(), 0);
assert!(body.contains("No flows declared"));
}
#[test]
fn hostile_names_sanitized_in_step_text() {
let hostile = r#"
@namespace "h"
Entity "x<y>" in h
Entity "a\"b" in h
Resource "R" units in h
Flow "R" from "x<y>" to "a\"b" quantity 1
"#;
let files = project(hostile);
let body = &files["h.spec"];
for line in body.lines().filter(|l| l.starts_with("* ")) {
assert!(
!line.contains('<') && !line.contains('>'),
"reserved angle bracket in step (M2): {line}"
);
}
assert!(
!body.contains("a\"b"),
"raw quote must not appear in step text (M2)"
);
}
}