first
This commit is contained in:
@@ -0,0 +1,237 @@
|
||||
'use strict';
|
||||
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert');
|
||||
|
||||
const LabelLayout = require('../src/layout');
|
||||
const HelveticaMetrics = require('../assets/helvetica-metrics').HelveticaMetrics;
|
||||
|
||||
function approx(actual, expected, tolerance, message) {
|
||||
assert.ok(
|
||||
Math.abs(actual - expected) <= tolerance,
|
||||
(message || 'nilai') + ': ' + actual + ' !== ' + expected + ' (toleransi ' + tolerance + ')'
|
||||
);
|
||||
}
|
||||
|
||||
test('ukuran halaman 100 x 150 mm dikonversi ke point dengan rumus 1 mm = 72/25.4 pt', () => {
|
||||
const layout = LabelLayout.create();
|
||||
approx(layout.page.widthPt, 283.4646, 0.001, 'lebar halaman (pt)');
|
||||
approx(layout.page.heightPt, 425.1969, 0.001, 'tinggi halaman (pt)');
|
||||
approx(layout.page.widthMm, 100, 0.0001, 'lebar halaman (mm)');
|
||||
approx(layout.page.heightMm, 150, 0.0001, 'tinggi halaman (mm)');
|
||||
});
|
||||
|
||||
test('layout bawaan: 3 kolom x 6 baris = 18 label per halaman', () => {
|
||||
const layout = LabelLayout.create();
|
||||
assert.strictEqual(layout.grid.columns, 3);
|
||||
assert.strictEqual(layout.grid.rows, 6);
|
||||
assert.strictEqual(layout.perPage, 18);
|
||||
});
|
||||
|
||||
test('ukuran sel label mengikuti format 100 x 150 mm 3 kolom x 6 baris', () => {
|
||||
const layout = LabelLayout.create();
|
||||
// (100 - 2*2.5 margin - 2*4.5 gap) / 3 kolom = 28.667 mm
|
||||
approx(layout.grid.cellWidthMm, 28.6667, 0.001, 'lebar sel');
|
||||
// (150 - 2*2.5 margin - 5*5 gap) / 6 baris = 20 mm
|
||||
approx(layout.grid.cellHeightMm, 20, 0.001, 'tinggi sel');
|
||||
});
|
||||
|
||||
test('jarak antar baris mengisi tinggi halaman (6 baris)', () => {
|
||||
const layout = LabelLayout.create();
|
||||
const row2 = layout.labelBox(3);
|
||||
approx(row2.yMm, 2.5 + 20 + 5, 0.0001, 'baris kedua');
|
||||
const row4 = layout.labelBox(9);
|
||||
approx(row4.yMm, 2.5 + 3 * 25, 0.0001, 'baris keempat');
|
||||
const row6 = layout.labelBox(15);
|
||||
approx(row6.yMm, 2.5 + 5 * 25, 0.0001, 'baris keenam');
|
||||
approx(row6.yMm + row6.heightMm, 147.5, 0.0001, 'tepi bawah baris keenam');
|
||||
});
|
||||
|
||||
test('teks dan QR rata tengah di dalam sel label', () => {
|
||||
const layout = LabelLayout.create();
|
||||
const box = layout.labelBox(0);
|
||||
const boxes = layout.contentBoxes(0);
|
||||
const centerYMm = box.yMm + box.heightMm / 2;
|
||||
approx(boxes.textBox.yMm, 2.5 + 1.0, 0.0001, 'teks memakai padding atas');
|
||||
approx(boxes.qrBox.yMm, 2.5 + (20 - 13.5) / 2, 0.0001, 'QR rata tengah');
|
||||
approx(boxes.qrBox.yMm + boxes.qrBox.heightMm / 2, centerYMm, 0.0001, 'pusat QR');
|
||||
assert.strictEqual(layout.config.verticalAlign, 'center');
|
||||
|
||||
const text = LabelLayout.composeText(
|
||||
{ main: 'ONT-1910', unique: '26-3BA3', snText: 'SN: - 12345678' },
|
||||
boxes.textBox,
|
||||
layout.config,
|
||||
HelveticaMetrics.measure
|
||||
);
|
||||
const first = text.blocks[0];
|
||||
const last = text.blocks[text.blocks.length - 1];
|
||||
approx(
|
||||
(first.yMm + last.yMm + last.heightMm) / 2,
|
||||
centerYMm,
|
||||
0.0001,
|
||||
'pusat blok teks'
|
||||
);
|
||||
});
|
||||
|
||||
test('QR tepat di pusat area barcode pada semua label dan halaman', () => {
|
||||
const layout = LabelLayout.create();
|
||||
const offsets = [];
|
||||
|
||||
// Dua halaman penuh: pola koordinat harus sama di halaman berikutnya.
|
||||
for (let i = 0; i < layout.perPage * 2; i++) {
|
||||
const box = layout.labelBox(i);
|
||||
const boxes = layout.contentBoxes(i);
|
||||
const area = boxes.barcodeArea;
|
||||
const qr = boxes.qrBox;
|
||||
|
||||
approx(qr.widthMm, layout.config.qrSizeMm, 1e-9, 'ukuran QR label ' + i);
|
||||
approx(qr.heightMm, qr.widthMm, 1e-9, 'QR harus persegi pada label ' + i);
|
||||
approx(qr.xMm + qr.widthMm / 2, area.xMm + area.widthMm / 2, 1e-9,
|
||||
'pusat X QR pada label ' + i);
|
||||
approx(qr.yMm + qr.heightMm / 2, area.yMm + area.heightMm / 2, 1e-9,
|
||||
'pusat Y QR pada label ' + i);
|
||||
|
||||
assert.ok(qr.xMm >= area.xMm - 1e-9 && qr.xMm + qr.widthMm <= area.xMm + area.widthMm + 1e-9,
|
||||
'QR di dalam area barcode (X) pada label ' + i);
|
||||
assert.ok(qr.yMm >= area.yMm - 1e-9 && qr.yMm + qr.heightMm <= area.yMm + area.heightMm + 1e-9,
|
||||
'QR di dalam area barcode (Y) pada label ' + i);
|
||||
assert.ok(qr.xMm >= box.xMm && qr.xMm + qr.widthMm <= box.xMm + box.widthMm + 1e-9,
|
||||
'QR di dalam label (X) pada label ' + i);
|
||||
assert.ok(qr.yMm >= box.yMm && qr.yMm + qr.heightMm <= box.yMm + box.heightMm + 1e-9,
|
||||
'QR di dalam label (Y) pada label ' + i);
|
||||
|
||||
offsets.push({
|
||||
x: qr.xMm - box.xMm,
|
||||
y: qr.yMm - box.yMm
|
||||
});
|
||||
}
|
||||
|
||||
// Satu fungsi layout untuk semua label: offset tidak boleh berbeda-beda.
|
||||
offsets.forEach(function (offset, index) {
|
||||
approx(offset.x, offsets[0].x, 1e-9, 'offset X konsisten pada label ' + index);
|
||||
approx(offset.y, offsets[0].y, 1e-9, 'offset Y konsisten pada label ' + index);
|
||||
});
|
||||
});
|
||||
|
||||
test('semua kotak label berada di dalam halaman dan tidak saling tumpang tindih', () => {
|
||||
const layout = LabelLayout.create();
|
||||
const boxes = [];
|
||||
for (let i = 0; i < layout.perPage; i++) {
|
||||
const box = layout.labelBox(i);
|
||||
assert.ok(box.xMm >= 0, 'x label di dalam halaman');
|
||||
assert.ok(box.yMm >= 0, 'y label di dalam halaman');
|
||||
assert.ok(box.xMm + box.widthMm <= layout.page.widthMm + 1e-9, 'label tidak melewati tepi kanan');
|
||||
assert.ok(box.yMm + box.heightMm <= layout.page.heightMm + 1e-9, 'label tidak melewati tepi bawah');
|
||||
boxes.push(box);
|
||||
}
|
||||
for (let a = 0; a < boxes.length; a++) {
|
||||
for (let b = a + 1; b < boxes.length; b++) {
|
||||
const A = boxes[a];
|
||||
const B = boxes[b];
|
||||
const overlap = A.xMm < B.xMm + B.widthMm - 1e-9 &&
|
||||
B.xMm < A.xMm + A.widthMm - 1e-9 &&
|
||||
A.yMm < B.yMm + B.heightMm - 1e-9 &&
|
||||
B.yMm < A.yMm + A.heightMm - 1e-9;
|
||||
assert.ok(!overlap, 'label ' + a + ' dan ' + b + ' tumpang tindih');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('area teks dan QR tidak saling menimpa dan tetap di dalam sel', () => {
|
||||
const layout = LabelLayout.create();
|
||||
for (let i = 0; i < layout.perPage; i++) {
|
||||
const box = layout.labelBox(i);
|
||||
const boxes = layout.contentBoxes(i);
|
||||
assert.ok(boxes.qrBox.xMm >= box.xMm, 'QR di dalam sel (kiri)');
|
||||
assert.ok(boxes.qrBox.xMm + boxes.qrBox.widthMm <= box.xMm + box.widthMm + 1e-9, 'QR di dalam sel (kanan)');
|
||||
assert.ok(boxes.textBox.xMm + boxes.textBox.widthMm <= boxes.qrBox.xMm + 1e-9, 'teks berhenti sebelum QR');
|
||||
assert.ok(boxes.textBox.widthMm > 0, 'area teks tidak nol');
|
||||
}
|
||||
});
|
||||
|
||||
test('jumlah baris dihitung otomatis dari tinggi label (roll 18 mm)', () => {
|
||||
const layout = LabelLayout.create({ labelHeightMm: 18, rows: 0 });
|
||||
// (150 - 5 + 5) / (18 + 5) = 6 baris
|
||||
assert.strictEqual(layout.grid.rows, 6);
|
||||
assert.strictEqual(layout.perPage, 18);
|
||||
approx(layout.grid.cellHeightMm, 18, 0.0001, 'tinggi sel label');
|
||||
});
|
||||
|
||||
test('jumlah baris dapat dipatok dan tidak boleh melebihi kapasitas halaman', () => {
|
||||
const duaBaris = LabelLayout.create({ rows: 2 });
|
||||
assert.strictEqual(duaBaris.grid.rows, 2);
|
||||
assert.strictEqual(duaBaris.perPage, 6);
|
||||
|
||||
const terlaluBanyak = LabelLayout.create({ labelHeightMm: 18, rows: 99 });
|
||||
assert.strictEqual(terlaluBanyak.grid.rows, 6, 'baris dibatasi oleh tinggi halaman');
|
||||
});
|
||||
|
||||
test('ukuran halaman dan jumlah baris mengikuti opsi', () => {
|
||||
const strip = LabelLayout.create({ pageHeightMm: 43.6, rows: 2, labelHeightMm: 0 });
|
||||
assert.strictEqual(strip.grid.rows, 2);
|
||||
assert.strictEqual(strip.perPage, 6);
|
||||
approx(strip.page.heightPt, 43.6 * 72 / 25.4, 0.001, 'tinggi halaman strip (pt)');
|
||||
});
|
||||
|
||||
test('jumlah halaman dihitung dari jumlah label', () => {
|
||||
const layout = LabelLayout.create();
|
||||
assert.strictEqual(LabelLayout.pageCount(layout, 1), 1);
|
||||
assert.strictEqual(LabelLayout.pageCount(layout, 18), 1);
|
||||
assert.strictEqual(LabelLayout.pageCount(layout, 19), 2);
|
||||
assert.strictEqual(LabelLayout.pageCount(layout, 36), 2);
|
||||
assert.strictEqual(LabelLayout.pageCount(layout, 37), 3);
|
||||
});
|
||||
|
||||
test('ukuran font turun otomatis agar teks tidak keluar dari area teks', () => {
|
||||
const layout = LabelLayout.create();
|
||||
const boxes = layout.contentBoxes(0);
|
||||
const measure = function (text, sizeMm) {
|
||||
return text.length * sizeMm * 0.6;
|
||||
};
|
||||
const long = {
|
||||
main: 'KODE-UTAMA-SANGAT-PANJANG-1234567890',
|
||||
unique: 'KODE-UNIK-YANG-PANJANG-ABCDEF',
|
||||
snText: 'SN: - 12345678901234567890'
|
||||
};
|
||||
const text = LabelLayout.composeText(long, boxes.textBox, layout.config, measure);
|
||||
assert.ok(text.blocks.length >= 3, 'semua baris tetap digambar');
|
||||
text.blocks.forEach(function (block) {
|
||||
assert.ok(
|
||||
block.xMm + block.sizeMm * 0.6 * block.text.length <= boxes.textBox.xMm + boxes.textBox.widthMm + 1e-6,
|
||||
'baris "' + block.text + '" melebihi lebar area teks'
|
||||
);
|
||||
});
|
||||
const last = text.blocks[text.blocks.length - 1];
|
||||
assert.ok(last.yMm + last.heightMm <= boxes.textBox.yMm + boxes.textBox.heightMm + 1e-6, 'blok teks keluar dari area teks');
|
||||
});
|
||||
|
||||
test('teks contoh ONT-0910 tetap dua baris, tidak terpecah di tengah kode', () => {
|
||||
const layout = LabelLayout.create();
|
||||
const boxes = layout.contentBoxes(0);
|
||||
const text = LabelLayout.composeText(
|
||||
{ main: 'ONT-0910', unique: '26-5BD9', snText: 'SN: - 12345678' },
|
||||
boxes.textBox,
|
||||
layout.config,
|
||||
HelveticaMetrics.measure
|
||||
);
|
||||
assert.deepStrictEqual(
|
||||
text.blocks.map(function (block) { return block.text; }),
|
||||
['ONT-0910', '26-5BD9', 'SN: - 12345678']
|
||||
);
|
||||
});
|
||||
|
||||
test('teks panjang dipecah baris, tidak dipotong', () => {
|
||||
const layout = LabelLayout.create();
|
||||
const boxes = layout.contentBoxes(0);
|
||||
const measure = function (text, sizeMm) {
|
||||
return text.length * sizeMm * 0.6;
|
||||
};
|
||||
const text = LabelLayout.composeText(
|
||||
{ main: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', unique: '', snText: 'SN: - 1' },
|
||||
boxes.textBox,
|
||||
layout.config,
|
||||
measure
|
||||
);
|
||||
const joined = text.blocks.map(function (b) { return b.text; }).join(' ').replace(/\s+/g, '');
|
||||
assert.strictEqual(joined, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789SN:-1');
|
||||
});
|
||||
Reference in New Issue
Block a user