langchain-marketing-agent-dev / server /contentGenerator.test.ts
Benny-Tang's picture
Upload 117 files
453b50d verified
import { describe, it, expect, vi, beforeEach } from "vitest";
import { generateMarketingContent } from "./contentGenerator";
// Mock the invokeLLM function
vi.mock("./_core/llm", () => ({
invokeLLM: vi.fn(),
}));
describe("contentGenerator", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("should generate sales copy with required fields", async () => {
const { invokeLLM } = await import("./_core/llm");
(invokeLLM as any).mockResolvedValue({
choices: [
{
message: {
content:
"Introducing the Ultimate Product! Transform your life with our revolutionary solution. Limited time offer - get 50% off today!",
},
},
],
});
const result = await generateMarketingContent({
contentType: "sales_copy",
productName: "Amazing Widget",
productDescription: "A revolutionary product that changes everything",
targetAudience: "Tech enthusiasts",
tone: "Energetic",
});
expect(result.content).toBeTruthy();
expect(result.prompt).toContain("Amazing Widget");
expect(result.prompt).toContain("Tech enthusiasts");
});
it("should generate product description", async () => {
const { invokeLLM } = await import("./_core/llm");
(invokeLLM as any).mockResolvedValue({
choices: [
{
message: {
content:
"The Amazing Widget is a state-of-the-art device designed for modern consumers. Features include durability, ease of use, and exceptional performance.",
},
},
],
});
const result = await generateMarketingContent({
contentType: "product_description",
productName: "Amazing Widget",
keyFeatures: "Durable, Easy to use, High performance",
});
expect(result.content).toContain("Amazing Widget");
expect(result.prompt).toContain("product description");
});
it("should generate ad text", async () => {
const { invokeLLM } = await import("./_core/llm");
(invokeLLM as any).mockResolvedValue({
choices: [
{
message: {
content:
"Headline: Transform Your Life Today\nBody: Discover the power of our innovative solution. Click now for exclusive savings!",
},
},
],
});
const result = await generateMarketingContent({
contentType: "ad_text",
productName: "Life Transformer Pro",
targetAudience: "Busy professionals",
});
expect(result.content).toBeTruthy();
expect(result.prompt).toContain("ad copy");
});
it("should generate email template", async () => {
const { invokeLLM } = await import("./_core/llm");
(invokeLLM as any).mockResolvedValue({
choices: [
{
message: {
content:
"Subject: Exclusive Offer Just for You!\n\nDear Valued Customer,\n\nWe're excited to introduce our latest product...\n\nBest regards,\nThe Team",
},
},
],
});
const result = await generateMarketingContent({
contentType: "email_template",
productName: "Premium Service",
tone: "Professional",
});
expect(result.content).toContain("Subject:");
expect(result.prompt).toContain("email template");
});
it("should handle errors gracefully", async () => {
const { invokeLLM } = await import("./_core/llm");
(invokeLLM as any).mockRejectedValue(new Error("API Error"));
await expect(
generateMarketingContent({
contentType: "sales_copy",
productName: "Test Product",
})
).rejects.toThrow("Failed to generate marketing content");
});
it("should include all optional parameters in prompt", async () => {
const { invokeLLM } = await import("./_core/llm");
(invokeLLM as any).mockResolvedValue({
choices: [
{
message: {
content: "Generated content",
},
},
],
});
const result = await generateMarketingContent({
contentType: "sales_copy",
productName: "Premium Widget",
productDescription: "High-quality widget",
targetAudience: "Luxury buyers",
tone: "Sophisticated",
keyFeatures: "Premium, Durable, Elegant",
});
expect(result.prompt).toContain("Premium Widget");
expect(result.prompt).toContain("High-quality widget");
expect(result.prompt).toContain("Luxury buyers");
expect(result.prompt).toContain("Sophisticated");
expect(result.prompt).toContain("Premium, Durable, Elegant");
});
});