feat(onebot): migrate musicSignUrl from per-UIN to global config

musicSignUrl is a deployment-wide signing-service endpoint (same for every
account), so it belongs in the global config/snowluma.json next to rkey, not in
per-UIN onebot_<uin>.json.

- GlobalSettings gains musicSignUrl (http(s)-validated, '' = built-in default);
  consumption switches to globalSettings.musicSignUrl and hot-reloads via
  applyGlobalSettings.
- One-shot copy-up at boot (migrateGlobalSettings, called first in main()):
  lifts the first VALID legacy value from any config/onebot*.json into the
  global store. Idempotent via a key-PRESENCE guard, so an intentional
  clear-to-'' is never reverted by a stale legacy value.
- Remove musicSignUrl from per-UIN OneBotConfig (type + parse/serialize).

Phase 2 of the global-settings split. (WebUI form follows in Phase 3.)
This commit is contained in:
DrSmoothl committed 2026-06-28 21:38:32 +08:00
1 parent 659970077a
commit ea1c5ff9eb
8 files changed
+185 -23

No files matched your search

-3
View File
@@ -24,7 +24,6 @@ describe('makeDefaultOneBotConfig', () => {
expect(config.networks.wsServers[0].messageFormat).toBe('array');
expect(config.networks.wsServers[0].reportSelfMessage).toBe(false);
expect(config.networks.wsClients).toEqual([]);
expect(config.musicSignUrl).toBe('');
expect(config.statusCommand).toEqual({ enabled: true, swallow: false, cooldownSeconds: 5, trigger: '#sl' });
expect(config.notifications).toEqual({ channelIds: [] });
});
@@ -119,8 +118,6 @@ describe('loadOneBotConfig', () => {
expect(config.networks.wsClients[0].messageFormat).toBe('string');
expect(config.networks.wsClients[0].reportSelfMessage).toBe(true);
expect(config.musicSignUrl).toBe('https://example.com/sign');
// File should now be in unified format on disk.
const onDisk = JSON.parse(fs.readFileSync(path.join(dir, `onebot_${uin}.json`), 'utf8'));
expect(onDisk.networks).toBeDefined();
+83 -10
View File
@@ -5,6 +5,7 @@ import path from 'path';
import {
defaultGlobalSettings,
loadGlobalSettings,
migrateGlobalSettings,
normalizeGlobalSettings,
saveGlobalSettings,
} from '../src/global-config';
@@ -25,8 +26,8 @@ describe('global-config (config/snowluma.json)', () => {
});
it('defaults to an empty fallback list (feature off)', () => {
expect(defaultGlobalSettings()).toEqual({ rkey: { fallbackServers: [] } });
expect(loadGlobalSettings()).toEqual({ rkey: { fallbackServers: [] } });
expect(defaultGlobalSettings()).toEqual({ rkey: { fallbackServers: [] }, musicSignUrl: '' });
expect(loadGlobalSettings()).toEqual({ rkey: { fallbackServers: [] }, musicSignUrl: '' });
});
it('keeps only well-formed http(s) URLs, trims, and dedupes', () => {
@@ -38,8 +39,8 @@ describe('global-config (config/snowluma.json)', () => {
});
it('ignores a malformed rkey block', () => {
expect(normalizeGlobalSettings({ rkey: 'nope' })).toEqual({ rkey: { fallbackServers: [] } });
expect(normalizeGlobalSettings(null)).toEqual({ rkey: { fallbackServers: [] } });
expect(normalizeGlobalSettings({ rkey: 'nope' })).toEqual({ rkey: { fallbackServers: [] }, musicSignUrl: '' });
expect(normalizeGlobalSettings(null)).toEqual({ rkey: { fallbackServers: [] }, musicSignUrl: '' });
});
it('round-trips through save → disk → load', () => {
@@ -47,23 +48,95 @@ describe('global-config (config/snowluma.json)', () => {
expect(saved.rkey.fallbackServers).toEqual(['https://r.example/rkey']);
const onDisk = JSON.parse(fs.readFileSync(path.join(tempDir, 'config', 'snowluma.json'), 'utf8'));
expect(onDisk).toEqual({ rkey: { fallbackServers: ['https://r.example/rkey'] } });
expect(onDisk).toEqual({ rkey: { fallbackServers: ['https://r.example/rkey'] }, musicSignUrl: '' });
expect(loadGlobalSettings().rkey.fallbackServers).toEqual(['https://r.example/rkey']);
});
it('section-merges on save: a partial write never wipes a sibling section', () => {
saveGlobalSettings({ rkey: { fallbackServers: ['https://r.example/rkey'] } });
// A save that omits `rkey` (e.g. a future tenant saving only its own block)
// must preserve the existing rkey servers.
const after = saveGlobalSettings({});
// A save touching only musicSignUrl must preserve the rkey servers, and
// vice-versa.
const after = saveGlobalSettings({ musicSignUrl: 'https://sign.example/card' });
expect(after.rkey.fallbackServers).toEqual(['https://r.example/rkey']);
expect(loadGlobalSettings().rkey.fallbackServers).toEqual(['https://r.example/rkey']);
expect(after.musicSignUrl).toBe('https://sign.example/card');
expect(saveGlobalSettings({ rkey: { fallbackServers: [] } }).musicSignUrl).toBe('https://sign.example/card');
});
it('normalizes + round-trips musicSignUrl (trimmed)', () => {
expect(normalizeGlobalSettings({ musicSignUrl: ' https://s.example/c ' }).musicSignUrl).toBe('https://s.example/c');
const saved = saveGlobalSettings({ musicSignUrl: 'https://s.example/c' });
expect(saved.musicSignUrl).toBe('https://s.example/c');
expect(loadGlobalSettings().musicSignUrl).toBe('https://s.example/c');
});
describe('migrateGlobalSettings (musicSignUrl copy-up)', () => {
function writeOnebot(file: string, body: unknown) {
const dir = path.join(tempDir, 'config');
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(path.join(dir, file), JSON.stringify(body), 'utf8');
}
it('lifts a legacy per-UIN musicSignUrl into the global store', () => {
writeOnebot('onebot_123.json', { musicSignUrl: 'https://legacy.example/sign' });
migrateGlobalSettings();
expect(loadGlobalSettings().musicSignUrl).toBe('https://legacy.example/sign');
});
it('is a no-op when the global value is already set', () => {
saveGlobalSettings({ musicSignUrl: 'https://kept.example/sign' });
writeOnebot('onebot_123.json', { musicSignUrl: 'https://legacy.example/sign' });
migrateGlobalSettings();
expect(loadGlobalSettings().musicSignUrl).toBe('https://kept.example/sign');
});
it('takes the first file and tolerates differing/empty values across files', () => {
writeOnebot('onebot.json', { musicSignUrl: '' }); // empty → skipped
writeOnebot('onebot_111.json', { musicSignUrl: 'https://a.example/s' });
writeOnebot('onebot_222.json', { musicSignUrl: 'https://b.example/s' });
migrateGlobalSettings();
// onebot.json sorts first but is empty; onebot_111 is the first non-empty.
expect(loadGlobalSettings().musicSignUrl).toBe('https://a.example/s');
});
it('does nothing when no legacy value exists', () => {
writeOnebot('onebot_123.json', { networks: {} });
migrateGlobalSettings();
expect(loadGlobalSettings().musicSignUrl).toBe('');
});
it('migrates a value carried by the base onebot.json', () => {
writeOnebot('onebot.json', { musicSignUrl: 'https://base.example/s' });
migrateGlobalSettings();
expect(loadGlobalSettings().musicSignUrl).toBe('https://base.example/s');
});
it('skips an invalid earlier value and migrates the first VALID one', () => {
writeOnebot('onebot.json', { musicSignUrl: 'not-a-url' }); // earlier-sorting garbage
writeOnebot('onebot_111.json', { musicSignUrl: 'https://real.example/s' });
migrateGlobalSettings();
expect(loadGlobalSettings().musicSignUrl).toBe('https://real.example/s');
});
it('does NOT re-copy after an intentional clear-to-empty (presence guard)', () => {
// Operator cleared the global value to '' (the key is present in the file)…
saveGlobalSettings({ musicSignUrl: '' });
// …while a stale legacy value still lingers.
writeOnebot('onebot_123.json', { musicSignUrl: 'https://legacy.example/sign' });
migrateGlobalSettings();
expect(loadGlobalSettings().musicSignUrl).toBe(''); // stays cleared, not reverted
});
it('is a safe no-op when config/ does not exist', () => {
// fresh cwd, no config dir at all
expect(() => migrateGlobalSettings()).not.toThrow();
expect(loadGlobalSettings().musicSignUrl).toBe('');
});
});
it('falls back to defaults when the file is corrupt', () => {
fs.mkdirSync(path.join(tempDir, 'config'), { recursive: true });
fs.writeFileSync(path.join(tempDir, 'config', 'snowluma.json'), '{ not json', 'utf8');
expect(loadGlobalSettings()).toEqual({ rkey: { fallbackServers: [] } });
expect(loadGlobalSettings()).toEqual({ rkey: { fallbackServers: [] }, musicSignUrl: '' });
});
});