import { ensureTestDBExists } from "../../__tests__/internal/ensureTestDBExists";
import { mockTestStd } from "../../__tests__/internal/mockTestStd";
import { listActiveSchemas } from "../../internal/listActiveSchemas";
import { allocate } from "../allocate";
import { install } from "../install";

let dsn: string;

beforeEach(async () => {
  dsn = await ensureTestDBExists({ skipInstall: true });
});

test("install with default schema name fmt", async () => {
  const out = mockTestStd();
  try {
    await install({ dsn });
    await allocate({
      dsns: [dsn],
      from: 1,
      to: 1,
      migrateCmd: "true",
      activate: true,
    });
    expect(await listActiveSchemas({ dsn })).toEqual(["sh0001"]);
  } catch (e: unknown) {
    out.print();
    throw e;
  }
});

test("install with custom schema name fmt", async () => {
  const out = mockTestStd();
  try {
    await install({
      dsn,
      schemaNameFmt: "test_%d",
    });
    await allocate({
      dsns: [dsn],
      from: 1,
      to: 1,
      migrateCmd: "true",
      activate: true,
    });
    expect(await listActiveSchemas({ dsn })).toEqual(["test_1"]);
  } catch (e: unknown) {
    out.print();
    throw e;
  }
});
