tandem-server 0.7.1

HTTP server for Tandem engine APIs
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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
// Copyright (c) 2026 Frumu LTD
// Licensed under the Business Source License 1.1

//! Governance-focused Slack ingress tests for the TAN-762..TAN-766 stack:
//! multi-channel connection routing, department intersection scoping,
//! department-binding enrollment, sender discovery, and per-connection
//! binding verification. Shares the signed-events harness (mock Slack API,
//! signed requests, governed identity seeding) with `slack_events`.

use super::*;

use super::slack_events::{
    configure_slack_events, configure_slack_events_for_installation,
    install_governed_slack_provider, seed_governed_slack_identity,
    seed_governed_slack_identity_for_user, signed_slack_event_request,
    signed_slack_event_request_for_installation, start_slack_api_mock, wait_for_posts,
    wait_for_slack_tasks, ORG_ID, SIGNING_SECRET, SLACK_APP, SLACK_CHANNEL, SLACK_TEAM, SLACK_USER,
    WORKSPACE_ID,
};
use std::sync::atomic::Ordering;
use tandem_types::{
    AccessPermission, DataClass, OrganizationUnit, OrganizationUnitAccessGrant,
    OrganizationUnitKind, PrincipalRef, ResourceKind, ResourceRef, TenantContext,
};

/// Configure two per-channel connections (TAN-763) sharing one installation:
/// installation identity, signing secret, bot token, tenant, and model come
/// from the top level; each connection sets its own channel + allowlist.
async fn configure_slack_event_connections(
    state: &AppState,
    api_base_url: &str,
    sales_channel: &str,
    sales_user: &str,
    eng_channel: &str,
    eng_user: &str,
) {
    state
        .config
        .patch_project(json!({
            "channels": {
                "slack": {
                    "signing_secret": SIGNING_SECRET,
                    "events_enabled": true,
                    "bot_token": "xoxb-governed-test",
                    "team_id": SLACK_TEAM,
                    "app_id": SLACK_APP,
                    "api_base_url": api_base_url,
                    "model_provider_id": "governed-slack-test",
                    "model_id": "governed-slack-test-1",
                    "security_profile": "trusted_team",
                    "tenant": {
                        "org_id": ORG_ID,
                        "workspace_id": WORKSPACE_ID
                    },
                    "connections": [
                        {
                            "channel_id": sales_channel,
                            "allowed_users": [sales_user]
                        },
                        {
                            "channel_id": eng_channel,
                            "allowed_users": [eng_user]
                        }
                    ]
                }
            }
        }))
        .await
        .expect("configure Slack Events connections");
}

/// Seed only the engineering department unit + grant — no memberships. Used
/// by TAN-765 tests where the membership must come from enrollment.
async fn seed_engineering_unit_and_grant(state: &AppState, tool_patterns: &[&str]) {
    let now_ms = crate::now_ms();
    let tenant = TenantContext::explicit(ORG_ID, WORKSPACE_ID, None);
    let admin = PrincipalRef::human_user("admin");
    let department = OrganizationUnit::active(
        "engineering",
        tenant.clone(),
        "Engineering",
        OrganizationUnitKind::Department,
        admin,
        now_ms,
    )
    .with_taxonomy_id("department");
    let grant = OrganizationUnitAccessGrant::active(
        "engineering-read",
        tenant,
        department.principal_ref(),
        ResourceRef::new(ORG_ID, WORKSPACE_ID, ResourceKind::Workspace, WORKSPACE_ID),
        now_ms,
    )
    .with_permissions(vec![AccessPermission::Read, AccessPermission::Execute])
    .with_data_classes(vec![DataClass::Internal, DataClass::SourceCode])
    .with_tool_patterns(
        tool_patterns
            .iter()
            .map(|pattern| (*pattern).to_string())
            .collect(),
    );
    state
        .enterprise
        .org_units
        .write()
        .await
        .insert(department.unit_id.clone(), department);
    state
        .enterprise
        .org_unit_access_grants
        .write()
        .await
        .insert(grant.grant_id.clone(), grant);
}

#[tokio::test]
async fn multi_connection_events_route_to_their_own_channels() {
    const SALES_CHANNEL: &str = "C_SALES";
    const ENG_CHANNEL: &str = "C_ENG";
    const SALES_USER: &str = "U_SALES";
    const ENG_USER: &str = "U_ENG";

    let state = test_state().await;
    let (api_base_url, slack_mock, mock_task) = start_slack_api_mock().await;
    configure_slack_event_connections(
        &state,
        &api_base_url,
        SALES_CHANNEL,
        SALES_USER,
        ENG_CHANNEL,
        ENG_USER,
    )
    .await;
    seed_governed_slack_identity_for_user(&state, SALES_USER, &["mcp.crm.*"]).await;
    seed_governed_slack_identity_for_user(&state, ENG_USER, &["mcp.github.*"]).await;
    let provider = install_governed_slack_provider(&state, 0).await;
    let app = app_router(state.clone());
    let request_timestamp = chrono::Utc::now().timestamp();

    // Each connection accepts its own channel's events and replies in place.
    let sales_event = signed_slack_event_request_for_installation(
        "Ev-conn-sales-1",
        SALES_USER,
        SALES_CHANNEL,
        Some(SLACK_TEAM),
        Some(SLACK_APP),
        "1800000000.310001",
        None,
        request_timestamp,
    );
    let response = app
        .clone()
        .oneshot(sales_event)
        .await
        .expect("sales response");
    assert_eq!(response.status(), StatusCode::OK);
    wait_for_posts(&slack_mock, 1).await;

    let eng_event = signed_slack_event_request_for_installation(
        "Ev-conn-eng-1",
        ENG_USER,
        ENG_CHANNEL,
        Some(SLACK_TEAM),
        Some(SLACK_APP),
        "1800000000.320001",
        None,
        request_timestamp,
    );
    let response = app.clone().oneshot(eng_event).await.expect("eng response");
    assert_eq!(response.status(), StatusCode::OK);
    wait_for_posts(&slack_mock, 2).await;
    wait_for_slack_tasks(&state).await;
    assert_eq!(provider.calls.load(Ordering::SeqCst), 2);

    let posts = slack_mock.posts.lock().await;
    assert_eq!(posts[0]["channel"], SALES_CHANNEL);
    assert_eq!(posts[1]["channel"], ENG_CHANNEL);
    drop(posts);

    let mut scope_ids = state
        .storage
        .list_sessions()
        .await
        .into_iter()
        .filter_map(|session| {
            session
                .source_metadata
                .as_ref()
                .and_then(|metadata| metadata.get("scope_id"))
                .and_then(Value::as_str)
                .map(str::to_string)
        })
        .collect::<Vec<_>>();
    scope_ids.sort();
    assert_eq!(
        scope_ids,
        vec![
            format!("thread:{SLACK_TEAM}:{SLACK_APP}:{ENG_CHANNEL}:1800000000.320001"),
            format!("thread:{SLACK_TEAM}:{SLACK_APP}:{SALES_CHANNEL}:1800000000.310001"),
        ],
        "each connection keys its own session scope"
    );

    // A sender authorized on one connection is NOT authorized on the other:
    // per-connection allowlists must not pool.
    let cross_connection = signed_slack_event_request_for_installation(
        "Ev-conn-cross-1",
        SALES_USER,
        ENG_CHANNEL,
        Some(SLACK_TEAM),
        Some(SLACK_APP),
        "1800000000.330001",
        None,
        request_timestamp,
    );
    let response = app
        .clone()
        .oneshot(cross_connection)
        .await
        .expect("cross-connection response");
    assert_eq!(response.status(), StatusCode::FORBIDDEN);

    // A channel no connection claims stays rejected.
    let unknown_channel = signed_slack_event_request_for_installation(
        "Ev-conn-unknown-1",
        SALES_USER,
        "C_UNCONFIGURED",
        Some(SLACK_TEAM),
        Some(SLACK_APP),
        "1800000000.340001",
        None,
        request_timestamp,
    );
    let response = app
        .oneshot(unknown_channel)
        .await
        .expect("unknown channel response");
    assert_eq!(response.status(), StatusCode::FORBIDDEN);

    tokio::time::sleep(Duration::from_millis(100)).await;
    assert_eq!(
        provider.calls.load(Ordering::SeqCst),
        2,
        "denied events must not dispatch model runs"
    );
    assert_eq!(slack_mock.posts.lock().await.len(), 2);
    mock_task.abort();
}

async fn configure_slack_events_with_bound_departments(
    state: &AppState,
    api_base_url: &str,
    org_units: &[&str],
) {
    state
        .config
        .patch_project(json!({
            "channels": {
                "slack": {
                    "signing_secret": SIGNING_SECRET,
                    "events_enabled": true,
                    "bot_token": "xoxb-governed-test",
                    "channel_id": SLACK_CHANNEL,
                    "team_id": SLACK_TEAM,
                    "app_id": SLACK_APP,
                    "allowed_users": [SLACK_USER],
                    "api_base_url": api_base_url,
                    "model_provider_id": "governed-slack-test",
                    "model_id": "governed-slack-test-1",
                    "security_profile": "trusted_team",
                    "org_units": org_units,
                    "tenant": {
                        "org_id": ORG_ID,
                        "workspace_id": WORKSPACE_ID
                    }
                }
            }
        }))
        .await
        .expect("configure department-bound Slack Events");
}

#[tokio::test]
async fn department_bound_connection_narrows_run_authority_to_intersection() {
    let state = test_state().await;
    let (api_base_url, slack_mock, mock_task) = start_slack_api_mock().await;
    // The user holds department/engineering AND role/engineer; the channel
    // binds only the department, so the run must narrow to it.
    configure_slack_events_with_bound_departments(
        &state,
        &api_base_url,
        &["department/engineering"],
    )
    .await;
    seed_governed_slack_identity(&state).await;
    let provider = install_governed_slack_provider(&state, 0).await;
    let app = app_router(state.clone());
    let request_timestamp = chrono::Utc::now().timestamp();

    let event = signed_slack_event_request(
        "Ev-dept-narrow-1",
        SLACK_USER,
        "1800000000.410001",
        None,
        request_timestamp,
    );
    let response = app.oneshot(event).await.expect("narrowed response");
    assert_eq!(response.status(), StatusCode::OK);
    wait_for_posts(&slack_mock, 1).await;
    wait_for_slack_tasks(&state).await;
    assert_eq!(provider.calls.load(Ordering::SeqCst), 1);

    let sessions = state.storage.list_sessions().await;
    assert_eq!(sessions.len(), 1);
    let verified = sessions[0]
        .verified_tenant_context
        .as_ref()
        .expect("verified channel context");
    assert_eq!(
        verified.org_units,
        vec!["department/engineering"],
        "run authority must narrow to the intersection"
    );
    assert!(
        verified.roles.is_empty(),
        "role units outside the channel binding must not survive the intersection"
    );
    assert_eq!(
        verified.capabilities,
        vec!["mcp.github.*"],
        "grants sourced from the bound department are kept"
    );

    let audit_tenant = TenantContext::explicit(ORG_ID, WORKSPACE_ID, None);
    let audit = crate::audit::load_protected_audit_events_for_tenant(&state, &audit_tenant).await;
    let run_started = audit
        .iter()
        .find(|event| event.event_type == "channel.slack.run.started")
        .expect("run.started audit event");
    assert_eq!(
        run_started.payload.get("channel_org_units"),
        Some(&json!(["department/engineering"])),
        "receipts must show the channel's binding alongside the effective units"
    );
    assert_eq!(
        run_started.payload.get("org_units"),
        Some(&json!(["department/engineering"]))
    );
    mock_task.abort();
}

#[tokio::test]
async fn enrollment_code_with_department_binding_enables_governed_run() {
    let state = test_state().await;
    let (api_base_url, slack_mock, mock_task) = start_slack_api_mock().await;
    configure_slack_events(&state, &api_base_url).await;
    // Department + grant exist, but the sender has NO membership yet.
    seed_engineering_unit_and_grant(&state, &["mcp.github.*"]).await;
    let provider = install_governed_slack_provider(&state, 0).await;
    let app = app_router(state.clone());
    let request_timestamp = chrono::Utc::now().timestamp();

    // Unmapped sender fails closed.
    let before = signed_slack_event_request(
        "Ev-enroll-before",
        SLACK_USER,
        "1800000000.510001",
        None,
        request_timestamp,
    );
    let response = app
        .clone()
        .oneshot(before)
        .await
        .expect("pre-enrollment response");
    assert_eq!(response.status(), StatusCode::FORBIDDEN);
    assert_eq!(provider.calls.load(Ordering::SeqCst), 0);

    // Operator issues a pairing code carrying the department binding; the
    // identity redeems it (TAN-765). An unknown unit fails at issue time.
    let principal = format!("channel:slack:{SLACK_TEAM}:{SLACK_APP}:{SLACK_USER}");
    assert!(
        state
            .issue_channel_enrollment_code(
                "slack",
                principal.clone(),
                crate::app::state::channel_user_capabilities::StoredCommandTier::Approve,
                Some(60_000),
                Some("operator".to_string()),
                None,
                vec!["department/nonexistent".to_string()],
                None,
            )
            .await
            .is_err(),
        "unknown org unit must fail at issue time"
    );
    let code = state
        .issue_channel_enrollment_code(
            "slack",
            principal.clone(),
            crate::app::state::channel_user_capabilities::StoredCommandTier::Approve,
            Some(60_000),
            Some("operator".to_string()),
            None,
            vec!["department/engineering".to_string()],
            None,
        )
        .await
        .expect("issue department-bound enrollment code");
    let capability = state
        .confirm_channel_enrollment_code(&code.code, None)
        .await
        .expect("confirm department-bound enrollment code");
    assert_eq!(capability.org_units, vec!["department/engineering"]);
    assert!(
        state
            .enterprise
            .org_unit_memberships
            .read()
            .await
            .values()
            .any(|membership| {
                membership.member.id == principal
                    && membership.unit.id == "department/engineering"
                    && membership.state.is_active()
            }),
        "confirming the code must establish the org-unit membership"
    );

    // The same sender now runs governed, scoped to the enrolled department.
    let after = signed_slack_event_request(
        "Ev-enroll-after",
        SLACK_USER,
        "1800000000.520001",
        None,
        request_timestamp,
    );
    let response = app.oneshot(after).await.expect("post-enrollment response");
    assert_eq!(response.status(), StatusCode::OK);
    wait_for_posts(&slack_mock, 1).await;
    wait_for_slack_tasks(&state).await;
    assert_eq!(provider.calls.load(Ordering::SeqCst), 1);
    let sessions = state.storage.list_sessions().await;
    let verified = sessions[0]
        .verified_tenant_context
        .as_ref()
        .expect("verified channel context");
    assert_eq!(verified.org_units, vec!["department/engineering"]);
    assert_eq!(verified.capabilities, vec!["mcp.github.*"]);
    mock_task.abort();
}

#[tokio::test]
async fn slack_senders_endpoint_surfaces_mapped_and_unmapped_identities() {
    const UNMAPPED_USER: &str = "U_UNMAPPED";

    let state = test_state().await;
    let (api_base_url, slack_mock, mock_task) = start_slack_api_mock().await;
    configure_slack_events_for_installation(
        &state,
        &api_base_url,
        SLACK_TEAM,
        SLACK_APP,
        SLACK_CHANNEL,
        &[SLACK_USER, UNMAPPED_USER],
    )
    .await;
    seed_governed_slack_identity(&state).await;
    let _provider = install_governed_slack_provider(&state, 0).await;
    let app = app_router(state.clone());
    let request_timestamp = chrono::Utc::now().timestamp();

    // Mapped sender produces an accepted run; unmapped sender an audited
    // fail-closed denial.
    let accepted = signed_slack_event_request(
        "Ev-senders-accepted",
        SLACK_USER,
        "1800000000.610001",
        None,
        request_timestamp,
    );
    let response = app
        .clone()
        .oneshot(accepted)
        .await
        .expect("accepted response");
    assert_eq!(response.status(), StatusCode::OK);
    wait_for_posts(&slack_mock, 1).await;
    wait_for_slack_tasks(&state).await;

    let denied = signed_slack_event_request(
        "Ev-senders-denied",
        UNMAPPED_USER,
        "1800000000.620001",
        None,
        request_timestamp,
    );
    let response = app.clone().oneshot(denied).await.expect("denied response");
    assert_eq!(response.status(), StatusCode::FORBIDDEN);

    let request = Request::builder()
        .method("GET")
        .uri("/channels/slack/senders")
        .body(Body::empty())
        .expect("senders request");
    let response = app.oneshot(request).await.expect("senders response");
    assert_eq!(response.status(), StatusCode::OK);
    let body = axum::body::to_bytes(response.into_body(), usize::MAX)
        .await
        .expect("senders body");
    let payload: Value = serde_json::from_slice(&body).expect("senders json");
    let senders = payload
        .get("senders")
        .and_then(Value::as_array)
        .expect("senders array");

    let mapped = senders
        .iter()
        .find(|row| row.get("user_id").and_then(Value::as_str) == Some(SLACK_USER))
        .expect("mapped sender present");
    assert_eq!(mapped.get("mapped"), Some(&json!(true)));
    assert_eq!(
        mapped.get("principal").and_then(Value::as_str),
        Some(format!("channel:slack:{SLACK_TEAM}:{SLACK_APP}:{SLACK_USER}").as_str())
    );
    assert!(mapped
        .get("org_units")
        .and_then(Value::as_array)
        .expect("mapped org units")
        .iter()
        .any(|unit| unit == "department/engineering"));
    assert!(
        mapped
            .get("accepted_count")
            .and_then(Value::as_u64)
            .unwrap_or_default()
            >= 1
    );

    let unmapped = senders
        .iter()
        .find(|row| row.get("user_id").and_then(Value::as_str) == Some(UNMAPPED_USER))
        .expect("unmapped sender present — denials must be visible");
    assert_eq!(unmapped.get("mapped"), Some(&json!(false)));
    assert!(
        unmapped
            .get("denied_count")
            .and_then(Value::as_u64)
            .unwrap_or_default()
            >= 1
    );
    assert!(
        unmapped
            .get("last_denial_reason")
            .and_then(Value::as_str)
            .unwrap_or_default()
            .contains("organization-unit membership"),
        "unmapped denial reason must explain the fail-closed state"
    );
    mock_task.abort();
}

#[tokio::test]
async fn denials_are_audited_under_a_connection_bound_tenant() {
    let state = test_state().await;
    let (api_base_url, _slack_mock, mock_task) = start_slack_api_mock().await;
    // The tenant lives ONLY on the connection entry — no top-level binding.
    // Fail-closed denials must still land in the protected audit ledger so
    // the unmapped sender stays discoverable via /channels/slack/senders.
    state
        .config
        .patch_project(json!({
            "channels": {
                "slack": {
                    "signing_secret": SIGNING_SECRET,
                    "events_enabled": true,
                    "bot_token": "xoxb-governed-test",
                    "team_id": SLACK_TEAM,
                    "app_id": SLACK_APP,
                    "api_base_url": api_base_url,
                    "model_provider_id": "governed-slack-test",
                    "model_id": "governed-slack-test-1",
                    "security_profile": "trusted_team",
                    "connections": [
                        {
                            "channel_id": SLACK_CHANNEL,
                            "allowed_users": [SLACK_USER],
                            "tenant": {
                                "org_id": ORG_ID,
                                "workspace_id": WORKSPACE_ID
                            }
                        }
                    ]
                }
            }
        }))
        .await
        .expect("configure connection-tenant Slack Events");
    let provider = install_governed_slack_provider(&state, 0).await;
    let app = app_router(state.clone());
    let request_timestamp = chrono::Utc::now().timestamp();

    // Allowlisted but holding no org-unit membership: denied in governed
    // identity resolution — the exact denial an operator must be able to see.
    let denied = signed_slack_event_request(
        "Ev-conn-tenant-denied",
        SLACK_USER,
        "1800000000.710001",
        None,
        request_timestamp,
    );
    let response = app.clone().oneshot(denied).await.expect("denied response");
    assert_eq!(response.status(), StatusCode::FORBIDDEN);
    assert_eq!(provider.calls.load(Ordering::SeqCst), 0);

    let audit_tenant = TenantContext::explicit(ORG_ID, WORKSPACE_ID, None);
    let audit = crate::audit::load_protected_audit_events_for_tenant(&state, &audit_tenant).await;
    assert!(
        audit
            .iter()
            .any(|event| event.event_type == "channel.slack.ingress.denied"),
        "the denial must be audited under the connection's bound tenant"
    );

    let request = Request::builder()
        .method("GET")
        .uri("/channels/slack/senders")
        .body(Body::empty())
        .expect("senders request");
    let response = app.oneshot(request).await.expect("senders response");
    assert_eq!(response.status(), StatusCode::OK);
    let body = axum::body::to_bytes(response.into_body(), usize::MAX)
        .await
        .expect("senders body");
    let payload: Value = serde_json::from_slice(&body).expect("senders json");
    let row = payload
        .get("senders")
        .and_then(Value::as_array)
        .expect("senders array")
        .iter()
        .find(|row| row.get("user_id").and_then(Value::as_str) == Some(SLACK_USER))
        .cloned()
        .expect("denied sender must be discoverable with a connection-only tenant");
    assert_eq!(row.get("mapped"), Some(&json!(false)));
    assert!(
        row.get("denied_count")
            .and_then(Value::as_u64)
            .unwrap_or_default()
            >= 1
    );
    mock_task.abort();
}

#[tokio::test]
async fn sender_mapping_is_computed_against_each_channels_department_binding() {
    const SALES_CHANNEL: &str = "C_SALES_MAP";
    const ENG_CHANNEL: &str = "C_ENG_MAP";

    let state = test_state().await;
    let (api_base_url, slack_mock, mock_task) = start_slack_api_mock().await;
    state
        .config
        .patch_project(json!({
            "channels": {
                "slack": {
                    "signing_secret": SIGNING_SECRET,
                    "events_enabled": true,
                    "bot_token": "xoxb-governed-test",
                    "team_id": SLACK_TEAM,
                    "app_id": SLACK_APP,
                    "api_base_url": api_base_url,
                    "model_provider_id": "governed-slack-test",
                    "model_id": "governed-slack-test-1",
                    "security_profile": "trusted_team",
                    "tenant": {
                        "org_id": ORG_ID,
                        "workspace_id": WORKSPACE_ID
                    },
                    "connections": [
                        {
                            "channel_id": ENG_CHANNEL,
                            "allowed_users": [SLACK_USER],
                            "org_units": ["engineering"]
                        },
                        {
                            "channel_id": SALES_CHANNEL,
                            "allowed_users": [SLACK_USER],
                            "org_units": ["sales"]
                        }
                    ]
                }
            }
        }))
        .await
        .expect("configure department-bound connections");
    // The sender holds an engineering membership only.
    seed_governed_slack_identity(&state).await;
    let provider = install_governed_slack_provider(&state, 0).await;
    let app = app_router(state.clone());
    let request_timestamp = chrono::Utc::now().timestamp();

    // The engineering-bound channel accepts the engineering member…
    let accepted = signed_slack_event_request_for_installation(
        "Ev-perch-eng",
        SLACK_USER,
        ENG_CHANNEL,
        Some(SLACK_TEAM),
        Some(SLACK_APP),
        "1800000000.810001",
        None,
        request_timestamp,
    );
    let response = app.clone().oneshot(accepted).await.expect("eng response");
    assert_eq!(response.status(), StatusCode::OK);
    wait_for_posts(&slack_mock, 1).await;
    wait_for_slack_tasks(&state).await;
    assert_eq!(provider.calls.load(Ordering::SeqCst), 1);

    // …and the sales-bound channel fails the same sender closed.
    let denied = signed_slack_event_request_for_installation(
        "Ev-perch-sales",
        SLACK_USER,
        SALES_CHANNEL,
        Some(SLACK_TEAM),
        Some(SLACK_APP),
        "1800000000.820001",
        None,
        request_timestamp,
    );
    let response = app.clone().oneshot(denied).await.expect("sales response");
    assert_eq!(response.status(), StatusCode::FORBIDDEN);

    // Sender discovery must not let the engineering membership mask the
    // sales-channel gap: mapped is per observed channel, not tenant-wide.
    let request = Request::builder()
        .method("GET")
        .uri("/channels/slack/senders")
        .body(Body::empty())
        .expect("senders request");
    let response = app.oneshot(request).await.expect("senders response");
    assert_eq!(response.status(), StatusCode::OK);
    let body = axum::body::to_bytes(response.into_body(), usize::MAX)
        .await
        .expect("senders body");
    let payload: Value = serde_json::from_slice(&body).expect("senders json");
    let row = payload
        .get("senders")
        .and_then(Value::as_array)
        .expect("senders array")
        .iter()
        .find(|row| row.get("user_id").and_then(Value::as_str) == Some(SLACK_USER))
        .cloned()
        .expect("sender present");
    assert_eq!(
        row.get("mapped"),
        Some(&json!(false)),
        "an engineering membership must not mask the sales-channel denial"
    );
    assert!(
        row.get("last_denial_reason")
            .and_then(Value::as_str)
            .unwrap_or_default()
            .contains("do not intersect"),
        "the intersection denial reason must stay visible"
    );
    let access = row
        .get("channel_access")
        .and_then(Value::as_array)
        .cloned()
        .expect("channel_access rows");
    let by_channel = |channel: &str| {
        access
            .iter()
            .find(|entry| entry.get("channel_id").and_then(Value::as_str) == Some(channel))
            .cloned()
            .unwrap_or_else(|| panic!("channel_access row for {channel}"))
    };
    let sales = by_channel(SALES_CHANNEL);
    assert_eq!(sales.get("mapped"), Some(&json!(false)));
    assert_eq!(sales.get("bound_org_units"), Some(&json!(["sales"])));
    assert_eq!(sales.get("configured"), Some(&json!(true)));
    let eng = by_channel(ENG_CHANNEL);
    assert_eq!(eng.get("mapped"), Some(&json!(true)));
    assert_eq!(eng.get("bound_org_units"), Some(&json!(["engineering"])));
    mock_task.abort();
}

#[tokio::test]
async fn slack_verify_reports_per_connection_binding_state() {
    let state = test_state().await;
    let (api_base_url, slack_mock, mock_task) = start_slack_api_mock().await;
    configure_slack_events(&state, &api_base_url).await;
    let app = app_router(state.clone());

    // Healthy binding: token authenticates and belongs to the configured
    // team + app.
    let request = Request::builder()
        .method("POST")
        .uri("/channels/slack/verify")
        .header("content-type", "application/json")
        .body(Body::from("{}"))
        .expect("verify request");
    let response = app.clone().oneshot(request).await.expect("verify response");
    assert_eq!(response.status(), StatusCode::OK);
    let body = axum::body::to_bytes(response.into_body(), usize::MAX)
        .await
        .expect("verify body");
    let payload: Value = serde_json::from_slice(&body).expect("verify json");
    assert_eq!(payload.get("ok"), Some(&json!(true)));
    let rows = payload
        .get("connections")
        .and_then(Value::as_array)
        .expect("connection rows");
    assert_eq!(rows.len(), 1);
    assert_eq!(rows[0].get("channel_id"), Some(&json!(SLACK_CHANNEL)));
    assert_eq!(rows[0].get("ok"), Some(&json!(true)));
    assert_eq!(rows[0].get("token_ok"), Some(&json!(true)));
    assert_eq!(rows[0].get("team_ok"), Some(&json!(true)));
    assert_eq!(rows[0].get("app_ok"), Some(&json!(true)));

    // Token drifting to another workspace flips the connection (and the
    // aggregate) to not-ok with an explanatory error.
    *slack_mock.auth_team_id.lock().await = "T_OTHER".to_string();
    let request = Request::builder()
        .method("POST")
        .uri("/channels/slack/verify")
        .header("content-type", "application/json")
        .body(Body::from("{}"))
        .expect("verify request");
    let response = app.oneshot(request).await.expect("drifted verify response");
    assert_eq!(response.status(), StatusCode::OK);
    let body = axum::body::to_bytes(response.into_body(), usize::MAX)
        .await
        .expect("drifted verify body");
    let payload: Value = serde_json::from_slice(&body).expect("drifted verify json");
    assert_eq!(payload.get("ok"), Some(&json!(false)));
    let rows = payload
        .get("connections")
        .and_then(Value::as_array)
        .expect("drifted rows");
    assert_eq!(rows[0].get("team_ok"), Some(&json!(false)));
    assert!(rows[0]
        .get("error")
        .and_then(Value::as_str)
        .unwrap_or_default()
        .contains("T_OTHER"));
    mock_task.abort();
}

#[tokio::test]
async fn department_bound_connection_fails_closed_on_disjoint_membership() {
    let state = test_state().await;
    let (api_base_url, slack_mock, mock_task) = start_slack_api_mock().await;
    // The user is an engineering member; the channel binds sales only.
    configure_slack_events_with_bound_departments(&state, &api_base_url, &["department/sales"])
        .await;
    seed_governed_slack_identity(&state).await;
    let provider = install_governed_slack_provider(&state, 0).await;
    let app = app_router(state.clone());
    let request_timestamp = chrono::Utc::now().timestamp();

    let event = signed_slack_event_request(
        "Ev-dept-disjoint-1",
        SLACK_USER,
        "1800000000.420001",
        None,
        request_timestamp,
    );
    let response = app.oneshot(event).await.expect("disjoint response");
    assert_eq!(response.status(), StatusCode::FORBIDDEN);

    tokio::time::sleep(Duration::from_millis(100)).await;
    assert_eq!(
        provider.calls.load(Ordering::SeqCst),
        0,
        "a disjoint membership must never dispatch a model run"
    );
    assert!(slack_mock.posts.lock().await.is_empty());

    let audit_tenant = TenantContext::explicit(ORG_ID, WORKSPACE_ID, None);
    let audit = crate::audit::load_protected_audit_events_for_tenant(&state, &audit_tenant).await;
    let denial = audit
        .iter()
        .find(|event| event.event_type == "channel.slack.ingress.denied")
        .expect("audited fail-closed denial");
    let reason = denial
        .payload
        .get("reason")
        .and_then(Value::as_str)
        .unwrap_or_default();
    assert!(
        reason.contains("department/engineering") && reason.contains("department/sales"),
        "denial must record both intersection inputs, got: {reason}"
    );
    mock_task.abort();
}

/// P1 (PR #1910 review): the HMAC must bind to the claimed installation's
/// secret. A payload claiming installation B, signed with installation A's
/// secret, must be rejected — otherwise one compromised app secret breaks
/// tenant/app isolation for every other configured installation.
#[tokio::test]
async fn slack_event_signature_must_match_the_claimed_installations_secret() {
    const SECRET_A: &str = "secret-app-a";
    const SECRET_B: &str = "secret-app-b";

    let state = test_state().await;
    let (api_base_url, slack_mock, mock_task) = start_slack_api_mock().await;
    state
        .config
        .patch_project(json!({
            "channels": {
                "slack": {
                    "bot_token": "xoxb-governed-test",
                    "events_enabled": true,
                    "api_base_url": api_base_url,
                    "model_provider_id": "governed-slack-test",
                    "model_id": "governed-slack-test-1",
                    "security_profile": "trusted_team",
                    "tenant": { "org_id": ORG_ID, "workspace_id": WORKSPACE_ID },
                    "connections": [
                        {
                            "channel_id": "C_APP_A",
                            "team_id": SLACK_TEAM,
                            "app_id": "A_APP_A",
                            "signing_secret": SECRET_A,
                            "allowed_users": [SLACK_USER]
                        },
                        {
                            "channel_id": SLACK_CHANNEL,
                            "team_id": SLACK_TEAM,
                            "app_id": SLACK_APP,
                            "signing_secret": SECRET_B,
                            "allowed_users": [SLACK_USER]
                        }
                    ]
                }
            }
        }))
        .await
        .expect("configure two-app Slack connections");
    seed_governed_slack_identity(&state).await;
    let provider = install_governed_slack_provider(&state, 0).await;
    let app = app_router(state.clone());
    let request_timestamp = chrono::Utc::now().timestamp();

    let event_body = |event_id: &str| {
        json!({
            "type": "event_callback",
            "event_id": event_id,
            "team_id": SLACK_TEAM,
            "api_app_id": SLACK_APP,
            "event": {
                "type": "message",
                "user": SLACK_USER,
                "channel": SLACK_CHANNEL,
                "text": "What changed for ACME?",
                "ts": "1800000000.710001"
            }
        })
        .to_string()
    };

    // Signed with the OTHER installation's secret: must fail signature
    // verification even though SECRET_A is a configured secret.
    let body = event_body("Ev-cross-secret-1");
    let forged = Request::builder()
        .method("POST")
        .uri("/channels/slack/events")
        .header("content-type", "application/json")
        .header("x-slack-request-timestamp", request_timestamp.to_string())
        .header(
            "x-slack-signature",
            super::slack_events::sign_slack_event(SECRET_A, request_timestamp, body.as_bytes()),
        )
        .body(Body::from(body))
        .expect("cross-secret request");
    let response = app
        .clone()
        .oneshot(forged)
        .await
        .expect("cross-secret response");
    assert_eq!(
        response.status(),
        StatusCode::FORBIDDEN,
        "a payload claiming installation B must not verify against installation A's secret"
    );
    tokio::time::sleep(std::time::Duration::from_millis(50)).await;
    assert_eq!(provider.calls.load(Ordering::SeqCst), 0);

    // Signed with the claimed installation's own secret: accepted end-to-end.
    let body = event_body("Ev-cross-secret-2");
    let genuine = Request::builder()
        .method("POST")
        .uri("/channels/slack/events")
        .header("content-type", "application/json")
        .header("x-slack-request-timestamp", request_timestamp.to_string())
        .header(
            "x-slack-signature",
            super::slack_events::sign_slack_event(SECRET_B, request_timestamp, body.as_bytes()),
        )
        .body(Body::from(body))
        .expect("genuine request");
    let response = app.oneshot(genuine).await.expect("genuine response");
    assert_eq!(response.status(), StatusCode::OK);
    wait_for_posts(&slack_mock, 1).await;
    wait_for_slack_tasks(&state).await;
    assert_eq!(provider.calls.load(Ordering::SeqCst), 1);
    mock_task.abort();
}

/// P1 (PR #1910 review, round 3): a CONFIGURED installation that carries no
/// signing secret must fail closed — never fall back to verifying against the
/// other installations' secrets, or app A's secret could forge events for
/// app B.
#[tokio::test]
async fn slack_event_for_secretless_installation_rejects_other_apps_signatures() {
    const SECRET_A: &str = "secret-app-a";

    let state = test_state().await;
    let (api_base_url, _slack_mock, mock_task) = start_slack_api_mock().await;
    state
        .config
        .patch_project(json!({
            "channels": {
                "slack": {
                    "bot_token": "xoxb-governed-test",
                    "events_enabled": true,
                    "api_base_url": api_base_url,
                    "model_provider_id": "governed-slack-test",
                    "model_id": "governed-slack-test-1",
                    "security_profile": "trusted_team",
                    "tenant": { "org_id": ORG_ID, "workspace_id": WORKSPACE_ID },
                    "connections": [
                        {
                            "channel_id": "C_APP_A",
                            "team_id": SLACK_TEAM,
                            "app_id": "A_APP_A",
                            "signing_secret": SECRET_A,
                            "allowed_users": [SLACK_USER]
                        },
                        {
                            // App B is configured but has NO signing secret.
                            "channel_id": SLACK_CHANNEL,
                            "team_id": SLACK_TEAM,
                            "app_id": SLACK_APP,
                            "allowed_users": [SLACK_USER]
                        }
                    ]
                }
            }
        }))
        .await
        .expect("configure secretless-app Slack connections");
    seed_governed_slack_identity(&state).await;
    let provider = install_governed_slack_provider(&state, 0).await;
    let app = app_router(state.clone());
    let request_timestamp = chrono::Utc::now().timestamp();

    let body = json!({
        "type": "event_callback",
        "event_id": "Ev-secretless-1",
        "team_id": SLACK_TEAM,
        "api_app_id": SLACK_APP,
        "event": {
            "type": "message",
            "user": SLACK_USER,
            "channel": SLACK_CHANNEL,
            "text": "What changed for ACME?",
            "ts": "1800000000.910001"
        }
    })
    .to_string();
    let forged = Request::builder()
        .method("POST")
        .uri("/channels/slack/events")
        .header("content-type", "application/json")
        .header("x-slack-request-timestamp", request_timestamp.to_string())
        .header(
            "x-slack-signature",
            super::slack_events::sign_slack_event(SECRET_A, request_timestamp, body.as_bytes()),
        )
        .body(Body::from(body))
        .expect("secretless-app request");
    let response = app.oneshot(forged).await.expect("secretless-app response");
    assert_eq!(
        response.status(),
        StatusCode::FORBIDDEN,
        "a configured installation without its own secret must not accept another app's signature"
    );
    tokio::time::sleep(std::time::Duration::from_millis(50)).await;
    assert_eq!(provider.calls.load(Ordering::SeqCst), 0);
    mock_task.abort();
}

/// P1 (PR #1910 review, round 3): enrollment org-unit refs must resolve in
/// the sender's tenant. With two tenants sharing a unit name, an unscoped
/// ref is rejected as ambiguous, and a tenant-scoped enrollment creates the
/// membership in exactly that tenant.
#[tokio::test]
async fn enrollment_org_units_resolve_within_the_sender_tenant() {
    use tandem_types::{OrganizationUnit, OrganizationUnitKind};

    let state = test_state().await;
    let now_ms = crate::now_ms();
    let admin = PrincipalRef::human_user("admin");
    let tenant_a = TenantContext::explicit(ORG_ID, WORKSPACE_ID, None);
    let tenant_b = TenantContext::explicit("other-org", "other-ws", None);
    // Both tenants define `department/sales`. Key the registry rows uniquely
    // (production on-disk keys are arbitrary strings, not unit ids).
    for (key, tenant) in [("a:sales", tenant_a.clone()), ("b:sales", tenant_b)] {
        let unit = OrganizationUnit::active(
            "sales",
            tenant,
            "Sales",
            OrganizationUnitKind::Department,
            admin.clone(),
            now_ms,
        )
        .with_taxonomy_id("department");
        state
            .enterprise
            .org_units
            .write()
            .await
            .insert(key.to_string(), unit);
    }
    // Tenant A also has `region/sales`, so the bare ref "sales" is ambiguous
    // even inside the tenant.
    let region = OrganizationUnit::active(
        "sales",
        tenant_a.clone(),
        "Sales Region",
        OrganizationUnitKind::Department,
        admin.clone(),
        now_ms,
    )
    .with_taxonomy_id("region");
    state
        .enterprise
        .org_units
        .write()
        .await
        .insert("a:region-sales".to_string(), region);

    let principal = format!("channel:slack:{SLACK_TEAM}:{SLACK_APP}:{SLACK_USER}");
    let tier = crate::app::state::channel_user_capabilities::StoredCommandTier::Approve;

    // Unscoped ref matching units in two tenants: rejected, not first-match.
    let ambiguous = state
        .issue_channel_enrollment_code(
            "slack",
            principal.clone(),
            tier,
            Some(60_000),
            Some("operator".to_string()),
            None,
            vec!["department/sales".to_string()],
            None,
        )
        .await;
    assert!(
        ambiguous
            .err()
            .map(|error| error.to_string())
            .unwrap_or_default()
            .contains("ambiguous"),
        "an org unit ref matching multiple tenants must be rejected"
    );

    // Even scoped to one tenant, a bare unit id matching several taxonomies
    // (`department/sales` vs `region/sales`) is rejected, not first-match.
    let bare = state
        .issue_channel_enrollment_code(
            "slack",
            principal.clone(),
            tier,
            Some(60_000),
            Some("operator".to_string()),
            None,
            vec!["sales".to_string()],
            Some((ORG_ID.to_string(), WORKSPACE_ID.to_string())),
        )
        .await;
    assert!(
        bare.err()
            .map(|error| error.to_string())
            .unwrap_or_default()
            .contains("ambiguous within the tenant"),
        "a bare ref matching several taxonomies in one tenant must be rejected"
    );

    // Scoped to tenant A with the taxonomy-qualified id: resolves, and
    // redemption creates the membership in tenant A only.
    let code = state
        .issue_channel_enrollment_code(
            "slack",
            principal.clone(),
            tier,
            Some(60_000),
            Some("operator".to_string()),
            None,
            vec!["department/sales".to_string()],
            Some((ORG_ID.to_string(), WORKSPACE_ID.to_string())),
        )
        .await
        .expect("issue tenant-scoped enrollment code");
    assert_eq!(code.tenant_org_id.as_deref(), Some(ORG_ID));
    let capability = state
        .confirm_channel_enrollment_code(&code.code, None)
        .await
        .expect("confirm tenant-scoped enrollment code");
    assert_eq!(
        capability.tenant_org_id.as_deref(),
        Some(ORG_ID),
        "the capability must inherit the code's tenant scope"
    );
    let memberships = state.enterprise.org_unit_memberships.read().await;
    let created = memberships
        .values()
        .filter(|membership| membership.member.id == principal)
        .collect::<Vec<_>>();
    assert_eq!(created.len(), 1, "exactly one membership must be created");
    assert_eq!(created[0].tenant_context.org_id, ORG_ID);
    assert_eq!(created[0].tenant_context.workspace_id, WORKSPACE_ID);
}