65 lines
2.7 KiB
TypeScript
65 lines
2.7 KiB
TypeScript
import { readFileSync } from "node:fs";
|
||
import { resolve } from "node:path";
|
||
import { describe, expect, it } from "vitest";
|
||
import { calculateInvoiceQualityScore, withCalculatedInvoiceQuality } from "@shared/invoiceQuality";
|
||
|
||
const routerSource = readFileSync(resolve(process.cwd(), "server/routers.ts"), "utf8");
|
||
const emailImportSource = readFileSync(resolve(process.cwd(), "server/emailImportService.ts"), "utf8");
|
||
const folderImportSource = readFileSync(resolve(process.cwd(), "server/folderImportService.ts"), "utf8");
|
||
const webImportSource = readFileSync(resolve(process.cwd(), "server/_core/index.ts"), "utf8");
|
||
|
||
describe("score de qualité à l'import", () => {
|
||
it("calcule 100 % pour la facture lorsque tous les champs obligatoires configurés sont présents", () => {
|
||
const qualityScore = calculateInvoiceQualityScore({
|
||
supplierName: "Manus Butterfly",
|
||
invoiceNumber: "J6LNGDAL0021",
|
||
invoiceDate: new Date("2026-08-28"),
|
||
totalAmount: "120.00",
|
||
recipientName: "PAREIGE OLIVIER",
|
||
}, [
|
||
{ fieldName: "supplierName", isRequired: 1 },
|
||
{ fieldName: "invoiceNumber", isRequired: 1 },
|
||
{ fieldName: "invoiceDate", isRequired: 1 },
|
||
{ fieldName: "totalAmount", isRequired: 1 },
|
||
{ fieldName: "recipientName", isRequired: 1 },
|
||
{ fieldName: "orderNumber", isRequired: 0 },
|
||
]);
|
||
|
||
expect(qualityScore).toBe(100);
|
||
});
|
||
|
||
it("utilise le calcul serveur déterministe au lieu du score proposé par le modèle", () => {
|
||
expect(routerSource).toContain("withCalculatedInvoiceQuality(invoiceData, qualityFieldsConfig)");
|
||
expect(routerSource).toContain("qualityScore: invoiceWithQuality.qualityScore");
|
||
});
|
||
|
||
it("exécute le même recalcul pour chaque flux d’import", () => {
|
||
const requiredFields = [
|
||
{ fieldName: "supplierName", isRequired: 1 },
|
||
{ fieldName: "invoiceNumber", isRequired: 1 },
|
||
{ fieldName: "invoiceDate", isRequired: 1 },
|
||
{ fieldName: "totalAmount", isRequired: 1 },
|
||
{ fieldName: "recipientName", isRequired: 1 },
|
||
];
|
||
|
||
for (const rawModelScore of [0, 42, 90, 99]) {
|
||
const persistedInvoice = withCalculatedInvoiceQuality({
|
||
supplierName: "Manus Butterfly",
|
||
invoiceNumber: "J6LNGDAL0021",
|
||
invoiceDate: new Date("2026-08-28"),
|
||
totalAmount: "120.00",
|
||
recipientName: "PAREIGE OLIVIER",
|
||
qualityScore: rawModelScore,
|
||
}, requiredFields);
|
||
expect(persistedInvoice.qualityScore).toBe(100);
|
||
}
|
||
});
|
||
|
||
it("branche chaque flux sur le calcul partagé", () => {
|
||
for (const source of [routerSource, emailImportSource, folderImportSource, webImportSource]) {
|
||
expect(source).toContain("withCalculatedInvoiceQuality(");
|
||
expect(source).toContain("qualityFieldsConfig");
|
||
}
|
||
});
|
||
});
|