import * as requestModule from "../src/request"; import * as locationApi from "../src/api/Location"; import * as fugitiveApi from "../src/api/Fugitive"; import * as mobileApi from "../src/api/Mobile"; import * as stationaryApi from "../src/api/Stationary"; import * as GenericCalculation from "../src/api/Calculation"; import * as TransportationDistributionApi from "../src/api/TransportationAndDistribution"; import * as economicActivityApi from "../src/api/EconomicActivity"; import * as realEstateApi from "../src/api/RealEstate"; import * as physicalActivityApi from "../src/api/PhysicalActivity"; import * as UsageApi from "../src/api/Usage"; import * as Factors from "../src/api/Factor"; import * as FactorSets from "../src/api/FactorSets"; import * as VectorTypeSearch from "../src/api/TypeRecommender"; import * as AuditLogApi from "../src/api/AuditLog"; import { LOCATION_API_PATH, FUGITIVE_API_PATH, STATIONARY_API_PATH, MOBILE_API_PATH, GENERIC_CALCULATION_API_PATH, TRANSPORTATION_AND_DISTRIBUTION_API_PATH, FACTOR_API_PATH, FACTOR_SET_API_PATH, SEARCH_API_PATH, USAGE_API, ECONOMIC_ACTIVITY_API_PATH, REAL_ESTATE_API_PATH, PHYSICAL_ACTIVITY_API_PATH, TYPE_RECOMMENDER_API_PATH, AUDIT_LOG_API_PATH, } from "../src/Constants"; import locationPayload from "./mocks/LocationRequest"; import commonpayload from "./mocks/CommonRequest"; import { Client } from "../src/Client"; import GenericCalculationPayload from "./mocks/GenericCalculationRequest"; import FactorPayload from "./mocks/FactorRequest"; import SearchPayload from "./mocks/SearchRequest"; import physicalActivityPayload from "./mocks/PhysicalActivityRequest"; import physicalActivityEVICPayload from "./mocks/PhysicalActivityEVICRequest"; import economicActivityAttributionPayload from "./mocks/EconomicActivityAttributionRequest"; import attributionPayload from "./mocks/AttributionRequest"; type ApiTestCase = { name: string; func: (payload?: any) => Promise; path: string; payload?: any; pathParams?: string | string[]; queryParams?: Record | Record; method: "GET" | "POST" | "PUT"; }; const mockResp = "mock-success-response"; const testCases: ApiTestCase[] = [ { name: "Location API", func: locationApi.calculate, path: LOCATION_API_PATH, payload: locationPayload, method: "POST", }, { name: "Fugitive API", func: fugitiveApi.calculate, path: FUGITIVE_API_PATH, payload: commonpayload, method: "POST", }, { name: "Mobile API", func: mobileApi.calculate, path: MOBILE_API_PATH, payload: commonpayload, method: "POST", }, { name: "Stationary API", func: stationaryApi.calculate, path: STATIONARY_API_PATH, payload: commonpayload, method: "POST", }, { name: "GenericCalculation API", func: GenericCalculation.calculate, path: GENERIC_CALCULATION_API_PATH, payload: GenericCalculationPayload, method: "POST", }, { name: "Factor API", func: Factors.retrieveFactor, path: FACTOR_API_PATH, payload: FactorPayload, method: "POST", }, { name: "Transportation and Distribution API", func: TransportationDistributionApi.calculate, path: TRANSPORTATION_AND_DISTRIBUTION_API_PATH, payload: GenericCalculationPayload, method: "POST", }, { name: "Economic activity API", func: economicActivityApi.calculate, path: ECONOMIC_ACTIVITY_API_PATH, payload: commonpayload, method: "POST", }, { name: "Economic activity API with revenue attribution", func: economicActivityApi.calculate, path: ECONOMIC_ACTIVITY_API_PATH, payload: economicActivityAttributionPayload, method: "POST", }, { name: "Real estate API", func: realEstateApi.calculate, path: REAL_ESTATE_API_PATH, payload: commonpayload, method: "POST", }, { name: "Real estate API with attribution", func: realEstateApi.calculate, path: REAL_ESTATE_API_PATH, payload: attributionPayload, method: "POST", }, { name: "Physical activity API", func: physicalActivityApi.calculate, path: PHYSICAL_ACTIVITY_API_PATH, payload: commonpayload, method: "POST", }, { name: "Physical activity API with equity/debt attribution", func: physicalActivityApi.calculate, path: PHYSICAL_ACTIVITY_API_PATH, payload: physicalActivityPayload, method: "POST", }, { name: "Physical activity API with EVIC attribution", func: physicalActivityApi.calculate, path: PHYSICAL_ACTIVITY_API_PATH, payload: physicalActivityEVICPayload, method: "POST", }, { name: "FactorSet API", func: FactorSets.get, path: FACTOR_SET_API_PATH, method: "GET", }, { name: "Search API", func: Factors.search, path: SEARCH_API_PATH, payload: SearchPayload, method: "POST", }, { name: "Usage API - getUsage with history false", func: UsageApi.getUsage, path: USAGE_API, queryParams: { history: false }, method: "GET", }, { name: "Usage API - getUsage with history true", func: UsageApi.getUsage, path: USAGE_API, queryParams: { history: true }, method: "GET", }, { name: "Usage API - getUsage default", func: UsageApi.getUsage, path: USAGE_API, queryParams: { history: false }, method: "GET", }, { name: "VectorTypeSearch API", func: VectorTypeSearch.search, path: TYPE_RECOMMENDER_API_PATH, payload: SearchPayload, method: "POST", }, { name: "AuditLog API - getAuditLogConfig", func: AuditLogApi.getAuditConfig, path: AUDIT_LOG_API_PATH, method: "GET", }, { name: "AuditLog API - updateAuditLogConfig", func: AuditLogApi.updateAuditConfig, path: AUDIT_LOG_API_PATH, payload: { logRequest: true, logResponse: false }, method: "PUT", } ]; describe("API Test calculate functions", () => { let spy: jest.SpyInstance; let tokenSpy: jest.SpyInstance; beforeEach(() => { jest.clearAllMocks(); tokenSpy = jest .spyOn(Client as any, "requestToken") .mockResolvedValue( "eyJpzGciOiJIUzI1NlIsInR5cCI6IkplVCJ9." + Buffer.from( JSON.stringify({ exp: Math.floor(Date.now() / 1000) + 3600 }) ).toString("base64") + ".signature" ); Client.getClient({ apiKey: "mock-api-key", clientId: "mock-client-id", orgId: "mock-org-id", }); spy = jest .spyOn(requestModule, "makeApiRequest") .mockResolvedValue(mockResp); }); afterEach(() => { tokenSpy.mockRestore(); spy.mockRestore(); }); describe.each(testCases)( "$name", ({ func, path, payload, pathParams, queryParams, method }) => { it("Should call makeApiRequest with API url", async () => { let result; if (method === "POST" || method === "PUT") { result = await func(payload); } else { // For GET requests with queryParams if (queryParams) { // Check if it's a Usage API call with history parameter if (queryParams.hasOwnProperty('history')) { result = await func(queryParams.history); } else if (queryParams.type) { // For other APIs that use type parameter result = await func(queryParams.type); } else { result = await func(); } } else if (pathParams !== undefined) { result = await func(pathParams); } else { result = await func(); } } const clientDomain = Client.getInstance().getDomain(); let UrlWithParams = `${clientDomain}${path}`; if (method === "GET" && pathParams) { UrlWithParams = `${clientDomain}${path}/${ Array.isArray(pathParams) ? pathParams.join("/") : pathParams }`; } const expectedUrl = method === "GET" ? UrlWithParams : `${clientDomain}${path}`; const expectedRequest: any = { method, url: expectedUrl, }; if (method === "POST" || method === "PUT") expectedRequest.data = payload; if (method === "GET" && queryParams) expectedRequest.params = queryParams; expect(spy).toHaveBeenCalledWith(expectedRequest); expect(result).toBe(mockResp); }); } ); });