import { describe, expect, it } from "vitest"; import { generateDemoProSeed } from "./generator"; import { buildSeedFileObjectMetadata, seedFileStorageKey } from "./file-objects"; import { generateOperationalGraph, OPERATIONAL_COUNTS } from "./operations"; describe("demo-pro operational graph", () => { it("generates all agreed connected entity volumes", () => { const operational = generateOperationalGraph(generateDemoProSeed()); for (const [key, expected] of Object.entries(OPERATIONAL_COUNTS)) { expect(operational[key as keyof typeof operational]).toHaveLength(expected); } }); it("keeps task assignment and schedule data internally consistent", () => { const graph = generateDemoProSeed(); const operational = generateOperationalGraph(graph); expect(operational.tasks.every((task) => Number(Boolean(task.assignedToUserId)) + Number(Boolean(task.assignedToGroupId)) === 1)).toBe(true); expect( operational.tasks.every( (task) => new Date(task.createdAt) <= new Date(task.updatedAt) && new Date(task.updatedAt) <= new Date(graph.referenceDate) && (task.completedAt === null || (new Date(task.createdAt) <= new Date(task.completedAt) && new Date(task.completedAt) <= new Date(graph.referenceDate))) ) ).toBe(true); expect(operational.scheduleEntries.every((entry) => new Date(entry.startAt) < new Date(entry.endAt))).toBe(true); const projects = new Map(graph.projects.map((project) => [project.id, project])); expect( operational.scheduleEntries.every((entry) => { const project = projects.get(entry.projectId)!; return Boolean(project.startDate) && new Date(entry.startAt) >= new Date(project.startDate!) && new Date(entry.endAt) <= new Date(project.deadline); }) ).toBe(true); expect( operational.scheduleConflicts.every((conflict) => { const entry = operational.scheduleEntries.find(({ id }) => id === conflict.entryId)!; const source = operational.scheduleEntries.find(({ id }) => id === conflict.sourceEntryId)!; return ( entry.assignedUserId === source.assignedUserId && new Date(entry.startAt) < new Date(source.endAt) && new Date(source.startAt) < new Date(entry.endAt) ); }) ).toBe(true); expect( operational.scheduleEntries.every((entry) => { if (entry.calendarEventId === null) return entry.syncStatus === "pending"; const event = operational.calendarEvents.find(({ id }) => id === entry.calendarEventId); return ( entry.syncStatus === "synced" && event?.projectId === entry.projectId && event.userId === entry.assignedUserId && event.startsAt === entry.startAt && event.endsAt === entry.endAt ); }) ).toBe(true); expect(new Set(operational.files.map((file) => seedFileStorageKey("tenant-staging", file.id))).size).toBe(operational.files.length); expect( operational.files.every( (file) => seedFileStorageKey("tenant-staging", file.id).startsWith("tenants/tenant-staging/files/") && file.mimeType === "application/pdf" && file.sizeBytes === buildSeedFileObjectMetadata(file).sizeBytes && file.checksumSha256 === buildSeedFileObjectMetadata(file).checksumSha256 && file.tags.every((tag) => !/demo|synthet/u.test(tag.toLowerCase())) && !/demo|synthet|seed/u.test(file.notes.toLowerCase()) ) ).toBe(true); expect(operational.acceptanceSignatures.every((signature) => signature.signerRole === "kunde" || signature.signerRole === "monteur")).toBe(true); expect(operational.activities.every((activity) => new Date(activity.createdAt) <= new Date(graph.referenceDate))).toBe(true); expect(operational.acceptances.every((acceptance) => new Set(acceptance.attendees).size === acceptance.attendees.length)).toBe(true); expect( operational.acceptances.every( (acceptance) => new Date(acceptance.date) <= new Date(acceptance.createdAt) && new Date(acceptance.createdAt) <= new Date(acceptance.updatedAt) && new Date(acceptance.updatedAt) <= new Date(graph.referenceDate) ) ).toBe(true); expect( operational.acceptances .filter(({ kind }) => kind === "with_defects") .every((acceptance) => operational.checklists.find(({ id }) => id === acceptance.checklistId)?.type === "maengel") ).toBe(true); expect( operational.signatureDocuments.every((document) => operational.files.find(({ id }) => id === document.sourceFileId)?.displayName.startsWith("Angebot_")) ).toBe(true); expect( operational.signatureDocuments.every( (document) => document.createdAt === document.sentAt && new Date(document.createdAt) <= new Date(document.updatedAt) && new Date(document.updatedAt) <= new Date(graph.referenceDate) ) ).toBe(true); expect( operational.invoices.every((invoice) => { const project = projects.get(invoice.projectId)!; return project.pipeline === "auftrag" || project.stage === "gewonnen"; }) ).toBe(true); expect(operational.invoices.every((invoice) => new Date(invoice.date) <= new Date(graph.referenceDate))).toBe(true); const invoiceFileIds = operational.invoices.flatMap(({ fileId }) => (fileId ? [fileId] : [])); expect(invoiceFileIds.length).toBeGreaterThan(0); expect(new Set(invoiceFileIds).size).toBe(invoiceFileIds.length); expect( operational.invoices.every((invoice) => { if (!invoice.fileId) return true; const file = operational.files.find(({ id }) => id === invoice.fileId); return file?.projectId === invoice.projectId && file.displayName.startsWith("Rechnung_"); }) ).toBe(true); expect(operational.checklistItems.every(({ photoFileId }) => photoFileId === null)).toBe(true); expect(operational.acceptances.some(({ documentFileId }) => documentFileId !== null)).toBe(true); expect( operational.acceptances.every((acceptance) => { if (!acceptance.documentFileId) return true; const file = operational.files.find(({ id }) => id === acceptance.documentFileId); return file?.projectId === acceptance.projectId && file.displayName.startsWith("Abnahmeprotokoll_"); }) ).toBe(true); expect( operational.bookingAppointments.every((appointment) => operational.bookingProviders.some( (provider) => provider.bookingPageId === appointment.bookingPageId && provider.providerUserId === appointment.providerUserId ) ) ).toBe(true); expect(new Set(operational.bookingAppointments.map(({ internalCalendarEventId }) => internalCalendarEventId)).size).toBe( operational.bookingAppointments.length ); expect( operational.bookingAppointments.every((appointment) => { const page = operational.bookingPages.find(({ id }) => id === appointment.bookingPageId)!; return new Date(appointment.endsAt).getTime() - new Date(appointment.startsAt).getTime() === page.durationMinutes * 60_000; }) ).toBe(true); }); it("is deterministic across complete operational generations", () => { expect(generateOperationalGraph(generateDemoProSeed())).toEqual(generateOperationalGraph(generateDemoProSeed())); }); });