Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/commands/iteration/generate_snippet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ class Iteration::GenerateSnippet
initialize_with :iteration, retries_count: 0

MAX_RETRIES = 3
# The extractor Lambda times out after 3 seconds, which very large files can
# hit. The snippet only uses the first lines, so we send just the start.
MAX_SOURCE_CODE_CHARACTERS = 20_000
SnippetGeneratorTemporarilyUnavailable = Class.new(StandardError)

def call
Expand All @@ -23,7 +26,7 @@ def call
Exercism.config.snippet_generator_url,
{
language: iteration.track.slug,
source_code: file.content
source_code: file.content[0, MAX_SOURCE_CODE_CHARACTERS]
}.to_json,
{ content_type: :json, accept: :json }
).body
Expand Down
16 changes: 16 additions & 0 deletions test/commands/iteration/generate_snippet_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,20 @@ class Iteration::GenerateSnippetTest < ActiveJob::TestCase

assert_equal "lib snippet", iteration.reload.snippet
end

test "sends only the first 20,000 characters of a long file" do
long_code = "é" * 25_000
submission = create :submission
create :submission_file, submission: submission, content: long_code
iteration = create :iteration, submission: submission

stub = stub_request(:post, Exercism.config.snippet_generator_url).
with { |request| JSON.parse(request.body)["source_code"] == "é" * 20_000 }.
to_return(status: 200, body: "truncated snippet", headers: {})

Iteration::GenerateSnippet.(iteration)

assert_requested stub
assert_equal "truncated snippet", iteration.reload.snippet
end
end
Loading