# frozen_string_literal: true
require 'test_helper'

class AnswerFormatValidatorTest < ActiveSupport::TestCase
  test "live_code should PASS with populated hash" do
    obj = AnswerValidatable.new('live_code')
    obj.answer = { html: 'this is html', css: '', js: '', text: 'some reasons' }

    assert obj.valid?
    assert obj.errors.messages.empty?
  end

  test "live_code should PASS with finish later" do
    obj = AnswerValidatable.new('live_code')
    obj.answer = { later: "true" }

    assert obj.valid?
    assert obj.errors.messages.empty?
  end

  test "live_code should FAIL with nil" do
    obj = AnswerValidatable.new('live_code')
    obj.answer = nil

    refute obj.valid?
    assert_match(/write.*code/, obj.errors.messages[:answer][0])
  end

  test "live_code should FAIL without checking finish later" do
    obj = AnswerValidatable.new('live_code')
    obj.answer = { later: "" }

    refute obj.valid?
    assert_match(/come back/, obj.errors.messages[:answer][0])
  end

  test "live_code should FAIL without values" do
    obj = AnswerValidatable.new('live_code')
    obj.answer = { text: "", html: "", css: "", js: "" }

    refute obj.valid?
    assert_match(/write.*code/, obj.errors.messages[:answer][0])
  end

  test "live_code should PASS with text only" do
    obj = AnswerValidatable.new('live_code')
    obj.answer = { html: "", css: "", js: "", text: "reasons" }

    assert obj.valid?
    assert obj.errors.messages.empty?
  end

  test "live_code should PASS using seed data" do
    seeded_answer = questions(:fed7).input_options
    obj = AnswerValidatable.new('live_code', questions(:fed7).id)
    obj.answer = seeded_answer.merge(text: "no thanks", js: "")

    assert obj.valid?
    assert obj.errors.messages.empty?
  end

  test "live_code should FAIL with seed data only" do
    seeded_answer = questions(:fed7).input_options
    obj = AnswerValidatable.new('live_code', questions(:fed7).id)
    obj.answer = seeded_answer.merge(text: "", js: "")

    refute obj.valid?
    assert_match(/write.*code/, obj.errors.messages[:answer][0])
  end
end
