diff --git a/spec/committed_cache_spec.rb b/spec/committed_cache_spec.rb new file mode 100644 index 0000000..a28b0a5 --- /dev/null +++ b/spec/committed_cache_spec.rb @@ -0,0 +1,129 @@ +# frozen_string_literal: true + +require "spec_helper" +require "json" + +RSpec.describe "Committed Cache Directory" do + # Use a non-git-ignored directory for cache storage + let(:committed_cache_path) { File.join(__dir__, "fixtures", "committed_cache") } + # Configure store with raw mode to avoid compression + let(:store) { ActiveSupport::Cache::SourceControlCacheStore.new(cache_path: committed_cache_path, compress: false) } + + # Predefined cache entries (keys and values as JSON strings for readability) + let(:cache_entries) do + { + "user:123:profile" => { name: "John Doe", email: "john@example.com" }.to_json, + "user:456:profile" => { name: "Jane Smith", email: "jane@example.com" }.to_json, + "config:app:settings" => { theme: "dark", language: "en" }.to_json + } + end + + # File list helpers + let(:key_files) { Dir.glob(File.join(committed_cache_path, "*.key")) } + let(:value_files) { Dir.glob(File.join(committed_cache_path, "*.value")) } + let(:all_files) do + Dir.glob(File.join(committed_cache_path, "**", "*"), File::FNM_DOTMATCH) + .reject { |f| File.directory?(f) } + .sort + end + let(:key_contents) { key_files.map { |f| File.read(f) }.sort } + + # Shared examples for validating cache state + shared_examples "validates committed cache files" do + it "has the expected number of key files" do + expect(key_files.length).to eq(3) + end + + it "has the expected number of value files" do + expect(value_files.length).to eq(3) + end + + it "preserves original keys in .key files" do + expect(key_contents).to contain_exactly(*cache_entries.keys.sort) + end + + it "has valid value files that can be deserialized" do + # All value files should be readable + value_files.each do |value_file| + expect(File.read(value_file).length).to be > 0 + end + end + + it "maintains the exact file count" do + # Should have exactly 7 files (3 entries × 2 files each + 1 README.md) + expect(all_files.length).to eq(7) + end + end + + describe "cache stability verification" do + # Capture initial state for comparison + let(:initial_file_list) { all_files } + + before(:each) do + # Ensure cache entries exist before each test using raw mode + cache_entries.each do |key, value| + store.write(key, value, raw: true) + end + end + + it "does not create new files when reading existing entries" do + # Capture state before reading + files_before = initial_file_list + + # Read existing entries with raw mode + cache_entries.keys.each do |key| + store.read(key, raw: true) + end + + # Verify no new files were created + expect(all_files).to eq(files_before) + end + + it "does not create new files when writing to existing keys with same values" do + # Capture state before writing + files_before = initial_file_list + + # Write same values to existing keys with raw mode + cache_entries.each do |key, value| + store.write(key, value, raw: true) + end + + # Verify no new files were created (same files should exist) + expect(all_files).to eq(files_before) + end + + it "has all expected cache files present" do + # Verify that all expected keys exist + cache_entries.each do |key, expected_value| + expect(store.read(key, raw: true)).to eq(expected_value) + end + end + + it "does not create new files during multiple read operations" do + # Capture state before reading + files_before = initial_file_list + + # Perform multiple read operations + 10.times do + cache_entries.keys.each do |key| + store.read(key, raw: true) + end + end + + # Verify no new files were created + expect(all_files).to eq(files_before) + end + + include_examples "validates committed cache files" + end + + describe "file content verification" do + include_examples "validates committed cache files" + + it "includes README.md documentation" do + readme_path = File.join(committed_cache_path, "README.md") + expect(File.exist?(readme_path)).to be true + expect(File.read(readme_path)).to include("Committed Cache Directory") + end + end +end diff --git a/spec/fixtures/committed_cache/39cc398b810ada504bc9d10624575cf9.key b/spec/fixtures/committed_cache/39cc398b810ada504bc9d10624575cf9.key new file mode 100644 index 0000000..3c82369 --- /dev/null +++ b/spec/fixtures/committed_cache/39cc398b810ada504bc9d10624575cf9.key @@ -0,0 +1 @@ +config:app:settings \ No newline at end of file diff --git a/spec/fixtures/committed_cache/39cc398b810ada504bc9d10624575cf9.value b/spec/fixtures/committed_cache/39cc398b810ada504bc9d10624575cf9.value new file mode 100644 index 0000000..91157f8 Binary files /dev/null and b/spec/fixtures/committed_cache/39cc398b810ada504bc9d10624575cf9.value differ diff --git a/spec/fixtures/committed_cache/39d971627e43c9bf240a49201b62f115.key b/spec/fixtures/committed_cache/39d971627e43c9bf240a49201b62f115.key new file mode 100644 index 0000000..c02f5aa --- /dev/null +++ b/spec/fixtures/committed_cache/39d971627e43c9bf240a49201b62f115.key @@ -0,0 +1 @@ +user:123:profile \ No newline at end of file diff --git a/spec/fixtures/committed_cache/39d971627e43c9bf240a49201b62f115.value b/spec/fixtures/committed_cache/39d971627e43c9bf240a49201b62f115.value new file mode 100644 index 0000000..b6cea95 Binary files /dev/null and b/spec/fixtures/committed_cache/39d971627e43c9bf240a49201b62f115.value differ diff --git a/spec/fixtures/committed_cache/README.md b/spec/fixtures/committed_cache/README.md new file mode 100644 index 0000000..2fc4b84 --- /dev/null +++ b/spec/fixtures/committed_cache/README.md @@ -0,0 +1,20 @@ +# Committed Cache Directory + +This directory contains cache files that are intentionally committed to source control for testing purposes. + +## Purpose + +This directory is used by the `committed_cache_spec.rb` test suite to verify that: +1. Cache files can be stored in a non-git-ignored directory +2. Once populated, no new files are created on subsequent runs +3. The cache remains stable across different environments and PRs + +## Files + +The cache files in this directory are generated by the test suite and should not be manually modified. +They represent a stable set of cache entries used for validation testing. + +## Do Not Ignore + +**Important:** This directory and its contents should NOT be added to `.gitignore`. +The cache files are intentionally version-controlled to ensure test stability. diff --git a/spec/fixtures/committed_cache/ca63624a99ad3e461af0d7c8a2d5a520.key b/spec/fixtures/committed_cache/ca63624a99ad3e461af0d7c8a2d5a520.key new file mode 100644 index 0000000..e45287e --- /dev/null +++ b/spec/fixtures/committed_cache/ca63624a99ad3e461af0d7c8a2d5a520.key @@ -0,0 +1 @@ +user:456:profile \ No newline at end of file diff --git a/spec/fixtures/committed_cache/ca63624a99ad3e461af0d7c8a2d5a520.value b/spec/fixtures/committed_cache/ca63624a99ad3e461af0d7c8a2d5a520.value new file mode 100644 index 0000000..e7edfdb Binary files /dev/null and b/spec/fixtures/committed_cache/ca63624a99ad3e461af0d7c8a2d5a520.value differ