/**
 * Copyright IBM Corp. 2024, 2025
 */

import { EnvironmentSchema } from "../../src/schemas/environment.schema.js";

jest.mock("@apic/api-model/common/Metadata.js", () => ({
  Metadata_TypeEnum: {
    REST: "REST",
    SWAGGER: "SWAGGER",
    SOAP: "SOAP",
    GRAPHQL: "GRAPHQL",
    ODATA: "ODATA",
  },
}));

describe("EnvironmentSchema", () => {
  it("should validate a valid environment object", () => {
    const input = {
      kind: "environment",
      metadata: {
        name: "Env1",
        version: "1.0.0",
        namespace: "default",
      },
      spec: {
        variables: [
          { key: "API_KEY", value: "123abc", isSecret: true },
          { key: "BASE_URL", value: "https://api.example.com" },
        ],
      },
    };

    expect(() => EnvironmentSchema.parse(input)).not.toThrow();
  });

  it("should set isSecret default to false if missing", () => {
    const input = {
      kind: "environment",
      metadata: {
        name: "Env2",
        version: "1.0.0",
        namespace: "default",
      },
      spec: {
        variables: [{ key: "TOKEN", value: "xyz" }],
      },
    };

    const parsed = EnvironmentSchema.parse(input);
    expect(parsed.spec.variables[0].isSecret).toBe(false);
    expect(() => EnvironmentSchema.parse(input)).not.toThrow();
    expect(parsed.spec.variables?.length).toBe(1);
  });

  it("should fail if variables is not an array", () => {
    const input = {
      kind: "environment",
      metadata: {
        name: "Env3",
        version: "1.0.0",
        namespace: "default",
      },
      spec: {
        variables: "not-an-array",
      },
    };

    expect(() => EnvironmentSchema.parse(input)).toThrow(/variables/);
  });

  it("should fail if variable item is missing required fields", () => {
    const input = {
      kind: "environment",
      metadata: {
        name: "Env4",
        version: "1.0.0",
        namespace: "default",
      },
      spec: {
        variables: [{ key: "API_SECRET" }],
      },
    };

    expect(() => EnvironmentSchema.parse(input)).toThrow(/value/);
  });
});
