<commit_before>require "rails_helper"
require "govuk_schemas"

RSpec.describe "Message queue publishing" do
  it "puts the correct message on the queue" do
    base_path = "/#{SecureRandom.hex}"

    stub_content_store_calls(base_path)

    content_item = generate_random_content_item(base_path)

    put "/v2/content/#{content_id}", params: content_item.to_json

    expect(response).to be_ok

    post "/v2/content/#{content_id}/publish", params: { update_type: "major", locale: content_item["locale"] }.to_json

    expect(response).to be_ok

    ensure_message_queue_payload_validates_against_notification_schema
  end

  def stub_content_store_calls(base_path)
    allow(PublishingAPI.service(:queue_publisher)).to receive(:send_message)
    stub_request(:put, "http://draft-content-store.dev.gov.uk/content#{base_path}")
      .to_return(status: 200)
    stub_request(:put, "http://content-store.dev.gov.uk/content#{base_path}")
      .to_return(status: 200)
  end

  def generate_random_content_item(base_path)
    GovukSchemas::RandomExample.for_schema(publisher_schema: "placeholder").merge_and_validate(
      base_path: base_path,
      rendering_app: "something", # schema do not enforce a "dns-hostname" pattern yet
      publishing_app: "something", # schema do not enforce a "dns-hostname" pattern yet
      redirects: [], # is not validated in schemas yet
      routes: [
        { path: base_path, type: "prefix" } # hard to do in schemas
      ]
    )
  end

  def ensure_message_queue_payload_validates_against_notification_schema
    expect(PublishingAPI.service(:queue_publisher)).to have_received(:send_message) do |payload|
      errors = JSON::Validator.fully_validate(
        GovukSchemas::Schema.find(notification_schema: "placeholder"),
        payload,
        errors_as_objects: true,
      )
      expect(errors).to eql([])
    end
  end
end
<commit_msg>Split intermittent test into fail/pass paths<commit_after>require "rails_helper"
require "govuk_schemas"

RSpec.describe "Message queue publishing" do
  shared_examples "puts message on queue" do
    it "puts the correct message on the queue" do
      base_path = "/#{SecureRandom.hex}"

      stub_content_store_calls(base_path)

      content_item = generate_random_content_item(base_path, change_note)

      put "/v2/content/#{content_id}", params: content_item.to_json

      expect(response).to be_ok

      post "/v2/content/#{content_id}/publish", params: { locale: content_item["locale"] }.to_json

      expect(response).to be_ok

      ensure_message_queue_payload_validates_against_notification_schema
    end
  end

  context "when there is a change note" do
    let(:change_note) { true }
    include_examples "puts message on queue"
  end

  context "when there is not a change note" do
    let(:change_note) { false }
    include_examples "puts message on queue"
  end

  def stub_content_store_calls(base_path)
    allow(PublishingAPI.service(:queue_publisher)).to receive(:send_message)
    stub_request(:put, "http://draft-content-store.dev.gov.uk/content#{base_path}")
      .to_return(status: 200)
    stub_request(:put, "http://content-store.dev.gov.uk/content#{base_path}")
      .to_return(status: 200)
  end

  def generate_random_content_item(base_path, change_note)
    random = GovukSchemas::RandomExample.for_schema(publisher_schema: "placeholder")

    if change_note
      details = random.payload["details"].merge("change_note" => Faker::Lorem.sentence)
    else
      details = random.payload["details"].except("change_note")
    end

    random.merge_and_validate(
      base_path: base_path,
      details: details,
      rendering_app: "something", # schema do not enforce a "dns-hostname" pattern yet
      publishing_app: "something", # schema do not enforce a "dns-hostname" pattern yet
      redirects: [], # is not validated in schemas yet
      routes: [
        { path: base_path, type: "prefix" } # hard to do in schemas
      ],
      update_type: "major",
    )
  end

  def ensure_message_queue_payload_validates_against_notification_schema
    expect(PublishingAPI.service(:queue_publisher)).to have_received(:send_message) do |payload|
      errors = JSON::Validator.fully_validate(
        GovukSchemas::Schema.find(notification_schema: "placeholder"),
        payload,
        errors_as_objects: true,
      )
      expect(errors).to eql([])
    end
  end
end

{'fraction_non_alphanumeric': 0.10509554140127389, 'fraction_numerical': 0.003639672429481347, 'mean_word_length': 3.9796149490373725, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '819595', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1364, 'n_words': 292}