Changed AI to new version

This commit is contained in:
SepehrYahyaee
2026-11-03 15:58:23 +03:30
parent c5aa0f925e
commit e75a144e50
11 changed files with 4349 additions and 61 deletions
+63 -14
View File
@@ -1,6 +1,7 @@
import { ConfigService } from "@nestjs/config";
import { AiService, aiImageKey } from "./ai.service";
import { AiJobSchema } from "./ai-job.schema";
import { AiAssessmentError } from "./ai-result";
const image = { path: "files/part.jpg", label: "گلگیر" };
const result = { parts: [], report: "گزارش", caseReport: "گزارش کامل" };
@@ -129,7 +130,7 @@ describe("durable AI jobs", () => {
);
}
});
it("does not retry invalid/non-car images automatically", async () => {
it("does not retry rejected requests automatically or assume every 400 means no car", async () => {
const { service, jobs } = setup();
claimJob(jobs);
jest
@@ -141,7 +142,32 @@ describe("durable AI jobs", () => {
expect.objectContaining({
$set: expect.objectContaining({
status: "failed",
error: "خودرو در تصویر تشخیص داده نشد.",
error: "سرویس هوش مصنوعی درخواست تحلیل این تصویر را نپذیرفت.",
}),
}),
);
});
it("persists provider error tracking metadata and uses the HTTP status for retry decisions", async () => {
const { service, jobs } = setup();
claimJob(jobs);
jest
.spyOn(service, "aiRequestImage")
.mockRejectedValue(
new AiAssessmentError(
"INVALID_INPUT",
"req-123",
422,
"Unusable photo",
),
);
await service.work();
expect(jobs.updateOne).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
$set: expect.objectContaining({
status: "failed",
errorCode: "INVALID_INPUT",
errorRequestId: "req-123",
}),
}),
);
@@ -195,7 +221,7 @@ describe("durable AI jobs", () => {
});
describe("AI multipart transport", () => {
it("posts real image bytes under images with every pipeline flag and no returned-image dependency", async () => {
it("posts real image bytes to assessments with no retired pipeline flags", async () => {
const fs = await import("node:fs/promises");
const { resolve, join } = await import("node:path");
const axios = (await import("axios")).default;
@@ -214,15 +240,8 @@ describe("AI multipart transport", () => {
.spyOn(axios, "post")
.mockImplementation(async (url, form: any, config) => {
const parsed = new URL(String(url));
expect(parsed.pathname).toBe("/pipeline/detect_parts_with_damages");
for (const option of [
"side",
"segments",
"bounding_box",
"severity",
"confidence",
])
expect(parsed.searchParams.get(option)).toBe("true");
expect(parsed.pathname).toBe("/api/v1/assessments");
expect(parsed.search).toBe("");
expect(config?.headers?.["content-type"]).toMatch(
/^multipart\/form-data; boundary=/,
);
@@ -243,7 +262,9 @@ describe("AI multipart transport", () => {
expect(body.includes(bytes)).toBe(true);
return {
data: {
distinct_damaged_parts_report: { parts: [], report: "گزارش" },
request_id: "req-123",
damaged_parts: [],
internal_damages: [],
case_report: "گزارش کامل",
},
} as any;
@@ -252,11 +273,39 @@ describe("AI multipart transport", () => {
const { service } = setup();
await expect(
service.aiRequestImage({ path: file, fileName: "test.jpg" }),
).resolves.toMatchObject({ parts: [], report: "گزارش" });
).resolves.toMatchObject({ parts: [], requestId: "req-123" });
await expect(
service.aiRequestImage({ path: "/etc/hosts" }),
).rejects.toThrow("outside upload storage");
expect(post).toHaveBeenCalledTimes(1);
await fs.truncate(file, 15 * 1024 * 1024 + 1);
await expect(service.aiRequestImage({ path: file })).rejects.toThrow(
"Invalid image size",
);
expect(post).toHaveBeenCalledTimes(1);
await fs.writeFile(file, bytes);
const { AxiosError } = await import("axios");
post.mockRejectedValueOnce(
new AxiosError("Rejected", undefined, undefined, undefined, {
status: 422,
data: {
error: {
code: "INVALID_INPUT",
message: "Invalid image",
request_id: "req-error",
},
},
} as any),
);
await expect(
service.aiRequestImage({ path: file }),
).rejects.toMatchObject({
code: "INVALID_INPUT",
requestId: "req-error",
httpStatus: 422,
message: "Invalid image",
});
} finally {
pathSpy.mockRestore();
post.mockRestore();