silicon-commit-cli 0.6.0

Command-line client for Silicon Commit, the work manager for Carbons and Silicons
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
//! API commands: request shapes, local validation, errors, reports, configuration.

mod support;

use serde_json::json;
use std::fs;
use support::{Home, json_output, now, session, stderr};
use wiremock::{
    Mock, MockServer, ResponseTemplate,
    matchers::{body_json, header, method, path, query_param},
};

fn signed_in(home: &Home, api: &MockServer) {
    home.write_session(
        None,
        &session(
            "http://127.0.0.1:9",
            &api.uri(),
            "eyJ.saved",
            "sar_saved",
            now() + 1500,
        ),
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn commands_need_a_sign_in_but_health_checks_do_not() {
    let home = Home::new();
    let api = MockServer::start().await;
    for route in ["/healthz", "/readyz", "/api/v1/version"] {
        Mock::given(method("GET"))
            .and(path(route))
            .and(|r: &wiremock::Request| !r.headers.contains_key("authorization"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"status":"ok"})))
            .expect(1)
            .mount(&api)
            .await;
    }
    for command in ["health", "ready", "version"] {
        json_output(
            home.command()
                .env("COMMIT_API_URL", api.uri())
                .arg(command)
                .output()
                .unwrap(),
        );
    }
    let output = home
        .command()
        .env("COMMIT_API_URL", api.uri())
        .args(["todos", "list"])
        .output()
        .unwrap();
    assert_eq!(output.status.code(), Some(1));
    let text = stderr(&output);
    for part in [
        "Not signed in",
        "not_signed_in",
        "commit login",
        "silicon-accounts login --app commit -q | commit login --slt-stdin",
    ] {
        assert!(text.contains(part), "{text}");
    }
    assert!(!home.state().exists());
}

#[tokio::test(flavor = "multi_thread")]
async fn invalid_todos_are_explained_without_a_request() {
    let home = Home::new();
    let api = MockServer::start().await;
    signed_in(&home, &api);
    for (payload, expected) in [
        (
            json!({"title":"Eat","assignee_id":"si:builder"}),
            "use assigned_to",
        ),
        (
            json!({"title":"Eat","assignee":{"id":"si:builder"}}),
            "use assigned_to",
        ),
        (
            json!({"title":"Eat"}),
            "assigned_to is required and must be a string",
        ),
        (
            json!({"title":"Eat","assigned_to":{"id":"c:alice"}}),
            "assigned_to is required and must be a string",
        ),
        (
            json!({"assigned_to":"c:alice"}),
            "title is required and must be a string",
        ),
        (json!([]), "requires a JSON object"),
    ] {
        let file = home.0.join("todo.json");
        fs::write(&file, payload.to_string()).unwrap();
        for data in [payload.to_string(), format!("@{}", file.display())] {
            let output = home
                .command()
                .args(["todos", "create", "--data", &data])
                .output()
                .unwrap();
            assert_eq!(output.status.code(), Some(1));
            assert!(output.stdout.is_empty());
            assert!(stderr(&output).contains(expected), "{}", stderr(&output));
        }
    }
    let broken = home
        .command()
        .args(["todos", "create", "--data", "{not json"])
        .output()
        .unwrap();
    assert!(stderr(&broken).contains("--data is not valid JSON"));
    assert!(api.received_requests().await.unwrap().is_empty());
}

#[tokio::test(flavor = "multi_thread")]
async fn server_validation_keeps_the_code_request_id_details_and_hint() {
    let home = Home::new();
    let api = MockServer::start().await;
    signed_in(&home, &api);
    let payload = json!({"title":"Eat","assigned_to":"si:elsewhere","description":null,"status":"yet_to_do","attachments":[]});
    Mock::given(method("POST"))
        .and(path("/api/v1/todos"))
        .and(body_json(&payload))
        .and(header("authorization", "Bearer eyJ.saved"))
        .respond_with(
            ResponseTemplate::new(422)
                .insert_header("x-request-id", "todo-validation-request")
                .set_body_json(json!({"error":{"code":"validation_failed","message":"The request contains invalid data.","details":{"assigned_to":"unknown account"}}})),
        )
        .expect(1)
        .mount(&api)
        .await;
    let output = home
        .command()
        .args(["todos", "create", "--data", &payload.to_string()])
        .output()
        .unwrap();
    assert_eq!(output.status.code(), Some(1));
    assert!(output.stdout.is_empty());
    let text = stderr(&output);
    for part in [
        "422",
        "validation_failed",
        "todo-validation-request",
        "unknown account",
        "commit todos create --help",
        "not created",
    ] {
        assert!(text.contains(part), "{text}");
    }
    assert!(!text.contains("eyJ.saved"));
    Mock::given(method("POST"))
        .and(path("/api/v1/todos"))
        .and(body_json(json!({"title":"Help","assigned_to":"si:outsider"})))
        .respond_with(ResponseTemplate::new(403).set_body_json(json!({"error":{"code":"silicon_not_reachable","message":"si:outsider does not take work from you."}})))
        .mount(&api)
        .await;
    let refused = home
        .command()
        .args([
            "todos",
            "create",
            "--data",
            "{\"title\":\"Help\",\"assigned_to\":\"si:outsider\"}",
        ])
        .output()
        .unwrap();
    let text = stderr(&refused);
    assert!(
        text.contains("silicon_not_reachable") && text.contains("commit silicons allow"),
        "{text}"
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn list_filters_custodian_selectors_and_the_allow_list_reach_the_api() {
    let home = Home::new();
    let api = MockServer::start().await;
    signed_in(&home, &api);
    Mock::given(method("GET"))
        .and(path("/api/v1/todos"))
        .and(query_param("view", "delegated_by_me"))
        .and(query_param("assigned_to", "si:builder"))
        .and(query_param("limit", "10"))
        .and(query_param("cursor", "next"))
        .respond_with(
            ResponseTemplate::new(200).set_body_json(json!({"items":[],"next_cursor":null})),
        )
        .expect(1)
        .mount(&api)
        .await;
    Mock::given(method("PUT"))
        .and(path("/api/v1/notification-settings"))
        .and(query_param("silicon", "si:scout"))
        .and(header("if-match", "\"2\""))
        .and(body_json(json!({"webhook_url":"https://hooks.example/commit","todo_list_subscription":{"scope":"status_updates"}})))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({"version":3})))
        .expect(1)
        .mount(&api)
        .await;
    Mock::given(method("PUT"))
        .and(path("/api/v1/silicons/si:scout/allowed-accounts/c:alice"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_json(json!({"silicon":{"id":"si:scout"},"allowed":[{"id":"c:alice"}]})),
        )
        .expect(1)
        .mount(&api)
        .await;
    Mock::given(method("GET"))
        .and(path("/api/v1/me"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_json(json!({"uuid":"zQo","id":"c:ada","silicons":[{"id":"si:scout"}]})),
        )
        .expect(1)
        .mount(&api)
        .await;
    json_output(
        home.command()
            .args([
                "todos",
                "list",
                "--view",
                "delegated_by_me",
                "--assigned-to",
                "si:builder",
                "--limit",
                "10",
                "--cursor",
                "next",
            ])
            .output()
            .unwrap(),
    );
    let settings = json_output(
        home.command()
            .args(["--if-match", "2", "notifications", "--silicon", "si:scout", "--data",
                   "{\"webhook_url\":\"https://hooks.example/commit\",\"todo_list_subscription\":{\"scope\":\"status_updates\"}}"])
            .output()
            .unwrap(),
    );
    assert_eq!(settings["version"], 3);
    assert_eq!(
        json_output(
            home.command()
                .args(["silicons", "allow", "si:scout", "c:alice"])
                .output()
                .unwrap()
        )["allowed"][0]["id"],
        "c:alice"
    );
    assert_eq!(
        json_output(home.command().arg("me").output().unwrap())["silicons"][0]["id"],
        "si:scout"
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn carbons_without_a_shared_email_are_told_how_to_share_one() {
    let home = Home::new();
    let api = MockServer::start().await;
    signed_in(&home, &api);
    Mock::given(method("GET"))
        .and(path("/api/v1/email-settings"))
        .respond_with(
            ResponseTemplate::new(200).set_body_json(
                json!({"email":"","enabled":true,"saved":false,"shared_email":null}),
            ),
        )
        .mount(&api)
        .await;
    let output = home.command().arg("email").output().unwrap();
    assert!(
        stderr(&output).contains("commit login --scope email"),
        "{}",
        stderr(&output)
    );
    assert_eq!(json_output(output)["saved"], false);
}

#[tokio::test(flavor = "multi_thread")]
async fn a_failed_report_is_saved_locally_and_the_failure_is_kept() {
    let home = Home::new();
    let api = MockServer::start().await;
    signed_in(&home, &api);
    Mock::given(method("POST"))
        .and(path("/api/v1/reports"))
        .and(body_json(
            json!({"message":"Todos cannot be read.","pr":null}),
        ))
        .respond_with(ResponseTemplate::new(502).set_body_json(
            json!({"error":{"code":"invalid_provider_response","message":"Postmark refused."}}),
        ))
        .expect(1)
        .mount(&api)
        .await;
    let output = home
        .command()
        .args(["report", "Todos cannot be read."])
        .output()
        .unwrap();
    assert_eq!(output.status.code(), Some(1));
    let text = stderr(&output);
    assert!(
        text.contains("Saved locally:") && text.contains("invalid_provider_response"),
        "{text}"
    );
    let reports: Vec<_> = fs::read_dir(home.state())
        .unwrap()
        .map(|e| e.unwrap().path())
        .filter(|p| p.extension().is_some_and(|e| e == "md"))
        .collect();
    assert_eq!(reports.len(), 1);
    assert!(
        fs::read_to_string(&reports[0])
            .unwrap()
            .contains("Todos cannot be read.")
    );
}

#[test]
fn a_report_draft_needs_no_api_or_session() {
    let home = Home::new();
    fs::create_dir_all(home.state()).unwrap();
    fs::write(home.session_path(None), "invalid session").unwrap();
    let output = home
        .command()
        .env("COMMIT_API_URL", "invalid-url")
        .args(["report", "Offline report recovery", "--save-only"])
        .output()
        .unwrap();
    assert!(output.status.success(), "{}", stderr(&output));
    assert!(String::from_utf8_lossy(&output.stdout).contains("Saved bug report"));
    assert_eq!(
        fs::read_to_string(home.session_path(None)).unwrap(),
        "invalid session"
    );
    let bad_link = home
        .command()
        .args([
            "report",
            "x",
            "--pr",
            "https://example.com/pr",
            "--save-only",
        ])
        .output()
        .unwrap();
    assert!(stderr(&bad_link).contains("teamofsilicons/silicon-commit"));
}

#[test]
fn silicon_home_and_config_home_choose_where_state_lives() {
    let home = Home::new();
    let silicon = home.0.join("silicon-home");
    let chosen = home.0.join("chosen");
    fs::create_dir_all(&silicon).unwrap();
    fs::create_dir_all(&chosen).unwrap();
    let with_silicon = |args: &[&str]| {
        home.command()
            .env("SILICON_HOME", &silicon)
            .args(args)
            .output()
            .unwrap()
    };
    let draft = with_silicon(&["report", "Draft in the Silicon home", "--save-only"]);
    assert!(draft.status.success(), "{}", stderr(&draft));
    assert!(fs::read_dir(silicon.join(".commit")).unwrap().any(|e| {
        e.unwrap()
            .file_name()
            .to_string_lossy()
            .starts_with("report-")
    }));
    assert!(!home.state().exists(), "SILICON_HOME wins over HOME");
    let output = with_silicon(&["config", "home", chosen.to_str().unwrap()]);
    assert!(output.status.success(), "{}", stderr(&output));
    assert!(silicon.join(".commit/home_dir").exists());
    let shown = json_output(with_silicon(&["config", "show"]));
    assert_eq!(
        shown["state_dir"],
        json!(fs::canonicalize(&chosen).unwrap().join(".commit"))
    );
    assert!(
        shown.get("telemetry").is_none(),
        "Commit records no telemetry"
    );
    assert_eq!(shown["signed_in"], false);
    let missing = with_silicon(&["config", "home", home.0.join("missing").to_str().unwrap()]);
    assert_eq!(missing.status.code(), Some(1));
    assert!(stderr(&missing).contains("not a directory"));
}

#[test]
fn config_telemetry_is_an_accepted_no_op() {
    let home = Home::new();
    for value in ["on", "off"] {
        let output = home
            .command()
            .args(["config", "telemetry", value])
            .output()
            .unwrap();
        assert!(output.status.success(), "{}", stderr(&output));
        assert_eq!(
            String::from_utf8_lossy(&output.stdout),
            "Telemetry was removed from Commit; there is nothing to turn on or off.\n"
        );
    }
    assert!(!home.state().join("telemetry").exists());
    let help = home.command().args(["config", "--help"]).output().unwrap();
    assert!(!String::from_utf8_lossy(&help.stdout).contains("telemetry"));
}