Spaces:
Sleeping
Sleeping
File size: 18,538 Bytes
d97b8f9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 | /**
* Generated Test Cases Preview Modal
*
* Shows AI-generated test cases before saving.
* User can review, edit, and selectively save test cases.
*/
import React, { useState } from "react";
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogDescription,
DialogFooter,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Checkbox } from "@/components/ui/checkbox";
import { Loader2, CheckCircle, XCircle, Save, Sparkles, Edit, ChevronDown, ChevronUp } from "lucide-react";
import { toast } from "@/lib/custom-toast";
import { FrontendTestCase } from "@/services/aiTestCaseApi";
import { tasksApi } from "@/services/tasksApi";
interface GeneratedTestCasesPreviewProps {
open: boolean;
onOpenChange: (open: boolean) => void;
testCases: FrontendTestCase[];
featureId: number;
onSaveComplete: () => void;
}
const GeneratedTestCasesPreview: React.FC<GeneratedTestCasesPreviewProps> = ({
open,
onOpenChange,
testCases,
featureId,
onSaveComplete,
}) => {
const [selectedCases, setSelectedCases] = useState<Set<number>>(new Set(testCases.map((_, i) => i)));
const [savingStates, setSavingStates] = useState<Record<number, 'idle' | 'saving' | 'saved' | 'error'>>({});
const [isSavingAll, setIsSavingAll] = useState(false);
const [expandedCases, setExpandedCases] = useState<Set<number>>(new Set([0])); // First case expanded by default
const [editedCases, setEditedCases] = useState<Record<number, FrontendTestCase>>({});
// Toggle selection
const toggleSelection = (index: number) => {
const newSelected = new Set(selectedCases);
if (newSelected.has(index)) {
newSelected.delete(index);
} else {
newSelected.add(index);
}
setSelectedCases(newSelected);
};
// Toggle expand/collapse
const toggleExpand = (index: number) => {
const newExpanded = new Set(expandedCases);
if (newExpanded.has(index)) {
newExpanded.delete(index);
} else {
newExpanded.add(index);
}
setExpandedCases(newExpanded);
};
// Select/deselect all
const toggleSelectAll = () => {
if (selectedCases.size === testCases.length) {
setSelectedCases(new Set());
} else {
setSelectedCases(new Set(testCases.map((_, i) => i)));
}
};
// Update edited test case
const updateTestCase = (index: number, field: keyof FrontendTestCase, value: string) => {
setEditedCases(prev => ({
...prev,
[index]: {
...(editedCases[index] || testCases[index]),
[field]: value,
},
}));
};
// Save single test case
const saveTestCase = async (index: number) => {
const testCase = editedCases[index] || testCases[index];
setSavingStates(prev => ({ ...prev, [index]: 'saving' }));
try {
const taskData = {
type: 'Test Case',
title: testCase.title,
description: testCase.description,
issuesId: featureId,
projectId: testCase.projectId,
status: testCase.status || 'Draft',
priority: testCase.priority || 'Medium',
severity: testCase.severity || 'Minor',
environment: testCase.environment || 'Staging',
stepsToReproduce: testCase.stepsToReproduce || '',
inputs: testCase.inputs || '',
expectedBehavior: testCase.expectedBehavior || '',
actualBehavior: testCase.actualBehavior || '',
rootCause: testCase.rootCause || '',
resolution: testCase.resolution || '',
version: testCase.version || '',
url: testCase.url || '',
};
await tasksApi.create(taskData);
setSavingStates(prev => ({ ...prev, [index]: 'saved' }));
toast.success(`Saved: ${testCase.title}`);
} catch (error) {
console.error('Failed to save test case:', error);
setSavingStates(prev => ({ ...prev, [index]: 'error' }));
toast.error(`Failed to save: ${testCase.title}`);
}
};
// Save all selected test cases
const saveSelected = async () => {
if (selectedCases.size === 0) {
toast.warning('No test cases selected');
return;
}
setIsSavingAll(true);
const indices = Array.from(selectedCases);
for (const index of indices) {
if (savingStates[index] !== 'saved') {
await saveTestCase(index);
}
}
setIsSavingAll(false);
onSaveComplete();
// Check if all saved successfully
const savedCount = indices.filter(i => savingStates[i] === 'saved').length;
if (savedCount === indices.length) {
toast.success(`All ${savedCount} test cases saved successfully!`);
onOpenChange(false);
}
};
// Get priority badge color
const getPriorityColor = (priority: string) => {
switch (priority?.toLowerCase()) {
case 'critical': return 'bg-red-100 text-red-800 border-red-300';
case 'high': return 'bg-orange-100 text-orange-800 border-orange-300';
case 'medium': return 'bg-yellow-100 text-yellow-800 border-yellow-300';
case 'low': return 'bg-green-100 text-green-800 border-green-300';
default: return 'bg-gray-100 text-gray-800 border-gray-300';
}
};
// Get severity badge color
const getSeverityColor = (severity: string) => {
switch (severity?.toLowerCase()) {
case 'blocker': return 'bg-red-100 text-red-800 border-red-300';
case 'critical': return 'bg-red-100 text-red-800 border-red-300';
case 'major': return 'bg-orange-100 text-orange-800 border-orange-300';
case 'minor': return 'bg-yellow-100 text-yellow-800 border-yellow-300';
case 'trivial': return 'bg-green-100 text-green-800 border-green-300';
default: return 'bg-gray-100 text-gray-800 border-gray-300';
}
};
// Reset state when dialog opens
React.useEffect(() => {
if (open) {
setSelectedCases(new Set(testCases.map((_, i) => i)));
setSavingStates({});
setExpandedCases(new Set([0]));
setEditedCases({});
}
}, [open, testCases]);
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="max-w-4xl max-h-[90vh] overflow-hidden flex flex-col">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<Sparkles className="h-5 w-5 text-purple-500" />
Generated Test Cases Preview
</DialogTitle>
<DialogDescription>
Review the AI-generated test cases below. Select which ones to save to the database.
You can edit any field before saving.
</DialogDescription>
</DialogHeader>
<div className="flex items-center justify-between py-2 border-b">
<div className="flex items-center gap-2">
<Checkbox
id="select-all"
checked={selectedCases.size === testCases.length}
onCheckedChange={toggleSelectAll}
/>
<label htmlFor="select-all" className="text-sm font-medium cursor-pointer">
Select All ({selectedCases.size}/{testCases.length} selected)
</label>
</div>
<div className="text-sm text-muted-foreground">
Feature ID: {featureId}
</div>
</div>
<div className="flex-1 overflow-y-auto space-y-3 py-4 pr-2">
{testCases.map((tc, index) => {
const currentCase = editedCases[index] || tc;
const isExpanded = expandedCases.has(index);
const isSelected = selectedCases.has(index);
const saveState = savingStates[index] || 'idle';
return (
<div
key={index}
className={`border rounded-lg transition-all ${
isSelected ? 'border-purple-300 bg-purple-50/30' : 'border-gray-200'
}`}
>
{/* Header */}
<div className="flex items-start gap-3 p-3">
<Checkbox
checked={isSelected}
onCheckedChange={() => toggleSelection(index)}
className="mt-1"
/>
<div className="flex-1 min-w-0">
<div className="flex items-start justify-between gap-2">
<div className="flex-1">
<input
type="text"
value={currentCase.title}
onChange={(e) => updateTestCase(index, 'title', e.target.value)}
className="font-medium text-sm w-full bg-transparent border-b border-transparent hover:border-gray-300 focus:border-purple-500 focus:outline-none px-0.5"
/>
<div className="flex flex-wrap items-center gap-1.5 mt-1.5">
<Badge variant="outline" className={getPriorityColor(currentCase.priority)}>
{currentCase.priority}
</Badge>
<Badge variant="outline" className={getSeverityColor(currentCase.severity)}>
{currentCase.severity}
</Badge>
<Badge variant="outline" className="bg-blue-50 text-blue-800 border-blue-200">
{currentCase.environment}
</Badge>
{saveState === 'saved' && (
<Badge className="bg-green-500 text-white">
<CheckCircle className="h-3 w-3 mr-1" />
Saved
</Badge>
)}
{saveState === 'error' && (
<Badge className="bg-red-500 text-white">
<XCircle className="h-3 w-3 mr-1" />
Error
</Badge>
)}
</div>
</div>
<div className="flex items-center gap-1">
<Button
variant="ghost"
size="sm"
onClick={() => saveTestCase(index)}
disabled={saveState === 'saving' || saveState === 'saved'}
className="h-8"
>
{saveState === 'saving' ? (
<Loader2 className="h-4 w-4 animate-spin" />
) : (
<Save className="h-4 w-4" />
)}
</Button>
<Button
variant="ghost"
size="sm"
onClick={() => toggleExpand(index)}
className="h-8"
>
{isExpanded ? (
<ChevronUp className="h-4 w-4" />
) : (
<ChevronDown className="h-4 w-4" />
)}
</Button>
</div>
</div>
</div>
</div>
{/* Expanded Content */}
{isExpanded && (
<div className="px-3 pb-3 pt-0 space-y-3 border-t">
{/* Description */}
<div className="pt-3">
<label className="text-xs font-medium text-muted-foreground">Description</label>
<textarea
value={currentCase.description}
onChange={(e) => updateTestCase(index, 'description', e.target.value)}
className="w-full mt-1 text-sm border rounded-md p-2 min-h-[60px] focus:outline-none focus:ring-1 focus:ring-purple-500"
/>
</div>
{/* Steps to Reproduce */}
<div>
<label className="text-xs font-medium text-muted-foreground">Steps to Reproduce</label>
<textarea
value={currentCase.stepsToReproduce || ''}
onChange={(e) => updateTestCase(index, 'stepsToReproduce', e.target.value)}
className="w-full mt-1 text-sm border rounded-md p-2 min-h-[80px] font-mono focus:outline-none focus:ring-1 focus:ring-purple-500"
placeholder="1. First step 2. Second step 3. Third step"
/>
</div>
{/* Test Data / Inputs */}
<div>
<label className="text-xs font-medium text-muted-foreground">Test Data / Inputs</label>
<textarea
value={currentCase.inputs || ''}
onChange={(e) => updateTestCase(index, 'inputs', e.target.value)}
className="w-full mt-1 text-sm border rounded-md p-2 min-h-[40px] focus:outline-none focus:ring-1 focus:ring-purple-500"
/>
</div>
{/* Expected Behavior */}
<div>
<label className="text-xs font-medium text-muted-foreground">Expected Behavior</label>
<textarea
value={currentCase.expectedBehavior || ''}
onChange={(e) => updateTestCase(index, 'expectedBehavior', e.target.value)}
className="w-full mt-1 text-sm border rounded-md p-2 min-h-[40px] focus:outline-none focus:ring-1 focus:ring-purple-500"
/>
</div>
{/* Two column layout for smaller fields */}
<div className="grid grid-cols-2 gap-3">
<div>
<label className="text-xs font-medium text-muted-foreground">Priority</label>
<select
value={currentCase.priority || 'Medium'}
onChange={(e) => updateTestCase(index, 'priority', e.target.value)}
className="w-full mt-1 text-sm border rounded-md p-2 focus:outline-none focus:ring-1 focus:ring-purple-500"
>
<option value="Critical">Critical</option>
<option value="High">High</option>
<option value="Medium">Medium</option>
<option value="Low">Low</option>
</select>
</div>
<div>
<label className="text-xs font-medium text-muted-foreground">Severity</label>
<select
value={currentCase.severity || 'Minor'}
onChange={(e) => updateTestCase(index, 'severity', e.target.value)}
className="w-full mt-1 text-sm border rounded-md p-2 focus:outline-none focus:ring-1 focus:ring-purple-500"
>
<option value="Blocker">Blocker</option>
<option value="Critical">Critical</option>
<option value="Major">Major</option>
<option value="Minor">Minor</option>
<option value="Trivial">Trivial</option>
</select>
</div>
</div>
<div className="grid grid-cols-2 gap-3">
<div>
<label className="text-xs font-medium text-muted-foreground">Environment</label>
<select
value={currentCase.environment || 'Staging'}
onChange={(e) => updateTestCase(index, 'environment', e.target.value)}
className="w-full mt-1 text-sm border rounded-md p-2 focus:outline-none focus:ring-1 focus:ring-purple-500"
>
<option value="Development">Development</option>
<option value="Testing">Testing</option>
<option value="Staging">Staging</option>
<option value="Production">Production</option>
</select>
</div>
<div>
<label className="text-xs font-medium text-muted-foreground">Version</label>
<input
type="text"
value={currentCase.version || ''}
onChange={(e) => updateTestCase(index, 'version', e.target.value)}
className="w-full mt-1 text-sm border rounded-md p-2 focus:outline-none focus:ring-1 focus:ring-purple-500"
placeholder="e.g., 1.2.3"
/>
</div>
</div>
</div>
)}
</div>
);
})}
</div>
<DialogFooter className="border-t pt-4">
<div className="flex items-center justify-between w-full">
<div className="text-sm text-muted-foreground">
{selectedCases.size} test case(s) selected
</div>
<div className="flex gap-2">
<Button variant="outline" onClick={() => onOpenChange(false)}>
Cancel
</Button>
<Button
onClick={saveSelected}
disabled={selectedCases.size === 0 || isSavingAll}
className="bg-purple-600 hover:bg-purple-700"
>
{isSavingAll ? (
<>
<Loader2 className="h-4 w-4 animate-spin mr-2" />
Saving...
</>
) : (
<>
<Save className="h-4 w-4 mr-2" />
Save Selected ({selectedCases.size})
</>
)}
</Button>
</div>
</div>
</DialogFooter>
</DialogContent>
</Dialog>
);
};
export default GeneratedTestCasesPreview;
|