import { SourceMemory } from '@chunkd/core';
import { CogTiff } from '@cogeotiff/core';
import o from 'ospec';
import { TiffCache } from '../tiff.cache.js';
/** convert number to megabytes */
const MegaBytes = (num) => num * 1024 * 1024;
o.spec('TiffCache', () => {
    o.beforeEach(() => TiffCache.clear());
    o('should cache items', () => {
        var _a, _b;
        const source = new SourceMemory('fake', Buffer.from('foo'));
        const tiff = new CogTiff(source);
        o(TiffCache.get('fake')).equals(null);
        TiffCache.set('fake', tiff);
        o((_a = TiffCache.get('fake')) === null || _a === void 0 ? void 0 : _a.source.uri).equals(tiff.source.uri);
        TiffCache.check();
        o((_b = TiffCache.get('fake')) === null || _b === void 0 ? void 0 : _b.source.uri).equals(tiff.source.uri);
    });
    o('should expire cached object if size gets too large', () => {
        var _a, _b, _c, _d, _e;
        const source = new SourceMemory('fake', Buffer.from('foo'));
        const sourceB = new SourceMemory('fakeB', Buffer.from('foo'));
        // Override chunk size to say there is 500 mb of items
        source.chunkSize = MegaBytes(257);
        sourceB.chunkSize = MegaBytes(257);
        const tiff = new CogTiff(source);
        const tiffB = new CogTiff(sourceB);
        o(TiffCache.get('fake')).equals(null);
        TiffCache.set('fake', tiff);
        o((_a = TiffCache.get('fake')) === null || _a === void 0 ? void 0 : _a.source.uri).equals(tiff.source.uri);
        TiffCache.check();
        o((_b = TiffCache.cacheA.get('fake')) === null || _b === void 0 ? void 0 : _b.source.uri).equals(undefined);
        o((_c = TiffCache.cacheB.get('fake')) === null || _c === void 0 ? void 0 : _c.source.uri).equals(tiff.source.uri);
        // Should evict the cache
        TiffCache.set('fakeB', tiffB);
        TiffCache.check();
        o((_d = TiffCache.cacheA.get('fake')) === null || _d === void 0 ? void 0 : _d.source.uri).equals(undefined);
        o((_e = TiffCache.cacheB.get('fake')) === null || _e === void 0 ? void 0 : _e.source.uri).equals(undefined);
    });
    o('should allow multiple items in the cache', () => {
        const source = new SourceMemory('fake', Buffer.from('foo'));
        const tiff = new CogTiff(source);
        TiffCache.set('fake', tiff);
        const oneMb = MegaBytes(1);
        source.chunkSize = oneMb;
        for (let i = 1; i < 256; i++) {
            o(TiffCache.currentSize).equals(i * oneMb);
            source.chunks.set(i, source.chunks.get(0));
        }
        // should not have overflown
        TiffCache.check();
        o(TiffCache.cacheA.size).equals(1);
        // Overflow the cache
        source.chunks.set(257, source.chunks.get(0));
        TiffCache.check();
        o(TiffCache.cacheA.size).equals(0);
    });
});
