/**
 * Workflow validator (Zod schema) tests for UAT.
 * Covers create and update workflow validation – WF-01 to WF-04.
 *
 * Run: npm test -- workflow-validator
 */

import {
  createWorkflowSchema,
  updateWorkflowSchema,
  validateCreateWorkflow,
  validateUpdateWorkflow,
} from '../validators/workflow.validator';

const validApprovalLevel = {
  email: 'approver@example.com',
  tatHours: 24,
};

describe('Workflow validator', () => {
  describe('createWorkflowSchema (WF-01 to WF-04)', () => {
    it('WF-01: rejects missing title', () => {
      const data = {
        templateType: 'CUSTOM',
        description: 'Test description',
        priority: 'STANDARD',
        approvalLevels: [validApprovalLevel],
      };
      expect(() => createWorkflowSchema.parse(data)).toThrow();
    });

    it('WF-02: rejects invalid priority', () => {
      const data = {
        templateType: 'CUSTOM',
        title: 'Test',
        description: 'Desc',
        priority: 'INVALID_PRIORITY',
        approvalLevels: [validApprovalLevel],
      };
      expect(() => createWorkflowSchema.parse(data)).toThrow();
    });

    it('WF-03: rejects empty approval levels', () => {
      const data = {
        templateType: 'CUSTOM',
        title: 'Test',
        description: 'Desc',
        priority: 'STANDARD',
        approvalLevels: [],
      };
      expect(() => createWorkflowSchema.parse(data)).toThrow();
    });

    it('WF-04: accepts valid minimal create payload', () => {
      const data = {
        templateType: 'CUSTOM',
        title: 'Valid Title',
        description: 'Valid description',
        priority: 'STANDARD',
        approvalLevels: [validApprovalLevel],
      };
      const result = validateCreateWorkflow(data);
      expect(result.title).toBe('Valid Title');
      expect(result.priority).toBe('STANDARD');
      expect(result.approvalLevels).toHaveLength(1);
    });

    it('accepts EXPRESS priority', () => {
      const data = {
        templateType: 'CUSTOM',
        title: 'Express',
        description: 'Desc',
        priority: 'EXPRESS',
        approvalLevels: [validApprovalLevel],
      };
      const result = createWorkflowSchema.parse(data);
      expect(result.priority).toBe('EXPRESS');
    });
  });

  describe('updateWorkflowSchema', () => {
    it('accepts partial update with valid status', () => {
      const data = { status: 'APPROVED' };
      const result = updateWorkflowSchema.parse(data);
      expect(result.status).toBe('APPROVED');
    });

    it('rejects invalid status', () => {
      expect(() => updateWorkflowSchema.parse({ status: 'INVALID' })).toThrow();
    });
  });
});
