Skip to main content

kcode_k1_daemon_lib_testkit/
lib.rs

1#![doc = include_str!("../Documentation.md")]
2#![forbid(unsafe_code)]
3
4const REQUIRED_LINES: &[&str] = &[
5    "kcode-k1-access = \"0.6.2\"",
6    "kcode-k1-access-audio-classifier = \"0.1.0\"",
7    "kcode-k1-access-full-audio = \"0.10.0\"",
8    "kcode-k1-access-launch-nodes = \"0.2.2\"",
9    "kcode-k1-access-persons = \"0.3.0\"",
10    "kcode-k1-access-profiles = \"0.6.2\"",
11    "kcode-k1-audio-classification = \"0.6.0\"",
12    "kcode-k1-audio-classifier = \"0.1.1\"",
13    "kcode-k1-authority-filters = \"0.2.0\"",
14    "kcode-k1-bootstrap-state = \"0.1.0\"",
15    "kcode-k1-chat-service = \"0.11.0\"",
16    "kcode-k1-codex-adapter = \"0.5.3\"",
17    "kcode-k1-codex-websearch = \"0.2.0\"",
18    "kcode-k1-daemon-code-services = \"0.1.9\"",
19    "kcode-k1-daemon-http-boundary = \"0.1.1\"",
20    "kcode-k1-daemon-provider-config = \"0.2.0\"",
21    "kcode-k1-daemon-startup-error = \"0.1.1\"",
22    "kcode-k1-daemon-web-startup = \"0.1.1\"",
23    "kcode-k1-ese = \"0.1.0\"",
24    "kcode-k1-full-audio = \"0.4.0\"",
25    "kcode-k1-http-access-context = \"0.2.0\"",
26    "kcode-k1-http-access-profile-presentation = \"0.1.0\"",
27    "kcode-k1-http-audio = \"0.4.0\"",
28    "kcode-k1-http-chat = \"0.7.3\"",
29    "kcode-k1-http-ese = \"0.1.0\"",
30    "kcode-k1-http-kmap = \"0.1.0\"",
31    "kcode-k1-http-launch-nodes = \"0.3.0\"",
32    "kcode-k1-http-people = \"0.9.0\"",
33    "kcode-k1-http-persons = \"0.3.0\"",
34    "kcode-k1-ktool-set-launch-node = \"0.1.1\"",
35    "kcode-k1-ktool-social = \"0.1.1\"",
36    "kcode-k1-launch-nodes = \"0.4.0\"",
37    "kcode-k1-loom-bootstrap = \"0.1.2\"",
38    "kcode-k1-objects = \"0.2.2\"",
39    "kcode-k1-peering = \"0.3.1\"",
40    "kcode-k1-txn-ordering = \"0.5.1\"",
41    "kcode-speaker-v3-analysis = \"0.3.5\"",
42];
43
44const OBSOLETE_LINES: &[&str] = &[
45    "kcode-k1-chat-service = \"0.10.9\"",
46    "kcode-k1-codex-adapter = \"0.5.1\"",
47    "kcode-k1-daemon-code-services = \"0.1.8\"",
48    "kcode-k1-daemon-provider-config = \"0.1.6\"",
49    "kcode-k1-daemon-startup-error = \"0.1.0\"",
50    "kcode-k1-daemon-web-startup = \"0.1.0\"",
51    "kcode-k1-http-chat = \"0.7.2\"",
52    "kcode-k1-launch-nodes = \"0.3.0\"",
53    "kcode-k1-loom-bootstrap = \"0.1.0\"",
54    "kcode-k1-objects = \"0.2.1\"",
55    "kcode-k1-peering = \"0.3.0\"",
56];
57
58const REQUIRED_SOURCE: &[&str] = &[
59    "AudioClassification::open(",
60    ".shutdown()",
61    "kcode_k1_http_audio::authenticated_routes",
62    "SocialKtools::new(",
63    "SetLaunchNodeKtool::new(",
64    "let web = open(",
65    "web.router()",
66    "web.projection()",
67    "CodeServices::open(",
68    "let rust_projection = code_services.rust_projection();",
69    ".into_parts()",
70    "K1ChatService::open_with_social_and_set_launch_node_and_rust_code_and_web_code(",
71    "K1BootstrapState::open(",
72    "ensure_loom_bootstrap(",
73    "ensure_with_topology(",
74    "BootstrapServices {",
75    "state: &bootstrap_state",
76    "rust_projection: &rust_projection",
77    "with_cause(\"Loom bootstrap\")",
78    "use kcode_k1_ese::K1Ese;",
79    "K1Ese::open(",
80    "&state_root.join(\"ese\")",
81    "Arc::clone(&ordering)",
82    "Arc::clone(&peering)",
83    "Arc::clone(&groups)",
84    "with_cause(\"ESE\")",
85    "let ese_routes = kcode_k1_http_ese::authenticated_routes(",
86    ".merge(ese_routes)",
87    "with_cause(\"daemon files\")",
88    "with_cause(\"Code services\")",
89    "redacted_stage(\"Vault\")",
90    "redacted_stage(\"Gemini API key\")",
91    "redacted_stage(\"Gemini client\")",
92];
93
94const PROHIBITED_SOURCE: &[&str] = &[
95    "audio-classification-v2",
96    "http_audio_artifacts",
97    "retry_fragment",
98    "artifact_route",
99    "open_router(",
100    "RustCodeKtoolService::new(",
101    "K1WebCodeKtoolService::new(",
102    "let chat = K1ChatService::open(",
103    "K1ChatService::open_with_social(",
104    "K1ChatService::open_with_social_and_set_launch_node(",
105    "kcode_k1_loom_bootstrap::ensure(",
106    ".map_err(stage(",
107    "StartupError::Stage(",
108];
109
110pub fn verify_manifest(manifest: &str) {
111    for required in REQUIRED_LINES {
112        assert!(
113            manifest.lines().any(|line| line == *required),
114            "missing required manifest line: {required}"
115        );
116    }
117    for obsolete in OBSOLETE_LINES {
118        assert!(
119            !manifest.lines().any(|line| line == *obsolete),
120            "obsolete manifest line: {obsolete}"
121        );
122    }
123    assert!(
124        !manifest.lines().any(|line| line
125            .trim_start()
126            .starts_with("kcode-k1-http-audio-artifacts")),
127        "artifact manifest selection"
128    );
129    assert!(
130        !manifest.lines().any(|line| line.contains(" = \"=")),
131        "manifest contains an exact internal pin"
132    );
133}
134
135pub fn verify_source(source: &str) {
136    for required in REQUIRED_SOURCE {
137        assert!(
138            source.contains(required),
139            "missing required source evidence: {required}"
140        );
141    }
142    for prohibited in PROHIBITED_SOURCE {
143        assert!(
144            !source.contains(prohibited),
145            "prohibited source token: {prohibited}"
146        );
147    }
148    let topology_entry = source.find("ensure_with_topology(").unwrap();
149    let bootstrap = source.find("ensure_loom_bootstrap(").unwrap();
150    let ese_open = source.find("K1Ese::open(").unwrap();
151    let ese_routes = source
152        .find("let ese_routes = kcode_k1_http_ese::authenticated_routes(")
153        .unwrap();
154    let invites = source
155        .find("kcode_k1_daemon_invite_stock::reconcile(")
156        .expect("invite-stock call is required");
157    let ese_merge = source.find(".merge(ese_routes)").unwrap();
158    let listener = source
159        .find("Boundary::bind_with_public(")
160        .expect("listener binding is required");
161    assert!(
162        topology_entry < bootstrap && bootstrap < invites && invites < listener,
163        "topology-aware Loom bootstrap must precede invite stock and listener binding"
164    );
165    assert!(
166        ese_open < ese_routes && ese_routes < invites && ese_merge < listener,
167        "ESE must open and construct routes before invite stock, then merge before binding"
168    );
169}
170
171#[cfg(test)]
172mod tests {
173    use super::*;
174
175    fn good_manifest() -> String {
176        REQUIRED_LINES.join("\n")
177    }
178
179    fn good_source() -> &'static str {
180        "use kcode_k1_ese::K1Ese;
181AudioClassification::open(
182service.shutdown()
183kcode_k1_http_audio::authenticated_routes
184SocialKtools::new(
185SetLaunchNodeKtool::new(
186let web = open(
187web.router()
188web.projection()
189CodeServices::open(
190let rust_projection = code_services.rust_projection();
191code_services.into_parts()
192K1ChatService::open_with_social_and_set_launch_node_and_rust_code_and_web_code(
193K1BootstrapState::open(
194ensure_with_topology(
195BootstrapServices {
196state: &bootstrap_state
197rust_projection: &rust_projection
198with_cause(\"Loom bootstrap\")
199ensure_loom_bootstrap(
200K1Ese::open(
201&state_root.join(\"ese\")
202Arc::clone(&ordering)
203Arc::clone(&peering)
204Arc::clone(&groups)
205with_cause(\"ESE\")
206let ese_routes = kcode_k1_http_ese::authenticated_routes(
207kcode_k1_daemon_invite_stock::reconcile(
208authenticated.merge(ese_routes)
209Boundary::bind_with_public(
210with_cause(\"daemon files\")
211with_cause(\"Code services\")
212redacted_stage(\"Vault\")
213redacted_stage(\"Gemini API key\")
214redacted_stage(\"Gemini client\")"
215    }
216
217    #[test]
218    fn accepts_current_inputs() {
219        verify_manifest(&good_manifest());
220        verify_source(good_source());
221    }
222
223    #[test]
224    fn rejects_missing_current_selections() {
225        for line in [
226            "kcode-k1-bootstrap-state = \"0.1.0\"",
227            "kcode-k1-chat-service = \"0.11.0\"",
228            "kcode-k1-daemon-code-services = \"0.1.9\"",
229            "kcode-k1-ese = \"0.1.0\"",
230            "kcode-k1-http-ese = \"0.1.0\"",
231            "kcode-k1-loom-bootstrap = \"0.1.2\"",
232            "kcode-k1-objects = \"0.2.2\"",
233        ] {
234            assert!(
235                std::panic::catch_unwind(|| verify_manifest(&good_manifest().replace(line, "")))
236                    .is_err()
237            );
238        }
239    }
240
241    #[test]
242    fn rejects_source_regressions() {
243        for evidence in REQUIRED_SOURCE {
244            assert!(
245                std::panic::catch_unwind(|| verify_source(&good_source().replace(evidence, "")))
246                    .is_err()
247            );
248        }
249        for token in PROHIBITED_SOURCE {
250            assert!(
251                std::panic::catch_unwind(|| verify_source(&format!("{} {token}", good_source())))
252                    .is_err()
253            );
254        }
255        let late_ese = good_source().replace("K1Ese::open(", "ESE_OPEN(").replace(
256            "Boundary::bind_with_public(",
257            "K1Ese::open(\nBoundary::bind_with_public(",
258        );
259        assert!(std::panic::catch_unwind(|| verify_source(&late_ese)).is_err());
260    }
261
262    #[test]
263    fn rejects_obsolete_artifact_and_exact_pin_inputs() {
264        for line in OBSOLETE_LINES {
265            assert!(
266                std::panic::catch_unwind(|| {
267                    verify_manifest(&format!("{}\n{line}", good_manifest()))
268                })
269                .is_err()
270            );
271        }
272        assert!(
273            std::panic::catch_unwind(|| verify_manifest(&format!(
274                "{}\nkcode-k1-http-audio-artifacts = \"0.4.0\"",
275                good_manifest()
276            )))
277            .is_err()
278        );
279        assert!(
280            std::panic::catch_unwind(|| verify_manifest(&format!(
281                "{}\nkcode-k1-peering = \"=0.3.1\"",
282                good_manifest()
283            )))
284            .is_err()
285        );
286    }
287
288    #[test]
289    fn complete_package_is_bounded() {
290        let files = [
291            include_str!("../Cargo.toml"),
292            include_str!("../Documentation.md"),
293            include_str!("lib.rs"),
294        ];
295        assert!(
296            files
297                .iter()
298                .flat_map(|file| file.lines())
299                .filter(|line| !line.trim().is_empty())
300                .count()
301                < 500
302        );
303    }
304}