/*
 * Copyright 2020 Fluence Labs Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use air::CatchableError;
use air::ToErrorCode;
use air_test_utils::prelude::*;

#[test]
fn mismatch_equal() {
    let set_variable_peer_id = "set_variable_peer_id";
    let mut set_variable_vm = create_avm(echo_call_service(), set_variable_peer_id);

    let local_peer_id = "local_peer_id";
    let mut vm = create_avm(echo_call_service(), local_peer_id);

    let script = format!(
        r#"
            (seq
                (seq
                    (call "{set_variable_peer_id}" ("" "") ["value_1"] value_1)
                    (call "{set_variable_peer_id}" ("" "") ["value_1"] value_2)
                )
                (xor
                    (mismatch value_1 value_2
                        (call "{local_peer_id}" ("service_id_2" "local_fn_name") ["result_1"] result_1)
                    )
                    (call "{local_peer_id}" ("service_id_2" "local_fn_name") ["result_2"] result_2)
                )
            )"#
    );

    let result = checked_call_vm!(set_variable_vm, <_>::default(), &script, "", "");
    let result = checked_call_vm!(vm, <_>::default(), script, "", result.data);

    let actual_trace = trace_from_result(&result);
    let expected_state = scalar!(
        "result_2",
        peer = local_peer_id,
        service = "service_id_2",
        function = "local_fn_name",
        args = ["result_2"]
    );

    assert_eq!(actual_trace.len(), 3);
    assert_eq!(actual_trace[2.into()], expected_state);
}

#[test]
fn mismatch_not_equal() {
    let set_variable_peer_id = "set_variable_peer_id";
    let mut set_variable_vm = create_avm(echo_call_service(), set_variable_peer_id);

    let local_peer_id = "local_peer_id";
    let mut vm = create_avm(echo_call_service(), local_peer_id);

    let script = format!(
        r#"
            (seq
                (seq
                    (call "{set_variable_peer_id}" ("" "") ["value_1"] value_1)
                    (call "{set_variable_peer_id}" ("" "") ["value_2"] value_2)
                )
                (xor
                    (mismatch value_1 value_2
                        (call "{local_peer_id}" ("service_id_2" "local_fn_name") ["result_1"] result_1)
                    )
                    (call "{local_peer_id}" ("service_id_2" "local_fn_name") ["result_2"] result_2)
                )
            )"#
    );

    let result = checked_call_vm!(set_variable_vm, <_>::default(), &script, "", "");
    let result = checked_call_vm!(vm, <_>::default(), script, "", result.data);

    let actual_trace = trace_from_result(&result);
    let expected_state = scalar!(
        "result_1",
        peer = local_peer_id,
        service = "service_id_2",
        function = "local_fn_name",
        args = ["result_1"]
    );

    assert_eq!(actual_trace.len(), 3);
    assert_eq!(actual_trace[2.into()], expected_state);
}

#[test]
fn mismatch_with_string() {
    let set_variable_peer_id = "set_variable_peer_id";
    let mut set_variable_vm = create_avm(echo_call_service(), set_variable_peer_id);

    let local_peer_id = "local_peer_id";
    let mut vm = create_avm(echo_call_service(), local_peer_id);

    let script = format!(
        r#"
            (seq
                (call "{set_variable_peer_id}" ("" "") ["value_1"] value_1)
                (xor
                    (mismatch value_1 "value_1"
                        (call "{local_peer_id}" ("service_id_2" "local_fn_name") ["result_1"] result_1)
                    )
                    (call "{local_peer_id}" ("service_id_2" "local_fn_name") ["result_2"] result_2)
                )
            )"#
    );

    let result = checked_call_vm!(set_variable_vm, <_>::default(), &script, "", "");
    let result = checked_call_vm!(vm, <_>::default(), script, "", result.data);

    let actual_trace = trace_from_result(&result);
    let expected_state = scalar!(
        "result_2",
        peer = local_peer_id,
        service = "service_id_2",
        function = "local_fn_name",
        args = ["result_2"]
    );

    assert_eq!(actual_trace.len(), 2);
    assert_eq!(actual_trace[1.into()], expected_state);
}

#[test]
fn mismatch_without_xor() {
    let set_variable_peer_id = "set_variable_peer_id";
    let mut set_variable_vm = create_avm(echo_call_service(), set_variable_peer_id);

    let local_peer_id = "local_peer_id";
    let mut vm = create_avm(echo_call_service(), local_peer_id);

    let script = format!(
        r#"
            (seq
                (seq
                    (call "{set_variable_peer_id}" ("" "") ["value_1"] value_1)
                    (call "{set_variable_peer_id}" ("" "") ["value_1"] value_2)
                )
                (mismatch value_1 value_2
                    (call "{local_peer_id}" ("service_id_2" "local_fn_name") ["result_1"] result_1)
                )
            )"#
    );

    let result = call_vm!(set_variable_vm, <_>::default(), &script, "", "");
    let result = call_vm!(vm, <_>::default(), &script, "", result.data);

    let expected_error = CatchableError::MismatchValuesEqual;
    assert_eq!(expected_error.to_error_code(), 10002);
    assert!(check_error(&result, expected_error));

    let result = call_vm!(vm, <_>::default(), script, "", result.data);

    let expected_error = CatchableError::MismatchValuesEqual;
    assert!(check_error(&result, expected_error));
}

#[test]
fn mismatch_with_two_xors() {
    let local_peer_id = "local_peer_id";
    let mut vm = create_avm(set_variable_call_service(serde_json::json!(false)), local_peer_id);

    let local_peer_id_2 = "local_peer_id_2";

    let script = format!(
        r#"
            (xor
                (seq
                    (seq
                        (call "{local_peer_id}" ("getDataSrv" "condition") [] condition)
                        (call "{local_peer_id}" ("getDataSrv" "relay") [] relay)
                    )
                    (xor
                        (mismatch condition true
                            (call "{local_peer_id_2}" ("println" "print") ["it is false"])
                        )
                        (call "{local_peer_id}" ("println" "print") ["it is true"])
                    )
                )
                (call "{local_peer_id}" ("errorHandlingSrv" "error") [%last_error%])
            )
            "#
    );

    let result = checked_call_vm!(vm, <_>::default(), script, "", "");

    let mut actual_trace = trace_from_result(&result);
    let expected_executed_call_result = executed_state::request_sent_by(local_peer_id);

    assert_eq!(actual_trace.pop().unwrap(), expected_executed_call_result);
}

#[test]
fn mismatch_with_error() {
    use air::ExecutionCidState;

    let local_peer_id = "local_peer_id";
    let mut vm = create_avm(echo_call_service(), local_peer_id);

    let script = format!(
        r#"
        (xor
            (mismatch 42 42 (null))
            (call "local_peer_id" ("test" "error_code") [:error:.$.error_code] scalar)
        )
    "#
    );

    let result = checked_call_vm!(vm, <_>::default(), script, "", "");

    let actual_trace = trace_from_result(&result);

    let mut cid_state = ExecutionCidState::new();
    let errcode_lambda_output = json!(10002);

    let expected_trace = ExecutionTrace::from(vec![scalar_tracked!(
        errcode_lambda_output.clone(),
        cid_state,
        peer = local_peer_id,
        service = "test",
        function = "error_code",
        args = vec![errcode_lambda_output]
    )]);
    assert_eq!(actual_trace, expected_trace);
}
