|
| 1 | +import * as fs from "node:fs"; |
| 2 | +import * as path from "node:path"; |
| 3 | +import { mockAxiosGet, mockAxiosPost, mockedAxiosInstance } from "../../utls/http-requests-mock"; |
| 4 | +import { mockCreateReadStream, mockExistsSync } from "../../utls/fs-mock-utils"; |
| 5 | +import { ViewBookmarksCommandService } from "../../../src/commands/view/view-bookmarks-command.service"; |
| 6 | +import { ViewBookmarksManagerFactory } from "../../../src/commands/view/view-bookmarks.manager-factory"; |
| 7 | +import { loggingTestTransport, mockWriteFileSync } from "../../jest.setup"; |
| 8 | +import { testContext } from "../../utls/test-context"; |
| 9 | + |
| 10 | +describe("View bookmarks", () => { |
| 11 | + |
| 12 | + const boardId = "73d39112-73ae-4bbe-8051-3c0f14e065ec"; |
| 13 | + const exportBaseUrl = `https://myTeam.celonis.cloud/blueprint/api/bookmarks/export?boardId=${boardId}`; |
| 14 | + const importUrl = `https://myTeam.celonis.cloud/blueprint/api/bookmarks/import?boardId=${boardId}`; |
| 15 | + const bookmarksResponse = [ |
| 16 | + { |
| 17 | + bookmark: { name: "My View Bookmark", ownerId: "user-1", userPreferenceId: "pref-1" }, |
| 18 | + preference: { id: "pref-1", value: "{}" }, |
| 19 | + }, |
| 20 | + ]; |
| 21 | + |
| 22 | + describe("pull", () => { |
| 23 | + it("Should call export API with the default USER type and write the response to a file", async () => { |
| 24 | + mockAxiosGet(`${exportBaseUrl}&type=USER`, bookmarksResponse); |
| 25 | + |
| 26 | + await new ViewBookmarksCommandService(testContext).pullViewBookmarks(boardId, undefined); |
| 27 | + |
| 28 | + expect(mockedAxiosInstance.get).toHaveBeenCalledWith(`${exportBaseUrl}&type=USER`, expect.anything()); |
| 29 | + expect(mockWriteFileSync).toHaveBeenCalledWith( |
| 30 | + path.resolve(process.cwd(), `studio_view_bookmarks_${boardId}.json`), |
| 31 | + JSON.stringify(bookmarksResponse), |
| 32 | + { encoding: "utf-8", mode: 0o600 } |
| 33 | + ); |
| 34 | + expect(loggingTestTransport.logMessages.length).toBe(1); |
| 35 | + expect(loggingTestTransport.logMessages[0].message).toContain("File downloaded successfully. New filename: "); |
| 36 | + }); |
| 37 | + |
| 38 | + it("Should call export API with the provided type", async () => { |
| 39 | + mockAxiosGet(`${exportBaseUrl}&type=SHARED`, bookmarksResponse); |
| 40 | + |
| 41 | + await new ViewBookmarksCommandService(testContext).pullViewBookmarks(boardId, "SHARED"); |
| 42 | + |
| 43 | + expect(mockedAxiosInstance.get).toHaveBeenCalledWith(`${exportBaseUrl}&type=SHARED`, expect.anything()); |
| 44 | + expect(mockWriteFileSync).toHaveBeenCalledWith( |
| 45 | + path.resolve(process.cwd(), `studio_view_bookmarks_${boardId}.json`), |
| 46 | + JSON.stringify(bookmarksResponse), |
| 47 | + { encoding: "utf-8", mode: 0o600 } |
| 48 | + ); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe("push", () => { |
| 53 | + it("Should call import API with the file as multipart body", async () => { |
| 54 | + mockAxiosPost(importUrl, {}); |
| 55 | + mockExistsSync(); |
| 56 | + mockCreateReadStream(Buffer.from(JSON.stringify(bookmarksResponse))); |
| 57 | + |
| 58 | + await new ViewBookmarksCommandService(testContext).pushViewBookmarks(boardId, "bookmarks.json"); |
| 59 | + |
| 60 | + expect(mockedAxiosInstance.post).toHaveBeenCalledWith(importUrl, expect.anything(), expect.anything()); |
| 61 | + expect(loggingTestTransport.logMessages.length).toBe(1); |
| 62 | + expect(loggingTestTransport.logMessages[0].message).toContain("View Bookmarks were pushed successfully."); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe("manager factory", () => { |
| 67 | + it("Should report a fatal error when the push file does not exist", () => { |
| 68 | + (fs.existsSync as jest.Mock).mockReturnValue(false); |
| 69 | + const exitSpy = jest.spyOn(process, "exit").mockImplementation((() => undefined) as never); |
| 70 | + |
| 71 | + new ViewBookmarksManagerFactory(testContext).createViewBookmarksManager("missing.json", boardId); |
| 72 | + |
| 73 | + expect(exitSpy).toHaveBeenCalledWith(1); |
| 74 | + }); |
| 75 | + }); |
| 76 | +}); |
0 commit comments