src/test-diversity-chaining.lua
1#!/usr/bin/env lua
2
3-- Test script for diversity chaining algorithm
4-- Validates algorithm functionality with real poetry data
5
6local DIR = DIR or "/mnt/mtwo/programming/ai-stuff/neocities-modernization"
7
8-- Set up path for module loading
9package.path = './libs/?.lua;' .. package.path
10package.path = './src/?.lua;' .. package.path
11
12local utils = require('utils')
13local diversity = require('diversity-chaining')
14
15-- {{{ function run_basic_tests
16local function run_basic_tests()
17 utils.log_info("๐งช Running basic diversity chaining tests...")
18
19 local similarity_file = DIR .. "/assets/embeddings/embeddinggemma_latest/similarity_matrix.json"
20 local poems_file = DIR .. "/assets/poems.json"
21
22 -- Test 1: Basic chain generation
23 utils.log_info("Test 1: Basic chain generation")
24 local result = diversity.test_diversity_algorithm(similarity_file, poems_file, 1, 5)
25
26 if result then
27 utils.log_info("โ
Test 1 passed - Basic chain generation works")
28 else
29 utils.log_error("โ Test 1 failed - Basic chain generation failed")
30 return false
31 end
32
33 -- Test 2: Different starting poems
34 utils.log_info("Test 2: Different starting poems")
35 local test_poems = {1, 100, 500, 1000}
36 for _, poem_id in ipairs(test_poems) do
37 local test_result = diversity.test_diversity_algorithm(similarity_file, poems_file, poem_id, 3)
38 if not test_result then
39 utils.log_warn("Warning: Chain generation failed for poem " .. poem_id)
40 end
41 end
42 utils.log_info("โ
Test 2 completed - Multiple starting poem test")
43
44 -- Test 3: Configuration validation
45 utils.log_info("Test 3: Configuration validation")
46 local config = diversity.DiversityConfig:new({
47 chain_length = 15,
48 debug_logging = true,
49 max_length = 50
50 })
51
52 if config.chain_length == 15 and config.debug_logging == true then
53 utils.log_info("โ
Test 3 passed - Configuration system works")
54 else
55 utils.log_error("โ Test 3 failed - Configuration system broken")
56 return false
57 end
58
59 return true
60end
61-- }}}
62
63-- {{{ function run_performance_tests
64local function run_performance_tests()
65 utils.log_info("โก Running performance tests...")
66
67 local similarity_file = DIR .. "/assets/embeddings/embeddinggemma_latest/similarity_matrix.json"
68 local poems_file = DIR .. "/assets/poems.json"
69
70 -- Load data once
71 local similarity_data = diversity.load_similarity_data(similarity_file)
72 local poems_data = utils.read_json_file(poems_file)
73
74 if not similarity_data or not poems_data then
75 utils.log_error("Failed to load test data")
76 return false
77 end
78
79 -- Performance test: single chain generation
80 local start_time = os.clock()
81 local config = diversity.DiversityConfig:new({chain_length = 20, debug_logging = false})
82 local result = diversity.generate_maximum_diversity_chain(1, poems_data.poems, similarity_data, config)
83 local elapsed = os.clock() - start_time
84
85 utils.log_info(string.format("Single chain generation: %.3f seconds", elapsed))
86
87 if elapsed < 1.0 then -- Should complete in under 1 second
88 utils.log_info("โ
Performance test passed - Single chain < 1s")
89 else
90 utils.log_warn("โ ๏ธ Performance test marginal - Single chain took " .. elapsed .. "s")
91 end
92
93 return result ~= nil
94end
95-- }}}
96
97-- {{{ function run_diversity_analysis_tests
98local function run_diversity_analysis_tests()
99 utils.log_info("๐ Running diversity analysis tests...")
100
101 local similarity_file = DIR .. "/assets/embeddings/embeddinggemma_latest/similarity_matrix.json"
102 local poems_file = DIR .. "/assets/poems.json"
103
104 local similarity_data = diversity.load_similarity_data(similarity_file)
105 local poems_data = utils.read_json_file(poems_file)
106
107 if not similarity_data or not poems_data then
108 return false
109 end
110
111 -- Generate a test chain
112 local config = diversity.DiversityConfig:new({chain_length = 10})
113 local chain_result = diversity.generate_maximum_diversity_chain(1, poems_data.poems, similarity_data, config)
114
115 if not chain_result or not chain_result.chain then
116 utils.log_error("Failed to generate test chain for analysis")
117 return false
118 end
119
120 -- Analyze the chain
121 local analysis = diversity.analyze_chain_diversity(chain_result, similarity_data)
122
123 if analysis.error then
124 utils.log_error("Analysis failed: " .. analysis.error)
125 return false
126 end
127
128 utils.log_info(string.format("Diversity Analysis Results:"))
129 utils.log_info(string.format(" Chain length: %d", analysis.chain_length))
130 utils.log_info(string.format(" Average diversity: %.3f", analysis.average_diversity))
131 utils.log_info(string.format(" Median diversity: %.3f", analysis.median_diversity))
132 utils.log_info(string.format(" Min diversity: %.3f", analysis.min_diversity))
133 utils.log_info(string.format(" Max diversity: %.3f", analysis.max_diversity))
134 utils.log_info(string.format(" Quality score: %.3f", analysis.quality_score))
135
136 -- Validate diversity metrics
137 if analysis.average_diversity > 0 and analysis.quality_score > 0 then
138 utils.log_info("โ
Diversity analysis test passed")
139 return true
140 else
141 utils.log_error("โ Diversity analysis test failed - poor diversity scores")
142 return false
143 end
144end
145-- }}}
146
147-- {{{ function run_batch_generation_test
148local function run_batch_generation_test()
149 utils.log_info("๐ Running batch generation test...")
150
151 local similarity_file = DIR .. "/assets/embeddings/embeddinggemma_latest/similarity_matrix.json"
152 local poems_file = DIR .. "/assets/poems.json"
153
154 local similarity_data = diversity.load_similarity_data(similarity_file)
155 local poems_data = utils.read_json_file(poems_file)
156
157 if not similarity_data or not poems_data then
158 return false
159 end
160
161 -- Test with small batch
162 local test_poem_ids = {1, 2, 3, 4, 5}
163 local config = diversity.DiversityConfig:new({chain_length = 5, debug_logging = false})
164
165 local start_time = os.clock()
166 local batch_result = diversity.generate_multiple_diversity_chains(test_poem_ids, poems_data.poems, similarity_data, config)
167 local elapsed = os.clock() - start_time
168
169 utils.log_info(string.format("Batch generation: %.3f seconds for %d chains", elapsed, #test_poem_ids))
170
171 if batch_result and batch_result.metadata then
172 utils.log_info(string.format("Batch results: %d/%d successful (%.1f%% success rate)",
173 batch_result.metadata.successful_chains,
174 batch_result.metadata.total_requested,
175 batch_result.metadata.success_rate * 100))
176
177 if batch_result.metadata.success_rate >= 0.8 then -- 80% success rate minimum
178 utils.log_info("โ
Batch generation test passed")
179 return true
180 else
181 utils.log_warn("โ ๏ธ Batch generation test marginal - low success rate")
182 return true -- Still passing but with warning
183 end
184 else
185 utils.log_error("โ Batch generation test failed")
186 return false
187 end
188end
189-- }}}
190
191-- {{{ function main
192local function main()
193 utils.log_info("๐ Starting Diversity Chaining Algorithm Tests")
194 utils.log_info("=" .. string.rep("=", 50))
195
196 local tests = {
197 {"Basic Functionality", run_basic_tests},
198 {"Performance", run_performance_tests},
199 {"Diversity Analysis", run_diversity_analysis_tests},
200 {"Batch Generation", run_batch_generation_test}
201 }
202
203 local passed = 0
204 local total = #tests
205
206 for i, test in ipairs(tests) do
207 local name, test_func = test[1], test[2]
208 utils.log_info(string.format("\n๐ Test %d/%d: %s", i, total, name))
209 utils.log_info("-" .. string.rep("-", 30))
210
211 local success, result = pcall(test_func)
212 if success and result then
213 utils.log_info("โ
" .. name .. " - PASSED")
214 passed = passed + 1
215 else
216 utils.log_error("โ " .. name .. " - FAILED")
217 if not success then
218 utils.log_error("Error: " .. tostring(result))
219 end
220 end
221 end
222
223 utils.log_info("\n" .. string.rep("=", 50))
224 utils.log_info(string.format("๐ Test Results: %d/%d passed (%.1f%%)", passed, total, (passed/total)*100))
225
226 if passed == total then
227 utils.log_info("๐ All tests passed! Diversity chaining algorithm is ready.")
228 return true
229 else
230 utils.log_error("โ Some tests failed. Algorithm needs review.")
231 return false
232 end
233end
234-- }}}
235
236-- Run tests if executed directly
237if arg and arg[0] and arg[0]:match("test%-diversity%-chaining%.lua$") then
238 local success = main()
239 os.exit(success and 0 or 1)
240end
241
242return {
243 main = main,
244 run_basic_tests = run_basic_tests,
245 run_performance_tests = run_performance_tests,
246 run_diversity_analysis_tests = run_diversity_analysis_tests,
247 run_batch_generation_test = run_batch_generation_test
248}