All files / client/src/pages/model-map test-lab.tsx

0% Statements 0/109
0% Branches 0/72
0% Functions 0/43
0% Lines 0/103

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
import { useState, useEffect } from "react";
import { useQuery, useMutation } from "@tanstack/react-query";
import { useLocation } from "wouter";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardHeader } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import {
  Collapsible,
  CollapsibleContent,
  CollapsibleTrigger,
} from "@/components/ui/collapsible";
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { Beaker, Plus, ChevronDown, Trash2, Search } from "lucide-react";
import { apiRequest, queryClient } from "@/lib/queryClient";
import { ModelAvatar } from "@/components/model-avatar";
import { CreateTestDialog } from "@/components/create-test-dialog";
import { CompleteTestDialog } from "@/components/complete-test-dialog";
import type { AiModel, AiTool, ModelTest, ModelTestResult, UseCase } from "@shared/schema";
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
  DialogFooter,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Checkbox } from "@/components/ui/checkbox";
 
// Extended types for populated data
type PopulatedTestResult = ModelTestResult & { model?: AiModel; tool?: AiTool };
type PopulatedTest = ModelTest & { results?: PopulatedTestResult[]; useCase?: { category: string } };
 
export default function TestLabPage() {
  const [location] = useLocation();
  const [createDialogOpen, setCreateDialogOpen] = useState(false);
  const [completeDialogOpen, setCompleteDialogOpen] = useState(false);
  const [selectedTest, setSelectedTest] = useState<PopulatedTest | null>(null);
  const [selectedResult, setSelectedResult] = useState<PopulatedTestResult | null>(null);
  const [deleteTestId, setDeleteTestId] = useState<string | null>(null);
  const [expandedPrompts, setExpandedPrompts] = useState<Set<string>>(new Set());
  const [selectedUseCaseId, setSelectedUseCaseId] = useState<string | null>(null);
  const [addingModelsToTest, setAddingModelsToTest] = useState<PopulatedTest | null>(null);
  const [searchQuery, setSearchQuery] = useState("");
  const [selectedModelsToAdd, setSelectedModelsToAdd] = useState<string[]>([]);
  const [selectedToolsToAdd, setSelectedToolsToAdd] = useState<string[]>([]);
 
  // Extract useCase from query parameter
  useEffect(() => {
    const params = new URLSearchParams(window.location.search);
    const useCaseId = params.get("useCase");
    if (useCaseId) {
      setSelectedUseCaseId(useCaseId);
      setCreateDialogOpen(true);
    }
  }, []);
 
  // Fetch tests with results
  const { data: tests = [], isLoading } = useQuery<PopulatedTest[]>({
    queryKey: ["/api/model-map/tests"],
    queryFn: async () => {
      const res = await apiRequest("GET", "/api/model-map/tests?includeResults=true");
      return res.json();
    },
  });
 
  // Delete test mutation
  const deleteTestMutation = useMutation({
    mutationFn: async (testId: string) => {
      await apiRequest("DELETE", `/api/model-map/tests/${testId}`);
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["/api/model-map/tests"] });
      setDeleteTestId(null);
    },
  });
 
  // Fetch all available models
  const { data: allModels = [] } = useQuery<AiModel[]>({
    queryKey: ["/api/model-map/models"],
    queryFn: async () => {
      const res = await apiRequest("GET", "/api/model-map/models");
      return res.json();
    },
  });
 
  // Fetch all available tools
  const { data: allTools = [] } = useQuery<AiTool[]>({
    queryKey: ["/api/model-map/tools"],
    queryFn: async () => {
      const res = await apiRequest("GET", "/api/model-map/tools");
      return res.json();
    },
  });
 
  // Add models to existing test mutation
  const addModelsToTestMutation = useMutation({
    mutationFn: async (data: { testId: string; modelIds: string[]; toolIds: string[] }) => {
      const { testId, modelIds, toolIds } = data;
 
      // Create test results for each new model
      for (const modelId of modelIds) {
        await apiRequest("POST", `/api/model-map/tests/${testId}/results`, {
          modelId,
          status: "pending",
        });
      }
 
      // Create test results for each new tool
      for (const toolId of toolIds) {
        await apiRequest("POST", `/api/model-map/tests/${testId}/results`, {
          toolId,
          status: "pending",
        });
      }
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["/api/model-map/tests"] });
      setAddingModelsToTest(null);
      setSelectedModelsToAdd([]);
      setSelectedToolsToAdd([]);
      setSearchQuery("");
    },
  });
 
  const togglePromptExpanded = (testId: string) => {
    setExpandedPrompts((prev) => {
      const next = new Set(prev);
      if (next.has(testId)) {
        next.delete(testId);
      } else {
        next.add(testId);
      }
      return next;
    });
  };
 
  const handleResultClick = (test: PopulatedTest, result: PopulatedTestResult) => {
    setSelectedTest(test);
    setSelectedResult(result);
    setCompleteDialogOpen(true);
  };
 
  const getStatusBadge = (test: PopulatedTest) => {
    const results = test.results || [];
    const completed = results.filter((r) => r.status === "completed" || r.userRating);
    const pending = results.filter((r) => r.status === "pending" && !r.userRating);
 
    if (pending.length === results.length) {
      return <Badge variant="outline">Pending</Badge>;
    }
    if (completed.length === results.length) {
      return <Badge className="bg-green-100 text-green-700">Completed</Badge>;
    }
    return <Badge className="bg-amber-100 text-amber-700">In Progress</Badge>;
  };
 
  const getCategoryColor = (category?: string) => {
    const colors: Record<string, string> = {
      "Strategic Analysis": "bg-purple-100 text-purple-700",
      "Writing": "bg-blue-100 text-blue-700",
      "Code": "bg-green-100 text-green-700",
      "Research": "bg-amber-100 text-amber-700",
      "Automation": "bg-cyan-100 text-cyan-700",
      "Visual Design": "bg-pink-100 text-pink-700",
      "Audio/Video": "bg-red-100 text-red-700",
    };
    return colors[category || ""] || "bg-gray-100 text-gray-700";
  };
 
  return (
    <div className="container max-w-5xl mx-auto py-8 px-4">
      {/* Header */}
      <div className="flex items-center justify-between mb-8">
        <div>
          <h1 className="text-3xl font-bold mb-2 flex items-center gap-3">
            <Beaker className="h-8 w-8" />
            Test Lab
          </h1>
          <p className="text-muted-foreground">
            Run prompts against your models and log the results
          </p>
        </div>
        <Button onClick={() => setCreateDialogOpen(true)}>
          <Plus className="h-4 w-4 mr-2" />
          Start a New Test
        </Button>
      </div>
 
      {/* Test List */}
      {isLoading ? (
        <div className="text-center py-12 text-muted-foreground">
          Loading tests...
        </div>
      ) : tests.length === 0 ? (
        <Card className="py-12">
          <CardContent className="text-center">
            <Beaker className="h-12 w-12 mx-auto text-muted-foreground mb-4" />
            <h3 className="text-lg font-semibold mb-2">No tests yet</h3>
            <p className="text-muted-foreground mb-4">
              Create your first test to start comparing models
            </p>
            <Button onClick={() => setCreateDialogOpen(true)}>
              <Plus className="h-4 w-4 mr-2" />
              Start a New Test
            </Button>
          </CardContent>
        </Card>
      ) : (
        <div className="space-y-6">
          {tests.map((test) => (
            <Card key={test.id} className="overflow-hidden">
              <CardHeader className="pb-4">
                <div className="flex items-start justify-between">
                  <div className="flex items-center gap-3">
                    {test.useCase?.category && (
                      <Badge className={getCategoryColor(test.useCase.category)}>
                        {test.useCase.category}
                      </Badge>
                    )}
                    <div>
                      <h3 className="font-semibold text-lg">{test.title}</h3>
                      <p className="text-sm text-muted-foreground">
                        Created {new Date(test.createdAt).toLocaleDateString()} at{" "}
                        {new Date(test.createdAt).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}
                      </p>
                    </div>
                  </div>
                  <div className="flex items-center gap-2">
                    {getStatusBadge(test)}
                    <Button
                      variant="outline"
                      size="sm"
                      onClick={() => setAddingModelsToTest(test)}
                    >
                      <Plus className="h-4 w-4 mr-1" />
                      Test More Models
                    </Button>
                    <Button
                      variant="ghost"
                      size="icon"
                      className="text-muted-foreground hover:text-destructive"
                      onClick={() => setDeleteTestId(test.id)}
                    >
                      <Trash2 className="h-4 w-4" />
                    </Button>
                  </div>
                </div>
              </CardHeader>
 
              <CardContent className="space-y-4">
                {/* Model Result Cards */}
                <div className="flex flex-wrap gap-3">
                  {(test.results || []).map((result) => {
                    const item = result.model || result.tool;
                    const hasRating = result.userRating || result.accuracyRating;
                    return (
                      <div
                        key={result.id}
                        className="flex items-center gap-3 p-3 rounded-lg border bg-card hover:bg-muted/50 cursor-pointer transition-colors min-w-[200px]"
                        onClick={() => handleResultClick(test, result)}
                      >
                        <ModelAvatar
                          name={item?.name || "Unknown"}
                          shortName={item?.shortName}
                          provider={item?.provider || ""}
                          size="md"
                        />
                        <div className="flex-1 min-w-0">
                          <p className="font-medium text-sm truncate">
                            {item?.name || "Unknown"}
                          </p>
                          <p className="text-xs text-muted-foreground">
                            {item?.provider}
                          </p>
                          {!hasRating && (
                            <p className="text-xs text-primary mt-1">
                              Click to add results
                            </p>
                          )}
                          {hasRating && (
                            <div className="flex items-center gap-1 mt-1">
                              <span className="text-xs text-muted-foreground">
                                Rated: {result.accuracyRating || result.userRating}/5
                              </span>
                            </div>
                          )}
                        </div>
                      </div>
                    );
                  })}
                </div>
 
                {/* View Prompt Accordion */}
                <Collapsible
                  open={expandedPrompts.has(test.id)}
                  onOpenChange={() => togglePromptExpanded(test.id)}
                >
                  <CollapsibleTrigger asChild>
                    <Button variant="ghost" size="sm" className="gap-2 text-muted-foreground">
                      View Prompt
                      <ChevronDown
                        className={`h-4 w-4 transition-transform ${
                          expandedPrompts.has(test.id) ? "rotate-180" : ""
                        }`}
                      />
                    </Button>
                  </CollapsibleTrigger>
                  <CollapsibleContent className="mt-3">
                    <div className="bg-muted rounded-lg p-4">
                      {test.systemPrompt && (
                        <div className="mb-3">
                          <p className="text-xs font-medium text-muted-foreground mb-1">
                            System Prompt
                          </p>
                          <pre className="text-sm whitespace-pre-wrap">
                            {test.systemPrompt}
                          </pre>
                        </div>
                      )}
                      <div>
                        <p className="text-xs font-medium text-muted-foreground mb-1">
                          User Prompt
                        </p>
                        <pre className="text-sm whitespace-pre-wrap">{test.prompt}</pre>
                      </div>
                    </div>
                  </CollapsibleContent>
                </Collapsible>
              </CardContent>
            </Card>
          ))}
        </div>
      )}
 
      {/* Create Test Dialog */}
      <CreateTestDialog
        open={createDialogOpen}
        onOpenChange={setCreateDialogOpen}
        selectedUseCaseId={selectedUseCaseId}
        onSelectUseCaseChange={setSelectedUseCaseId}
      />
 
      {/* Complete Test Dialog */}
      <CompleteTestDialog
        open={completeDialogOpen}
        onOpenChange={setCompleteDialogOpen}
        test={selectedTest}
        result={selectedResult}
        onSaved={() => {
          queryClient.invalidateQueries({ queryKey: ["/api/model-map/tests"] });
        }}
      />
 
      {/* Add Models to Test Dialog */}
      <Dialog open={!!addingModelsToTest} onOpenChange={(open) => {
        if (!open) {
          setAddingModelsToTest(null);
          setSelectedModelsToAdd([]);
          setSelectedToolsToAdd([]);
          setSearchQuery("");
        }
      }}>
        <DialogContent className="max-w-lg max-h-[85vh] flex flex-col">
          <DialogHeader>
            <DialogTitle>Add More Models to Test</DialogTitle>
          </DialogHeader>
 
          {addingModelsToTest && (
            <div className="flex-1 overflow-y-auto space-y-4 py-4">
              {/* Test Info */}
              <div className="p-3 rounded-lg bg-muted/50">
                <p className="font-medium text-sm">{addingModelsToTest.title}</p>
                <p className="text-xs text-muted-foreground mt-1">
                  {addingModelsToTest.useCaseId ? "Linked to a use case" : "Custom test"}
                </p>
              </div>
 
              {/* Search */}
              <div className="relative">
                <Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
                <Input
                  placeholder="Search models or tools..."
                  value={searchQuery}
                  onChange={(e) => setSearchQuery(e.target.value)}
                  className="pl-10"
                />
              </div>
 
              {/* Models Section */}
              <div className="space-y-2">
                <p className="text-sm font-medium">Models</p>
                <div className="space-y-1 max-h-[200px] overflow-y-auto">
                  {allModels
                    .filter(m => !addingModelsToTest.results?.some(r => r.modelId === m.id))
                    .filter(m => !searchQuery || m.name.toLowerCase().includes(searchQuery.toLowerCase()))
                    .map((model) => (
                      <div key={model.id} className="flex items-center gap-2 p-2 hover:bg-muted/50 rounded">
                        <Checkbox
                          checked={selectedModelsToAdd.includes(model.id)}
                          onCheckedChange={(checked) => {
                            if (checked) {
                              setSelectedModelsToAdd([...selectedModelsToAdd, model.id]);
                            } else {
                              setSelectedModelsToAdd(selectedModelsToAdd.filter(id => id !== model.id));
                            }
                          }}
                        />
                        <ModelAvatar
                          name={model.name}
                          shortName={model.shortName}
                          provider={model.provider}
                          size="sm"
                        />
                        <div className="flex-1 min-w-0">
                          <p className="text-sm font-medium truncate">{model.name}</p>
                          <p className="text-xs text-muted-foreground">{model.provider}</p>
                        </div>
                      </div>
                    ))}
                </div>
              </div>
 
              {/* Tools Section */}
              <div className="space-y-2">
                <p className="text-sm font-medium">Tools</p>
                <div className="space-y-1 max-h-[200px] overflow-y-auto">
                  {allTools
                    .filter(t => !addingModelsToTest.results?.some(r => r.toolId === t.id))
                    .filter(t => !searchQuery || t.name.toLowerCase().includes(searchQuery.toLowerCase()))
                    .map((tool) => (
                      <div key={tool.id} className="flex items-center gap-2 p-2 hover:bg-muted/50 rounded">
                        <Checkbox
                          checked={selectedToolsToAdd.includes(tool.id)}
                          onCheckedChange={(checked) => {
                            if (checked) {
                              setSelectedToolsToAdd([...selectedToolsToAdd, tool.id]);
                            } else {
                              setSelectedToolsToAdd(selectedToolsToAdd.filter(id => id !== tool.id));
                            }
                          }}
                        />
                        <ModelAvatar
                          name={tool.name}
                          shortName={tool.shortName}
                          provider={tool.provider}
                          size="sm"
                        />
                        <div className="flex-1 min-w-0">
                          <p className="text-sm font-medium truncate">{tool.name}</p>
                          <p className="text-xs text-muted-foreground">{tool.provider}</p>
                        </div>
                      </div>
                    ))}
                </div>
              </div>
            </div>
          )}
 
          <DialogFooter>
            <Button variant="outline" onClick={() => setAddingModelsToTest(null)}>
              Cancel
            </Button>
            <Button
              onClick={() => {
                if (addingModelsToTest && (selectedModelsToAdd.length > 0 || selectedToolsToAdd.length > 0)) {
                  addModelsToTestMutation.mutate({
                    testId: addingModelsToTest.id,
                    modelIds: selectedModelsToAdd,
                    toolIds: selectedToolsToAdd,
                  });
                  setSelectedModelsToAdd([]);
                  setSelectedToolsToAdd([]);
                  setSearchQuery("");
                }
              }}
              disabled={selectedModelsToAdd.length === 0 && selectedToolsToAdd.length === 0}
            >
              <Plus className="h-4 w-4 mr-2" />
              Add Selected
            </Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>
 
      {/* Delete Confirmation Dialog */}
      <AlertDialog open={!!deleteTestId} onOpenChange={() => setDeleteTestId(null)}>
        <AlertDialogContent>
          <AlertDialogHeader>
            <AlertDialogTitle>Delete Test?</AlertDialogTitle>
            <AlertDialogDescription>
              This will permanently delete the test and all its results. This action cannot be undone.
            </AlertDialogDescription>
          </AlertDialogHeader>
          <AlertDialogFooter>
            <AlertDialogCancel>Cancel</AlertDialogCancel>
            <AlertDialogAction
              className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
              onClick={() => deleteTestId && deleteTestMutation.mutate(deleteTestId)}
            >
              Delete
            </AlertDialogAction>
          </AlertDialogFooter>
        </AlertDialogContent>
      </AlertDialog>
    </div>
  );
}