Files
smartexpose/test/test.node.ts
T

139 lines
4.1 KiB
TypeScript

import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as qenv from '@push.rocks/qenv';
import * as smartfile from '@push.rocks/smartfile';
import * as smartexpose from '../ts/index.js';
const testQenv = new qenv.Qenv('./', './.nogit/');
let testSmartexpose: smartexpose.SmartExpose;
const shouldRunIntegrationTests = () => process.env.SMARTEXPOSE_RUN_INTEGRATION_TESTS === 'true';
class TestExposeProvider extends smartexpose.ExposeProvider {
public receivedFileOptions: Parameters<smartexpose.ExposeProvider['exposeFile']>[0] | undefined;
public receivedFileArrayOptions: Parameters<smartexpose.ExposeProvider['exposeFileArray']>[0] | undefined;
public async houseKeeping(): Promise<void> {}
public async exposeFile(optionsArg: Parameters<smartexpose.ExposeProvider['exposeFile']>[0]) {
this.receivedFileOptions = optionsArg;
return {
url: 'https://example.com/test',
id: 'test',
status: 'created' as const,
};
}
public async exposeFileArray(
optionsArg: Parameters<smartexpose.ExposeProvider['exposeFileArray']>[0]
) {
this.receivedFileArrayOptions = optionsArg;
return [
{
url: 'https://example.com/test',
id: 'test',
status: 'created' as const,
},
];
}
public async start(): Promise<void> {}
public async stop(): Promise<void> {}
public async wipeAll(): Promise<{ id: string; status: 'deleted' | 'failed' | 'notfound' }[]> {
return [];
}
public async deleteFileById(
idArg: string
): Promise<{ id: string; status: 'deleted' | 'failed' | 'notfound' }> {
return {
id: idArg,
status: 'deleted',
};
}
}
const requireEnv = async (envNameArg: string): Promise<string> => {
const envValue = await testQenv.getEnvVarOnDemand(envNameArg);
if (!envValue) {
throw new Error(`Missing required environment variable: ${envNameArg}`);
}
return envValue;
};
tap.test('should apply constructor defaults when exposing files', async () => {
const smartExpose = new smartexpose.SmartExpose({
exposedBaseUrl: 'https://example.com/',
deleteAfterMillis: 1234,
privateUrl: true,
});
const testProvider = new TestExposeProvider();
smartExpose.provider = testProvider;
const smartfileFactory = new smartfile.SmartFileFactory(undefined);
const smartFile = smartfileFactory.fromString('okidoks', 'hi there', 'utf8');
await smartExpose.exposeFile({ smartFile });
expect(testProvider.receivedFileOptions?.deleteAfterMillis).toEqual(1234);
expect(testProvider.receivedFileOptions?.privateUrl).toEqual(true);
await smartExpose.exposeFileArray({ smartFiles: [smartFile] });
expect(testProvider.receivedFileArrayOptions?.deleteAfterMillis).toEqual(1234);
expect(testProvider.receivedFileArrayOptions?.privateUrl).toEqual(true);
});
tap.test('should create a valid instance of smartexpose using Webdav', async () => {
if (!shouldRunIntegrationTests()) {
console.log('Skipping SmartExpose integration test. Set SMARTEXPOSE_RUN_INTEGRATION_TESTS=true to run it.');
return;
}
testSmartexpose = new smartexpose.SmartExpose({
webdav: {
webdavCredentials: {
serverUrl: await requireEnv('WEBDAV_SERVER_URL'),
password: await requireEnv('WEBDAV_SERVER_TOKEN'),
},
webdavSubPath: await requireEnv('WEBDAV_SUB_PATH'),
},
deleteAfterMillis: 30000,
privateUrl: true,
exposedBaseUrl: await requireEnv('EXPOSED_BASE_URL'),
});
await testSmartexpose.start();
expect(testSmartexpose).toBeInstanceOf(smartexpose.SmartExpose);
});
tap.test('should expose a file', async () => {
if (!shouldRunIntegrationTests()) {
return;
}
const smartfileFactory = new smartfile.SmartFileFactory(undefined);
await testSmartexpose.exposeFile({
smartFile: smartfileFactory.fromString('okidoks', 'hi there', 'utf8'),
deleteAfterMillis: 10000,
privateUrl: true,
});
});
tap.test('should delete the file', async (toolsArg) => {
if (!shouldRunIntegrationTests()) {
return;
}
await toolsArg.delayFor(11000);
});
tap.test('should stop the smartexpose', async () => {
if (!shouldRunIntegrationTests()) {
return;
}
await testSmartexpose.stop();
});
export default tap.start();