src/lightspeed@ops@data_ergonomics_harness.erl

-module(lightspeed@ops@data_ergonomics_harness).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/lightspeed/ops/data_ergonomics_harness.gleam").
-export([pass_fail_label/1, run_scenario/1, run_matrix/0, profile_version_label/0, scenario_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 ORM/data ergonomics parity harness for M57.\n").

-type scenario() :: crud_and_dashboard_ergonomics_coverage |
    nested_changeset_validation_propagation |
    unit_of_work_and_optimistic_concurrency_contracts |
    query_diagnostics_regression_signals |
    regression_gate_determinism.

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

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

-file("src/lightspeed/ops/data_ergonomics_harness.gleam", 418).
-spec count_nondeterministic(list(scenario_outcome())) -> integer().
count_nondeterministic(Outcomes) ->
    _pipe = Outcomes,
    _pipe@1 = gleam@list:filter(
        _pipe,
        fun(Outcome) -> not erlang:element(4, Outcome) end
    ),
    erlang:length(_pipe@1).

-file("src/lightspeed/ops/data_ergonomics_harness.gleam", 412).
-spec count_failed(list(scenario_outcome())) -> integer().
count_failed(Outcomes) ->
    _pipe = Outcomes,
    _pipe@1 = gleam@list:filter(
        _pipe,
        fun(Outcome) -> not erlang:element(3, Outcome) end
    ),
    erlang:length(_pipe@1).

-file("src/lightspeed/ops/data_ergonomics_harness.gleam", 95).
?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/data_ergonomics_harness.gleam", 327).
-spec evaluate_query_diagnostics_regression_signals() -> {boolean(), binary()}.
evaluate_query_diagnostics_regression_signals() ->
    Noisy = begin
        _pipe = lightspeed@data_ergonomics:query_shape(
            <<"dashboard_posts"/utf8>>,
            120,
            8,
            190,
            <<"trace-m57-a"/utf8>>
        ),
        lightspeed@data_ergonomics:diagnose_query(_pipe)
    end,
    Healthy = begin
        _pipe@1 = lightspeed@data_ergonomics:query_shape(
            <<"dashboard_posts"/utf8>>,
            120,
            2,
            45,
            <<"trace-m57-b"/utf8>>
        ),
        lightspeed@data_ergonomics:diagnose_query(_pipe@1)
    end,
    Noisy_signature = lightspeed@data_ergonomics:diagnostics_signature(Noisy),
    Healthy_signature = lightspeed@data_ergonomics:diagnostics_signature(
        Healthy
    ),
    Passed = (Noisy_signature =:= <<"n_plus_one:dashboard_posts:statements=8;slow_query:dashboard_posts:latency_ms=190;trace_correlated:trace-m57-a:query=dashboard_posts"/utf8>>)
    andalso (Healthy_signature =:= <<"trace_correlated:trace-m57-b:query=dashboard_posts"/utf8>>),
    {Passed,
        <<<<<<"noisy="/utf8, Noisy_signature/binary>>/binary, "|healthy="/utf8>>/binary,
            Healthy_signature/binary>>}.

-file("src/lightspeed/ops/data_ergonomics_harness.gleam", 431).
-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/data_ergonomics_harness.gleam", 395).
-spec published_changeset(binary()) -> lightspeed@data_ergonomics:changeset().
published_changeset(Title) ->
    lightspeed@data_ergonomics:changeset(
        <<"article"/utf8>>,
        [lightspeed@data_ergonomics:text_field(
                <<"title"/utf8>>,
                Title,
                [required, {min_length, 3}]
            ),
            lightspeed@data_ergonomics:text_field(
                <<"status"/utf8>>,
                <<"published"/utf8>>,
                [required]
            ),
            lightspeed@data_ergonomics:int_field(
                <<"views"/utf8>>,
                10,
                [{min_int, 0}]
            )],
        []
    ).

-file("src/lightspeed/ops/data_ergonomics_harness.gleam", 380).
-spec draft_changeset(binary()) -> lightspeed@data_ergonomics:changeset().
draft_changeset(Title) ->
    lightspeed@data_ergonomics:changeset(
        <<"article"/utf8>>,
        [lightspeed@data_ergonomics:text_field(
                <<"title"/utf8>>,
                Title,
                [required, {min_length, 3}]
            ),
            lightspeed@data_ergonomics:text_field(
                <<"status"/utf8>>,
                <<"draft"/utf8>>,
                [required]
            ),
            lightspeed@data_ergonomics:int_field(
                <<"views"/utf8>>,
                0,
                [{min_int, 0}]
            )],
        []
    ).

-file("src/lightspeed/ops/data_ergonomics_harness.gleam", 267).
-spec evaluate_unit_of_work_and_optimistic_concurrency_contracts() -> {boolean(),
    binary()}.
evaluate_unit_of_work_and_optimistic_concurrency_contracts() ->
    Runtime = lightspeed@data_ergonomics:new_runtime(),
    {Runtime@2, Created@1} = case lightspeed@data_ergonomics:create(
        Runtime,
        draft_changeset(<<"Article A"/utf8>>)
    ) of
        {ok, {Runtime@1, Created}} -> {Runtime@1, Created};
        _assert_fail ->
            erlang:error(#{gleam_error => let_assert,
                        message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                        file => <<?FILEPATH/utf8>>,
                        module => <<"lightspeed/ops/data_ergonomics_harness"/utf8>>,
                        function => <<"evaluate_unit_of_work_and_optimistic_concurrency_contracts"/utf8>>,
                        line => 272,
                        value => _assert_fail,
                        start => 7419,
                        'end' => 7521,
                        pattern_start => 7430,
                        pattern_end => 7453})
    end,
    Failing_uow = lightspeed@data_ergonomics:unit_of_work(
        <<"fail-fast"/utf8>>,
        [{update_from_changeset,
                erlang:element(2, Created@1),
                0,
                published_changeset(<<"Article A"/utf8>>)},
            {create_from_changeset, published_changeset(<<"Article B"/utf8>>)}]
    ),
    Failed_label = case lightspeed@data_ergonomics:run_unit_of_work(
        Runtime@2,
        Failing_uow
    ) of
        {error, Error} ->
            lightspeed@data_ergonomics:unit_of_work_error_label(Error);

        {ok, _} ->
            <<"unexpected_success"/utf8>>
    end,
    Successful_uow = lightspeed@data_ergonomics:unit_of_work(
        <<"success"/utf8>>,
        [{update_from_changeset,
                erlang:element(2, Created@1),
                erlang:element(3, Created@1),
                published_changeset(<<"Article A"/utf8>>)},
            {create_from_changeset, published_changeset(<<"Article B"/utf8>>)}]
    ),
    {Passed, Success_label} = case lightspeed@data_ergonomics:run_unit_of_work(
        Runtime@2,
        Successful_uow
    ) of
        {error, Error@1} ->
            {false,
                lightspeed@data_ergonomics:unit_of_work_error_label(Error@1)};

        {ok, {Next_runtime, Signatures}} ->
            {(Signatures =:= [<<"update:row-1:rev=2"/utf8>>,
                    <<"create:row-2:rev=1"/utf8>>])
                andalso (lightspeed@data_ergonomics:dashboard_signature(
                    lightspeed@data_ergonomics:dashboard_summary(Next_runtime)
                )
                =:= <<"total=2:draft=0:published=2:stale=0"/utf8>>),
                <<"signatures="/utf8,
                    (join_with(<<","/utf8>>, Signatures))/binary>>}
    end,
    {Passed andalso (Failed_label =:= <<"operation_failed:1:concurrency_conflict:row-1:expected=0:actual=1"/utf8>>),
        <<<<<<"failed="/utf8, Failed_label/binary>>/binary, "|success="/utf8>>/binary,
            Success_label/binary>>}.

-file("src/lightspeed/ops/data_ergonomics_harness.gleam", 233).
-spec evaluate_nested_changeset_validation_propagation() -> {boolean(),
    binary()}.
evaluate_nested_changeset_validation_propagation() ->
    Validated = begin
        _pipe = lightspeed@data_ergonomics:changeset(
            <<"invoice"/utf8>>,
            [lightspeed@data_ergonomics:text_field(
                    <<"customer_name"/utf8>>,
                    <<"ab"/utf8>>,
                    [required, {min_length, 3}]
                )],
            [lightspeed@data_ergonomics:changeset(
                    <<"line_item"/utf8>>,
                    [lightspeed@data_ergonomics:empty_field(
                            <<"sku"/utf8>>,
                            [required]
                        ),
                        lightspeed@data_ergonomics:int_field(
                            <<"quantity"/utf8>>,
                            -1,
                            [{min_int, 1}]
                        )],
                    []
                )]
        ),
        lightspeed@data_ergonomics:validate_changeset(_pipe)
    end,
    Signature = lightspeed@data_ergonomics:changeset_error_signature(Validated),
    Passed = ((not lightspeed@data_ergonomics:changeset_valid(Validated) andalso gleam_stdlib:contains_string(
        Signature,
        <<"min_length:customer_name:3"/utf8>>
    ))
    andalso gleam_stdlib:contains_string(
        Signature,
        <<"nested:line_item:required:sku"/utf8>>
    ))
    andalso gleam_stdlib:contains_string(
        Signature,
        <<"nested:line_item:min_int:quantity:1"/utf8>>
    ),
    {Passed, Signature}.

-file("src/lightspeed/ops/data_ergonomics_harness.gleam", 204).
-spec evaluate_crud_and_dashboard_ergonomics_coverage() -> {boolean(), binary()}.
evaluate_crud_and_dashboard_ergonomics_coverage() ->
    Runtime = lightspeed@data_ergonomics:new_runtime(),
    {Runtime@2, Created_a@1} = case lightspeed@data_ergonomics:create(
        Runtime,
        draft_changeset(<<"Draft A"/utf8>>)
    ) of
        {ok, {Runtime@1, Created_a}} -> {Runtime@1, Created_a};
        _assert_fail ->
            erlang:error(#{gleam_error => let_assert,
                        message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                        file => <<?FILEPATH/utf8>>,
                        module => <<"lightspeed/ops/data_ergonomics_harness"/utf8>>,
                        function => <<"evaluate_crud_and_dashboard_ergonomics_coverage"/utf8>>,
                        line => 206,
                        value => _assert_fail,
                        start => 5399,
                        'end' => 5501,
                        pattern_start => 5410,
                        pattern_end => 5435})
    end,
    Runtime@4 = case lightspeed@data_ergonomics:create(
        Runtime@2,
        published_changeset(<<"Published B"/utf8>>)
    ) of
        {ok, {Runtime@3, _}} -> Runtime@3;
        _assert_fail@1 ->
            erlang:error(#{gleam_error => let_assert,
                        message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                        file => <<?FILEPATH/utf8>>,
                        module => <<"lightspeed/ops/data_ergonomics_harness"/utf8>>,
                        function => <<"evaluate_crud_and_dashboard_ergonomics_coverage"/utf8>>,
                        line => 208,
                        value => _assert_fail@1,
                        start => 5504,
                        'end' => 5615,
                        pattern_start => 5515,
                        pattern_end => 5541})
    end,
    {Runtime@6, Updated_a@1} = case lightspeed@data_ergonomics:update(
        Runtime@4,
        erlang:element(2, Created_a@1),
        erlang:element(3, Created_a@1),
        published_changeset(<<"Published A"/utf8>>)
    ) of
        {ok, {Runtime@5, Updated_a}} -> {Runtime@5, Updated_a};
        _assert_fail@2 ->
            erlang:error(#{gleam_error => let_assert,
                        message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                        file => <<?FILEPATH/utf8>>,
                        module => <<"lightspeed/ops/data_ergonomics_harness"/utf8>>,
                        function => <<"evaluate_crud_and_dashboard_ergonomics_coverage"/utf8>>,
                        line => 210,
                        value => _assert_fail@2,
                        start => 5618,
                        'end' => 5793,
                        pattern_start => 5629,
                        pattern_end => 5654})
    end,
    Dashboard = lightspeed@data_ergonomics:dashboard_summary(Runtime@6),
    Passed = ((erlang:element(3, Updated_a@1) =:= 2) andalso (lightspeed@data_ergonomics:dashboard_signature(
        Dashboard
    )
    =:= <<"total=2:draft=0:published=2:stale=0"/utf8>>))
    andalso gleam_stdlib:contains_string(
        lightspeed@data_ergonomics:runtime_signature(Runtime@6),
        <<"rows=2"/utf8>>
    ),
    {Passed,
        <<<<<<"runtime="/utf8,
                    (lightspeed@data_ergonomics:runtime_signature(Runtime@6))/binary>>/binary,
                "|dashboard="/utf8>>/binary,
            (lightspeed@data_ergonomics:dashboard_signature(Dashboard))/binary>>}.

-file("src/lightspeed/ops/data_ergonomics_harness.gleam", 344).
-spec evaluate_regression_gate_determinism() -> {boolean(), binary()}.
evaluate_regression_gate_determinism() ->
    Crud = run_scenario(crud_and_dashboard_ergonomics_coverage),
    Nested = run_scenario(nested_changeset_validation_propagation),
    Uow = run_scenario(unit_of_work_and_optimistic_concurrency_contracts),
    Diagnostics = run_scenario(query_diagnostics_regression_signals),
    Passed = ((((((erlang:element(3, Crud) andalso erlang:element(3, Nested))
    andalso erlang:element(3, Uow))
    andalso erlang:element(3, Diagnostics))
    andalso erlang:element(4, Crud))
    andalso erlang:element(4, Nested))
    andalso erlang:element(4, Uow))
    andalso erlang:element(4, Diagnostics),
    {Passed,
        <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"crud="/utf8,
                                                                    (pass_fail_label(
                                                                        Crud
                                                                    ))/binary>>/binary,
                                                                ":"/utf8>>/binary,
                                                            (erlang:element(
                                                                5,
                                                                Crud
                                                            ))/binary>>/binary,
                                                        "|nested="/utf8>>/binary,
                                                    (pass_fail_label(Nested))/binary>>/binary,
                                                ":"/utf8>>/binary,
                                            (erlang:element(5, Nested))/binary>>/binary,
                                        "|uow="/utf8>>/binary,
                                    (pass_fail_label(Uow))/binary>>/binary,
                                ":"/utf8>>/binary,
                            (erlang:element(5, Uow))/binary>>/binary,
                        "|diagnostics="/utf8>>/binary,
                    (pass_fail_label(Diagnostics))/binary>>/binary,
                ":"/utf8>>/binary,
            (erlang:element(5, Diagnostics))/binary>>}.

-file("src/lightspeed/ops/data_ergonomics_harness.gleam", 190).
-spec evaluate(scenario()) -> {boolean(), binary()}.
evaluate(Scenario) ->
    case Scenario of
        crud_and_dashboard_ergonomics_coverage ->
            evaluate_crud_and_dashboard_ergonomics_coverage();

        nested_changeset_validation_propagation ->
            evaluate_nested_changeset_validation_propagation();

        unit_of_work_and_optimistic_concurrency_contracts ->
            evaluate_unit_of_work_and_optimistic_concurrency_contracts();

        query_diagnostics_regression_signals ->
            evaluate_query_diagnostics_regression_signals();

        regression_gate_determinism ->
            evaluate_regression_gate_determinism()
    end.

-file("src/lightspeed/ops/data_ergonomics_harness.gleam", 60).
?DOC(" Run one M57 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/data_ergonomics_harness.gleam", 41).
?DOC(" Run all M57 scenarios.\n").
-spec run_matrix() -> report().
run_matrix() ->
    Outcomes = begin
        _pipe = [crud_and_dashboard_ergonomics_coverage,
            nested_changeset_validation_propagation,
            unit_of_work_and_optimistic_concurrency_contracts,
            query_diagnostics_regression_signals,
            regression_gate_determinism],
        gleam@list:map(_pipe, fun run_scenario/1)
    end,
    {report, Outcomes, count_failed(Outcomes), count_nondeterministic(Outcomes)}.

-file("src/lightspeed/ops/data_ergonomics_harness.gleam", 76).
?DOC(" M57 profile version label.\n").
-spec profile_version_label() -> binary().
profile_version_label() ->
    <<"m57.profile.v"/utf8, (erlang:integer_to_binary(1))/binary>>.

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

        nested_changeset_validation_propagation ->
            <<"nested_changeset_validation_propagation"/utf8>>;

        unit_of_work_and_optimistic_concurrency_contracts ->
            <<"unit_of_work_and_optimistic_concurrency_contracts"/utf8>>;

        query_diagnostics_regression_signals ->
            <<"query_diagnostics_regression_signals"/utf8>>;

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

-file("src/lightspeed/ops/data_ergonomics_harness.gleam", 103).
?DOC(" Scenario signature accessor.\n").
-spec signature(scenario_outcome()) -> binary().
signature(Outcome) ->
    erlang:element(5, Outcome).

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

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

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

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

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

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

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

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

-file("src/lightspeed/ops/data_ergonomics_harness.gleam", 157).
?DOC(" Deterministic markdown report for M57 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,
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"# Data Ergonomics Fixture Report\n\n"/utf8,
                                                                                        "snapshot_version: "/utf8>>/binary,
                                                                                    (erlang:integer_to_binary(
                                                                                        1
                                                                                    ))/binary>>/binary,
                                                                                "\n"/utf8>>/binary,
                                                                            "profile_version: "/utf8>>/binary,
                                                                        (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>>.