uikit/tests/provider.smoke.test.mjs
Mohamadzadeh f11313909d
All checks were successful
Build and Deploy Next.js + Nginx Docker Image / build-and-deploy (push) Has been skipped
Build and Deploy Next.js + Nginx Docker Image / deploy (push) Has been skipped
add new BiProvider and v1.1.0
2026-10-25 16:15:10 +03:30

73 lines
4.6 KiB
JavaScript

import assert from "node:assert/strict";
import { readFile, writeFile, mkdir, copyFile, mkdtemp, rm, access } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { pathToFileURL } from "node:url";
// Tests the actual distribution entry. This intentionally does not install
// optional peers; modular UI Kit base must work with React alone.
const scratch = await mkdtemp(path.join(tmpdir(), "uikit-provider-smoke-"));
try {
await writeFile(path.join(scratch, "package.json"), '{"type":"module"}');
for (const filename of ["provider.js", "provider-shared.js", "provider-permission.js", "provider-runtime.js", "core-runtime.js"]) {
await copyFile(`dist/${filename}`, path.join(scratch, filename));
}
const reactDir = path.join(scratch, "node_modules/react");
await mkdir(reactDir, { recursive: true });
await writeFile(path.join(reactDir, "package.json"), '{"name":"react","version":"18.0.0","type":"module","main":"index.js","exports":{".":"./index.js","./jsx-runtime":"./jsx-runtime.js"}}');
await writeFile(path.join(reactDir, "index.js"), `
export const Fragment = Symbol.for('mock.react.fragment');
export const createElement = (type, props, ...children) => ({type, props: {...props, ...(children.length ? {children: children.length === 1 ? children[0] : children} : {})}});
export const isValidElement = v => v && typeof v === 'object' && 'type' in v && 'props' in v;
export const Children = {forEach: (children, fn) => { const walk = v => { if (Array.isArray(v)) v.forEach(walk); else if (v !== undefined && v !== null) fn(v); }; walk(children); }};
export const createContext = (defaultValue) => ({defaultValue});
export const useContext = (ctx) => ctx.defaultValue;
export const useCallback = (fn) => fn;
export const useEffect = () => {};
export const useMemo = (fn) => fn();
export const useRef = (val) => ({current:val});
export const useState = (value) => [typeof value === 'function' ? value() : value, () => {}];
export default {Fragment, createElement, isValidElement, Children};
`);
await writeFile(path.join(reactDir, "jsx-runtime.js"), `
import {createElement, Fragment} from './index.js';
export {Fragment};
export const jsx = createElement;
export const jsxs = createElement;
`);
const { BiProvider } = await import(pathToFileURL(path.join(scratch, "provider.js")).href);
const { defineFeature } = await import(pathToFileURL(path.join(scratch, "provider-shared.js")).href);
const React = (await import(pathToFileURL(path.join(reactDir, "index.js")).href)).default;
const Foo = defineFeature("Foo", 50, function FooLayer(){});
const Bar = defineFeature("Bar", 10, function BarLayer(){});
const app = React.createElement("main", { id:"app" });
const tree = BiProvider({children: [React.createElement(Foo,{id:1}), app, React.createElement(Bar,{id:2})]});
assert.equal(tree.type.name, "BarLayer");
assert.equal(tree.props.children.type.name, "FooLayer");
assert.equal(tree.props.children.props.children.type, React.Fragment);
assert.throws(() => BiProvider({children: [React.createElement(Foo,{}), React.createElement(Foo,{})]}), /duplicate/);
assert.equal(BiProvider({children:app}).type, React.Fragment);
const Permission = await import(pathToFileURL(path.join(scratch, "provider-permission.js")).href);
const set = Permission.createPermissionSet([{name:'panel_access:*'},{name:'activity:read'}]);
assert.equal(Permission.checkPermission(set,'panel_access:dashboard'), true);
assert.equal(Permission.checkPermission(set,'activity'), true); // legacy prefix matching
assert.equal(Permission.checkPermission(set,'admin'), false);
assert.equal(typeof Permission.AdminGuard, 'function');
assert.equal(typeof Permission.PanelAccessGuard, 'function');
assert.equal(typeof Permission.getCachedPermissions, 'function');
const base = await readFile("dist/provider-shared.js", "utf8");
assert.match(base, /from ["']react["']/);
assert.doesNotMatch(base, /keycloak-js|@chakra-ui|axios|next-query-params|\.\/core\.js|provider-legacy/);
const legacy = await readFile("dist/provider-legacy.js", "utf8");
assert.match(legacy, /from ["']\.\/core\.js["']/);
const packageJson = JSON.parse(await readFile("package.json", "utf8"));
assert.ok(packageJson.exports["./provider/legacy"]);
assert.ok(!packageJson.exports["./provider/remote-ui"]);
await assert.rejects(access("dist/provider-remote-ui.js"));
console.log("Modular composition, independent imports, permission matching and strict legacy entry passed.");
} finally {
await rm(scratch, { recursive: true, force: true });
}