html2canvas-pro
Version:
Screenshots with JavaScript. Next generation!
158 lines • 6.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const assert_1 = require("assert");
const cache_storage_1 = require("../cache-storage");
const context_1 = require("../context");
const bounds_1 = require("../../css/layout/bounds");
const config_1 = require("../../config");
describe('Cache LRU', () => {
let context;
let cache;
beforeEach(() => {
const mockWindow = {
document: {
createElement: (_name) => {
let _href = '';
return {
set href(value) {
_href = value;
},
get href() {
return _href;
},
get protocol() {
return 'http:';
},
get hostname() {
return 'localhost';
},
get port() {
return '';
}
};
}
},
location: { href: 'http://localhost/' }
};
const config = new config_1.Html2CanvasConfig({ window: mockWindow });
context = new context_1.Context({
logging: false,
imageTimeout: 15000,
useCORS: false,
allowTaint: false
}, new bounds_1.Bounds(0, 0, 800, 600), config);
const options = {
imageTimeout: 15000,
useCORS: false,
allowTaint: false,
maxCacheSize: 3 // Small size for testing
};
cache = new cache_storage_1.Cache(context, options);
});
it('should enforce maximum cache size', () => {
(0, assert_1.strictEqual)(cache.getMaxSize(), 3);
(0, assert_1.strictEqual)(cache.size(), 0);
});
it('should add images to cache', () => {
const blob1 = 'blob:http://localhost/image1';
const blob2 = 'blob:http://localhost/image2';
cache.addImage(blob1);
(0, assert_1.strictEqual)(cache.size(), 1);
cache.addImage(blob2);
(0, assert_1.strictEqual)(cache.size(), 2);
});
it('should not exceed max cache size', () => {
const blobs = [
'blob:http://localhost/image1',
'blob:http://localhost/image2',
'blob:http://localhost/image3',
'blob:http://localhost/image4' // This should trigger eviction
];
blobs.forEach((blob) => cache.addImage(blob));
// Should not exceed max size (3)
(0, assert_1.strictEqual)(cache.size(), 3);
});
it('should evict least recently used entry when full', async () => {
const blob1 = 'blob:http://localhost/image1';
const blob2 = 'blob:http://localhost/image2';
const blob3 = 'blob:http://localhost/image3';
const blob4 = 'blob:http://localhost/image4';
// Add 3 images (fill cache)
cache.addImage(blob1);
await new Promise((resolve) => setTimeout(resolve, 10));
cache.addImage(blob2);
await new Promise((resolve) => setTimeout(resolve, 10));
cache.addImage(blob3);
await new Promise((resolve) => setTimeout(resolve, 10));
(0, assert_1.strictEqual)(cache.size(), 3);
// Access blob2 and blob3 (update their access time)
cache.match(blob2);
await new Promise((resolve) => setTimeout(resolve, 10));
cache.match(blob3);
await new Promise((resolve) => setTimeout(resolve, 10));
// Add blob4 - should evict blob1 (oldest, never accessed)
cache.addImage(blob4);
(0, assert_1.strictEqual)(cache.size(), 3);
// blob1 should be evicted
(0, assert_1.strictEqual)(cache.match(blob1), undefined);
// blob2, blob3, blob4 should exist
(0, assert_1.ok)(cache.match(blob2) !== undefined);
(0, assert_1.ok)(cache.match(blob3) !== undefined);
(0, assert_1.ok)(cache.match(blob4) !== undefined);
});
it('should update access time on repeated access', async () => {
const blob1 = 'blob:http://localhost/image1';
const blob2 = 'blob:http://localhost/image2';
const blob3 = 'blob:http://localhost/image3';
cache.addImage(blob1);
await new Promise((resolve) => setTimeout(resolve, 10));
cache.addImage(blob2);
await new Promise((resolve) => setTimeout(resolve, 10));
cache.addImage(blob3);
(0, assert_1.strictEqual)(cache.size(), 3);
// Access blob1 multiple times (update access time)
cache.match(blob1);
await new Promise((resolve) => setTimeout(resolve, 10));
cache.addImage(blob1); // Should update access time
await new Promise((resolve) => setTimeout(resolve, 10));
// Add 4th image - should evict blob2 (oldest unaccessed)
const blob4 = 'blob:http://localhost/image4';
cache.addImage(blob4);
(0, assert_1.strictEqual)(cache.size(), 3);
(0, assert_1.strictEqual)(cache.match(blob2), undefined); // Evicted
(0, assert_1.ok)(cache.match(blob1) !== undefined); // Still exists
});
it('should clear all cache entries', () => {
cache.addImage('blob:http://localhost/image1');
cache.addImage('blob:http://localhost/image2');
(0, assert_1.strictEqual)(cache.size(), 2);
cache.clear();
(0, assert_1.strictEqual)(cache.size(), 0);
});
it('should throw error for invalid max size', () => {
const options = {
imageTimeout: 15000,
useCORS: false,
allowTaint: false,
maxCacheSize: 0 // Invalid
};
try {
new cache_storage_1.Cache(context, options);
(0, assert_1.ok)(false, 'Should have thrown error');
}
catch (error) {
(0, assert_1.ok)(error.message.includes('at least 1'));
}
});
it('should use default max size if not specified', () => {
const options = {
imageTimeout: 15000,
useCORS: false,
allowTaint: false
// maxCacheSize not specified
};
const cacheWithDefault = new cache_storage_1.Cache(context, options);
(0, assert_1.strictEqual)(cacheWithDefault.getMaxSize(), 100); // Default
});
});
//# sourceMappingURL=cache-storage.test.js.map