<commit_before>require_relative "test_helper"
require_relative "../app/site"

describe Tint::Site do
	let(:subject) { Tint::Site.new(options) }
	let(:default_options) do
		{
			site_id: site_id,
			cache_path: Pathname.new(__FILE__).dirname.join(cache_path),
			fn: "my test site",
			user_id: "3"
		}
	end
	let(:cache_path) { "data" }
	let(:options) { default_options }
	let(:site_id) { 1 }

	describe "#==" do
		describe "when cache_path and type are the same" do
			it "should be considered equal" do
				assert_equal(Tint::Site.new(options), subject)
			end
		end

		describe "when cache_path is not the same" do
			let(:other_site) do
				Tint::Site.new(cache_path: Pathname.new("stuff"))
			end

			it "should not be considered equal" do
				assert_equal(false, other_site == subject)
			end
		end
	end

	describe "#to_h" do
		it "should return the options" do
			assert_equal(options, subject.to_h)
		end

		it "should be a clone of the original options" do
			refute_same(options, subject.to_h)
		end
	end

	describe "#route" do
		describe "without a path" do
			it "should return /:site_id/" do
				assert_equal("/#{site_id}/", subject.route)
			end
		end

		describe "with a path" do
			it "should return /:site_id/:path" do
				path = "path/to/something"
				assert_equal("/#{site_id}/#{path}", subject.route(path))
			end
		end
	end

	describe "#fn" do
		it "should return the value for fn from options" do
			assert_equal(options[:fn], subject.fn)
		end
	end

	describe "#user_id" do
		it "should return the integer value of options[:user_id]" do
			assert_equal(options[:user_id].to_i, subject.user_id)
		end
	end

	describe "#cache_path" do
		describe "when cache_path is passed in" do
			describe "when it does not exist on the file system" do
				let(:cache_path) { "data/nonexistent" }

				before do
					FileUtils.rmtree default_options[:cache_path]
				end

				it "should create the path" do
					refute(default_options[:cache_path].exist?)
					subject.cache_path
					assert(default_options[:cache_path].exist?)
				end

				it "should return the path" do
					assert_equal(default_options[:cache_path], subject.cache_path)
				end

				after do
					FileUtils.rmtree default_options[:cache_path]
				end
			end

			describe "when it does exist on the file system" do
				it "should return the path" do
					assert_equal(default_options[:cache_path], subject.cache_path)
				end
			end
		end

		describe "when no cache_path is passed in" do
			let(:options) do
				options = default_options
				options.delete(:cache_path)
				options
			end

			describe "when it exists in the environment" do
				before do
					ENV["CACHE_PATH"] = default_options[:cache_path].to_s
				end

				after do
					ENV.delete("CACHE_PATH")
				end

				describe "when it does not exist on the file system" do
					let(:cache_path) { "data/nonexistent" }
					let(:expected_path) { default_options[:cache_path].join(site_id.to_s) }

					before do
						FileUtils.rmtree expected_path
					end

					it "should create the path" do
						refute(expected_path.exist?)
						subject.cache_path
						assert(expected_path.exist?)
					end

					it "should return the path with the site_id" do
						assert_equal(
							expected_path,
							subject.cache_path
						)
					end

					after do
						FileUtils.rmtree expected_path
					end
				end

				describe "when it does exist on the file system" do
					it "should return the path with the site_id" do
						assert_equal(
							default_options[:cache_path].join(site_id.to_s),
							subject.cache_path
						)
					end
				end
			end

			describe "when it does not exist in the environment" do
				before { ENV.delete("CACHE_PATH") }

				it "should raise an exception" do
					assert_raises KeyError do
						subject.cache_path<commit_msg>Also test deploy path method<commit_after>require_relative "test_helper"
require_relative "../app/site"

describe Tint::Site do
	let(:subject) { Tint::Site.new(options) }
	let(:default_options) do
		{
			site_id: site_id,
			cache_path: Pathname.new(__FILE__).dirname.join(cache_path),
			deploy_path: Pathname.new(__FILE__).dirname.join(deploy_path),
			fn: "my test site",
			user_id: "3"
		}
	end
	let(:cache_path) { "data" }
	let(:deploy_path) { "deploy" }
	let(:options) { default_options }
	let(:site_id) { 1 }

	describe "#==" do
		describe "when cache_path and type are the same" do
			it "should be considered equal" do
				assert_equal(Tint::Site.new(options), subject)
			end
		end

		describe "when cache_path is not the same" do
			let(:other_site) do
				Tint::Site.new(cache_path: Pathname.new("stuff"))
			end

			it "should not be considered equal" do
				assert_equal(false, other_site == subject)
			end
		end
	end

	describe "#to_h" do
		it "should return the options" do
			assert_equal(options, subject.to_h)
		end

		it "should be a clone of the original options" do
			refute_same(options, subject.to_h)
		end
	end

	describe "#route" do
		describe "without a path" do
			it "should return /:site_id/" do
				assert_equal("/#{site_id}/", subject.route)
			end
		end

		describe "with a path" do
			it "should return /:site_id/:path" do
				path = "path/to/something"
				assert_equal("/#{site_id}/#{path}", subject.route(path))
			end
		end
	end

	describe "#fn" do
		it "should return the value for fn from options" do
			assert_equal(options[:fn], subject.fn)
		end
	end

	describe "#user_id" do
		it "should return the integer value of options[:user_id]" do
			assert_equal(options[:user_id].to_i, subject.user_id)
		end
	end

	[:cache_path, :deploy_path].each do |path_method|
		describe "##{path_method}" do
			describe "when #{path_method} is passed in" do
				describe "when it does not exist on the file system" do
					let(path_method) { "data/#{path_method}_nonexistent" }

					before do
						FileUtils.rmtree default_options[path_method]
					end

					it "should create the path" do
						refute(default_options[path_method].exist?)
						subject.public_send(path_method)
						assert(default_options[path_method].exist?)
					end

					it "should return the path" do
						assert_equal(default_options[path_method], subject.public_send(path_method))
					end

					after do
						FileUtils.rmtree default_options[path_method]
					end
				end

				describe "when it does exist on the file system" do
					it "should return the path" do
						assert_equal(default_options[path_method], subject.public_send(path_method))
					end
				end
			end

			describe "when no cache_path is passed in" do
				let(:options) do
					options = default_options
					options.delete(path_method)
					options
				end

				describe "when it exists in the environment" do
					before do
						ENV[path_method.to_s.upcase] = default_options[path_method].to_s
					end

					after do
						ENV.delete(path_method.to_s.upcase)
					end

					describe "when it does not exist on the file system" do
						let(:cache_path) { "data/#{path_method}_nonexistent" }
						let(:expected_path) { default_options[path_method].join(site_id.to_s) }

						before do
							FileUtils.rmtree expected_path
						end

						it "should create the path" do
							refute(expected_path.exist?)
							subject.public_send(path_method)
							assert(expected_path.exist?)
						end

						it "should return the path with the site_id" do
							assert_equal(expected_path, subject.public_send(path_method))
						end

						after do
							FileUtils.rmtree expected_path
						end
					end

					describe "when it does exist on the file system" do
						it "should return the path with the site_id" do
							assert_equal(
								default_options[path_method].join(site_id.to_s),
								subject.public_send(path_method)
							)
						end
					end
				end

				describe "when it does not exist in the environment" do
					before { ENV.delete(path_method.to_s.upcase) }

					it "should raise an exception" do
						assert_raises KeyError do
							subject.public_send(path_method)
						end
{'fraction_non_alphanumeric': 0.09259491291818069, 'fraction_numerical': 0.0005011903270266884, 'mean_word_length': 3.0954335556695742, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5677680', 'n_tokens_mistral': 3361, 'n_tokens_neox': 2556, 'n_words': 751}