mirror of
https://github.com/kjanat/livedash-node.git
synced 2026-01-16 10:52:08 +01:00
fix: resolve TypeScript errors and eliminate manual coordinate hardcoding
- Fix sendEmail function call to use proper EmailOptions object - Improve GeographicMap by replacing 52 hardcoded coordinates with automatic extraction from @rapideditor/country-coder library - Fix test imports to use correct exported functions from lib modules - Add missing required properties to Prisma mock objects in tests - Properly type all mock objects with correct enum values and required fields - Simplify rate limiter mock to avoid private property conflicts - Fix linting issues with variable declarations and useEffect dependencies
This commit is contained in:
@ -63,8 +63,12 @@ describe("Authentication API Routes", () => {
|
||||
const mockCompany = {
|
||||
id: "company1",
|
||||
name: "Test Company",
|
||||
status: "ACTIVE",
|
||||
status: "ACTIVE" as const,
|
||||
csvUrl: "http://example.com/data.csv",
|
||||
csvUsername: null,
|
||||
csvPassword: null,
|
||||
dashboardOpts: {},
|
||||
maxUsers: 10,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
@ -74,8 +78,12 @@ describe("Authentication API Routes", () => {
|
||||
email: "test@example.com",
|
||||
name: "Test User",
|
||||
companyId: "company1",
|
||||
role: "USER",
|
||||
role: "USER" as const,
|
||||
password: "hashed-password",
|
||||
resetToken: null,
|
||||
resetTokenExpiry: null,
|
||||
invitedAt: null,
|
||||
invitedBy: null,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
@ -197,8 +205,12 @@ describe("Authentication API Routes", () => {
|
||||
const mockCompany = {
|
||||
id: "company1",
|
||||
name: "Test Company",
|
||||
status: "ACTIVE",
|
||||
status: "ACTIVE" as const,
|
||||
csvUrl: "http://example.com/data.csv",
|
||||
csvUsername: null,
|
||||
csvPassword: null,
|
||||
dashboardOpts: {},
|
||||
maxUsers: 10,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
@ -208,8 +220,12 @@ describe("Authentication API Routes", () => {
|
||||
email: "test@example.com",
|
||||
name: "Existing User",
|
||||
companyId: "company1",
|
||||
role: "USER",
|
||||
role: "USER" as const,
|
||||
password: "hashed-password",
|
||||
resetToken: null,
|
||||
resetTokenExpiry: null,
|
||||
invitedAt: null,
|
||||
invitedBy: null,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
@ -240,15 +256,17 @@ describe("Authentication API Routes", () => {
|
||||
it("should handle rate limiting", async () => {
|
||||
const { InMemoryRateLimiter } = await import("../../lib/rateLimiter");
|
||||
|
||||
// Mock rate limiter to return not allowed
|
||||
const mockRateLimiter = {
|
||||
checkRateLimit: vi.fn().mockReturnValue({
|
||||
allowed: false,
|
||||
resetTime: Date.now() + 60000,
|
||||
}),
|
||||
};
|
||||
// Mock rate limiter class constructor
|
||||
const mockCheckRateLimit = vi.fn().mockReturnValue({
|
||||
allowed: false,
|
||||
resetTime: Date.now() + 60000,
|
||||
});
|
||||
|
||||
vi.mocked(InMemoryRateLimiter).mockImplementation(() => mockRateLimiter);
|
||||
vi.mocked(InMemoryRateLimiter).mockImplementation(() => ({
|
||||
checkRateLimit: mockCheckRateLimit,
|
||||
cleanup: vi.fn(),
|
||||
destroy: vi.fn(),
|
||||
} as any));
|
||||
|
||||
const request = new NextRequest("http://localhost:3000/api/register", {
|
||||
method: "POST",
|
||||
@ -283,8 +301,12 @@ describe("Authentication API Routes", () => {
|
||||
email: "test@example.com",
|
||||
name: "Test User",
|
||||
companyId: "company1",
|
||||
role: "USER",
|
||||
role: "USER" as const,
|
||||
password: "hashed-password",
|
||||
resetToken: null,
|
||||
resetTokenExpiry: null,
|
||||
invitedAt: null,
|
||||
invitedBy: null,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
@ -418,8 +440,12 @@ describe("Authentication API Routes", () => {
|
||||
email: "test@example.com",
|
||||
name: "Test User",
|
||||
companyId: "company1",
|
||||
role: "USER",
|
||||
role: "USER" as const,
|
||||
password: "hashed-password",
|
||||
resetToken: null,
|
||||
resetTokenExpiry: null,
|
||||
invitedAt: null,
|
||||
invitedBy: null,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
||||
import { GET } from "../../app/api/dashboard/metrics/route";
|
||||
import { describe, it, expect, beforeEach, vi } from "vitest";
|
||||
import { NextRequest } from "next/server";
|
||||
import { GET } from "../../app/api/dashboard/metrics/route";
|
||||
|
||||
// Mock NextAuth
|
||||
vi.mock("next-auth", () => ({
|
||||
@ -10,35 +10,28 @@ vi.mock("next-auth", () => ({
|
||||
// Mock prisma
|
||||
vi.mock("../../lib/prisma", () => ({
|
||||
prisma: {
|
||||
session: {
|
||||
count: vi.fn(),
|
||||
findMany: vi.fn(),
|
||||
aggregate: vi.fn(),
|
||||
},
|
||||
user: {
|
||||
findUnique: vi.fn(),
|
||||
},
|
||||
company: {
|
||||
findUnique: vi.fn(),
|
||||
},
|
||||
session: {
|
||||
findMany: vi.fn(),
|
||||
count: vi.fn(),
|
||||
},
|
||||
sessionQuestion: {
|
||||
findMany: vi.fn(),
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
// Mock auth options
|
||||
vi.mock("../../lib/auth", () => ({
|
||||
authOptions: {},
|
||||
}));
|
||||
|
||||
describe("/api/dashboard/metrics", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe("GET /api/dashboard/metrics", () => {
|
||||
describe("GET", () => {
|
||||
it("should return 401 for unauthenticated users", async () => {
|
||||
const { getServerSession } = await import("next-auth");
|
||||
vi.mocked(getServerSession).mockResolvedValue(null);
|
||||
@ -85,10 +78,15 @@ describe("/api/dashboard/metrics", () => {
|
||||
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue({
|
||||
id: "user1",
|
||||
name: "Test User",
|
||||
email: "test@example.com",
|
||||
companyId: "company1",
|
||||
role: "ADMIN",
|
||||
role: "ADMIN" as const,
|
||||
password: "hashed",
|
||||
resetToken: null,
|
||||
resetTokenExpiry: null,
|
||||
invitedAt: null,
|
||||
invitedBy: null,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
@ -105,16 +103,26 @@ describe("/api/dashboard/metrics", () => {
|
||||
expect(data.error).toBe("Company not found");
|
||||
});
|
||||
|
||||
it("should return metrics data for valid requests", async () => {
|
||||
it("should return dashboard metrics successfully for admin", async () => {
|
||||
const { getServerSession } = await import("next-auth");
|
||||
const { prisma } = await import("../../lib/prisma");
|
||||
|
||||
vi.mocked(getServerSession).mockResolvedValue({
|
||||
user: { email: "admin@example.com" },
|
||||
expires: "2024-12-31",
|
||||
});
|
||||
|
||||
const mockUser = {
|
||||
id: "user1",
|
||||
email: "test@example.com",
|
||||
name: "Test Admin",
|
||||
email: "admin@example.com",
|
||||
companyId: "company1",
|
||||
role: "ADMIN",
|
||||
role: "ADMIN" as const,
|
||||
password: "hashed",
|
||||
resetToken: null,
|
||||
resetTokenExpiry: null,
|
||||
invitedAt: null,
|
||||
invitedBy: null,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
@ -123,7 +131,10 @@ describe("/api/dashboard/metrics", () => {
|
||||
id: "company1",
|
||||
name: "Test Company",
|
||||
csvUrl: "http://example.com/data.csv",
|
||||
sentimentAlert: 0.5,
|
||||
csvUsername: null,
|
||||
csvPassword: null,
|
||||
dashboardOpts: {},
|
||||
maxUsers: 10,
|
||||
status: "ACTIVE" as const,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
@ -132,49 +143,32 @@ describe("/api/dashboard/metrics", () => {
|
||||
const mockSessions = [
|
||||
{
|
||||
id: "session1",
|
||||
sessionId: "s1",
|
||||
companyId: "company1",
|
||||
importId: "import1",
|
||||
startTime: new Date("2024-01-01T10:00:00Z"),
|
||||
endTime: new Date("2024-01-01T10:30:00Z"),
|
||||
sentiment: "POSITIVE",
|
||||
messagesSent: 5,
|
||||
avgResponseTime: 2.5,
|
||||
tokens: 100,
|
||||
tokensEur: 0.002,
|
||||
language: "en",
|
||||
endTime: new Date("2024-01-01T11:00:00Z"),
|
||||
ipAddress: "192.168.1.1",
|
||||
country: "US",
|
||||
category: "SUPPORT",
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
{
|
||||
id: "session2",
|
||||
sessionId: "s2",
|
||||
companyId: "company1",
|
||||
startTime: new Date("2024-01-02T14:00:00Z"),
|
||||
endTime: new Date("2024-01-02T14:15:00Z"),
|
||||
sentiment: "NEGATIVE",
|
||||
messagesSent: 3,
|
||||
avgResponseTime: 1.8,
|
||||
tokens: 75,
|
||||
tokensEur: 0.0015,
|
||||
language: "es",
|
||||
country: "ES",
|
||||
category: "BILLING",
|
||||
fullTranscriptUrl: null,
|
||||
avgResponseTime: null,
|
||||
initialMsg: null,
|
||||
messagesSent: 5,
|
||||
escalated: false,
|
||||
forwardedHr: false,
|
||||
sentiment: "POSITIVE" as const,
|
||||
category: "SCHEDULE_HOURS" as const,
|
||||
language: "en",
|
||||
summary: "Test summary",
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
];
|
||||
|
||||
vi.mocked(getServerSession).mockResolvedValue({
|
||||
user: { email: "test@example.com" },
|
||||
expires: "2024-12-31",
|
||||
});
|
||||
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(mockUser);
|
||||
vi.mocked(prisma.company.findUnique).mockResolvedValue(mockCompany);
|
||||
vi.mocked(prisma.session.findMany).mockResolvedValue(mockSessions);
|
||||
vi.mocked(prisma.session.count).mockResolvedValue(2);
|
||||
vi.mocked(prisma.session.count).mockResolvedValue(1);
|
||||
vi.mocked(prisma.sessionQuestion.findMany).mockResolvedValue([]);
|
||||
|
||||
const request = new NextRequest(
|
||||
"http://localhost:3000/api/dashboard/metrics"
|
||||
@ -183,23 +177,33 @@ describe("/api/dashboard/metrics", () => {
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
const data = await response.json();
|
||||
|
||||
expect(data.metrics).toBeDefined();
|
||||
expect(data.company).toBeDefined();
|
||||
expect(data.metrics.totalSessions).toBe(2);
|
||||
expect(data.company.name).toBe("Test Company");
|
||||
expect(data).toHaveProperty("totalSessions");
|
||||
expect(data).toHaveProperty("sentimentDistribution");
|
||||
expect(data).toHaveProperty("countryCounts");
|
||||
expect(data).toHaveProperty("topQuestions");
|
||||
expect(data.totalSessions).toBe(1);
|
||||
});
|
||||
|
||||
it("should handle date range filtering", async () => {
|
||||
it("should return dashboard metrics successfully for regular user", async () => {
|
||||
const { getServerSession } = await import("next-auth");
|
||||
const { prisma } = await import("../../lib/prisma");
|
||||
|
||||
vi.mocked(getServerSession).mockResolvedValue({
|
||||
user: { email: "user@example.com" },
|
||||
expires: "2024-12-31",
|
||||
});
|
||||
|
||||
const mockUser = {
|
||||
id: "user1",
|
||||
email: "test@example.com",
|
||||
name: "Test User",
|
||||
email: "user@example.com",
|
||||
companyId: "company1",
|
||||
role: "ADMIN",
|
||||
role: "USER" as const,
|
||||
password: "hashed",
|
||||
resetToken: null,
|
||||
resetTokenExpiry: null,
|
||||
invitedAt: null,
|
||||
invitedBy: null,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
@ -208,142 +212,122 @@ describe("/api/dashboard/metrics", () => {
|
||||
id: "company1",
|
||||
name: "Test Company",
|
||||
csvUrl: "http://example.com/data.csv",
|
||||
sentimentAlert: 0.5,
|
||||
csvUsername: null,
|
||||
csvPassword: null,
|
||||
dashboardOpts: {},
|
||||
maxUsers: 10,
|
||||
status: "ACTIVE" as const,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
const mockSessions = [
|
||||
{
|
||||
id: "session1",
|
||||
companyId: "company1",
|
||||
importId: "import1",
|
||||
startTime: new Date("2024-01-01T10:00:00Z"),
|
||||
endTime: new Date("2024-01-01T11:00:00Z"),
|
||||
ipAddress: "192.168.1.1",
|
||||
country: "US",
|
||||
fullTranscriptUrl: null,
|
||||
avgResponseTime: null,
|
||||
initialMsg: null,
|
||||
messagesSent: 5,
|
||||
escalated: false,
|
||||
forwardedHr: false,
|
||||
sentiment: "POSITIVE" as const,
|
||||
category: "SCHEDULE_HOURS" as const,
|
||||
language: "en",
|
||||
summary: "Test summary",
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
];
|
||||
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(mockUser);
|
||||
vi.mocked(prisma.company.findUnique).mockResolvedValue(mockCompany);
|
||||
vi.mocked(prisma.session.findMany).mockResolvedValue(mockSessions);
|
||||
vi.mocked(prisma.session.count).mockResolvedValue(1);
|
||||
vi.mocked(prisma.sessionQuestion.findMany).mockResolvedValue([]);
|
||||
|
||||
const request = new NextRequest(
|
||||
"http://localhost:3000/api/dashboard/metrics"
|
||||
);
|
||||
const response = await GET(request);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
const data = await response.json();
|
||||
expect(data).toHaveProperty("totalSessions");
|
||||
expect(data).toHaveProperty("sentimentDistribution");
|
||||
expect(data).toHaveProperty("countryCounts");
|
||||
expect(data).toHaveProperty("topQuestions");
|
||||
});
|
||||
|
||||
it("should handle date range filters", async () => {
|
||||
const { getServerSession } = await import("next-auth");
|
||||
const { prisma } = await import("../../lib/prisma");
|
||||
|
||||
vi.mocked(getServerSession).mockResolvedValue({
|
||||
user: { email: "test@example.com" },
|
||||
user: { email: "user@example.com" },
|
||||
expires: "2024-12-31",
|
||||
});
|
||||
|
||||
const mockUser = {
|
||||
id: "user1",
|
||||
name: "Test User",
|
||||
email: "user@example.com",
|
||||
companyId: "company1",
|
||||
role: "USER" as const,
|
||||
password: "hashed",
|
||||
resetToken: null,
|
||||
resetTokenExpiry: null,
|
||||
invitedAt: null,
|
||||
invitedBy: null,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
const mockCompany = {
|
||||
id: "company1",
|
||||
name: "Test Company",
|
||||
csvUrl: "http://example.com/data.csv",
|
||||
csvUsername: null,
|
||||
csvPassword: null,
|
||||
dashboardOpts: {},
|
||||
maxUsers: 10,
|
||||
status: "ACTIVE" as const,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(mockUser);
|
||||
vi.mocked(prisma.company.findUnique).mockResolvedValue(mockCompany);
|
||||
vi.mocked(prisma.session.findMany).mockResolvedValue([]);
|
||||
vi.mocked(prisma.session.count).mockResolvedValue(0);
|
||||
vi.mocked(prisma.sessionQuestion.findMany).mockResolvedValue([]);
|
||||
|
||||
const request = new NextRequest(
|
||||
"http://localhost:3000/api/dashboard/metrics?startDate=2024-01-01&endDate=2024-01-31"
|
||||
);
|
||||
const response = await GET(request);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(prisma.session.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: expect.objectContaining({
|
||||
companyId: "company1",
|
||||
startTime: expect.objectContaining({
|
||||
gte: expect.any(Date),
|
||||
lte: expect.any(Date),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("should calculate metrics correctly", async () => {
|
||||
const { getServerSession } = await import("next-auth");
|
||||
const { prisma } = await import("../../lib/prisma");
|
||||
|
||||
const mockUser = {
|
||||
id: "user1",
|
||||
email: "test@example.com",
|
||||
companyId: "company1",
|
||||
role: "ADMIN",
|
||||
password: "hashed",
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
const mockCompany = {
|
||||
id: "company1",
|
||||
name: "Test Company",
|
||||
csvUrl: "http://example.com/data.csv",
|
||||
sentimentAlert: 0.5,
|
||||
status: "ACTIVE" as const,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
const mockSessions = [
|
||||
{
|
||||
id: "session1",
|
||||
sessionId: "s1",
|
||||
companyId: "company1",
|
||||
startTime: new Date("2024-01-01T10:00:00Z"),
|
||||
endTime: new Date("2024-01-01T10:30:00Z"),
|
||||
sentiment: "POSITIVE",
|
||||
messagesSent: 5,
|
||||
avgResponseTime: 2.0,
|
||||
tokens: 100,
|
||||
tokensEur: 0.002,
|
||||
language: "en",
|
||||
country: "US",
|
||||
category: "SUPPORT",
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
{
|
||||
id: "session2",
|
||||
sessionId: "s2",
|
||||
companyId: "company1",
|
||||
startTime: new Date("2024-01-01T14:00:00Z"),
|
||||
endTime: new Date("2024-01-01T14:20:00Z"),
|
||||
sentiment: "NEGATIVE",
|
||||
messagesSent: 3,
|
||||
avgResponseTime: 3.0,
|
||||
tokens: 150,
|
||||
tokensEur: 0.003,
|
||||
language: "en",
|
||||
country: "US",
|
||||
category: "BILLING",
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
];
|
||||
|
||||
vi.mocked(getServerSession).mockResolvedValue({
|
||||
user: { email: "test@example.com" },
|
||||
expires: "2024-12-31",
|
||||
});
|
||||
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(mockUser);
|
||||
vi.mocked(prisma.company.findUnique).mockResolvedValue(mockCompany);
|
||||
vi.mocked(prisma.session.findMany).mockResolvedValue(mockSessions);
|
||||
vi.mocked(prisma.session.count).mockResolvedValue(2);
|
||||
|
||||
const request = new NextRequest(
|
||||
"http://localhost:3000/api/dashboard/metrics"
|
||||
);
|
||||
const response = await GET(request);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
const data = await response.json();
|
||||
|
||||
expect(data.metrics.totalSessions).toBe(2);
|
||||
expect(data.metrics.avgResponseTime).toBe(2.5); // (2.0 + 3.0) / 2
|
||||
expect(data.metrics.totalTokens).toBe(250); // 100 + 150
|
||||
expect(data.metrics.totalTokensEur).toBe(0.005); // 0.002 + 0.003
|
||||
expect(data.metrics.sentimentPositiveCount).toBe(1);
|
||||
expect(data.metrics.sentimentNegativeCount).toBe(1);
|
||||
expect(data.metrics.languages).toEqual({ en: 2 });
|
||||
expect(data.metrics.countries).toEqual({ US: 2 });
|
||||
expect(data.metrics.categories).toEqual({ SUPPORT: 1, BILLING: 1 });
|
||||
expect(data.totalSessions).toBe(0);
|
||||
});
|
||||
|
||||
it("should handle errors gracefully", async () => {
|
||||
it("should handle database errors gracefully", async () => {
|
||||
const { getServerSession } = await import("next-auth");
|
||||
const { prisma } = await import("../../lib/prisma");
|
||||
|
||||
vi.mocked(getServerSession).mockResolvedValue({
|
||||
user: { email: "test@example.com" },
|
||||
user: { email: "user@example.com" },
|
||||
expires: "2024-12-31",
|
||||
});
|
||||
|
||||
vi.mocked(prisma.user.findUnique).mockRejectedValue(
|
||||
new Error("Database error")
|
||||
new Error("Database connection failed")
|
||||
);
|
||||
|
||||
const request = new NextRequest(
|
||||
@ -353,57 +337,7 @@ describe("/api/dashboard/metrics", () => {
|
||||
|
||||
expect(response.status).toBe(500);
|
||||
const data = await response.json();
|
||||
expect(data.error).toBe("Database error");
|
||||
});
|
||||
|
||||
it("should return empty metrics for companies with no sessions", async () => {
|
||||
const { getServerSession } = await import("next-auth");
|
||||
const { prisma } = await import("../../lib/prisma");
|
||||
|
||||
const mockUser = {
|
||||
id: "user1",
|
||||
email: "test@example.com",
|
||||
companyId: "company1",
|
||||
role: "ADMIN",
|
||||
password: "hashed",
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
const mockCompany = {
|
||||
id: "company1",
|
||||
name: "Test Company",
|
||||
csvUrl: "http://example.com/data.csv",
|
||||
sentimentAlert: 0.5,
|
||||
status: "ACTIVE" as const,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
vi.mocked(getServerSession).mockResolvedValue({
|
||||
user: { email: "test@example.com" },
|
||||
expires: "2024-12-31",
|
||||
});
|
||||
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(mockUser);
|
||||
vi.mocked(prisma.company.findUnique).mockResolvedValue(mockCompany);
|
||||
vi.mocked(prisma.session.findMany).mockResolvedValue([]);
|
||||
vi.mocked(prisma.session.count).mockResolvedValue(0);
|
||||
|
||||
const request = new NextRequest(
|
||||
"http://localhost:3000/api/dashboard/metrics"
|
||||
);
|
||||
const response = await GET(request);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
const data = await response.json();
|
||||
|
||||
expect(data.metrics.totalSessions).toBe(0);
|
||||
expect(data.metrics.avgResponseTime).toBe(0);
|
||||
expect(data.metrics.totalTokens).toBe(0);
|
||||
expect(data.metrics.languages).toEqual({});
|
||||
expect(data.metrics.countries).toEqual({});
|
||||
expect(data.metrics.categories).toEqual({});
|
||||
expect(data.error).toBe("Internal server error");
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -1,362 +1,149 @@
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
import { ProcessingScheduler } from "../../lib/processingScheduler";
|
||||
import { processUnprocessedSessions, getAIProcessingCosts } from "../../lib/processingScheduler";
|
||||
|
||||
vi.mock("../../lib/prisma", () => ({
|
||||
prisma: new PrismaClient(),
|
||||
}));
|
||||
|
||||
vi.mock("../../lib/env", () => ({
|
||||
env: {
|
||||
OPENAI_API_KEY: "test-key",
|
||||
PROCESSING_BATCH_SIZE: "10",
|
||||
PROCESSING_INTERVAL_MS: "5000",
|
||||
prisma: {
|
||||
session: {
|
||||
findMany: vi.fn(),
|
||||
update: vi.fn(),
|
||||
},
|
||||
aIProcessingRequest: {
|
||||
findMany: vi.fn(),
|
||||
aggregate: vi.fn(),
|
||||
},
|
||||
sessionProcessingStatus: {
|
||||
findMany: vi.fn(),
|
||||
create: vi.fn(),
|
||||
update: vi.fn(),
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
describe("Processing Scheduler", () => {
|
||||
let scheduler: ProcessingScheduler;
|
||||
vi.mock("../../lib/schedulerConfig", () => ({
|
||||
getSchedulerConfig: () => ({ enabled: true }),
|
||||
}));
|
||||
|
||||
vi.mock("node-fetch", () => ({
|
||||
default: vi.fn(),
|
||||
}));
|
||||
|
||||
describe("Processing Scheduler", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
scheduler = new ProcessingScheduler();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (scheduler) {
|
||||
scheduler.stop();
|
||||
}
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe("Scheduler lifecycle", () => {
|
||||
it("should initialize with correct default settings", () => {
|
||||
expect(scheduler).toBeDefined();
|
||||
expect(scheduler.isRunning()).toBe(false);
|
||||
});
|
||||
|
||||
it("should start and stop correctly", async () => {
|
||||
scheduler.start();
|
||||
expect(scheduler.isRunning()).toBe(true);
|
||||
|
||||
scheduler.stop();
|
||||
expect(scheduler.isRunning()).toBe(false);
|
||||
});
|
||||
|
||||
it("should not start multiple times", () => {
|
||||
scheduler.start();
|
||||
const firstStart = scheduler.isRunning();
|
||||
|
||||
scheduler.start(); // Should not start again
|
||||
const secondStart = scheduler.isRunning();
|
||||
|
||||
expect(firstStart).toBe(true);
|
||||
expect(secondStart).toBe(true);
|
||||
|
||||
scheduler.stop();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Processing pipeline stages", () => {
|
||||
it("should process transcript fetch stage", async () => {
|
||||
describe("processUnprocessedSessions", () => {
|
||||
it("should process sessions needing AI analysis", async () => {
|
||||
const mockSessions = [
|
||||
{
|
||||
id: "session1",
|
||||
import: {
|
||||
fullTranscriptUrl: "http://example.com/transcript1",
|
||||
rawTranscriptContent: null,
|
||||
messages: [
|
||||
{ id: "msg1", content: "Hello", role: "user" },
|
||||
{ id: "msg2", content: "Hi there", role: "assistant" },
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
const { prisma } = await import("../../lib/prisma");
|
||||
vi.mocked(prisma.session.findMany).mockResolvedValue(mockSessions);
|
||||
vi.mocked(prisma.session.update).mockResolvedValue({} as any);
|
||||
|
||||
// Mock fetch for OpenAI API
|
||||
const mockFetch = await import("node-fetch");
|
||||
vi.mocked(mockFetch.default).mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
id: "chatcmpl-test",
|
||||
model: "gpt-4o",
|
||||
usage: {
|
||||
prompt_tokens: 100,
|
||||
completion_tokens: 50,
|
||||
total_tokens: 150,
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const prismaMock = {
|
||||
session: {
|
||||
findMany: vi.fn().mockResolvedValue(mockSessions),
|
||||
update: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
};
|
||||
|
||||
vi.doMock("../../lib/prisma", () => ({
|
||||
prisma: prismaMock,
|
||||
}));
|
||||
|
||||
// Mock fetch for transcript content
|
||||
global.fetch = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
text: () => Promise.resolve("Mock transcript content"),
|
||||
});
|
||||
|
||||
await scheduler.processTranscriptFetch();
|
||||
|
||||
expect(prismaMock.session.findMany).toHaveBeenCalled();
|
||||
expect(global.fetch).toHaveBeenCalledWith(
|
||||
"http://example.com/transcript1"
|
||||
);
|
||||
});
|
||||
|
||||
it("should process AI analysis stage", async () => {
|
||||
const mockSessions = [
|
||||
{
|
||||
id: "session1",
|
||||
transcriptContent: "User: Hello\nAssistant: Hi there!",
|
||||
sentiment: null,
|
||||
summary: null,
|
||||
},
|
||||
];
|
||||
|
||||
const prismaMock = {
|
||||
session: {
|
||||
findMany: vi.fn().mockResolvedValue(mockSessions),
|
||||
update: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
aIProcessingRequest: {
|
||||
create: vi.fn().mockResolvedValue({ id: "request1" }),
|
||||
},
|
||||
};
|
||||
|
||||
vi.doMock("../../lib/prisma", () => ({
|
||||
prisma: prismaMock,
|
||||
}));
|
||||
|
||||
// Mock OpenAI API
|
||||
global.fetch = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: () =>
|
||||
Promise.resolve({
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: JSON.stringify({
|
||||
sentiment: "POSITIVE",
|
||||
summary: "Friendly greeting exchange",
|
||||
}),
|
||||
},
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: JSON.stringify({
|
||||
summary: "Test summary",
|
||||
sentiment: "POSITIVE",
|
||||
category: "SUPPORT",
|
||||
language: "en",
|
||||
}),
|
||||
},
|
||||
],
|
||||
usage: {
|
||||
prompt_tokens: 50,
|
||||
completion_tokens: 20,
|
||||
total_tokens: 70,
|
||||
},
|
||||
}),
|
||||
});
|
||||
],
|
||||
}),
|
||||
} as any);
|
||||
|
||||
await scheduler.processAIAnalysis();
|
||||
|
||||
expect(prismaMock.session.findMany).toHaveBeenCalled();
|
||||
expect(prismaMock.aIProcessingRequest.create).toHaveBeenCalled();
|
||||
await expect(processUnprocessedSessions(1)).resolves.not.toThrow();
|
||||
});
|
||||
|
||||
it("should handle OpenAI API errors gracefully", async () => {
|
||||
const mockSessions = [
|
||||
{
|
||||
id: "session1",
|
||||
transcriptContent: "User: Hello",
|
||||
},
|
||||
];
|
||||
it("should handle errors gracefully", async () => {
|
||||
const { prisma } = await import("../../lib/prisma");
|
||||
vi.mocked(prisma.session.findMany).mockRejectedValue(new Error("Database error"));
|
||||
|
||||
const prismaMock = {
|
||||
session: {
|
||||
findMany: vi.fn().mockResolvedValue(mockSessions),
|
||||
},
|
||||
aIProcessingRequest: {
|
||||
create: vi.fn().mockResolvedValue({ id: "request1" }),
|
||||
},
|
||||
};
|
||||
|
||||
vi.doMock("../../lib/prisma", () => ({
|
||||
prisma: prismaMock,
|
||||
}));
|
||||
|
||||
// Mock failed OpenAI API call
|
||||
global.fetch = vi.fn().mockResolvedValue({
|
||||
ok: false,
|
||||
status: 429,
|
||||
text: () => Promise.resolve("Rate limit exceeded"),
|
||||
});
|
||||
|
||||
await expect(scheduler.processAIAnalysis()).rejects.toThrow();
|
||||
});
|
||||
|
||||
it("should process question extraction stage", async () => {
|
||||
const mockSessions = [
|
||||
{
|
||||
id: "session1",
|
||||
transcriptContent:
|
||||
"User: How do I reset my password?\nAssistant: You can reset it in settings.",
|
||||
},
|
||||
];
|
||||
|
||||
const prismaMock = {
|
||||
session: {
|
||||
findMany: vi.fn().mockResolvedValue(mockSessions),
|
||||
update: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
question: {
|
||||
upsert: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
aIProcessingRequest: {
|
||||
create: vi.fn().mockResolvedValue({ id: "request1" }),
|
||||
},
|
||||
};
|
||||
|
||||
vi.doMock("../../lib/prisma", () => ({
|
||||
prisma: prismaMock,
|
||||
}));
|
||||
|
||||
// Mock OpenAI API for question extraction
|
||||
global.fetch = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: () =>
|
||||
Promise.resolve({
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: JSON.stringify({
|
||||
questions: ["How do I reset my password?"],
|
||||
}),
|
||||
},
|
||||
},
|
||||
],
|
||||
usage: {
|
||||
prompt_tokens: 30,
|
||||
completion_tokens: 15,
|
||||
total_tokens: 45,
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
await scheduler.processQuestionExtraction();
|
||||
|
||||
expect(prismaMock.session.findMany).toHaveBeenCalled();
|
||||
expect(prismaMock.question.upsert).toHaveBeenCalled();
|
||||
await expect(processUnprocessedSessions(1)).resolves.not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Error handling", () => {
|
||||
it("should handle database connection errors", async () => {
|
||||
const prismaMock = {
|
||||
session: {
|
||||
findMany: vi
|
||||
.fn()
|
||||
.mockRejectedValue(new Error("Database connection failed")),
|
||||
describe("getAIProcessingCosts", () => {
|
||||
it("should calculate processing costs correctly", async () => {
|
||||
const mockAggregation = {
|
||||
_sum: {
|
||||
totalCostEur: 10.50,
|
||||
promptTokens: 1000,
|
||||
completionTokens: 500,
|
||||
totalTokens: 1500,
|
||||
},
|
||||
_count: {
|
||||
id: 25,
|
||||
},
|
||||
};
|
||||
|
||||
vi.doMock("../../lib/prisma", () => ({
|
||||
prisma: prismaMock,
|
||||
}));
|
||||
const { prisma } = await import("../../lib/prisma");
|
||||
vi.mocked(prisma.aIProcessingRequest.aggregate).mockResolvedValue(mockAggregation);
|
||||
|
||||
await expect(scheduler.processTranscriptFetch()).rejects.toThrow(
|
||||
"Database connection failed"
|
||||
);
|
||||
});
|
||||
const result = await getAIProcessingCosts();
|
||||
|
||||
it("should handle invalid transcript URLs", async () => {
|
||||
const mockSessions = [
|
||||
{
|
||||
id: "session1",
|
||||
import: {
|
||||
fullTranscriptUrl: "invalid-url",
|
||||
rawTranscriptContent: null,
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const prismaMock = {
|
||||
session: {
|
||||
findMany: vi.fn().mockResolvedValue(mockSessions),
|
||||
},
|
||||
};
|
||||
|
||||
vi.doMock("../../lib/prisma", () => ({
|
||||
prisma: prismaMock,
|
||||
}));
|
||||
|
||||
global.fetch = vi.fn().mockRejectedValue(new Error("Invalid URL"));
|
||||
|
||||
await expect(scheduler.processTranscriptFetch()).rejects.toThrow();
|
||||
});
|
||||
|
||||
it("should handle malformed JSON responses from OpenAI", async () => {
|
||||
const mockSessions = [
|
||||
{
|
||||
id: "session1",
|
||||
transcriptContent: "User: Hello",
|
||||
},
|
||||
];
|
||||
|
||||
const prismaMock = {
|
||||
session: {
|
||||
findMany: vi.fn().mockResolvedValue(mockSessions),
|
||||
},
|
||||
aIProcessingRequest: {
|
||||
create: vi.fn().mockResolvedValue({ id: "request1" }),
|
||||
},
|
||||
};
|
||||
|
||||
vi.doMock("../../lib/prisma", () => ({
|
||||
prisma: prismaMock,
|
||||
}));
|
||||
|
||||
global.fetch = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: () =>
|
||||
Promise.resolve({
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: "Invalid JSON response",
|
||||
},
|
||||
},
|
||||
],
|
||||
usage: { total_tokens: 10 },
|
||||
}),
|
||||
expect(result).toEqual({
|
||||
totalCostEur: 10.50,
|
||||
totalRequests: 25,
|
||||
totalPromptTokens: 1000,
|
||||
totalCompletionTokens: 500,
|
||||
totalTokens: 1500,
|
||||
});
|
||||
});
|
||||
|
||||
await expect(scheduler.processAIAnalysis()).rejects.toThrow();
|
||||
it("should handle null aggregation results", async () => {
|
||||
const mockAggregation = {
|
||||
_sum: {
|
||||
totalCostEur: null,
|
||||
promptTokens: null,
|
||||
completionTokens: null,
|
||||
totalTokens: null,
|
||||
},
|
||||
_count: {
|
||||
id: 0,
|
||||
},
|
||||
};
|
||||
|
||||
const { prisma } = await import("../../lib/prisma");
|
||||
vi.mocked(prisma.aIProcessingRequest.aggregate).mockResolvedValue(mockAggregation);
|
||||
|
||||
const result = await getAIProcessingCosts();
|
||||
|
||||
expect(result).toEqual({
|
||||
totalCostEur: 0,
|
||||
totalRequests: 0,
|
||||
totalPromptTokens: 0,
|
||||
totalCompletionTokens: 0,
|
||||
totalTokens: 0,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("Rate limiting and batching", () => {
|
||||
it("should respect batch size limits", async () => {
|
||||
const mockSessions = Array.from({ length: 25 }, (_, i) => ({
|
||||
id: `session${i}`,
|
||||
transcriptContent: `Content ${i}`,
|
||||
}));
|
||||
|
||||
const prismaMock = {
|
||||
session: {
|
||||
findMany: vi.fn().mockResolvedValue(mockSessions),
|
||||
},
|
||||
};
|
||||
|
||||
vi.doMock("../../lib/prisma", () => ({
|
||||
prisma: prismaMock,
|
||||
}));
|
||||
|
||||
await scheduler.processAIAnalysis();
|
||||
|
||||
// Should only process up to batch size (10 by default)
|
||||
expect(prismaMock.session.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
take: 10,
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("should handle rate limiting gracefully", async () => {
|
||||
const consoleSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
|
||||
global.fetch = vi.fn().mockResolvedValue({
|
||||
ok: false,
|
||||
status: 429,
|
||||
text: () => Promise.resolve("Rate limit exceeded"),
|
||||
});
|
||||
|
||||
await expect(scheduler.processAIAnalysis()).rejects.toThrow();
|
||||
|
||||
consoleSpy.mockRestore();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -1,193 +1,188 @@
|
||||
import { describe, it, expect, beforeEach, vi } from "vitest";
|
||||
import { parseTranscriptContent } from "../../lib/transcriptParser";
|
||||
import { parseTranscriptToMessages } from "../../lib/transcriptParser";
|
||||
|
||||
describe("Transcript Parser", () => {
|
||||
const startTime = new Date('2024-01-01T10:00:00Z');
|
||||
const endTime = new Date('2024-01-01T10:30:00Z');
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("parseTranscriptContent", () => {
|
||||
describe("parseTranscriptToMessages", () => {
|
||||
it("should parse basic transcript with timestamps", () => {
|
||||
const transcript = `
|
||||
[10:00:00] User: Hello, I need help with my account
|
||||
[10:00:15] Assistant: I'd be happy to help you with your account. What specific issue are you experiencing?
|
||||
[10:00:45] User: I can't log in to my dashboard
|
||||
[10:01:00] Assistant: Let me help you troubleshoot that login issue.
|
||||
`.trim();
|
||||
[10.01.2024 10:00:00] User: Hello, I need help with my account
|
||||
[10.01.2024 10:00:15] Assistant: I'd be happy to help you with your account. What specific issue are you experiencing?
|
||||
[10.01.2024 10:00:45] User: I can't log in to my account
|
||||
`;
|
||||
|
||||
const messages = parseTranscriptContent(transcript);
|
||||
const result = parseTranscriptToMessages(transcript, startTime, endTime);
|
||||
|
||||
expect(messages).toHaveLength(4);
|
||||
|
||||
expect(messages[0]).toEqual({
|
||||
timestamp: new Date("1970-01-01T10:00:00.000Z"),
|
||||
role: "User",
|
||||
content: "Hello, I need help with my account",
|
||||
order: 0,
|
||||
});
|
||||
|
||||
expect(messages[1]).toEqual({
|
||||
timestamp: new Date("1970-01-01T10:00:15.000Z"),
|
||||
role: "Assistant",
|
||||
content:
|
||||
"I'd be happy to help you with your account. What specific issue are you experiencing?",
|
||||
order: 1,
|
||||
});
|
||||
|
||||
expect(messages[3].order).toBe(3);
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.messages).toHaveLength(3);
|
||||
expect(result.messages![0].role).toBe("User");
|
||||
expect(result.messages![0].content).toBe("Hello, I need help with my account");
|
||||
expect(result.messages![1].role).toBe("Assistant");
|
||||
expect(result.messages![2].role).toBe("User");
|
||||
expect(result.messages![2].content).toBe("I can't log in to my account");
|
||||
});
|
||||
|
||||
it("should handle transcript without timestamps", () => {
|
||||
it("should parse transcript without timestamps", () => {
|
||||
const transcript = `
|
||||
User: Hello there
|
||||
Assistant: Hi! How can I help you today?
|
||||
User: I need support
|
||||
Assistant: I'm here to help.
|
||||
`.trim();
|
||||
Assistant: Hello! How can I help you today?
|
||||
User: I need support with my order
|
||||
`;
|
||||
|
||||
const messages = parseTranscriptContent(transcript);
|
||||
const result = parseTranscriptToMessages(transcript, startTime, endTime);
|
||||
|
||||
expect(messages).toHaveLength(4);
|
||||
expect(messages[0].timestamp).toBeNull();
|
||||
expect(messages[0].role).toBe("User");
|
||||
expect(messages[0].content).toBe("Hello there");
|
||||
expect(messages[0].order).toBe(0);
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.messages).toHaveLength(3);
|
||||
expect(result.messages![0].role).toBe("User");
|
||||
expect(result.messages![0].content).toBe("Hello there");
|
||||
expect(result.messages![1].role).toBe("Assistant");
|
||||
expect(result.messages![1].content).toBe("Hello! How can I help you today?");
|
||||
expect(result.messages![2].role).toBe("User");
|
||||
expect(result.messages![2].content).toBe("I need support with my order");
|
||||
});
|
||||
|
||||
it("should handle multi-line messages", () => {
|
||||
const transcript = `
|
||||
User: I have a complex question
|
||||
about my billing
|
||||
Assistant: I understand your concern.
|
||||
Let me help you with that billing question.
|
||||
`;
|
||||
|
||||
const result = parseTranscriptToMessages(transcript, startTime, endTime);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.messages).toHaveLength(2);
|
||||
expect(result.messages![0].role).toBe("User");
|
||||
expect(result.messages![0].content).toContain("about my billing");
|
||||
expect(result.messages![1].role).toBe("Assistant");
|
||||
expect(result.messages![1].content).toContain("Let me help you");
|
||||
});
|
||||
|
||||
it("should handle mixed timestamp formats", () => {
|
||||
const transcript = `
|
||||
[2024-01-01 10:00:00] User: Hello
|
||||
10:00:15 Assistant: Hi there
|
||||
[10:00:30] User: How are you?
|
||||
Assistant: I'm doing well, thanks!
|
||||
`.trim();
|
||||
[10.01.2024 10:00:00] User: First message with timestamp
|
||||
Assistant: Message without timestamp
|
||||
[10.01.2024 10:01:00] User: Another message with timestamp
|
||||
`;
|
||||
|
||||
const messages = parseTranscriptContent(transcript);
|
||||
const result = parseTranscriptToMessages(transcript, startTime, endTime);
|
||||
|
||||
expect(messages).toHaveLength(4);
|
||||
expect(messages[0].timestamp).toEqual(
|
||||
new Date("2024-01-01T10:00:00.000Z")
|
||||
);
|
||||
expect(messages[1].timestamp).toEqual(
|
||||
new Date("1970-01-01T10:00:15.000Z")
|
||||
);
|
||||
expect(messages[2].timestamp).toEqual(
|
||||
new Date("1970-01-01T10:00:30.000Z")
|
||||
);
|
||||
expect(messages[3].timestamp).toBeNull();
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.messages).toHaveLength(3);
|
||||
expect(result.messages![0].role).toBe("User");
|
||||
expect(result.messages![1].role).toBe("Assistant");
|
||||
expect(result.messages![2].role).toBe("User");
|
||||
});
|
||||
|
||||
it("should handle various role formats", () => {
|
||||
it("should assign order values correctly", () => {
|
||||
const transcript = `
|
||||
Customer: I have a problem
|
||||
Support Agent: What can I help with?
|
||||
USER: My account is locked
|
||||
ASSISTANT: Let me check that for you
|
||||
System: Connection established
|
||||
`.trim();
|
||||
User: First
|
||||
Assistant: Second
|
||||
User: Third
|
||||
`;
|
||||
|
||||
const messages = parseTranscriptContent(transcript);
|
||||
const result = parseTranscriptToMessages(transcript, startTime, endTime);
|
||||
|
||||
expect(messages).toHaveLength(5);
|
||||
expect(messages[0].role).toBe("User"); // Customer -> User
|
||||
expect(messages[1].role).toBe("Assistant"); // Support Agent -> Assistant
|
||||
expect(messages[2].role).toBe("User"); // USER -> User
|
||||
expect(messages[3].role).toBe("Assistant"); // ASSISTANT -> Assistant
|
||||
expect(messages[4].role).toBe("System"); // System -> System
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.messages).toHaveLength(3);
|
||||
expect(result.messages![0].order).toBe(0);
|
||||
expect(result.messages![1].order).toBe(1);
|
||||
expect(result.messages![2].order).toBe(2);
|
||||
});
|
||||
|
||||
it("should handle malformed transcript gracefully", () => {
|
||||
it("should distribute timestamps evenly when no timestamps are present", () => {
|
||||
const transcript = `
|
||||
This is not a proper transcript format
|
||||
No colons here
|
||||
: Empty role
|
||||
User:
|
||||
: Empty content
|
||||
`.trim();
|
||||
User: First
|
||||
Assistant: Second
|
||||
User: Third
|
||||
`;
|
||||
|
||||
const messages = parseTranscriptContent(transcript);
|
||||
const result = parseTranscriptToMessages(transcript, startTime, endTime);
|
||||
|
||||
// Should still try to parse what it can
|
||||
expect(messages.length).toBeGreaterThanOrEqual(0);
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.messages).toHaveLength(3);
|
||||
|
||||
// First message should be at start time
|
||||
expect(result.messages![0].timestamp.getTime()).toBe(startTime.getTime());
|
||||
|
||||
// Last message should be at end time
|
||||
expect(result.messages![2].timestamp.getTime()).toBe(endTime.getTime());
|
||||
|
||||
// Middle message should be between start and end
|
||||
const midTime = result.messages![1].timestamp.getTime();
|
||||
expect(midTime).toBeGreaterThan(startTime.getTime());
|
||||
expect(midTime).toBeLessThan(endTime.getTime());
|
||||
});
|
||||
|
||||
// Check that all messages have required fields
|
||||
messages.forEach((message, index) => {
|
||||
expect(message).toHaveProperty("role");
|
||||
expect(message).toHaveProperty("content");
|
||||
expect(message).toHaveProperty("order", index);
|
||||
expect(message).toHaveProperty("timestamp");
|
||||
it("should handle empty content", () => {
|
||||
expect(parseTranscriptToMessages("", startTime, endTime)).toEqual({
|
||||
success: false,
|
||||
error: "Empty transcript content"
|
||||
});
|
||||
expect(parseTranscriptToMessages(" \n\n ", startTime, endTime)).toEqual({
|
||||
success: false,
|
||||
error: "Empty transcript content"
|
||||
});
|
||||
expect(parseTranscriptToMessages("\t\r\n", startTime, endTime)).toEqual({
|
||||
success: false,
|
||||
error: "Empty transcript content"
|
||||
});
|
||||
});
|
||||
|
||||
it("should preserve message order correctly", () => {
|
||||
it("should handle content with no valid messages", () => {
|
||||
const transcript = `
|
||||
User: First message
|
||||
Assistant: Second message
|
||||
User: Third message
|
||||
Assistant: Fourth message
|
||||
User: Fifth message
|
||||
`.trim();
|
||||
Just some random text
|
||||
without any proper format
|
||||
`;
|
||||
|
||||
const messages = parseTranscriptContent(transcript);
|
||||
const result = parseTranscriptToMessages(transcript, startTime, endTime);
|
||||
|
||||
expect(messages).toHaveLength(5);
|
||||
messages.forEach((message, index) => {
|
||||
expect(message.order).toBe(index);
|
||||
});
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBe("No messages found in transcript");
|
||||
});
|
||||
|
||||
it("should handle empty or whitespace-only transcript", () => {
|
||||
expect(parseTranscriptContent("")).toEqual([]);
|
||||
expect(parseTranscriptContent(" \n\n ")).toEqual([]);
|
||||
expect(parseTranscriptContent("\t\r\n")).toEqual([]);
|
||||
});
|
||||
|
||||
it("should handle special characters in content", () => {
|
||||
it("should handle case-insensitive roles", () => {
|
||||
const transcript = `
|
||||
User: Hello! How are you? 😊
|
||||
Assistant: I'm great! Thanks for asking. 🤖
|
||||
User: Can you help with this: https://example.com/issue?id=123&type=urgent
|
||||
Assistant: Absolutely! I'll check that URL for you.
|
||||
`.trim();
|
||||
user: Lower case user
|
||||
ASSISTANT: Upper case assistant
|
||||
System: Mixed case system
|
||||
`;
|
||||
|
||||
const messages = parseTranscriptContent(transcript);
|
||||
const result = parseTranscriptToMessages(transcript, startTime, endTime);
|
||||
|
||||
expect(messages).toHaveLength(4);
|
||||
expect(messages[0].content).toBe("Hello! How are you? 😊");
|
||||
expect(messages[2].content).toBe(
|
||||
"Can you help with this: https://example.com/issue?id=123&type=urgent"
|
||||
);
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.messages).toHaveLength(3);
|
||||
expect(result.messages![0].role).toBe("User");
|
||||
expect(result.messages![1].role).toBe("Assistant");
|
||||
expect(result.messages![2].role).toBe("System");
|
||||
});
|
||||
|
||||
it("should normalize role names consistently", () => {
|
||||
it("should parse European date format correctly", () => {
|
||||
const transcript = `
|
||||
customer: Hello
|
||||
support: Hi there
|
||||
CUSTOMER: How are you?
|
||||
SUPPORT: Good thanks
|
||||
Client: Great
|
||||
Agent: Wonderful
|
||||
`.trim();
|
||||
[10.01.2024 14:30:45] User: Message with European date format
|
||||
[10.01.2024 14:31:00] Assistant: Response message
|
||||
`;
|
||||
|
||||
const messages = parseTranscriptContent(transcript);
|
||||
const result = parseTranscriptToMessages(transcript, startTime, endTime);
|
||||
|
||||
expect(messages[0].role).toBe("User");
|
||||
expect(messages[1].role).toBe("Assistant");
|
||||
expect(messages[2].role).toBe("User");
|
||||
expect(messages[3].role).toBe("Assistant");
|
||||
expect(messages[4].role).toBe("User");
|
||||
expect(messages[5].role).toBe("Assistant");
|
||||
});
|
||||
|
||||
it("should handle long content without truncation", () => {
|
||||
const longContent = "A".repeat(5000);
|
||||
const transcript = `User: ${longContent}`;
|
||||
|
||||
const messages = parseTranscriptContent(transcript);
|
||||
|
||||
expect(messages).toHaveLength(1);
|
||||
expect(messages[0].content).toBe(longContent);
|
||||
expect(messages[0].content.length).toBe(5000);
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.messages).toHaveLength(2);
|
||||
|
||||
// Check that timestamps were parsed correctly
|
||||
const firstTimestamp = result.messages![0].timestamp;
|
||||
expect(firstTimestamp.getFullYear()).toBe(2024);
|
||||
expect(firstTimestamp.getMonth()).toBe(0); // January (0-indexed)
|
||||
expect(firstTimestamp.getDate()).toBe(10);
|
||||
expect(firstTimestamp.getHours()).toBe(14);
|
||||
expect(firstTimestamp.getMinutes()).toBe(30);
|
||||
expect(firstTimestamp.getSeconds()).toBe(45);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user