import { mkdtempSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; import { defaultSettings, loadSettings, parseSettings, SettingsError } from "../src/core/settings.js"; const ID = "123456789012345678"; describe("settings", () => { it("an empty file gives the defaults", () => { expect(parseSettings({})).toEqual(defaultSettings()); }); it("a missing file gives the defaults", () => { const dir = mkdtempSync(join(process.env.TMPDIR ?? tmpdir(), "jarvis-settings-")); expect(loadSettings(join(dir, "nope.json"))).toEqual(defaultSettings()); }); it("reads a full file, BOM included", () => { const dir = mkdtempSync(join(process.env.TMPDIR ?? tmpdir(), "jarvis-settings-")); const path = join(dir, "settings.json"); writeFileSync(path, "" + JSON.stringify({ discord: { guildId: ID, voiceChannelId: ID, commandName: "jarvis", ephemeralReplies: false }, voice: { maxSpeakers: 3, listenToBots: true, joinNotice: "알림", wakeWords: [" 자비스 ", "", "javis"], stt: { end_silence_ms: 250 } }, chatChannels: [{ id: ID, name: "일정", description: "일정 올리는 곳", send: false }], voiceWorker: { python: "/venv/bin/python", cwd: "/repo/src", args: ["--melo-python", "/m/python"] }, somethingNew: 1, })); const s = loadSettings(path); expect(s.discord).toEqual({ guildId: ID, voiceChannelId: ID, commandName: "jarvis", ephemeralReplies: false }); expect(s.voice).toEqual({ maxSpeakers: 3, listenToBots: true, joinNotice: "알림", wakeWords: ["자비스", "javis"], stt: { end_silence_ms: 250 } }); expect(s.chatChannels).toEqual([{ id: ID, name: "일정", description: "일정 올리는 곳", read: true, send: false }]); expect(s.voiceWorker.args).toEqual(["--melo-python", "/m/python"]); }); it.each([ [{ discord: { guildId: "abc" } }, /discord.guildId/], [{ discord: { commandName: "Jarvis" } }, /commandName/], [{ discord: { commandName: "두 단어" } }, /commandName/], [{ voice: { maxSpeakers: 11 } }, /maxSpeakers/], [{ voice: { maxSpeakers: 0 } }, /maxSpeakers/], [{ voice: { joinNotice: " " } }, /joinNotice/], [{ chatChannels: [{ id: ID }, { id: ID }] }, /twice/], [{ chatChannels: [{ id: "1" }] }, /chatChannels\[0\].id/], [{ chatChannels: {} }, /chatChannels/], [{ voice: [] }, /voice/], [[], /object/], ])("refuses %j", (raw, message) => { expect(() => parseSettings(raw)).toThrow(SettingsError); expect(() => parseSettings(raw)).toThrow(message); }); it("reports invalid JSON with the path", () => { const dir = mkdtempSync(join(process.env.TMPDIR ?? tmpdir(), "jarvis-settings-")); const path = join(dir, "settings.json"); writeFileSync(path, "{ nope"); expect(() => loadSettings(path)).toThrow(/not valid JSON/); }); });