silphius/app/util/resources.test.js
2024-09-21 06:24:26 -05:00

46 lines
1.5 KiB
JavaScript

import {expect, test} from 'vitest';
import {computeMissing, parseResources} from './resources.js';
import {encodeResources} from './resources.server.js';
test('cache', async () => {
class TestCache {
current;
async get() {
return new Promise((resolve) => {
resolve(this.current);
});
}
async set(manifest) {
return new Promise((resolve) => {
this.current = manifest;
resolve();
});
}
}
const cache = new TestCache();
expect(await computeMissing(await cache.get(), {foo: 'bar'})).to.deep.equal(['foo']);
await cache.set({foo: {hash: 'bar'}});
expect(await computeMissing(await cache.get(), {foo: 'bar'})).to.deep.equal([]);
expect(await computeMissing(await cache.get(), {foo: 'bar', baz: '32'})).to.deep.equal(['baz']);
await cache.set({foo: {hash: 'bar'}, baz: '32'});
expect(await computeMissing(await cache.get(), {foo: 'bar', baz: '64'})).to.deep.equal(['baz']);
});
test('codec', async () => {
const asset = {
asset: (new Uint8Array('hello'.split('').map((letter) => letter.charCodeAt(0)))).buffer,
hash: (new Uint8Array(Array(20).fill(0).map((_, i) => i))).buffer,
};
const buffer = await encodeResources([
asset,
]);
const parsed = await parseResources(['hello.txt'], buffer);
expect('hello.txt' in parsed).to.equal(true);
expect(parsed['hello.txt'].hash).to.equal('000102030405060708090a0b0c0d0e0f10111213');
const hello = new Uint8Array(parsed['hello.txt'].asset);
for (let i = 0; i < 5; ++i) {
expect(hello[i]).to.equal('hello'.charCodeAt(i));
}
});