207 lines
7.5 KiB
JavaScript
207 lines
7.5 KiB
JavaScript
/*
|
|
Copyright (C) 2025 QuantumNous
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
|
|
import assert from 'node:assert/strict';
|
|
import { mkdir, mkdtemp, readFile, writeFile, rm } from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
import { createServer as createHttpServer } from 'node:http';
|
|
import { fileURLToPath } from 'node:url';
|
|
import test from 'node:test';
|
|
import { loadConfigFromFile, loadEnv, createServer } from 'vite';
|
|
|
|
const root = fileURLToPath(new URL('../', import.meta.url));
|
|
const html = '<!--umami--><!--Google Analytics-->';
|
|
|
|
async function fixture(run) {
|
|
const dir = await mkdtemp(path.join(root, '.env-test-'));
|
|
try {
|
|
await writeFile(
|
|
path.join(dir, 'vite.config.js'),
|
|
await readFile(path.join(root, 'vite.config.js')),
|
|
);
|
|
// 保留 Vite 配置引用的构建插件目录结构。
|
|
await mkdir(path.join(dir, 'scripts/vite'), { recursive: true });
|
|
await writeFile(
|
|
path.join(dir, 'scripts/vite/legacy-css-layers.js'),
|
|
await readFile(path.join(root, 'scripts/vite/legacy-css-layers.js')),
|
|
);
|
|
await writeFile(
|
|
path.join(dir, '.env.development'),
|
|
'DEV_PROXY_TARGET=http://localhost:13001\nVITE_REACT_APP_SERVER_URL=https://dev.example.com\nVITE_CLOUD_BRAIN_UI_ENABLED=true\nVITE_UMAMI_WEBSITE_ID=dev-id\nVITE_UMAMI_SCRIPT_URL=https://dev.example.com/script.js\n',
|
|
);
|
|
await writeFile(
|
|
path.join(dir, '.env.production'),
|
|
'VITE_REACT_APP_SERVER_URL=https://prod.example.com\nVITE_CLOUD_BRAIN_UI_ENABLED=false\nVITE_GOOGLE_ANALYTICS_ID=G-PROD\n',
|
|
);
|
|
await run(dir);
|
|
} finally {
|
|
await rm(dir, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
async function config(dir, mode) {
|
|
return (
|
|
await loadConfigFromFile(
|
|
{ command: mode === 'development' ? 'serve' : 'build', mode },
|
|
path.join(dir, 'vite.config.js'),
|
|
)
|
|
).config;
|
|
}
|
|
|
|
function analytics(config) {
|
|
return config.plugins
|
|
.find((plugin) => plugin.name === 'inject-analytics')
|
|
.transformIndexHtml(html);
|
|
}
|
|
|
|
test('development and production isolate API, feature flag and analytics settings', async () => {
|
|
await fixture(async (dir) => {
|
|
const dev = await config(dir, 'development');
|
|
const prod = await config(dir, 'production');
|
|
assert.match(analytics(dev), /dev-id/);
|
|
assert.match(analytics(dev), /https:\/\/dev.example.com\/script.js/);
|
|
assert.doesNotMatch(analytics(dev), /G-PROD/);
|
|
assert.match(analytics(prod), /G-PROD/);
|
|
assert.doesNotMatch(analytics(prod), /dev-id/);
|
|
for (const proxy of Object.values(dev.server.proxy)) {
|
|
assert.equal(proxy.target, 'http://localhost:13001');
|
|
assert.equal(proxy.changeOrigin, true);
|
|
}
|
|
assert.equal(dev.server.proxy['/v1'].ws, true);
|
|
for (const [mode, url, flag] of [
|
|
['development', 'https://dev.example.com', 'true'],
|
|
['production', 'https://prod.example.com', 'false'],
|
|
]) {
|
|
const env = loadEnv(mode, dir);
|
|
assert.equal(env.VITE_REACT_APP_SERVER_URL, url);
|
|
assert.equal(env.VITE_CLOUD_BRAIN_UI_ENABLED, flag);
|
|
assert.equal(env.DEV_PROXY_TARGET, undefined);
|
|
}
|
|
});
|
|
});
|
|
|
|
test('mode local overrides and CI variables reach the Vite plugins and proxy', async () => {
|
|
await fixture(async (dir) => {
|
|
await writeFile(
|
|
path.join(dir, '.env.development.local'),
|
|
'DEV_PROXY_TARGET=http://localhost:13002\nVITE_UMAMI_WEBSITE_ID=local-id\n',
|
|
);
|
|
assert.match(analytics(await config(dir, 'development')), /local-id/);
|
|
assert.equal(
|
|
(await config(dir, 'development')).server.proxy['/api'].target,
|
|
'http://localhost:13002',
|
|
);
|
|
const previous = process.env.VITE_UMAMI_WEBSITE_ID;
|
|
try {
|
|
process.env.VITE_UMAMI_WEBSITE_ID = 'ci-id';
|
|
assert.match(analytics(await config(dir, 'development')), /ci-id/);
|
|
} finally {
|
|
if (previous === undefined) delete process.env.VITE_UMAMI_WEBSITE_ID;
|
|
else process.env.VITE_UMAMI_WEBSITE_ID = previous;
|
|
}
|
|
});
|
|
});
|
|
|
|
test('checked-in defaults preserve same-origin behavior and disable analytics', async () => {
|
|
for (const mode of ['development', 'production']) {
|
|
const env = loadEnv(mode, root);
|
|
assert.equal(env.VITE_REACT_APP_SERVER_URL, '');
|
|
assert.equal(
|
|
env.VITE_CLOUD_BRAIN_UI_ENABLED,
|
|
mode === 'development' ? 'true' : 'false',
|
|
);
|
|
assert.equal(analytics(await config(root, mode)), html);
|
|
}
|
|
});
|
|
|
|
test('development server transforms the HTML and runtime configuration modules', async () => {
|
|
// Inline `watch: null` is dropped when merged with the config file, so pass the
|
|
// loaded config directly; otherwise chokidar keeps the test process alive.
|
|
const userConfig = await config(root, 'development');
|
|
const server = await createServer({
|
|
...userConfig,
|
|
configFile: false,
|
|
root,
|
|
cacheDir: path.join(root, '.cache/vite-environment-test'),
|
|
server: { ...userConfig.server, middlewareMode: true, watch: null },
|
|
optimizeDeps: { noDiscovery: true, include: [] },
|
|
});
|
|
try {
|
|
const output = await server.transformIndexHtml(
|
|
'/',
|
|
await readFile(path.join(root, 'index.html'), 'utf8'),
|
|
);
|
|
assert.match(output, /@vite\/client/);
|
|
for (const url of [
|
|
'/src/helpers/config.js',
|
|
'/src/config/cloudBrainUi.js',
|
|
]) {
|
|
const transformed = await server.transformRequest(url);
|
|
assert.match(transformed.code, /VITE_CLOUD_BRAIN_UI_ENABLED/);
|
|
assert.doesNotMatch(transformed.code, /134\.175\.227\.134/);
|
|
}
|
|
} finally {
|
|
await server.close();
|
|
}
|
|
});
|
|
|
|
test('Vite proxies API requests on Node 22 without DEP0060', async () => {
|
|
const upstream = createHttpServer((request, response) => {
|
|
response.setHeader('Content-Type', 'application/json');
|
|
response.end(JSON.stringify({ path: request.url }));
|
|
});
|
|
await new Promise((resolve) => upstream.listen(0, '127.0.0.1', resolve));
|
|
const warnings = [];
|
|
const onWarning = (warning) => warnings.push(warning.code);
|
|
process.on('warning', onWarning);
|
|
let server;
|
|
try {
|
|
server = await createServer({
|
|
configFile: false,
|
|
root,
|
|
cacheDir: path.join(root, '.cache/vite-proxy-test'),
|
|
optimizeDeps: { noDiscovery: true, include: [] },
|
|
server: {
|
|
host: '127.0.0.1',
|
|
port: 0,
|
|
watch: null,
|
|
proxy: {
|
|
'/api': {
|
|
target: `http://127.0.0.1:${upstream.address().port}`,
|
|
changeOrigin: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
await server.listen();
|
|
const response = await fetch(
|
|
`http://127.0.0.1:${server.httpServer.address().port}/api/status`,
|
|
{ headers: { Connection: 'close' } },
|
|
);
|
|
assert.equal(response.status, 200);
|
|
assert.deepEqual(await response.json(), { path: '/api/status' });
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
assert.ok(!warnings.includes('DEP0060'));
|
|
} finally {
|
|
process.off('warning', onWarning);
|
|
await server?.close();
|
|
await new Promise((resolve) => upstream.close(resolve));
|
|
}
|
|
});
|