src/lightspeed@ops@convention_profile_harness.erl

-module(lightspeed@ops@convention_profile_harness).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/lightspeed/ops/convention_profile_harness.gleam").
-export([run_scenario/1, run_matrix/0, scenario_label/1, pass_fail_label/1, signature/1, scenario/1, deterministic/1, outcomes/1, failed_scenarios/1, nondeterministic_failures/1, report_signature/1, snapshot_signature/0, snapshot_report_markdown/0]).
-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 convention-profile conformance harness for M45.\n").

-type scenario() :: default_profile_quick_start |
    runtime_data_template_surface_reduction |
    explicit_escape_hatch_stability |
    deterministic_reference_fixture_certification.

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

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

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

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

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

-file("src/lightspeed/ops/convention_profile_harness.gleam", 264).
-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/convention_profile_harness.gleam", 293).
-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/convention_profile_harness.gleam", 250).
-spec evaluate_deterministic_reference_fixture_certification() -> {boolean(),
    binary()}.
evaluate_deterministic_reference_fixture_certification() ->
    First = lightspeed@platform@convention_profile:snapshot_signature(),
    Second = lightspeed@platform@convention_profile:snapshot_signature(),
    Fixtures = lightspeed@platform@convention_profile:fixture_snapshots(),
    Passed = (First =:= Second) andalso (erlang:length(Fixtures) =:= 3),
    Fixture_entries = gleam@list:map(
        Fixtures,
        fun(Entry) ->
            {Label, Signature} = Entry,
            <<<<Label/binary, "="/utf8>>/binary, Signature/binary>>
        end
    ),
    {Passed,
        <<<<First/binary, "|fixtures="/utf8>>/binary,
            (join_with(<<";"/utf8>>, Fixture_entries))/binary>>}.

-file("src/lightspeed/ops/convention_profile_harness.gleam", 227).
-spec evaluate_explicit_escape_hatch_stability() -> {boolean(), binary()}.
evaluate_explicit_escape_hatch_stability() ->
    Profile = begin
        _pipe = lightspeed@platform@convention_profile:default_profile(),
        _pipe@1 = lightspeed@platform@convention_profile:with_escape_hatch(
            _pipe,
            lightspeed@platform@convention_profile:websocket_fallback_hatch()
        ),
        _pipe@2 = lightspeed@platform@convention_profile:with_escape_hatch(
            _pipe@1,
            lightspeed@platform@convention_profile:data_partition_hatch()
        ),
        _pipe@3 = lightspeed@platform@convention_profile:with_escape_hatch(
            _pipe@2,
            lightspeed@platform@convention_profile:template_diagnostics_hatch()
        ),
        lightspeed@platform@convention_profile:with_escape_hatch(
            _pipe@3,
            lightspeed@platform@convention_profile:websocket_fallback_hatch()
        )
    end,
    Passed = lightspeed@platform@convention_profile:has_stable_override_seams(
        Profile
    )
    andalso (erlang:length(
        lightspeed@platform@convention_profile:escape_hatches(Profile)
    )
    =:= 3),
    {Passed,
        lightspeed@platform@convention_profile:override_seam_signature(Profile)}.

-file("src/lightspeed/ops/convention_profile_harness.gleam", 196).
-spec evaluate_runtime_data_template_surface_reduction() -> {boolean(),
    binary()}.
evaluate_runtime_data_template_surface_reduction() ->
    Baseline = lightspeed@platform@convention_profile:default_profile(),
    Baseline_surface = lightspeed@platform@convention_profile:configuration_surface_size(
        Baseline
    ),
    Expanded = begin
        _pipe = Baseline,
        _pipe@1 = lightspeed@platform@convention_profile:with_escape_hatch(
            _pipe,
            lightspeed@platform@convention_profile:websocket_fallback_hatch()
        ),
        _pipe@2 = lightspeed@platform@convention_profile:with_escape_hatch(
            _pipe@1,
            lightspeed@platform@convention_profile:data_partition_hatch()
        ),
        lightspeed@platform@convention_profile:with_escape_hatch(
            _pipe@2,
            lightspeed@platform@convention_profile:template_diagnostics_hatch()
        )
    end,
    Expanded_surface = lightspeed@platform@convention_profile:configuration_surface_size(
        Expanded
    ),
    Passed = ((Baseline_surface =:= 6) andalso (Expanded_surface =:= 9)) andalso (Expanded_surface
    > Baseline_surface),
    {Passed,
        <<<<<<<<<<"baseline_surface="/utf8,
                            (erlang:integer_to_binary(Baseline_surface))/binary>>/binary,
                        "|expanded_surface="/utf8>>/binary,
                    (erlang:integer_to_binary(Expanded_surface))/binary>>/binary,
                "|baseline_signature="/utf8>>/binary,
            (lightspeed@platform@convention_profile:configuration_surface_signature(
                Baseline
            ))/binary>>}.

-file("src/lightspeed/ops/convention_profile_harness.gleam", 186).
-spec evaluate_default_profile_quick_start() -> {boolean(), binary()}.
evaluate_default_profile_quick_start() ->
    Profile = lightspeed@platform@convention_profile:default_profile(),
    Passed = (lightspeed@platform@convention_profile:quick_start_ready(Profile)
    andalso not lightspeed@platform@convention_profile:hidden_runtime_behavior(
        Profile
    ))
    andalso (lightspeed@platform@convention_profile:configuration_surface_size(
        Profile
    )
    =:= 6),
    {Passed, lightspeed@platform@convention_profile:signature(Profile)}.

-file("src/lightspeed/ops/convention_profile_harness.gleam", 175).
-spec evaluate(scenario()) -> {boolean(), binary()}.
evaluate(Scenario) ->
    case Scenario of
        default_profile_quick_start ->
            evaluate_default_profile_quick_start();

        runtime_data_template_surface_reduction ->
            evaluate_runtime_data_template_surface_reduction();

        explicit_escape_hatch_stability ->
            evaluate_explicit_escape_hatch_stability();

        deterministic_reference_fixture_certification ->
            evaluate_deterministic_reference_fixture_certification()
    end.

-file("src/lightspeed/ops/convention_profile_harness.gleam", 55).
?DOC(" Run one scenario twice and require deterministic parity.\n").
-spec run_scenario(scenario()) -> scenario_outcome().
run_scenario(Scenario) ->
    {First_passed, First_signature} = evaluate(Scenario),
    {Second_passed, Second_signature} = evaluate(Scenario),
    Deterministic = (First_passed =:= Second_passed) andalso (First_signature
    =:= Second_signature),
    Passed = (First_passed andalso Second_passed) andalso Deterministic,
    {scenario_outcome, Scenario, Passed, Deterministic, First_signature}.

-file("src/lightspeed/ops/convention_profile_harness.gleam", 37).
?DOC(" Run all M45 scenarios.\n").
-spec run_matrix() -> report().
run_matrix() ->
    Outcomes = begin
        _pipe = [default_profile_quick_start,
            runtime_data_template_surface_reduction,
            explicit_escape_hatch_stability,
            deterministic_reference_fixture_certification],
        gleam@list:map(_pipe, fun run_scenario/1)
    end,
    {report, Outcomes, count_failed(Outcomes), count_nondeterministic(Outcomes)}.

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

        runtime_data_template_surface_reduction ->
            <<"runtime_data_template_surface_reduction"/utf8>>;

        explicit_escape_hatch_stability ->
            <<"explicit_escape_hatch_stability"/utf8>>;

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

-file("src/lightspeed/ops/convention_profile_harness.gleam", 83).
?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/convention_profile_harness.gleam", 91).
?DOC(" Scenario signature accessor.\n").
-spec signature(scenario_outcome()) -> binary().
signature(Outcome) ->
    erlang:element(5, Outcome).

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

-file("src/lightspeed/ops/convention_profile_harness.gleam", 101).
?DOC(" Determinism accessor.\n").
-spec deterministic(scenario_outcome()) -> boolean().
deterministic(Outcome) ->
    erlang:element(4, Outcome).

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

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

-file("src/lightspeed/ops/convention_profile_harness.gleam", 116).
?DOC(" Nondeterministic scenario count.\n").
-spec nondeterministic_failures(report()) -> integer().
nondeterministic_failures(Report) ->
    erlang:element(4, Report).

-file("src/lightspeed/ops/convention_profile_harness.gleam", 286).
-spec bool_label(boolean()) -> binary().
bool_label(Value) ->
    case Value of
        true ->
            <<"true"/utf8>>;

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

-file("src/lightspeed/ops/convention_profile_harness.gleam", 121).
?DOC(" Stable report signature.\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,
                            ":deterministic="/utf8>>/binary,
                        (bool_label(erlang:element(4, Outcome)))/binary>>/binary,
                    ":"/utf8>>/binary,
                (erlang:element(5, Outcome))/binary>>
        end
    ),
    join_with(<<";"/utf8>>, Entries).

-file("src/lightspeed/ops/convention_profile_harness.gleam", 137).
?DOC(" Deterministic snapshot signature for M45 fixture drift gates.\n").
-spec snapshot_signature() -> binary().
snapshot_signature() ->
    <<<<<<"m45.snapshot.v"/utf8, (erlang:integer_to_binary(1))/binary>>/binary,
            "|"/utf8>>/binary,
        (report_signature(run_matrix()))/binary>>.

-file("src/lightspeed/ops/convention_profile_harness.gleam", 145).
?DOC(" Deterministic markdown report for M45 fixture scripts.\n").
-spec snapshot_report_markdown() -> binary().
snapshot_report_markdown() ->
    Report = run_matrix(),
    Failed = failed_scenarios(Report),
    Nondeterministic = nondeterministic_failures(Report),
    Status = case (Failed =:= 0) andalso (Nondeterministic =:= 0) of
        true ->
            <<"OK"/utf8>>;

        false ->
            <<"FAIL"/utf8>>
    end,
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"# Convention Profile Fixture Report\n\n"/utf8,
                                                                            "snapshot_version: "/utf8>>/binary,
                                                                        (erlang:integer_to_binary(
                                                                            1
                                                                        ))/binary>>/binary,
                                                                    "\n"/utf8>>/binary,
                                                                "status: "/utf8>>/binary,
                                                            Status/binary>>/binary,
                                                        "\n"/utf8>>/binary,
                                                    "failed_scenarios: "/utf8>>/binary,
                                                (erlang:integer_to_binary(
                                                    Failed
                                                ))/binary>>/binary,
                                            "\n"/utf8>>/binary,
                                        "nondeterministic_failures: "/utf8>>/binary,
                                    (erlang:integer_to_binary(Nondeterministic))/binary>>/binary,
                                "\n\n"/utf8>>/binary,
                            "snapshot_signature: "/utf8>>/binary,
                        (snapshot_signature())/binary>>/binary,
                    "\n\n"/utf8>>/binary,
                "report_signature: "/utf8>>/binary,
            (report_signature(Report))/binary>>/binary,
        "\n"/utf8>>.