jamjet-api 0.5.0

JamJet REST API server — control plane for workflow management
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
//! Validated approval API tests.
//!
//! Tests for POST /executions/:id/approve (validation against pending state)
//! and GET /executions/:id/approvals (listing pending + decided approvals).
//! Uses the in-memory backend + dev_mode router (no auth required).

use axum::body::Body;
use axum::http::{Request, StatusCode};
use http_body_util::BodyExt;
use jamjet_agents::InMemoryAgentRegistry;
use jamjet_api::{routes::build_router_with_opts, state::AppState};
use jamjet_audit::{AuditEnricher, NoopAuditBackend};
use jamjet_core::workflow::{ExecutionId, WorkflowExecution, WorkflowStatus};
use jamjet_scheduler::Projector;
use jamjet_state::backend::StateBackend;
use jamjet_state::event::{ApprovalDecision, EventKind};
use jamjet_state::{Event, InMemoryBackend};
use serde_json::Value;
use std::sync::Arc;
use tower::ServiceExt;

// ── Helpers ───────────────────────────────────────────────────────────────────

fn make_state() -> AppState {
    let backend = Arc::new(InMemoryBackend::new());
    let backend_clone = backend.clone();
    let audit: Arc<dyn jamjet_audit::AuditBackend> = Arc::new(NoopAuditBackend);
    let enricher = Arc::new(AuditEnricher::new(Arc::clone(&audit)));
    AppState {
        backend: backend.clone() as Arc<dyn jamjet_state::StateBackend>,
        backend_for_fn: Arc::new(move |_tenant_id: &jamjet_state::TenantId| {
            backend_clone.clone() as Arc<dyn jamjet_state::StateBackend>
        }),
        agents: Arc::new(InMemoryAgentRegistry::new()),
        audit,
        enricher,
        protocols: jamjet_api::state::default_protocol_registry(),
        cron_store: None,
    }
}

/// Create a fresh execution in the backend and return its ID string.
async fn create_execution(backend: &Arc<dyn StateBackend>) -> ExecutionId {
    let execution_id = ExecutionId::new();
    let now = chrono::Utc::now();
    backend
        .create_execution(WorkflowExecution {
            execution_id: execution_id.clone(),
            workflow_id: "test-wf".into(),
            workflow_version: "0.1.0".into(),
            status: WorkflowStatus::Running,
            initial_input: serde_json::json!({}),
            current_state: serde_json::json!({}),
            started_at: now,
            updated_at: now,
            completed_at: None,
            session_type: None,
            parent_execution_id: None,
            segment_number: 0,
        })
        .await
        .expect("create_execution");

    backend
        .append_event(Event::new(
            execution_id.clone(),
            1,
            EventKind::WorkflowStarted {
                workflow_id: "test-wf".into(),
                workflow_version: "0.1.0".into(),
                initial_input: serde_json::json!({}),
            },
        ))
        .await
        .expect("append WorkflowStarted");

    execution_id
}

/// Append a `ToolApprovalRequired` for the given node.
async fn seed_approval_required(
    backend: &Arc<dyn StateBackend>,
    execution_id: &ExecutionId,
    node_id: &str,
) {
    let seq = backend
        .latest_sequence(execution_id)
        .await
        .expect("latest_sequence")
        + 1;
    backend
        .append_event(Event::new(
            execution_id.clone(),
            seq,
            EventKind::ToolApprovalRequired {
                node_id: node_id.into(),
                tool_name: format!("tool_{node_id}"),
                approver: "human".into(),
                context: serde_json::json!({"action": node_id}),
            },
        ))
        .await
        .expect("append ToolApprovalRequired");
}

async fn body_json(body: Body) -> Value {
    let bytes = body.collect().await.unwrap().to_bytes();
    serde_json::from_slice(&bytes).unwrap()
}

fn approve_body(decision: &str, node_id: Option<&str>) -> Body {
    let mut map = serde_json::json!({ "decision": decision });
    if let Some(n) = node_id {
        map["node_id"] = serde_json::json!(n);
    }
    Body::from(serde_json::to_vec(&map).unwrap())
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[tokio::test]
async fn approve_with_no_pending_returns_409() {
    let state = make_state();
    let backend = state.backend.clone();
    let app = build_router_with_opts(state, true);

    let execution_id = create_execution(&backend).await;
    let id_str = execution_id.to_string();

    let resp = app
        .oneshot(
            Request::post(format!("/executions/{id_str}/approve"))
                .header("content-type", "application/json")
                .body(approve_body("approved", None))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(resp.status(), StatusCode::CONFLICT);
    let json = body_json(resp.into_body()).await;
    let error = json["error"].as_str().unwrap_or("");
    assert!(
        error.contains("no pending approval"),
        "expected 'no pending approval' in error, got: {error}"
    );
}

#[tokio::test]
async fn approve_unknown_node_returns_409() {
    let state = make_state();
    let backend = state.backend.clone();
    let app = build_router_with_opts(state, true);

    let execution_id = create_execution(&backend).await;
    seed_approval_required(&backend, &execution_id, "a").await;
    let id_str = execution_id.to_string();

    let resp = app
        .oneshot(
            Request::post(format!("/executions/{id_str}/approve"))
                .header("content-type", "application/json")
                .body(approve_body("approved", Some("zzz")))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(resp.status(), StatusCode::CONFLICT);
    let json = body_json(resp.into_body()).await;
    let error = json["error"].as_str().unwrap_or("");
    assert!(
        error.contains("no pending approval"),
        "expected 'no pending approval' in error for unknown node, got: {error}"
    );
}

#[tokio::test]
async fn approve_single_pending_infers_node() {
    let state = make_state();
    let backend = state.backend.clone();
    let app = build_router_with_opts(state, true);

    let execution_id = create_execution(&backend).await;
    seed_approval_required(&backend, &execution_id, "a").await;
    let id_str = execution_id.to_string();

    let resp = app
        .oneshot(
            Request::post(format!("/executions/{id_str}/approve"))
                .header("content-type", "application/json")
                .body(approve_body("approved", None))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(resp.status(), StatusCode::OK);
    let json = body_json(resp.into_body()).await;
    assert_eq!(json["node_id"], "a", "inferred node_id must be 'a'");
    assert_eq!(json["accepted"], true);

    // Backend must have an ApprovalReceived for node "a".
    let events = backend.get_events(&execution_id).await.unwrap();
    let has_received = events
        .iter()
        .any(|e| matches!(&e.kind, EventKind::ApprovalReceived { node_id, .. } if node_id == "a"));
    assert!(
        has_received,
        "ApprovalReceived for node 'a' must be in event log"
    );
}

#[tokio::test]
async fn approve_multiple_pending_requires_node_id() {
    let state = make_state();
    let backend = state.backend.clone();
    let app = build_router_with_opts(state, true);

    let execution_id = create_execution(&backend).await;
    seed_approval_required(&backend, &execution_id, "a").await;
    seed_approval_required(&backend, &execution_id, "b").await;
    let id_str = execution_id.to_string();

    let resp = app
        .oneshot(
            Request::post(format!("/executions/{id_str}/approve"))
                .header("content-type", "application/json")
                .body(approve_body("approved", None))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
    let json = body_json(resp.into_body()).await;
    let error = json["error"].as_str().unwrap_or("");
    assert!(
        error.contains("specify node_id"),
        "expected 'specify node_id' in error, got: {error}"
    );
}

#[tokio::test]
async fn double_approve_returns_409() {
    let state = make_state();
    let backend = state.backend.clone();
    let router = build_router_with_opts(state, true);

    let execution_id = create_execution(&backend).await;
    seed_approval_required(&backend, &execution_id, "a").await;
    let id_str = execution_id.to_string();

    // First approval — must succeed.
    let resp1 = router
        .clone()
        .oneshot(
            Request::post(format!("/executions/{id_str}/approve"))
                .header("content-type", "application/json")
                .body(approve_body("approved", Some("a")))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp1.status(), StatusCode::OK);

    // Second identical approval — must 409.
    let resp2 = router
        .oneshot(
            Request::post(format!("/executions/{id_str}/approve"))
                .header("content-type", "application/json")
                .body(approve_body("approved", Some("a")))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp2.status(), StatusCode::CONFLICT);
    let json = body_json(resp2.into_body()).await;
    let error = json["error"].as_str().unwrap_or("");
    assert!(
        error.contains("no pending approval"),
        "double-approve error must mention 'no pending approval', got: {error}"
    );
}

#[tokio::test]
async fn invalid_decision_returns_400() {
    let state = make_state();
    let backend = state.backend.clone();
    let app = build_router_with_opts(state, true);

    let execution_id = create_execution(&backend).await;
    seed_approval_required(&backend, &execution_id, "a").await;
    let id_str = execution_id.to_string();

    let resp = app
        .oneshot(
            Request::post(format!("/executions/{id_str}/approve"))
                .header("content-type", "application/json")
                .body(Body::from(
                    serde_json::to_vec(&serde_json::json!({"decision": "maybe"})).unwrap(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
    let json = body_json(resp.into_body()).await;
    let error = json["error"].as_str().unwrap_or("");
    assert!(
        error.contains("unknown decision") || error.contains("maybe"),
        "expected decision error, got: {error}"
    );
}

#[tokio::test]
async fn list_approvals_shows_pending_then_decided() {
    let state = make_state();
    let backend = state.backend.clone();
    let router = build_router_with_opts(state, true);

    let execution_id = create_execution(&backend).await;
    seed_approval_required(&backend, &execution_id, "a").await;
    seed_approval_required(&backend, &execution_id, "b").await;
    let id_str = execution_id.to_string();

    // The endpoint now reads from the async projection.  Run a tick to
    // fold the seeded events into the projection before querying.
    let projector = Projector::new(backend.clone());
    projector.tick().await.expect("projector tick 1");

    // GET /executions/:id/approvals — both pending.
    let resp = router
        .clone()
        .oneshot(
            Request::get(format!("/executions/{id_str}/approvals"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(resp.status(), StatusCode::OK);
    let json = body_json(resp.into_body()).await;
    let pending = json["pending"].as_array().expect("pending must be array");
    assert_eq!(pending.len(), 2, "both nodes must be pending");
    let pending_ids: Vec<&str> = pending
        .iter()
        .map(|p| p["node_id"].as_str().unwrap())
        .collect();
    assert!(pending_ids.contains(&"a"), "node 'a' must be pending");
    assert!(pending_ids.contains(&"b"), "node 'b' must be pending");

    // Each pending entry must have node_id, tool_name, approver.
    for p in pending {
        assert!(p["node_id"].is_string(), "pending entry missing node_id");
        assert!(
            p["tool_name"].is_string(),
            "pending entry missing tool_name"
        );
        assert!(p["approver"].is_string(), "pending entry missing approver");
    }

    // Approve node "a" via the write path (uses get_events for strong consistency).
    let approve_resp = router
        .clone()
        .oneshot(
            Request::post(format!("/executions/{id_str}/approve"))
                .header("content-type", "application/json")
                .body(approve_body("approved", Some("a")))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(approve_resp.status(), StatusCode::OK);

    // Fold the new ApprovalReceived event into the projection.
    projector.tick().await.expect("projector tick 2");

    // GET again — "a" decided, "b" still pending.
    let resp2 = router
        .oneshot(
            Request::get(format!("/executions/{id_str}/approvals"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(resp2.status(), StatusCode::OK);
    let json2 = body_json(resp2.into_body()).await;

    let pending2 = json2["pending"].as_array().expect("pending must be array");
    assert_eq!(pending2.len(), 1, "only node 'b' remains pending");
    assert_eq!(pending2[0]["node_id"], "b");

    let decided = json2["decided"].as_array().expect("decided must be array");
    assert_eq!(decided.len(), 1, "node 'a' must be in decided");
    assert_eq!(decided[0]["node_id"], "a");
    assert_eq!(decided[0]["status"], "approved");
}

#[tokio::test]
async fn approve_on_terminal_execution_returns_409() {
    let state = make_state();
    let backend = state.backend.clone();
    let app = build_router_with_opts(state, true);

    let execution_id = create_execution(&backend).await;
    seed_approval_required(&backend, &execution_id, "a").await;
    backend
        .update_execution_status(&execution_id, WorkflowStatus::Cancelled)
        .await
        .expect("cancel execution");
    let id_str = execution_id.to_string();

    let resp = app
        .oneshot(
            Request::post(format!("/executions/{id_str}/approve"))
                .header("content-type", "application/json")
                .body(approve_body("approved", Some("a")))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(resp.status(), StatusCode::CONFLICT);
    let json = body_json(resp.into_body()).await;
    let error = json["error"].as_str().unwrap_or("");
    assert!(
        error.contains("Cancelled"),
        "expected terminal status in error, got: {error}"
    );
}

/// Verify the projection-driven GET /executions/:id/approvals endpoint:
/// seed approval events, run a projector tick, assert the response shape
/// matches what the events imply (node, latest status, user, comment, tool_name).
/// Also covers a Rejected outcome and the "pending" pending-metadata fields
/// (tool_name, approver) that the legacy approvals_view always returned.
#[tokio::test]
async fn projection_driven_approvals_endpoint() {
    let state = make_state();
    let backend = state.backend.clone();
    let app = build_router_with_opts(state, true);

    let execution_id = create_execution(&backend).await;

    // Seed: node "gate" gets ToolApprovalRequired, then ApprovalReceived(Rejected).
    // node "check" stays pending (only ToolApprovalRequired, no decision yet).
    let seq = backend.latest_sequence(&execution_id).await.unwrap();
    backend
        .append_event(Event::new(
            execution_id.clone(),
            seq + 1,
            EventKind::ToolApprovalRequired {
                node_id: "gate".into(),
                tool_name: "run_payment".into(),
                approver: "compliance".into(),
                context: serde_json::json!({"amount": 5000}),
            },
        ))
        .await
        .unwrap();
    backend
        .append_event(Event::new(
            execution_id.clone(),
            seq + 2,
            EventKind::ApprovalReceived {
                node_id: "gate".into(),
                user_id: "carol".into(),
                decision: ApprovalDecision::Rejected,
                comment: Some("exceeds limit".into()),
                state_patch: None,
            },
        ))
        .await
        .unwrap();
    backend
        .append_event(Event::new(
            execution_id.clone(),
            seq + 3,
            EventKind::ToolApprovalRequired {
                node_id: "check".into(),
                tool_name: "verify_id".into(),
                approver: "human".into(),
                context: serde_json::json!({}),
            },
        ))
        .await
        .unwrap();

    // Run a projector tick to fold events into the projection.
    let projector = Projector::new(backend.clone());
    projector.tick().await.expect("projector tick must succeed");

    // Call the endpoint — now served from the projection.
    let id_str = execution_id.to_string();
    let resp = app
        .oneshot(
            Request::get(format!("/executions/{id_str}/approvals"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(resp.status(), StatusCode::OK);
    let json = body_json(resp.into_body()).await;

    // "check" is pending; "gate" is decided (rejected).
    let pending = json["pending"]
        .as_array()
        .expect("pending must be an array");
    assert_eq!(pending.len(), 1, "one node still pending");
    let p = &pending[0];
    assert_eq!(p["node_id"], "check");
    assert_eq!(
        p["tool_name"], "verify_id",
        "tool_name must be preserved in projection"
    );
    assert_eq!(
        p["approver"], "human",
        "approver must be preserved in projection"
    );
    assert!(p["sequence"].is_number(), "sequence must be present");

    let decided = json["decided"]
        .as_array()
        .expect("decided must be an array");
    assert_eq!(decided.len(), 1, "one node decided");
    let d = &decided[0];
    assert_eq!(d["node_id"], "gate");
    assert_eq!(d["status"], "rejected");
    assert_eq!(d["user_id"], "carol");
    assert_eq!(d["comment"], "exceeds limit");
    assert!(d["sequence"].is_number(), "sequence must be present");

    // Second tick with no new events: idempotent — response unchanged.
    let resp2 = Projector::new(backend.clone())
        .tick()
        .await
        .expect("second tick");
    assert_eq!(resp2, 0, "second tick with no new events applies zero rows");
}

/// Finding A + B — event-log fallback: an execution that has approval events but
/// whose projector tick has never run (projection table is empty) must NOT return
/// an empty list.  The endpoint falls back to event-log replay.
///
/// This is the same code path exercised by:
/// - Terminal executions (projector only scans Running status; terminal execs
///   never get a row in proj_approvals).
/// - Non-default-tenant executions (the projector writes proj_approvals with
///   tenant_id='default'; reading via backend_for(&tenant) returns nothing).
#[tokio::test]
async fn list_approvals_fallback_when_not_yet_projected() {
    let state = make_state();
    let backend = state.backend.clone();
    let app = build_router_with_opts(state, true);

    let execution_id = create_execution(&backend).await;
    seed_approval_required(&backend, &execution_id, "gate").await;
    let id_str = execution_id.to_string();

    // No projector tick: proj_approvals is empty for this execution.
    // The endpoint must fall back to event-log replay and return the pending approval.
    let resp = app
        .oneshot(
            Request::get(format!("/executions/{id_str}/approvals"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(resp.status(), StatusCode::OK);
    let json = body_json(resp.into_body()).await;
    let pending = json["pending"]
        .as_array()
        .expect("pending must be an array");
    assert_eq!(
        pending.len(),
        1,
        "endpoint must return the pending approval via fallback when projection is empty"
    );
    assert_eq!(
        pending[0]["node_id"], "gate",
        "fallback must identify the correct node"
    );
    assert!(
        pending[0]["tool_name"].is_string(),
        "fallback must return tool_name from event log"
    );
    let decided = json["decided"]
        .as_array()
        .expect("decided must be an array");
    assert_eq!(decided.len(), 0, "no decided approvals yet");
}

/// Finding A + B — projection fast path: once the projector has ticked, the
/// endpoint serves from proj_approvals (not the event log).
/// Both paths return the same JSON shape, so the response is byte-compatible.
#[tokio::test]
async fn list_approvals_fast_path_from_projection_after_tick() {
    let state = make_state();
    let backend = state.backend.clone();
    let app = build_router_with_opts(state, true);

    let execution_id = create_execution(&backend).await;
    seed_approval_required(&backend, &execution_id, "check").await;
    let id_str = execution_id.to_string();

    // Run a projector tick to populate proj_approvals.
    let projector = Projector::new(backend.clone());
    projector.tick().await.expect("projector tick");

    // Projection is now populated: endpoint must serve from it (non-empty fast path).
    let resp = app
        .oneshot(
            Request::get(format!("/executions/{id_str}/approvals"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(resp.status(), StatusCode::OK);
    let json = body_json(resp.into_body()).await;
    let pending = json["pending"]
        .as_array()
        .expect("pending must be an array");
    assert_eq!(pending.len(), 1, "one pending approval from projection");
    assert_eq!(pending[0]["node_id"], "check");
    assert_eq!(
        pending[0]["tool_name"], "tool_check",
        "projection preserves tool_name from ToolApprovalRequired"
    );
    assert_eq!(
        pending[0]["approver"], "human",
        "projection preserves approver from ToolApprovalRequired"
    );
    let decided = json["decided"]
        .as_array()
        .expect("decided must be an array");
    assert_eq!(decided.len(), 0, "no decided approvals");
}