import { describe, it, expect } from 'vitest';
import { isBucketRelativePath, validateBucketRelativePath } from './bucketPath';

describe('isBucketRelativePath (C2 / IL6 / V1)', () => {
  it('accepts bucket-relative paths', () => {
    expect(isBucketRelativePath('redbrick-bucket/project-2/brain-mri.dcm')).toBe(true);
    expect(isBucketRelativePath('my-bucket/file.dcm')).toBe(true);
  });
  it('rejects full URLs', () => {
    expect(isBucketRelativePath('https://s3.amazonaws.com/bucket/x.dcm')).toBe(false);
    expect(isBucketRelativePath('http://example.com/x')).toBe(false);
    expect(isBucketRelativePath('s3://bucket/key')).toBe(false);
    expect(isBucketRelativePath('gs://bucket/key')).toBe(false);
  });
  it('rejects absolute paths and traversal', () => {
    expect(isBucketRelativePath('/abs/path.dcm')).toBe(false);
    expect(isBucketRelativePath('bucket/../secret.dcm')).toBe(false);
  });
  it('rejects empty, bucket-only, and trailing-slash paths', () => {
    expect(isBucketRelativePath('')).toBe(false);
    expect(isBucketRelativePath('   ')).toBe(false);
    expect(isBucketRelativePath('just-a-bucket')).toBe(false);
    expect(isBucketRelativePath('bucket/')).toBe(false);
  });
});

describe('validateBucketRelativePath', () => {
  it('returns null for a valid path', () => {
    expect(validateBucketRelativePath('bucket/a.dcm')).toBeNull();
  });
  it('returns a located PATH_NOT_BUCKET_RELATIVE error for a URL', () => {
    const err = validateBucketRelativePath('https://x/y.dcm', { entry: 'task-1' });
    expect(err?.code).toBe('PATH_NOT_BUCKET_RELATIVE');
    expect(err?.entry).toBe('task-1');
  });
});
