gemini-suggestions, tests-fail

This commit is contained in:
Ole-Morten Duesund 2025-06-06 21:12:46 +02:00
commit 314bffc4f8
8 changed files with 429 additions and 15 deletions

96
test/app_test.rb Normal file
View file

@ -0,0 +1,96 @@
# frozen_string_literal: true
# test/app_test.rb
require_relative 'test_helper' # Load common setup
class AppTest < Minitest::Test
include Rack::Test::Methods # Include Rack::Test methods
# The `app` method is defined in test_helper.rb, required by Rack::Test
def test_root_path_returns_default_english_no
get '/'
# Debugging: puts last_response.status, last_response.body
assert last_response.ok?, "Expected status 200, got #{last_response.status}"
assert_includes last_response.body, 'No', "Expected body to include 'No', got:\n#{last_response.body}"
assert_includes last_response.content_type, 'text/html',
"Expected HTML content type, got #{last_response.content_type}"
end
def test_language_path_returns_specific_language_no
# Test Norwegian
get '/no'
assert last_response.ok?, "Expected status 200 for /no, got #{last_response.status}"
assert_includes last_response.body, 'Nei', "Expected body to include 'Nei' for /no, got:\n#{last_response.body}"
assert_includes last_response.content_type, 'text/html',
"Expected HTML content type for /no, got #{last_response.content_type}"
# Test Swedish
get '/sv'
assert last_response.ok?, "Expected status 200 for /sv, got #{last_response.status}"
assert_includes last_response.body, 'Nej', "Expected body to include 'Nej' for /sv, got:\n#{last_response.body}"
assert_includes last_response.content_type, 'text/html',
"Expected HTML content type for /sv, got #{last_response.content_type}"
end
def test_unsupported_language_returns_not_found
get '/xyz' # Non-existent language
assert_equal 404, last_response.status, "Expected status 404 for unsupported language, got #{last_response.status}"
# Asserting a general 'Not Found' message. Adjust if your 404 page has a specific string.
assert_includes last_response.body, 'Not Found', "Expected body to include 'Not Found', got:\n#{last_response.body}"
end
def test_lists_all_supported_languages_in_json
get '/languages'
assert last_response.ok?, "Expected status 200 for /languages, got #{last_response.status}"
assert_includes last_response.content_type, 'application/json',
"Expected JSON content type for /languages, got #{last_response.content_type}"
# Ensure JSON parsing is successful
json_response = JSON.parse(last_response.body)
assert_kind_of Array, json_response, "Expected JSON response to be an Array, got #{json_response.class}"
# Check for a subset of expected languages. Add all languages from your responses.yml.
expected_languages = %w[en no sv fi is da fo].sort # Sort for consistent comparison if order isn't guaranteed
actual_languages = json_response.sort
assert_equal expected_languages.size, actual_languages.size,
"Expected #{expected_languages.size} languages, got #{actual_languages.size}"
assert_equal expected_languages, actual_languages, 'Language list mismatch.'
end
def test_returns_json_when_accept_header_is_json
header 'Accept', 'application/json' # Set the Accept header
get '/'
assert last_response.ok?, "Expected status 200 with Accept: JSON, got #{last_response.status}"
assert_includes last_response.content_type, 'application/json',
"Expected JSON content type with Accept: JSON, got #{last_response.content_type}"
json_response = JSON.parse(last_response.body)
assert_kind_of Hash, json_response, "Expected JSON response to be a Hash, got #{json_response.class}"
assert json_response['response'].is_a?(String),
"Expected 'response' key with string value, got #{json_response['response'].class}"
assert json_response['response'].length.positive?, "Expected 'response' value to be non-empty"
end
def test_returns_json_when_format_param_is_json
get '/?format=json' # Use the format parameter
assert last_response.ok?, "Expected status 200 with ?format=json, got #{last_response.status}"
assert_includes last_response.content_type, 'application/json',
"Expected JSON content type with ?format=json, got #{last_response.content_type}"
json_response = JSON.parse(last_response.body)
assert_kind_of Hash, json_response, "Expected JSON response to be a Hash, got #{json_response.class}"
assert json_response['response'].is_a?(String),
"Expected 'response' key with string value, got #{json_response['response'].class}"
assert json_response['response'].length.positive?, "Expected 'response' value to be non-empty"
end
def test_health_check_returns_ok
get '/health'
assert last_response.ok?, "Expected status 200 for /health, got #{last_response.status}"
assert_equal 'OK', last_response.body, "Expected body 'OK' for /health, got '#{last_response.body}'"
assert_includes last_response.content_type, 'text/plain',
"Expected text/plain content type for /health, got #{last_response.content_type}"
end
end

30
test/test_helper.rb Normal file
View file

@ -0,0 +1,30 @@
# frozen_string_literal: true
# test/test_helper.rb
ENV['RACK_ENV'] = 'test' # Set environment to test, important for Sinatra config
require 'rack/test'
require 'minitest/autorun' # For Minitest
require 'json' # Required if you're testing JSON responses
# --- CRITICAL APP LOADING ---
# Adjust this path if your app.rb is NOT directly in the parent directory of 'test/'
# Example: If app.rb is in `src/app.rb`, use `require_relative '../src/app.rb'`
require_relative '../app'
# Configure Rack::Test
module Rack::Test::Methods
def app
# This is *THE* most critical part for Sinatra tests.
# It must return the instance of your Sinatra application.
# Option 1: For classic style Sinatra apps (most common for small apps)
# where you just have `get '/' do ... end` directly in app.rb
Sinatra::Application
# Option 2: For modular Sinatra apps (if your app.rb defines a class)
# class MyApp < Sinatra::Base; ... end
# If this is your case, uncomment the line below and replace 'MyApp'
# MyApp.new
end
end