import { ensureTestDBExists } from "../../__tests__/internal/ensureTestDBExists";
import { mockTestStd } from "../../__tests__/internal/mockTestStd";
import { getSchemaName } from "../../internal/getSchemaName";
import { listActiveSchemas } from "../../internal/listActiveSchemas";
import { promiseAll } from "../../internal/promiseAll";
import { move } from "../move";

let fromDsn: string;
let toDsn: string;

beforeEach(async () => {
  [fromDsn, toDsn] = await promiseAll([
    ensureTestDBExists({
      from: 1,
      to: 1,
      state: "active",
    }),
    ensureTestDBExists({
      dbNameSuffix: "to",
    }),
  ]);
});

test("move a microshard with no tables", async () => {
  const out = mockTestStd();
  try {
    await move({
      schema: await getSchemaName(fromDsn, 1),
      fromDsn,
      toDsn,
      commitAction: "deactivate-activate",
    });
    expect(await listActiveSchemas({ dsn: toDsn })).toEqual(["sh0001"]);
    expect(await listActiveSchemas({ dsn: fromDsn })).toEqual([]);
  } catch (e: unknown) {
    out.print();
    throw e;
  }
});
