|
| 1 | +# SPDX-FileCopyrightText: 2025 ash_typescript contributors <https://github.com/ash-project/ash_typescript/graphs/contributors> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +defmodule AshTypescript.Resource.Transformers.PersistFormattedFieldsTest do |
| 6 | + use ExUnit.Case, async: true |
| 7 | + |
| 8 | + alias AshTypescript.FieldFormatter |
| 9 | + alias AshTypescript.Resource.Info |
| 10 | + |
| 11 | + defmodule PlainResource do |
| 12 | + use Ash.Resource, |
| 13 | + domain: nil, |
| 14 | + data_layer: Ash.DataLayer.Ets, |
| 15 | + extensions: [AshTypescript.Resource] |
| 16 | + |
| 17 | + typescript do |
| 18 | + type_name "PlainResource" |
| 19 | + |
| 20 | + field_names address_line_1: "addressLine1", |
| 21 | + is_active?: "isActive" |
| 22 | + end |
| 23 | + |
| 24 | + attributes do |
| 25 | + uuid_primary_key :id |
| 26 | + attribute :first_name, :string, public?: true |
| 27 | + attribute :address_line_1, :string, public?: true |
| 28 | + attribute :is_active?, :boolean, public?: true |
| 29 | + attribute :is_super_admin, :boolean, public?: true |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + defmodule ResourceWithCallback do |
| 34 | + use Ash.Resource, |
| 35 | + domain: nil, |
| 36 | + data_layer: Ash.DataLayer.Ets, |
| 37 | + extensions: [AshTypescript.Resource] |
| 38 | + |
| 39 | + typescript do |
| 40 | + type_name "ResourceWithCallback" |
| 41 | + field_names is_active?: "isActiveFromDsl" |
| 42 | + end |
| 43 | + |
| 44 | + attributes do |
| 45 | + uuid_primary_key :id |
| 46 | + attribute :is_active?, :boolean, public?: true |
| 47 | + attribute :first_name, :string, public?: true |
| 48 | + end |
| 49 | + |
| 50 | + # Module-level callback — takes priority over `field_names` DSL per the |
| 51 | + # documented precedence in `compute_field_for_client/3`. |
| 52 | + def typescript_field_names do |
| 53 | + [is_active?: "isActiveFromCallback"] |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + defmodule NonTypescriptResource do |
| 58 | + use Ash.Resource, |
| 59 | + domain: nil, |
| 60 | + data_layer: Ash.DataLayer.Ets |
| 61 | + |
| 62 | + attributes do |
| 63 | + uuid_primary_key :id |
| 64 | + attribute :name, :string, public?: true |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + defmodule LinkedResource do |
| 69 | + use Ash.Resource, |
| 70 | + domain: nil, |
| 71 | + data_layer: Ash.DataLayer.Ets |
| 72 | + |
| 73 | + attributes do |
| 74 | + uuid_primary_key :id |
| 75 | + attribute :resource_with_all_kinds_id, :uuid, public?: true |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + defmodule ResourceWithAllKinds do |
| 80 | + use Ash.Resource, |
| 81 | + domain: nil, |
| 82 | + data_layer: Ash.DataLayer.Ets, |
| 83 | + extensions: [AshTypescript.Resource] |
| 84 | + |
| 85 | + typescript do |
| 86 | + type_name "ResourceWithAllKinds" |
| 87 | + end |
| 88 | + |
| 89 | + attributes do |
| 90 | + uuid_primary_key :id |
| 91 | + attribute :first_name, :string, public?: true |
| 92 | + end |
| 93 | + |
| 94 | + relationships do |
| 95 | + has_many :linked_resources, LinkedResource, public?: true |
| 96 | + end |
| 97 | + |
| 98 | + calculations do |
| 99 | + calculate :display_name, :string, expr(first_name) do |
| 100 | + public? true |
| 101 | + end |
| 102 | + end |
| 103 | + |
| 104 | + aggregates do |
| 105 | + count :linked_count, :linked_resources do |
| 106 | + public? true |
| 107 | + end |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + describe "Resource.Info.get_formatted_field/3" do |
| 112 | + test "returns formatted name for unmapped public attribute" do |
| 113 | + assert Info.get_formatted_field(PlainResource, :first_name, :camel_case) == "firstName" |
| 114 | + assert Info.get_formatted_field(PlainResource, :first_name, :snake_case) == "first_name" |
| 115 | + assert Info.get_formatted_field(PlainResource, :first_name, :pascal_case) == "FirstName" |
| 116 | + end |
| 117 | + |
| 118 | + test "returns the field_names override regardless of formatter" do |
| 119 | + # Override wins for ALL builtin formatters — the DSL value is the literal |
| 120 | + # client name with no further formatting applied. |
| 121 | + assert Info.get_formatted_field(PlainResource, :address_line_1, :camel_case) == |
| 122 | + "addressLine1" |
| 123 | + |
| 124 | + assert Info.get_formatted_field(PlainResource, :address_line_1, :snake_case) == |
| 125 | + "addressLine1" |
| 126 | + |
| 127 | + assert Info.get_formatted_field(PlainResource, :address_line_1, :pascal_case) == |
| 128 | + "addressLine1" |
| 129 | + end |
| 130 | + |
| 131 | + test "applies formatter to fields without override" do |
| 132 | + assert Info.get_formatted_field(PlainResource, :is_super_admin, :camel_case) == |
| 133 | + "isSuperAdmin" |
| 134 | + |
| 135 | + assert Info.get_formatted_field(PlainResource, :is_super_admin, :pascal_case) == |
| 136 | + "IsSuperAdmin" |
| 137 | + end |
| 138 | + |
| 139 | + test "returns nil for missing field" do |
| 140 | + assert Info.get_formatted_field(PlainResource, :nonexistent_field, :camel_case) == nil |
| 141 | + end |
| 142 | + |
| 143 | + test "returns nil for non-AshTypescript resource" do |
| 144 | + assert Info.get_formatted_field(NonTypescriptResource, :name, :camel_case) == nil |
| 145 | + end |
| 146 | + |
| 147 | + test "returns nil for non-builtin formatter" do |
| 148 | + # Only camel/snake/pascal are pre-computed at compile time. MFA tuples |
| 149 | + # and any other formatter shape fall through to the runtime path. |
| 150 | + assert Info.get_formatted_field(PlainResource, :first_name, {SomeMod, :format}) == nil |
| 151 | + end |
| 152 | + |
| 153 | + test "collects public attributes" do |
| 154 | + assert Info.get_formatted_field(ResourceWithAllKinds, :first_name, :camel_case) == |
| 155 | + "firstName" |
| 156 | + end |
| 157 | + |
| 158 | + test "collects public relationships" do |
| 159 | + assert Info.get_formatted_field(ResourceWithAllKinds, :linked_resources, :camel_case) == |
| 160 | + "linkedResources" |
| 161 | + end |
| 162 | + |
| 163 | + test "collects public calculations" do |
| 164 | + assert Info.get_formatted_field(ResourceWithAllKinds, :display_name, :camel_case) == |
| 165 | + "displayName" |
| 166 | + end |
| 167 | + |
| 168 | + test "collects public aggregates" do |
| 169 | + assert Info.get_formatted_field(ResourceWithAllKinds, :linked_count, :camel_case) == |
| 170 | + "linkedCount" |
| 171 | + end |
| 172 | + end |
| 173 | + |
| 174 | + describe "format_field_for_client/3 precedence" do |
| 175 | + test "uses persisted state for resource without callback (fast path)" do |
| 176 | + assert FieldFormatter.format_field_for_client(:first_name, PlainResource, :camel_case) == |
| 177 | + "firstName" |
| 178 | + |
| 179 | + assert FieldFormatter.format_field_for_client(:address_line_1, PlainResource, :camel_case) == |
| 180 | + "addressLine1" |
| 181 | + end |
| 182 | + |
| 183 | + test "callback takes priority over persisted DSL state" do |
| 184 | + # ResourceWithCallback has BOTH field_names DSL AND a typescript_field_names/0 |
| 185 | + # callback. The callback must win. |
| 186 | + assert FieldFormatter.format_field_for_client( |
| 187 | + :is_active?, |
| 188 | + ResourceWithCallback, |
| 189 | + :camel_case |
| 190 | + ) == "isActiveFromCallback" |
| 191 | + end |
| 192 | + |
| 193 | + test "callback path falls through to formatter for fields not in callback map" do |
| 194 | + # `:first_name` is NOT in the callback's typescript_field_names list, so |
| 195 | + # the callback path falls through to format_field_name/2 — NOT to the DSL. |
| 196 | + # This matches the documented behavior in compute_field_for_client/3. |
| 197 | + assert FieldFormatter.format_field_for_client( |
| 198 | + :first_name, |
| 199 | + ResourceWithCallback, |
| 200 | + :camel_case |
| 201 | + ) == "firstName" |
| 202 | + end |
| 203 | + |
| 204 | + test "non-typescript resource falls through to plain formatter" do |
| 205 | + assert FieldFormatter.format_field_for_client(:name, NonTypescriptResource, :camel_case) == |
| 206 | + "name" |
| 207 | + end |
| 208 | + end |
| 209 | +end |
0 commit comments