84 lines
3.8 KiB
JavaScript
84 lines
3.8 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { readFile } from 'node:fs/promises';
|
|
import { encodeV3Bits, V3_CHIPS, v3ChipPosition, v3ChipRects, V3_WIDTH, V3_HEIGHT } from '../src/watermark/v3.js';
|
|
import { createWatermarkTile } from '../src/watermark/render.js';
|
|
import { encodeUserId } from '../src/watermark/codec.js';
|
|
|
|
const UUID = 'ccea506f-d984-4044-824b-62bf0c3dcf86';
|
|
function withFakeCanvas(callback) {
|
|
const original = globalThis.document;
|
|
const tiles = [];
|
|
globalThis.document = {
|
|
createElement(type) {
|
|
assert.equal(type, 'canvas');
|
|
const tile = { width: 0, height: 0, rects: [], getContext(kind) {
|
|
assert.equal(kind, '2d');
|
|
const context = { fillStyle: '', fillRect(x,y,w,h) {
|
|
tile.rects.push({ x,y,w,h,color:context.fillStyle });
|
|
}};
|
|
return context;
|
|
}};
|
|
tiles.push(tile);
|
|
return tile;
|
|
}
|
|
};
|
|
try { callback(tiles); }
|
|
finally { globalThis.document = original; }
|
|
}
|
|
|
|
test('WM-v3 exactly 340 binary chips and the interleaver covers all cells', () => {
|
|
const bits = encodeV3Bits(UUID);
|
|
assert.equal(bits.length, 340);
|
|
assert.ok(bits.every(bit => bit === 0 || bit === 1));
|
|
assert.equal(new Set(Array.from({length:V3_CHIPS},(_,i)=>v3ChipPosition(i))).size, V3_CHIPS);
|
|
for (let chip=0; chip<V3_CHIPS; chip++) {
|
|
for (const r of Object.values(v3ChipRects(v3ChipPosition(chip)))) {
|
|
assert.ok(r.x >= 0 && r.x+r.w <= V3_WIDTH && r.y >= 0 && r.y+r.h <= V3_HEIGHT);
|
|
}
|
|
}
|
|
});
|
|
|
|
test('WM-v3 and WM-v2 preserve shape; SDK new default opacity is 0.014', () => {
|
|
withFakeCanvas(() => {
|
|
const v3 = createWatermarkTile(UUID,{version:3});
|
|
assert.deepEqual([v3.width,v3.height,v3.rects.length],[240,204,680]);
|
|
assert.ok(v3.rects.every(r=>r.color.endsWith(',0.014)')));
|
|
const v2 = createWatermarkTile(UUID,{version:2});
|
|
assert.deepEqual([v2.width,v2.height,v2.rects.length],[240,200,960]);
|
|
assert.ok(v2.rects.every(r=>r.color.endsWith(',0.014)')));
|
|
const prior = createWatermarkTile(UUID,{version:3,opacity:0.03});
|
|
assert.ok(prior.rects.every(r=>r.color.endsWith(',0.03)')));
|
|
assert.throws(()=>createWatermarkTile(UUID,{version:5}),/Short ID must be five or eight ASCII decimal digits/);
|
|
assert.throws(()=>createWatermarkTile(UUID,{version:7}),/Unsupported watermark version/);
|
|
assert.throws(()=>createWatermarkTile(UUID,{version:3,opacity:0.09}),/opacity/);
|
|
});
|
|
});
|
|
|
|
test('legacy payload preserved; v3.1, v3 bundled; no decoder bundled', async () => {
|
|
assert.equal(encodeUserId(UUID).length,160);
|
|
const [dist,reactSource,types,distTypes,packageJson] = await Promise.all([
|
|
readFile('dist/watermark.js','utf8'),
|
|
readFile('src/watermark/ForensicWatermark.jsx','utf8'),
|
|
readFile('src/watermark/index.d.ts','utf8'),
|
|
readFile('dist/watermark.d.ts','utf8'),
|
|
readFile('package.json','utf8'),
|
|
]);
|
|
assert.match(dist,/function createV3Tile/);
|
|
assert.match(dist,/function createV31Tile/);
|
|
assert.match(dist,/version = 6/);
|
|
assert.match(dist,/function createV6Tile/);
|
|
assert.match(dist,/function createShort8Tile/);
|
|
assert.match(dist,/version === 4/);
|
|
assert.match(dist,/version === 3/);
|
|
assert.match(dist,/V31_DEFAULT_OPACITY = 0\.014/);
|
|
assert.doesNotMatch(dist,/from ["']\.\/v3\.js["']/);
|
|
assert.match(reactSource,/createWatermarkTile\(userId, \{ opacity, version, spatialProfile \}\)/);
|
|
assert.match(reactSource,/\[enabled, userId, opacity, spatialProfile, version, sessionKey\]/);
|
|
assert.match(types,/version\?: 2 \| 3 \| 4 \| 5 \| 6/);
|
|
assert.match(distTypes,/version\?: 2 \| 3 \| 4 \| 5 \| 6/);
|
|
// WM-v3 remains bundled in every release from v1.5.0 onward.
|
|
const [major, minor, patch] = JSON.parse(packageJson).version.split('.').map(Number);
|
|
assert.ok(major > 1 || (major === 1 && minor >= 7), 'WM-v3.1 requires UI Kit >= 1.7.0');
|
|
});
|