UNPKG

@testomatio/reporter

Version:
72 lines (71 loc) 2.45 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.linkStorage = void 0; const debug_1 = __importDefault(require("debug")); const data_storage_js_1 = require("../data-storage.js"); const debug = (0, debug_1.default)('@testomatio/reporter:services-links'); class LinkStorage { static #instance; /** * * @returns {LinkStorage} */ static getInstance() { if (!this.#instance) { this.#instance = new LinkStorage(); } return this.#instance; } /** * Stores links array and passes it to reporter * @param {object[]} links - array of link objects * @param {*} context - full test title */ put(links, context = null) { if (!links || !Array.isArray(links)) return; data_storage_js_1.dataStorage.putData('links', links, context); } /** * Returns links array for the test * @param {*} context testId or test context from test runner * @returns {{[key: 'test' | 'jira']: string}[]} links array, e.g. [{test: 'TEST-123'}, {jira: 'JIRA-456'}] */ get(context = null) { const linksList = data_storage_js_1.dataStorage.getData('links', context); if (!linksList || !linksList?.length) return []; const allLinks = []; for (const links of linksList) { if (Array.isArray(links)) { allLinks.push(...links); } else if (typeof links === 'string') { try { const parsedLinks = JSON.parse(links); if (Array.isArray(parsedLinks)) { allLinks.push(...parsedLinks); } } catch (e) { debug(`Error parsing links for test ${context}`, links); } } } // Remove duplicates based on JSON string comparison const uniqueLinks = []; const seen = new Set(); for (const link of allLinks) { const key = JSON.stringify(link); if (!seen.has(key)) { seen.add(key); uniqueLinks.push(link); } } return uniqueLinks; } } exports.linkStorage = LinkStorage.getInstance();