From 3f4d1a4bfb12c15e3a5ec07a8ee9bbab0713fa27 Mon Sep 17 00:00:00 2001 From: Anders Gustafsson <34234789+anders-photowall@users.noreply.github.com> Date: Mon, 1 Sep 2025 10:09:59 +0200 Subject: [PATCH] Add test command to test status of set list of local routes (#29) --- src/commands/pwcli.ts | 9 ++- src/commands/setup/settings/settings.ts | 19 ++++++ src/commands/test/routes.ts | 81 +++++++++++++++++++++++++ src/commands/test/test.ts | 13 ++++ src/globals.ts | 3 + 5 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 src/commands/test/routes.ts create mode 100644 src/commands/test/test.ts diff --git a/src/commands/pwcli.ts b/src/commands/pwcli.ts index 9430c2a..b2fe380 100644 --- a/src/commands/pwcli.ts +++ b/src/commands/pwcli.ts @@ -7,7 +7,14 @@ const command: GluegunCommand = { run: async (toolbox: GluegunMenuToolbox) => { const { print } = toolbox; print.info('Welcome to photowall CLI for everything, almost 😬'); - await toolbox.menu.showMenu(null); + await toolbox.menu.showMenu(null, { + sections: [ + { + name: 'test', + description: 'Test commands', + }, + ], + }); }, }; diff --git a/src/commands/setup/settings/settings.ts b/src/commands/setup/settings/settings.ts index ebce883..660badc 100644 --- a/src/commands/setup/settings/settings.ts +++ b/src/commands/setup/settings/settings.ts @@ -105,11 +105,15 @@ module.exports = { print.info( `Created configuration file ${chalk.yellow(`~/.pwcli_settings`)}`, ); + if (!config) { config = { gh_token: '', photowall_repo: '', mandrill_api_key: '', + localhost_url: '', + localhost_username: '', + localhost_password: '', }; } @@ -130,6 +134,21 @@ module.exports = { initial: config.mandrill_api_key, }).run(); + config.localhost_url = await new Input({ + message: `If you want to be able to run route tests against localhost, please enter the base URL for localhost (e.g. https://www.photowall-local.se:8080) >`, + initial: config.localhost_url, + }).run(); + + config.localhost_username = await new Input({ + message: `If you want to be able to run route tests against localhost, please enter the username for localhost >`, + initial: config.localhost_username, + }).run(); + + config.localhost_password = await new Input({ + message: `If you want to be able to run route tests against localhost, please enter the password for localhost >`, + initial: config.localhost_password, + }).run(); + print.info(` ${JSON.stringify(config)} diff --git a/src/commands/test/routes.ts b/src/commands/test/routes.ts new file mode 100644 index 0000000..92164f2 --- /dev/null +++ b/src/commands/test/routes.ts @@ -0,0 +1,81 @@ +import { GluegunCommand } from 'gluegun'; +import { getSettings } from '../../globals'; +import { Agent } from 'https'; +import fetch from 'node-fetch'; + +const PATHS = [ + '/', + '/forest-fun-tapet', + '/foggy-forest-4-tapet', + '/sweet-safari-sky-rose-tapet', + '/rough-watercolors-tavla', + '/adorable-animals-in-the-forest-beige-poster', + '/farg', + '/vegas-farg', + '/fototapet', + '/canvas', + '/posters', + '/egen-fototapet/2239465/882eae1e', + '/api/product/data?p=94054&group=wallpaper', + '/api/price/wallpaper_product?height=321&width=123&unit=cm&productId=73543&materialId=4', + '/favoriter', + '/kollektioner', + '/inspiration', + '/magazine', + '/influencer-collections/emily-vanwagenen-x-photowall', +]; + +const command: GluegunCommand = { + name: 'routes (r)', + alias: ['r'], + description: + 'Test pre-configured routes for 200 status, defaults to run on configured localhost', + run: async (toolbox) => { + const { print } = toolbox; + const config = await getSettings(toolbox); + if (!config || !config.localhost_url) { + print.error( + 'No localhost URL set in settings. Please run "pwcli setup settings" and set localhost_url.', + ); + process.exit(1); + } + const HOST = config.localhost_url; + const USERNAME = config.localhost_username; + const PASSWORD = config.localhost_password; + + const httpsAgent = new Agent({ + rejectUnauthorized: false, + }); + + const results = await Promise.all( + PATHS.map(async (path) => { + try { + console.log(`Fetching ${HOST + path} ...`); + const res = await fetch(HOST + path, { + headers: { + Authorization: + 'Basic ' + + Buffer.from(`${USERNAME}:${PASSWORD}`).toString('base64'), + }, + agent: httpsAgent, + }); + return { path, status: res.status, message: res.statusText }; + } catch (e) { + console.error(e); + return { path, message: e.message || 'error' }; + } + }), + ); + for (const { path, status, message } of results) { + const msg = + status === 200 ? '✅' : `${message}${status ? ` (${status})` : ''}`; + if (status === 200) { + print.success(`${HOST}${path}: ${msg}`); + } else { + print.error(`${HOST}${path}: ${msg}`); + } + } + }, +}; + +module.exports = command; diff --git a/src/commands/test/test.ts b/src/commands/test/test.ts new file mode 100644 index 0000000..74b6ef7 --- /dev/null +++ b/src/commands/test/test.ts @@ -0,0 +1,13 @@ +import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu'; +import { GluegunCommand } from 'gluegun'; + +const command: GluegunCommand = { + name: 'test', + description: 'Test commands (t)', + alias: ['t'], + run: async (toolbox: GluegunMenuToolbox) => { + await toolbox.menu.showMenu('test'); + }, +}; + +module.exports = command; diff --git a/src/globals.ts b/src/globals.ts index deaa390..064e1b9 100644 --- a/src/globals.ts +++ b/src/globals.ts @@ -11,6 +11,9 @@ export type TConfig = { gh_token: string; photowall_repo: string; mandrill_api_key: string; + localhost_url?: string; + localhost_username?: string; + localhost_password?: string; }; export const getSettings = async (toolbox): Promise => { const { filesystem, print } = toolbox;