Skip to content

Commit 055006f

Browse files
committed
Add AI configuration json view
1 parent e02a286 commit 055006f

File tree

4 files changed

+75
-43
lines changed

4 files changed

+75
-43
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
defmodule TrentoWeb.V1.AIConfigurationJSON do
2+
def ai_configuration(%{ai_configuration: ai_configuration}),
3+
do: ai_configuration_entry(ai_configuration)
4+
5+
def ai_configuration_entry(%{
6+
provider: provider,
7+
model: model
8+
}),
9+
do: %{
10+
provider: provider,
11+
model: model
12+
}
13+
14+
def ai_configuration_entry(_), do: nil
15+
end

lib/trento_web/controllers/v1/profile_json.ex

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
defmodule TrentoWeb.V1.ProfileJSON do
2-
alias TrentoWeb.V1.{AbilityJSON, PersonalAccessTokensJSON}
2+
alias TrentoWeb.V1.{
3+
AbilityJSON,
4+
AIConfigurationJSON,
5+
PersonalAccessTokensJSON
6+
}
37

48
def profile(%{
59
user: %{
@@ -32,7 +36,7 @@ defmodule TrentoWeb.V1.ProfileJSON do
3236
created_at: created_at,
3337
analytics_enabled: analytics_enabled_at != nil,
3438
analytics_eula_accepted: analytics_eula_accepted_at != nil,
35-
ai_configuration: ai_configuration(ai_configuration),
39+
ai_configuration: AIConfigurationJSON.ai_configuration_entry(ai_configuration),
3640
idp_user: length(user_identities) > 0,
3741
updated_at: updated_at
3842
}
@@ -49,15 +53,4 @@ defmodule TrentoWeb.V1.ProfileJSON do
4953
}
5054
}),
5155
do: %{secret: Base.encode32(secret, padding: false), secret_qr_encoded: secret_qr_encoded}
52-
53-
defp ai_configuration(%{
54-
provider: provider,
55-
model: model
56-
}),
57-
do: %{
58-
provider: provider,
59-
model: model
60-
}
61-
62-
defp ai_configuration(_), do: nil
6356
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
defmodule TrentoWeb.V1.AIConfigurationJSONTest do
2+
use ExUnit.Case
3+
4+
import Trento.Factory
5+
6+
alias TrentoWeb.V1.AIConfigurationJSON
7+
8+
describe "rendering AI Configuration" do
9+
test "AI configuration" do
10+
%{
11+
provider: provider,
12+
model: model
13+
} = ai_user_configuration = build(:ai_user_configuration)
14+
15+
scenarios = [
16+
%{
17+
ai_configuration: ai_user_configuration,
18+
expected_ai_configuration: %{
19+
provider: provider,
20+
model: model
21+
}
22+
},
23+
%{
24+
ai_configuration: nil,
25+
expected_ai_configuration: nil
26+
},
27+
%{
28+
ai_configuration: "foo",
29+
expected_ai_configuration: nil
30+
}
31+
]
32+
33+
for %{
34+
ai_configuration: ai_configuration,
35+
expected_ai_configuration: expected_ai_configuration
36+
} <- scenarios do
37+
assert AIConfigurationJSON.ai_configuration(%{ai_configuration: ai_configuration}) ==
38+
expected_ai_configuration
39+
end
40+
end
41+
end
42+
end

test/trento_web/views/v1/profile_view_json_test.exs

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -73,39 +73,21 @@ defmodule TrentoWeb.V1.ProfileJSONTest do
7373
end
7474

7575
test "should render a user profile with AI configuration" do
76-
%{
77-
provider: provider,
78-
model: model
79-
} = ai_user_configuration = build(:ai_user_configuration)
80-
8176
scenarios = [
82-
%{
83-
user: build(:user, abilities: [], user_identities: [], ai_configuration: nil),
84-
expected_ai_configuration: nil
85-
},
86-
%{
87-
# not providing ai_configuration results in an Ecto.Association.NotLoaded
88-
user: build(:user, abilities: [], user_identities: []),
89-
expected_ai_configuration: nil
90-
},
91-
%{
92-
user:
93-
build(:user,
94-
abilities: [],
95-
user_identities: [],
96-
ai_configuration: ai_user_configuration
97-
),
98-
expected_ai_configuration: %{
99-
provider: provider,
100-
model: model
101-
}
102-
}
77+
# not providing ai_configuration results in an Ecto.Association.NotLoaded
78+
build(:user, abilities: [], user_identities: []),
79+
build(:user, abilities: [], user_identities: [], ai_configuration: nil),
80+
build(:user,
81+
abilities: [],
82+
user_identities: [],
83+
ai_configuration: build(:ai_user_configuration)
84+
)
10385
]
10486

105-
for %{user: user, expected_ai_configuration: expected_ai_configuration} <- scenarios do
106-
rendered_user = ProfileJSON.profile(%{user: user})
107-
108-
assert rendered_user.ai_configuration == expected_ai_configuration
87+
for user <- scenarios do
88+
assert %{user: user}
89+
|> ProfileJSON.profile()
90+
|> Map.has_key?(:ai_configuration)
10991
end
11092
end
11193
end

0 commit comments

Comments
 (0)