|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | require "spec_helper" |
| 4 | +require "json" |
4 | 5 |
|
5 | 6 | RSpec.describe "Committed Cache Directory" do |
6 | 7 | # Use a non-git-ignored directory for cache storage |
7 | 8 | let(:committed_cache_path) { File.join(__dir__, "fixtures", "committed_cache") } |
8 | | - let(:store) { ActiveSupport::Cache::SourceControlCacheStore.new(cache_path: committed_cache_path) } |
9 | | - |
10 | | - describe "initial cache population" do |
11 | | - it "creates the cache directory if it doesn't exist" do |
12 | | - expect(File.directory?(committed_cache_path)).to be true |
| 9 | + # Configure store with raw mode to avoid compression |
| 10 | + let(:store) { ActiveSupport::Cache::SourceControlCacheStore.new(cache_path: committed_cache_path, compress: false) } |
| 11 | + |
| 12 | + # Predefined cache entries (keys and values as JSON strings for readability) |
| 13 | + let(:cache_entries) do |
| 14 | + { |
| 15 | + "user:123:profile" => { name: "John Doe", email: "[email protected]" }.to_json, |
| 16 | + "user:456:profile" => { name: "Jane Smith", email: "[email protected]" }.to_json, |
| 17 | + "config:app:settings" => { theme: "dark", language: "en" }.to_json |
| 18 | + } |
| 19 | + end |
| 20 | + |
| 21 | + # Shared examples for validating cache state |
| 22 | + shared_examples "validates committed cache files" do |
| 23 | + it "has the expected number of key files" do |
| 24 | + key_files = Dir.glob(File.join(committed_cache_path, "*.key")) |
| 25 | + expect(key_files.length).to eq(3) |
13 | 26 | end |
14 | | - |
15 | | - it "populates cache with predefined entries" do |
16 | | - # Write predefined cache entries that will be committed |
17 | | - store.write("user:123:profile", { name: "John Doe", email: "[email protected]" }) |
18 | | - store.write("user:456:profile", { name: "Jane Smith", email: "[email protected]" }) |
19 | | - store.write("config:app:settings", { theme: "dark", language: "en" }) |
20 | | - |
21 | | - # Verify the entries were written |
22 | | - expect(store.read("user:123:profile")).to eq({ name: "John Doe", email: "[email protected]" }) |
23 | | - expect(store.read("user:456:profile")).to eq({ name: "Jane Smith", email: "[email protected]" }) |
24 | | - expect(store.read("config:app:settings")).to eq({ theme: "dark", language: "en" }) |
| 27 | + |
| 28 | + it "has the expected number of value files" do |
| 29 | + value_files = Dir.glob(File.join(committed_cache_path, "*.value")) |
| 30 | + expect(value_files.length).to eq(3) |
25 | 31 | end |
26 | | - |
27 | | - it "creates .key and .value files for each entry" do |
28 | | - # Ensure files exist |
29 | | - cache_files = Dir.glob(File.join(committed_cache_path, "*")) |
30 | | - |
31 | | - # We expect at least 6 files (3 entries × 2 files each) |
32 | | - expect(cache_files.length).to be >= 6 |
33 | | - |
34 | | - # Check that we have both .key and .value files |
35 | | - key_files = cache_files.select { |f| f.end_with?(".key") } |
36 | | - value_files = cache_files.select { |f| f.end_with?(".value") } |
37 | | - |
38 | | - expect(key_files.length).to be >= 3 |
39 | | - expect(value_files.length).to be >= 3 |
| 32 | + |
| 33 | + it "preserves original keys in .key files" do |
| 34 | + key_files = Dir.glob(File.join(committed_cache_path, "*.key")) |
| 35 | + key_contents = key_files.map { |f| File.read(f) }.sort |
| 36 | + expect(key_contents).to contain_exactly(*cache_entries.keys.sort) |
| 37 | + end |
| 38 | + |
| 39 | + it "has valid value files that can be deserialized" do |
| 40 | + value_files = Dir.glob(File.join(committed_cache_path, "*.value")) |
| 41 | + # All value files should be readable |
| 42 | + value_files.each do |value_file| |
| 43 | + expect(File.read(value_file).length).to be > 0 |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + it "maintains the exact file count" do |
| 48 | + current_files = Dir.glob(File.join(committed_cache_path, "**", "*"), File::FNM_DOTMATCH) |
| 49 | + .reject { |f| File.directory?(f) } |
| 50 | + .sort |
| 51 | + # Should have exactly 7 files (3 entries × 2 files each + 1 README.md) |
| 52 | + expect(current_files.length).to eq(7) |
40 | 53 | end |
41 | 54 | end |
42 | 55 |
|
|
49 | 62 | end |
50 | 63 |
|
51 | 64 | before(:each) do |
52 | | - # Ensure cache entries exist before each test |
53 | | - store.write("user:123:profile", { name: "John Doe", email: "[email protected]" }) |
54 | | - store.write("user:456:profile", { name: "Jane Smith", email: "[email protected]" }) |
55 | | - store.write("config:app:settings", { theme: "dark", language: "en" }) |
| 65 | + # Ensure cache entries exist before each test using raw mode |
| 66 | + cache_entries.each do |key, value| |
| 67 | + store.write(key, value, raw: true) |
| 68 | + end |
56 | 69 | end |
57 | 70 |
|
58 | 71 | it "does not create new files when reading existing entries" do |
59 | 72 | # Capture state before reading |
60 | 73 | files_before = initial_file_list |
61 | 74 |
|
62 | | - # Read existing entries |
63 | | - store.read("user:123:profile") |
64 | | - store.read("user:456:profile") |
65 | | - store.read("config:app:settings") |
| 75 | + # Read existing entries with raw mode |
| 76 | + cache_entries.keys.each do |key| |
| 77 | + store.read(key, raw: true) |
| 78 | + end |
66 | 79 |
|
67 | 80 | # Get current file list |
68 | 81 | current_files = Dir.glob(File.join(committed_cache_path, "**", "*"), File::FNM_DOTMATCH) |
|
77 | 90 | # Capture state before writing |
78 | 91 | files_before = initial_file_list |
79 | 92 |
|
80 | | - # Write same values to existing keys |
81 | | - store.write("user:123:profile", { name: "John Doe", email: "[email protected]" }) |
82 | | - store.write("user:456:profile", { name: "Jane Smith", email: "[email protected]" }) |
83 | | - store.write("config:app:settings", { theme: "dark", language: "en" }) |
| 93 | + # Write same values to existing keys with raw mode |
| 94 | + cache_entries.each do |key, value| |
| 95 | + store.write(key, value, raw: true) |
| 96 | + end |
84 | 97 |
|
85 | 98 | # Get current file list |
86 | 99 | current_files = Dir.glob(File.join(committed_cache_path, "**", "*"), File::FNM_DOTMATCH) |
|
93 | 106 |
|
94 | 107 | it "has all expected cache files present" do |
95 | 108 | # Verify that all expected keys exist |
96 | | - expect(store.read("user:123:profile")).to eq({ name: "John Doe", email: "[email protected]" }) |
97 | | - expect(store.read("user:456:profile")).to eq({ name: "Jane Smith", email: "[email protected]" }) |
98 | | - expect(store.read("config:app:settings")).to eq({ theme: "dark", language: "en" }) |
99 | | - end |
100 | | - |
101 | | - it "maintains the exact file count" do |
102 | | - current_files = Dir.glob(File.join(committed_cache_path, "**", "*"), File::FNM_DOTMATCH) |
103 | | - .reject { |f| File.directory?(f) } |
104 | | - .sort |
105 | | - |
106 | | - # Should have exactly 7 files (3 entries × 2 files each + 1 README.md) |
107 | | - expect(current_files.length).to eq(7) |
| 109 | + cache_entries.each do |key, expected_value| |
| 110 | + expect(store.read(key, raw: true)).to eq(expected_value) |
| 111 | + end |
108 | 112 | end |
109 | 113 |
|
110 | 114 | it "does not create new files during multiple read operations" do |
|
113 | 117 |
|
114 | 118 | # Perform multiple read operations |
115 | 119 | 10.times do |
116 | | - store.read("user:123:profile") |
117 | | - store.read("user:456:profile") |
118 | | - store.read("config:app:settings") |
| 120 | + cache_entries.keys.each do |key| |
| 121 | + store.read(key, raw: true) |
| 122 | + end |
119 | 123 | end |
120 | 124 |
|
121 | 125 | # Get current file list |
|
126 | 130 | # Verify no new files were created |
127 | 131 | expect(current_files).to eq(files_before) |
128 | 132 | end |
| 133 | + |
| 134 | + include_examples "validates committed cache files" |
129 | 135 | end |
130 | 136 |
|
131 | 137 | describe "file content verification" do |
132 | | - it "preserves original keys in .key files" do |
133 | | - key_files = Dir.glob(File.join(committed_cache_path, "*.key")) |
134 | | - expect(key_files.length).to eq(3) |
135 | | - |
136 | | - # Read all key files and verify they contain expected keys |
137 | | - key_contents = key_files.map { |f| File.read(f) }.sort |
138 | | - expect(key_contents).to contain_exactly( |
139 | | - "config:app:settings", |
140 | | - "user:123:profile", |
141 | | - "user:456:profile" |
142 | | - ) |
143 | | - end |
144 | | - |
145 | | - it "has valid value files that can be deserialized" do |
146 | | - value_files = Dir.glob(File.join(committed_cache_path, "*.value")) |
147 | | - expect(value_files.length).to eq(3) |
148 | | - |
149 | | - # All value files should be readable and deserializable |
150 | | - value_files.each do |value_file| |
151 | | - expect(File.read(value_file).length).to be > 0 |
152 | | - end |
153 | | - end |
154 | | - |
| 138 | + include_examples "validates committed cache files" |
| 139 | + |
155 | 140 | it "includes README.md documentation" do |
156 | 141 | readme_path = File.join(committed_cache_path, "README.md") |
157 | 142 | expect(File.exist?(readme_path)).to be true |
|
0 commit comments