forked from Yara724/api
244 lines
7.5 KiB
TypeScript
244 lines
7.5 KiB
TypeScript
import { AiAssessmentError, 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", () => {
|
|
const assessment = () => ({
|
|
request_id: "assessment-123",
|
|
pipeline: { version: "v1", vlm_model: "model", prompt_version: "p1" },
|
|
vehicle: { color: "خاکستری", body_type: "sedan" },
|
|
damaged_parts: [
|
|
{
|
|
part: "Mirror",
|
|
side: "left",
|
|
severity: "minor",
|
|
localized: true,
|
|
damage_types: ["scratch"],
|
|
descriptions: ["خراش روی آینه"],
|
|
evidence: [
|
|
{
|
|
image_index: 0,
|
|
filename: "mirror.jpg",
|
|
view: "front",
|
|
severity: "minor",
|
|
link_status: "linked",
|
|
part: {
|
|
instance_id: 1,
|
|
confidence: 0.9,
|
|
box: { x1: 10, y1: 20, x2: 40, y2: 80 },
|
|
polygon: [
|
|
[10, 20],
|
|
[40, 20],
|
|
[40, 80],
|
|
],
|
|
},
|
|
},
|
|
],
|
|
},
|
|
],
|
|
internal_damages: [
|
|
{
|
|
category: "radiator",
|
|
label_fa: "رادیاتور",
|
|
likelihood: "medium",
|
|
reason: "نیاز به بررسی",
|
|
},
|
|
],
|
|
case_report: "گزارش ارزیابی",
|
|
});
|
|
|
|
it("normalizes assessments, retaining evidence, damage descriptions and provenance", () => {
|
|
const result = normalizeAiResult(assessment());
|
|
expect(result).toMatchObject({
|
|
requestId: "assessment-123",
|
|
pipeline: { version: "v1" },
|
|
vehicle: { color: "خاکستری", bodyType: "sedan" },
|
|
internalDamages: [
|
|
{ labelFa: "رادیاتور", likelihood: "medium", reason: "نیاز به بررسی" },
|
|
],
|
|
caseReport: "گزارش ارزیابی",
|
|
parts: [
|
|
{
|
|
name: "Mirror",
|
|
side: "left",
|
|
severity: "minor",
|
|
localized: true,
|
|
damageTypes: ["scratch"],
|
|
descriptions: ["خراش روی آینه"],
|
|
confidence: 0.9,
|
|
bbox: { x1: 10, y1: 20, x2: 40, y2: 80 },
|
|
evidence: [
|
|
{
|
|
imageIndex: 0,
|
|
instanceId: 1,
|
|
polygon: [
|
|
[10, 20],
|
|
[40, 20],
|
|
[40, 80],
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
});
|
|
});
|
|
|
|
it("keeps multiple evidence boxes for a part without drawing evidence from another photo", () => {
|
|
const value = assessment();
|
|
const evidence = value.damaged_parts[0].evidence;
|
|
evidence.push({
|
|
...evidence[0],
|
|
part: { ...evidence[0].part, instance_id: 2 },
|
|
});
|
|
evidence.push({ ...evidence[0], image_index: 1 });
|
|
const result = normalizeAiResult(value);
|
|
expect(result.parts).toHaveLength(1);
|
|
expect(result.parts[0].evidence).toHaveLength(2);
|
|
expect(
|
|
result.parts[0].evidence.every((item) => item.imageIndex === 0),
|
|
).toBe(true);
|
|
});
|
|
|
|
it("does not invent boxes for unlocalized, unlinked or invalid evidence", () => {
|
|
const value = assessment();
|
|
value.damaged_parts[0].localized = false;
|
|
expect(normalizeAiResult(value).parts[0].bbox).toBeNull();
|
|
value.damaged_parts[0].localized = true;
|
|
value.damaged_parts[0].evidence[0].link_status = "unlinked";
|
|
expect(normalizeAiResult(value).parts[0].bbox).toBeNull();
|
|
value.damaged_parts[0].evidence[0].link_status = "linked";
|
|
value.damaged_parts[0].evidence[0].part.box.x2 = NaN;
|
|
value.damaged_parts[0].evidence[0].part.polygon = [[null, null]];
|
|
expect(normalizeAiResult(value).parts[0].evidence[0]).toMatchObject({
|
|
bbox: null,
|
|
polygon: null,
|
|
});
|
|
});
|
|
|
|
it("accepts zero detections and parts without evidence", () => {
|
|
const value = assessment();
|
|
value.damaged_parts[0].evidence = [];
|
|
expect(normalizeAiResult(value).parts[0]).toMatchObject({
|
|
bbox: null,
|
|
evidence: [],
|
|
});
|
|
value.damaged_parts = [];
|
|
expect(normalizeAiResult(value).parts).toEqual([]);
|
|
});
|
|
|
|
it("rejects error envelopes even with an HTTP-success response, preserving code and request id", () => {
|
|
try {
|
|
normalizeAiResult({
|
|
error: {
|
|
code: "INVALID_IMAGE",
|
|
message: "Invalid image",
|
|
request_id: "r-1",
|
|
},
|
|
});
|
|
throw new Error("Expected assessment failure");
|
|
} catch (error) {
|
|
expect(error).toBeInstanceOf(AiAssessmentError);
|
|
expect(error).toMatchObject({
|
|
code: "INVALID_IMAGE",
|
|
requestId: "r-1",
|
|
message: "Invalid image",
|
|
});
|
|
}
|
|
expect(() =>
|
|
normalizeAiResult({ damaged_parts: [], case_report: null }),
|
|
).toThrow();
|
|
expect(() =>
|
|
normalizeAiResult({ ...assessment(), internal_damages: {} }),
|
|
).toThrow();
|
|
});
|
|
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);
|
|
});
|
|
});
|