feat(runtime): override deploy settings from the environment
SNOWLUMA_ variables can set the factory OneBot listen ports, WebUI TLS, packet and hook timeouts, the stream directory, file download timeout, and notification HTTP timeout. The values apply in memory and are not written back to saved config.
This commit is contained in:
19 files changed
+342
-14
No files matched your search
@@ -640,3 +640,61 @@ describe('OneBotConfig login history sync', () => {
|
||||
.toThrow(/historySync\.enabled must be a boolean/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('OneBot listen port environment overrides', () => {
|
||||
let tempDir = '';
|
||||
let previous = '';
|
||||
const saved: Record<string, string | undefined> = {};
|
||||
|
||||
beforeEach(() => {
|
||||
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'snowluma-onebot-port-env-'));
|
||||
previous = process.cwd();
|
||||
process.chdir(tempDir);
|
||||
for (const name of ['SNOWLUMA_ONEBOT_HTTP_PORT', 'SNOWLUMA_ONEBOT_WS_PORT']) {
|
||||
saved[name] = process.env[name];
|
||||
delete process.env[name];
|
||||
}
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
for (const name of ['SNOWLUMA_ONEBOT_HTTP_PORT', 'SNOWLUMA_ONEBOT_WS_PORT']) {
|
||||
if (saved[name] === undefined) delete process.env[name];
|
||||
else process.env[name] = saved[name];
|
||||
}
|
||||
process.chdir(previous);
|
||||
fs.rmSync(tempDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it('overrides only the factory listeners and does not write the port back', () => {
|
||||
process.env.SNOWLUMA_ONEBOT_HTTP_PORT = '3800';
|
||||
process.env.SNOWLUMA_ONEBOT_WS_PORT = '3801';
|
||||
const loaded = loadOneBotConfig('10001', { persistDefaults: true });
|
||||
expect(loaded.networks.httpServers[0]?.port).toBe(3800);
|
||||
expect(loaded.networks.wsServers[0]?.port).toBe(3801);
|
||||
|
||||
const onDisk = JSON.parse(fs.readFileSync(path.join(tempDir, 'config', 'onebot_10001.json'), 'utf8')) as {
|
||||
networks: { httpServers: Array<{ port: number }>; wsServers: Array<{ port: number }> };
|
||||
};
|
||||
expect(onDisk.networks.httpServers[0]?.port).toBe(3000);
|
||||
expect(onDisk.networks.wsServers[0]?.port).toBe(3001);
|
||||
});
|
||||
|
||||
it('leaves a custom listener alone', () => {
|
||||
const config = makeDefaultOneBotConfig();
|
||||
config.networks.httpServers = [{
|
||||
...config.networks.httpServers[0]!,
|
||||
name: 'public-http',
|
||||
port: 4000,
|
||||
}];
|
||||
saveOneBotConfig('10001', config);
|
||||
process.env.SNOWLUMA_ONEBOT_HTTP_PORT = '3800';
|
||||
const loaded = loadOneBotConfig('10001');
|
||||
expect(loaded.networks.httpServers.map((server) => server.port)).toEqual([4000]);
|
||||
});
|
||||
|
||||
it('keeps the saved port when the override is not a port', () => {
|
||||
process.env.SNOWLUMA_ONEBOT_HTTP_PORT = 'nope';
|
||||
const loaded = loadOneBotConfig('10001', { persistDefaults: true });
|
||||
expect(loaded.networks.httpServers[0]?.port).toBe(3000);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,36 @@
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { afterEach, describe, expect, it } from 'vitest';
|
||||
import { createStreamStorage } from '../src/stream-storage';
|
||||
|
||||
describe('SNOWLUMA_STREAM_DIR', () => {
|
||||
const created: string[] = [];
|
||||
|
||||
afterEach(() => {
|
||||
for (const directory of created.splice(0)) {
|
||||
fs.rmSync(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('uses the system temp directory by default', () => {
|
||||
const storage = createStreamStorage({});
|
||||
expect(storage.root.endsWith(`${path.sep}onebot-stream`)).toBe(true);
|
||||
created.push(storage.root);
|
||||
});
|
||||
|
||||
it('uses a caller directory when the variable is set', () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'snowluma-stream-env-'));
|
||||
created.push(root);
|
||||
const storage = createStreamStorage({ SNOWLUMA_STREAM_DIR: root });
|
||||
expect(storage.root).toBe(fs.realpathSync(root));
|
||||
});
|
||||
|
||||
it('falls back when the variable does not name a usable directory', () => {
|
||||
const file = path.join(os.tmpdir(), `snowluma-stream-not-dir-${process.pid}`);
|
||||
fs.writeFileSync(file, 'x');
|
||||
created.push(file);
|
||||
const storage = createStreamStorage({ SNOWLUMA_STREAM_DIR: file });
|
||||
expect(storage.root.endsWith(`${path.sep}onebot-stream`)).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user