file-box
Version:
Pack a File into Box for easy move/transfer between servers no matter of where it is.(local path, remote url, or cloud storage)
81 lines • 3.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tstest_1 = require("tstest");
const file_box_js_1 = require("../file-box.js");
const uniform_resource_name_registry_js_1 = require("./uniform-resource-name-registry.js");
(0, tstest_1.test)('UniformResourceNameRegistry class', async (t) => {
const QRCODE = 'test qrcode';
const urnRegistry = new uniform_resource_name_registry_js_1.UniformResourceNameRegistry();
await urnRegistry.init();
const fileBox = file_box_js_1.FileBox.fromQRCode(QRCODE);
const stream = await fileBox.toStream();
const uuid = await urnRegistry.save(stream);
const stream2 = await urnRegistry.load(uuid);
t.ok(stream2, 'should load stream');
const fileBox2 = file_box_js_1.FileBox.fromStream(stream2, 'test');
const qrcode = await fileBox2.toQRCode();
t.equal(qrcode, QRCODE, 'should get back the qrcode data');
await t.resolves(() => urnRegistry.load(uuid), 'should not reject when load a UUID again');
await urnRegistry.destroy();
});
(0, tstest_1.test)('expireMilliseconds: in time', async (t) => {
const expireMilliseconds = 3;
const urnRegistry = new uniform_resource_name_registry_js_1.UniformResourceNameRegistry({
expireMilliseconds,
});
await urnRegistry.init();
const uuid = await urnRegistry.save(await file_box_js_1.FileBox.fromQRCode('qr').toStream());
await new Promise(resolve => setTimeout(resolve, 1));
await t.resolves(() => urnRegistry.load(uuid), `should not expire after 1ms (with ${expireMilliseconds}ms expire)`);
await urnRegistry.destroy();
});
(0, tstest_1.test)('expireMilliseconds: time out', async (t) => {
const sandbox = tstest_1.sinon.createSandbox({
useFakeTimers: true,
});
const expireMilliseconds = 10 * 60 * 1000; // 10 minute
const urnRegistry = new uniform_resource_name_registry_js_1.UniformResourceNameRegistry({
expireMilliseconds,
});
await urnRegistry.init();
/**
* Time: 0
*/
const uuid1 = await urnRegistry.save(await file_box_js_1.FileBox.fromQRCode('qr').toStream());
await t.resolves(() => urnRegistry.load(uuid1), 'should load uuid1 successfully');
/**
* Time: 5
*/
await sandbox.clock.tickAsync(5 * 60 * 1000);
const uuid2 = await urnRegistry.save(await file_box_js_1.FileBox.fromQRCode('qr2').toStream());
await t.resolves(() => urnRegistry.load(uuid1), 'should load uuid1 successfully after 5 minutes');
await t.resolves(() => urnRegistry.load(uuid2), 'should load uuid2 successfully');
/**
* Time: 11
*/
await sandbox.clock.tickAsync(6 * 60 * 1000);
await t.rejects(() => urnRegistry.load(uuid1), 'should load uuid1 fail because it is expired');
await t.resolves(() => urnRegistry.load(uuid2), 'should load uuid2 successfully 6 minutes after it has been saved');
/**
* Time 20
*/
await sandbox.clock.tickAsync(9 * 60 * 1000);
await t.rejects(() => urnRegistry.load(uuid2), 'should load uuid2 fail because it is expired');
await urnRegistry.destroy();
sandbox.restore();
});
(0, tstest_1.test)('URN FileBox helper smoke testing', async (t) => {
const QRCODE = 'test qrcode';
const urnRegistry = new uniform_resource_name_registry_js_1.UniformResourceNameRegistry();
const UUIDFileBox = urnRegistry.getFileBox();
const uuid = await UUIDFileBox
.fromQRCode(QRCODE)
.toUuid();
const qrcode = await UUIDFileBox
.fromUuid(uuid)
.toQRCode();
t.equal(qrcode, QRCODE, 'should get back the qrcode data');
urnRegistry.destroy();
});
//# sourceMappingURL=uniform-resource-name-registry.spec.js.map