1
use forms::FieldError;
2
use server::logical::STALE_REVISION;
3
use server::script_state::STALE_SCRIPT;
4
use server::tag_state::STALE_TAG;
5
use server::telemetry::recorder::{FORM_REQUESTS, build_recorder};
6
use server::template_state::STALE_TEMPLATE;
7

            
8
use super::{Entity, Failure, FormAction, FormOutcome, count_result, record_form_request};
9
use crate::host_failure::{busy_condition, not_found_condition};
10
use crate::natives::forms::args_error;
11

            
12
6
fn invalid(field: &str, message: &str) -> Failure {
13
6
    Failure::Invalid(vec![FieldError {
14
6
        field: field.to_string(),
15
6
        message: message.to_string(),
16
6
    }])
17
6
}
18

            
19
#[test]
20
1
fn each_failure_maps_to_exactly_one_outcome() {
21
1
    let causes = [
22
1
        (invalid("date", "bad"), FormOutcome::Invalid),
23
1
        (invalid("revision", STALE_REVISION), FormOutcome::Stale),
24
1
        (invalid("revision", STALE_TAG), FormOutcome::Stale),
25
1
        (invalid("revision", STALE_TEMPLATE), FormOutcome::Stale),
26
1
        (invalid("revision", STALE_SCRIPT), FormOutcome::Stale),
27
1
        (invalid("revision", "other"), FormOutcome::Invalid),
28
1
        (Failure::UnknownForm("x".into()), FormOutcome::UnknownForm),
29
1
        (
30
1
            Failure::MalformedId {
31
1
                entity: Entity::Template,
32
1
                id: "x".into(),
33
1
            },
34
1
            FormOutcome::MalformedId,
35
1
        ),
36
1
        (Failure::Missing(Entity::Transaction), FormOutcome::Missing),
37
1
        (Failure::Refused("no".into()), FormOutcome::Refused),
38
1
        (
39
1
            Failure::Internal(wasmtime::Error::msg("db")),
40
1
            FormOutcome::Internal,
41
1
        ),
42
1
        (
43
1
            Failure::Internal(not_found_condition("gone", None)),
44
1
            FormOutcome::Missing,
45
1
        ),
46
1
        (Failure::Internal(args_error("bad")), FormOutcome::Refused),
47
1
        (
48
1
            Failure::Internal(busy_condition("later")),
49
1
            FormOutcome::Internal,
50
1
        ),
51
1
    ];
52
1
    for (failure, outcome) in causes {
53
14
        assert_eq!(FormOutcome::from(&failure), outcome, "{failure}");
54
    }
55
1
}
56

            
57
#[test]
58
1
fn outcome_and_action_labels_are_the_documented_sets() {
59
1
    let outcomes = [
60
1
        FormOutcome::Ok,
61
1
        FormOutcome::Invalid,
62
1
        FormOutcome::UnknownForm,
63
1
        FormOutcome::Malformed,
64
1
        FormOutcome::MalformedId,
65
1
        FormOutcome::Missing,
66
1
        FormOutcome::Refused,
67
1
        FormOutcome::Stale,
68
1
        FormOutcome::Internal,
69
1
    ]
70
1
    .map(FormOutcome::as_str);
71
1
    assert_eq!(
72
1
        outcomes,
73
1
        [
74
1
            "ok",
75
1
            "invalid",
76
1
            "unknown_form",
77
1
            "malformed",
78
1
            "malformed_id",
79
1
            "missing",
80
1
            "refused",
81
1
            "stale",
82
1
            "internal"
83
1
        ]
84
    );
85
1
    let actions = [FormAction::Load, FormAction::Submit, FormAction::Query].map(FormAction::as_str);
86
1
    assert_eq!(actions, ["load", "submit", "query"]);
87
1
}
88

            
89
#[test]
90
1
fn only_a_compiled_in_form_name_becomes_a_label() {
91
1
    let recorder = build_recorder().expect("recorder");
92
1
    let handle = recorder.handle();
93
1
    metrics::with_local_recorder(&recorder, || {
94
1
        record_form_request("transaction-create", FormAction::Submit, FormOutcome::Ok);
95
1
        record_form_request("<script>", FormAction::Load, FormOutcome::UnknownForm);
96
1
        let _ = count_result::<()>("", FormAction::Query, Err(Failure::Refused("x".into())));
97
1
    });
98
1
    let rendered = handle.render();
99
1
    assert!(rendered.contains(&format!(
100
1
        "{FORM_REQUESTS}{{form=\"transaction-create\",action=\"submit\",outcome=\"ok\"}} 1"
101
1
    )));
102
1
    assert!(
103
1
        rendered.contains("form=\"unknown\",action=\"load\""),
104
        "{rendered}"
105
    );
106
1
    assert!(rendered.contains("form=\"unknown\",action=\"query\",outcome=\"refused\""));
107
1
    assert!(!rendered.contains("<script>"));
108
1
}