diff --git a/src/components/config/__tests__/parse-postman.test.ts b/src/components/config/__tests__/parse-postman.test.ts index 176975b1c..e3b3e5692 100644 --- a/src/components/config/__tests__/parse-postman.test.ts +++ b/src/components/config/__tests__/parse-postman.test.ts @@ -49,7 +49,7 @@ const unsupportedCollection = fs.readFileSync( ) describe('parseConfigFromPostman', () => { - xit('throws invalid JSON format', () => { + it('throws invalid JSON format', () => { try { parseConfigFromPostman('../fetch.ts') } catch (error: unknown) { @@ -59,7 +59,7 @@ describe('parseConfigFromPostman', () => { } }) - xit('throws unsupported collection version', () => { + it('throws unsupported collection version', () => { try { const collectionStr = unsupportedCollection @@ -74,7 +74,7 @@ describe('parseConfigFromPostman', () => { }) describe('basic Postman collection', () => { - xit('[v2.0] - should converted to Monika config', () => { + it('[v2.0] - should converted to Monika config', () => { const collectionStr = basicCollectionV20 const collectionJson = JSON.parse(collectionStr) const config: Config = parseConfigFromPostman(collectionStr) @@ -123,7 +123,7 @@ describe('parseConfigFromPostman', () => { } }) - xit('[v2.1] - should converted to Monika config', () => { + it('[v2.1] - should converted to Monika config', () => { const collectionStr = basicCollectionV21 const collectionJson = JSON.parse(collectionStr) const config: Config = parseConfigFromPostman(collectionStr) @@ -174,7 +174,7 @@ describe('parseConfigFromPostman', () => { }) describe('grouped Postman collection', () => { - xit('[v2.0] - should converted to Monika config', () => { + it('[v2.0] - should converted to Monika config', () => { const collectionStr = groupedCollectionV20 const collectionJson = JSON.parse(collectionStr) const config: Config = parseConfigFromPostman(collectionStr) @@ -226,7 +226,7 @@ describe('parseConfigFromPostman', () => { } }) - xit('[v2.1] - should converted to Monika config', () => { + it('[v2.1] - should converted to Monika config', () => { const collectionStr = groupedCollectionV21 const collectionJson = JSON.parse(collectionStr) const config: Config = parseConfigFromPostman(collectionStr) diff --git a/src/components/config/parse.ts b/src/components/config/parse.ts deleted file mode 100644 index 2abd15fc6..000000000 --- a/src/components/config/parse.ts +++ /dev/null @@ -1,116 +0,0 @@ -/********************************************************************************** - * MIT License * - * * - * Copyright (c) 2021 Hyperjump Technology * - * * - * Permission is hereby granted, free of charge, to any person obtaining a copy * - * of this software and associated documentation files (the "Software"), to deal * - * in the Software without restriction, including without limitation the rights * - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * - * copies of the Software, and to permit persons to whom the Software is * - * furnished to do so, subject to the following conditions: * - * * - * The above copyright notice and this permission notice shall be included in all * - * copies or substantial portions of the Software. * - * * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * - * SOFTWARE. * - **********************************************************************************/ - -import { readFile } from 'node:fs/promises' -import path from 'node:path' -import isUrl from 'is-url' -import yml from 'js-yaml' - -import type { Config } from '../../interfaces/config.js' -import { sendHttpRequest } from '../../utils/http.js' -import { parseHarFile } from './parser/har.js' -import { parseInsomnia } from './parser/insomnia.js' -import { parseConfigFromPostman } from './parser/postman.js' -import { parseConfigFromSitemap } from './parser/sitemap.js' -import { parseConfigFromText } from './parser/text.js' - -export type ConfigType = - | 'har' - | 'insomnia' - | 'monika' - | 'postman' - | 'sitemap' - | 'text' - -export async function parseByType( - source: string, - type: ConfigType -): Promise { - const config = isUrl(source) - ? await getConfigFileFromUrl(source) - : await readFile(source, { encoding: 'utf8' }) - const isEmpty = config.length === 0 - - if (isEmpty) { - throw new Error(`Failed to read ${source}, the file is empty.`) - } - - const extension = path.extname(source) - - if (type === 'har') return parseHarFile(config) - if (type === 'text') return parseConfigFromText(config) - if (type === 'postman') return parseConfigFromPostman(config) - if (type === 'sitemap') return parseConfigFromSitemap(config) - if (type === 'insomnia') - return parseInsomnia(config, extension.replace('.', '')) - - return parseConfigByExt({ - config, - extension, - source, - }) -} - -async function getConfigFileFromUrl(url: string) { - const config = await fetchConfigFile(url) - const isEmpty = config.length === 0 - - if (isEmpty) { - throw new Error( - `The remote file ${url} is empty. Please check the URL or your connection again.` - ) - } - - return config -} - -async function fetchConfigFile(url: string) { - try { - const { data } = await sendHttpRequest({ url }) - - return data - } catch { - throw new Error(`The configuration file in ${url} is unreachable.`) - } -} - -type ParseConfigByExtParams = { - config: string - extension: string - source: string -} - -function parseConfigByExt({ - config, - extension, - source, -}: ParseConfigByExtParams) { - const isYaml = ['.yaml', '.yml'].includes(extension) - - if (isYaml) { - return yml.load(config, { json: true }) as Config - } - - return isUrl(source) ? config : JSON.parse(config) -} diff --git a/src/components/config/probe.test.ts b/src/components/config/probe.test.ts index f81c8018b..5e466b8cc 100644 --- a/src/components/config/probe.test.ts +++ b/src/components/config/probe.test.ts @@ -39,7 +39,7 @@ const probe: Probe = { alerts: [], id: 'xVUcW', interval: 10, - name: 'Sample probe.js', + name: 'Sample Probe', } describe('Probe cache', () => { @@ -48,7 +48,7 @@ describe('Probe cache', () => { deleteProbe(id) } }) - it('should add a probe.js', () => { + it('should add a probe', () => { // act addProbe(probe) @@ -56,10 +56,10 @@ describe('Probe cache', () => { expect(findProbe(probe.id)).eq(probe) }) - it('should update a probe.js', () => { + it('should update a probe', () => { // arrange addProbe(probe) - const updatedName = 'Updated probe.js' + const updatedName = 'Updated probe' // act const isUpdated = updateProbe(probe.id, { ...probe, name: updatedName }) @@ -69,9 +69,9 @@ describe('Probe cache', () => { expect(findProbe(probe.id)?.name).eq(updatedName) }) - it('should not update a nonexistent probe.js', () => { + it('should not update a nonexistent probe', () => { // arrange - const updatedName = 'Updated probe.js' + const updatedName = 'Updated probe' // act const isUpdated = updateProbe('9WpFB', { ...probe, name: updatedName }) @@ -92,7 +92,7 @@ describe('Probe cache', () => { expect(probes.length).eq(1) }) - it('should not remove a nonexistent probe.js', () => { + it('should not remove a nonexistent probe', () => { // arrange addProbe(probe) @@ -104,7 +104,7 @@ describe('Probe cache', () => { expect(getProbes().length).eq(1) }) - it('should remove a probe.js', () => { + it('should remove a probe', () => { // arrange addProbe(probe) diff --git a/src/components/logger/history.ts b/src/components/logger/history.ts index ed7887cef..e16bdaa3e 100644 --- a/src/components/logger/history.ts +++ b/src/components/logger/history.ts @@ -373,7 +373,6 @@ export async function deleteNotificationLogs( * @returns Promise */ export async function flushAllLogs(): Promise { - console.log('mashook: lala lala lala') const dropAtlassianStatusPageTableSQL = 'DROP TABLE IF EXISTS atlassian_status_page_incidents;' const dropInstatusPageTableSQL = diff --git a/src/symon/index.test.ts b/src/symon/index.test.ts index 5a9094983..50d13b301 100644 --- a/src/symon/index.test.ts +++ b/src/symon/index.test.ts @@ -285,11 +285,11 @@ describe('Symon initiate', () => { }) }).timeout(15_000) - it('should add a new probe.js', async () => { + it('should add a new probe', async () => { // arrange const newProbe: Probe = { id: '3', - name: 'New probe.js', + name: 'New Probe', interval: 2, requests: [ { @@ -346,7 +346,7 @@ describe('Symon initiate', () => { await symon.stop() }).timeout(15_000) - it('should update a probe.js', async () => { + it('should update a probe', async () => { // arrange server.use( http.get('http://localhost:4000/api/v1/monika/1234/probe-changes', () => @@ -401,7 +401,7 @@ describe('Symon initiate', () => { await symon.stop() }).timeout(15_000) - it('should delete a probe.js', async () => { + it('should delete a probe', async () => { // arrange server.use( http.get('http://localhost:4000/api/v1/monika/1234/probe-changes', () => diff --git a/src/utils/read-file.ts b/src/utils/read-file.ts deleted file mode 100644 index 0683c1e16..000000000 --- a/src/utils/read-file.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { log } from './pino.js' -import fs from 'fs' - -// Alternative of fs.readFileSync() API with type safety error NodeJS.ErrnoException -export async function readFile(path: string, encoding: BufferEncoding) { - return new Promise((resolve, reject) => { - fs.readFile(path, { encoding }, (error, result) => { - if (error && error?.code === 'ENOENT') { - log.info( - `Could not find the file: ${path}. Monika is probably not running or ran from a diffent directory` - ) - reject(error) - } else { - resolve(result) - } - }) - }) -} diff --git a/test/plugins/prometheus/index.test.ts b/test/plugins/prometheus/index.test.ts index 6cfd01d57..0032eb103 100644 --- a/test/plugins/prometheus/index.test.ts +++ b/test/plugins/prometheus/index.test.ts @@ -78,13 +78,10 @@ describe('Prometheus plugin', () => { } catch (error: unknown) { // assert if (error instanceof Error) { - console.error('Error message:', error.message) - // Narrowing the error further to check for HTTP status const statusMatch = error.message.match(/HTTP error: (\d+)/) if (statusMatch) { const status = Number.parseInt(statusMatch[1], 10) - console.error('HTTP Status Code:', status) expect(status).to.equal(405) } }