kcode-k1-daemon-lib-testkit 0.5.6

Concrete daemon Audio, Social, code-service, Loom bootstrap, provider, startup-error, ESE, and HTTP composition verifier
Documentation
#![doc = include_str!("../Documentation.md")]
#![forbid(unsafe_code)]

const REQUIRED_LINES: &[&str] = &[
    "kcode-k1-access = \"0.6.2\"",
    "kcode-k1-access-audio-classifier = \"0.1.0\"",
    "kcode-k1-access-full-audio = \"0.10.0\"",
    "kcode-k1-access-launch-nodes = \"0.2.2\"",
    "kcode-k1-access-persons = \"0.3.0\"",
    "kcode-k1-access-profiles = \"0.6.2\"",
    "kcode-k1-audio-classification = \"0.6.0\"",
    "kcode-k1-audio-classifier = \"0.1.1\"",
    "kcode-k1-authority-filters = \"0.2.0\"",
    "kcode-k1-bootstrap-state = \"0.1.0\"",
    "kcode-k1-chat-service = \"0.11.0\"",
    "kcode-k1-codex-adapter = \"0.5.3\"",
    "kcode-k1-codex-websearch = \"0.2.0\"",
    "kcode-k1-daemon-code-services = \"0.1.10\"",
    "kcode-k1-daemon-http-boundary = \"0.1.3\"",
    "kcode-k1-daemon-http-composition = \"0.1.1\"",
    "kcode-k1-daemon-provider-config = \"0.2.0\"",
    "kcode-k1-daemon-startup-error = \"0.1.1\"",
    "kcode-k1-daemon-web-startup = \"0.1.1\"",
    "kcode-k1-ese = \"0.1.0\"",
    "kcode-k1-full-audio = \"0.4.0\"",
    "kcode-k1-ktool-set-launch-node = \"0.1.1\"",
    "kcode-k1-ktool-social = \"0.1.1\"",
    "kcode-k1-launch-nodes = \"0.4.0\"",
    "kcode-k1-loom-bootstrap = \"0.1.3\"",
    "kcode-k1-objects = \"0.2.2\"",
    "kcode-k1-peering = \"0.3.1\"",
    "kcode-k1-txn-ordering = \"0.5.1\"",
    "kcode-speaker-v3-analysis = \"0.3.5\"",
];

const OBSOLETE_LINES: &[&str] = &[
    "kcode-k1-chat-service = \"0.10.9\"",
    "kcode-k1-codex-adapter = \"0.5.1\"",
    "kcode-k1-daemon-code-services = \"0.1.8\"",
    "kcode-k1-daemon-code-services = \"0.1.9\"",
    "kcode-k1-daemon-http-boundary = \"0.1.1\"",
    "kcode-k1-daemon-http-boundary = \"0.1.2\"",
    "kcode-k1-daemon-provider-config = \"0.1.6\"",
    "kcode-k1-daemon-startup-error = \"0.1.0\"",
    "kcode-k1-daemon-web-startup = \"0.1.0\"",
    "kcode-k1-launch-nodes = \"0.3.0\"",
    "kcode-k1-loom-bootstrap = \"0.1.0\"",
    "kcode-k1-loom-bootstrap = \"0.1.2\"",
    "kcode-k1-objects = \"0.2.1\"",
    "kcode-k1-peering = \"0.3.0\"",
];

const REQUIRED_SOURCE: &[&str] = &[
    "AudioClassification::open(",
    ".shutdown()",
    "SocialKtools::new(",
    "SetLaunchNodeKtool::new(",
    "let web = open(",
    "web.router()",
    "web.projection()",
    "CodeServices::open(",
    "let web_bootstrap_importer = code_services.web_bootstrap_importer();",
    "let rust_projection = code_services.rust_projection();",
    ".into_parts()",
    "K1ChatService::open_with_social_and_set_launch_node_and_rust_code_and_web_code(",
    "K1BootstrapState::open(",
    "ensure_with_topology(&k1_root, &access, &web_bootstrap_importer, services)",
    "let bootstrap = ensure_loom_bootstrap(BootstrapServices {",
    "state: &bootstrap_state",
    "rust_projection: &rust_projection",
    "with_cause(\"Loom bootstrap\")",
    "let prepared_http = kcode_k1_daemon_http_composition::prepare(",
    "kcode_k1_daemon_http_composition::Inputs {",
    "launch_nodes: bootstrap.launch_nodes(),",
    "ese: Arc::clone(&ese),",
    "let boundary = prepared_http.bind()",
    "use kcode_k1_ese::K1Ese;",
    "K1Ese::open(",
    "&state_root.join(\"ese\")",
    "Arc::clone(&ordering)",
    "Arc::clone(&peering)",
    "Arc::clone(&groups)",
    "with_cause(\"ESE\")",
    "with_cause(\"daemon files\")",
    "with_cause(\"Code services\")",
    "redacted_stage(\"Vault\")",
    "redacted_stage(\"Gemini API key\")",
    "redacted_stage(\"Gemini client\")",
];

const PROHIBITED_SOURCE: &[&str] = &[
    "audio-classification-v2",
    "http_audio_artifacts",
    "retry_fragment",
    "artifact_route",
    "open_router(",
    "RustCodeKtoolService::new(",
    "K1WebCodeKtoolService::new(",
    "let chat = K1ChatService::open(",
    "K1ChatService::open_with_social(",
    "K1ChatService::open_with_social_and_set_launch_node(",
    "kcode_k1_loom_bootstrap::ensure(",
    ".map_err(stage(",
    "StartupError::Stage(",
    "LaunchNodesConfig {",
    "Boundary::bind_with_public(",
    "Boundary::bind_with_public_and_launch_nodes(",
    "kcode_k1_http_ese::authenticated_routes(",
];

pub fn verify_manifest(manifest: &str) {
    for required in REQUIRED_LINES {
        assert!(
            manifest.lines().any(|line| line == *required),
            "missing required manifest line: {required}"
        );
    }
    for obsolete in OBSOLETE_LINES {
        assert!(
            !manifest.lines().any(|line| line == *obsolete),
            "obsolete manifest line: {obsolete}"
        );
    }
    assert!(
        !manifest
            .lines()
            .any(|line| line.trim_start().starts_with("kcode-k1-http-")),
        "HTTP composition dependency leaked back into Daemon Lib"
    );
    assert!(
        !manifest.lines().any(|line| line.contains(" = \"=")),
        "manifest contains an exact internal pin"
    );
}

pub fn verify_source(source: &str) {
    for required in REQUIRED_SOURCE {
        assert!(
            source.contains(required),
            "missing required source evidence: {required}"
        );
    }
    for prohibited in PROHIBITED_SOURCE {
        assert!(
            !source.contains(prohibited),
            "prohibited source token: {prohibited}"
        );
    }
    let topology_entry = source.find("ensure_with_topology(").unwrap();
    let bootstrap = source
        .find("let bootstrap = ensure_loom_bootstrap(BootstrapServices {")
        .unwrap();
    let http = source
        .find("let prepared_http = kcode_k1_daemon_http_composition::prepare(")
        .unwrap();
    let ese_open = source.find("K1Ese::open(").unwrap();
    let invites = source
        .find("kcode_k1_daemon_invite_stock::reconcile(")
        .expect("invite-stock call is required");
    let listener = source
        .find("let boundary = prepared_http.bind()")
        .expect("prepared HTTP listener binding is required");
    assert!(
        topology_entry < bootstrap && bootstrap < http && http < invites && invites < listener,
        "Loom bootstrap and HTTP composition must precede invite stock and listener binding"
    );
    assert!(
        ese_open < http,
        "ESE must open before the concrete HTTP composition consumes it"
    );
}

#[cfg(test)]
mod tests {
    use super::*;

    fn good_manifest() -> String {
        REQUIRED_LINES.join("\n")
    }

    fn good_source() -> &'static str {
        "use kcode_k1_ese::K1Ese;
AudioClassification::open(
service.shutdown()
SocialKtools::new(
SetLaunchNodeKtool::new(
let web = open(
web.router()
web.projection()
CodeServices::open(
let web_bootstrap_importer = code_services.web_bootstrap_importer();
let rust_projection = code_services.rust_projection();
code_services.into_parts()
K1ChatService::open_with_social_and_set_launch_node_and_rust_code_and_web_code(
K1BootstrapState::open(
ensure_with_topology(&k1_root, &access, &web_bootstrap_importer, services)
let bootstrap = ensure_loom_bootstrap(BootstrapServices {
state: &bootstrap_state
rust_projection: &rust_projection
with_cause(\"Loom bootstrap\")
K1Ese::open(
&state_root.join(\"ese\")
Arc::clone(&ordering)
Arc::clone(&peering)
Arc::clone(&groups)
with_cause(\"ESE\")
let prepared_http = kcode_k1_daemon_http_composition::prepare(
kcode_k1_daemon_http_composition::Inputs {
web.router()
launch_nodes: bootstrap.launch_nodes(),
ese: Arc::clone(&ese),
kcode_k1_daemon_invite_stock::reconcile(
let boundary = prepared_http.bind()
with_cause(\"daemon files\")
with_cause(\"Code services\")
redacted_stage(\"Vault\")
redacted_stage(\"Gemini API key\")
redacted_stage(\"Gemini client\")"
    }

    #[test]
    fn accepts_current_inputs() {
        verify_manifest(&good_manifest());
        verify_source(good_source());
    }

    #[test]
    fn rejects_missing_current_selections() {
        for line in [
            "kcode-k1-bootstrap-state = \"0.1.0\"",
            "kcode-k1-chat-service = \"0.11.0\"",
            "kcode-k1-daemon-code-services = \"0.1.10\"",
            "kcode-k1-daemon-http-boundary = \"0.1.3\"",
            "kcode-k1-daemon-http-composition = \"0.1.1\"",
            "kcode-k1-ese = \"0.1.0\"",
            "kcode-k1-loom-bootstrap = \"0.1.3\"",
            "kcode-k1-objects = \"0.2.2\"",
        ] {
            assert!(
                std::panic::catch_unwind(|| verify_manifest(&good_manifest().replace(line, "")))
                    .is_err()
            );
        }
    }

    #[test]
    fn rejects_source_regressions() {
        for evidence in REQUIRED_SOURCE {
            assert!(
                std::panic::catch_unwind(|| verify_source(&good_source().replace(evidence, "")))
                    .is_err()
            );
        }
        for token in PROHIBITED_SOURCE {
            assert!(
                std::panic::catch_unwind(|| verify_source(&format!("{} {token}", good_source())))
                    .is_err()
            );
        }
    }

    #[test]
    fn rejects_obsolete_http_and_exact_pin_inputs() {
        for line in OBSOLETE_LINES {
            assert!(
                std::panic::catch_unwind(|| {
                    verify_manifest(&format!("{}\n{line}", good_manifest()))
                })
                .is_err()
            );
        }
        assert!(
            std::panic::catch_unwind(|| verify_manifest(&format!(
                "{}\nkcode-k1-http-chat = \"0.7.3\"",
                good_manifest()
            )))
            .is_err()
        );
        assert!(
            std::panic::catch_unwind(|| verify_manifest(&format!(
                "{}\nkcode-k1-peering = \"=0.3.1\"",
                good_manifest()
            )))
            .is_err()
        );
    }

    #[test]
    fn complete_package_is_bounded() {
        let files = [
            include_str!("../Cargo.toml"),
            include_str!("../Documentation.md"),
            include_str!("lib.rs"),
        ];
        assert!(
            files
                .iter()
                .flat_map(|file| file.lines())
                .filter(|line| !line.trim().is_empty())
                .count()
                < 500
        );
    }
}