uikit/tests/watermark-v6.test.mjs
Mohamadzadeh ca1ad036f4
All checks were successful
Build and Deploy Next.js + Nginx Docker Image / build-and-deploy (push) Successful in 53s
Build and Deploy Next.js + Nginx Docker Image / deploy (push) Successful in 14s
uikit-v1.8.0-watermark-wmv6-numeric5-complete
2026-11-14 14:38:37 +03:30

69 lines
4.2 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { resolveWatermarkUserId } from '../src/watermark/identity.js';
import { normalizeNumericId, short8Packet, encodeShort8Bits } from '../src/watermark/short8.js';
import { v6PayloadBits, v6CodedSymbols, encodeV6Bits, createV6Tile, V6_PAYLOAD_BITS, V6_FEC_SYMBOLS, V6_PILOT_INDICES, V6_DATA_INDICES, V6_PILOTS } from '../src/watermark/v6.js';
import { createWatermarkTile } from '../src/watermark/render.js';
const uuid = 'ccea506f-d984-4044-824b-62bf0c3dcf86';
const record = { id: uuid, attributes: { numeric_id: ['00042'] } };
test('WM-v6/v5 read authenticated strict Numeric5 from User API; no mock or UUID fallback', () => {
assert.equal(resolveWatermarkUserId(record,6),'00042');
assert.equal(resolveWatermarkUserId(record,5),'00042');
for (const version of [2,3,4]) assert.equal(resolveWatermarkUserId(record,version),uuid);
for (const malformed of [undefined, null, '1234', '123456', 42, '۱۲۳۴۵', ' 00042', '00a42']) {
assert.throws(()=>normalizeNumericId(malformed), TypeError);
assert.throws(()=>resolveWatermarkUserId({id:uuid, attributes:{numeric_id:[malformed]}}, 6), TypeError);
}
assert.throws(()=>resolveWatermarkUserId({id:uuid}, 6), TypeError);
assert.throws(()=>resolveWatermarkUserId({id:uuid,attributes:{numeric_id:'00042'}},6), TypeError);
});
test('WM-v6 protocol payload, convolutional symbols and independent pilot test vectors match SDK', () => {
assert.equal(V6_PAYLOAD_BITS,37);
assert.equal(V6_FEC_SYMBOLS,86);
assert.equal(V6_PILOT_INDICES.length,43);
assert.equal(V6_DATA_INDICES.length,297);
assert.equal(v6PayloadBits('65498').join(''),'1101011111111110110101101011100101011');
assert.equal(v6CodedSymbols('65498').join(''),'11100100011000101101001111111100101111011010000110101000100011011110010011010111010111');
assert.equal(V6_PILOTS.join(''),'1101010100100110001010011111110001010101110');
const all=encodeV6Bits('65498');
assert.equal(all.length,340);
assert.equal(all.filter(bit=>bit===undefined).length,0);
assert.deepEqual(V6_PILOT_INDICES.map(i=>all[i]),V6_PILOTS);
assert.equal(short8Packet('65498').length,8);
assert.equal(encodeShort8Bits('65498').length,340);
assert.equal(Buffer.from(short8Packet('65498')).toString('hex'),'574d0000ffdaef1f');
});
function fakeCanvas() {
globalThis.document = {createElement(tag){ assert.equal(tag,'canvas'); return {width:0,height:0,getContext(){return {createImageData(w,h){return {data:new Uint8ClampedArray(w*h*4)};},putImageData(){}, drawImage(){},set imageSmoothingEnabled(_) {}}}};}};
}
test('WM-v6 tile has 240x204 geometry and allows SDK spatial profiles', () => {
fakeCanvas();
for (const profile of ['legacy','webp-research','multi-scale']) {
const canvas=createV6Tile('65498',{opacity:0.014,spatialProfile:profile});
assert.deepEqual([canvas.width,canvas.height],[240,204]);
}
assert.deepEqual([createWatermarkTile('65498',{version:6}).width,createWatermarkTile('65498',{version:6}).height],[240,204]);
assert.deepEqual([createWatermarkTile('65498',{version:5}).width,createWatermarkTile('65498',{version:5}).height],[240,204]);
assert.throws(()=>createV6Tile('1234'), TypeError);
assert.throws(()=>createV6Tile('65498',{opacity:0.09}),RangeError);
assert.throws(()=>createV6Tile('65498',{spatialProfile:'no'}),RangeError);
});
test('UIKit default version 6; version switch re-fetches API identity, preserving token and session handling', () => {
const component = readFileSync(new URL('../src/watermark/ForensicWatermark.jsx',import.meta.url),'utf8');
assert.match(component,/version\s*=\s*6/);
assert.match(component,/resolveWatermarkUserId\(data, version\)/);
assert.match(component,/\[enabled, userUrl, sessionKey, version\]/);
assert.match(component,/identity\.version === version/);
assert.match(component,/createWatermarkTile\(userId, \{ opacity, version, spatialProfile \}\)/);
assert.doesNotMatch(component,/Math\.random\(|mockUserId|DEFAULT_SHORT_ID/);
const types=readFileSync(new URL('../src/watermark/index.d.ts',import.meta.url),'utf8');
assert.match(types,/version\?: 2 \| 3 \| 4 \| 5 \| 6/);
assert.match(types,/spatialProfile\?/);
});