import { describe, it, expect } from 'vitest';
import {
validateInput,
GmailSearchSchema,
GmailGetMessageSchema,
DriveSearchSchema,
DriveCreateFolderSchema,
SheetsGetValuesSchema,
CalendarListEventsSchema,
CalendarEventSchema,
ISODateTimeSchema,
} from '../src/validation.js';
describe('validateInput', () => {
it('should return success for valid input', () => {
const result = validateInput(GmailSearchSchema, {
query: 'is:unread',
maxResults: 10,
});
expect(result.success).toBe(true);
if (result.success) {
expect(result.data.query).toBe('is:unread');
expect(result.data.maxResults).toBe(10);
}
});
it('should return errors for invalid input', () => {
const result = validateInput(GmailSearchSchema, {
query: '', // empty query is invalid
});
expect(result.success).toBe(false);
if (!result.success) {
expect(result.errors.length).toBeGreaterThan(0);
}
});
});
describe('GmailSearchSchema', () => {
it('should accept valid search params', () => {
const result = GmailSearchSchema.safeParse({
query: 'from:user@example.com',
maxResults: 50,
});
expect(result.success).toBe(true);
});
it('should use default maxResults', () => {
const result = GmailSearchSchema.safeParse({
query: 'is:unread',
});
expect(result.success).toBe(true);
if (result.success) {
expect(result.data.maxResults).toBe(10);
}
});
it('should reject empty query', () => {
const result = GmailSearchSchema.safeParse({
query: '',
});
expect(result.success).toBe(false);
});
it('should reject maxResults over 500', () => {
const result = GmailSearchSchema.safeParse({
query: 'test',
maxResults: 501,
});
expect(result.success).toBe(false);
});
});
describe('GmailGetMessageSchema', () => {
it('should accept valid params', () => {
const result = GmailGetMessageSchema.safeParse({
messageId: 'abc123',
format: 'full',
});
expect(result.success).toBe(true);
});
it('should use default format', () => {
const result = GmailGetMessageSchema.safeParse({
messageId: 'abc123',
});
expect(result.success).toBe(true);
if (result.success) {
expect(result.data.format).toBe('metadata');
}
});
});
describe('DriveSearchSchema', () => {
it('should accept valid search params', () => {
const result = DriveSearchSchema.safeParse({
q: "name contains 'report'",
pageSize: 100,
});
expect(result.success).toBe(true);
});
});
describe('DriveCreateFolderSchema', () => {
it('should accept folder name only', () => {
const result = DriveCreateFolderSchema.safeParse({
name: 'New Folder',
});
expect(result.success).toBe(true);
});
it('should accept folder name with parent', () => {
const result = DriveCreateFolderSchema.safeParse({
name: 'New Folder',
parentFolderId: 'abc123',
});
expect(result.success).toBe(true);
});
});
describe('SheetsGetValuesSchema', () => {
it('should accept valid range', () => {
const result = SheetsGetValuesSchema.safeParse({
spreadsheetId: 'abc123',
range: 'Sheet1!A1:B10',
});
expect(result.success).toBe(true);
});
});
describe('CalendarListEventsSchema', () => {
it('should use default calendarId', () => {
const result = CalendarListEventsSchema.safeParse({});
expect(result.success).toBe(true);
if (result.success) {
expect(result.data.calendarId).toBe('primary');
}
});
it('should accept time range', () => {
const result = CalendarListEventsSchema.safeParse({
timeMinISO: '2024-01-01T00:00:00Z',
timeMaxISO: '2024-01-31T23:59:59Z',
});
expect(result.success).toBe(true);
});
});
describe('CalendarEventSchema', () => {
it('should accept valid event', () => {
const result = CalendarEventSchema.safeParse({
summary: 'Meeting',
startISO: '2024-01-15T10:00:00Z',
endISO: '2024-01-15T11:00:00Z',
});
expect(result.success).toBe(true);
});
it('should reject if start is after end', () => {
const result = CalendarEventSchema.safeParse({
summary: 'Meeting',
startISO: '2024-01-15T12:00:00Z',
endISO: '2024-01-15T11:00:00Z',
});
expect(result.success).toBe(false);
});
});
describe('ISODateTimeSchema', () => {
it('should accept valid ISO datetime', () => {
const result = ISODateTimeSchema.safeParse('2024-01-15T10:00:00Z');
expect(result.success).toBe(true);
});
it('should accept ISO datetime with timezone offset', () => {
const result = ISODateTimeSchema.safeParse('2024-01-15T10:00:00+09:00');
expect(result.success).toBe(true);
});
it('should reject invalid datetime', () => {
const result = ISODateTimeSchema.safeParse('not-a-date');
expect(result.success).toBe(false);
});
});