import createKubernetesApplyManifestAction from './k8s-apply';
import { KubernetesObjectApi } from '@kubernetes/client-node';

jest.mock('@kubernetes/client-node', () => {
	const originalModule = jest.requireActual('@kubernetes/client-node');
	return {
		...originalModule,
		KubeConfig: jest.fn(),
		KubernetesObjectApi: {
			makeApiClient: jest.fn().mockReturnValue({
				create: jest.fn().mockResolvedValue('Mocked API Call Success'),
			}),
		},
	};
});

describe('createKubernetesApplyManifestAction', () => {
	it('successfully reads a Kubernetes manifest from a file and processes it', async () => {
		const action = createKubernetesApplyManifestAction();
		const mockContext = {
			input: {
				manifestPath: './manifests/pod.yaml',
			},
			logger: {
				info: jest.fn(),
				error: jest.fn(),
			},
		};

		expect(process.env.TOKEN).not.toBeUndefined();
		expect(process.env.CLUSTER_URL).not.toBeUndefined();

		await action.handler(mockContext);

		expect(KubernetesObjectApi.makeApiClient().create).toHaveBeenCalled();
		expect(mockContext.logger.info).toHaveBeenCalled();
	});
});
