58 lines
3.5 KiB
JavaScript
58 lines
3.5 KiB
JavaScript
import test from 'node:test';
|
||
import assert from 'node:assert/strict';
|
||
import { mkdtemp, cp, readFile, writeFile, rm } from 'node:fs/promises';
|
||
import { tmpdir } from 'node:os';
|
||
import { join } from 'node:path';
|
||
import { execFileSync } from 'node:child_process';
|
||
|
||
// Exercise the real renderer and both supported templates in an isolated project.
|
||
test('image opening is opt-in per carousel and survives ordinary model rendering', async t => {
|
||
const root = await mkdtemp(join(tmpdir(), 'nodedc-carousel-'));
|
||
t.after(() => rm(root, { recursive: true, force: true }));
|
||
for (const path of ['tools', 'templates', 'content', 'assets/uploads/optimized']) await cp(new URL('../'+path, import.meta.url), join(root,path), {recursive:true});
|
||
const modelPath = join(root, 'content/pages/home.json');
|
||
const page = JSON.parse(await readFile(modelPath, 'utf8'));
|
||
const galleries = page.regions.flatMap(r=>r.blocks).filter(b=>b.content?.sliderItems);
|
||
assert.equal(galleries.length, 4);
|
||
const render = async () => {
|
||
await writeFile(modelPath, JSON.stringify(page));
|
||
execFileSync(process.execPath, ['tools/render-home.mjs'], {cwd:root});
|
||
return await readFile(join(root, 'index.html'),'utf8');
|
||
};
|
||
const count = (html, pattern) => [...html.matchAll(pattern)].length;
|
||
const baseline = await render();
|
||
assert.equal(count(baseline, /— открыть изображение/g), 0);
|
||
const mediaCount = count(baseline, /class="carousel-media"/g);
|
||
const videoCount = count(baseline, /<video /g);
|
||
galleries[0].content.openImagesInFullscreen = true;
|
||
galleries[1].template = 'project-composition.html';
|
||
galleries[1].content.openImagesInFullscreen = true;
|
||
const enabled = await render();
|
||
const expected = galleries.slice(0,2).flatMap(b=>b.content.sliderItems).filter(i=>i.enabled && i.imageSrc).length;
|
||
assert.equal(count(enabled, /— открыть изображение/g), expected);
|
||
assert.equal(count(enabled, /class="carousel-media"/g), mediaCount);
|
||
assert.equal(count(enabled, /<video /g), videoCount);
|
||
delete galleries[0].content.openImagesInFullscreen;
|
||
galleries[1].content.openImagesInFullscreen = 'false';
|
||
assert.equal(count(await render(), /— открыть изображение/g), 0);
|
||
galleries.forEach(b=>b.content.openImagesInFullscreen=false);
|
||
assert.equal(count(await render(), /— открыть изображение/g), 0);
|
||
// Details remain independent of fullscreen media, including after a CMS template change.
|
||
for (const template of ['project-video-detail.html', 'project-composition.html']) {
|
||
const gallery = galleries.find(b=>b.id==='applications');
|
||
gallery.template = template;
|
||
gallery.content.sliderItems[0].detailsHref = '/knowledge/applications/hidden-works-passport/';
|
||
gallery.content.sliderItems[0].detailsLabel = 'Подробно <о решении>';
|
||
gallery.content.sliderItems[1].detailsHref = 'javascript:alert(1)';
|
||
gallery.content.sliderItems[2].detailsHref = 'java\nscript:alert(1)';
|
||
gallery.content.sliderItems[3].detailsHref = 'data:text/html,<script>alert(1)</script>';
|
||
const details = await render();
|
||
assert.match(details, /href="\/knowledge\/applications\/hidden-works-passport\/"/);
|
||
assert.match(details, /Подробно <о решении>/);
|
||
assert.doesNotMatch(details, /href="(?:javascript:|data:text\/html)/);
|
||
assert.equal(count(details, /— открыть изображение/g), 0);
|
||
assert.equal(count(details, /class="carousel-media"/g), mediaCount);
|
||
assert.equal(count(details, /<video /g), videoCount);
|
||
}
|
||
});
|