171 lines
8.0 KiB
TypeScript
171 lines
8.0 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import { LEAD_OUTCOME_COUNTS, OFFICE_ROLE_COUNTS, PROJECT_TYPE_COUNTS } from "./catalog";
|
|
import { generateDemoProSeed, validateDemoProSeedGraph } from "./generator";
|
|
import { DEMO_PRO_SEED_NAMESPACE } from "./types";
|
|
|
|
function frequencies(values: readonly string[]): Record<string, number> {
|
|
return values.reduce<Record<string, number>>((result, value) => {
|
|
result[value] = (result[value] ?? 0) + 1;
|
|
return result;
|
|
}, {});
|
|
}
|
|
|
|
describe("demo-pro Rhein-Main seed generator", () => {
|
|
it("generates the exact employee structure with realistic odd staffing and crews", () => {
|
|
const graph = generateDemoProSeed();
|
|
const officeEmployees = graph.employees.filter((employee) => employee.seatCategory === "office");
|
|
const fieldEmployees = graph.employees.filter((employee) => employee.seatCategory === "field");
|
|
|
|
expect(graph.employees).toHaveLength(50);
|
|
expect(officeEmployees).toHaveLength(17);
|
|
expect(fieldEmployees).toHaveLength(33);
|
|
expect(frequencies(officeEmployees.map((employee) => employee.role))).toEqual(
|
|
Object.fromEntries(OFFICE_ROLE_COUNTS.map(({ role, count }) => [role, count]))
|
|
);
|
|
expect(graph.crews).toHaveLength(16);
|
|
expect(frequencies(graph.crews.map((crew) => String(crew.memberEmployeeIds.length)))).toEqual({ "2": 15, "3": 1 });
|
|
expect(new Set(graph.crews.flatMap((crew) => crew.memberEmployeeIds)).size).toBe(33);
|
|
expect(fieldEmployees.every((employee) => graph.crews.some((crew) => crew.id === employee.crewId && crew.memberEmployeeIds.includes(employee.id)))).toBe(
|
|
true
|
|
);
|
|
});
|
|
|
|
it("keeps every globally persisted v1 employee identity stable across staffing changes", () => {
|
|
const employees = generateDemoProSeed().employees;
|
|
const serializedIdentities = employees.map(({ id, firstName, lastName, email }) => [id, firstName, lastName, email].join("|")).join("\n");
|
|
|
|
expect(createHash("sha256").update(serializedIdentities).digest("hex")).toBe("507aaa29467a9b830d447552af41071aa0e1bd109257c9c41dead3e7eccb5877");
|
|
expect(employees.slice(16, 21).map(({ id, firstName, lastName, email, seatCategory }) => ({ id, firstName, lastName, email, seatCategory }))).toEqual([
|
|
{
|
|
id: "rheinmain-shk-v1-employee-0017",
|
|
firstName: "Bianca",
|
|
lastName: "Schmitz",
|
|
email: "bianca.schmitz.17@mitarbeiter.example.test",
|
|
seatCategory: "office",
|
|
},
|
|
{
|
|
id: "rheinmain-shk-v1-employee-0018",
|
|
firstName: "Dennis",
|
|
lastName: "Köhler",
|
|
email: "dennis.kohler.18@mitarbeiter.example.test",
|
|
seatCategory: "field",
|
|
},
|
|
{
|
|
id: "rheinmain-shk-v1-employee-0019",
|
|
firstName: "Heike",
|
|
lastName: "Hoffmann",
|
|
email: "heike.hoffmann.19@mitarbeiter.example.test",
|
|
seatCategory: "field",
|
|
},
|
|
{
|
|
id: "rheinmain-shk-v1-employee-0020",
|
|
firstName: "Oliver",
|
|
lastName: "Schröder",
|
|
email: "oliver.schroder.20@mitarbeiter.example.test",
|
|
seatCategory: "field",
|
|
},
|
|
{
|
|
id: "rheinmain-shk-v1-employee-0021",
|
|
firstName: "Miriam",
|
|
lastName: "Wagner",
|
|
email: "miriam.wagner.21@mitarbeiter.example.test",
|
|
seatCategory: "field",
|
|
},
|
|
]);
|
|
});
|
|
|
|
it("builds the full realistic contact and project volume", () => {
|
|
const graph = generateDemoProSeed();
|
|
|
|
expect(graph.organizations).toHaveLength(247);
|
|
expect(graph.persons).toHaveLength(536);
|
|
expect(graph.contactRelations).toHaveLength(421);
|
|
expect(graph.projects).toHaveLength(537);
|
|
expect(frequencies(graph.projects.map((project) => project.projectType))).toEqual(PROJECT_TYPE_COUNTS);
|
|
expect(new Set(graph.organizations.map((organization) => organization.branche)).size).toBeGreaterThanOrEqual(12);
|
|
expect(new Set(graph.persons.map((person) => `${person.firstName} ${person.lastName}`)).size).toBeGreaterThan(100);
|
|
expect(graph.projects.every((project) => project.description.length > 100 && project.contacts.length >= 3)).toBe(true);
|
|
expect(
|
|
graph.organizations.every((organization) => {
|
|
const locationToken = organization.address.ort.replace(" am Main", "").replace(" vor der Höhe", "");
|
|
return organization.orgName.includes(locationToken);
|
|
})
|
|
).toBe(true);
|
|
});
|
|
|
|
it("uses the requested exact lead outcomes and complete conversion links", () => {
|
|
const graph = generateDemoProSeed();
|
|
const converted = graph.leads.filter((lead) => lead.outcome === "converted");
|
|
|
|
expect(graph.leads).toHaveLength(193);
|
|
expect(frequencies(graph.leads.map((lead) => lead.outcome))).toEqual(LEAD_OUTCOME_COUNTS);
|
|
expect(converted).toHaveLength(116);
|
|
expect(converted.every((lead) => lead.linkedContactId && lead.convertedProjectId && lead.convertedAt)).toBe(true);
|
|
expect(graph.projects.filter((project) => project.convertedFromLeadId)).toHaveLength(116);
|
|
expect(
|
|
converted.every((lead) => {
|
|
const project = graph.projects.find(({ id }) => id === lead.convertedProjectId)!;
|
|
return new Date(lead.createdAt) < new Date(lead.convertedAt!) && new Date(lead.convertedAt!) <= new Date(project.createdAt);
|
|
})
|
|
).toBe(true);
|
|
expect(graph.leads.every((lead) => !lead.lostAt || new Date(lead.lostAt) <= new Date(graph.referenceDate))).toBe(true);
|
|
});
|
|
|
|
it("keeps every project lifecycle chronologically plausible", () => {
|
|
const graph = generateDemoProSeed();
|
|
const referenceDate = new Date(graph.referenceDate);
|
|
|
|
expect(graph.projects.every((project) => !project.startDate || new Date(project.createdAt) < new Date(project.startDate))).toBe(true);
|
|
expect(graph.projects.every((project) => new Date(project.createdAt) < new Date(project.deadline))).toBe(true);
|
|
expect(graph.projects.every((project) => !project.startDate || new Date(project.startDate) < new Date(project.deadline))).toBe(true);
|
|
expect(
|
|
graph.projects.every((project) => !project.wonAt || (new Date(project.createdAt) < new Date(project.wonAt) && new Date(project.wonAt) <= referenceDate))
|
|
).toBe(true);
|
|
expect(
|
|
graph.projects.every(
|
|
(project) => !project.lostAt || (new Date(project.createdAt) < new Date(project.lostAt) && new Date(project.lostAt) <= referenceDate)
|
|
)
|
|
).toBe(true);
|
|
});
|
|
|
|
it("keeps every external contact channel non-routable and every ID namespaced", () => {
|
|
const graph = generateDemoProSeed();
|
|
const ids = [
|
|
...graph.employees.map(({ id }) => id),
|
|
...graph.crews.map(({ id }) => id),
|
|
...graph.organizations.map(({ id }) => id),
|
|
...graph.persons.map(({ id }) => id),
|
|
...graph.contactRelations.map(({ id }) => id),
|
|
...graph.projects.map(({ id }) => id),
|
|
...graph.leads.map(({ id }) => id),
|
|
];
|
|
const emails = [
|
|
...graph.employees.map(({ email }) => email),
|
|
...graph.organizations.flatMap((organization) => [organization.email, ...organization.additionalEmails.map(({ value }) => value)]),
|
|
...graph.persons.flatMap((person) => [person.email, ...person.additionalEmails.map(({ value }) => value)]),
|
|
...graph.leads.map(({ email }) => email),
|
|
];
|
|
const phones = [
|
|
...graph.employees.map(({ phone }) => phone),
|
|
...graph.organizations.flatMap((organization) => [organization.phone, ...organization.additionalPhones.map(({ value }) => value)]),
|
|
...graph.persons.flatMap((person) => [person.phone, ...person.additionalPhones.map(({ value }) => value)]),
|
|
...graph.leads.map(({ phone }) => phone),
|
|
];
|
|
|
|
expect(ids.every((id) => id.startsWith(`${DEMO_PRO_SEED_NAMESPACE}-`))).toBe(true);
|
|
expect(new Set(ids).size).toBe(ids.length);
|
|
expect(emails.every((email) => email.endsWith(".example.test"))).toBe(true);
|
|
expect(phones.every((phone) => /^\+496990009\d{3}$/.test(phone))).toBe(true);
|
|
});
|
|
|
|
it("is byte-for-byte deterministic and passes its integrity validator", () => {
|
|
const first = generateDemoProSeed();
|
|
const second = generateDemoProSeed();
|
|
|
|
expect(second).toEqual(first);
|
|
expect(validateDemoProSeedGraph(first)).toEqual({ valid: true, errors: [] });
|
|
});
|
|
});
|