import { describe, expect, it } from 'vitest';
import { adminUrlFrom, dbNameFromUrl } from './db-test-support.js';

describe('db-test-support pure helpers', () => {
  it('extracts the database name from a url', () => {
    expect(dbNameFromUrl('postgres://user:pass@localhost:5432/mdv_test')).toBe('mdv_test');
  });

  it('rejects database names that are not safe identifiers', () => {
    expect(() => dbNameFromUrl('postgres://u:p@localhost:5432/bad"name')).toThrowError(
      /not a safe identifier/,
    );
    expect(() => dbNameFromUrl('postgres://u:p@localhost:5432/semi;colon')).toThrowError(
      /not a safe identifier/,
    );
  });

  it('rejects urls without database name', () => {
    expect(() => dbNameFromUrl('postgres://u:p@localhost:5432/')).toThrowError(
      /must include a database name/,
    );
  });

  it('rewrites a url to the admin postgres database', () => {
    expect(adminUrlFrom('postgres://u:p@localhost:5432/mdv_test')).toBe(
      'postgres://u:p@localhost:5432/postgres',
    );
  });
});
