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