138 lines
4.6 KiB
JavaScript
138 lines
4.6 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
|
|
const { createQrCodeSvg, createStyledQrCodeSvg, createStyledQrCodeDataUrl, buildQrCodeContent } = require('../src/data/qr-code');
|
|
const { buildSlidePayload } = require('../src/data/slides');
|
|
|
|
test('createQrCodeSvg renders a qr svg for non-empty values', async () => {
|
|
const svg = await createQrCodeSvg('https://example.com');
|
|
|
|
assert.equal(typeof svg, 'string');
|
|
assert.match(svg, /<svg[\s>]/);
|
|
});
|
|
|
|
test('createStyledQrCodeSvg renders a styled qr svg for non-empty values', async () => {
|
|
const svg = await createStyledQrCodeSvg('https://example.com');
|
|
|
|
assert.equal(typeof svg, 'string');
|
|
assert.match(svg, /<svg[\s>]/);
|
|
});
|
|
|
|
test('createStyledQrCodeSvg keeps the qr background solid when gradient fields are provided', async () => {
|
|
const svg = await createStyledQrCodeSvg({
|
|
value: 'https://example.com',
|
|
qr_background_color_mode: 'gradient',
|
|
qr_background_gradient_type: 'radial',
|
|
qr_background_gradient_color_1: '#ff0000',
|
|
qr_background_gradient_color_2: '#0000ff'
|
|
});
|
|
|
|
assert.equal(typeof svg, 'string');
|
|
assert.match(svg, /<svg[\s>]/);
|
|
assert.doesNotMatch(svg, /<linearGradient|<radialGradient/i);
|
|
});
|
|
|
|
test('buildQrCodeContent forces qr background to single colour mode', async () => {
|
|
const content = await buildQrCodeContent({
|
|
value: 'https://example.com',
|
|
qr_background_color_mode: 'gradient',
|
|
qr_background_gradient_type: 'radial',
|
|
qr_background_gradient_color_1: '#ff0000',
|
|
qr_background_gradient_color_2: '#0000ff'
|
|
});
|
|
|
|
assert.equal(content.qr_background_color_mode, 'single');
|
|
assert.equal(content.qr_background_use_gradient, false);
|
|
});
|
|
|
|
test('createStyledQrCodeDataUrl renders a styled qr data url for non-empty values', async () => {
|
|
const dataUrl = await createStyledQrCodeDataUrl('https://example.com');
|
|
|
|
assert.equal(typeof dataUrl, 'string');
|
|
assert.match(dataUrl, /^data:image\/svg\+xml/);
|
|
});
|
|
|
|
test('buildQrCodeContent stores qr svg content for qr-code regions', async () => {
|
|
const content = await buildQrCodeContent('https://example.com');
|
|
|
|
assert.equal(content.type, 'qr-code');
|
|
assert.equal(content.value, 'https://example.com');
|
|
assert.equal(typeof content.qr_svg, 'string');
|
|
assert.match(content.qr_svg, /<svg[\s>]/);
|
|
assert.equal(typeof content.qr_preview, 'string');
|
|
assert.match(content.qr_preview, /^data:image\/svg\+xml/);
|
|
});
|
|
|
|
test('buildSlidePayload stores qr-code slide content from the submitted url', async () => {
|
|
const pool = {
|
|
async query(sql) {
|
|
if (sql.includes('FROM c_templates st')) {
|
|
return [[{ id: 7, name: 'Template 7', canvas_size_id: 1, canvas_size_width: 1920, canvas_size_height: 1080 }]];
|
|
}
|
|
|
|
if (sql.includes('FROM c_template_regions')) {
|
|
return [[{ id: 22, template_id: 7, region_key: 'qrRegion', region_type: 'qr-code', label: 'QR' }]];
|
|
}
|
|
|
|
if (sql.includes('FROM i_rss_feed_items')) {
|
|
return [[{ count: 2 }]];
|
|
}
|
|
|
|
return [[]];
|
|
}
|
|
};
|
|
|
|
const payload = await buildSlidePayload(pool, {
|
|
body: {
|
|
title: 'QR Slide',
|
|
template_id: '7',
|
|
region_qr_code_22: 'https://example.com'
|
|
},
|
|
files: []
|
|
}, null);
|
|
|
|
const content = JSON.parse(payload.contentJson);
|
|
assert.equal(content.qrRegion.type, 'qr-code');
|
|
assert.equal(content.qrRegion.value, 'https://example.com');
|
|
assert.equal(typeof content.qrRegion.qr_svg, 'string');
|
|
assert.match(content.qrRegion.qr_svg, /<svg[\s>]/);
|
|
assert.equal(typeof content.qrRegion.qr_preview, 'string');
|
|
assert.match(content.qrRegion.qr_preview, /^data:image\/svg\+xml/);
|
|
});
|
|
|
|
test('buildSlidePayload clamps rss item_number to the available item count', async () => {
|
|
const pool = {
|
|
async query(sql) {
|
|
if (sql.includes('FROM c_templates st')) {
|
|
return [[{ id: 8, name: 'Template 8', canvas_size_id: 1, canvas_size_width: 1920, canvas_size_height: 1080 }]];
|
|
}
|
|
|
|
if (sql.includes('FROM c_template_regions')) {
|
|
return [[{ id: 23, template_id: 8, region_key: 'rssRegion', region_type: 'rss', label: 'RSS' }]];
|
|
}
|
|
|
|
if (sql.includes('FROM i_rss_feed_items')) {
|
|
return [[{ count: 2 }]];
|
|
}
|
|
|
|
return [[]];
|
|
}
|
|
};
|
|
|
|
const payload = await buildSlidePayload(pool, {
|
|
body: {
|
|
title: 'RSS Slide',
|
|
template_id: '8',
|
|
region_rss_feed_id_23: '11',
|
|
region_rss_item_number_23: '5',
|
|
region_text_23: '<p>Hello</p>'
|
|
},
|
|
files: []
|
|
}, null);
|
|
|
|
const content = JSON.parse(payload.contentJson);
|
|
assert.equal(content.rssRegion.type, 'rss');
|
|
assert.equal(content.rssRegion.feed_id, 11);
|
|
assert.equal(content.rssRegion.item_number, 2);
|
|
});
|