/**
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { mkdtemp, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { dirname, join } from 'node:path';
import snippetAdapter from '../../scripts/docs/snippetadapter.mjs';
// Keep the fixtures independent of the commercial repository and its license configuration.
const paths = vi.hoisted( () => ( {
CKEDITOR5_ROOT_PATH: '/ckeditor5-snippet-fixture',
CKEDITOR5_COMMERCIAL_PATH: '/ckeditor5-snippet-fixture'
} ) );
vi.mock( '../../scripts/constants.mjs', () => paths );
describe( 'snippetAdapter asset loading', () => {
let directory;
let snippets;
let input;
let output;
beforeEach( async () => {
directory = await mkdtemp( join( tmpdir(), 'ckeditor5-snippet-adapter-' ) );
paths.CKEDITOR5_COMMERCIAL_PATH = directory;
input = join( directory, 'input', 'latest', '_snippets' );
output = join( directory, 'output', 'latest' );
snippets = new Set();
vi.spyOn( console, 'log' ).mockImplementation( () => {} );
await mkdir( join( input, 'examples' ), { recursive: true } );
await writeFile( join( input, 'examples', 'bootstrap-ui.html' ), page() );
await writeFile( join( input, 'examples', 'bootstrap-ui.js' ), '' );
} );
afterEach( async () => {
await rm( directory, { recursive: true, force: true } );
} );
it( 'adds premium assets only to documents whose emitted snippets use them, retaining core and CKBox assets', async () => {
await addSnippet( 'core', 'core', 'import "ckeditor5";' );
await addSnippet( 'premium', 'premium', 'import "ckeditor5-premium-features";' );
await addSnippet( 'mixed', 'core', 'import "ckeditor5";' );
await addSnippet( 'mixed', 'premium', 'import "ckeditor5-premium-features";' );
await addSnippet( 'html-only', 'html-only' );
await addSnippet( 'unused', 'unused', 'if (false) { import("ckeditor5-premium-features"); }' );
await snippetAdapter( snippets, {}, {
getSnippetPlaceholder: name => ``
} );
const ckboxDirectory = join( output, 'assets', 'ckbox' );
const getChunkImports = source => Array.from(
source.matchAll( /from\s*["'](\.\/chunk-[^"']+\.js)["']/g ),
match => match[ 1 ]
);
const ckboxChunks = getChunkImports( await readFile( join( ckboxDirectory, 'ckbox.js' ), 'utf8' ) );
const uploaderChunks = getChunkImports( await readFile( join( ckboxDirectory, 'ckboxWidget.js' ), 'utf8' ) );
const sharedChunks = ckboxChunks.filter( chunk => uploaderChunks.includes( chunk ) );
// The two module entry points must share their runtime, and every shared chunk must be available immediately.
expect( sharedChunks.length ).toBeGreaterThan( 0 );
for ( const chunk of sharedChunks ) {
expect( await readFile( join( ckboxDirectory, chunk ), 'utf8' ) ).not.toBe( '' );
}
for ( const name of [ 'core', 'premium', 'mixed', 'html-only', 'unused' ] ) {
const content = await readFile( join( output, `${ name }.html` ), 'utf8' );
const assetTags = content.match( /]*>|