images-watermark
Version:
๐ A powerful, fast, and user-friendly Node.js library for adding professional watermarks to images. Protect your content with text watermarks, logo overlays, or both! Built with Sharp for lightning-fast processing, featuring intelligent caching, flexible
139 lines (125 loc) โข 5.5 kB
JavaScript
const Watermark = require('./index');
const assert = require('assert');
/**
* Robust test suite for the Images Watermark package
* Covers logic, cache, and access control
*/
async function runTests() {
console.log('๐งช Running Images Watermark Tests\n');
let passedTests = 0;
let totalTests = 0;
async function test(name, testFunction) {
totalTests++;
process.stdout.write(`Test ${totalTests}: ${name} ... `);
try {
await testFunction();
console.log(`โ
PASSED`);
passedTests++;
} catch (error) {
console.log(`โ FAILED: ${error.message}`);
}
}
// Test 1: normalizeReferrer
test('normalizeReferrer strips protocol and trailing slash', () => {
assert.strictEqual(Watermark.normalizeReferrer('https://example.com/'), 'example.com');
assert.strictEqual(Watermark.normalizeReferrer('http://test.com'), 'test.com');
assert.strictEqual(Watermark.normalizeReferrer(null), '');
});
// Test 2: shouldApplyWatermark logic
test('shouldApplyWatermark returns true for external referrer, false for allowed', () => {
const allowedReferrers = ['https://mywebsite.com'];
// Allowed referrer (should NOT apply watermark)
const headers1 = {
referer: 'https://mywebsite.com',
'user-agent': 'Mozilla/5.0...',
cookie: 'session=abc123'
};
assert.strictEqual(Watermark.shouldApplyWatermark(headers1, allowedReferrers), false);
// External referrer (should apply watermark)
const headers2 = {
referer: 'https://external-site.com',
'user-agent': 'Mozilla/5.0...',
cookie: 'session=abc123'
};
assert.strictEqual(Watermark.shouldApplyWatermark(headers2, allowedReferrers), true);
// undici user-agent (should NOT apply watermark)
const headers3 = {
referer: 'https://external-site.com',
'user-agent': 'undici',
cookie: 'session=abc123'
};
assert.strictEqual(Watermark.shouldApplyWatermark(headers3, allowedReferrers), false);
});
// Test 3: validateOptions
test('validateOptions throws for missing/invalid options', () => {
// Valid options
const validOptions = {
imagePath: './test-image.jpg',
allowedReferrers: ['https://mywebsite.com'],
headers: { referer: 'https://mywebsite.com' }
};
assert.doesNotThrow(() => Watermark.validateOptions(validOptions));
// Missing imagePath
const invalidOptions1 = {
allowedReferrers: ['https://mywebsite.com'],
headers: { referer: 'https://mywebsite.com' }
};
assert.throws(() => Watermark.validateOptions(invalidOptions1), /Image path is required/);
// Empty allowedReferrers
const invalidOptions2 = {
imagePath: './test-image.jpg',
allowedReferrers: [],
headers: { referer: 'https://mywebsite.com' }
};
assert.throws(() => Watermark.validateOptions(invalidOptions2), /allowed referrer domains/);
// Missing headers
const invalidOptions3 = {
imagePath: './test-image.jpg',
allowedReferrers: ['https://mywebsite.com']
};
assert.throws(() => Watermark.validateOptions(invalidOptions3), /headers are required/);
});
// Test 4: Cache management
test('Cache management functions work', () => {
const stats = Watermark.getCacheStats();
assert.strictEqual(typeof stats, 'object');
assert.strictEqual(typeof stats.hits, 'number');
assert.strictEqual(typeof stats.misses, 'number');
assert.doesNotThrow(() => Watermark.clearCache());
});
// Test 5: generateRepeatingTextWatermark returns SVG string
test('generateRepeatingTextWatermark returns SVG with correct content', () => {
const svg = Watermark.generateRepeatingTextWatermark(
'TEST', 800, 600, 'black', '0.5', 'bold', '5', 'Arial'
);
assert.strictEqual(typeof svg, 'string');
assert(svg.includes('<svg'));
assert(svg.includes('TEST'));
assert(svg.includes('width="800"'));
assert(svg.includes('height="600"'));
});
// Test 6: Cache key uniqueness for generateRepeatingTextWatermark
test('generateRepeatingTextWatermark uses cache for identical params', () => {
const svg1 = Watermark.generateRepeatingTextWatermark('TEST', 800, 600);
const svg2 = Watermark.generateRepeatingTextWatermark('TEST', 800, 600);
assert.strictEqual(svg1, svg2, 'Should use cache for identical params');
const svg3 = Watermark.generateRepeatingTextWatermark('TEST', 800, 600, 'red');
assert.notStrictEqual(svg1, svg3, 'Different params should not use same cache');
});
// (Optional) More tests for image processing can be added with mocks if needed
// Summary
setTimeout(() => {
console.log(`\n๐ Test Results: ${passedTests}/${totalTests} tests passed`);
if (passedTests === totalTests) {
console.log('๐ All tests passed!');
process.exit(0);
} else {
console.log('โ Some tests failed');
process.exit(1);
}
}, 100); // Allow async tests to finish
}
if (require.main === module) {
runTests();
}
module.exports = { runTests };