/**
 * Copyright IBM Corp. 2024, 2025
 */
import path from 'path';
import fs from 'fs';

jest.mock('@apic/studio-shared', () => ({
  Logger: {
    debug: jest.fn(),
    info: jest.fn(),
    warn: jest.fn(),
    error: jest.fn(),
    log: jest.fn(),
    createChildLogger: jest.fn().mockReturnThis(),
  },
  LogComponent: () => (target: any) => target,
  Component: {
    Build: 'Build',
    All: 'Studio',
  },
  SchemaHandler: jest.fn().mockImplementation(() => ({
    getSchema: jest.fn().mockReturnValue('{}'),
  })),
  YamlContent: jest.fn().mockImplementation(() => ({
    kind: '',
    apiVersion: '',
    spec: {},
    metadata: {
      name: '',
      version: '',
      namespace: '',
      labels: {
        gatewayTypes: [],
      },
    },
  })),
  Api_Spec_Ref: {},
}));

import { DataPowerAdapter } from '../src/adapter/datapower-adapter.js';

describe('Build Asset Project Modules', () => {
  it('should process the zip and return false if datapower asset is not there', async () => {
    const zipFilePath = path.resolve(__dirname, './assets/gateway-asset.zip');
    const Buffer = fs.readFileSync(zipFilePath);
    const obj = new DataPowerAdapter();
    const result = await obj['checkForDataPowerAssembly'](Buffer);
    expect(result).not.toBe(true);
  });

  it('should validate the zip and return 0 ', async () => {
    const zipFilePath = path.resolve(__dirname, './assets/gateway-asset.zip');
    const Buffer = fs.readFileSync(zipFilePath);
    const obj = new DataPowerAdapter();

    const result = await obj['getDataPowerAssemblyContent'](Buffer);
    expect(result).not.toBe(null);
    expect(result).not.toBe(undefined);
    expect(result.size).toBe(0);
  });

  it('should process the zip and return true if datapower asset is there', async () => {
    const zipFilePath = path.resolve(__dirname, './assets/datapower-gateway-jsonspec.zip');
    const Buffer = fs.readFileSync(zipFilePath);
    const obj = new DataPowerAdapter();
    const result = await obj['checkForDataPowerAssembly'](Buffer);
    expect(result).toBe(true);
  });

  it('should validate the zip and return true ', async () => {
    const zipFilePath = path.resolve(__dirname, './assets/datapower-gateway-jsonspec.zip');
    const Buffer = fs.readFileSync(zipFilePath);
    const obj = new DataPowerAdapter();

    const result = await obj['getDataPowerAssemblyContent'](Buffer);
    expect(result).not.toBe(null);
    expect(result).not.toBe(undefined);
    expect(result.size).toBe(1);
  });
});

describe('DataPowerAdapter CORS and Properties Processing', () => {
  it('should process CORS content and add it to x-ibm-configuration', async () => {
    const zipFilePath = path.resolve(
      __dirname,
      './assets/datapower-gateway-yamlspec-cors-properties.zip'
    );
    const buffer = fs.readFileSync(zipFilePath);
    const adapter = new DataPowerAdapter();

    const result = await adapter.getDataPowerAssemblyContent(buffer);

    expect(result.size).toBeGreaterThan(0);

    for (const [specFile, content] of result.entries()) {
      expect(specFile).toContain('.yml');
      expect(content).toHaveProperty('x-ibm-configuration');
      expect(content['x-ibm-configuration']).toHaveProperty('cors');
      expect(content['x-ibm-configuration'].cors).toHaveProperty('enabled', true);
      expect(content['x-ibm-configuration'].cors).toHaveProperty('policy');
      expect(Array.isArray(content['x-ibm-configuration'].cors.policy)).toBe(true);

      const policy = content['x-ibm-configuration'].cors.policy[0];
      expect(policy).toHaveProperty('allow-credentials');
      expect(policy).toHaveProperty('allow-origin');
      expect(policy).toHaveProperty('expose-headers');
      expect(policy['expose-headers']).toHaveProperty('predefined');
      expect(policy['expose-headers']).toHaveProperty('backend');
      expect(policy['expose-headers']).toHaveProperty('custom');
    }
  });
});
