refactor(api): extract rate card match support

This commit is contained in:
2026-03-31 14:33:38 +02:00
parent aeffb2a069
commit b093a47c1b
3 changed files with 195 additions and 71 deletions
@@ -0,0 +1,76 @@
import { describe, expect, it } from "vitest";
import {
pickBestRateCardLineMatch,
scoreRateCardLineMatch,
} from "../router/rate-card-match-support.js";
describe("rate card match support", () => {
it("scores weighted matches with optional case-insensitive keys", () => {
expect(scoreRateCardLineMatch({
roleId: "role_1",
chapter: "Delivery",
seniority: "Senior",
}, {
criteria: {
roleId: "role_1",
chapter: "delivery",
seniority: "senior",
},
weights: {
roleId: 4,
chapter: 2,
seniority: 1,
},
caseInsensitiveKeys: ["chapter", "seniority"],
})).toEqual({
line: {
roleId: "role_1",
chapter: "Delivery",
seniority: "Senior",
},
score: 7,
mismatch: false,
});
});
it("marks mismatches when a populated criterion conflicts with a populated line field", () => {
expect(scoreRateCardLineMatch({
roleId: "role_pm",
chapter: "Project Management",
}, {
criteria: {
roleId: "role_3d",
chapter: "Project Management",
},
weights: {
roleId: 4,
chapter: 2,
},
})).toEqual({
line: {
roleId: "role_pm",
chapter: "Project Management",
},
score: 2,
mismatch: true,
});
});
it("picks the highest-scoring non-mismatching line", () => {
const lines = [
{ id: "generic", roleId: null, chapter: null, location: null, seniority: null, workType: null },
{ id: "specific", roleId: "role_3d", chapter: "Delivery", location: null, seniority: null, workType: null },
];
expect(pickBestRateCardLineMatch(lines, {
criteria: {
roleId: "role_3d",
chapter: "Delivery",
},
weights: {
roleId: 4,
chapter: 2,
},
})).toEqual(lines[1]);
});
});