test: user-menu: coverage

This commit is contained in:
coderiaser 2026-02-03 15:45:45 +02:00
parent 4533a25c6f
commit 3d19f4727a
4 changed files with 38 additions and 12 deletions

View file

@ -84,7 +84,7 @@ test('sw: register: registerSW', async (t, {location, navigator}) => {
const {register} = navigator.serviceWorker;
await registerSW('/hello');
t.calledWith(register, ['/hello/sw.mjs'], 'should call register');
t.calledWith(register, ['/hello/sw.js'], 'should call register');
t.end();
});
@ -98,6 +98,6 @@ test('sw: register: unregisterSW', async (t, {location, navigator}) => {
await unregisterSW('/hello');
t.calledWith(register, ['/hello/sw.mjs'], 'should call register');
t.calledWith(register, ['/hello/sw.js'], 'should call register');
t.end();
});

View file

@ -18,7 +18,7 @@ import * as cloudfunc from '#common/cloudfunc';
import authentication from './auth.js';
import {createConfig, configPath} from './config.js';
import modulas from './modulas.js';
import userMenu from './user-menu.js';
import {userMenu} from './user-menu.js';
import rest from './rest/index.js';
import route from './route.js';
import * as validate from './validate.js';

View file

@ -1,6 +1,8 @@
import {homedir} from 'node:os';
import {readFile as _readFile} from 'node:fs/promises';
import {join} from 'node:path';
import {readFileSync} from 'node:fs';
// warm up worker cache
import montag from 'montag';
import {tryToCatch} from 'try-to-catch';
import currify from 'currify';
@ -10,9 +12,8 @@ import {putout, codeframe} from 'putout';
transpile('');
const PREFIX = '/api/v1/user-menu';
const DEFAULT_MENU_PATH = new URL('../static/user-menu.js', import.meta.url).pathname;
export default currify(async ({menuName, readFile = _readFile}, req, res, next) => {
export const userMenu = currify(async ({menuName, readFile = _readFile}, req, res, next) => {
if (!req.url.startsWith(PREFIX))
return next();
@ -34,7 +35,7 @@ async function onGET({req, res, menuName, readFile}) {
const url = req.url.replace(PREFIX, '');
if (url === '/default')
return sendDefaultMenu(res);
return await sendDefaultMenu(res);
const {findUp} = await import('find-up');
@ -57,7 +58,7 @@ async function onGET({req, res, menuName, readFile}) {
.send(e.message);
if (e)
return sendDefaultMenu(res);
return await sendDefaultMenu(res);
const [parseError, result] = await transpile(source);
@ -85,10 +86,12 @@ function getError(error, source) {
`;
}
function sendDefaultMenu(res) {
res.sendFile(DEFAULT_MENU_PATH, {
cacheControl: false,
});
async function sendDefaultMenu(res) {
const menu = await getDefaultMenu();
res
.type('js')
.send(menu);
}
async function transpile(source) {
@ -102,3 +105,11 @@ async function transpile(source) {
],
});
}
async function getDefaultMenu() {
const DEFAULT_MENU_PATH = new URL('../static/user-menu.js', import.meta.url).pathname;
const menu = readFileSync(DEFAULT_MENU_PATH, 'utf8');
const [, result] = await transpile(menu);
return result.code;
}

View file

@ -4,7 +4,7 @@ import {readFileSync} from 'node:fs';
import {test, stub} from 'supertape';
import serveOnce from 'serve-once';
import {putout} from 'putout';
import userMenu from './user-menu.js';
import {userMenu} from './user-menu.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
@ -54,6 +54,21 @@ test('cloudcmd: user menu: io.mv', async (t) => {
t.end();
});
test('cloudcmd: user menu: default menu', async (t) => {
const options = {
menuName: '111.cloudcmd.menu.js',
};
const {request} = serveOnce(userMenu);
const {body} = await request.get(`/api/v1/user-menu?dir=abcd`, {
options,
});
t.match(body, 'module.exports');
t.end();
});
test('cloudcmd: user menu: io.cp', async (t) => {
const readFile = stub().returns(fixtureCopy);
const options = {