/**
 * Copyright Super iPaaS Integration LLC, an IBM Company 2025
 */

import { spawnSync } from "node:child_process";
import {
  MIGRATION_COMPLETED,
  MIGRATION_STARTED,
} from "../../../src/constants/message-constants";
import fs from "fs";

describe.skip("Functional tests for the migration test command", () => {
  const output = "test/resources/atm/func-out";

  afterEach(() => {
    fs.rmSync(output, { recursive: true, force: true });
  });

  test("should check for successful migrate tests in localDir and generate file in output", () => {
    const localDir = "test/resources/atm/atm-pet-store.yaml";
    const result = spawnSync(
      "node",
      [
        "dist/cli.js",
        "apim",
        "migrate",
        "--localDir",
        localDir,
        "--output",
        output,
      ],
      { encoding: "utf-8" }
    );
    const expectedLogs = [MIGRATION_STARTED, MIGRATION_COMPLETED];
    expectedLogs.forEach((log) => {
      expect(result.stdout.trim()).toContain(log);
    });

    expect(fs.existsSync(`${output}/atm-pet-store.yaml`)).toBeTruthy();
    expect(fs.existsSync(`${output}/assertion_atm-pet-store.yaml`)).toBeTruthy();
    expect(fs.existsSync(`${output}/assertion_1_atm-pet-store.yaml`)).toBeTruthy();
    expect(fs.existsSync(`${output}/environment_default_atm-pet-store.yaml`)).toBeTruthy();
  });

  test("should migrate tests in directory in batch", () => {
    const localDir = "test/resources/atm/";
    const result = spawnSync(
      "node",
      [
        "dist/cli.js",
        "apim",
        "migrate",
        "--localDir",
        localDir,
        "--output",
        output,
      ],
      { encoding: "utf-8" }
    );

    const files = fs.readdirSync(output, { withFileTypes: true }).map(({name}) => name);
    expect(files.length).toBe(20);

    expect(files.includes(`atm-pet-store.yaml`)).toBeTruthy();
    expect(files.includes(`atm-test-if-v2.yaml`)).toBeTruthy();
    expect(files.includes(`atm-test-v1.yaml`)).toBeTruthy();
    expect(files.includes(`atm-test-v2.yaml`)).toBeTruthy();
  });

  test("should work with short options also", () => {
    const localDir = "test/resources/atm/atm-pet-store.yaml";
    const result = spawnSync(
      "node",
      [
        "dist/cli.js",
        "apim",
        "migrate",
        "-l",
        localDir,
        "-o",
        output,
      ],
      { encoding: "utf-8" }
    );
    expect(fs.existsSync(`${output}/atm-pet-store.yaml`)).toBeTruthy();
  });
});
