50 lines
1.9 KiB
JavaScript
50 lines
1.9 KiB
JavaScript
'use strict';
|
|
|
|
const test = require('node:test');
|
|
const assert = require('node:assert');
|
|
|
|
const LabelSvg = require('../src/svg');
|
|
const { createMatrix } = require('../src/qr');
|
|
|
|
function countRuns(matrix) {
|
|
let runs = 0;
|
|
for (let r = 0; r < matrix.size; r++) {
|
|
let c = 0;
|
|
while (c < matrix.size) {
|
|
if (matrix.get(r, c)) {
|
|
runs++;
|
|
while (c < matrix.size && matrix.get(r, c)) { c++; }
|
|
} else {
|
|
c++;
|
|
}
|
|
}
|
|
}
|
|
return runs;
|
|
}
|
|
|
|
test('SVG QR hanya berisi modul yang gelap (tidak ada kotak penuh)', () => {
|
|
const matrix = createMatrix('ONT-0910 26-5BD9', 'M');
|
|
const svg = LabelSvg.matrixToSvg(matrix, { quietZone: 4, sizeMm: 13.5 });
|
|
|
|
assert.ok(svg.indexOf('<svg') === 0, 'SVG valid');
|
|
assert.ok(svg.indexOf('viewBox="0 0 ' + (matrix.size + 8) + ' ' + (matrix.size + 8) + '"') > 0, 'viewBox menyertakan quiet zone');
|
|
assert.ok(svg.indexOf('width="13.5mm"') > 0, 'ukuran fisik QR ikut ditulis');
|
|
|
|
const path = svg.match(/<path d="([^"]+)"/)[1];
|
|
const commands = path.split('M').length - 1;
|
|
assert.strictEqual(commands, countRuns(matrix), 'jumlah segmen path harus sama dengan jumlah deretan modul gelap');
|
|
|
|
// Modul pojok kiri atas pada QR selalu gelap, jadi path harus dimulai di sana.
|
|
const first = path.match(/^M(\d+) (\d+)/);
|
|
assert.strictEqual(Number(first[1]), 4, 'path dimulai pada kolom 0 + quiet zone');
|
|
assert.strictEqual(Number(first[2]), 4, 'path dimulai pada baris 0 + quiet zone');
|
|
assert.ok(matrix.get(0, 0), 'modul pojok kiri atas harus gelap');
|
|
});
|
|
|
|
test('SVG memuat quiet zone (area putih di sekitar QR)', () => {
|
|
const matrix = createMatrix('ONT-0910 26-5BD9', 'M');
|
|
const svg = LabelSvg.matrixToSvg(matrix, { quietZone: 4 });
|
|
assert.ok(svg.indexOf('<rect x="0" y="0"') > 0, 'latar putih quiet zone digambar');
|
|
assert.ok(svg.indexOf('shape-rendering="crispEdges"') > 0, 'modul digambar tanpa interpolasi');
|
|
});
|