durable-workflow 4.28.0

Rust client and worker SDK for Durable Workflow Cloud and self-hosted Server
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
//! Synchronous pre-accept update validators (durable-workflow/.github#234).

use super::*;
use crate::update_validation::{UPDATE_VALIDATION_WORKER_FAILED, UPDATE_VALIDATOR_REJECTED};
use std::sync::atomic::AtomicUsize;

const WORKER: &str = "validator-worker";

static HANDLER_CALLS: AtomicUsize = AtomicUsize::new(0);

fn envelope(arguments: Value) -> Value {
    encode_typed_envelope(
        &AvroValue::from_serialize(&arguments).unwrap(),
        DEFAULT_CODEC,
    )
    .unwrap()
}

pub(super) fn capable_info() -> Value {
    json!({"worker_protocol":{"server_capabilities":{
        "update_validation_tasks":true,
        "synchronous_update_validation":{
            "supported":true,
            "acceptance_boundary":"validator_approved",
            "worker_capability":"update_validation_tasks",
            "workflow_contract_field":"update_validators",
            "task_poll":{
                "strategy":"multiplexed",
                "endpoint":"/worker/workflow-tasks/poll",
                "request_field":"task_kinds",
                "task_kinds":["workflow","update_validation"],
                "response_discriminator":"task.task_kind",
            },
        },
    }}})
}

fn validation_task(id: &str, update_name: &str, amount: u64) -> Value {
    json!({
        "task_kind": "update_validation",
        "update_validation_task_id": id,
        "update_id": id,
        "update_validation_attempt": 1,
        "workflow_id": "order-1",
        "run_id": "run-1",
        "workflow_type": "orders",
        "update_name": update_name,
        "update_arguments": envelope(json!([amount])),
        "payload_codec": DEFAULT_CODEC,
        "task_queue": "rust-workers",
        "lease_owner": WORKER,
        "workflow_arguments": envelope(json!([5])),
        "run_status": "running",
        "history_events": [{"sequence": 1, "event_type": "WorkflowStarted", "payload": {}}],
    })
}

#[derive(Clone, Default)]
struct Order {
    limit: u64,
    context: Option<WorkflowContext>,
}

/// An `orders` workflow whose state holds the start limit, with an `approve`
/// update that counts handler calls.
fn order_worker(client: Client) -> Worker {
    let mut worker = Worker::new(client, "rust-workers").worker_id(WORKER);
    worker.register_replayed_workflow("orders", Order::default, |ctx, input, state| async move {
        let limit = input[0].as_u64().unwrap_or_default();
        let kept = ctx.clone();
        state.update(move |order| {
            order.limit = limit;
            order.context = Some(kept);
        })?;
        ctx.sleep(Duration::from_secs(3600)).await?;
        Ok(Value::Null)
    });
    worker.register_update("orders", "approve", |_ctx, args| async move {
        HANDLER_CALLS.fetch_add(1, Ordering::SeqCst);
        Ok(args)
    });
    worker
}

fn with_limit_validator(mut worker: Worker) -> Worker {
    worker.register_replayed_update_validator(
        "orders",
        "approve",
        |_ctx, order: Arc<Order>, args| {
            let amount = args[0].as_u64().unwrap_or_default();
            if amount > order.limit {
                return Err(UpdateRejection::new(format!(
                    "amount {amount} exceeds the limit {}",
                    order.limit
                ))
                .with_type("LimitExceeded")
                .with_validation_error("amount", "exceeds the order limit"));
            }
            Ok(())
        },
    );
    worker
}

fn offline_client() -> Client {
    Client::new("http://127.0.0.1:9").unwrap()
}

#[tokio::test]
async fn replayed_validator_reads_reconstructed_state_and_rejects_or_approves() {
    let worker = with_limit_validator(order_worker(offline_client()));
    let calls = HANDLER_CALLS.load(Ordering::SeqCst);

    let (reason, rejection) = worker
        .decide_update_validation(validation_task("v1", "approve", 7))
        .await
        .expect_err("an amount above the replayed limit is rejected");
    assert_eq!(reason, UPDATE_VALIDATOR_REJECTED);
    assert_eq!(rejection.message, "amount 7 exceeds the limit 5");
    assert_eq!(rejection.failure_type.as_deref(), Some("LimitExceeded"));
    assert_eq!(
        rejection.validation_errors,
        BTreeMap::from([(
            "amount".to_string(),
            vec!["exceeds the order limit".to_string()]
        )])
    );

    worker
        .decide_update_validation(validation_task("v2", "approve", 3))
        .await
        .expect("an amount within the replayed limit is approved");

    // Validation never runs the update handler.
    assert_eq!(HANDLER_CALLS.load(Ordering::SeqCst), calls);
}

#[tokio::test]
async fn validator_that_schedules_work_is_refused() {
    let mut worker = order_worker(offline_client());
    worker.register_replayed_update_validator(
        "orders",
        "approve",
        |_ctx, order: Arc<Order>, _args| {
            // The reconstructed run's context is reachable through state, but
            // a validator cannot add durable work through it.
            let context = order.context.as_ref().expect("replayed context");
            context.upsert_memo(json!({"approved_by": "validator"}))?;
            Ok(())
        },
    );
    let (reason, rejection) = worker
        .decide_update_validation(validation_task("v1", "approve", 1))
        .await
        .expect_err("scheduling work from a validator is refused");
    assert_eq!(reason, UPDATE_VALIDATOR_REJECTED);
    assert_eq!(
        rejection.failure_type.as_deref(),
        Some("UpdateValidatorCommandRefused")
    );
    assert!(rejection.message.contains("read-only"), "{rejection}");
}

#[tokio::test]
async fn snapshot_validator_sees_history_and_arguments_without_state() {
    let mut worker = order_worker(offline_client());
    worker.register_update_validator("orders", "approve", |ctx, args| {
        assert_eq!(ctx.history_events().len(), 1);
        assert_eq!(ctx.workflow_input(), &json!([5]));
        if args == json!([0]) {
            return Err(UpdateRejection::new("amount must be positive"));
        }
        Ok(())
    });
    let (reason, rejection) = worker
        .decide_update_validation(validation_task("v1", "approve", 0))
        .await
        .unwrap_err();
    assert_eq!(reason, UPDATE_VALIDATOR_REJECTED);
    assert_eq!(rejection.message, "amount must be positive");
    assert_eq!(rejection.failure_type, None);
    worker
        .decide_update_validation(validation_task("v2", "approve", 2))
        .await
        .unwrap();
}

#[tokio::test]
async fn validator_execution_failures_are_not_application_rejections() {
    let mut worker = order_worker(offline_client());
    worker.register_update_validator("orders", "approve", |_ctx, _args| {
        panic!("validator bug");
    });
    let (reason, rejection) = worker
        .decide_update_validation(validation_task("v1", "approve", 1))
        .await
        .unwrap_err();
    assert_eq!(reason, UPDATE_VALIDATION_WORKER_FAILED);
    assert_eq!(
        rejection.failure_type.as_deref(),
        Some("UpdateValidatorPanicked")
    );

    // An update without a declared validator, or not handled at all.
    let (reason, rejection) = worker
        .decide_update_validation(validation_task("v2", "missing", 1))
        .await
        .unwrap_err();
    assert_eq!(reason, UPDATE_VALIDATION_WORKER_FAILED);
    assert!(rejection.message.contains("unknown update"), "{rejection}");
    worker.register_update("orders", "plain", |_ctx, args| async move { Ok(args) });
    let (reason, rejection) = worker
        .decide_update_validation(validation_task("v3", "plain", 1))
        .await
        .unwrap_err();
    assert_eq!(reason, UPDATE_VALIDATION_WORKER_FAILED);
    assert!(rejection.message.contains("is not declared"), "{rejection}");

    // A replayed validator on a workflow without replayable state.
    let mut plain = Worker::new(offline_client(), "rust-workers");
    plain.register_workflow("orders", |_ctx, input| async move { Ok(input) });
    plain.register_update("orders", "approve", |_ctx, args| async move { Ok(args) });
    plain.register_replayed_update_validator(
        "orders",
        "approve",
        |_ctx, _state: Arc<u64>, _args| Ok(()),
    );
    let (reason, _) = plain
        .decide_update_validation(validation_task("v4", "approve", 1))
        .await
        .unwrap_err();
    assert_eq!(reason, UPDATE_VALIDATION_WORKER_FAILED);
}

fn validating_server(path: &str, _body: &str, number: usize) -> Option<(&'static str, String)> {
    match path {
        "/api/cluster/info" => Some(("200 OK", capable_info().to_string())),
        "/api/worker/workflow-tasks/poll" => {
            let task = match number {
                1 => validation_task("val-reject", "approve", 7),
                2 => validation_task("val-approve", "approve", 3),
                _ => Value::Null,
            };
            Some(("200 OK", json!({"task": task}).to_string()))
        }
        "/api/worker/update-validation-tasks/val-reject/reject"
        | "/api/worker/update-validation-tasks/val-approve/approve" => Some((
            "200 OK",
            json!({"outcome": "recorded", "status": 200}).to_string(),
        )),
        _ => None,
    }
}

#[tokio::test]
async fn worker_advertises_validators_and_answers_multiplexed_validation_tasks() {
    let server = MockWorkerServer::start_with_behavior(MockWorkerBehavior {
        request_override: Some(validating_server),
        ..MockWorkerBehavior::default()
    });
    let worker = with_limit_validator(order_worker(Client::new(server.base_url()).unwrap()));
    let calls = HANDLER_CALLS.load(Ordering::SeqCst);
    worker
        .register()
        .await
        .expect("capable Server registration");

    let registration = server.request_body("/api/worker/register");
    assert_eq!(
        registration["workflow_command_contracts"]["orders"]["update_validators"],
        json!(["approve"])
    );
    assert!(registration["capabilities"]
        .as_array()
        .unwrap()
        .contains(&json!(UPDATE_VALIDATION_TASKS_CAPABILITY)));

    assert_eq!(worker.run_once().await.unwrap(), 1);
    assert_eq!(worker.run_once().await.unwrap(), 1);

    for poll in server.request_bodies("/api/worker/workflow-tasks/poll") {
        assert_eq!(poll["task_kinds"], json!(["workflow", "update_validation"]));
    }
    // Activity polls are not multiplexed.
    for poll in server.request_bodies("/api/worker/activity-tasks/poll") {
        assert!(poll.get("task_kinds").is_none(), "{poll}");
    }

    let rejected = server.request_body("/api/worker/update-validation-tasks/val-reject/reject");
    assert_eq!(rejected["lease_owner"], WORKER);
    assert_eq!(rejected["update_validation_attempt"], 1);
    assert_eq!(
        rejected["failure"],
        json!({
            "message": "amount 7 exceeds the limit 5",
            "reason": "update_validator_rejected",
            "type": "LimitExceeded",
            "validation_errors": {"amount": ["exceeds the order limit"]},
        })
    );
    let approved = server.request_body("/api/worker/update-validation-tasks/val-approve/approve");
    assert_eq!(
        approved,
        json!({"lease_owner": WORKER, "update_validation_attempt": 1})
    );

    // Neither validation completed a workflow task, so nothing reached the
    // run's history from this worker, and the update handler never ran.
    let paths: Vec<String> = server
        .requests
        .lock()
        .unwrap()
        .iter()
        .map(|request| request.path.clone())
        .collect();
    assert!(
        !paths
            .iter()
            .any(|path| path.starts_with("/api/worker/workflow-tasks/") && !path.ends_with("/poll")),
        "{paths:?}"
    );
    assert_eq!(HANDLER_CALLS.load(Ordering::SeqCst), calls);
}

fn fencing_server(path: &str, _body: &str, number: usize) -> Option<(&'static str, String)> {
    match path {
        "/api/cluster/info" => Some(("200 OK", capable_info().to_string())),
        "/api/worker/workflow-tasks/poll" if number == 1 => Some((
            "200 OK",
            json!({"task": validation_task("val-stale", "approve", 1)}).to_string(),
        )),
        "/api/worker/update-validation-tasks/val-stale/approve" => Some((
            "409 Conflict",
            json!({"update_validation_task_id": "val-stale", "outcome": "rejected",
                "reason": "stale_update_validation_completion", "status": 409})
            .to_string(),
        )),
        _ => None,
    }
}

#[tokio::test]
async fn fenced_validation_completion_is_settled_not_a_worker_failure() {
    let server = MockWorkerServer::start_with_behavior(MockWorkerBehavior {
        request_override: Some(fencing_server),
        ..MockWorkerBehavior::default()
    });
    let worker = with_limit_validator(order_worker(Client::new(server.base_url()).unwrap()));
    worker.register().await.unwrap();
    assert_eq!(worker.run_once().await.unwrap(), 1);
    assert_eq!(
        server.request_count("/api/worker/update-validation-tasks/val-stale/approve"),
        1
    );
    assert_eq!(worker.run_once().await.unwrap(), 0);
}

#[tokio::test]
async fn registration_refuses_servers_without_synchronous_validation() {
    let server = MockWorkerServer::start();
    let worker = with_limit_validator(order_worker(Client::new(server.base_url()).unwrap()));
    let error = worker.register().await.unwrap_err();
    assert!(
        matches!(&error, Error::UpdateValidationUnavailable(message) if message.contains("does not advertise synchronous pre-accept update validation")),
        "{error}"
    );
    assert_eq!(server.request_count("/api/worker/register"), 0);
    assert_eq!(server.request_count("/api/cluster/info"), 1);

    // A worker that never confirmed registration does not poll unvalidated.
    let error = worker.run_once().await.unwrap_err();
    assert!(
        error
            .to_string()
            .contains("update_validation_registration_unconfirmed"),
        "{error}"
    );
    assert_eq!(server.request_count("/api/worker/workflow-tasks/poll"), 0);
}

#[tokio::test]
async fn registration_refuses_a_validator_without_an_update_handler() {
    let server = MockWorkerServer::start_with_behavior(MockWorkerBehavior {
        request_override: Some(validating_server),
        ..MockWorkerBehavior::default()
    });
    let mut worker = Worker::new(Client::new(server.base_url()).unwrap(), "rust-workers");
    worker.register_workflow("orders", |_ctx, input| async move { Ok(input) });
    worker.register_update_validator("orders", "approve", |_ctx, _args| Ok(()));
    let error = worker.register().await.unwrap_err();
    assert!(
        matches!(&error, Error::UpdateValidationUnavailable(message) if message.contains("orders.approve")),
        "{error}"
    );
    assert_eq!(server.request_count("/api/cluster/info"), 0);
    assert_eq!(server.request_count("/api/worker/register"), 0);
}

#[test]
fn duplicate_validators_invalidate_the_worker() {
    let mut worker = order_worker(offline_client());
    worker.register_update_validator("orders", "approve", |_ctx, _args| Ok(()));
    worker.register_update_validator_avro_value("orders", "approve", |_ctx, _args| Ok(()));
    let error = worker.validate_registration().unwrap_err();
    assert_eq!(error.handler_kind, RegistrationKind::Update);
    assert_eq!(error.handler_name, "approve#validator");
}

fn rejecting_update_server(path: &str, _body: &str, _: usize) -> Option<(&'static str, String)> {
    match path {
        "/api/workflows/order-1/update/approve" => Some((
            "422 Unprocessable Entity",
            json!({
                "workflow_id": "order-1", "run_id": "run-1", "update_name": "approve",
                "update_id": "upd-1", "accepted": false, "approved": false,
                "outcome": "rejected_invalid_arguments", "reason": "update_validator_rejected",
                "rejection_reason": "update_validator_rejected", "update_status": "rejected",
                "command_status": "rejected", "message": "amount 7 exceeds the limit 5",
                "validation_errors": {"amount": ["exceeds the order limit"]},
            })
            .to_string(),
        )),
        "/api/workflows/order-1/update/slow" => Some((
            "422 Unprocessable Entity",
            json!({
                "workflow_id": "order-1", "update_name": "slow",
                "outcome": "update_validation_failed", "reason": "update_validation_timed_out",
                "message": "Synchronous update validation did not complete before the acceptance deadline.",
                "retryable": true,
            })
            .to_string(),
        )),
        _ => None,
    }
}

#[tokio::test]
async fn client_reports_validator_rejection_distinctly_from_failed_updates() {
    let server = MockWorkerServer::start_with_behavior(MockWorkerBehavior {
        request_override: Some(rejecting_update_server),
        ..MockWorkerBehavior::default()
    });
    let client = Client::new(server.base_url()).unwrap();
    let error = client
        .update_workflow("order-1", "approve", json!([7]), Some("approve-7"))
        .await
        .unwrap_err();
    assert!(error.is_update_validator_rejected());
    assert!(!error.is_update_validation_failed());
    let rejection = error.update_validator_rejection().unwrap();
    assert_eq!(rejection.status, 422);
    assert_eq!(rejection.update_id.as_deref(), Some("upd-1"));
    assert_eq!(rejection.update_name.as_deref(), Some("approve"));
    assert_eq!(rejection.message, "amount 7 exceeds the limit 5");
    assert_eq!(
        rejection.validation_errors,
        BTreeMap::from([(
            "amount".to_string(),
            vec!["exceeds the order limit".to_string()]
        )])
    );

    let error = client
        .update_workflow("order-1", "slow", json!([1]), Some("slow-1"))
        .await
        .unwrap_err();
    assert!(error.is_update_validation_failed());
    assert!(!error.is_update_validator_rejected());
    assert!(error.update_validator_rejection().is_none());

    // An ordinary failed update is neither.
    let failed = Error::Http {
        status: reqwest::StatusCode::UNPROCESSABLE_ENTITY,
        body: json!({"outcome": "update_failed", "reason": "update_failed"}).to_string(),
    };
    assert!(!failed.is_update_validator_rejected() && !failed.is_update_validation_failed());
}

#[test]
fn server_capability_check_requires_the_multiplexed_poll_shape() {
    use crate::update_validation::server_supports_update_validation_tasks as supports;
    assert!(supports(&capable_info()));
    assert!(!supports(&json!({})));
    for pointer in [
        "/worker_protocol/server_capabilities/update_validation_tasks",
        "/worker_protocol/server_capabilities/synchronous_update_validation/supported",
        "/worker_protocol/server_capabilities/synchronous_update_validation/task_poll/strategy",
        "/worker_protocol/server_capabilities/synchronous_update_validation/task_poll/endpoint",
    ] {
        let mut info = capable_info();
        *info.pointer_mut(pointer).unwrap() = json!("other");
        assert!(!supports(&info), "{pointer}");
    }
    let mut info = capable_info();
    info["worker_protocol"]["server_capabilities"]["synchronous_update_validation"]["task_poll"]
        ["task_kinds"] = json!(["workflow"]);
    assert!(!supports(&info));
}

#[tokio::test]
async fn registration_refuses_a_replayed_validator_with_another_state_type() {
    let server = MockWorkerServer::start_with_behavior(MockWorkerBehavior {
        request_override: Some(validating_server),
        ..MockWorkerBehavior::default()
    });
    let mut worker = order_worker(Client::new(server.base_url()).unwrap());
    worker.register_replayed_update_validator(
        "orders",
        "approve",
        |_ctx, _state: Arc<u64>, _args| Ok(()),
    );
    let error = worker.register().await.unwrap_err();
    assert!(
        matches!(&error, Error::UpdateValidationUnavailable(message) if message.contains("same state type")),
        "{error}"
    );
    assert_eq!(server.request_count("/api/cluster/info"), 0);
    assert_eq!(server.request_count("/api/worker/register"), 0);
}

#[derive(Default)]
struct TaskOutcomes(Mutex<Vec<(String, String)>>);

impl metrics::MetricsRecorder for TaskOutcomes {
    fn increment(&self, name: &str, _: u64, tags: &metrics::MetricTags) {
        if name == "durable_workflow_worker_tasks" {
            let tag = |key: &str| tags.get(key).cloned().unwrap_or_default();
            self.0
                .lock()
                .unwrap()
                .push((tag("task_kind"), tag("outcome")));
        }
    }
    fn record(&self, _: &str, _: f64, _: &metrics::MetricTags) {}
}

#[tokio::test]
async fn validation_tasks_record_worker_task_metrics_and_answer_while_stopping() {
    let server = MockWorkerServer::start_with_behavior(MockWorkerBehavior {
        request_override: Some(validating_server),
        ..MockWorkerBehavior::default()
    });
    let outcomes = Arc::new(TaskOutcomes::default());
    let mut worker = with_limit_validator(order_worker(Client::new(server.base_url()).unwrap()))
        .metrics_recorder(outcomes.clone());
    worker.register().await.unwrap();
    assert_eq!(worker.run_once().await.unwrap(), 1);

    // A stop requested while a validation is in flight still sends its answer.
    worker.run_stop = Some(Arc::new(AtomicBool::new(true)));
    let task = validation_task("val-approve", "approve", 3);
    assert!(matches!(
        worker.execute_update_validation_task(task).await,
        ManagedPollOutcome::Handled
    ));
    assert_eq!(
        server.request_count("/api/worker/update-validation-tasks/val-approve/approve"),
        1
    );
    assert_eq!(
        *outcomes.0.lock().unwrap(),
        vec![
            ("update_validation".to_string(), "rejected".to_string()),
            ("update_validation".to_string(), "approved".to_string()),
        ]
    );
}

/// Records what an update-validation interceptor observed, and how it breaks the chain.
#[derive(Clone, Copy, PartialEq, Eq)]
enum ValidationAuditMode {
    Observe,
    SkipNext,
    FailAfter,
}

struct ValidationAudit {
    mode: ValidationAuditMode,
    seen: Arc<Mutex<Vec<String>>>,
}

impl WorkerInterceptor for ValidationAudit {
    fn execute_update_validation_task<'a>(
        &'a self,
        context: &'a crate::interceptors::UpdateValidationInterceptorContext,
        next: crate::interceptors::UpdateValidationNext<'a>,
    ) -> crate::interceptors::InterceptorFuture<'a, Result<UpdateValidationOutcome>> {
        Box::pin(async move {
            let identity = format!(
                "{}:{}:{}:{}",
                context.update_validation_task_id(),
                context.workflow_id().unwrap_or_default(),
                context.update_name(),
                context.attempt()
            );
            if self.mode == ValidationAuditMode::SkipNext {
                self.seen
                    .lock()
                    .unwrap()
                    .push(format!("{identity}:skipped"));
                // Outcomes cannot be forged; skipping next returns an error.
                return Err(Error::WorkflowTaskInterceptor("skipped".into()));
            }
            let outcome = next.run().await?;
            self.seen.lock().unwrap().push(format!(
                "{identity}:{}",
                outcome.reason().unwrap_or("approved")
            ));
            if self.mode == ValidationAuditMode::FailAfter {
                return Err(Error::WorkflowTaskInterceptor("audit store down".into()));
            }
            Ok(outcome)
        })
    }
}

fn audited(mode: ValidationAuditMode) -> (Worker, Arc<Mutex<Vec<String>>>) {
    let seen = Arc::new(Mutex::new(Vec::new()));
    let worker =
        with_limit_validator(order_worker(offline_client())).interceptor(ValidationAudit {
            mode,
            seen: seen.clone(),
        });
    (worker, seen)
}

async fn intercepted(
    worker: &Worker,
    task: Value,
) -> std::result::Result<(), (&'static str, UpdateRejection)> {
    let context = worker.update_validation_interceptor_context(&task);
    worker
        .intercept_update_validation(context, worker.decide_update_validation(task))
        .await
}

#[tokio::test]
async fn validation_interceptors_observe_the_decision_without_changing_it() {
    let (worker, seen) = audited(ValidationAuditMode::Observe);

    let (reason, rejection) = intercepted(&worker, validation_task("v1", "approve", 7))
        .await
        .expect_err("the validator's rejection stands");
    assert_eq!(reason, UPDATE_VALIDATOR_REJECTED);
    assert_eq!(rejection.message, "amount 7 exceeds the limit 5");
    intercepted(&worker, validation_task("v2", "approve", 3))
        .await
        .expect("the validator's approval stands");

    assert_eq!(
        *seen.lock().unwrap(),
        vec![
            "v1:order-1:approve:1:update_validator_rejected".to_string(),
            "v2:order-1:approve:1:approved".to_string(),
        ]
    );
}

#[tokio::test]
async fn a_broken_validation_chain_is_a_worker_failure_not_an_approval() {
    // Skipping next never runs the validator, so nothing may be approved.
    let (worker, seen) = audited(ValidationAuditMode::SkipNext);
    let (reason, rejection) = intercepted(&worker, validation_task("v1", "approve", 3))
        .await
        .expect_err("a skipped validator cannot approve");
    assert_eq!(reason, UPDATE_VALIDATION_WORKER_FAILED);
    assert!(
        rejection.message.contains("failed without running next"),
        "{rejection}"
    );
    assert_eq!(
        rejection.failure_type.as_deref(),
        Some("UpdateValidationInterceptor")
    );
    assert_eq!(*seen.lock().unwrap(), vec!["v1:order-1:approve:1:skipped"]);

    // An interceptor error after an approval also withholds acceptance.
    let (worker, _) = audited(ValidationAuditMode::FailAfter);
    let (reason, rejection) = intercepted(&worker, validation_task("v2", "approve", 3))
        .await
        .expect_err("a failed interceptor withholds the approval");
    assert_eq!(reason, UPDATE_VALIDATION_WORKER_FAILED);
    assert!(
        rejection.message.contains("audit store down"),
        "{rejection}"
    );
}

#[tokio::test]
async fn worker_answers_intercepted_validations_with_the_validators_decision() {
    let server = MockWorkerServer::start_with_behavior(MockWorkerBehavior {
        request_override: Some(validating_server),
        ..MockWorkerBehavior::default()
    });
    let seen = Arc::new(Mutex::new(Vec::new()));
    let worker = with_limit_validator(order_worker(Client::new(server.base_url()).unwrap()))
        .interceptor(ValidationAudit {
            mode: ValidationAuditMode::Observe,
            seen: seen.clone(),
        });
    worker.register().await.unwrap();
    assert_eq!(worker.run_once().await.unwrap(), 1);
    assert_eq!(worker.run_once().await.unwrap(), 1);

    // The interceptor saw both validations; the answers are the validator's own.
    assert_eq!(
        *seen.lock().unwrap(),
        vec![
            "val-reject:order-1:approve:1:update_validator_rejected".to_string(),
            "val-approve:order-1:approve:1:approved".to_string(),
        ]
    );
    let rejected = server.request_body("/api/worker/update-validation-tasks/val-reject/reject");
    assert_eq!(rejected["failure"]["reason"], "update_validator_rejected");
    assert_eq!(
        server.request_count("/api/worker/update-validation-tasks/val-approve/approve"),
        1
    );
}