icydb-cli 0.179.3

Developer CLI tools for IcyDB
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
//! Module: CLI argument parsing tests.
//! Responsibility: exercise clap-derived command surfaces and target defaults.
//! Does not own: command execution or helper rendering.
//! Boundary: test-only assertions over parsed `CliArgs`.

use std::path::Path;

use clap::Parser;

use crate::{
    cli::{
        CanisterCommand, CliArgs, CliCommand, ConfigCommand, DEFAULT_ENVIRONMENT, SchemaCommand,
    },
    shell::test_support::sql_shell_config_inputs,
};

#[test]
fn clap_help_exposes_target_environment_flags() {
    for args in [
        ["icydb", "snapshot", "--help"].as_slice(),
        ["icydb", "metrics", "--help"].as_slice(),
        ["icydb", "schema", "show", "--help"].as_slice(),
        ["icydb", "schema", "check", "--help"].as_slice(),
        ["icydb", "canister", "refresh", "--help"].as_slice(),
    ] {
        let help = clap_help_text(args);

        assert!(
            help.contains("<CANISTER>"),
            "help should expose positional canister target: {help}"
        );
        assert!(
            help.contains("-e, --environment"),
            "help should expose -e shorthand: {help}"
        );
        assert!(
            !help.contains("-c, --canister"),
            "target commands should not expose duplicate -c canister target: {help}"
        );
    }
}

#[test]
fn clap_help_exposes_available_short_flags_on_config_commands() {
    let sql_help = clap_help_text(["icydb", "sql", "--help"].as_slice());
    assert!(sql_help.contains("-c, --canister"));
    assert!(sql_help.contains("including supported DDL"));
    assert!(sql_help.contains("icydb sql -c demo_rpg"));
    assert!(sql_help.contains("CREATE INDEX character_renown_idx ON character (renown)"));
    assert!(sql_help.contains("DROP INDEX character_renown_idx ON character"));

    let init_help = clap_help_text(["icydb", "config", "init", "--help"].as_slice());
    assert!(init_help.contains("-c, --canister"));

    for args in [
        ["icydb", "config", "show", "--help"].as_slice(),
        ["icydb", "config", "check", "--help"].as_slice(),
    ] {
        let help = clap_help_text(args);
        assert!(
            help.contains("-e, --environment"),
            "help should expose -e shorthand: {help}"
        );
    }
}

fn clap_help_text(args: &[&str]) -> String {
    let err = CliArgs::try_parse_from(args).expect_err("help invocation should exit through clap");

    assert_eq!(err.kind(), clap::error::ErrorKind::DisplayHelp);

    err.to_string()
}

#[test]
fn cli_args_preserve_trailing_sql_convenience_form() {
    let args = CliArgs::try_parse_from([
        "icydb",
        "sql",
        "--canister",
        "test_sql",
        "SELECT",
        "name",
        "FROM",
        "character;",
    ])
    .expect("trailing SQL should parse");
    let CliCommand::Sql(sql_args) = args.into_command() else {
        panic!("expected sql command");
    };
    let (canister, environment, _, sql) = sql_shell_config_inputs(sql_args);

    assert_eq!(canister, "test_sql");
    assert_eq!(environment, DEFAULT_ENVIRONMENT);
    assert_eq!(sql.as_deref(), Some("SELECT name FROM character;"));
}

#[test]
fn cli_args_accept_explicit_sql_option() {
    let args = CliArgs::try_parse_from([
        "icydb",
        "sql",
        "--canister",
        "demo_rpg",
        "--history-file",
        ".cache/custom_history",
        "--sql",
        "SELECT name FROM character;",
    ])
    .expect("--sql should parse");
    let CliCommand::Sql(sql_args) = args.into_command() else {
        panic!("expected sql command");
    };
    let (_, environment, history_file, sql) = sql_shell_config_inputs(sql_args);

    assert_eq!(history_file, Path::new(".cache/custom_history"));
    assert_eq!(environment, DEFAULT_ENVIRONMENT);
    assert_eq!(sql.as_deref(), Some("SELECT name FROM character;"));
}

#[test]
fn cli_args_require_sql_target_canister() {
    let err = CliArgs::try_parse_from(["icydb", "sql", "SELECT * FROM character;"])
        .expect_err("sql command should require explicit canister");

    assert_eq!(err.kind(), clap::error::ErrorKind::MissingRequiredArgument);
}

#[test]
fn cli_args_accept_explicit_icp_environment() {
    let args = CliArgs::try_parse_from([
        "icydb",
        "sql",
        "--canister",
        "demo_rpg",
        "--environment",
        "test",
        "SELECT",
        "*",
        "FROM",
        "character;",
    ])
    .expect("sql environment should parse");
    let CliCommand::Sql(sql_args) = args.into_command() else {
        panic!("expected sql command");
    };
    let (_, environment, _, sql) = sql_shell_config_inputs(sql_args);

    assert_eq!(environment, "test");
    assert_eq!(sql.as_deref(), Some("SELECT * FROM character;"));
}

#[test]
fn cli_args_group_snapshot_under_top_level_keyword() {
    let args = CliArgs::try_parse_from(["icydb", "snapshot", "demo_rpg", "--environment", "test"])
        .expect("snapshot command should parse");
    let CliCommand::Snapshot(target) = args.into_command() else {
        panic!("expected snapshot command");
    };

    assert_eq!(target.canister_name(), "demo_rpg");
    assert_eq!(target.environment(), "test");
}

#[test]
fn cli_args_reject_canister_flag_on_target_commands() {
    let err = CliArgs::try_parse_from(["icydb", "snapshot", "--canister", "demo_rpg"])
        .expect_err("snapshot command should reject flagged canister target");

    assert_eq!(err.kind(), clap::error::ErrorKind::UnknownArgument);
}

#[test]
fn cli_args_group_metrics_under_top_level_keyword() {
    let args =
        CliArgs::try_parse_from(["icydb", "metrics", "demo_rpg", "--window-start-ms", "123"])
            .expect("metrics command should parse");
    let CliCommand::Metrics(args) = args.into_command() else {
        panic!("expected metrics command");
    };

    assert_eq!(args.target().canister_name(), "demo_rpg");
    assert_eq!(args.target().environment(), DEFAULT_ENVIRONMENT);
    assert_eq!(args.window_start_ms(), Some(123));
    assert!(!args.reset());
}

#[test]
fn cli_args_group_metrics_reset_under_top_level_keyword() {
    let args = CliArgs::try_parse_from([
        "icydb",
        "metrics",
        "demo_rpg",
        "--environment",
        "test",
        "--reset",
    ])
    .expect("metrics reset command should parse");
    let CliCommand::Metrics(args) = args.into_command() else {
        panic!("expected metrics command");
    };

    assert_eq!(args.target().canister_name(), "demo_rpg");
    assert_eq!(args.target().environment(), "test");
    assert_eq!(args.window_start_ms(), None);
    assert!(args.reset());
}

#[test]
fn cli_args_group_schema_under_top_level_keyword() {
    let args = CliArgs::try_parse_from([
        "icydb",
        "schema",
        "show",
        "demo_rpg",
        "--environment",
        "test",
    ])
    .expect("schema show command should parse");
    let CliCommand::Schema(SchemaCommand::Show(target)) = args.into_command() else {
        panic!("expected schema command");
    };

    assert_eq!(target.canister_name(), "demo_rpg");
    assert_eq!(target.environment(), "test");
}

#[test]
fn cli_args_group_schema_check_under_schema_keyword() {
    let args = CliArgs::try_parse_from([
        "icydb",
        "schema",
        "check",
        "demo_rpg",
        "--environment",
        "test",
    ])
    .expect("schema check command should parse");
    let CliCommand::Schema(SchemaCommand::Check(target)) = args.into_command() else {
        panic!("expected schema check command");
    };

    assert_eq!(target.canister_name(), "demo_rpg");
    assert_eq!(target.environment(), "test");
}

#[test]
fn cli_args_group_config_show_under_config_keyword() {
    let args = CliArgs::try_parse_from([
        "icydb",
        "config",
        "show",
        "--environment",
        "demo",
        "--start-dir",
        "canisters/demo/rpg",
    ])
    .expect("config show should parse");
    let CliCommand::Config(ConfigCommand::Show(args)) = args.into_command() else {
        panic!("expected config show command");
    };

    assert_eq!(args.environment(), Some("demo"));
    assert_eq!(args.start_dir(), Some(Path::new("canisters/demo/rpg")));
}

#[test]
fn cli_args_group_config_check_under_config_keyword() {
    let args = CliArgs::try_parse_from([
        "icydb",
        "config",
        "check",
        "--environment",
        "demo",
        "--start-dir",
        "canisters/demo/rpg",
    ])
    .expect("config check should parse");
    let CliCommand::Config(ConfigCommand::Check(args)) = args.into_command() else {
        panic!("expected config check command");
    };

    assert_eq!(args.environment(), Some("demo"));
    assert_eq!(args.start_dir(), Some(Path::new("canisters/demo/rpg")));
}

#[test]
fn cli_args_group_config_init_under_config_keyword() {
    let args = CliArgs::try_parse_from([
        "icydb",
        "config",
        "init",
        "--canister",
        "demo_rpg",
        "--ddl",
        "--fixtures",
        "--metrics",
        "--metrics-reset",
        "--snapshot",
        "--schema",
        "--start-dir",
        "canisters/demo/rpg",
    ])
    .expect("config init should parse");
    let CliCommand::Config(ConfigCommand::Init(args)) = args.into_command() else {
        panic!("expected config init command");
    };

    assert_eq!(args.canister_name(), "demo_rpg");
    assert!(args.readonly());
    assert!(args.ddl());
    assert!(args.fixtures());
    assert!(args.metrics());
    assert!(args.metrics_reset());
    assert!(args.snapshot());
    assert!(args.schema());
    assert_eq!(args.start_dir(), Some(Path::new("canisters/demo/rpg")));
}

#[test]
fn cli_args_config_init_no_readonly_overrides_all() {
    let args = CliArgs::try_parse_from([
        "icydb",
        "config",
        "init",
        "--canister",
        "demo_rpg",
        "--all",
        "--no-readonly",
    ])
    .expect("config init should parse all without readonly");
    let CliCommand::Config(ConfigCommand::Init(args)) = args.into_command() else {
        panic!("expected config init command");
    };

    assert!(!args.readonly());
    assert!(args.ddl());
    assert!(args.fixtures());
    assert!(args.metrics());
    assert!(args.metrics_reset());
    assert!(args.snapshot());
    assert!(args.schema());
}

#[test]
fn cli_args_group_canister_list_under_canister_keyword() {
    let args = CliArgs::try_parse_from(["icydb", "canister", "list", "--environment", "test"])
        .expect("canister list should parse");
    let CliCommand::Canister(CanisterCommand::List(target)) = args.into_command() else {
        panic!("expected canister list command");
    };

    assert_eq!(target.environment(), "test");
}

#[test]
fn cli_args_group_canister_status_under_canister_keyword() {
    let args = CliArgs::try_parse_from(["icydb", "canister", "status", "demo"])
        .expect("canister status should parse");
    let CliCommand::Canister(CanisterCommand::Status(target)) = args.into_command() else {
        panic!("expected canister status command");
    };

    assert_eq!(target.canister_name(), "demo");
}

#[test]
fn cli_args_group_canister_refresh_under_canister_keyword() {
    let args = CliArgs::try_parse_from(["icydb", "canister", "refresh", "demo", "-e", "test"])
        .expect("canister refresh should parse");
    let CliCommand::Canister(CanisterCommand::Refresh(target)) = args.into_command() else {
        panic!("expected canister refresh command");
    };

    assert_eq!(target.canister_name(), "demo");
    assert_eq!(target.environment(), "test");
}

#[test]
fn cli_args_group_canister_deploy_under_canister_keyword() {
    let args = CliArgs::try_parse_from(["icydb", "canister", "deploy", "demo", "-e", "test"])
        .expect("canister deploy should parse");
    let CliCommand::Canister(CanisterCommand::Deploy(target)) = args.into_command() else {
        panic!("expected canister deploy command");
    };

    assert_eq!(target.canister_name(), "demo");
    assert_eq!(target.environment(), "test");
}

#[test]
fn cli_args_group_canister_upgrade_under_canister_keyword() {
    let args = CliArgs::try_parse_from([
        "icydb",
        "canister",
        "upgrade",
        "demo",
        "-e",
        "test",
        "--wasm",
        ".icp/local/canisters/demo/demo.wasm",
    ])
    .expect("canister upgrade should parse");
    let CliCommand::Canister(CanisterCommand::Upgrade(args)) = args.into_command() else {
        panic!("expected canister upgrade command");
    };

    assert_eq!(args.target().canister_name(), "demo");
    assert_eq!(args.target().environment(), "test");
    assert_eq!(
        args.wasm(),
        Some(Path::new(".icp/local/canisters/demo/demo.wasm")),
    );
}