src/lightspeed@ops@production_readiness_harness.erl

-module(lightspeed@ops@production_readiness_harness).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/lightspeed/ops/production_readiness_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 production-readiness certification harness.\n").

-type scenario() :: gate_prefix_certification |
    risk_class_coverage |
    operator_recovery_coverage |
    deterministic_snapshot_gate |
    required_gate_passes.

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

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

-file("src/lightspeed/ops/production_readiness_harness.gleam", 290).
-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/production_readiness_harness.gleam", 279).
-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/production_readiness_harness.gleam", 308).
-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/production_readiness_harness.gleam", 301).
-spec bool_label(boolean()) -> binary().
bool_label(Value) ->
    case Value of
        true ->
            <<"true"/utf8>>;

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

-file("src/lightspeed/ops/production_readiness_harness.gleam", 249).
-spec evaluate_required_gate_passes() -> {boolean(), binary()}.
evaluate_required_gate_passes() ->
    Failing = lightspeed@release@production_readiness:failing_required_gates(),
    Failing_count = erlang:length(Failing),
    Failing_names = gleam@list:map(
        Failing,
        fun lightspeed@release@production_readiness:gate_name/1
    ),
    Passed = lightspeed@release@production_readiness:ready() andalso (Failing_count
    =:= 0),
    Signature = <<<<<<<<<<"failing_required="/utf8,
                        (erlang:integer_to_binary(Failing_count))/binary>>/binary,
                    "|ready="/utf8>>/binary,
                (bool_label(lightspeed@release@production_readiness:ready()))/binary>>/binary,
            "|failing_names="/utf8>>/binary,
        (join_with(<<","/utf8>>, Failing_names))/binary>>,
    {Passed, Signature}.

-file("src/lightspeed/ops/production_readiness_harness.gleam", 238).
-spec evaluate_deterministic_snapshot_gate() -> {boolean(), binary()}.
evaluate_deterministic_snapshot_gate() ->
    First = lightspeed@release@production_readiness:snapshot_signature(),
    Second = lightspeed@release@production_readiness:snapshot_signature(),
    Passed = ((First =:= Second) andalso gleam_stdlib:string_starts_with(
        First,
        <<"prod.ready.v1|"/utf8>>
    ))
    andalso gleam_stdlib:contains_string(First, <<"|ready=true|"/utf8>>),
    {Passed, First}.

-file("src/lightspeed/ops/production_readiness_harness.gleam", 226).
-spec evaluate_operator_recovery_coverage() -> {boolean(), binary()}.
evaluate_operator_recovery_coverage() ->
    Signature = lightspeed@release@production_readiness:snapshot_signature(),
    Passed = (((lightspeed@release@production_readiness:ready() andalso gleam_stdlib:contains_string(
        Signature,
        <<"m40.snapshot.v1|"/utf8>>
    ))
    andalso gleam_stdlib:contains_string(Signature, <<"m42.snapshot.v1|"/utf8>>))
    andalso gleam_stdlib:contains_string(Signature, <<"m54.snapshot.v1|"/utf8>>))
    andalso gleam_stdlib:contains_string(Signature, <<"m60.snapshot.v1|"/utf8>>),
    {Passed, Signature}.

-file("src/lightspeed/ops/production_readiness_harness.gleam", 265).
-spec has_risk_class(
    list(lightspeed@release@production_readiness:gate_status()),
    lightspeed@release@production_readiness:risk_class()
) -> boolean().
has_risk_class(Gates, Expected) ->
    case Gates of
        [] ->
            false;

        [Entry | Rest] ->
            case lightspeed@release@production_readiness:risk_class(Entry) =:= Expected of
                true ->
                    true;

                false ->
                    has_risk_class(Rest, Expected)
            end
    end.

-file("src/lightspeed/ops/production_readiness_harness.gleam", 201).
-spec evaluate_risk_class_coverage() -> {boolean(), binary()}.
evaluate_risk_class_coverage() ->
    Gates = lightspeed@release@production_readiness:required_gates(),
    Has_runtime = has_risk_class(Gates, runtime_resilience),
    Has_data = has_risk_class(Gates, data_integrity),
    Has_tenant = has_risk_class(Gates, tenant_isolation),
    Has_ops = has_risk_class(Gates, operational_control),
    Has_release = has_risk_class(Gates, release_governance),
    Passed = (((Has_runtime andalso Has_data) andalso Has_tenant) andalso Has_ops)
    andalso Has_release,
    Signature = <<<<<<<<<<<<<<<<<<"runtime_resilience="/utf8,
                                        (bool_label(Has_runtime))/binary>>/binary,
                                    "|data_integrity="/utf8>>/binary,
                                (bool_label(Has_data))/binary>>/binary,
                            "|tenant_isolation="/utf8>>/binary,
                        (bool_label(Has_tenant))/binary>>/binary,
                    "|operational_control="/utf8>>/binary,
                (bool_label(Has_ops))/binary>>/binary,
            "|release_governance="/utf8>>/binary,
        (bool_label(Has_release))/binary>>,
    {Passed, Signature}.

-file("src/lightspeed/ops/production_readiness_harness.gleam", 187).
-spec evaluate_gate_prefix_certification() -> {boolean(), binary()}.
evaluate_gate_prefix_certification() ->
    Gates = lightspeed@release@production_readiness:required_gates(),
    Signatures = gleam@list:map(
        Gates,
        fun lightspeed@release@production_readiness:gate_signature/1
    ),
    Joined = join_with(<<";"/utf8>>, Signatures),
    Passed = (((lightspeed@release@production_readiness:ready() andalso (erlang:length(
        Gates
    )
    =:= 11))
    andalso gleam_stdlib:contains_string(
        Joined,
        <<"name=m27_failover_fixture"/utf8>>
    ))
    andalso gleam_stdlib:contains_string(
        Joined,
        <<"name=m33_etl_reliability_fixture"/utf8>>
    ))
    andalso gleam_stdlib:contains_string(
        Joined,
        <<"name=m60_pipeline_operations_surface_fixture"/utf8>>
    ),
    {Passed, Joined}.

-file("src/lightspeed/ops/production_readiness_harness.gleam", 177).
-spec evaluate(scenario()) -> {boolean(), binary()}.
evaluate(Scenario) ->
    case Scenario of
        gate_prefix_certification ->
            evaluate_gate_prefix_certification();

        risk_class_coverage ->
            evaluate_risk_class_coverage();

        operator_recovery_coverage ->
            evaluate_operator_recovery_coverage();

        deterministic_snapshot_gate ->
            evaluate_deterministic_snapshot_gate();

        required_gate_passes ->
            evaluate_required_gate_passes()
    end.

-file("src/lightspeed/ops/production_readiness_harness.gleam", 58).
?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/production_readiness_harness.gleam", 39).
?DOC(" Run all production-readiness scenarios.\n").
-spec run_matrix() -> report().
run_matrix() ->
    Outcomes = begin
        _pipe = [gate_prefix_certification,
            risk_class_coverage,
            operator_recovery_coverage,
            deterministic_snapshot_gate,
            required_gate_passes],
        gleam@list:map(_pipe, fun run_scenario/1)
    end,
    {report, Outcomes, count_failed(Outcomes), count_nondeterministic(Outcomes)}.

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

        risk_class_coverage ->
            <<"risk_class_coverage"/utf8>>;

        operator_recovery_coverage ->
            <<"operator_recovery_coverage"/utf8>>;

        deterministic_snapshot_gate ->
            <<"deterministic_snapshot_gate"/utf8>>;

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

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

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

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

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

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

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

-file("src/lightspeed/ops/production_readiness_harness.gleam", 123).
?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/production_readiness_harness.gleam", 139).
?DOC(" Deterministic snapshot signature for production-readiness drift gates.\n").
-spec snapshot_signature() -> binary().
snapshot_signature() ->
    <<<<<<"prod.ready.fixture.v"/utf8, (erlang:integer_to_binary(1))/binary>>/binary,
            "|"/utf8>>/binary,
        (report_signature(run_matrix()))/binary>>.

-file("src/lightspeed/ops/production_readiness_harness.gleam", 147).
?DOC(" Deterministic markdown report for production-readiness 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,
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"# Production Readiness 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>>.