src/lightspeed@ops@app_platform_harness.erl

-module(lightspeed@ops@app_platform_harness).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/lightspeed/ops/app_platform_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 app-platform conformance harness for M38.\n").

-type scenario() :: reference_composition_patterns |
    integration_seam_overrides |
    lifecycle_workflow_conformance |
    workflow_without_framework_patching.

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

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

-file("src/lightspeed/ops/app_platform_harness.gleam", 305).
-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/app_platform_harness.gleam", 294).
-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/app_platform_harness.gleam", 323).
-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/app_platform_harness.gleam", 316).
-spec bool_label(boolean()) -> binary().
bool_label(Value) ->
    case Value of
        true ->
            <<"true"/utf8>>;

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

-file("src/lightspeed/ops/app_platform_harness.gleam", 282).
-spec profile_workflows_run_without_patching(
    lightspeed@platform@foundation:app_profile(),
    list(lightspeed@platform@foundation:lifecycle_workflow())
) -> boolean().
profile_workflows_run_without_patching(Profile, Required_workflows) ->
    case Required_workflows of
        [] ->
            true;

        [Workflow | Rest] ->
            lightspeed@platform@foundation:workflow_runs_without_internal_patching(
                Profile,
                Workflow
            )
            andalso profile_workflows_run_without_patching(Profile, Rest)
    end.

-file("src/lightspeed/ops/app_platform_harness.gleam", 269).
-spec profiles_run_without_framework_patching(
    list(lightspeed@platform@foundation:app_profile()),
    list(lightspeed@platform@foundation:lifecycle_workflow())
) -> boolean().
profiles_run_without_framework_patching(Profiles, Required_workflows) ->
    case Profiles of
        [] ->
            true;

        [Profile | Rest] ->
            (not lightspeed@platform@foundation:requires_framework_internal_patching(
                Profile
            )
            andalso profile_workflows_run_without_patching(
                Profile,
                Required_workflows
            ))
            andalso profiles_run_without_framework_patching(
                Rest,
                Required_workflows
            )
    end.

-file("src/lightspeed/ops/app_platform_harness.gleam", 213).
-spec evaluate_workflow_without_framework_patching() -> {boolean(), binary()}.
evaluate_workflow_without_framework_patching() ->
    Profiles = lightspeed@platform@foundation:reference_profiles(),
    Required = lightspeed@platform@foundation:required_workflows(),
    Passed = profiles_run_without_framework_patching(Profiles, Required),
    Entries = gleam@list:map(
        Profiles,
        fun(Profile) ->
            <<<<(lightspeed@platform@foundation:name(Profile))/binary,
                    ":patching_required="/utf8>>/binary,
                (bool_label(
                    lightspeed@platform@foundation:requires_framework_internal_patching(
                        Profile
                    )
                ))/binary>>
        end
    ),
    {Passed, join_with(<<";"/utf8>>, Entries)}.

-file("src/lightspeed/ops/app_platform_harness.gleam", 257).
-spec profile_supports_workflows(
    lightspeed@platform@foundation:app_profile(),
    list(lightspeed@platform@foundation:lifecycle_workflow())
) -> boolean().
profile_supports_workflows(Profile, Required_workflows) ->
    case Required_workflows of
        [] ->
            true;

        [Workflow | Rest] ->
            lightspeed@platform@foundation:supports_workflow(Profile, Workflow)
            andalso profile_supports_workflows(Profile, Rest)
    end.

-file("src/lightspeed/ops/app_platform_harness.gleam", 245).
-spec profiles_support_required_workflows(
    list(lightspeed@platform@foundation:app_profile()),
    list(lightspeed@platform@foundation:lifecycle_workflow())
) -> boolean().
profiles_support_required_workflows(Profiles, Required_workflows) ->
    case Profiles of
        [] ->
            true;

        [Profile | Rest] ->
            profile_supports_workflows(Profile, Required_workflows) andalso profiles_support_required_workflows(
                Rest,
                Required_workflows
            )
    end.

-file("src/lightspeed/ops/app_platform_harness.gleam", 204).
-spec evaluate_lifecycle_workflow_conformance() -> {boolean(), binary()}.
evaluate_lifecycle_workflow_conformance() ->
    Profiles = lightspeed@platform@foundation:reference_profiles(),
    Entries = gleam@list:map(
        Profiles,
        fun lightspeed@platform@foundation:workflow_matrix_signature/1
    ),
    Required = lightspeed@platform@foundation:required_workflows(),
    Passed = profiles_support_required_workflows(Profiles, Required),
    {Passed, join_with(<<";"/utf8>>, Entries)}.

-file("src/lightspeed/ops/app_platform_harness.gleam", 234).
-spec profiles_all_have_explicit_overrides(
    list(lightspeed@platform@foundation:app_profile())
) -> boolean().
profiles_all_have_explicit_overrides(Profiles) ->
    case Profiles of
        [] ->
            true;

        [Profile | Rest] ->
            lightspeed@platform@foundation:explicit_override_points(Profile)
            andalso profiles_all_have_explicit_overrides(Rest)
    end.

-file("src/lightspeed/ops/app_platform_harness.gleam", 191).
-spec evaluate_integration_seam_overrides() -> {boolean(), binary()}.
evaluate_integration_seam_overrides() ->
    Profiles = lightspeed@platform@foundation:reference_profiles(),
    Entries = gleam@list:map(
        Profiles,
        fun(Profile) ->
            <<<<(lightspeed@platform@foundation:name(Profile))/binary,
                    ":"/utf8>>/binary,
                (lightspeed@platform@foundation:seam_contract_signature(Profile))/binary>>
        end
    ),
    Passed = profiles_all_have_explicit_overrides(Profiles),
    {Passed, join_with(<<";"/utf8>>, Entries)}.

-file("src/lightspeed/ops/app_platform_harness.gleam", 227).
-spec profiles_all_valid(list(lightspeed@platform@foundation:app_profile())) -> boolean().
profiles_all_valid(Profiles) ->
    case Profiles of
        [] ->
            true;

        [Profile | Rest] ->
            lightspeed@platform@foundation:valid(Profile) andalso profiles_all_valid(
                Rest
            )
    end.

-file("src/lightspeed/ops/app_platform_harness.gleam", 183).
-spec evaluate_reference_composition_patterns() -> {boolean(), binary()}.
evaluate_reference_composition_patterns() ->
    Profiles = lightspeed@platform@foundation:reference_profiles(),
    Signatures = gleam@list:map(
        Profiles,
        fun lightspeed@platform@foundation:signature/1
    ),
    Passed = (erlang:length(Profiles) =:= 3) andalso profiles_all_valid(
        Profiles
    ),
    {Passed, join_with(<<";"/utf8>>, Signatures)}.

-file("src/lightspeed/ops/app_platform_harness.gleam", 173).
-spec evaluate(scenario()) -> {boolean(), binary()}.
evaluate(Scenario) ->
    case Scenario of
        reference_composition_patterns ->
            evaluate_reference_composition_patterns();

        integration_seam_overrides ->
            evaluate_integration_seam_overrides();

        lifecycle_workflow_conformance ->
            evaluate_lifecycle_workflow_conformance();

        workflow_without_framework_patching ->
            evaluate_workflow_without_framework_patching()
    end.

-file("src/lightspeed/ops/app_platform_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/app_platform_harness.gleam", 37).
?DOC(" Run all M38 scenarios.\n").
-spec run_matrix() -> report().
run_matrix() ->
    Outcomes = begin
        _pipe = [reference_composition_patterns,
            integration_seam_overrides,
            lifecycle_workflow_conformance,
            workflow_without_framework_patching],
        gleam@list:map(_pipe, fun run_scenario/1)
    end,
    {report, Outcomes, count_failed(Outcomes), count_nondeterministic(Outcomes)}.

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

        integration_seam_overrides ->
            <<"integration_seam_overrides"/utf8>>;

        lifecycle_workflow_conformance ->
            <<"lifecycle_workflow_conformance"/utf8>>;

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

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

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

-file("src/lightspeed/ops/app_platform_harness.gleam", 99).
?DOC(" Scenario determinism accessor.\n").
-spec deterministic(scenario_outcome()) -> boolean().
deterministic(Outcome) ->
    erlang:element(4, Outcome).

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

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

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

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

-file("src/lightspeed/ops/app_platform_harness.gleam", 143).
?DOC(" Deterministic markdown report for M38 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,
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"# App Platform 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>>.