import { describe, it, expect } from 'vitest';
import { backupInternals } from './run.js';

describe('backup internals', () => {
  it('builds minio host URL with protocol and encoded credentials', () => {
    const host = backupInternals.buildMinioHost({
      endpoint: 'localhost:9000',
      accessKey: 'minio-admin',
      secretKey: 's3cr3t/with:chars',
      secure: false,
    });

    expect(host).toBe('http://minio-admin:s3cr3t%2Fwith%3Achars@localhost:9000');
  });

  it('builds object key from prefix and file name', () => {
    expect(backupInternals.buildObjectKey('flynn/daily', 'a.tar.gz')).toBe('flynn/daily/a.tar.gz');
    expect(backupInternals.buildObjectKey('/flynn/daily/', 'a.tar.gz')).toBe('flynn/daily/a.tar.gz');
    expect(backupInternals.buildObjectKey('', 'a.tar.gz')).toBe('a.tar.gz');
  });

  it('resolves custom mc path with fallback', () => {
    expect(backupInternals.resolveMcPath('/usr/local/bin/mc')).toBe('/usr/local/bin/mc');
    expect(backupInternals.resolveMcPath('')).toBe('mc');
    expect(backupInternals.resolveMcPath(undefined)).toBe('mc');
  });

  it('formats missing mc binary errors with setup hint', () => {
    const error = new Error('spawn mc ENOENT') as Error & { code?: string };
    error.code = 'ENOENT';
    expect(backupInternals.formatMinioCliError(error, '/custom/mc')).toContain('MinIO client binary not found: /custom/mc');
  });
});
