|
247 | 247 | ->toContain('Domains') |
248 | 248 | ->toContain('SSL Certificates and HTTPS'); |
249 | 249 | })->skip(! $pluginInstalled, 'AiAssistant plugin not installed'); |
| 250 | + |
| 251 | +it('parses gallery_changes from ai response', function () { |
| 252 | + AssistantAgent::fake([ |
| 253 | + '{"reply": "Updated gallery type.", "is_undo": false, "design_changes": [], "settings_changes": [], "section_changes": [], "entry_changes": [], "gallery_changes": [{"operation": "update_setting", "section": "portfolio", "entry_id": "3", "setting": "type", "value": "row"}]}', |
| 254 | + ]); |
| 255 | + |
| 256 | + $agent = new AssistantAgent('system prompt'); |
| 257 | + $result = $agent->chat([['role' => 'user', 'content' => 'change gallery to row']]); |
| 258 | + |
| 259 | + expect($result['gallery_changes'])->toHaveCount(1) |
| 260 | + ->and($result['gallery_changes'][0]['operation'])->toBe('update_setting') |
| 261 | + ->and($result['gallery_changes'][0]['section'])->toBe('portfolio') |
| 262 | + ->and($result['gallery_changes'][0]['entry_id'])->toBe('3') |
| 263 | + ->and($result['gallery_changes'][0]['setting'])->toBe('type') |
| 264 | + ->and($result['gallery_changes'][0]['value'])->toBe('row'); |
| 265 | +})->skip(! $pluginInstalled, 'AiAssistant plugin not installed'); |
| 266 | + |
| 267 | +it('returns empty gallery_changes when not in ai response', function () { |
| 268 | + AssistantAgent::fake([ |
| 269 | + '{"reply": "Done.", "design_changes": [], "settings_changes": [], "section_changes": []}', |
| 270 | + ]); |
| 271 | + |
| 272 | + $agent = new AssistantAgent('system prompt'); |
| 273 | + $result = $agent->chat([['role' => 'user', 'content' => 'hello']]); |
| 274 | + |
| 275 | + expect($result['gallery_changes'])->toBeArray()->toBeEmpty(); |
| 276 | +})->skip(! $pluginInstalled, 'AiAssistant plugin not installed'); |
| 277 | + |
| 278 | +it('includes gallery changes in change history section', function () { |
| 279 | + $controller = new AiChatController; |
| 280 | + $method = new ReflectionMethod($controller, 'buildChangeHistorySection'); |
| 281 | + |
| 282 | + $changeHistory = [ |
| 283 | + [ |
| 284 | + 'user_message' => 'change gallery to row', |
| 285 | + 'design_changes' => [], |
| 286 | + 'settings_changes' => [], |
| 287 | + 'section_changes' => [], |
| 288 | + 'entry_changes' => [], |
| 289 | + 'gallery_changes' => [ |
| 290 | + ['operation' => 'update_setting', 'section' => 'portfolio', 'entry_id' => '3', 'setting' => 'type', 'value' => 'row', 'previous_value' => 'slideshow'], |
| 291 | + ], |
| 292 | + ], |
| 293 | + [ |
| 294 | + 'user_message' => 'update image caption', |
| 295 | + 'design_changes' => [], |
| 296 | + 'settings_changes' => [], |
| 297 | + 'section_changes' => [], |
| 298 | + 'entry_changes' => [], |
| 299 | + 'gallery_changes' => [ |
| 300 | + ['operation' => 'update_caption', 'section' => 'portfolio', 'entry_id' => '3', 'file_index' => 0, 'value' => '<p>New caption</p>', 'previous_value' => '<p>Old caption</p>'], |
| 301 | + ], |
| 302 | + ], |
| 303 | + ]; |
| 304 | + |
| 305 | + $result = $method->invoke($controller, $changeHistory); |
| 306 | + |
| 307 | + expect($result) |
| 308 | + ->toContain('gallery: update_setting entry #3 in "portfolio"') |
| 309 | + ->toContain('"slideshow" → "row"') |
| 310 | + ->toContain('gallery: update_caption entry #3 in "portfolio" file[0]') |
| 311 | + ->toContain('"Old caption" → "New caption"'); |
| 312 | +})->skip(! $pluginInstalled, 'AiAssistant plugin not installed'); |
| 313 | + |
| 314 | +it('restricts change_history gallery_changes operation to update_setting and update_caption', function () { |
| 315 | + $request = new AiChatRequest; |
| 316 | + $rules = $request->rules(); |
| 317 | + |
| 318 | + expect($rules['change_history.*.gallery_changes.*.operation']) |
| 319 | + ->toContain('in:update_setting,update_caption'); |
| 320 | +})->skip(! $pluginInstalled, 'AiAssistant plugin not installed'); |
| 321 | + |
| 322 | +it('includes galleries section in system prompt', function () { |
| 323 | + $controller = new AiChatController; |
| 324 | + $method = new ReflectionMethod($controller, 'buildGalleriesSection'); |
| 325 | + |
| 326 | + $result = $method->invoke($controller); |
| 327 | + |
| 328 | + expect($result) |
| 329 | + ->toContain('Galleries') |
| 330 | + ->toContain('get_entry_gallery') |
| 331 | + ->toContain('update_setting') |
| 332 | + ->toContain('update_caption') |
| 333 | + ->toContain('slideshow') |
| 334 | + ->toContain('fullscreen') |
| 335 | + ->toContain('file_index') |
| 336 | + ->toContain('manual'); |
| 337 | +})->skip(! $pluginInstalled, 'AiAssistant plugin not installed'); |
0 commit comments