chore(repo): initialize planarchy workspace
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import type { inferProcedureInput } from "@trpc/server";
|
||||
import type { AppRouter } from "../router/index.js";
|
||||
|
||||
// Minimal mock helpers
|
||||
function mockCtx(overrides: Record<string, unknown> = {}) {
|
||||
return {
|
||||
ctx: {
|
||||
session: { user: { id: "user_1", systemRole: "MANAGER" } },
|
||||
db: overrides,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("rateCard router", () => {
|
||||
describe("list", () => {
|
||||
it("returns rate cards with line counts", async () => {
|
||||
const findMany = vi.fn().mockResolvedValue([
|
||||
{ id: "rc_1", name: "Standard 2026", currency: "EUR", isActive: true, _count: { lines: 5 } },
|
||||
{ id: "rc_2", name: "India Rates", currency: "INR", isActive: true, _count: { lines: 3 } },
|
||||
]);
|
||||
|
||||
const result = await findMany({
|
||||
where: {},
|
||||
include: { _count: { select: { lines: true } } },
|
||||
orderBy: [{ isActive: "desc" }, { effectiveFrom: "desc" }, { name: "asc" }],
|
||||
});
|
||||
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result[0]._count.lines).toBe(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe("create", () => {
|
||||
it("creates a rate card with lines", async () => {
|
||||
const create = vi.fn().mockResolvedValue({
|
||||
id: "rc_new",
|
||||
name: "Q1 2026 Rates",
|
||||
currency: "EUR",
|
||||
isActive: true,
|
||||
lines: [
|
||||
{ id: "rcl_1", costRateCents: 9500, billRateCents: 14000, chapter: "Digital Content Production" },
|
||||
],
|
||||
});
|
||||
|
||||
const result = await create({
|
||||
data: {
|
||||
name: "Q1 2026 Rates",
|
||||
currency: "EUR",
|
||||
lines: {
|
||||
create: [{ costRateCents: 9500, billRateCents: 14000, chapter: "Digital Content Production" }],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(result.id).toBe("rc_new");
|
||||
expect(result.lines).toHaveLength(1);
|
||||
expect(result.lines[0].costRateCents).toBe(9500);
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveRate", () => {
|
||||
it("returns the most specific matching line", () => {
|
||||
const lines = [
|
||||
{ id: "rcl_1", roleId: null, chapter: "Digital Content Production", costRateCents: 7000, billRateCents: 12000 },
|
||||
{ id: "rcl_2", roleId: "role_3d", chapter: "Digital Content Production", costRateCents: 9500, billRateCents: 14000 },
|
||||
{ id: "rcl_3", roleId: null, chapter: null, costRateCents: 6000, billRateCents: 10000 },
|
||||
];
|
||||
|
||||
const criteria = { roleId: "role_3d", chapter: "Digital Content Production" };
|
||||
|
||||
const scored = lines.map((line) => {
|
||||
let score = 0;
|
||||
let mismatch = false;
|
||||
if (criteria.roleId && line.roleId) {
|
||||
if (line.roleId === criteria.roleId) score += 4;
|
||||
else mismatch = true;
|
||||
}
|
||||
if (criteria.chapter && line.chapter) {
|
||||
if (line.chapter === criteria.chapter) score += 2;
|
||||
else mismatch = true;
|
||||
}
|
||||
return { line, score, mismatch };
|
||||
});
|
||||
|
||||
const candidates = scored
|
||||
.filter((s) => !s.mismatch)
|
||||
.sort((a, b) => b.score - a.score);
|
||||
|
||||
const best = candidates[0];
|
||||
const result = best ? best.line : null;
|
||||
|
||||
// Most specific match: role + chapter = score 6
|
||||
expect(result?.id).toBe("rcl_2");
|
||||
expect(result?.costRateCents).toBe(9500);
|
||||
});
|
||||
|
||||
it("returns null when no lines match", () => {
|
||||
const lines = [
|
||||
{ id: "rcl_1", roleId: "role_pm", chapter: "Project Management", costRateCents: 7000 },
|
||||
];
|
||||
|
||||
const criteria = { roleId: "role_3d", chapter: "Digital Content Production" };
|
||||
|
||||
const scored = lines.map((line) => {
|
||||
let score = 0;
|
||||
let mismatch = false;
|
||||
if (criteria.roleId && line.roleId) {
|
||||
if (line.roleId === criteria.roleId) score += 4;
|
||||
else mismatch = true;
|
||||
}
|
||||
if (criteria.chapter && line.chapter) {
|
||||
if (line.chapter === criteria.chapter) score += 2;
|
||||
else mismatch = true;
|
||||
}
|
||||
return { line, score, mismatch };
|
||||
});
|
||||
|
||||
const candidates = scored.filter((s) => !s.mismatch);
|
||||
const best = candidates[0];
|
||||
expect(best).toBeUndefined();
|
||||
});
|
||||
|
||||
it("falls back to generic line when specific criteria don't match", () => {
|
||||
const lines = [
|
||||
{ id: "rcl_1", roleId: null, chapter: null, costRateCents: 6000 },
|
||||
{ id: "rcl_2", roleId: "role_pm", chapter: "Project Management", costRateCents: 8000 },
|
||||
];
|
||||
|
||||
const criteria = { roleId: "role_3d", chapter: "Digital Content Production" };
|
||||
|
||||
const scored = lines.map((line) => {
|
||||
let score = 0;
|
||||
let mismatch = false;
|
||||
if (criteria.roleId && line.roleId) {
|
||||
if (line.roleId === criteria.roleId) score += 4;
|
||||
else mismatch = true;
|
||||
}
|
||||
if (criteria.chapter && line.chapter) {
|
||||
if (line.chapter === criteria.chapter) score += 2;
|
||||
else mismatch = true;
|
||||
}
|
||||
return { line, score, mismatch };
|
||||
});
|
||||
|
||||
const candidates = scored
|
||||
.filter((s) => !s.mismatch)
|
||||
.sort((a, b) => b.score - a.score);
|
||||
|
||||
const best = candidates[0];
|
||||
const result = best ? best.line : null;
|
||||
|
||||
// Generic line (no criteria set) should match as fallback
|
||||
expect(result?.id).toBe("rcl_1");
|
||||
expect(result?.costRateCents).toBe(6000);
|
||||
});
|
||||
});
|
||||
|
||||
describe("replaceLines", () => {
|
||||
it("deletes all lines and creates new ones in a transaction", async () => {
|
||||
const deleteMany = vi.fn().mockResolvedValue({ count: 3 });
|
||||
const createLine = vi.fn()
|
||||
.mockResolvedValueOnce({ id: "rcl_new_1", costRateCents: 8000 })
|
||||
.mockResolvedValueOnce({ id: "rcl_new_2", costRateCents: 9500 });
|
||||
|
||||
await deleteMany({ where: { rateCardId: "rc_1" } });
|
||||
const line1 = await createLine({ data: { rateCardId: "rc_1", costRateCents: 8000 } });
|
||||
const line2 = await createLine({ data: { rateCardId: "rc_1", costRateCents: 9500 } });
|
||||
|
||||
expect(deleteMany).toHaveBeenCalledWith({ where: { rateCardId: "rc_1" } });
|
||||
expect(line1.id).toBe("rcl_new_1");
|
||||
expect(line2.id).toBe("rcl_new_2");
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user