Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lead-capture-trim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@chatcops/core": patch
---

Require non-blank trimmed `name` and `projectDetails` in `capture_lead`, and name the offending field in validation errors.
12 changes: 6 additions & 6 deletions packages/core/src/tools/lead-capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import type { ChatTool } from './base.js';
export type LeadCaptureCallback = (lead: LeadData) => void | Promise<void>;

const leadInputSchema = z.object({
name: z.string(),
email: z.string().email(),
company: z.string().optional(),
phone: z.string().optional(),
projectDetails: z.string(),
name: z.string().trim().min(1, 'Required'),
email: z.string().trim().email('Invalid email'),
company: z.string().trim().optional(),
phone: z.string().trim().optional(),
projectDetails: z.string().trim().min(1, 'Required'),
});

export class LeadCaptureTool implements ChatTool {
Expand Down Expand Up @@ -41,7 +41,7 @@ export class LeadCaptureTool implements ChatTool {
if (!parsed.success) {
return {
success: false,
message: `Invalid lead data: ${parsed.error.issues.map((i) => i.message).join(', ')}`,
message: `Invalid lead data: ${parsed.error.issues.map((i) => `${i.path.join('.') || 'value'}: ${i.message}`).join(', ')}`,
};
}

Expand Down
62 changes: 62 additions & 0 deletions packages/core/tests/tools/lead-capture.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,68 @@ describe('LeadCaptureTool', () => {
errorSpy.mockRestore();
});

it('rejects a blank name', async () => {
const onCapture = vi.fn();
const tool = new LeadCaptureTool(onCapture);
const result = await tool.execute({
name: '',
email: 'john@example.com',
projectDetails: 'Need a website',
});
expect(result.success).toBe(false);
expect(result.message).toContain('name: Required');
expect(onCapture).not.toHaveBeenCalled();
});

it('rejects a whitespace-only name', async () => {
const tool = new LeadCaptureTool();
const result = await tool.execute({
name: ' ',
email: 'john@example.com',
projectDetails: 'Need a website',
});
expect(result.success).toBe(false);
expect(result.message).toContain('name: Required');
});

it('rejects a blank projectDetails', async () => {
const tool = new LeadCaptureTool();
const result = await tool.execute({
name: 'John',
email: 'john@example.com',
projectDetails: '',
});
expect(result.success).toBe(false);
expect(result.message).toContain('projectDetails: Required');
});

it('names every field that is wrong', async () => {
const tool = new LeadCaptureTool();
const result = await tool.execute({
name: '',
email: 'not-an-email',
projectDetails: ' ',
});
expect(result.success).toBe(false);
expect(result.message).toContain('name: Required');
expect(result.message).toContain('email: Invalid email');
expect(result.message).toContain('projectDetails: Required');
});

it('trims values with surrounding spaces in the captured lead', async () => {
const tool = new LeadCaptureTool();
const result = await tool.execute({
name: ' John Doe ',
email: ' john@example.com ',
projectDetails: ' Need a website ',
});
expect(result.success).toBe(true);
expect(result.data).toMatchObject({
name: 'John Doe',
email: 'john@example.com',
projectDetails: 'Need a website',
});
});
it('handles optional fields', async () => {
const tool = new LeadCaptureTool();
const result = await tool.execute({
Expand Down