|
| 1 | +import { mockAxiosPost } from "../../utls/http-requests-mock"; |
| 2 | +import { AssetRegistryService } from "../../../src/commands/asset-registry/asset-registry.service"; |
| 3 | +import { testContext } from "../../utls/test-context"; |
| 4 | +import { loggingTestTransport, mockWriteFileSync } from "../../jest.setup"; |
| 5 | +import { FileService } from "../../../src/core/utils/file-service"; |
| 6 | +import * as path from "path"; |
| 7 | +import * as fs from "fs"; |
| 8 | + |
| 9 | +jest.mock("fs", () => ({ |
| 10 | + ...jest.requireActual("fs"), |
| 11 | + readFileSync: jest.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +describe("Asset registry validate", () => { |
| 15 | + const validateResponse = { |
| 16 | + valid: false, |
| 17 | + diagnostics: [ |
| 18 | + { |
| 19 | + severity: "ERROR", |
| 20 | + nodeKey: "my-view", |
| 21 | + assetType: "BOARD_V2", |
| 22 | + path: "$.components[0].type", |
| 23 | + code: "INVALID_ENUM_VALUE", |
| 24 | + message: "Invalid component type", |
| 25 | + }, |
| 26 | + ], |
| 27 | + }; |
| 28 | + |
| 29 | + const requestBody = { |
| 30 | + assetType: "BOARD_V2", |
| 31 | + packageKey: "my-pkg", |
| 32 | + nodes: [{ key: "my-view", configuration: { components: [{ type: "bad" }] } }], |
| 33 | + }; |
| 34 | + |
| 35 | + it("Should validate with inline body and print result", async () => { |
| 36 | + mockAxiosPost( |
| 37 | + "https://myTeam.celonis.cloud/pacman/api/core/asset-registry/validate/BOARD_V2", |
| 38 | + validateResponse |
| 39 | + ); |
| 40 | + |
| 41 | + await new AssetRegistryService(testContext).validate( |
| 42 | + "BOARD_V2", JSON.stringify(requestBody), undefined, false |
| 43 | + ); |
| 44 | + |
| 45 | + expect(loggingTestTransport.logMessages.length).toBe(1); |
| 46 | + const output = loggingTestTransport.logMessages[0].message; |
| 47 | + expect(output).toContain("INVALID_ENUM_VALUE"); |
| 48 | + expect(output).toContain("my-view"); |
| 49 | + }); |
| 50 | + |
| 51 | + it("Should validate with inline body and save as JSON file", async () => { |
| 52 | + mockAxiosPost( |
| 53 | + "https://myTeam.celonis.cloud/pacman/api/core/asset-registry/validate/BOARD_V2", |
| 54 | + validateResponse |
| 55 | + ); |
| 56 | + |
| 57 | + await new AssetRegistryService(testContext).validate( |
| 58 | + "BOARD_V2", JSON.stringify(requestBody), undefined, true |
| 59 | + ); |
| 60 | + |
| 61 | + const expectedFileName = loggingTestTransport.logMessages[0].message.split(FileService.fileDownloadedMessage)[1]; |
| 62 | + expect(mockWriteFileSync).toHaveBeenCalledWith( |
| 63 | + path.resolve(process.cwd(), expectedFileName), |
| 64 | + expect.any(String), |
| 65 | + { encoding: "utf-8" } |
| 66 | + ); |
| 67 | + |
| 68 | + const written = JSON.parse(mockWriteFileSync.mock.calls[0][1]); |
| 69 | + expect(written.valid).toBe(false); |
| 70 | + expect(written.diagnostics[0].code).toBe("INVALID_ENUM_VALUE"); |
| 71 | + }); |
| 72 | + |
| 73 | + it("Should validate with body from file", async () => { |
| 74 | + (fs.readFileSync as jest.Mock).mockReturnValue(JSON.stringify(requestBody)); |
| 75 | + |
| 76 | + mockAxiosPost( |
| 77 | + "https://myTeam.celonis.cloud/pacman/api/core/asset-registry/validate/BOARD_V2", |
| 78 | + validateResponse |
| 79 | + ); |
| 80 | + |
| 81 | + await new AssetRegistryService(testContext).validate( |
| 82 | + "BOARD_V2", undefined, "request.json", false |
| 83 | + ); |
| 84 | + |
| 85 | + expect(fs.readFileSync).toHaveBeenCalledWith("request.json", "utf-8"); |
| 86 | + expect(loggingTestTransport.logMessages.length).toBe(1); |
| 87 | + expect(loggingTestTransport.logMessages[0].message).toContain("INVALID_ENUM_VALUE"); |
| 88 | + }); |
| 89 | + |
| 90 | + it("Should throw when neither body nor file is provided", async () => { |
| 91 | + await expect( |
| 92 | + new AssetRegistryService(testContext).validate("BOARD_V2", undefined, undefined, false) |
| 93 | + ).rejects.toThrow("Provide either --body (inline JSON) or -f (path to JSON file)."); |
| 94 | + }); |
| 95 | + |
| 96 | + it("Should throw when both body and file are provided", async () => { |
| 97 | + await expect( |
| 98 | + new AssetRegistryService(testContext).validate("BOARD_V2", "{}", "file.json", false) |
| 99 | + ).rejects.toThrow("Provide either --body or -f, not both."); |
| 100 | + }); |
| 101 | + |
| 102 | + it("Should throw when body is not valid JSON", async () => { |
| 103 | + await expect( |
| 104 | + new AssetRegistryService(testContext).validate("BOARD_V2", "not-json{", undefined, false) |
| 105 | + ).rejects.toThrow("Request body must be valid JSON."); |
| 106 | + }); |
| 107 | +}); |
0 commit comments