77 lines
4.4 KiB
JavaScript
77 lines
4.4 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { test } from 'node:test';
|
|
import { mkdtemp, mkdir, cp, writeFile, rm, readFile } from 'node:fs/promises';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { pathToFileURL } from 'node:url';
|
|
|
|
const directory = await mkdtemp(path.join(os.tmpdir(), 'uikit-watermark-'));
|
|
try {
|
|
await mkdir(path.join(directory, 'chunks'), {recursive:true});
|
|
await writeFile(path.join(directory,'package.json'), '{"type":"module"}');
|
|
for (const f of ['provider.js','watermark.js','chunks/_runtime-B6XIrDzf.js','chunks/_shared-DHh79jO9.js']) {
|
|
await cp(path.join('dist',f), path.join(directory,f));
|
|
}
|
|
const fake = path.join(directory,'node_modules/react');
|
|
await mkdir(fake, {recursive:true});
|
|
await writeFile(path.join(fake,'package.json'),'{"type":"module","exports":{".":"./index.js"}}');
|
|
await writeFile(path.join(fake,'index.js'), `
|
|
export const Fragment = Symbol.for('test.fragment');
|
|
export function createContext(defaultValue){return {value:defaultValue};}
|
|
export function useContext(context){return context.value;}
|
|
export const Children = {forEach(children,fn){const visit=child=>{if(Array.isArray(child))child.forEach(visit);else if(child!=null)fn(child)};visit(children)}};
|
|
export function createElement(type, props, ...args){return {type,props:{...props,...(args.length?{children:args.length===1?args[0]:args}:{})}};}
|
|
export function isValidElement(element){return !!element && typeof element==='object' && 'type' in element && 'props' in element;}
|
|
export function useRef(value){return {current:value};}
|
|
export function useCallback(fn){return fn;}
|
|
export function useState(value){return [value,()=>{}];}
|
|
export function useEffect(){}
|
|
export default { Fragment, createContext, useContext, Children, createElement, isValidElement, useRef, useCallback, useState, useEffect };
|
|
`);
|
|
const uri = p => pathToFileURL(path.join(directory,p)).href;
|
|
const [{BiProvider},{Watermark,ForensicWatermark},react,{K:context}] = await Promise.all([
|
|
import(uri('provider.js')),import(uri('watermark.js')),
|
|
import(uri('node_modules/react/index.js')),
|
|
import(uri('chunks/_runtime-B6XIrDzf.js')),
|
|
]);
|
|
const marker=Symbol.for('uikit.provider.feature.v1');
|
|
test('Watermark is a BiProvider marker, optional and ordered after Keycloak', () => {
|
|
assert.equal(Watermark[marker].name,'Watermark');
|
|
assert.equal(Watermark[marker].priority,65);
|
|
assert.equal(typeof ForensicWatermark,'function');
|
|
function Keycloak(){};
|
|
Object.defineProperty(Keycloak,marker,{value:{name:'Keycloak',priority:40,Provider:({children})=>children}});
|
|
const root=BiProvider({children:[react.createElement(Watermark,{}),react.createElement(Keycloak,{}),react.createElement('p',{})]});
|
|
assert.equal(root.type,Keycloak[marker].Provider);
|
|
assert.equal(root.props.children.type,Watermark[marker].Provider);
|
|
assert.throws(()=>BiProvider({children:[react.createElement(Watermark,{}),react.createElement(Watermark,{})]}),/duplicate/);
|
|
});
|
|
test('Watermark shares the same Keycloak context and can run without it', async () => {
|
|
const markerLayer = Watermark[marker].Provider;
|
|
let external=0;
|
|
const getToken=()=>{external++;return 'external-token'};
|
|
context.value={authenticated:true, token:'cached', instance:{token:'keycloak-token',isTokenExpired:()=>false,tokenParsed:{sub:'account-A'}}};
|
|
const out=markerLayer({children:'app',getToken});
|
|
assert.equal(out.type,react.Fragment);
|
|
const internal=out.props.children[0];
|
|
assert.equal(internal.type,ForensicWatermark);
|
|
assert.equal(internal.props.sessionKey,'account-A');
|
|
assert.equal(await internal.props.getAccessToken(),'keycloak-token');
|
|
assert.equal(external,0);
|
|
context.value=null;
|
|
const standalone=markerLayer({children:'app',getToken});
|
|
assert.equal(await standalone.props.children[0].props.getAccessToken(),'external-token');
|
|
assert.equal(external,1);
|
|
assert.equal(standalone.props.children[1],'app');
|
|
});
|
|
test('dist entry does not import application dependencies', async () => {
|
|
const dist=await readFile('dist/watermark.js','utf8');
|
|
assert.match(dist,/^"use client";/);
|
|
assert.doesNotMatch(dist,/from ['"](?:@chakra-ui|axios|keycloak-js|next|swr|react-router)/);
|
|
assert.match(dist,/data-forensic-watermark/);
|
|
});
|
|
} finally {
|
|
// Node's test runner finishes synchronous tests before returning here.
|
|
await rm(directory,{recursive:true,force:true});
|
|
}
|