Files
yara724api/src/ai/ai-result.spec.ts
T
2026-10-03 16:38:48 +03:30

96 lines
2.9 KiB
TypeScript

import { normalizeAiResult } from "./ai-result";
import { currentAiCaptures } from "./ai-captures";
const response = () => ({
distinct_damaged_parts_report: {
parts: [
{
part_name: "Headlight",
damage_severity: "Severe",
part_bbox: { x1: 10.5, y1: 20, x2: 40, y2: 80 },
confidence: 0.95,
side: "Right",
car_side: "front_right",
},
{
part_name: "Headlight",
damage_severity: null,
part_bbox: null,
confidence: null,
},
],
report: "گزارش تصویر",
},
damaged_part_table: [{ name: "Headlight" }],
case_report: "گزارش کامل",
});
describe("AI response contract", () => {
it("preserves distinct repeated detections, nullable fields and Persian reports without requiring a returned image", () => {
const result = normalizeAiResult(response());
expect(result.parts).toHaveLength(2);
expect(result.parts[0]).toMatchObject({
name: "Headlight",
severity: "Severe",
confidence: 0.95,
bbox: { x1: 10.5, y1: 20, x2: 40, y2: 80 },
});
expect(result.parts[1]).toMatchObject({
name: "Headlight",
bbox: null,
severity: null,
confidence: null,
});
expect(result).toMatchObject({
report: "گزارش تصویر",
caseReport: "گزارش کامل",
});
});
it("does not turn corrupt or failed responses into a successful empty analysis", () => {
for (const value of [
null,
{},
{ detail: "failure" },
{ ...response(), case_report: null },
])
expect(() => normalizeAiResult(value)).toThrow();
});
it("accepts a legitimate zero-detection response", () => {
const value = response();
value.distinct_damaged_parts_report.parts = [];
expect(normalizeAiResult(value).parts).toEqual([]);
});
it("omits corrupt boxes and out-of-range confidence without discarding the part", () => {
const value = response();
value.distinct_damaged_parts_report.parts[0].part_bbox.x2 = NaN;
value.distinct_damaged_parts_report.parts[0].confidence = 1.2;
expect(normalizeAiResult(value).parts[0]).toMatchObject({
name: "Headlight",
bbox: null,
confidence: null,
});
});
it("lists current photos only, deduplicates identical files and excludes history and unphotographed additions", () => {
const images = currentAiCaptures({
media: {
damagedParts: [
{ path: "files/new.jpg", label_fa: "گلگیر" },
{ path: "files/new.jpg" },
{ name: "door" },
],
},
damage: {
partSelectionHistory: [{ removedParts: [{ path: "files/old.jpg" }] }],
},
});
expect(images).toEqual([
{ path: "files/new.jpg", fileName: undefined, label: "گلگیر" },
]);
expect(
currentAiCaptures({
media: { damagedParts: { door: { path: "files/door.jpg" } } },
}),
).toHaveLength(1);
});
});