/**
 * Test script to verify image source detection logic
 * Run with: npx tsx test-image-detection.ts
 */

/**
 * Determines if a path is a URL (http/https) or a file system path
 */
function isUrl(path: string): boolean {
  return path.startsWith('http://') || path.startsWith('https://');
}

/**
 * Gets the appropriate image source URL
 */
function getImageSrc(photoId: number, path: string): string {
  if (isUrl(path)) {
    return path; // Direct access
  } else {
    return `/api/photos/${photoId}/image`; // API proxy
  }
}

// Test cases
const testCases = [
  {
    id: 1,
    path: 'https://picsum.photos/800/600',
    expected: 'direct',
    description: 'HTTPS URL (SharePoint, CDN)',
  },
  {
    id: 2,
    path: 'http://example.com/image.jpg',
    expected: 'direct',
    description: 'HTTP URL',
  },
  {
    id: 3,
    path: '/path/to/photos/image.jpg',
    expected: 'proxy',
    description: 'Unix file system path',
  },
  {
    id: 4,
    path: 'C:\\Photos\\image.jpg',
    expected: 'proxy',
    description: 'Windows file system path',
  },
  {
    id: 5,
    path: 'https://yourcompany.sharepoint.com/sites/Photos/image.jpg',
    expected: 'direct',
    description: 'SharePoint Online URL',
  },
  {
    id: 6,
    path: 'https://sharepoint.company.com/sites/Photos/image.jpg',
    expected: 'direct',
    description: 'SharePoint Server URL',
  },
];

console.log('🧪 Testing Image Source Detection Logic\n');
console.log('=' .repeat(60));

let passed = 0;
let failed = 0;

testCases.forEach((testCase) => {
  const result = getImageSrc(testCase.id, testCase.path);
  const isDirect = isUrl(testCase.path);
  const actual = isDirect ? 'direct' : 'proxy';
  const success = actual === testCase.expected;

  if (success) {
    passed++;
    console.log(`✅ Test ${testCase.id}: PASSED`);
  } else {
    failed++;
    console.log(`❌ Test ${testCase.id}: FAILED`);
  }

  console.log(`   Description: ${testCase.description}`);
  console.log(`   Path: ${testCase.path}`);
  console.log(`   Detected as: ${actual} (expected: ${testCase.expected})`);
  console.log(`   Image src: ${result}`);
  console.log('');
});

console.log('=' .repeat(60));
console.log(`Results: ${passed} passed, ${failed} failed`);

if (failed === 0) {
  console.log('🎉 All tests passed!');
  process.exit(0);
} else {
  console.log('⚠️  Some tests failed');
  process.exit(1);
}


