Files
TREK/client/scripts/native-sandbox-config.test.mjs
T

46 lines
1.5 KiB
JavaScript

import assert from 'node:assert/strict';
import { basename } from 'node:path';
import { test } from 'node:test';
import config from '../vite.native.config.js';
const variable = 'TREK_CLOUD_SANDBOX_ORIGIN';
function setOrigin(context, origin) {
const previous = process.env[variable];
process.env[variable] = origin;
context.after(() => {
if (previous === undefined) delete process.env[variable];
else process.env[variable] = previous;
});
}
test('isolates the sandbox output and keeps the ordinary app on the production configuration', (context) => {
const origin = 'https://store-sandbox.example.com';
setOrigin(context, origin);
const sandbox = config({ command: 'build', mode: 'store-sandbox' });
assert.deepEqual(sandbox.define, { __TREK_STORE_SANDBOX_ORIGIN__: JSON.stringify(origin) });
assert.equal(basename(sandbox.build.outDir), 'native-sandbox-dist');
for (const input of [
{ command: 'build', mode: 'production' },
{ command: 'serve', mode: 'store-sandbox' },
]) {
const regular = config(input);
assert.deepEqual(regular.define, {});
assert.equal(basename(regular.build.outDir), 'native-dist');
}
});
for (const origin of [
'',
'http://qa.example.com',
'https://qa.example.com/path',
'https://liketrek.com',
'https://www.liketrek.com',
]) {
test(`refuses a sandbox configuration with origin ${JSON.stringify(origin)}`, (context) => {
setOrigin(context, origin);
assert.throws(() => config({ command: 'build', mode: 'store-sandbox' }), { message: new RegExp(variable) });
});
}