lenso-cli 0.4.11

Authoring CLI for Lenso Plugins and App intent.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use super::scaffold::{
    bun_plugin_scaffold, create, multi_plugin_scaffold, plugin_scaffold, process_plugin_scaffold,
    web_plugin_scaffold,
};
use super::*;

#[test]
fn bun_descriptor_lowers_named_dependencies_into_the_plugin_contract() {
    let descriptor = parse_descriptor_bytes(
        br#"{
            "abi":"lenso.json-request@1",
            "configuration_schema":{"type":"object","required":["prefix"]},
            "capabilities":[{
                "capability_id":"company.notes@1",
                "descriptor_version":"1.0.0",
                "request_operations":["list"]
            }],
            "required_capabilities":[{
                "requirement_id":"store",
                "capability_id":"company.notes-store@1",
                "descriptor_version":"1.0.0",
                "cardinality":"one"
            }]
        }"#,
    )
    .unwrap();
    let package = BunPackage {
        version: "1.0.0".to_owned(),
        metadata: BunPackageMetadata {
            plugin_id: "company.notes".to_owned(),
            root_slot: "notes".to_owned(),
            runtime: "bun".to_owned(),
        },
    };

    let contract = contract_from_bun_descriptor(&package, &descriptor).unwrap();
    let requirement = &contract.required_capabilities()[0];

    assert_eq!(
        contract.configuration_schema(),
        Some(&serde_json::json!({"type":"object","required":["prefix"]}))
    );
    assert_eq!(requirement.requirement_id(), "store");
    assert_eq!(requirement.capability_id(), "company.notes-store@1");
}

#[test]
fn bun_descriptor_accepts_a_providerless_lifecycle_plugin() {
    let descriptor = parse_descriptor_bytes(
        br#"{
            "abi":"lenso.json-request@1",
            "capabilities":[],
            "required_capabilities":[{
                "requirement_id":"store",
                "capability_id":"company.notes-store@1",
                "descriptor_version":"1.0.0",
                "cardinality":"one"
            }]
        }"#,
    )
    .unwrap();

    assert!(descriptor.capabilities.is_empty());
    assert_eq!(descriptor.required_capabilities[0].requirement_id, "store");
}

#[test]
fn bun_descriptor_rejects_duplicate_providers() {
    let error = parse_descriptor_bytes(
        br#"{
            "abi":"lenso.json-request@1",
            "capabilities":[
                {"capability_id":"company.notes@1","descriptor_version":"1.0.0","request_operations":["read"]},
                {"capability_id":"company.notes@1","descriptor_version":"1.0.0","request_operations":["write"]}
            ]
        }"#,
    )
    .unwrap_err();

    assert!(error.to_string().contains("repeats provided Capability"));
}

#[test]
fn web_plugin_scaffold_uses_canonical_endpoint_authoring() {
    let files = web_plugin_scaffold("company.greetings-http");
    let manifest = files.get(Path::new("Cargo.toml")).unwrap();
    let source = files.get(Path::new("src/lib.rs")).unwrap();
    let readme = files.get(Path::new("README.md")).unwrap();

    assert!(manifest.contains("plugin-id = \"company.greetings-http\""));
    assert!(manifest.contains("root-slot = \"web\""));
    assert!(manifest.contains("lenso-capability-http-endpoint"));
    assert!(manifest.contains("version = \"0.2.8\""));
    assert!(source.contains("#[lenso::plugin]"));
    assert!(source.contains("#[endpoint]"));
    assert!(source.contains("#[query("));
    assert!(source.contains("Result<(StatusCode, Json<Greeting>), Problem>"));
    assert!(source.contains("EndpointTest"));
    assert!(source.contains("pub const fn link()"));
    assert!(!source.contains("NativeModuleFactory"));
    assert!(!readme.contains("lenso plugin pack"));
    assert!(readme.contains("lenso plugin dev"));
}

#[test]
fn web_plugin_new_writes_the_complete_project() {
    let root = tempfile::tempdir().unwrap();
    create(PluginNewArgs {
        plugin_id: "company.greetings-http".to_owned(),
        repo_root: Some(root.path().to_path_buf()),
        dir: None,
        runtime: PluginRuntimeArg::Multi,
        web: true,
        no_install: true,
        dry_run: false,
    })
    .unwrap();

    let project = root.path().join("company.greetings-http");
    for path in ["Cargo.toml", "src/lib.rs", "README.md"] {
        assert!(project.join(path).is_file(), "missing generated {path}");
    }
}

#[test]
#[ignore = "clean-room test downloads pinned Web dependencies and runs generated tests"]
fn clean_room_web_plugin_runs_generated_tests() {
    let root = tempfile::tempdir().unwrap();
    create(PluginNewArgs {
        plugin_id: "company.greetings-http".to_owned(),
        repo_root: Some(root.path().to_path_buf()),
        dir: None,
        runtime: PluginRuntimeArg::Multi,
        web: true,
        no_install: false,
        dry_run: false,
    })
    .unwrap();
}

#[test]
#[ignore = "clean-room test downloads pinned Web dependencies and builds the generated dev Host"]
fn clean_room_web_plugin_builds_generated_dev_host() {
    let root = tempfile::tempdir().unwrap();
    create(PluginNewArgs {
        plugin_id: "company.greetings-http".to_owned(),
        repo_root: Some(root.path().to_path_buf()),
        dir: None,
        runtime: PluginRuntimeArg::Multi,
        web: true,
        no_install: false,
        dry_run: false,
    })
    .unwrap();
    let project = root.path().join("company.greetings-http");
    let package = read_package(&project.join("Cargo.toml")).unwrap();
    let host = super::web_dev::DevHost::prepare(&project, &package).unwrap();
    host.build().unwrap();
}

#[test]
fn rust_plugin_scaffold_exposes_only_portable_authoring() {
    let files = plugin_scaffold("uppercase");
    let author_source = files.get(Path::new("src/lib.rs")).unwrap();
    let all = files.values().cloned().collect::<String>();

    assert!(author_source.contains("#[lenso::plugin]"));
    assert!(author_source.contains("#[lenso_agent_tool_sdk::tool_provider]"));
    assert!(author_source.contains("#[tool("));
    assert!(author_source.contains("fn execute(arguments: Arguments)"));
    assert!(all.contains("plugin-id = \"uppercase\""));
    assert!(all.contains("root-slot = \"tool-providers\""));
    assert!(all.contains("lenso plugin new"));
    assert!(all.contains("lenso plugin dev"));
    assert!(all.contains("lenso plugin check"));
    assert!(all.contains("lenso plugin pack"));
    for internal in [
        "wit_bindgen",
        "guest_request_plugin",
        "ProcessPlugin",
        "ProcessOutcome",
        "request_json",
        "arguments_json",
        "lenso.agent.tool-provider",
        "lenso.generated",
    ] {
        assert!(
            !author_source.contains(internal),
            "author source leaked `{internal}`"
        );
    }
    for removed in [
        "src/plugin.rs",
        "src/lenso.generated.rs",
        "src/lenso.wasm.generated.rs",
        "src/lenso.process.generated.rs",
        "lenso.generated.descriptor.json",
        "wit/world.wit",
    ] {
        assert!(
            !files.contains_key(Path::new(removed)),
            "unexpected `{removed}`"
        );
    }
}

#[test]
fn bun_plugin_scaffold_uses_generated_runtime_lowering() {
    let files = bun_plugin_scaffold("example.echo");
    let package = files.get(Path::new("package.json")).unwrap();
    let author = files.get(Path::new("src/plugin.ts")).unwrap();
    let runtime = files.get(Path::new("src/lenso.bun.generated.ts")).unwrap();

    assert!(package.contains("\"runtime\": \"bun\""));
    assert!(package.contains("\"@lenso/bun\": \"0.3.0\""));
    assert!(author.contains("bindToolProviderProvider"));
    assert!(author.contains("definePlugin"));
    assert!(!author.contains("serve("));
    assert!(runtime.contains("serve(plugin)"));
}

#[test]
fn process_plugin_scaffold_uses_the_sdk_owned_lowering() {
    let files = process_plugin_scaffold("uppercase");
    let manifest = files.get(Path::new("Cargo.toml")).unwrap();
    let entrypoint = files.get(Path::new("src/main.rs")).unwrap();

    assert!(manifest.contains("runtime = \"process\""));
    assert!(manifest.contains("package = \"lenso-plugin-sdk\""));
    assert!(manifest.contains("lenso-agent-tool-sdk"));
    assert_eq!(
        entrypoint,
        "// Cargo Process entrypoint; the SDK supplies main and protocol lowering.\ninclude!(\"lib.rs\");\n"
    );
    assert!(!files.contains_key(Path::new("lenso.generated.descriptor.json")));
}

#[test]
fn multi_scaffold_keeps_one_business_source_for_two_outputs() {
    let files = multi_plugin_scaffold("uppercase");
    let manifest = files.get(Path::new("Cargo.toml")).unwrap();

    assert!(manifest.contains("outputs = [\"wasm\", \"process\"]"));
    assert!(files.contains_key(Path::new("src/lib.rs")));
    assert!(files.contains_key(Path::new("src/main.rs")));
    assert_eq!(
        files
            .keys()
            .filter(|path| path.extension().is_some_and(|extension| extension == "rs"))
            .count(),
        2
    );
    let author_source = files.get(Path::new("src/lib.rs")).unwrap();
    for runtime_detail in ["wit_bindgen", "ProcessPlugin", "ProcessOutcome", "Guest"] {
        assert!(
            !author_source.contains(runtime_detail),
            "author source leaked runtime detail `{runtime_detail}`"
        );
    }
}
#[test]
fn duplicate_plugin_identity_is_rejected() {
    let root = tempfile::tempdir().unwrap();
    let manifest = root.path().join("Cargo.toml");
    fs::write(
        &manifest,
        r#"[package]
name = "duplicate"
version = "0.1.0"
[package.metadata.lenso]
plugin-id = "first"
plugin-id = "second"
"#,
    )
    .unwrap();

    assert!(read_package(&manifest).is_err());
}

#[test]
fn multi_dev_auto_selects_only_the_fast_process_path() {
    assert_eq!(
        resolve_dev_selection(ProjectRuntime::Multi, DevImplementationArg::Auto).unwrap(),
        DevSelection {
            build: DevBuild::Process,
            invoke: ProjectRuntime::Process,
        }
    );
    assert_eq!(
        resolve_dev_selection(ProjectRuntime::Multi, DevImplementationArg::Wasm).unwrap(),
        DevSelection {
            build: DevBuild::Wasm,
            invoke: ProjectRuntime::Wasm,
        }
    );
    assert_eq!(
        resolve_dev_selection(ProjectRuntime::Multi, DevImplementationArg::All).unwrap(),
        DevSelection {
            build: DevBuild::All,
            invoke: ProjectRuntime::Process,
        }
    );
}

#[test]
fn dev_rejects_an_implementation_the_project_does_not_declare() {
    assert!(resolve_dev_selection(ProjectRuntime::Wasm, DevImplementationArg::Process).is_err());
    assert!(resolve_dev_selection(ProjectRuntime::Process, DevImplementationArg::Wasm).is_err());
}

#[test]
fn malformed_plugin_package_is_rejected() {
    let root = tempfile::tempdir().unwrap();
    fs::write(root.path().join("lenso-plugin.json"), b"{}\n").unwrap();

    assert!(verify_bundle_directory(root.path()).is_err());
}

#[tokio::test]
#[ignore = "clean-room test downloads released crates and compiles wasm32"]
async fn clean_room_plugin_runs_new_check_dev_and_pack() {
    let root = tempfile::tempdir().unwrap();
    create(PluginNewArgs {
        plugin_id: "company.uppercase".to_owned(),
        repo_root: Some(root.path().to_path_buf()),
        dir: None,
        runtime: PluginRuntimeArg::Wasm,
        web: false,
        no_install: false,
        dry_run: false,
    })
    .unwrap();
    let project = root.path().join("company.uppercase");
    check(PluginCheckArgs {
        repo_root: Some(project.clone()),
        json: true,
    })
    .unwrap();
    dev::run(PluginDevArgs {
        repo_root: Some(project.clone()),
        operation: Some("execute".to_owned()),
        request_json: r#"{"name":"company.uppercase","arguments_json":"{\"text\":\"hello\"}"}"#
            .to_owned(),
        json: true,
        watch: false,
        implementation: DevImplementationArg::Auto,
    })
    .await
    .unwrap();
    let output = project.join("dist/company.uppercase.lenso-plugin");
    pack(PluginPackArgs {
        repo_root: Some(project.clone()),
        output: Some(output.clone()),
        json: true,
    })
    .unwrap();
    with_bundle_directory(&output, |directory| {
        verify_bundle_directory(directory)
            .map(|_| ())
            .map_err(Into::into)
    })
    .unwrap();
    assert!(
        pack(PluginPackArgs {
            repo_root: Some(project),
            output: Some(output),
            json: false,
        })
        .is_err()
    );
}

#[tokio::test]
#[ignore = "clean-room test downloads git dependencies and compiles both scaffold outputs"]
async fn clean_room_multi_plugin_auto_dev_runs_the_process_build() {
    let root = tempfile::tempdir().unwrap();
    create(PluginNewArgs {
        plugin_id: "company.multi-smoke".to_owned(),
        repo_root: Some(root.path().to_path_buf()),
        dir: None,
        runtime: PluginRuntimeArg::Multi,
        web: false,
        no_install: false,
        dry_run: false,
    })
    .unwrap();
    dev::run(PluginDevArgs {
        repo_root: Some(root.path().join("company.multi-smoke")),
        operation: Some("execute".to_owned()),
        request_json:
            r#"{"name":"company.multi-smoke","arguments_json":"{\"text\":\"auto-process\"}"}"#
                .to_owned(),
        json: true,
        watch: false,
        implementation: DevImplementationArg::Auto,
    })
    .await
    .unwrap();
}

#[tokio::test]
#[ignore = "clean-room test downloads git dependencies and compiles a native executable"]
async fn clean_room_process_plugin_runs_new_check_dev_and_pack() {
    let root = tempfile::tempdir().unwrap();
    create(PluginNewArgs {
        plugin_id: "company.uppercase".to_owned(),
        repo_root: Some(root.path().to_path_buf()),
        dir: None,
        runtime: PluginRuntimeArg::Process,
        web: false,
        no_install: false,
        dry_run: false,
    })
    .unwrap();
    let project = root.path().join("company.uppercase");
    check(PluginCheckArgs {
        repo_root: Some(project.clone()),
        json: true,
    })
    .unwrap();
    dev::run(PluginDevArgs {
        repo_root: Some(project.clone()),
        operation: Some("execute".to_owned()),
        request_json: r#"{"name":"company.uppercase","arguments_json":"{\"text\":\"hello\"}"}"#
            .to_owned(),
        json: true,
        watch: false,
        implementation: DevImplementationArg::Auto,
    })
    .await
    .unwrap();
    let output = project.join("dist/company.uppercase.lenso-plugin");
    pack(PluginPackArgs {
        repo_root: Some(project),
        output: Some(output.clone()),
        json: true,
    })
    .unwrap();
    with_bundle_directory(&output, |directory| {
        verify_bundle_directory(directory)
            .map(|_| ())
            .map_err(Into::into)
    })
    .unwrap();
}