diff --git a/spec/api/oembed_spec.rb b/spec/api/oembed_spec.rb index fcf52e7..1585b94 100644 --- a/spec/api/oembed_spec.rb +++ b/spec/api/oembed_spec.rb @@ -1,11 +1,11 @@ require 'rails_helper' -describe "oEmbed provider" do +describe "oEmbed provider", needs_phantomjs_2_bin: true do let(:asciicast) { create(:asciicast) } it "responds with status 200 for JSON" do - get "/oembed?url=http://localhost:3000/a/#{asciicast.id}&format=json" + get "/oembed?url=http://localhost:3000/a/#{asciicast.id}&format=json&maxwidth=500&maxheight=300" expect(response.status).to eq(200) end diff --git a/spec/features/asciicast_spec.rb b/spec/features/asciicast_spec.rb index 2ac487d..a6d02b4 100644 --- a/spec/features/asciicast_spec.rb +++ b/spec/features/asciicast_spec.rb @@ -15,29 +15,4 @@ feature "Asciicast page", :js => true do expect(page).to have_selector('.cinema .play-button') end - def rgb(color) - [ChunkyPNG::Color.r(color), ChunkyPNG::Color.g(color), ChunkyPNG::Color.b(color)] - end - - scenario 'Requesting PNG', needs_phantomjs_2_bin: true do - visit asciicast_path(asciicast, format: :png) - - expect(current_path).to match(%r{/uploads/test/asciicast/image/\d+/\w+\.png$}) - - png = ChunkyPNG::Image.from_file("#{Rails.root}/public/#{current_path}") - - # make sure there are black-ish borders - expect(rgb(png[1, 1])).to eq([18, 19, 20]) - expect(rgb(png[png.width - 2, png.height - 2])).to eq([18, 19, 20]) - - # check content color (blue background) - expect(rgb(png[15, 15])).to eq([0, 175, 255]) - - # make sure white SVG play icon is rendered correctly - expect(rgb(png[png.width / 2, (png.height / 2) - 10])).to eq([255, 255, 255]) - - # make sure PowerlineSymbols are rendered - expect(rgb(png[144, 795])).to eq([0, 95, 255]) - end - end diff --git a/spec/features/png_spec.rb b/spec/features/png_spec.rb new file mode 100644 index 0000000..4541305 --- /dev/null +++ b/spec/features/png_spec.rb @@ -0,0 +1,13 @@ +require 'rails_helper' + +feature "asciicast-as-png", needs_phantomjs_2_bin: true do + + let(:asciicast) { create(:asciicast) } + + scenario "Requesting PNG" do + visit asciicast_path(asciicast, format: :png) + + expect(current_path).to match(%r{/uploads/test/asciicast/image/\d+/\w+\.png$}) + end + +end diff --git a/spec/services/asciicast_image_generator_spec.rb b/spec/services/asciicast_image_generator_spec.rb new file mode 100644 index 0000000..e0cda84 --- /dev/null +++ b/spec/services/asciicast_image_generator_spec.rb @@ -0,0 +1,42 @@ +require 'rails_helper' + +describe AsciicastImageGenerator, needs_phantomjs_2_bin: true do + + let(:image_generator) { AsciicastImageGenerator.new(template_renderer) } + let(:template_renderer) { ApplicationController.new } + + describe '#generate' do + let(:asciicast) { create(:asciicast) } + + def rgb(color) + [ChunkyPNG::Color.r(color), ChunkyPNG::Color.g(color), ChunkyPNG::Color.b(color)] + end + + before do + image_generator.generate(asciicast) + end + + it 'generates screenshot of "snapshot frame"' do + png = ChunkyPNG::Image.from_file("#{Rails.root}/public/#{asciicast.image_url}") + + # make sure there are black-ish borders + expect(rgb(png[1, 1])).to eq([18, 19, 20]) + expect(rgb(png[png.width - 2, png.height - 2])).to eq([18, 19, 20]) + + # check content color (blue background) + expect(rgb(png[15, 15])).to eq([0, 175, 255]) + + # make sure white SVG play icon is rendered correctly + expect(rgb(png[png.width / 2, (png.height / 2) - 10])).to eq([255, 255, 255]) + + # make sure PowerlineSymbols are rendered + expect(rgb(png[144, 795])).to eq([0, 95, 255]) + end + + it 'sets image_width and image_height on the asciicast' do + expect(asciicast.image_width).to_not be(nil) + expect(asciicast.image_height).to_not be(nil) + end + end + +end