From 19fec10fa3b83aec8ceb5986fe79997f46f5cf8f Mon Sep 17 00:00:00 2001 From: shadowusr Date: Wed, 1 Nov 2023 15:15:02 +0300 Subject: [PATCH] chore: re-write gui/constants to typescript --- lib/gui/api/facade.js | 4 ++-- lib/gui/constants/client-events.js | 13 ------------- lib/gui/constants/client-events.ts | 17 +++++++++++++++++ lib/gui/constants/custom-gui-control-types.js | 6 ------ lib/gui/constants/custom-gui-control-types.ts | 3 +++ lib/gui/constants/gui-events.js | 6 ------ lib/gui/constants/gui-events.ts | 10 ++++++++++ lib/gui/constants/server.js | 7 ------- lib/gui/constants/server.ts | 5 +++++ lib/gui/server.js | 2 +- lib/gui/tool-runner/report-subscriber.js | 16 ++++++++-------- lib/static/components/gui.jsx | 10 +++++----- test/unit/lib/gui/api/index.js | 8 ++++---- 13 files changed, 55 insertions(+), 52 deletions(-) delete mode 100644 lib/gui/constants/client-events.js create mode 100644 lib/gui/constants/client-events.ts delete mode 100644 lib/gui/constants/custom-gui-control-types.js create mode 100644 lib/gui/constants/custom-gui-control-types.ts delete mode 100644 lib/gui/constants/gui-events.js create mode 100644 lib/gui/constants/gui-events.ts delete mode 100644 lib/gui/constants/server.js create mode 100644 lib/gui/constants/server.ts diff --git a/lib/gui/api/facade.js b/lib/gui/api/facade.js index fd3b0cc63..2856e22f3 100644 --- a/lib/gui/api/facade.js +++ b/lib/gui/api/facade.js @@ -1,7 +1,7 @@ 'use strict'; const EventEmitter2 = require('eventemitter2'); -const guiEvents = require('../constants/gui-events'); +const {GuiEvents} = require('../constants/gui-events'); module.exports = class ApiFacade extends EventEmitter2 { static create() { @@ -11,6 +11,6 @@ module.exports = class ApiFacade extends EventEmitter2 { constructor() { super(); - this.events = guiEvents; + this.events = GuiEvents; } }; diff --git a/lib/gui/constants/client-events.js b/lib/gui/constants/client-events.js deleted file mode 100644 index a502317ca..000000000 --- a/lib/gui/constants/client-events.js +++ /dev/null @@ -1,13 +0,0 @@ -'use strict'; - -module.exports = { - BEGIN_SUITE: 'beginSuite', - BEGIN_STATE: 'beginState', - - TEST_RESULT: 'testResult', - - RETRY: 'retry', - ERROR: 'err', - - END: 'end' -}; diff --git a/lib/gui/constants/client-events.ts b/lib/gui/constants/client-events.ts new file mode 100644 index 000000000..e1216cda1 --- /dev/null +++ b/lib/gui/constants/client-events.ts @@ -0,0 +1,17 @@ +import {ValueOf} from 'type-fest'; + +export const ClientEvents = { + BEGIN_SUITE: 'beginSuite', + BEGIN_STATE: 'beginState', + + TEST_RESULT: 'testResult', + + RETRY: 'retry', + ERROR: 'err', + + END: 'end' +} as const; + +export type ClientEvents = typeof ClientEvents; + +export type ClientEvent = ValueOf; diff --git a/lib/gui/constants/custom-gui-control-types.js b/lib/gui/constants/custom-gui-control-types.js deleted file mode 100644 index 70d2f8e87..000000000 --- a/lib/gui/constants/custom-gui-control-types.js +++ /dev/null @@ -1,6 +0,0 @@ -'use strict'; - -module.exports = { - CONTROL_TYPE_BUTTON: 'button', - CONTROL_TYPE_RADIOBUTTON: 'radiobutton' -}; diff --git a/lib/gui/constants/custom-gui-control-types.ts b/lib/gui/constants/custom-gui-control-types.ts new file mode 100644 index 000000000..59634f992 --- /dev/null +++ b/lib/gui/constants/custom-gui-control-types.ts @@ -0,0 +1,3 @@ +export const CONTROL_TYPE_BUTTON = 'button'; + +export const CONTROL_TYPE_RADIOBUTTON = 'radiobutton'; diff --git a/lib/gui/constants/gui-events.js b/lib/gui/constants/gui-events.js deleted file mode 100644 index e88133511..000000000 --- a/lib/gui/constants/gui-events.js +++ /dev/null @@ -1,6 +0,0 @@ -'use strict'; - -module.exports = { - SERVER_INIT: 'serverInit', - SERVER_READY: 'serverReady' -}; diff --git a/lib/gui/constants/gui-events.ts b/lib/gui/constants/gui-events.ts new file mode 100644 index 000000000..b31455b85 --- /dev/null +++ b/lib/gui/constants/gui-events.ts @@ -0,0 +1,10 @@ +import {ValueOf} from 'type-fest'; + +export const GuiEvents = { + SERVER_INIT: 'serverInit', + SERVER_READY: 'serverReady' +} as const; + +export type GuiEvents = typeof GuiEvents; + +export type GuiEvent = ValueOf; diff --git a/lib/gui/constants/server.js b/lib/gui/constants/server.js deleted file mode 100644 index 54d45e942..000000000 --- a/lib/gui/constants/server.js +++ /dev/null @@ -1,7 +0,0 @@ -'use strict'; - -module.exports = { - MAX_REQUEST_SIZE: '100mb', - KEEP_ALIVE_TIMEOUT: 120 * 1000, - HEADERS_TIMEOUT: 125 * 1000 -}; diff --git a/lib/gui/constants/server.ts b/lib/gui/constants/server.ts new file mode 100644 index 000000000..e0d7094f9 --- /dev/null +++ b/lib/gui/constants/server.ts @@ -0,0 +1,5 @@ +export const MAX_REQUEST_SIZE = '100mb'; + +export const KEEP_ALIVE_TIMEOUT = 120 * 1000; + +export const HEADERS_TIMEOUT = 125 * 1000; diff --git a/lib/gui/server.js b/lib/gui/server.js index f3bee2c71..84e795216 100644 --- a/lib/gui/server.js +++ b/lib/gui/server.js @@ -8,7 +8,7 @@ const bodyParser = require('body-parser'); const {INTERNAL_SERVER_ERROR, OK} = require('http-codes'); const App = require('./app'); -const {MAX_REQUEST_SIZE, KEEP_ALIVE_TIMEOUT, HEADERS_TIMEOUT} = require('./constants/server'); +const {MAX_REQUEST_SIZE, KEEP_ALIVE_TIMEOUT, HEADERS_TIMEOUT} = require('./constants'); const {initializeCustomGui, runCustomGuiAction} = require('../server-utils'); const {logger} = require('../common-utils'); const initPluginsRoutes = require('./routes/plugins'); diff --git a/lib/gui/tool-runner/report-subscriber.js b/lib/gui/tool-runner/report-subscriber.js index 03ad83148..30490a0d7 100644 --- a/lib/gui/tool-runner/report-subscriber.js +++ b/lib/gui/tool-runner/report-subscriber.js @@ -2,7 +2,7 @@ const os = require('os'); const PQueue = require('p-queue'); -const clientEvents = require('../constants/client-events'); +const {ClientEvents} = require('../constants/client-events'); const {getSuitePath} = require('../../plugin-utils'); const {createWorkers} = require('../../workers/create-workers'); const {logError, formatTestResult} = require('../../server-utils'); @@ -34,7 +34,7 @@ module.exports = (hermione, reportBuilder, client, reportPath) => { return; } - client.emit(clientEvents.BEGIN_SUITE, { + client.emit(ClientEvents.BEGIN_SUITE, { suiteId: getSuitePath(suite).join(' '), status: TestStatus.RUNNING }); @@ -47,7 +47,7 @@ module.exports = (hermione, reportBuilder, client, reportPath) => { reportBuilder.addRunning(formattedResult); const testBranch = reportBuilder.getTestBranch(formattedResult.id); - return client.emit(clientEvents.BEGIN_STATE, testBranch); + return client.emit(ClientEvents.BEGIN_STATE, testBranch); }); hermione.on(hermione.events.TEST_PASS, (testResult) => { @@ -59,7 +59,7 @@ module.exports = (hermione, reportBuilder, client, reportPath) => { reportBuilder.addSuccess(formattedResult); const testBranch = reportBuilder.getTestBranch(formattedResult.id); - client.emit(clientEvents.TEST_RESULT, testBranch); + client.emit(ClientEvents.TEST_RESULT, testBranch); }).catch(logError); }); @@ -73,7 +73,7 @@ module.exports = (hermione, reportBuilder, client, reportPath) => { reportBuilder.addRetry(formattedResult); const testBranch = reportBuilder.getTestBranch(formattedResult.id); - client.emit(clientEvents.TEST_RESULT, testBranch); + client.emit(ClientEvents.TEST_RESULT, testBranch); }).catch(logError); }); @@ -89,7 +89,7 @@ module.exports = (hermione, reportBuilder, client, reportPath) => { : reportBuilder.addError(formattedResult); const testBranch = reportBuilder.getTestBranch(formattedResult.id); - client.emit(clientEvents.TEST_RESULT, testBranch); + client.emit(ClientEvents.TEST_RESULT, testBranch); }).catch(logError); }); @@ -102,14 +102,14 @@ module.exports = (hermione, reportBuilder, client, reportPath) => { reportBuilder.addSkipped(formattedResult); const testBranch = reportBuilder.getTestBranch(formattedResult.id); - client.emit(clientEvents.TEST_RESULT, testBranch); + client.emit(ClientEvents.TEST_RESULT, testBranch); }).catch(logError); }); hermione.on(hermione.events.RUNNER_END, async () => { try { await queue.onIdle(); - client.emit(clientEvents.END); + client.emit(ClientEvents.END); } catch (err) { logError(err); } diff --git a/lib/static/components/gui.jsx b/lib/static/components/gui.jsx index 3ba3bf12c..81bcf2c72 100644 --- a/lib/static/components/gui.jsx +++ b/lib/static/components/gui.jsx @@ -11,7 +11,7 @@ import Loading from './loading'; import ModalContainer from '../containers/modal'; import MainTree from './main-tree'; import CustomScripts from './custom-scripts'; -import clientEvents from '../../gui/constants/client-events'; +import {ClientEvents} from '../../gui/constants/client-events'; import FaviconChanger from './favicon-changer'; import ExtensionPoint from './extension-point'; import BottomProgressBar from './bottom-progress-bar'; @@ -40,24 +40,24 @@ class Gui extends Component { const {actions} = this.props; const eventSource = new EventSource('/events'); - eventSource.addEventListener(clientEvents.BEGIN_SUITE, (e) => { + eventSource.addEventListener(ClientEvents.BEGIN_SUITE, (e) => { const data = JSON.parse(e.data); actions.suiteBegin(data); }); - eventSource.addEventListener(clientEvents.BEGIN_STATE, (e) => { + eventSource.addEventListener(ClientEvents.BEGIN_STATE, (e) => { const data = JSON.parse(e.data); actions.testBegin(data); }); - [clientEvents.TEST_RESULT, clientEvents.ERROR].forEach((eventName) => { + [ClientEvents.TEST_RESULT, ClientEvents.ERROR].forEach((eventName) => { eventSource.addEventListener(eventName, (e) => { const data = JSON.parse(e.data); actions.testResult(data); }); }); - eventSource.addEventListener(clientEvents.END, () => { + eventSource.addEventListener(ClientEvents.END, () => { actions.testsEnd(); }); } diff --git a/test/unit/lib/gui/api/index.js b/test/unit/lib/gui/api/index.js index cd2e2c853..8ba58af75 100644 --- a/test/unit/lib/gui/api/index.js +++ b/test/unit/lib/gui/api/index.js @@ -1,7 +1,7 @@ 'use strict'; const EventEmitter2 = require('eventemitter2'); -const guiEvents = require('lib/gui/constants/gui-events'); +const {GuiEvents} = require('lib/gui/constants/gui-events'); const Api = require('lib/gui/api'); const {stubTool} = require('../../../utils'); @@ -20,7 +20,7 @@ describe('lig/gui/api', () => { Api.create(tool); - assert.deepEqual(tool.gui.events, guiEvents); + assert.deepEqual(tool.gui.events, GuiEvents); }); }); @@ -29,7 +29,7 @@ describe('lig/gui/api', () => { const tool = stubTool(); const api = Api.create(tool); const onServerInit = sinon.spy().named('onServerInit'); - tool.gui.on(guiEvents.SERVER_INIT, onServerInit); + tool.gui.on(GuiEvents.SERVER_INIT, onServerInit); api.initServer({foo: 'bar'}); @@ -42,7 +42,7 @@ describe('lig/gui/api', () => { const tool = stubTool(); const api = Api.create(tool); const onServerReady = sinon.spy().named('onServerReady'); - tool.gui.on(guiEvents.SERVER_READY, onServerReady); + tool.gui.on(GuiEvents.SERVER_READY, onServerReady); api.serverReady({url: 'http://my.server'});