refactor(onebot): make config persistence explicit
Persist user saves as authoritative per-account snapshots while retaining legacy overlay compatibility. Validate deterministic topology errors before writing and replace configuration files atomically.
This commit is contained in:
2 files changed
+500
-19
No files matched your search
+262
-16
@@ -21,6 +21,8 @@ const log = createLogger('OneBot.Config');
|
||||
const CONFIG_DIR = 'config';
|
||||
const DEFAULT_CONFIG_PATH = path.join(CONFIG_DIR, 'onebot.json');
|
||||
const DEFAULT_ACCESS_TOKEN_BYTES = 32;
|
||||
const NODE_TIMER_MAX_MS = 2_147_483_647;
|
||||
const PER_UIN_SNAPSHOT_MARKER = 'snapshot';
|
||||
|
||||
const DEFAULT_STATUS_COMMAND: StatusCommandConfig = {
|
||||
enabled: true,
|
||||
@@ -75,31 +77,243 @@ export interface LoadOneBotConfigOptions {
|
||||
persistDefaults?: boolean;
|
||||
}
|
||||
|
||||
/** A deterministic configuration error. Callers may safely return this as a
|
||||
* 4xx without conflating it with filesystem/runtime failures. */
|
||||
export class OneBotConfigValidationError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = 'OneBotConfigValidationError';
|
||||
}
|
||||
}
|
||||
|
||||
export function loadOneBotConfig(uin: string, options: LoadOneBotConfigOptions = {}): OneBotConfig {
|
||||
ensureConfigDir();
|
||||
|
||||
const perUinPath = path.join(CONFIG_DIR, `onebot_${uin}.json`);
|
||||
const globalRaw = tryLoadJson(DEFAULT_CONFIG_PATH);
|
||||
const perUinRaw = tryLoadJson(perUinPath);
|
||||
// A per-account desired config is authoritative state. Corruption must stop
|
||||
// startup instead of silently reviving global/default endpoints.
|
||||
const perUinRaw = tryLoadJson(perUinPath, true);
|
||||
const legacy = !!perUinRaw && hasLegacyTopLevel(perUinRaw);
|
||||
|
||||
const sources: JsonObject[] = [];
|
||||
if (globalRaw) sources.push(globalRaw);
|
||||
const isCanonicalSnapshot = perUinRaw?.mode === PER_UIN_SNAPSHOT_MARKER;
|
||||
if (globalRaw && !isCanonicalSnapshot) sources.push(globalRaw);
|
||||
if (perUinRaw) sources.push(perUinRaw);
|
||||
|
||||
const config = fromJson(sources, !perUinRaw && !globalRaw);
|
||||
|
||||
if (options.persistDefaults && (!perUinRaw || legacy)) {
|
||||
saveOneBotConfig(uin, config);
|
||||
saveOneBotConfig(uin, config, { mode: globalRaw ? 'overlay' : 'snapshot' });
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
export function saveOneBotConfig(uin: string, config: OneBotConfig): void {
|
||||
export interface SaveOneBotConfigOptions {
|
||||
/** Snapshot is the WebUI/user-save contract. Overlay is reserved for the
|
||||
* automatic legacy/global-default materialization path. */
|
||||
mode?: 'snapshot' | 'overlay';
|
||||
}
|
||||
|
||||
export function saveOneBotConfig(
|
||||
uin: string,
|
||||
config: OneBotConfig,
|
||||
options: SaveOneBotConfigOptions = {},
|
||||
): void {
|
||||
assertValidOneBotConfig(config);
|
||||
ensureConfigDir();
|
||||
const perUinPath = path.join(CONFIG_DIR, `onebot_${uin}.json`);
|
||||
saveJson(perUinPath, toJsonObject(config));
|
||||
saveJson(perUinPath, toJsonObject(config, options.mode ?? 'snapshot'));
|
||||
}
|
||||
|
||||
/** Validate the normalized, public OneBot configuration shape before it is
|
||||
* persisted or compiled into a live network plan. Adapter names form one
|
||||
* process-wide namespace per account, not four independent namespaces. */
|
||||
export function assertValidOneBotConfig(value: unknown): asserts value is OneBotConfig {
|
||||
if (!isObject(value)) invalid('configuration must be an object');
|
||||
if (!isObject(value.networks)) invalid('networks must be an object');
|
||||
|
||||
const seen = new Map<string, keyof OneBotNetworks>();
|
||||
const serverBindings = new Map<string, string>();
|
||||
validateNetworkList(value.networks, 'httpServers', seen, (item, at) => {
|
||||
validateServer(item, at);
|
||||
validateServerBinding(item, at, serverBindings);
|
||||
});
|
||||
validateNetworkList(value.networks, 'httpClients', seen, (item, at) => {
|
||||
validateClient(item, at, new Set(['http:', 'https:']));
|
||||
if (
|
||||
item.timeoutMs !== undefined &&
|
||||
(
|
||||
typeof item.timeoutMs !== 'number' ||
|
||||
!Number.isSafeInteger(item.timeoutMs) ||
|
||||
item.timeoutMs <= 0 ||
|
||||
item.timeoutMs > NODE_TIMER_MAX_MS
|
||||
)
|
||||
) {
|
||||
invalid(`${at}.timeoutMs must be an integer between 1 and ${NODE_TIMER_MAX_MS}`);
|
||||
}
|
||||
});
|
||||
validateNetworkList(value.networks, 'wsServers', seen, (item, at) => {
|
||||
validateServer(item, at);
|
||||
validateServerBinding(item, at, serverBindings);
|
||||
validateRole(item.role, `${at}.role`);
|
||||
});
|
||||
validateNetworkList(value.networks, 'wsClients', seen, (item, at) => {
|
||||
validateClient(item, at, new Set(['ws:', 'wss:']));
|
||||
validateRole(item.role, `${at}.role`);
|
||||
if (
|
||||
item.reconnectIntervalMs !== undefined &&
|
||||
(
|
||||
typeof item.reconnectIntervalMs !== 'number' ||
|
||||
!Number.isSafeInteger(item.reconnectIntervalMs) ||
|
||||
item.reconnectIntervalMs < 1000 ||
|
||||
item.reconnectIntervalMs > NODE_TIMER_MAX_MS
|
||||
)
|
||||
) {
|
||||
invalid(`${at}.reconnectIntervalMs must be an integer between 1000 and ${NODE_TIMER_MAX_MS}`);
|
||||
}
|
||||
});
|
||||
|
||||
if (!isObject(value.statusCommand)) invalid('statusCommand must be an object');
|
||||
const status = value.statusCommand;
|
||||
if (typeof status.enabled !== 'boolean') invalid('statusCommand.enabled must be a boolean');
|
||||
if (typeof status.swallow !== 'boolean') invalid('statusCommand.swallow must be a boolean');
|
||||
if (
|
||||
typeof status.cooldownSeconds !== 'number' ||
|
||||
!Number.isInteger(status.cooldownSeconds) ||
|
||||
status.cooldownSeconds < 0
|
||||
) {
|
||||
invalid('statusCommand.cooldownSeconds must be a non-negative integer');
|
||||
}
|
||||
if (
|
||||
typeof status.trigger !== 'string' ||
|
||||
!status.trigger.trim() ||
|
||||
status.trigger.length > STATUS_COMMAND_TRIGGER_MAX_LENGTH ||
|
||||
/[\r\n]/.test(status.trigger)
|
||||
) {
|
||||
invalid(`statusCommand.trigger must be non-empty, single-line, and <= ${STATUS_COMMAND_TRIGGER_MAX_LENGTH} characters`);
|
||||
}
|
||||
|
||||
if (value.notifications !== undefined) {
|
||||
if (!isObject(value.notifications) || !Array.isArray(value.notifications.channelIds)) {
|
||||
invalid('notifications.channelIds must be an array');
|
||||
}
|
||||
for (const [index, channelId] of value.notifications.channelIds.entries()) {
|
||||
if (typeof channelId !== 'string' || !channelId || channelId.length > 64 || !/^[\w.-]+$/.test(channelId)) {
|
||||
invalid(`notifications.channelIds[${index}] is invalid`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function validateNetworkList(
|
||||
networks: JsonObject,
|
||||
kind: keyof OneBotNetworks,
|
||||
seen: Map<string, keyof OneBotNetworks>,
|
||||
validateSpecific: (item: JsonObject, at: string) => void,
|
||||
): void {
|
||||
const list = networks[kind];
|
||||
if (!Array.isArray(list)) invalid(`networks.${kind} must be an array`);
|
||||
for (let index = 0; index < list.length; index += 1) {
|
||||
const item = list[index];
|
||||
const at = `networks.${kind}[${index}]`;
|
||||
if (!isObject(item)) invalid(`${at} must be an object`);
|
||||
validateNetworkBase(item, at);
|
||||
const name = item.name as string;
|
||||
const previousKind = seen.get(name);
|
||||
if (previousKind !== undefined) {
|
||||
invalid(`network adapter name "${name}" is duplicated in ${previousKind} and ${kind}`);
|
||||
}
|
||||
seen.set(name, kind);
|
||||
validateSpecific(item, at);
|
||||
}
|
||||
}
|
||||
|
||||
function validateNetworkBase(item: JsonObject, at: string): void {
|
||||
if (typeof item.name !== 'string' || !item.name.trim()) invalid(`${at}.name must be a non-empty string`);
|
||||
if (item.name !== (item.name as string).trim()) invalid(`${at}.name must not have surrounding whitespace`);
|
||||
if (item.enabled !== undefined && typeof item.enabled !== 'boolean') invalid(`${at}.enabled must be a boolean`);
|
||||
if (item.accessToken !== undefined && typeof item.accessToken !== 'string') {
|
||||
invalid(`${at}.accessToken must be a string`);
|
||||
}
|
||||
if (item.messageFormat !== 'array' && item.messageFormat !== 'string') {
|
||||
invalid(`${at}.messageFormat must be "array" or "string"`);
|
||||
}
|
||||
if (typeof item.reportSelfMessage !== 'boolean') invalid(`${at}.reportSelfMessage must be a boolean`);
|
||||
}
|
||||
|
||||
function validateServer(item: JsonObject, at: string): void {
|
||||
if (!Number.isInteger(item.port) || (item.port as number) <= 0 || (item.port as number) > 65535) {
|
||||
invalid(`${at}.port must be an integer between 1 and 65535`);
|
||||
}
|
||||
if (item.host !== undefined) {
|
||||
if (typeof item.host !== 'string') invalid(`${at}.host must be a string`);
|
||||
if (!item.host.trim()) invalid(`${at}.host must be a non-empty string when provided`);
|
||||
if (item.host !== item.host.trim()) invalid(`${at}.host must not have surrounding whitespace`);
|
||||
}
|
||||
if (item.path !== undefined) {
|
||||
if (typeof item.path !== 'string') invalid(`${at}.path must be a string`);
|
||||
if (item.path !== item.path.trim()) invalid(`${at}.path must not have surrounding whitespace`);
|
||||
const pathValue = item.path || '/';
|
||||
if (!pathValue.startsWith('/')) invalid(`${at}.path must be empty or start with /`);
|
||||
if (pathValue.includes('?') || pathValue.includes('#')) invalid(`${at}.path must not include query or hash`);
|
||||
if (new URL(`http://127.0.0.1${pathValue}`).pathname !== pathValue) {
|
||||
invalid(`${at}.path must already be a normalized URL pathname`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function validateServerBinding(item: JsonObject, at: string, bindings: Map<string, string>): void {
|
||||
if (item.enabled === false) return;
|
||||
const host = typeof item.host === 'string' && item.host.trim()
|
||||
? item.host.trim().toLowerCase()
|
||||
: '0.0.0.0';
|
||||
const port = item.port as number;
|
||||
const binding = `${host}:${String(port)}`;
|
||||
const exact = bindings.get(binding);
|
||||
if (exact) invalid(`${at} conflicts with ${exact} on server binding ${binding}`);
|
||||
|
||||
// A wildcard listener owns every address in its family. Conservatively
|
||||
// reject any second listener on the same port when either side is a
|
||||
// wildcard; otherwise the saved plan deterministically degrades at bind.
|
||||
const wildcardKey = `*:${String(port)}`;
|
||||
const previousWildcard = bindings.get(wildcardKey);
|
||||
if (previousWildcard) invalid(`${at} conflicts with ${previousWildcard} on wildcard server port ${String(port)}`);
|
||||
const isWildcard = host === '0.0.0.0' || host === '::' || host === '[::]';
|
||||
if (isWildcard) {
|
||||
const previousSamePort = [...bindings.entries()].find(([key]) => key.endsWith(`:${String(port)}`));
|
||||
if (previousSamePort) {
|
||||
invalid(`${at} conflicts with ${previousSamePort[1]} on wildcard server port ${String(port)}`);
|
||||
}
|
||||
bindings.set(wildcardKey, at);
|
||||
}
|
||||
bindings.set(binding, at);
|
||||
}
|
||||
|
||||
function validateClient(item: JsonObject, at: string, protocols: ReadonlySet<string>): void {
|
||||
if (typeof item.url !== 'string') invalid(`${at}.url must be a string`);
|
||||
if (item.enabled === false) return;
|
||||
if (!item.url.trim()) invalid(`${at}.url must be non-empty while the adapter is enabled`);
|
||||
let parsed: URL;
|
||||
try {
|
||||
parsed = new URL(item.url);
|
||||
} catch {
|
||||
invalid(`${at}.url must be a valid absolute URL`);
|
||||
}
|
||||
if (!protocols.has(parsed.protocol)) {
|
||||
invalid(`${at}.url protocol must be one of ${[...protocols].join(', ')}`);
|
||||
}
|
||||
}
|
||||
|
||||
function validateRole(value: unknown, at: string): void {
|
||||
if (value !== undefined && value !== 'Api' && value !== 'Event' && value !== 'Universal') {
|
||||
invalid(`${at} must be Api, Event, or Universal`);
|
||||
}
|
||||
}
|
||||
|
||||
function invalid(message: string): never {
|
||||
throw new OneBotConfigValidationError(message);
|
||||
}
|
||||
|
||||
function ensureConfigDir(): void {
|
||||
@@ -138,9 +352,13 @@ export function cleanupInvalidPerUinConfigs(): string[] {
|
||||
return removed;
|
||||
}
|
||||
|
||||
function toJsonObject(config: OneBotConfig): JsonObject {
|
||||
function toJsonObject(config: OneBotConfig, mode: 'snapshot' | 'overlay'): JsonObject {
|
||||
const nets = config.networks;
|
||||
return {
|
||||
// Canonical per-UIN files are complete desired-state snapshots. Older
|
||||
// files without this marker retain the legacy global-overlay migration
|
||||
// behavior until their next successful save.
|
||||
mode,
|
||||
networks: {
|
||||
httpServers: nets.httpServers.map(httpServerToJson),
|
||||
httpClients: nets.httpClients.map(httpClientToJson),
|
||||
@@ -235,11 +453,13 @@ function fromJson(sources: JsonObject[], freshInstall: boolean): OneBotConfig {
|
||||
}
|
||||
|
||||
const networks: OneBotNetworks = { httpServers, httpClients, wsServers, wsClients };
|
||||
return {
|
||||
const config: OneBotConfig = {
|
||||
networks,
|
||||
statusCommand: parseStatusCommand(sources),
|
||||
notifications: parseNotifications(sources),
|
||||
};
|
||||
assertValidOneBotConfig(config);
|
||||
return config;
|
||||
}
|
||||
|
||||
/** Last-write-wins merge of `notifications.channelIds` across config sources,
|
||||
@@ -369,10 +589,11 @@ function parseHttpServer(value: JsonObject, defaults: AdapterDefaults): HttpServ
|
||||
|
||||
function parseHttpClient(value: JsonObject, defaults: AdapterDefaults): HttpClientNetwork | null {
|
||||
const url = asString(value.url);
|
||||
if (!url) return null;
|
||||
const base = parseBase(value, defaults);
|
||||
if (!url && base.enabled !== false) return null;
|
||||
const timeout = asNumber(value.timeoutMs, 0);
|
||||
return clean({
|
||||
...parseBase(value, defaults),
|
||||
...base,
|
||||
url,
|
||||
timeoutMs: timeout > 0 ? timeout : undefined,
|
||||
});
|
||||
@@ -392,10 +613,11 @@ function parseWsServer(value: JsonObject, defaults: AdapterDefaults): WsServerNe
|
||||
|
||||
function parseWsClient(value: JsonObject, defaults: AdapterDefaults): WsClientNetwork | null {
|
||||
const url = asString(value.url);
|
||||
if (!url) return null;
|
||||
const base = parseBase(value, defaults);
|
||||
if (!url && base.enabled !== false) return null;
|
||||
const reconnectIntervalMs = asNumber(value.reconnectIntervalMs, 5000);
|
||||
return clean({
|
||||
...parseBase(value, defaults),
|
||||
...base,
|
||||
url,
|
||||
role: asRole(value.role, 'Universal'),
|
||||
reconnectIntervalMs: Math.max(1000, reconnectIntervalMs),
|
||||
@@ -448,21 +670,45 @@ function asNumber(value: unknown, fallback = 0): number {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
function tryLoadJson(filePath: string): JsonObject | null {
|
||||
function tryLoadJson(filePath: string, failOnCorrupt = false): JsonObject | null {
|
||||
if (!fs.existsSync(filePath)) return null;
|
||||
try {
|
||||
const raw = fs.readFileSync(filePath, 'utf8');
|
||||
const parsed = JSON.parse(raw) as unknown;
|
||||
return isObject(parsed) ? parsed : null;
|
||||
if (!isObject(parsed)) throw new Error('configuration root must be an object');
|
||||
return parsed;
|
||||
} catch (err) {
|
||||
log.warn('config file %s is corrupt and will be ignored: %s', filePath, err instanceof Error ? err.message : String(err));
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
if (failOnCorrupt) {
|
||||
throw new Error(`config file ${filePath} is corrupt: ${message}`, { cause: err });
|
||||
}
|
||||
log.warn('config file %s is corrupt and will be ignored: %s', filePath, message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function saveJson(filePath: string, json: JsonObject): void {
|
||||
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
||||
fs.writeFileSync(filePath, JSON.stringify(json, null, 2), 'utf8');
|
||||
const dir = path.dirname(filePath);
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
const tempPath = path.join(
|
||||
dir,
|
||||
`.${path.basename(filePath)}.${String(process.pid)}.${randomBytes(6).toString('hex')}.tmp`,
|
||||
);
|
||||
let fd: number | null = null;
|
||||
try {
|
||||
fd = fs.openSync(tempPath, 'wx', 0o600);
|
||||
fs.writeFileSync(fd, JSON.stringify(json, null, 2), 'utf8');
|
||||
fs.fsyncSync(fd);
|
||||
fs.closeSync(fd);
|
||||
fd = null;
|
||||
fs.renameSync(tempPath, filePath);
|
||||
} catch (error) {
|
||||
if (fd !== null) {
|
||||
try { fs.closeSync(fd); } catch { /* preserve the primary write error */ }
|
||||
}
|
||||
try { fs.unlinkSync(tempPath); } catch { /* temp may not exist or rename succeeded */ }
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function isObject(value: unknown): value is JsonObject {
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import fs from 'fs';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import { loadOneBotConfig, makeDefaultOneBotConfig, saveOneBotConfig, STATUS_COMMAND_TRIGGER_MAX_LENGTH } from '../src/config';
|
||||
import {
|
||||
assertValidOneBotConfig,
|
||||
loadOneBotConfig,
|
||||
makeDefaultOneBotConfig,
|
||||
saveOneBotConfig,
|
||||
STATUS_COMMAND_TRIGGER_MAX_LENGTH,
|
||||
} from '../src/config';
|
||||
|
||||
const TOKEN_PATTERN = /^[A-Za-z0-9_-]{43}$/;
|
||||
|
||||
@@ -29,6 +35,190 @@ describe('makeDefaultOneBotConfig', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('assertValidOneBotConfig', () => {
|
||||
it('rejects duplicate adapter names across different kinds', () => {
|
||||
const config = makeDefaultOneBotConfig();
|
||||
config.networks.httpClients.push({
|
||||
name: 'http-default',
|
||||
url: 'http://127.0.0.1:5700',
|
||||
messageFormat: 'array',
|
||||
reportSelfMessage: false,
|
||||
});
|
||||
|
||||
expect(() => assertValidOneBotConfig(config)).toThrow(
|
||||
/duplicated in httpServers and httpClients/,
|
||||
);
|
||||
});
|
||||
|
||||
it('allows a disabled client draft with an empty URL', () => {
|
||||
const config = makeDefaultOneBotConfig();
|
||||
config.networks.httpClients.push({
|
||||
name: 'disabled-draft',
|
||||
enabled: false,
|
||||
url: '',
|
||||
messageFormat: 'array',
|
||||
reportSelfMessage: false,
|
||||
});
|
||||
|
||||
expect(() => assertValidOneBotConfig(config)).not.toThrow();
|
||||
});
|
||||
|
||||
it('rejects enabled clients with malformed or wrong-protocol URLs', () => {
|
||||
const http = makeDefaultOneBotConfig();
|
||||
http.networks.httpClients.push({
|
||||
name: 'bad-http',
|
||||
url: 'not a url',
|
||||
messageFormat: 'array',
|
||||
reportSelfMessage: false,
|
||||
});
|
||||
expect(() => assertValidOneBotConfig(http)).toThrow(/valid absolute URL/);
|
||||
|
||||
const ws = makeDefaultOneBotConfig();
|
||||
ws.networks.wsClients.push({
|
||||
name: 'bad-ws',
|
||||
url: 'https://example.com/socket',
|
||||
messageFormat: 'array',
|
||||
reportSelfMessage: false,
|
||||
});
|
||||
expect(() => assertValidOneBotConfig(ws)).toThrow(/protocol must be one of ws:, wss:/);
|
||||
});
|
||||
|
||||
it('rejects enabled servers with the same normalized host and port', () => {
|
||||
const config = makeDefaultOneBotConfig();
|
||||
config.networks.wsServers.push({
|
||||
name: 'conflicting-ws',
|
||||
host: '0.0.0.0',
|
||||
port: 3000,
|
||||
path: '/different-path',
|
||||
messageFormat: 'array',
|
||||
reportSelfMessage: false,
|
||||
});
|
||||
|
||||
expect(() => assertValidOneBotConfig(config)).toThrow(
|
||||
/conflicts with networks\.httpServers\[0\] on server binding 0\.0\.0\.0:3000/,
|
||||
);
|
||||
});
|
||||
|
||||
it('rejects blank hosts and wildcard/specific listeners on the same port', () => {
|
||||
const blank = makeDefaultOneBotConfig();
|
||||
blank.networks.httpServers[0].host = ' ';
|
||||
expect(() => assertValidOneBotConfig(blank)).toThrow(/\.host/);
|
||||
|
||||
const wildcard = makeDefaultOneBotConfig();
|
||||
wildcard.networks.wsServers.push({
|
||||
name: 'specific-ws',
|
||||
host: '127.0.0.1',
|
||||
port: 3000,
|
||||
path: '/ws',
|
||||
messageFormat: 'array',
|
||||
reportSelfMessage: false,
|
||||
});
|
||||
expect(() => assertValidOneBotConfig(wildcard)).toThrow(/wildcard server port 3000/);
|
||||
});
|
||||
|
||||
it('rejects timer values that Node would clamp to a 1ms loop', () => {
|
||||
const http = makeDefaultOneBotConfig();
|
||||
http.networks.httpClients.push({
|
||||
name: 'overflow-http',
|
||||
url: 'http://127.0.0.1:5700',
|
||||
timeoutMs: 1e100,
|
||||
messageFormat: 'array',
|
||||
reportSelfMessage: false,
|
||||
});
|
||||
expect(() => assertValidOneBotConfig(http)).toThrow(/timeoutMs/);
|
||||
|
||||
const ws = makeDefaultOneBotConfig();
|
||||
ws.networks.wsClients.push({
|
||||
name: 'overflow-ws',
|
||||
url: 'ws://127.0.0.1:5700',
|
||||
reconnectIntervalMs: 2_147_483_648,
|
||||
messageFormat: 'array',
|
||||
reportSelfMessage: false,
|
||||
});
|
||||
expect(() => assertValidOneBotConfig(ws)).toThrow(/reconnectIntervalMs/);
|
||||
});
|
||||
|
||||
it('rejects server paths that cannot match a URL pathname', () => {
|
||||
for (const invalidPath of ['api', '/api?token=x', '/api#fragment', ' /api']) {
|
||||
const config = makeDefaultOneBotConfig();
|
||||
config.networks.httpServers[0].path = invalidPath;
|
||||
expect(() => assertValidOneBotConfig(config), invalidPath).toThrow(/\.path/);
|
||||
}
|
||||
const valid = makeDefaultOneBotConfig();
|
||||
valid.networks.httpServers[0].path = '';
|
||||
expect(() => assertValidOneBotConfig(valid)).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('saveOneBotConfig validation boundary', () => {
|
||||
it('does not write an invalid enabled-client URL to disk', () => {
|
||||
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'snowluma-onebot-invalid-save-'));
|
||||
const previous = process.cwd();
|
||||
process.chdir(tempDir);
|
||||
try {
|
||||
const config = makeDefaultOneBotConfig();
|
||||
config.networks.wsClients.push({
|
||||
name: 'bad-client',
|
||||
url: 'javascript:alert(1)',
|
||||
messageFormat: 'array',
|
||||
reportSelfMessage: false,
|
||||
});
|
||||
|
||||
expect(() => saveOneBotConfig('10001', config)).toThrow(/protocol must be one of ws:, wss:/);
|
||||
expect(fs.existsSync(path.join(tempDir, 'config', 'onebot_10001.json'))).toBe(false);
|
||||
} finally {
|
||||
process.chdir(previous);
|
||||
fs.rmSync(tempDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('does not write a deterministic server bind conflict to disk', () => {
|
||||
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'snowluma-onebot-bind-conflict-'));
|
||||
const previous = process.cwd();
|
||||
process.chdir(tempDir);
|
||||
try {
|
||||
const config = makeDefaultOneBotConfig();
|
||||
config.networks.wsServers.push({
|
||||
name: 'same-bind',
|
||||
port: 3000,
|
||||
path: '/ws',
|
||||
messageFormat: 'array',
|
||||
reportSelfMessage: false,
|
||||
});
|
||||
|
||||
expect(() => saveOneBotConfig('10001', config)).toThrow(/server binding 0\.0\.0\.0:3000/);
|
||||
expect(fs.existsSync(path.join(tempDir, 'config', 'onebot_10001.json'))).toBe(false);
|
||||
} finally {
|
||||
process.chdir(previous);
|
||||
fs.rmSync(tempDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('preserves the previous desired config when the atomic rename fails', () => {
|
||||
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'snowluma-onebot-atomic-save-'));
|
||||
const previous = process.cwd();
|
||||
process.chdir(tempDir);
|
||||
try {
|
||||
const original = makeDefaultOneBotConfig();
|
||||
saveOneBotConfig('10001', original);
|
||||
const changed = structuredClone(original);
|
||||
changed.networks.httpServers[0].port = 3999;
|
||||
const rename = vi.spyOn(fs, 'renameSync').mockImplementationOnce(() => {
|
||||
throw new Error('injected rename failure');
|
||||
});
|
||||
expect(() => saveOneBotConfig('10001', changed)).toThrow(/injected rename failure/);
|
||||
rename.mockRestore();
|
||||
|
||||
expect(loadOneBotConfig('10001').networks.httpServers[0].port).toBe(3000);
|
||||
expect(fs.readdirSync(path.join(tempDir, 'config')).some((name) => name.endsWith('.tmp'))).toBe(false);
|
||||
} finally {
|
||||
vi.restoreAllMocks();
|
||||
process.chdir(previous);
|
||||
fs.rmSync(tempDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadOneBotConfig', () => {
|
||||
let tempDir: string;
|
||||
let prevCwd: string;
|
||||
@@ -147,6 +337,52 @@ describe('loadOneBotConfig', () => {
|
||||
expect(reloaded.networks.httpClients[0].reportSelfMessage).toBe(true);
|
||||
});
|
||||
|
||||
it('replays a saved per-UIN snapshot without reviving deleted global adapters', () => {
|
||||
const dir = path.join(tempDir, 'config');
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
const global = makeDefaultOneBotConfig();
|
||||
global.networks.httpServers[0].name = 'inherited';
|
||||
fs.writeFileSync(path.join(dir, 'onebot.json'), JSON.stringify(global), 'utf8');
|
||||
|
||||
const config = loadOneBotConfig('10004');
|
||||
expect(config.networks.httpServers.map((item) => item.name)).toContain('inherited');
|
||||
config.networks.httpServers = [];
|
||||
config.networks.wsServers.push({
|
||||
name: 'inherited',
|
||||
host: '127.0.0.1',
|
||||
port: 3998,
|
||||
path: '/',
|
||||
role: 'Universal',
|
||||
messageFormat: 'array',
|
||||
reportSelfMessage: false,
|
||||
});
|
||||
saveOneBotConfig('10004', config);
|
||||
|
||||
const reloaded = loadOneBotConfig('10004');
|
||||
expect(reloaded.networks.httpServers).toEqual([]);
|
||||
expect(reloaded.networks.wsServers.map((item) => item.name)).toContain('inherited');
|
||||
const disk = JSON.parse(fs.readFileSync(path.join(dir, 'onebot_10004.json'), 'utf8'));
|
||||
expect(disk.mode).toBe('snapshot');
|
||||
});
|
||||
|
||||
it('fails fast instead of replacing a corrupt per-UIN desired config with defaults', () => {
|
||||
const dir = path.join(tempDir, 'config');
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
fs.writeFileSync(path.join(dir, 'onebot_10005.json'), '{broken', 'utf8');
|
||||
|
||||
expect(() => loadOneBotConfig('10005', { persistDefaults: true })).toThrow(/is corrupt/);
|
||||
expect(fs.readFileSync(path.join(dir, 'onebot_10005.json'), 'utf8')).toBe('{broken');
|
||||
});
|
||||
|
||||
it('treats a valid JSON non-object root as corrupt desired state', () => {
|
||||
const dir = path.join(tempDir, 'config');
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
fs.writeFileSync(path.join(dir, 'onebot_10006.json'), '[]', 'utf8');
|
||||
|
||||
expect(() => loadOneBotConfig('10006', { persistDefaults: true })).toThrow(/root must be an object/);
|
||||
expect(fs.readFileSync(path.join(dir, 'onebot_10006.json'), 'utf8')).toBe('[]');
|
||||
});
|
||||
|
||||
it('fills new statusCommand fields with defaults when absent', () => {
|
||||
const uin = '10042';
|
||||
const dir = path.join(tempDir, 'config');
|
||||
@@ -337,4 +573,3 @@ describe('OneBotConfig.notifications (per-UIN channel opt-in)', () => {
|
||||
expect(onDisk.notifications).toEqual({ channelIds: ['discord', 'serverchan'] });
|
||||
});
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user