<commit_before># frozen_string_literal: true

require "rails_helper"
require "fileutils"

describe "form rendering for odk", :odk, :reset_factory_sequences do
  let(:user) { create(:user) }
  let(:form) { create(:form) }
  let(:fixture_filename) { "#{fixture_name}.xml" }

  # Set this to true temporarily to make the spec save the prepared XML files under `tmp/odk/forms`.
  # Then use e.g. `adb push tmp/odk/forms/my_form/. /sdcard/odk/forms` to push one onto a phone for testing.
  let(:save_fixtures) { false }

  before do
    login(user)
  end

  context "form with various question types" do
    let!(:form) do
      create(:form,
        :published,
        :with_version,
        name: "Sample",
        question_types: %w[text long_text integer decimal location select_one
                           multilevel_select_one select_multiple datetime date time
                           formstart formend barcode counter counter_with_inc])
    end

    before do
      # Include multiple conditions on one question.
      form.c[6].display_conditions.create!(left_qing: form.c[2], op: "gt", value: "5")
      form.c[6].display_conditions.create!(left_qing: form.c[5], op: "eq",
                                           option_node: form.c[5].option_set.c[0])
      form.c[6].update!(display_if: "all_met")
    end

    it "should render proper xml" do
      expect_xml(form, "various_question_types")
    end
  end

  context "form with incomplete responses allowed" do
    let(:form) do
      create(:form, :published, :with_version, name: "Allows Incomplete",
                                               question_types: %w[integer], allow_incomplete: true)
    end

    before do
      # Things don't get interesting unless you have at least one required question.
      form.c[0].update!(required: true)
    end

    it "should render proper xml" do
      expect_xml(form, "allows_incomplete")
    end
  end

  context "display conditions, skip rules, constraints" do
    let!(:form) do
      create(:form, :published, :with_version, name: "Conditional Logic",
                                               question_types: %w[text long_text integer decimal location
                                                                  select_one multilevel_select_one
                                                                  select_multiple datetime integer integer])
    end

    before do
      # Include multiple conditions on one question.
      form.c[6].display_conditions.create!(left_qing: form.c[2], op: "gt", value: "5")
      form.c[6].display_conditions.create!(left_qing: form.c[5],
                                           op: "eq",
                                           option_node: form.c[5].option_set.c[0])
      form.c[6].update!(display_if: "all_met")
      create(:skip_rule,
        source_item: form.c[2],
        destination: "item",
        dest_item_id: form.c[7].id,
        skip_if: "all_met",
        conditions_attributes: [{left_qing_id: form.c[2].id, op: "eq", value: 0}])

      # Add both old style constraints and new style so we can check for the right expression.
      form.c[9].question.update!(minimum: 10, maximum: 100)
      form.c[9].constraints.create!(
        accept_if: "any_met",
        conditions_attributes: [
          {left_qing_id: form.c[3].id, op: "eq", value: 10},
          {left_qing_id: form.c[9].id, op: "eq", right_side_type: "qing", right_qing_id: form.c[2].id}
        ]
      )
      form.c[9].constraints.create!(
        accept_if: "all_met",
        conditions_attributes: [
          {left_qing_id: form.c[9].id, op: "neq", value: 55}
        ]
      )

      form.c[10].constraints.create!(
        accept_if: "all_met",
        conditions_attributes: [
          {left_qing_id: form.c[10].id, op: "eq", value: 10}
        ],
        rejection_msg_translations: {"en": "Custom rejection message."}<commit_msg>Using symbol key for convention.<commit_after># frozen_string_literal: true

require "rails_helper"
require "fileutils"

describe "form rendering for odk", :odk, :reset_factory_sequences do
  let(:user) { create(:user) }
  let(:form) { create(:form) }
  let(:fixture_filename) { "#{fixture_name}.xml" }

  # Set this to true temporarily to make the spec save the prepared XML files under `tmp/odk/forms`.
  # Then use e.g. `adb push tmp/odk/forms/my_form/. /sdcard/odk/forms` to push one onto a phone for testing.
  let(:save_fixtures) { false }

  before do
    login(user)
  end

  context "form with various question types" do
    let!(:form) do
      create(:form,
        :published,
        :with_version,
        name: "Sample",
        question_types: %w[text long_text integer decimal location select_one
                           multilevel_select_one select_multiple datetime date time
                           formstart formend barcode counter counter_with_inc])
    end

    before do
      # Include multiple conditions on one question.
      form.c[6].display_conditions.create!(left_qing: form.c[2], op: "gt", value: "5")
      form.c[6].display_conditions.create!(left_qing: form.c[5], op: "eq",
                                           option_node: form.c[5].option_set.c[0])
      form.c[6].update!(display_if: "all_met")
    end

    it "should render proper xml" do
      expect_xml(form, "various_question_types")
    end
  end

  context "form with incomplete responses allowed" do
    let(:form) do
      create(:form, :published, :with_version, name: "Allows Incomplete",
                                               question_types: %w[integer], allow_incomplete: true)
    end

    before do
      # Things don't get interesting unless you have at least one required question.
      form.c[0].update!(required: true)
    end

    it "should render proper xml" do
      expect_xml(form, "allows_incomplete")
    end
  end

  context "display conditions, skip rules, constraints" do
    let!(:form) do
      create(:form, :published, :with_version, name: "Conditional Logic",
                                               question_types: %w[text long_text integer decimal location
                                                                  select_one multilevel_select_one
                                                                  select_multiple datetime integer integer])
    end

    before do
      # Include multiple conditions on one question.
      form.c[6].display_conditions.create!(left_qing: form.c[2], op: "gt", value: "5")
      form.c[6].display_conditions.create!(left_qing: form.c[5],
                                           op: "eq",
                                           option_node: form.c[5].option_set.c[0])
      form.c[6].update!(display_if: "all_met")
      create(:skip_rule,
        source_item: form.c[2],
        destination: "item",
        dest_item_id: form.c[7].id,
        skip_if: "all_met",
        conditions_attributes: [{left_qing_id: form.c[2].id, op: "eq", value: 0}])

      # Add both old style constraints and new style so we can check for the right expression.
      form.c[9].question.update!(minimum: 10, maximum: 100)
      form.c[9].constraints.create!(
        accept_if: "any_met",
        conditions_attributes: [
          {left_qing_id: form.c[3].id, op: "eq", value: 10},
          {left_qing_id: form.c[9].id, op: "eq", right_side_type: "qing", right_qing_id: form.c[2].id}
        ]
      )
      form.c[9].constraints.create!(
        accept_if: "all_met",
        conditions_attributes: [
          {left_qing_id: form.c[9].id, op: "neq", value: 55}
        ]
      )

      form.c[10].constraints.create!(
        accept_if: "all_met",
        conditions_attributes: [
          {left_qing_id: form.c[10].id, op: "eq", value: 10}
        ],
        rejection_msg_translations: {:en => "Custom rejection message."}
{'fraction_non_alphanumeric': 0.11345074017355794, 'fraction_numerical': 0.010974987238386932, 'mean_word_length': 2.1562625855819575, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2636819', 'n_tokens_mistral': 2370, 'n_tokens_neox': 2289, 'n_words': 601}