src/lightspeed@ops@documentation_system_harness.erl

-module(lightspeed@ops@documentation_system_harness).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/lightspeed/ops/documentation_system_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 documentation-system conformance harness for M46.\n").

-type scenario() :: coherent_documentation_system_surface |
    roadmap_module_fixture_drift_contracts |
    versioned_upgrade_troubleshooting_paths |
    documentation_evidence_update_requirements.

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

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

-file("src/lightspeed/ops/documentation_system_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/documentation_system_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/documentation_system_harness.gleam", 286).
-spec contains(list(binary()), binary()) -> boolean().
contains(Values, Expected) ->
    case Values of
        [] ->
            false;

        [Value | Rest] ->
            case Value =:= Expected of
                true ->
                    true;

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

-file("src/lightspeed/ops/documentation_system_harness.gleam", 239).
-spec evaluate_documentation_evidence_update_requirements() -> {boolean(),
    binary()}.
evaluate_documentation_evidence_update_requirements() ->
    Requirements = lightspeed@tooling@documentation_system:evidence_requirements(
        
    ),
    Passed = ((((contains(Requirements, <<"ROADMAP.md"/utf8>>) andalso contains(
        Requirements,
        <<"MILESTONES.md"/utf8>>
    ))
    andalso contains(Requirements, <<"CHANGELOG.md"/utf8>>))
    andalso contains(Requirements, <<"rfcs/"/utf8>>))
    andalso contains(Requirements, <<"adrs/"/utf8>>))
    andalso contains(
        Requirements,
        <<"docs/milestone_upgrade_troubleshooting.md"/utf8>>
    ),
    {Passed,
        lightspeed@tooling@documentation_system:evidence_requirement_signature()}.

-file("src/lightspeed/ops/documentation_system_harness.gleam", 308).
-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/documentation_system_harness.gleam", 297).
-spec contains_string(binary(), binary()) -> boolean().
contains_string(Value, Expected) ->
    (Value =:= Expected) orelse gleam_stdlib:contains_string(Value, Expected).

-file("src/lightspeed/ops/documentation_system_harness.gleam", 252).
-spec paths_use_shared_upgrade_doc(list(binary())) -> boolean().
paths_use_shared_upgrade_doc(Upgrades) ->
    case Upgrades of
        [] ->
            true;

        [Entry | Rest] ->
            contains_string(
                Entry,
                <<"upgrade_doc=docs/milestone_upgrade_troubleshooting.md"/utf8>>
            )
            andalso paths_use_shared_upgrade_doc(Rest)
    end.

-file("src/lightspeed/ops/documentation_system_harness.gleam", 217).
-spec evaluate_versioned_upgrade_troubleshooting_paths() -> {boolean(),
    binary()}.
evaluate_versioned_upgrade_troubleshooting_paths() ->
    Upgrades = lightspeed@tooling@documentation_system:versioned_upgrade_paths(),
    Troubleshooting = lightspeed@tooling@documentation_system:versioned_troubleshooting_paths(
        
    ),
    Expected = [<<"m40"/utf8>>,
        <<"m41"/utf8>>,
        <<"m42"/utf8>>,
        <<"m43"/utf8>>,
        <<"m44"/utf8>>,
        <<"m45"/utf8>>,
        <<"m46"/utf8>>],
    Milestones = gleam@list:map(
        lightspeed@tooling@documentation_system:reference_bundles(),
        fun lightspeed@tooling@documentation_system:milestone/1
    ),
    Passed = ((Milestones =:= Expected) andalso paths_use_shared_upgrade_doc(
        Upgrades
    ))
    andalso (erlang:length(Troubleshooting) =:= 7),
    Signatures = gleam@list:map(
        lightspeed@tooling@documentation_system:reference_bundles(),
        fun lightspeed@tooling@documentation_system:upgrade_troubleshooting_signature/1
    ),
    {Passed, join_with(<<";"/utf8>>, Signatures)}.

-file("src/lightspeed/ops/documentation_system_harness.gleam", 200).
-spec evaluate_roadmap_module_fixture_drift_contracts() -> {boolean(), binary()}.
evaluate_roadmap_module_fixture_drift_contracts() ->
    Bundles = lightspeed@tooling@documentation_system:reference_bundles(),
    Passed = lightspeed@tooling@documentation_system:drift_check_contracts(),
    Module_entries = gleam@list:map(
        Bundles,
        fun lightspeed@tooling@documentation_system:module_reference_signature/1
    ),
    Fixture_entries = gleam@list:map(
        Bundles,
        fun lightspeed@tooling@documentation_system:fixture_reference_signature/1
    ),
    {Passed,
        <<<<<<"modules="/utf8,
                    (join_with(<<";"/utf8>>, Module_entries))/binary>>/binary,
                "|fixtures="/utf8>>/binary,
            (join_with(<<";"/utf8>>, Fixture_entries))/binary>>}.

-file("src/lightspeed/ops/documentation_system_harness.gleam", 191).
-spec evaluate_coherent_documentation_system_surface() -> {boolean(), binary()}.
evaluate_coherent_documentation_system_surface() ->
    Bundles = lightspeed@tooling@documentation_system:reference_bundles(),
    Passed = lightspeed@tooling@documentation_system:coherent_system() andalso (erlang:length(
        Bundles
    )
    =:= 7),
    Entries = gleam@list:map(
        Bundles,
        fun lightspeed@tooling@documentation_system:signature/1
    ),
    {Passed, join_with(<<";"/utf8>>, Entries)}.

-file("src/lightspeed/ops/documentation_system_harness.gleam", 178).
-spec evaluate(scenario()) -> {boolean(), binary()}.
evaluate(Scenario) ->
    case Scenario of
        coherent_documentation_system_surface ->
            evaluate_coherent_documentation_system_surface();

        roadmap_module_fixture_drift_contracts ->
            evaluate_roadmap_module_fixture_drift_contracts();

        versioned_upgrade_troubleshooting_paths ->
            evaluate_versioned_upgrade_troubleshooting_paths();

        documentation_evidence_update_requirements ->
            evaluate_documentation_evidence_update_requirements()
    end.

-file("src/lightspeed/ops/documentation_system_harness.gleam", 56).
?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/documentation_system_harness.gleam", 38).
?DOC(" Run all M46 scenarios.\n").
-spec run_matrix() -> report().
run_matrix() ->
    Outcomes = begin
        _pipe = [coherent_documentation_system_surface,
            roadmap_module_fixture_drift_contracts,
            versioned_upgrade_troubleshooting_paths,
            documentation_evidence_update_requirements],
        gleam@list:map(_pipe, fun run_scenario/1)
    end,
    {report, Outcomes, count_failed(Outcomes), count_nondeterministic(Outcomes)}.

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

        roadmap_module_fixture_drift_contracts ->
            <<"roadmap_module_fixture_drift_contracts"/utf8>>;

        versioned_upgrade_troubleshooting_paths ->
            <<"versioned_upgrade_troubleshooting_paths"/utf8>>;

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

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

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

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

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

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

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

-file("src/lightspeed/ops/documentation_system_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/documentation_system_harness.gleam", 124).
?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/documentation_system_harness.gleam", 140).
?DOC(" Deterministic snapshot signature for M46 fixture drift gates.\n").
-spec snapshot_signature() -> binary().
snapshot_signature() ->
    <<<<<<"m46.snapshot.v"/utf8, (erlang:integer_to_binary(1))/binary>>/binary,
            "|"/utf8>>/binary,
        (report_signature(run_matrix()))/binary>>.

-file("src/lightspeed/ops/documentation_system_harness.gleam", 148).
?DOC(" Deterministic markdown report for M46 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,
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"# Documentation System 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>>.