src/lightspeed@ops@verified_routes_harness.erl

-module(lightspeed@ops@verified_routes_harness).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/lightspeed/ops/verified_routes_harness.gleam").
-export([run_scenario/1, run_matrix/0, scenario_label/1, pass_fail_label/1, signature/1, scenario/1, outcomes/1, failed_scenarios/1, report_signature/1]).
-export_type([scenario/0, scenario_outcome/0, report/0]).

-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.

?MODULEDOC(" Deterministic compile-fixture harness for M22 verified-route hard parity.\n").

-type scenario() :: valid_reference |
    unknown_route_reference |
    method_mismatch_reference |
    param_mismatch_reference.

-type scenario_outcome() :: {scenario_outcome, scenario(), boolean(), binary()}.

-type report() :: {report, list(scenario_outcome()), integer()}.

-file("src/lightspeed/ops/verified_routes_harness.gleam", 225).
-spec count_failed(list(scenario_outcome())) -> integer().
count_failed(Outcomes) ->
    case Outcomes of
        [] ->
            0;

        [Outcome | Rest] ->
            case erlang:element(3, Outcome) of
                true ->
                    count_failed(Rest);

                false ->
                    1 + count_failed(Rest)
            end
    end.

-file("src/lightspeed/ops/verified_routes_harness.gleam", 201).
-spec evaluate_param_mismatch(
    lightspeed@framework@verified_routes:router_index(),
    binary()
) -> scenario_outcome().
evaluate_param_mismatch(Index, Index_signature) ->
    Expected = <<"param_mismatch:todo_show:expected=id:actual="/utf8>>,
    Actual = case lightspeed@framework@verified_routes:compile_reference(
        Index,
        lightspeed@framework@verified_routes:reference0(
            <<"todo_show"/utf8>>,
            method_get
        )
    ) of
        {ok, _} ->
            <<"unexpected_ok"/utf8>>;

        {error, Diagnostic} ->
            lightspeed@framework@verified_routes:diagnostic_label(Diagnostic)
    end,
    Passed = Actual =:= Expected,
    Signature = <<<<Index_signature/binary,
            "|reference=todo_show|get|none|"/utf8>>/binary,
        Actual/binary>>,
    {scenario_outcome, param_mismatch_reference, Passed, Signature}.

-file("src/lightspeed/ops/verified_routes_harness.gleam", 177).
-spec evaluate_method_mismatch(
    lightspeed@framework@verified_routes:router_index(),
    binary()
) -> scenario_outcome().
evaluate_method_mismatch(Index, Index_signature) ->
    Expected = <<"method_mismatch:todo_show:expected=GET:actual=POST"/utf8>>,
    Actual = case lightspeed@framework@verified_routes:compile_reference(
        Index,
        lightspeed@framework@verified_routes:reference1(
            <<"todo_show"/utf8>>,
            method_post,
            <<"id"/utf8>>
        )
    ) of
        {ok, _} ->
            <<"unexpected_ok"/utf8>>;

        {error, Diagnostic} ->
            lightspeed@framework@verified_routes:diagnostic_label(Diagnostic)
    end,
    Passed = Actual =:= Expected,
    Signature = <<<<Index_signature/binary,
            "|reference=todo_show|post|id|"/utf8>>/binary,
        Actual/binary>>,
    {scenario_outcome, method_mismatch_reference, Passed, Signature}.

-file("src/lightspeed/ops/verified_routes_harness.gleam", 148).
-spec evaluate_unknown_route(
    lightspeed@framework@verified_routes:router_index(),
    binary()
) -> scenario_outcome().
evaluate_unknown_route(Index, Index_signature) ->
    Expected = <<"unknown_route:missing_route"/utf8>>,
    Actual = case lightspeed@framework@verified_routes:compile_reference(
        Index,
        lightspeed@framework@verified_routes:reference1(
            <<"missing_route"/utf8>>,
            method_get,
            <<"id"/utf8>>
        )
    ) of
        {ok, _} ->
            <<"unexpected_ok"/utf8>>;

        {error, Diagnostic} ->
            lightspeed@framework@verified_routes:diagnostic_label(Diagnostic)
    end,
    Passed = Actual =:= Expected,
    Signature = <<<<Index_signature/binary,
            "|reference=missing_route|get|id|"/utf8>>/binary,
        Actual/binary>>,
    {scenario_outcome, unknown_route_reference, Passed, Signature}.

-file("src/lightspeed/ops/verified_routes_harness.gleam", 121).
-spec evaluate_valid_reference(
    lightspeed@framework@verified_routes:router_index(),
    binary()
) -> scenario_outcome().
evaluate_valid_reference(Index, Index_signature) ->
    Result = lightspeed@framework@verified_routes:compile_reference(
        Index,
        lightspeed@framework@verified_routes:reference2(
            <<"member_show"/utf8>>,
            method_get,
            <<"team_id"/utf8>>,
            <<"id"/utf8>>
        )
    ),
    Passed = case Result of
        {ok, _} ->
            true;

        {error, _} ->
            false
    end,
    Signature = <<Index_signature/binary,
        "|reference=member_show|get|team_id,id|ok"/utf8>>,
    {scenario_outcome, valid_reference, Passed, Signature}.

-file("src/lightspeed/ops/verified_routes_harness.gleam", 102).
-spec fixture_index() -> lightspeed@framework@verified_routes:router_index().
fixture_index() ->
    Index = lightspeed@framework@verified_routes:new_index(),
    {Index@1, _} = lightspeed@framework@verified_routes:index_get0(
        Index,
        <<"home"/utf8>>,
        <<"/"/utf8>>
    ),
    {Index@2, _} = lightspeed@framework@verified_routes:index_get1(
        Index@1,
        <<"todo_show"/utf8>>,
        <<"/todos/:id"/utf8>>,
        <<"id"/utf8>>
    ),
    {Index@3, _} = lightspeed@framework@verified_routes:index_get2(
        Index@2,
        <<"member_show"/utf8>>,
        <<"/teams/:team_id/members/:id"/utf8>>,
        <<"team_id"/utf8>>,
        <<"id"/utf8>>
    ),
    {_, _} = lightspeed@framework@verified_routes:index_post0(
        Index@3,
        <<"todo_create"/utf8>>,
        <<"/todos"/utf8>>
    ),
    Index@3.

-file("src/lightspeed/ops/verified_routes_harness.gleam", 38).
?DOC(" Run one M22 scenario.\n").
-spec run_scenario(scenario()) -> scenario_outcome().
run_scenario(Scenario) ->
    Index = fixture_index(),
    Index_signature = lightspeed@framework@verified_routes:index_signature(
        Index
    ),
    case Scenario of
        valid_reference ->
            evaluate_valid_reference(Index, Index_signature);

        unknown_route_reference ->
            evaluate_unknown_route(Index, Index_signature);

        method_mismatch_reference ->
            evaluate_method_mismatch(Index, Index_signature);

        param_mismatch_reference ->
            evaluate_param_mismatch(Index, Index_signature)
    end.

-file("src/lightspeed/ops/verified_routes_harness.gleam", 25).
?DOC(" Run all M22 compile-fixture scenarios.\n").
-spec run_matrix() -> report().
run_matrix() ->
    Scenarios = [valid_reference,
        unknown_route_reference,
        method_mismatch_reference,
        param_mismatch_reference],
    Outcomes = gleam@list:map(Scenarios, fun run_scenario/1),
    {report, Outcomes, count_failed(Outcomes)}.

-file("src/lightspeed/ops/verified_routes_harness.gleam", 51).
?DOC(" Stable scenario label.\n").
-spec scenario_label(scenario()) -> binary().
scenario_label(Scenario) ->
    case Scenario of
        valid_reference ->
            <<"valid_reference"/utf8>>;

        unknown_route_reference ->
            <<"unknown_route_reference"/utf8>>;

        method_mismatch_reference ->
            <<"method_mismatch_reference"/utf8>>;

        param_mismatch_reference ->
            <<"param_mismatch_reference"/utf8>>
    end.

-file("src/lightspeed/ops/verified_routes_harness.gleam", 61).
?DOC(" Stable pass/fail label.\n").
-spec pass_fail_label(scenario_outcome()) -> binary().
pass_fail_label(Outcome) ->
    case erlang:element(3, Outcome) of
        true ->
            <<"pass"/utf8>>;

        false ->
            <<"fail"/utf8>>
    end.

-file("src/lightspeed/ops/verified_routes_harness.gleam", 69).
?DOC(" Scenario signature accessor.\n").
-spec signature(scenario_outcome()) -> binary().
signature(Outcome) ->
    erlang:element(4, Outcome).

-file("src/lightspeed/ops/verified_routes_harness.gleam", 74).
?DOC(" Scenario accessor.\n").
-spec scenario(scenario_outcome()) -> scenario().
scenario(Outcome) ->
    erlang:element(2, Outcome).

-file("src/lightspeed/ops/verified_routes_harness.gleam", 79).
?DOC(" Outcomes accessor.\n").
-spec outcomes(report()) -> list(scenario_outcome()).
outcomes(Report) ->
    erlang:element(2, Report).

-file("src/lightspeed/ops/verified_routes_harness.gleam", 84).
?DOC(" Failed scenario count.\n").
-spec failed_scenarios(report()) -> integer().
failed_scenarios(Report) ->
    erlang:element(3, Report).

-file("src/lightspeed/ops/verified_routes_harness.gleam", 236).
-spec join_with(binary(), list(binary())) -> binary().
join_with(Separator, Values) ->
    case Values of
        [] ->
            <<""/utf8>>;

        [Value] ->
            Value;

        [Value@1 | Rest] ->
            <<<<Value@1/binary, Separator/binary>>/binary,
                (join_with(Separator, Rest))/binary>>
    end.

-file("src/lightspeed/ops/verified_routes_harness.gleam", 89).
?DOC(" Stable report signature for CI fixtures.\n").
-spec report_signature(report()) -> binary().
report_signature(Report) ->
    Entries = gleam@list:map(
        erlang:element(2, Report),
        fun(Outcome) ->
            <<<<<<<<(scenario_label(erlang:element(2, Outcome)))/binary,
                            "="/utf8>>/binary,
                        (pass_fail_label(Outcome))/binary>>/binary,
                    ":"/utf8>>/binary,
                (erlang:element(4, Outcome))/binary>>
        end
    ),
    join_with(<<";"/utf8>>, Entries).