osp-cli 1.5.1

CLI and REPL for querying and managing OSP infrastructure data
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
446
447
448
449
450
451
452
453
454
455
456
457
#[test]
fn rebuild_repl_state_preserves_session_defaults_and_shell_context_unit() {
    let mut state = make_test_state(Vec::new());
    state.session.prompt_prefix = "osp-dev".to_string();
    state.session.history_enabled = false;
    state.session.max_cached_results = 7;
    state
        .session
        .config_overrides
        .set("user.name", "launch-user");
    state
        .session
        .config_overrides
        .set("ui.message.verbosity", "trace");
    state.session.config_overrides.set("debug.level", 2i64);
    state.session.config_overrides.set("ui.format", "json");
    state.session.config_overrides.set("theme.name", "dracula");
    state.session.scope.enter("orch");

    state.session.history_shell = HistoryShellContext::default();
    state.sync_history_shell_context();

    let next = super::super::rebuild_repl_state(&state).expect("rebuild should succeed");

    assert_eq!(
        next.runtime.config.resolved().get_string("user.name"),
        Some("launch-user")
    );
    assert_eq!(next.runtime.ui.message_verbosity, MessageLevel::Trace);
    assert_eq!(next.runtime.ui.debug_verbosity, 2);
    assert_eq!(next.runtime.ui.render_settings.format, OutputFormat::Json);
    assert_eq!(next.runtime.ui.render_settings.theme_name, "dracula");
    assert_eq!(next.session.prompt_prefix, "osp-dev");
    assert!(!next.session.history_enabled);
    assert_eq!(next.session.max_cached_results, 7);
    assert_eq!(next.session.scope.commands(), vec!["orch".to_string()]);
    assert_eq!(
        next.session.history_shell.prefix(),
        Some("orch ".to_string())
    );
}

#[test]
fn rebuild_repl_state_preserves_rich_terminal_render_runtime_unit() {
    let mut state = make_test_state(Vec::new());
    state.session.config_overrides.set("theme.name", "dracula");
    state.runtime.ui.render_settings.mode = crate::core::output::RenderMode::Auto;
    state.runtime.ui.render_settings.color = crate::core::output::ColorMode::Auto;
    state.runtime.ui.render_settings.unicode = crate::core::output::UnicodeMode::Auto;
    state.runtime.ui.render_settings.runtime.stdout_is_tty = true;
    state.runtime.ui.render_settings.runtime.locale_utf8 = Some(true);
    state.runtime.ui.render_settings.runtime.terminal = Some("xterm-256color".to_string());

    let next = super::super::rebuild_repl_state(&state).expect("rebuild should succeed");
    let resolved = next.runtime.ui.render_settings.resolve_render_settings();

    assert_eq!(next.runtime.ui.render_settings.theme_name, "dracula");
    assert!(resolved.color);
    assert!(resolved.unicode);
    assert_eq!(resolved.backend, crate::ui::RenderBackend::Rich);
    assert_eq!(resolved.theme.id, "dracula");
    assert_eq!(resolved.theme.repl_completion_text_spec(), "#000000");
}

#[test]
fn rebuild_repl_state_preserves_session_render_defaults_unit() {
    let mut state = make_test_state(Vec::new());
    state.session.config_overrides.set("ui.format", "table");

    let next = super::super::rebuild_repl_state(&state).expect("rebuild should succeed");

    assert_eq!(next.runtime.ui.render_settings.format, OutputFormat::Table);
}

#[test]
fn rebuild_repl_state_preserves_path_discovery_enabled_by_config_unit() {
    let _guard = env_lock().lock().expect("env lock should not be poisoned");
    let path_root = make_temp_dir("osp-cli-repl-path-discovery");
    let plugins_dir = path_root.join("plugins");
    std::fs::create_dir_all(&plugins_dir).expect("plugin dir should be created");
    let _plugin = write_pipeline_test_plugin(&plugins_dir);

    let original_path = std::env::var_os("PATH");
    let joined_path = std::env::join_paths(
        std::iter::once(plugins_dir.clone())
            .chain(original_path.iter().flat_map(std::env::split_paths)),
    )
    .expect("PATH should join");
    unsafe { std::env::set_var("PATH", joined_path) };

    let mut state = make_test_state(Vec::new());
    state
        .session
        .config_overrides
        .set("extensions.plugins.discovery.path", true);

    let next = super::super::rebuild_repl_state(&state).expect("rebuild should succeed");
    let plugins = next.clients.plugins().list_plugins();
    assert!(plugins.iter().any(|plugin| plugin.plugin_id == "hello"));

    match original_path {
        Some(value) => unsafe { std::env::set_var("PATH", value) },
        None => unsafe { std::env::remove_var("PATH") },
    }
}

#[test]
fn repl_plugin_enable_restart_refreshes_command_catalog_unit() {
    with_temp_config_paths(|| {
        let dir = make_temp_dir("osp-cli-repl-plugin-enable");
        let _plugin = write_pipeline_test_plugin(&dir);
        let mut state = make_test_state(vec![dir.to_path_buf()]);
        state
            .clients
            .plugins()
            .set_command_state("hello", crate::plugin::state::PluginCommandState::Disabled)
            .expect("command should disable");
        assert!(
            state
                .clients
                .plugins()
                .command_catalog()
                .is_empty()
        );

        let history = make_test_history(&mut state);
        let result = repl_dispatch::execute_repl_plugin_line(
            &mut state.runtime,
            &mut state.session,
            &state.clients,
            &history,
            "plugins enable hello",
        )
        .expect("plugin enable should succeed");
        assert!(matches!(
            result,
            crate::repl::ReplLineResult::Restart {
                reload: crate::repl::ReplReloadKind::Default,
                ..
            }
        ));

        let next = super::super::rebuild_repl_state(&state).expect("rebuild should succeed");
        let catalog = next
            .clients
            .plugins()
            .command_catalog();
        assert!(
            catalog.iter().any(|entry| entry.name == "hello"),
            "enabled plugin should appear after rebuild"
        );

    });
}

#[test]
fn repl_provider_selection_restart_invalidates_command_cache_unit() {
    with_temp_config_paths(|| {
        let dir = make_temp_dir("osp-cli-repl-provider-selection-restart");
        let _alpha = write_provider_test_plugin(&dir, "alpha-provider", "hello", "alpha");
        let _beta = write_provider_test_plugin(&dir, "beta-provider", "hello", "beta");
        let mut state = make_test_state(vec![dir.to_path_buf()]);
        let history = make_test_history(&mut state);

        let first = repl_dispatch::execute_repl_plugin_line(
            &mut state.runtime,
            &mut state.session,
            &state.clients,
            &history,
            "--plugin-provider alpha-provider hello --cache",
        )
        .expect("one-shot provider override should succeed");
        match first {
            crate::repl::ReplLineResult::Continue(text) => {
                assert!(text.contains("alpha-from-plugin"))
            }
            other => panic!("unexpected repl result: {other:?}"),
        }
        assert!(!state.session.command_cache.is_empty());

        let selection = repl_dispatch::execute_repl_plugin_line(
            &mut state.runtime,
            &mut state.session,
            &state.clients,
            &history,
            "plugins select-provider hello beta-provider",
        )
        .expect("provider selection should succeed");
        assert!(matches!(
            selection,
            crate::repl::ReplLineResult::Restart {
                reload: crate::repl::ReplReloadKind::Default,
                ..
            }
        ));

        let mut next = super::super::rebuild_repl_state(&state).expect("rebuild should succeed");
        assert!(next.session.command_cache.is_empty());
        assert!(next.session.command_cache_order.is_empty());

        let history = make_test_history(&mut next);
        let second = repl_dispatch::execute_repl_plugin_line(
            &mut next.runtime,
            &mut next.session,
            &next.clients,
            &history,
            "hello --cache",
        )
        .expect("selected provider should execute after rebuild");
        match second {
            crate::repl::ReplLineResult::Continue(text) => {
                assert!(text.contains("beta-from-plugin"));
                assert!(!text.contains("alpha-from-plugin"));
            }
            other => panic!("unexpected repl result: {other:?}"),
        }

    });
}

#[test]
fn repl_reload_intent_matches_command_scope_unit() {
    let mut state = make_test_state(Vec::new());
    state.runtime.themes =
        crate::ui::theme_catalog::load_theme_catalog(state.runtime.config.resolved());
    let history = make_test_history(&mut state);

    let theme_result = repl_dispatch::execute_repl_plugin_line(
        &mut state.runtime,
        &mut state.session,
        &state.clients,
        &history,
        "theme use dracula",
    )
    .expect("theme use should succeed");
    assert!(matches!(
        theme_result,
        crate::repl::ReplLineResult::Restart {
            reload: crate::repl::ReplReloadKind::WithIntro,
            ..
        }
    ));

    let format_result = repl_dispatch::execute_repl_plugin_line(
        &mut state.runtime,
        &mut state.session,
        &state.clients,
        &history,
        "config set ui.format json",
    )
    .expect("config set should succeed");
    assert!(matches!(
        format_result,
        crate::repl::ReplLineResult::Restart {
            reload: crate::repl::ReplReloadKind::Default,
            ..
        }
    ));

    let color_result = repl_dispatch::execute_repl_plugin_line(
        &mut state.runtime,
        &mut state.session,
        &state.clients,
        &history,
        "config set color.prompt.text '#ffffff'",
    )
    .expect("color config set should succeed");
    assert!(matches!(
        color_result,
        crate::repl::ReplLineResult::Restart {
            reload: crate::repl::ReplReloadKind::WithIntro,
            ..
        }
    ));

    let unset_format_result = repl_dispatch::execute_repl_plugin_line(
        &mut state.runtime,
        &mut state.session,
        &state.clients,
        &history,
        "config unset ui.format",
    )
    .expect("config unset should succeed");
    assert!(matches!(
        unset_format_result,
        crate::repl::ReplLineResult::Restart {
            reload: crate::repl::ReplReloadKind::Default,
            ..
        }
    ));

    let unset_color_result = repl_dispatch::execute_repl_plugin_line(
        &mut state.runtime,
        &mut state.session,
        &state.clients,
        &history,
        "config unset color.prompt.text",
    )
    .expect("color config unset should succeed");
    assert!(matches!(
        unset_color_result,
        crate::repl::ReplLineResult::Restart {
            reload: crate::repl::ReplReloadKind::WithIntro,
            ..
        }
    ));

    let dry_run_unset_result = repl_dispatch::execute_repl_plugin_line(
        &mut state.runtime,
        &mut state.session,
        &state.clients,
        &history,
        "config unset ui.format --dry-run",
    )
    .expect("dry-run config unset should succeed");
    assert!(matches!(
        dry_run_unset_result,
        crate::repl::ReplLineResult::Continue(_)
    ));
}

#[test]
fn repl_config_unset_rebuilds_runtime_state_unit() {
    let mut state = make_test_state(Vec::new());
    state
        .session
        .config_overrides
        .set_for_profile("default", "ui.format", "table");
    state = super::super::rebuild_repl_state(&state).expect("rebuild should succeed");
    assert_eq!(state.runtime.ui.render_settings.format, OutputFormat::Table);

    let history = make_test_history(&mut state);
    let result = repl_dispatch::execute_repl_plugin_line(
        &mut state.runtime,
        &mut state.session,
        &state.clients,
        &history,
        "config unset ui.format",
    )
    .expect("config unset should succeed");
    assert!(matches!(
        result,
        crate::repl::ReplLineResult::Restart {
            reload: crate::repl::ReplReloadKind::Default,
            ..
        }
    ));
    assert_eq!(
        layer_value(&state.session.config_overrides, "ui.format"),
        None
    );

    let next = super::super::rebuild_repl_state(&state).expect("rebuild should succeed");
    assert_eq!(next.runtime.config.resolved().get_string("ui.format"), None);
    assert_eq!(next.runtime.ui.render_settings.format, OutputFormat::Auto);
}

#[test]
fn repl_config_unset_dry_run_preserves_session_state_unit() {
    let mut state = make_test_state(Vec::new());
    state
        .session
        .config_overrides
        .set_for_profile("default", "ui.format", "table");
    state = super::super::rebuild_repl_state(&state).expect("rebuild should succeed");
    assert_eq!(state.runtime.ui.render_settings.format, OutputFormat::Table);

    let history = make_test_history(&mut state);
    let result = repl_dispatch::execute_repl_plugin_line(
        &mut state.runtime,
        &mut state.session,
        &state.clients,
        &history,
        "config unset ui.format --dry-run",
    )
    .expect("dry-run config unset should succeed");
    assert!(matches!(result, crate::repl::ReplLineResult::Continue(_)));
    assert_eq!(
        layer_value(&state.session.config_overrides, "ui.format"),
        Some(&ConfigValue::from("table"))
    );
    assert_eq!(state.runtime.ui.render_settings.format, OutputFormat::Table);
}

#[test]
fn repl_config_prompt_color_change_rebuilds_deterministically_unit() {
    let mut state = make_test_state(Vec::new());
    state
        .session
        .config_overrides
        .set("ui.color.mode", "always");
    state.session.config_overrides.set("ui.mode", "rich");
    state
        .session
        .config_overrides
        .set("repl.simple_prompt", true);
    state = super::super::rebuild_repl_state(&state).expect("rebuild should succeed");
    assert!(
        state
            .runtime
            .ui
            .render_settings
            .resolve_render_settings()
            .color
    );

    let default_prompt =
        crate::repl::presentation::build_repl_prompt(repl_view(&state.runtime, &state.session))
            .left;
    assert!(default_prompt.contains("\x1b["));

    let history = make_test_history(&mut state);
    let result = repl_dispatch::execute_repl_plugin_line(
        &mut state.runtime,
        &mut state.session,
        &state.clients,
        &history,
        "config set color.prompt.text white",
    )
    .expect("prompt color config set should succeed");
    assert!(matches!(
        result,
        crate::repl::ReplLineResult::Restart {
            reload: crate::repl::ReplReloadKind::WithIntro,
            ..
        }
    ));

    state = super::super::rebuild_repl_state(&state).expect("rebuild should succeed");
    let white_prompt =
        crate::repl::presentation::build_repl_prompt(repl_view(&state.runtime, &state.session))
            .left;
    assert!(white_prompt.contains("\x1b[37mdefault"));

    let history = make_test_history(&mut state);
    let result = repl_dispatch::execute_repl_plugin_line(
        &mut state.runtime,
        &mut state.session,
        &state.clients,
        &history,
        "config unset color.prompt.text",
    )
    .expect("prompt color config unset should succeed");
    assert!(matches!(
        result,
        crate::repl::ReplLineResult::Restart {
            reload: crate::repl::ReplReloadKind::WithIntro,
            ..
        }
    ));

    state = super::super::rebuild_repl_state(&state).expect("rebuild should succeed");
    let restored_prompt =
        crate::repl::presentation::build_repl_prompt(repl_view(&state.runtime, &state.session))
            .left;
    assert_eq!(restored_prompt, default_prompt);
}