kcode-k1-daemon-lib-testkit 0.5.4

Concrete daemon Audio, Social, code-service, Loom bootstrap, provider, startup-error, and ESE 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.9\"",
    "kcode-k1-daemon-http-boundary = \"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-http-access-context = \"0.2.0\"",
    "kcode-k1-http-access-profile-presentation = \"0.1.0\"",
    "kcode-k1-http-audio = \"0.4.0\"",
    "kcode-k1-http-chat = \"0.7.3\"",
    "kcode-k1-http-ese = \"0.1.0\"",
    "kcode-k1-http-kmap = \"0.1.0\"",
    "kcode-k1-http-launch-nodes = \"0.3.0\"",
    "kcode-k1-http-people = \"0.9.0\"",
    "kcode-k1-http-persons = \"0.3.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.2\"",
    "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-provider-config = \"0.1.6\"",
    "kcode-k1-daemon-startup-error = \"0.1.0\"",
    "kcode-k1-daemon-web-startup = \"0.1.0\"",
    "kcode-k1-http-chat = \"0.7.2\"",
    "kcode-k1-launch-nodes = \"0.3.0\"",
    "kcode-k1-loom-bootstrap = \"0.1.0\"",
    "kcode-k1-objects = \"0.2.1\"",
    "kcode-k1-peering = \"0.3.0\"",
];

const REQUIRED_SOURCE: &[&str] = &[
    "AudioClassification::open(",
    ".shutdown()",
    "kcode_k1_http_audio::authenticated_routes",
    "SocialKtools::new(",
    "SetLaunchNodeKtool::new(",
    "let web = open(",
    "web.router()",
    "web.projection()",
    "CodeServices::open(",
    "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_loom_bootstrap(",
    "ensure_with_topology(",
    "BootstrapServices {",
    "state: &bootstrap_state",
    "rust_projection: &rust_projection",
    "with_cause(\"Loom bootstrap\")",
    "use kcode_k1_ese::K1Ese;",
    "K1Ese::open(",
    "&state_root.join(\"ese\")",
    "Arc::clone(&ordering)",
    "Arc::clone(&peering)",
    "Arc::clone(&groups)",
    "with_cause(\"ESE\")",
    "let ese_routes = kcode_k1_http_ese::authenticated_routes(",
    ".merge(ese_routes)",
    "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(",
];

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-audio-artifacts")),
        "artifact manifest selection"
    );
    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("ensure_loom_bootstrap(").unwrap();
    let ese_open = source.find("K1Ese::open(").unwrap();
    let ese_routes = source
        .find("let ese_routes = kcode_k1_http_ese::authenticated_routes(")
        .unwrap();
    let invites = source
        .find("kcode_k1_daemon_invite_stock::reconcile(")
        .expect("invite-stock call is required");
    let ese_merge = source.find(".merge(ese_routes)").unwrap();
    let listener = source
        .find("Boundary::bind_with_public(")
        .expect("listener binding is required");
    assert!(
        topology_entry < bootstrap && bootstrap < invites && invites < listener,
        "topology-aware Loom bootstrap must precede invite stock and listener binding"
    );
    assert!(
        ese_open < ese_routes && ese_routes < invites && ese_merge < listener,
        "ESE must open and construct routes before invite stock, then merge before binding"
    );
}

#[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()
kcode_k1_http_audio::authenticated_routes
SocialKtools::new(
SetLaunchNodeKtool::new(
let web = open(
web.router()
web.projection()
CodeServices::open(
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(
BootstrapServices {
state: &bootstrap_state
rust_projection: &rust_projection
with_cause(\"Loom bootstrap\")
ensure_loom_bootstrap(
K1Ese::open(
&state_root.join(\"ese\")
Arc::clone(&ordering)
Arc::clone(&peering)
Arc::clone(&groups)
with_cause(\"ESE\")
let ese_routes = kcode_k1_http_ese::authenticated_routes(
kcode_k1_daemon_invite_stock::reconcile(
authenticated.merge(ese_routes)
Boundary::bind_with_public(
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.9\"",
            "kcode-k1-ese = \"0.1.0\"",
            "kcode-k1-http-ese = \"0.1.0\"",
            "kcode-k1-loom-bootstrap = \"0.1.2\"",
            "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()
            );
        }
        let late_ese = good_source().replace("K1Ese::open(", "ESE_OPEN(").replace(
            "Boundary::bind_with_public(",
            "K1Ese::open(\nBoundary::bind_with_public(",
        );
        assert!(std::panic::catch_unwind(|| verify_source(&late_ese)).is_err());
    }

    #[test]
    fn rejects_obsolete_artifact_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-audio-artifacts = \"0.4.0\"",
                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
        );
    }
}