assay-lua 0.20.14

General-purpose enhanced Lua runtime. Batteries-included scripting, automation, and web services.
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
mod common;

use common::run_lua;
use wiremock::matchers::{body_json, method, path, query_param};
use wiremock::{Mock, MockServer, ResponseTemplate};

async fn json_route(server: &MockServer, verb: &str, route: &str, body: serde_json::Value) {
    Mock::given(method(verb))
        .and(path(route))
        .respond_with(ResponseTemplate::new(200).set_body_json(body))
        .mount(server)
        .await;
}

#[tokio::test]
async fn test_crm_campaign_reads() {
    let server = MockServer::start().await;
    json_route(
        &server,
        "GET",
        "/api/admin/crm/campaigns",
        serde_json::json!({"campaigns": [{"id": "alpha", "state": "active"}]}),
    )
    .await;
    json_route(
        &server,
        "GET",
        "/api/admin/crm/campaigns/alpha/overview",
        serde_json::json!({"sent": 42, "replies": 3}),
    )
    .await;

    let script = format!(
        r#"
        local neutron = require("assay.neutron")
        local c = neutron.client("{}", {{ token = "nck_test" }})
        local list, list_status = c.crm.campaigns:list()
        assert.eq(list_status, 200)
        assert.eq(list.campaigns[1].id, "alpha")
        local overview = c.crm.campaigns:overview("alpha")
        assert.eq(overview.sent, 42)
        "#,
        server.uri()
    );
    run_lua(&script).await.unwrap();
}

#[tokio::test]
async fn test_crm_campaign_patch_sends_body() {
    let server = MockServer::start().await;
    Mock::given(method("PATCH"))
        .and(path("/api/admin/crm/campaigns/alpha"))
        .and(body_json(serde_json::json!({"brief": "shorter"})))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"id": "alpha"})))
        .mount(&server)
        .await;

    let script = format!(
        r#"
        local neutron = require("assay.neutron")
        local c = neutron.client("{}", {{ token = "nck_test" }})
        local out, status = c.crm.campaigns:update("alpha", {{ brief = "shorter" }})
        assert.eq(status, 200)
        assert.eq(out.id, "alpha")
        "#,
        server.uri()
    );
    run_lua(&script).await.unwrap();
}

#[tokio::test]
async fn test_crm_people_list_builds_query() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/api/admin/crm/people"))
        .and(query_param("campaign", "alpha"))
        .and(query_param("state", "REPLIED"))
        .and(query_param("limit", "50"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"people": []})))
        .mount(&server)
        .await;

    let script = format!(
        r#"
        local neutron = require("assay.neutron")
        local c = neutron.client("{}", {{ token = "nck_test" }})
        local out = c.crm.people:list({{ campaign = "alpha", state = "REPLIED", limit = 50 }})
        assert.eq(#out.people, 0)
        "#,
        server.uri()
    );
    run_lua(&script).await.unwrap();
}

#[tokio::test]
async fn test_crm_inbox_draft_lifecycle() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/api/admin/crm/inbox/drafts"))
        .and(body_json(
            serde_json::json!({"person_id": "p1", "body_text": "hello"}),
        ))
        .respond_with(ResponseTemplate::new(201).set_body_json(serde_json::json!({"id": "d1"})))
        .mount(&server)
        .await;
    Mock::given(method("PATCH"))
        .and(path("/api/admin/crm/inbox/drafts/d1"))
        .and(body_json(serde_json::json!({"subject": "Re: hello"})))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"id": "d1"})))
        .mount(&server)
        .await;
    json_route(
        &server,
        "POST",
        "/api/admin/crm/inbox/drafts/d1/send",
        serde_json::json!({"sent": true}),
    )
    .await;

    let script = format!(
        r#"
        local neutron = require("assay.neutron")
        local c = neutron.client("{}", {{ token = "nck_test" }})
        local draft, created = c.crm.inbox:draft_create({{ person_id = "p1", body_text = "hello" }})
        assert.eq(created, 201)
        assert.eq(draft.id, "d1")
        c.crm.inbox:draft_update("d1", {{ subject = "Re: hello" }})
        local sent = c.crm.inbox:draft_send("d1")
        assert.eq(sent.sent, true)
        "#,
        server.uri()
    );
    run_lua(&script).await.unwrap();
}

#[tokio::test]
async fn test_crm_suppression_lift_encodes_path_segments() {
    let server = MockServer::start().await;
    json_route(
        &server,
        "DELETE",
        "/api/admin/crm/suppressions/address/ada%40example.com",
        serde_json::json!({"removed": 1}),
    )
    .await;

    let script = format!(
        r#"
        local neutron = require("assay.neutron")
        local c = neutron.client("{}", {{ token = "nck_test" }})
        local out, status = c.crm.suppressions:lift("address", "ada@example.com")
        assert.eq(status, 200)
        assert.eq(out.removed, 1)
        "#,
        server.uri()
    );
    run_lua(&script).await.unwrap();
}

#[tokio::test]
async fn test_crm_sends_fleet_and_reports() {
    let server = MockServer::start().await;
    json_route(
        &server,
        "GET",
        "/api/admin/crm/sends/pending",
        serde_json::json!({"sends": [{"id": "s1"}]}),
    )
    .await;
    json_route(
        &server,
        "POST",
        "/api/admin/crm/sends/s1/approve",
        serde_json::json!({"approved": "s1"}),
    )
    .await;
    json_route(
        &server,
        "GET",
        "/api/admin/crm/fleet/summary",
        serde_json::json!({"healthy": 12}),
    )
    .await;
    json_route(
        &server,
        "GET",
        "/api/admin/crm/reports/weekly-note",
        serde_json::json!({"note": "steady"}),
    )
    .await;

    let script = format!(
        r#"
        local neutron = require("assay.neutron")
        local c = neutron.client("{}", {{ token = "nck_test" }})
        assert.eq(c.crm.sends:pending().sends[1].id, "s1")
        assert.eq(c.crm.sends:approve("s1").approved, "s1")
        assert.eq(c.crm.fleet:summary().healthy, 12)
        assert.eq(c.crm.reports:weekly_note().note, "steady")
        "#,
        server.uri()
    );
    run_lua(&script).await.unwrap();
}

#[tokio::test]
async fn test_crm_integrations_account_put() {
    let server = MockServer::start().await;
    Mock::given(method("PUT"))
        .and(path(
            "/api/admin/crm/integrations/salesforge/accounts/acct-1",
        ))
        .and(body_json(serde_json::json!({"settings": {"region": "eu"}})))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"ok": true})))
        .mount(&server)
        .await;

    let script = format!(
        r#"
        local neutron = require("assay.neutron")
        local c = neutron.client("{}", {{ token = "nck_test" }})
        local out = c.crm.integrations:account_put("salesforge", "acct-1", {{
            settings = {{ region = "eu" }},
        }})
        assert.eq(out.ok, true)
        "#,
        server.uri()
    );
    run_lua(&script).await.unwrap();
}

#[tokio::test]
async fn test_crm_sequencer_events_query_and_reapply() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/api/admin/crm/sequencer-events"))
        .and(query_param("campaign", "alpha"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"events": []})))
        .mount(&server)
        .await;
    json_route(
        &server,
        "POST",
        "/api/admin/crm/sequencer-events/e1/reapply",
        serde_json::json!({"reapplied": true}),
    )
    .await;

    let script = format!(
        r#"
        local neutron = require("assay.neutron")
        local c = neutron.client("{}", {{ token = "nck_test" }})
        assert.eq(#c.crm:sequencer_events({{ campaign = "alpha" }}).events, 0)
        assert.eq(c.crm:sequencer_event_reapply("e1").reapplied, true)
        "#,
        server.uri()
    );
    run_lua(&script).await.unwrap();
}

#[tokio::test]
async fn test_work_items_move_and_unrelate() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/api/work/items/i1/move"))
        .and(body_json(
            serde_json::json!({"to_status": "in_review", "expected_revision": 4}),
        ))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_json(serde_json::json!({"id": "i1", "revision": 5})),
        )
        .mount(&server)
        .await;
    json_route(
        &server,
        "DELETE",
        "/api/work/items/i1/relations/i2/blocks",
        serde_json::json!({"removed": true}),
    )
    .await;

    let script = format!(
        r#"
        local neutron = require("assay.neutron")
        local c = neutron.client("{}", {{ token = "nck_test" }})
        local moved = c.work.items:move("i1", {{ to_status = "in_review", expected_revision = 4 }})
        assert.eq(moved.revision, 5)
        assert.eq(c.work.items:unrelate("i1", "i2", "blocks").removed, true)
        "#,
        server.uri()
    );
    run_lua(&script).await.unwrap();
}

#[tokio::test]
async fn test_work_projects_plans_and_ready() {
    let server = MockServer::start().await;
    json_route(
        &server,
        "GET",
        "/api/work/projects/pr1/board",
        serde_json::json!({"columns": [{"key": "ready"}]}),
    )
    .await;
    Mock::given(method("POST"))
        .and(path("/api/work/plans/pl1/decide"))
        .and(body_json(serde_json::json!({"accept": true})))
        .respond_with(
            ResponseTemplate::new(200).set_body_json(serde_json::json!({"state": "accepted"})),
        )
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path("/api/work/ready"))
        .and(query_param("project", "pr1"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"items": []})))
        .mount(&server)
        .await;

    let script = format!(
        r#"
        local neutron = require("assay.neutron")
        local c = neutron.client("{}", {{ token = "nck_test" }})
        assert.eq(c.work.projects:board("pr1").columns[1].key, "ready")
        assert.eq(c.work.plans:decide("pl1", {{ accept = true }}).state, "accepted")
        assert.eq(#c.work:ready({{ project = "pr1" }}).items, 0)
        "#,
        server.uri()
    );
    run_lua(&script).await.unwrap();
}

#[tokio::test]
async fn test_request_escape_hatch_returns_body_and_status() {
    let server = MockServer::start().await;
    json_route(
        &server,
        "GET",
        "/api/admin/crm/overview",
        serde_json::json!({"campaigns": 3}),
    )
    .await;

    let script = format!(
        r#"
        local neutron = require("assay.neutron")
        local c = neutron.client("{}", {{ token = "nck_test" }})
        local body, status = c:request("GET", "/api/admin/crm/overview")
        assert.eq(status, 200)
        assert.eq(body.campaigns, 3)
        "#,
        server.uri()
    );
    run_lua(&script).await.unwrap();
}

#[tokio::test]
async fn test_non_2xx_returns_error_body_without_throwing() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/api/admin/crm/campaigns"))
        .respond_with(
            ResponseTemplate::new(409).set_body_json(serde_json::json!({"error": "id taken"})),
        )
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path("/api/admin/crm/claims"))
        .respond_with(ResponseTemplate::new(503).set_body_string("upstream down"))
        .mount(&server)
        .await;

    let script = format!(
        r#"
        local neutron = require("assay.neutron")
        local c = neutron.client("{}", {{ token = "nck_test" }})
        local body, status = c.crm.campaigns:create({{ name = "Alpha" }})
        assert.eq(status, 409)
        assert.eq(body.error, "id taken")
        local raw, raw_status = c.crm.claims:list()
        assert.eq(raw_status, 503)
        assert.eq(raw, "upstream down")
        "#,
        server.uri()
    );
    run_lua(&script).await.unwrap();
}

#[tokio::test]
async fn test_client_falls_back_to_env_url_and_token() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/api/work/repositories"))
        .and(wiremock::matchers::header(
            "authorization",
            "Bearer nck_from_env",
        ))
        .respond_with(
            ResponseTemplate::new(200).set_body_json(serde_json::json!({"repositories": []})),
        )
        .mount(&server)
        .await;

    let script = format!(
        r#"
        env.set("NEUTRON_URL", "{}/")
        env.set("NEUTRON_TOKEN", "nck_from_env")
        local neutron = require("assay.neutron")
        local c = neutron.client()
        assert.eq(#c.work:repositories().repositories, 0)
        "#,
        server.uri()
    );
    run_lua(&script).await.unwrap();
}

#[tokio::test]
async fn test_claims_approve_sends_review_by() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/api/admin/crm/claims/cl1/approve"))
        .and(body_json(serde_json::json!({"review_by": "2027-03-01"})))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"ok": true})))
        .mount(&server)
        .await;

    let script = format!(
        r#"
        local neutron = require("assay.neutron")
        local c = neutron.client("{}", {{ token = "nck_test" }})
        local out = c.crm.claims:approve("cl1", {{ review_by = "2027-03-01" }})
        assert.eq(out.ok, true)
        "#,
        server.uri()
    );
    run_lua(&script).await.unwrap();
}

#[tokio::test]
async fn test_people_imports_passes_campaign_query() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/api/admin/crm/people/imports"))
        .and(query_param("campaign", "alpha"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"batches": []})))
        .mount(&server)
        .await;

    let script = format!(
        r#"
        local neutron = require("assay.neutron")
        local c = neutron.client("{}", {{ token = "nck_test" }})
        assert.eq(#c.crm.people:imports({{ campaign = "alpha" }}).batches, 0)
        "#,
        server.uri()
    );
    run_lua(&script).await.unwrap();
}

#[tokio::test]
async fn test_people_import_dry_runs_unless_told_otherwise() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/api/admin/crm/people/import"))
        .and(query_param("dry_run", "1"))
        .respond_with(
            ResponseTemplate::new(200).set_body_json(serde_json::json!({"dry_run": true})),
        )
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/api/admin/crm/people/import"))
        .and(wiremock::matchers::query_param_is_missing("dry_run"))
        .respond_with(
            ResponseTemplate::new(200).set_body_json(serde_json::json!({"dry_run": false})),
        )
        .mount(&server)
        .await;

    let script = format!(
        r#"
        local neutron = require("assay.neutron")
        local c = neutron.client("{}", {{ token = "nck_test" }})
        local body = {{ campaign = "alpha", csv = "email\nada@example.com" }}
        assert.eq(c.crm.people:import(body).dry_run, true)
        assert.eq(c.crm.people:import(body, {{}}).dry_run, true)
        assert.eq(c.crm.people:import(body, {{ dry_run = true }}).dry_run, true)
        assert.eq(c.crm.people:import(body, {{ dry_run = false }}).dry_run, false)
        "#,
        server.uri()
    );
    run_lua(&script).await.unwrap();
}

#[tokio::test]
async fn test_opts_headers_ride_every_request() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/api/admin/crm/overview"))
        .and(wiremock::matchers::header("x-neutron-internal", "1"))
        .and(wiremock::matchers::header("x-neutron-token", "int_tok"))
        .and(wiremock::matchers::header(
            "authorization",
            "Bearer nck_test",
        ))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"ok": true})))
        .mount(&server)
        .await;

    let script = format!(
        r#"
        local neutron = require("assay.neutron")
        local c = neutron.client("{}", {{
            token = "nck_test",
            headers = {{ ["x-neutron-internal"] = "1", ["x-neutron-token"] = "int_tok" }},
        }})
        assert.eq(c.crm:overview().ok, true)
        "#,
        server.uri()
    );
    run_lua(&script).await.unwrap();
}

#[tokio::test]
async fn test_opts_headers_cannot_replace_the_bearer() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/api/admin/crm/overview"))
        .and(wiremock::matchers::header(
            "authorization",
            "Bearer nck_test",
        ))
        .and(wiremock::matchers::header("x-trace", "t1"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"ok": true})))
        .mount(&server)
        .await;

    let script = format!(
        r#"
        local neutron = require("assay.neutron")
        local c = neutron.client("{}", {{
            token = "nck_test",
            headers = {{ Authorization = "Bearer stolen", ["x-trace"] = "t1" }},
        }})
        assert.eq(c.crm:overview().ok, true)
        "#,
        server.uri()
    );
    run_lua(&script).await.unwrap();
}

#[tokio::test]
async fn test_opts_headers_rejects_a_non_table() {
    let script = r#"
        local neutron = require("assay.neutron")
        local ok, err = pcall(function()
            return neutron.client("http://x.example", { token = "t", headers = "nope" })
        end)
        assert.eq(ok, false)
        assert.contains(tostring(err), "opts.headers must be a table")
    "#;
    run_lua(script).await.unwrap();
}

#[tokio::test]
async fn test_proposals_list_accept_and_decline() {
    let server = MockServer::start().await;
    json_route(
        &server,
        "GET",
        "/api/admin/crm/proposals",
        serde_json::json!({"proposals": [{"id": "p1"}]}),
    )
    .await;
    json_route(
        &server,
        "POST",
        "/api/admin/crm/proposals/p1/accept",
        serde_json::json!({"ok": true}),
    )
    .await;
    Mock::given(method("POST"))
        .and(path("/api/admin/crm/proposals/p2/decline"))
        .respond_with(
            ResponseTemplate::new(409)
                .set_body_json(serde_json::json!({"error": "already accepted"})),
        )
        .mount(&server)
        .await;

    let script = format!(
        r#"
        local neutron = require("assay.neutron")
        local c = neutron.client("{}", {{ token = "nck_test" }})
        assert.eq(c.crm.proposals:list().proposals[1].id, "p1")
        assert.eq(c.crm.proposals:accept("p1").ok, true)
        local body, status = c.crm.proposals:decline("p2")
        assert.eq(status, 409)
        assert.eq(body.error, "already accepted")
        "#,
        server.uri()
    );
    run_lua(&script).await.unwrap();
}

#[tokio::test]
async fn test_integration_account_lifecycle() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/api/admin/crm/integrations/salesforge/accounts"))
        .and(body_json(serde_json::json!({"label": "EU"})))
        .respond_with(ResponseTemplate::new(201).set_body_json(serde_json::json!({"added": "a2"})))
        .mount(&server)
        .await;
    Mock::given(method("PATCH"))
        .and(path("/api/admin/crm/integrations/salesforge/accounts/a2"))
        .and(body_json(serde_json::json!({"fleet_label": "fleet-002"})))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"ok": true})))
        .mount(&server)
        .await;
    json_route(
        &server,
        "POST",
        "/api/admin/crm/integrations/salesforge/accounts/a2/webhook-secret",
        serde_json::json!({"rotated": true}),
    )
    .await;
    json_route(
        &server,
        "DELETE",
        "/api/admin/crm/integrations/salesforge/accounts/a2/secrets/api_key",
        serde_json::json!({"cleared": true}),
    )
    .await;
    json_route(
        &server,
        "DELETE",
        "/api/admin/crm/integrations/salesforge/accounts/a2",
        serde_json::json!({"removed": true}),
    )
    .await;

    let script = format!(
        r#"
        local neutron = require("assay.neutron")
        local c = neutron.client("{}", {{ token = "nck_test" }})
        local ig = c.crm.integrations
        local created, status = ig:account_create("salesforge", {{ label = "EU" }})
        assert.eq(status, 201)
        assert.eq(created.added, "a2")
        assert.eq(ig:account_update("salesforge", "a2", {{ fleet_label = "fleet-002" }}).ok, true)
        assert.eq(ig:account_webhook_secret("salesforge", "a2").rotated, true)
        assert.eq(ig:account_secret_delete("salesforge", "a2", "api_key").cleared, true)
        assert.eq(ig:account_delete("salesforge", "a2").removed, true)
        "#,
        server.uri()
    );
    run_lua(&script).await.unwrap();
}

#[tokio::test]
async fn test_parameters_the_old_signatures_could_not_reach() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/api/admin/crm/domains/mail.example.com/pause"))
        .and(body_json(serde_json::json!({"reason": "bounces"})))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"paused": true})))
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/api/admin/crm/domains/mail.example.com/recheck"))
        .and(body_json(serde_json::json!({"dkim_selector": "s1"})))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"ok": true})))
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path("/api/admin/crm/claims"))
        .and(query_param("citable", "1"))
        .and(query_param("locale", "en-GB"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"claims": []})))
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path("/api/admin/crm/reports/conversion"))
        .and(query_param("since", "2026-01-01"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"rows": []})))
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path("/api/admin/crm/reports/weekly-note"))
        .and(query_param("week", "2026-09-14"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"note": "ok"})))
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path("/api/work/projects/pr1/board"))
        .and(query_param("sprint", "current"))
        .and(query_param("done", "1"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"columns": []})))
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/api/work/sprints/s1/close"))
        .and(body_json(serde_json::json!({"carry_to": "next"})))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"closed": true})))
        .mount(&server)
        .await;

    let script = format!(
        r#"
        local neutron = require("assay.neutron")
        local c = neutron.client("{}", {{ token = "nck_test" }})
        assert.eq(c.crm.domains:pause("mail.example.com", {{ reason = "bounces" }}).paused, true)
        assert.eq(c.crm.domains:recheck("mail.example.com", {{ dkim_selector = "s1" }}).ok, true)
        assert.eq(#c.crm.claims:list({{ citable = "1", locale = "en-GB" }}).claims, 0)
        assert.eq(#c.crm.reports:conversion({{ since = "2026-01-01" }}).rows, 0)
        assert.eq(c.crm.reports:weekly_note({{ week = "2026-09-14" }}).note, "ok")
        local board = c.work.projects:board("pr1", {{ sprint = "current", done = "1" }})
        assert.eq(#board.columns, 0)
        assert.eq(c.work.sprints:close("s1", {{ carry_to = "next" }}).closed, true)
        "#,
        server.uri()
    );
    run_lua(&script).await.unwrap();
}

#[tokio::test]
async fn test_move_unassign_sprint_puts_a_real_null_on_the_wire() {
    let server = MockServer::start().await;
    // Both matchers on purpose: the raw substring proves a JSON null literally
    // reached the wire, the parsed shape proves nothing else was mangled.
    Mock::given(method("POST"))
        .and(path("/api/work/items/i1/move"))
        .and(wiremock::matchers::body_string_contains(
            "\"sprint_id\":null",
        ))
        .and(body_json(serde_json::json!({
            "to_status": "ready",
            "expected_revision": 7,
            "sprint_id": null,
        })))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"moved": true})))
        .mount(&server)
        .await;
    // A body carrying only the null, to cover the empty-object seam.
    Mock::given(method("POST"))
        .and(path("/api/work/items/i2/move"))
        .and(body_json(serde_json::json!({"sprint_id": null})))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"moved": true})))
        .mount(&server)
        .await;
    // Without the option the key is simply absent, which keeps the sprint.
    Mock::given(method("POST"))
        .and(path("/api/work/items/i3/move"))
        .and(body_json(
            serde_json::json!({"to_status": "ready", "expected_revision": 7}),
        ))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"moved": true})))
        .mount(&server)
        .await;

    let script = format!(
        r#"
        local neutron = require("assay.neutron")
        local c = neutron.client("{}", {{ token = "nck_test" }})
        local unassign = {{ unassign_sprint = true }}

        -- a camelCase sprintId in the body must not outrank the null
        local body = {{ to_status = "ready", expected_revision = 7, sprintId = "s9" }}
        assert.eq(c.work.items:move("i1", body, unassign).moved, true)

        assert.eq(c.work.items:move("i2", {{}}, unassign).moved, true)
        assert.eq(c.work.items:move("i2", nil, unassign).moved, true)

        -- sprint_id = nil is an absent key, not a null: the sprint is kept
        local kept = {{ to_status = "ready", expected_revision = 7, sprint_id = nil }}
        assert.eq(c.work.items:move("i3", kept).moved, true)
        "#,
        server.uri()
    );
    run_lua(&script).await.unwrap();
}