src/test-mass-diversity-generator.lua

307 lines

1#!/usr/bin/env lua
2
3-- Test script for mass diversity page generation system
4-- Validates batch generation, HTML output, and file organization
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
12-- Issue 8-059: ensure the project's tmpfs-backed tmp/ symlink is in place
13-- before any test output is written underneath it.
14os.execute(string.format('"%s/scripts/ensure-tmp-symlink" "%s"', DIR, DIR))
15
16local utils = require('utils')
17local mass_generator = require('mass-diversity-generator')
18
19-- {{{ function read_file
20local function read_file(filepath)
21 local file = io.open(filepath, "r")
22 if not file then return nil end
23 local content = file:read("*a")
24 file:close()
25 return content
26end
27-- }}}
28
29-- {{{ function run_basic_generation_tests
30local function run_basic_generation_tests()
31 utils.log_info("๐Ÿงช Running basic mass generation tests...")
32
33 local similarity_file = DIR .. "/assets/embeddings/embeddinggemma_latest/similarity_matrix.json"
34 local poems_file = DIR .. "/assets/poems.json"
35 local test_output_dir = DIR .. "/tmp/diversity_test_output"
36
37 -- Clean test directory
38 os.execute("rm -rf " .. test_output_dir)
39 os.execute("mkdir -p " .. test_output_dir)
40
41 -- Test 1: Small batch generation (5 poems)
42 utils.log_info("Test 1: Small batch generation")
43 local result = mass_generator.test_mass_generation(similarity_file, poems_file, test_output_dir, 5)
44
45 if result then
46 utils.log_info(string.format("โœ… Test 1 passed - Generated %d pages with %.1f%% success rate",
47 result.total_pages, result.success_rate * 100))
48 else
49 utils.log_error("โŒ Test 1 failed - Small batch generation failed")
50 return false
51 end
52
53 -- Test 2: Check HTML file structure
54 utils.log_info("Test 2: HTML file validation")
55 local sample_file = test_output_dir .. "/poems/diversity/by-category/fediverse/poem-001.html"
56 if utils.file_exists(sample_file) then
57 local content = read_file(sample_file)
58 if content and content:find("<!DOCTYPE html>") and content:find("Diversity Chain:") then
59 utils.log_info("โœ… Test 2 passed - HTML structure valid")
60 else
61 utils.log_error("โŒ Test 2 failed - Invalid HTML structure")
62 return false
63 end
64 else
65 utils.log_warn("โš ๏ธ Test 2 skipped - No sample file found")
66 end
67
68 -- Test 3: Directory structure validation
69 utils.log_info("Test 3: Directory structure validation")
70 local diversity_dir = test_output_dir .. "/poems/diversity"
71 local index_file = diversity_dir .. "/index.html"
72 local category_dir = diversity_dir .. "/by-category"
73
74 if utils.file_exists(index_file) and utils.file_exists(category_dir) then
75 utils.log_info("โœ… Test 3 passed - Directory structure correct")
76 else
77 utils.log_error("โŒ Test 3 failed - Missing directory structure")
78 return false
79 end
80
81 return true
82end
83-- }}}
84
85-- {{{ function run_performance_tests
86local function run_performance_tests()
87 utils.log_info("โšก Running performance tests...")
88
89 local similarity_file = DIR .. "/assets/embeddings/embeddinggemma_latest/similarity_matrix.json"
90 local poems_file = DIR .. "/assets/poems.json"
91 local test_output_dir = DIR .. "/tmp/diversity_perf_test"
92
93 -- Clean test directory
94 os.execute("rm -rf " .. test_output_dir)
95 os.execute("mkdir -p " .. test_output_dir)
96
97 -- Performance test: 25 poems (5 batches)
98 local start_time = os.clock()
99 local result = mass_generator.test_mass_generation(similarity_file, poems_file, test_output_dir, 25)
100 local elapsed = os.clock() - start_time
101
102 if result then
103 local rate = result.total_pages / elapsed
104 utils.log_info(string.format("Performance results: %d pages in %.2f seconds (%.1f pages/sec)",
105 result.total_pages, elapsed, rate))
106
107 if rate > 5.0 then -- Should generate at least 5 pages per second
108 utils.log_info("โœ… Performance test passed - Generation rate > 5 pages/sec")
109 else
110 utils.log_warn("โš ๏ธ Performance test marginal - Rate below 5 pages/sec")
111 end
112
113 return result
114 else
115 utils.log_error("โŒ Performance test failed")
116 return false
117 end
118end
119-- }}}
120
121-- {{{ function run_html_validation_tests
122local function run_html_validation_tests()
123 utils.log_info("๐Ÿ“„ Running HTML validation tests...")
124
125 local similarity_file = DIR .. "/assets/embeddings/embeddinggemma_latest/similarity_matrix.json"
126 local poems_file = DIR .. "/assets/poems.json"
127 local test_output_dir = DIR .. "/tmp/diversity_html_test"
128
129 -- Clean and generate test pages
130 os.execute("rm -rf " .. test_output_dir)
131 os.execute("mkdir -p " .. test_output_dir)
132
133 local result = mass_generator.test_mass_generation(similarity_file, poems_file, test_output_dir, 10)
134
135 if not result then
136 utils.log_error("โŒ HTML validation test failed - No pages generated")
137 return false
138 end
139
140 -- Test HTML structure elements
141 local test_passed = true
142 local files_checked = 0
143
144 for _, page in ipairs(result.pages) do
145 if files_checked >= 3 then break end -- Check first 3 files
146
147 if utils.file_exists(page.file) then
148 local content = read_file(page.file)
149
150 -- Check required HTML elements
151 local checks = {
152 ["DOCTYPE"] = content:find("<!DOCTYPE html>"),
153 ["title"] = content:find("<title>Diversity Chain:"),
154 ["navigation"] = content:find("nav class=\"breadcrumb\""),
155 ["chain"] = content:find("diversity%-chain"),
156 ["styles"] = content:find("<style>"),
157 ["responsive"] = content:find("@media")
158 }
159
160 for check_name, found in pairs(checks) do
161 if not found then
162 utils.log_error(string.format("โŒ HTML validation failed - Missing %s in %s",
163 check_name, page.file))
164 test_passed = false
165 end
166 end
167
168 files_checked = files_checked + 1
169 end
170 end
171
172 if test_passed then
173 utils.log_info(string.format("โœ… HTML validation passed - %d files checked", files_checked))
174 return true
175 else
176 utils.log_error("โŒ HTML validation failed")
177 return false
178 end
179end
180-- }}}
181
182-- {{{ function run_batch_processing_tests
183local function run_batch_processing_tests()
184 utils.log_info("๐Ÿ”„ Running batch processing tests...")
185
186 local similarity_file = DIR .. "/assets/embeddings/embeddinggemma_latest/similarity_matrix.json"
187 local poems_file = DIR .. "/assets/poems.json"
188
189 -- Load data for batch testing
190 local similarity_data = require('diversity-chaining').load_similarity_data(similarity_file)
191 local poems_data = utils.read_json_file(poems_file)
192
193 if not similarity_data or not poems_data then
194 utils.log_error("โŒ Batch processing test failed - Could not load data")
195 return false
196 end
197
198 -- Create test batch with first 10 poems
199 local test_batch = {}
200 local count = 0
201 for poem_id, poem_data in pairs(poems_data.poems) do
202 if count >= 10 then break end
203 table.insert(test_batch, {id = tonumber(poem_id), data = poem_data})
204 count = count + 1
205 end
206
207 -- Test batch processing
208 local test_output_dir = DIR .. "/tmp/diversity_batch_test"
209 os.execute("rm -rf " .. test_output_dir)
210 os.execute("mkdir -p " .. test_output_dir)
211
212 local config = require('diversity-chaining').DiversityConfig:new({
213 chain_length = 3, -- Short chains for testing
214 debug_logging = false
215 })
216
217 local start_time = os.clock()
218 local batch_results = mass_generator.generate_diversity_batch(
219 test_batch, similarity_data, test_output_dir, poems_data, config
220 )
221 local elapsed = os.clock() - start_time
222
223 if #batch_results > 0 then
224 utils.log_info(string.format("โœ… Batch processing passed: %d/%d poems processed in %.2f seconds",
225 #batch_results, #test_batch, elapsed))
226
227 -- Verify files were created
228 local files_created = 0
229 for _, result in ipairs(batch_results) do
230 if utils.file_exists(result.file) then
231 files_created = files_created + 1
232 end
233 end
234
235 if files_created == #batch_results then
236 utils.log_info("โœ… All batch files created successfully")
237 return true
238 else
239 utils.log_error(string.format("โŒ Only %d/%d batch files created",
240 files_created, #batch_results))
241 return false
242 end
243 else
244 utils.log_error("โŒ Batch processing test failed - No results")
245 return false
246 end
247end
248-- }}}
249
250-- {{{ function main
251local function main()
252 utils.log_info("๐Ÿญ Starting Mass Diversity Generator Tests")
253 utils.log_info("=" .. string.rep("=", 50))
254
255 local tests = {
256 {"Basic Generation", run_basic_generation_tests},
257 {"Performance", run_performance_tests},
258 {"HTML Validation", run_html_validation_tests},
259 {"Batch Processing", run_batch_processing_tests}
260 }
261
262 local passed = 0
263 local total = #tests
264
265 for i, test in ipairs(tests) do
266 local name, test_func = test[1], test[2]
267 utils.log_info(string.format("\n๐Ÿ“‹ Test %d/%d: %s", i, total, name))
268 utils.log_info("-" .. string.rep("-", 30))
269
270 local success, result = pcall(test_func)
271 if success and result then
272 utils.log_info("โœ… " .. name .. " - PASSED")
273 passed = passed + 1
274 else
275 utils.log_error("โŒ " .. name .. " - FAILED")
276 if not success then
277 utils.log_error("Error: " .. tostring(result))
278 end
279 end
280 end
281
282 utils.log_info("\n" .. string.rep("=", 50))
283 utils.log_info(string.format("๐Ÿ Test Results: %d/%d passed (%.1f%%)", passed, total, (passed/total)*100))
284
285 if passed == total then
286 utils.log_info("๐ŸŽ‰ All tests passed! Mass diversity generator is ready.")
287 return true
288 else
289 utils.log_error("โŒ Some tests failed. Generator needs review.")
290 return false
291 end
292end
293-- }}}
294
295-- Run tests if executed directly
296if arg and arg[0] and arg[0]:match("test%-mass%-diversity%-generator%.lua$") then
297 local success = main()
298 os.exit(success and 0 or 1)
299end
300
301return {
302 main = main,
303 run_basic_generation_tests = run_basic_generation_tests,
304 run_performance_tests = run_performance_tests,
305 run_html_validation_tests = run_html_validation_tests,
306 run_batch_processing_tests = run_batch_processing_tests
307}