Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
67 changes: 67 additions & 0 deletions workspace-server/src/__tests__/services/CalendarService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2088,5 +2088,72 @@ describe('CalendarService', () => {
const parsedResult = JSON.parse(result.content[0].text);
expect(parsedResult.error).toBe('Invalid input format');
});

it('should reject start with both dateTime and date', async () => {
const result = await calendarService.createEvent({
summary: 'Ambiguous Event',
start: { dateTime: '2024-01-15T10:00:00Z', date: '2024-01-15' },
end: { dateTime: '2024-01-15T12:00:00Z' },
});

const parsedResult = JSON.parse(result.content[0].text);
expect(parsedResult.error).toBe('Invalid input format');
});

it('should reject end with both dateTime and date', async () => {
const result = await calendarService.createEvent({
summary: 'Ambiguous Event',
start: { dateTime: '2024-01-15T10:00:00Z' },
end: { dateTime: '2024-01-15T12:00:00Z', date: '2024-01-15' },
});

const parsedResult = JSON.parse(result.content[0].text);
expect(parsedResult.error).toBe('Invalid input format');
});

it('should require summary for regular events', async () => {
const result = await calendarService.createEvent({
start: { dateTime: '2024-01-15T10:00:00Z' },
end: { dateTime: '2024-01-15T12:00:00Z' },
});

const parsedResult = JSON.parse(result.content[0].text);
expect(parsedResult.error).toBe('Invalid input format');
});

it('should require summary for explicit default eventType', async () => {
const result = await calendarService.createEvent({
start: { dateTime: '2024-01-15T10:00:00Z' },
end: { dateTime: '2024-01-15T12:00:00Z' },
eventType: 'default',
});

const parsedResult = JSON.parse(result.content[0].text);
expect(parsedResult.error).toBe('Invalid input format');
});

it('should reject officeLocation type without officeLocation details', async () => {
const result = await calendarService.createEvent({
start: { date: '2024-01-15' },
end: { date: '2024-01-16' },
eventType: 'workingLocation',
workingLocationProperties: { type: 'officeLocation' },
});

const parsedResult = JSON.parse(result.content[0].text);
expect(parsedResult.error).toBe('Invalid input format');
});

it('should reject customLocation type without customLocation details', async () => {
const result = await calendarService.createEvent({
start: { date: '2024-01-15' },
end: { date: '2024-01-16' },
eventType: 'workingLocation',
workingLocationProperties: { type: 'customLocation' },
});

const parsedResult = JSON.parse(result.content[0].text);
expect(parsedResult.error).toBe('Invalid input format');
});
});
});
73 changes: 67 additions & 6 deletions workspace-server/src/services/CalendarService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,42 @@ export class CalendarService {
const summary =
input.summary ?? (eventType ? summaryDefaults[eventType] : undefined);

// Validate start/end: at least one of dateTime or date must be provided
if ((!start.dateTime && !start.date) || (!end.dateTime && !end.date)) {
// Validate start: exactly one of dateTime or date must be provided
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The validations here make sense. At some point it may make sense to have a validation layer to separate all this logic to make it easier to read.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point filed #313 to cover this work.

if ((!start.dateTime && !start.date) || (start.dateTime && start.date)) {
return this.createValidationErrorResponse(
new z.ZodError([
{
code: 'custom',
message:
'start and end must each have either "dateTime" (for timed events) or "date" (for all-day events)',
path: ['start/end'],
'start must have exactly one of "dateTime" (for timed events) or "date" (for all-day events)',
path: ['start'],
},
]),
);
}

// Validate end: exactly one of dateTime or date must be provided
if ((!end.dateTime && !end.date) || (end.dateTime && end.date)) {
return this.createValidationErrorResponse(
new z.ZodError([
{
code: 'custom',
message:
'end must have exactly one of "dateTime" (for timed events) or "date" (for all-day events)',
path: ['end'],
},
]),
);
}

// Require summary for regular events
if ((!eventType || eventType === 'default') && !input.summary) {
return this.createValidationErrorResponse(
new z.ZodError([
{
code: 'custom',
message: 'summary is required for regular events',
path: ['summary'],
},
]),
);
Expand Down Expand Up @@ -304,6 +331,40 @@ export class CalendarService {
);
}

// Validate working location sub-properties match the declared type
if (eventType === 'workingLocation' && workingLocationProperties) {
if (
workingLocationProperties.type === 'officeLocation' &&
!workingLocationProperties.officeLocation
) {
return this.createValidationErrorResponse(
new z.ZodError([
{
code: 'custom',
message:
'officeLocation is required when workingLocationProperties.type is "officeLocation"',
path: ['workingLocationProperties', 'officeLocation'],
},
]),
);
}
if (
workingLocationProperties.type === 'customLocation' &&
!workingLocationProperties.customLocation
) {
return this.createValidationErrorResponse(
new z.ZodError([
{
code: 'custom',
message:
'customLocation is required when workingLocationProperties.type is "customLocation"',
path: ['workingLocationProperties', 'customLocation'],
},
]),
);
}
}

// Validate datetime formats (skip for date-only / all-day events)
try {
if (start.dateTime) {
Expand Down Expand Up @@ -613,10 +674,10 @@ export class CalendarService {

// Validate datetime formats if provided
try {
if (start) {
if (start?.dateTime !== undefined) {
iso8601DateTimeSchema.parse(start.dateTime);
}
if (end) {
if (end?.dateTime !== undefined) {
iso8601DateTimeSchema.parse(end.dateTime);
}
Comment thread
allenhutchison marked this conversation as resolved.
if (attendees) {
Expand Down
Loading