@@ -4,12 +4,13 @@ defmodule CadetWeb.AICodeAnalysisControllerTest do
44 use CadetWeb.ConnCase
55 alias Cadet . { Repo , AIComments }
66 alias Cadet . { AIComments.AIComment , Courses.Course }
7+ alias CadetWeb.AICommentsHelpers
78
89 setup do
910 course_with_llm =
1011 insert ( :course , % {
1112 enable_llm_grading: true ,
12- llm_api_key: Course . encrypt_llm_api_key ( "test_key" ) ,
13+ llm_api_key: AICommentsHelpers . encrypt_llm_api_key ( "test_key" ) ,
1314 llm_model: "gpt-5-mini" ,
1415 llm_api_url: "http://testapi.com" ,
1516 llm_course_level_prompt: "Example Prompt"
@@ -34,7 +35,7 @@ defmodule CadetWeb.AICodeAnalysisControllerTest do
3435 } }
3536 end
3637
37- describe "GET /v2/courses/:course_id/admin/generate-comments/:submissionid/:questionid " do
38+ describe "GET /v2/courses/:course_id/admin/generate-comments/:answer_id " do
3839 test "success with happy path, admin and staff" , % {
3940 conn: conn ,
4041 admin_user: admin_user ,
@@ -46,42 +47,24 @@ defmodule CadetWeb.AICodeAnalysisControllerTest do
4647 answer: answer
4748 } do
4849 # Make the API call
49- with_mock HTTPoison , [ :passthrough ] ,
50- post: fn _url , _body , _headers , _opts ->
51- { :ok ,
52- % HTTPoison.Response {
53- status_code: 200 ,
54- body:
55- Jason . encode! ( % {
56- "choices" => [ % { "message" => % { "content" => "Comment1|||Comment2" } } ]
57- } )
58- } }
50+ with_mock OpenAI ,
51+ chat_completion: fn _input , _overrides ->
52+ { :ok , % { :choices => [ % { "message" => % { "content" => "Comment1|||Comment2" } } ] } }
5953 end do
6054 conn
6155 |> sign_in ( staff_user . user )
62- |> post (
63- build_url_generate_ai_comments ( course_with_llm . id , new_submission . id , question . id )
64- )
56+ |> post ( build_url_generate_ai_comments ( course_with_llm . id , answer . id ) )
6557 |> json_response ( 200 )
6658 end
6759
68- with_mock HTTPoison , [ :passthrough ] ,
69- post: fn _url , _body , _headers , _opts ->
70- { :ok ,
71- % HTTPoison.Response {
72- status_code: 200 ,
73- body:
74- Jason . encode! ( % {
75- "choices" => [ % { "message" => % { "content" => "Comment1|||Comment2" } } ]
76- } )
77- } }
60+ with_mock OpenAI ,
61+ chat_completion: fn _input , _overrides ->
62+ { :ok , % { :choices => [ % { "message" => % { "content" => "Comment1|||Comment2" } } ] } }
7863 end do
7964 response =
8065 conn
8166 |> sign_in ( admin_user . user )
82- |> post (
83- build_url_generate_ai_comments ( course_with_llm . id , new_submission . id , question . id )
84- )
67+ |> post ( build_url_generate_ai_comments ( course_with_llm . id , answer . id ) )
8568 |> json_response ( 200 )
8669
8770 # Verify response
@@ -92,13 +75,12 @@ defmodule CadetWeb.AICodeAnalysisControllerTest do
9275 comments = Repo . all ( AIComment )
9376 assert length ( comments ) > 0
9477 latest_comment = List . first ( comments )
95- assert latest_comment . submission_id == new_submission . id
96- assert latest_comment . question_id == question . id
78+ assert latest_comment . answer_id == answer . id
9779 assert latest_comment . raw_prompt != nil
9880 assert latest_comment . answers_json != nil
9981 end
10082
101- test "errors out when given an invalid submission " , % {
83+ test "errors out when given an invalid answer id " , % {
10284 conn: conn ,
10385 admin_user: admin_user ,
10486 staff_user: staff_user ,
@@ -108,26 +90,17 @@ defmodule CadetWeb.AICodeAnalysisControllerTest do
10890 question: question ,
10991 answer: answer
11092 } do
111- random_submission_id = 123
93+ random_answer_id = 324_324
11294
11395 # Make the API call that should fail
114- with_mock HTTPoison , [ :passthrough ] ,
115- post: fn _url , _body , _headers , _opts ->
116- { :ok ,
117- % HTTPoison.Response {
118- status_code: 200 ,
119- body:
120- Jason . encode! ( % {
121- "choices" => [ % { "message" => % { "content" => "Comment1|||Comment2" } } ]
122- } )
123- } }
96+ with_mock OpenAI , [ :passthrough ] ,
97+ chat_completion: fn _input , _overrides ->
98+ { :ok , % { :choices => [ % { "message" => % { "content" => "Comment1|||Comment2" } } ] } }
12499 end do
125100 response =
126101 conn
127102 |> sign_in ( admin_user . user )
128- |> post (
129- build_url_generate_ai_comments ( course_with_llm . id , random_submission_id , question . id )
130- )
103+ |> post ( build_url_generate_ai_comments ( course_with_llm . id , random_answer_id ) )
131104 |> text_response ( 400 )
132105 end
133106 end
@@ -143,31 +116,28 @@ defmodule CadetWeb.AICodeAnalysisControllerTest do
143116 answer: answer
144117 } do
145118 # Make the API call that should fail
146- with_mock HTTPoison , [ :passthrough ] ,
147- post : fn _url , _body , _headers , _opts ->
148- { :ok , % HTTPoison.Response { status_code: 200 , body: "invalid response"} }
119+ with_mock OpenAI , [ :passthrough ] ,
120+ chat_completion : fn _input , _overrides ->
121+ { :ok , % { " body" => "Some unexpected response"} }
149122 end do
150123 response =
151124 conn
152125 |> sign_in ( admin_user . user )
153- |> post (
154- build_url_generate_ai_comments ( course_with_llm . id , new_submission . id , question . id )
155- )
156- |> text_response ( 500 )
126+ |> post ( build_url_generate_ai_comments ( course_with_llm . id , answer . id ) )
127+ |> text_response ( 502 )
157128 end
158129
159130 # Verify database entry even with error
160131 comments = Repo . all ( AIComment )
161132 assert length ( comments ) > 0
162133 latest_comment = List . first ( comments )
163- assert latest_comment . submission_id == new_submission . id
164- assert latest_comment . question_id == question . id
134+ assert latest_comment . answer_id == answer . id
165135 assert latest_comment . raw_prompt != nil
166136 assert latest_comment . answers_json != nil
167137 end
168138 end
169139
170- defp build_url_generate_ai_comments ( course_id , submission_id , question_id ) do
171- "/v2/courses/#{ course_id } /admin/generate-comments/#{ submission_id } / #{ question_id } "
140+ defp build_url_generate_ai_comments ( course_id , answer_id ) do
141+ "/v2/courses/#{ course_id } /admin/generate-comments/#{ answer_id } "
172142 end
173143end
0 commit comments