opencode-voice-dictation/tests/config.test.ts
opencode-agent 3bd443dde3 feat: initial implementation of OpenCode Voice Dictation
- TypeScript + Vite (vite-plugin-monkey) userscript
- Groq API integration with whisper-large-v3
- Tap-to-toggle recording with noise suppression
- Contenteditable text insertion for OpenCode web
- Ctrl+Space keyboard shortcut (desktop)
- Custom Whisper prompt for software development context
- Recording timer, toast notifications, auto-submit option
- 44 unit tests, 100% line coverage
- Biome linter, Knip, Vitest, CI/CD pipeline
- Bilingual README (EN/RU) with compatibility table
2026-07-08 05:37:17 +00:00

115 lines
3.2 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
vi.mock("$", () => ({
GM_getValue: vi.fn((key: string, defaultValue: unknown) => defaultValue),
GM_setValue: vi.fn(),
GM_registerMenuCommand: vi.fn(),
GM_xmlhttpRequest: vi.fn(),
}));
import { GM_getValue, GM_registerMenuCommand, GM_setValue } from "$";
import {
DEFAULTS,
getConfig,
isFirstRun,
registerMenuCommands,
setConfig,
validateApiKey,
} from "../src/config.js";
describe("DEFAULTS", () => {
it("should have whisper-large-v3 as default model", () => {
expect(DEFAULTS.model).toBe("whisper-large-v3");
});
it("should have empty language for auto-detect", () => {
expect(DEFAULTS.language).toBe("");
});
it("should have non-empty whisperPrompt", () => {
expect(DEFAULTS.whisperPrompt.length).toBeGreaterThan(50);
});
it("should have autoSubmit disabled by default", () => {
expect(DEFAULTS.autoSubmit).toBe(false);
});
});
describe("validateApiKey", () => {
it("should accept valid key with gsk_ prefix", () => {
expect(validateApiKey("gsk_test_fake_key_1234567890abcdef")).toBe(true);
});
it("should reject key without gsk_ prefix", () => {
expect(validateApiKey("sk_test_key_12345")).toBe(false);
});
it("should reject key that is too short", () => {
expect(validateApiKey("gsk_short")).toBe(false);
});
it("should reject empty string", () => {
expect(validateApiKey("")).toBe(false);
});
});
describe("getConfig", () => {
it("should return defaults when GM storage is empty", () => {
vi.mocked(GM_getValue).mockImplementation((key: string, def: unknown) => def);
const config = getConfig();
expect(config.model).toBe(DEFAULTS.model);
expect(config.language).toBe(DEFAULTS.language);
expect(config.groqApiKey).toBe("");
});
it("should return stored values when present", () => {
vi.mocked(GM_getValue).mockImplementation((key: string, def: unknown) => {
if (key === "groqApiKey") {
return "gsk_stored_key";
}
if (key === "model") {
return "whisper-large-v3-turbo";
}
return def;
});
const config = getConfig();
expect(config.groqApiKey).toBe("gsk_stored_key");
expect(config.model).toBe("whisper-large-v3-turbo");
});
});
describe("isFirstRun", () => {
it("should return true when no API key stored", () => {
vi.mocked(GM_getValue).mockReturnValue("");
expect(isFirstRun()).toBe(true);
});
it("should return false when API key is stored", () => {
vi.mocked(GM_getValue).mockReturnValue("gsk_stored_key");
expect(isFirstRun()).toBe(false);
});
});
describe("setConfig", () => {
it("should call GM_setValue for each key", () => {
setConfig({ groqApiKey: "gsk_new", model: "whisper-large-v3-turbo" });
expect(GM_setValue).toHaveBeenCalledWith("groqApiKey", "gsk_new");
expect(GM_setValue).toHaveBeenCalledWith("model", "whisper-large-v3-turbo");
});
});
describe("registerMenuCommands", () => {
it("should register all menu commands", () => {
const callbacks = {
onSetKey: () => {},
onToggleAutoSubmit: () => {},
onSetModel: () => {},
onSetLanguage: () => {},
onSetPrompt: () => {},
};
registerMenuCommands(callbacks);
expect(GM_registerMenuCommand).toHaveBeenCalledTimes(5);
});
});