src/lightspeed@ops@turnkey_presets_harness.erl

-module(lightspeed@ops@turnkey_presets_harness).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/lightspeed/ops/turnkey_presets_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 turnkey-presets conformance harness for M58.\n").

-type scenario() :: one_command_secure_scaffold_baseline |
    secure_defaults_and_explicit_override_seams |
    reference_scenario_critical_flow_certification |
    overrides_without_framework_internal_edits |
    preset_lifecycle_determinism_gate.

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

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

-file("src/lightspeed/ops/turnkey_presets_harness.gleam", 335).
-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/turnkey_presets_harness.gleam", 324).
-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/turnkey_presets_harness.gleam", 353).
-spec join_with(binary(), list(binary())) -> binary().
join_with(Separator, Values) ->
    case Values of
        [] ->
            <<""/utf8>>;

        [First | Rest] ->
            gleam@list:fold(
                Rest,
                First,
                fun(Accumulator, Value) ->
                    <<<<Accumulator/binary, Separator/binary>>/binary,
                        Value/binary>>
                end
            )
    end.

-file("src/lightspeed/ops/turnkey_presets_harness.gleam", 262).
-spec evaluate_preset_lifecycle_determinism_gate() -> {boolean(), binary()}.
evaluate_preset_lifecycle_determinism_gate() ->
    First = lightspeed@platform@turnkey_presets:snapshot_signature(),
    Second = lightspeed@platform@turnkey_presets:snapshot_signature(),
    Fixtures = lightspeed@platform@turnkey_presets:fixture_snapshots(),
    Passed = (First =:= Second) andalso (erlang:length(Fixtures) =:= 3),
    Fixture_entries = gleam@list:map(
        Fixtures,
        fun(Entry) ->
            {Label, Value} = Entry,
            <<<<Label/binary, "="/utf8>>/binary, Value/binary>>
        end
    ),
    {Passed,
        <<<<First/binary, "|fixtures="/utf8>>/binary,
            (join_with(<<";"/utf8>>, Fixture_entries))/binary>>}.

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

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

-file("src/lightspeed/ops/turnkey_presets_harness.gleam", 238).
-spec evaluate_overrides_without_framework_internal_edits() -> {boolean(),
    binary()}.
evaluate_overrides_without_framework_internal_edits() ->
    Baseline = lightspeed@platform@turnkey_presets:secure_saas_preset(),
    Custom_override = lightspeed@platform@turnkey_presets:override_hook(
        queue_seam,
        <<"MyApp.Queue.CustomPolicy"/utf8>>,
        <<"select_retry_queue"/utf8>>,
        <<"route retry queue by tenant risk class"/utf8>>
    ),
    Customized = lightspeed@platform@turnkey_presets:with_override_hook(
        Baseline,
        Custom_override
    ),
    Passed = lightspeed@platform@turnkey_presets:override_without_internal_edits(
        Customized
    )
    andalso lightspeed@platform@turnkey_presets:valid(Customized),
    Signature = <<<<<<<<<<"baseline="/utf8,
                        (lightspeed@platform@turnkey_presets:override_surface_signature(
                            Baseline
                        ))/binary>>/binary,
                    "|customized="/utf8>>/binary,
                (lightspeed@platform@turnkey_presets:override_surface_signature(
                    Customized
                ))/binary>>/binary,
            "|internal_edits="/utf8>>/binary,
        (bool_label(
            lightspeed@platform@turnkey_presets:requires_framework_internal_edits(
                Customized
            )
        ))/binary>>,
    {Passed, Signature}.

-file("src/lightspeed/ops/turnkey_presets_harness.gleam", 313).
-spec scenarios_all_certified(
    list(lightspeed@platform@turnkey_presets:reference_scenario())
) -> boolean().
scenarios_all_certified(Scenarios) ->
    case Scenarios of
        [] ->
            true;

        [Scenario | Rest] ->
            lightspeed@platform@turnkey_presets:scenario_certified(Scenario)
            andalso scenarios_all_certified(Rest)
    end.

-file("src/lightspeed/ops/turnkey_presets_harness.gleam", 230).
-spec evaluate_reference_scenario_critical_flow_certification() -> {boolean(),
    binary()}.
evaluate_reference_scenario_critical_flow_certification() ->
    Scenarios = lightspeed@platform@turnkey_presets:reference_scenarios(),
    Passed = (erlang:length(Scenarios) =:= 3) andalso scenarios_all_certified(
        Scenarios
    ),
    Entries = gleam@list:map(
        Scenarios,
        fun lightspeed@platform@turnkey_presets:scenario_signature/1
    ),
    {Passed, join_with(<<";"/utf8>>, Entries)}.

-file("src/lightspeed/ops/turnkey_presets_harness.gleam", 302).
-spec presets_all_have_explicit_seams(
    list(lightspeed@platform@turnkey_presets:starter_preset())
) -> boolean().
presets_all_have_explicit_seams(Presets) ->
    case Presets of
        [] ->
            true;

        [Preset | Rest] ->
            lightspeed@platform@turnkey_presets:has_explicit_override_seams(
                Preset
            )
            andalso presets_all_have_explicit_seams(Rest)
    end.

-file("src/lightspeed/ops/turnkey_presets_harness.gleam", 294).
-spec presets_all_secure(
    list(lightspeed@platform@turnkey_presets:starter_preset())
) -> boolean().
presets_all_secure(Presets) ->
    case Presets of
        [] ->
            true;

        [Preset | Rest] ->
            lightspeed@platform@turnkey_presets:secure_defaults(Preset) andalso presets_all_secure(
                Rest
            )
    end.

-file("src/lightspeed/ops/turnkey_presets_harness.gleam", 287).
-spec presets_all_valid(
    list(lightspeed@platform@turnkey_presets:starter_preset())
) -> boolean().
presets_all_valid(Presets) ->
    case Presets of
        [] ->
            true;

        [Preset | Rest] ->
            lightspeed@platform@turnkey_presets:valid(Preset) andalso presets_all_valid(
                Rest
            )
    end.

-file("src/lightspeed/ops/turnkey_presets_harness.gleam", 212).
-spec evaluate_secure_defaults_and_explicit_override_seams() -> {boolean(),
    binary()}.
evaluate_secure_defaults_and_explicit_override_seams() ->
    Presets = lightspeed@platform@turnkey_presets:reference_presets(),
    Passed = (presets_all_valid(Presets) andalso presets_all_secure(Presets))
    andalso presets_all_have_explicit_seams(Presets),
    Entries = gleam@list:map(
        Presets,
        fun(Preset) ->
            <<<<<<<<(lightspeed@platform@turnkey_presets:name(Preset))/binary,
                            ":secure="/utf8>>/binary,
                        (bool_label(
                            lightspeed@platform@turnkey_presets:secure_defaults(
                                Preset
                            )
                        ))/binary>>/binary,
                    ":overrides="/utf8>>/binary,
                (lightspeed@platform@turnkey_presets:override_surface_signature(
                    Preset
                ))/binary>>
        end
    ),
    {Passed, join_with(<<";"/utf8>>, Entries)}.

-file("src/lightspeed/ops/turnkey_presets_harness.gleam", 276).
-spec presets_all_scaffoldable_secure_baselines(
    list(lightspeed@platform@turnkey_presets:starter_preset())
) -> boolean().
presets_all_scaffoldable_secure_baselines(Presets) ->
    case Presets of
        [] ->
            true;

        [Preset | Rest] ->
            lightspeed@platform@turnkey_presets:scaffoldable_secure_baseline(
                Preset
            )
            andalso presets_all_scaffoldable_secure_baselines(Rest)
    end.

-file("src/lightspeed/ops/turnkey_presets_harness.gleam", 197).
-spec evaluate_one_command_secure_scaffold_baseline() -> {boolean(), binary()}.
evaluate_one_command_secure_scaffold_baseline() ->
    Presets = lightspeed@platform@turnkey_presets:reference_presets(),
    Passed = (erlang:length(Presets) =:= 3) andalso presets_all_scaffoldable_secure_baselines(
        Presets
    ),
    Entries = gleam@list:map(
        Presets,
        fun(Preset) ->
            <<<<(lightspeed@platform@turnkey_presets:name(Preset))/binary,
                    ":"/utf8>>/binary,
                (lightspeed@platform@turnkey_presets:scaffold_command(Preset))/binary>>
        end
    ),
    {Passed, join_with(<<";"/utf8>>, Entries)}.

-file("src/lightspeed/ops/turnkey_presets_harness.gleam", 182).
-spec evaluate(scenario()) -> {boolean(), binary()}.
evaluate(Scenario) ->
    case Scenario of
        one_command_secure_scaffold_baseline ->
            evaluate_one_command_secure_scaffold_baseline();

        secure_defaults_and_explicit_override_seams ->
            evaluate_secure_defaults_and_explicit_override_seams();

        reference_scenario_critical_flow_certification ->
            evaluate_reference_scenario_critical_flow_certification();

        overrides_without_framework_internal_edits ->
            evaluate_overrides_without_framework_internal_edits();

        preset_lifecycle_determinism_gate ->
            evaluate_preset_lifecycle_determinism_gate()
    end.

-file("src/lightspeed/ops/turnkey_presets_harness.gleam", 57).
?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/turnkey_presets_harness.gleam", 38).
?DOC(" Run all M58 scenarios.\n").
-spec run_matrix() -> report().
run_matrix() ->
    Outcomes = begin
        _pipe = [one_command_secure_scaffold_baseline,
            secure_defaults_and_explicit_override_seams,
            reference_scenario_critical_flow_certification,
            overrides_without_framework_internal_edits,
            preset_lifecycle_determinism_gate],
        gleam@list:map(_pipe, fun run_scenario/1)
    end,
    {report, Outcomes, count_failed(Outcomes), count_nondeterministic(Outcomes)}.

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

        secure_defaults_and_explicit_override_seams ->
            <<"secure_defaults_and_explicit_override_seams"/utf8>>;

        reference_scenario_critical_flow_certification ->
            <<"reference_scenario_critical_flow_certification"/utf8>>;

        overrides_without_framework_internal_edits ->
            <<"overrides_without_framework_internal_edits"/utf8>>;

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

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

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

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

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

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

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

-file("src/lightspeed/ops/turnkey_presets_harness.gleam", 125).
?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/turnkey_presets_harness.gleam", 141).
?DOC(" Deterministic snapshot signature for M58 fixture drift gates.\n").
-spec snapshot_signature() -> binary().
snapshot_signature() ->
    <<<<<<"m58.snapshot.v"/utf8, (erlang:integer_to_binary(1))/binary>>/binary,
            "|"/utf8>>/binary,
        (report_signature(run_matrix()))/binary>>.

-file("src/lightspeed/ops/turnkey_presets_harness.gleam", 149).
?DOC(" Deterministic markdown report for M58 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,
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"# Turnkey Presets Fixture Report\n\n"/utf8,
                                                                                        "snapshot_version: "/utf8>>/binary,
                                                                                    (erlang:integer_to_binary(
                                                                                        1
                                                                                    ))/binary>>/binary,
                                                                                "\n"/utf8>>/binary,
                                                                            "profile_version: "/utf8>>/binary,
                                                                        (lightspeed@platform@turnkey_presets:profile_version_label(
                                                                            
                                                                        ))/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>>.