Spaces:
Paused
Paused
File size: 7,397 Bytes
ff7a361 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | import { describe, expect, it, vi } from "vitest";
import type { ServerConfig } from "./config";
import {
applyServerConfig,
defaultSettings,
getDefaultCpuThreads,
getInferenceTypes,
prefersLocalDictationModel,
} from "./settings";
const mockConfig: ServerConfig = {
accessKeysEnabled: false,
accessKeyTimeoutHours: 0,
wllamaDefaultModelId: "littlelamb-290m",
internalApiEnabled: false,
internalApiName: "Internal API",
defaultInferenceType: "browser",
searchToken: "a".repeat(64),
};
describe("Settings Module", () => {
it("should have correct default settings values", () => {
expect(defaultSettings.showEnableAiResponsePrompt).toBe(true);
expect(defaultSettings.enableAiResponse).toBe(false);
expect(defaultSettings.enableImageSearch).toBe(true);
expect(defaultSettings.searchResultsLimit).toBe(15);
expect(defaultSettings.enablePageContentFetch).toBe(true);
expect(defaultSettings.inferenceType).toBeDefined();
});
describe("getDefaultCpuThreads", () => {
it("should always leave at least one thread on tiny machines", () => {
expect(getDefaultCpuThreads(1)).toBe(1);
expect(getDefaultCpuThreads(2)).toBe(1);
});
it("should scale with the machine instead of using a fixed ceiling", () => {
expect(getDefaultCpuThreads(4)).toBe(2);
expect(getDefaultCpuThreads(8)).toBe(4);
expect(getDefaultCpuThreads(16)).toBe(8);
expect(getDefaultCpuThreads(32)).toBe(16);
expect(getDefaultCpuThreads(128)).toBe(64);
});
it("should round down on an odd processor count", () => {
expect(getDefaultCpuThreads(3)).toBe(1);
expect(getDefaultCpuThreads(9)).toBe(4);
});
});
describe("prefersLocalDictationModel", () => {
it("is true when the primary language is English", () => {
expect(prefersLocalDictationModel("en")).toBe(true);
expect(prefersLocalDictationModel("en-US")).toBe(true);
expect(prefersLocalDictationModel("en-GB")).toBe(true);
expect(prefersLocalDictationModel("EN")).toBe(true);
});
it("is false when the primary language is not English", () => {
expect(prefersLocalDictationModel("pt")).toBe(false);
expect(prefersLocalDictationModel("pt-BR")).toBe(false);
expect(prefersLocalDictationModel("de-DE")).toBe(false);
});
it("compares the subtag exactly instead of by prefix", () => {
// `startsWith("en")` would match historical subtags like Middle
// English, which no browser UI actually uses.
expect(prefersLocalDictationModel("enm")).toBe(false);
});
});
it("should include core inference types", () => {
const values = getInferenceTypes(mockConfig).map((i) => i.value);
expect(values).toContain("browser");
expect(values).toContain("openai");
expect(values).toContain("horde");
});
it("should include internal API when enabled in config", () => {
const configWithInternalApi = {
...mockConfig,
internalApiEnabled: true,
internalApiName: "Custom LLM",
};
const values = getInferenceTypes(configWithInternalApi).map((i) => i.value);
expect(values).toContain("internal");
expect(
getInferenceTypes(configWithInternalApi).find(
(i) => i.value === "internal",
)?.label,
).toBe("Custom LLM");
});
it("should exclude internal API when disabled in config", () => {
const values = getInferenceTypes(mockConfig).map((i) => i.value);
expect(values).not.toContain("internal");
});
it("should apply server config defaults to a profile that was never saved", () => {
const config = {
...mockConfig,
wllamaDefaultModelId: "custom-model",
defaultInferenceType: "internal",
};
const applied = applyServerConfig(defaultSettings, config, false);
expect(applied.wllamaModelId).toBe("custom-model");
expect(applied.inferenceType).toBe("internal");
});
it("should preserve stored settings that differ from the server defaults", () => {
const config = {
...mockConfig,
wllamaDefaultModelId: "different-model",
defaultInferenceType: "openai",
};
const userSettings = {
...defaultSettings,
wllamaModelId: "user-picked-model",
inferenceType: "horde",
};
const applied = applyServerConfig(userSettings, config, true);
expect(applied.wllamaModelId).toBe("user-picked-model");
expect(applied.inferenceType).toBe("horde");
});
it("should preserve a stored setting that happens to equal a shipped default", () => {
const config = {
...mockConfig,
wllamaDefaultModelId: "admin-preferred-model",
defaultInferenceType: "internal",
};
const applied = applyServerConfig(defaultSettings, config, true);
expect(applied.wllamaModelId).toBe(defaultSettings.wllamaModelId);
expect(applied.inferenceType).toBe(defaultSettings.inferenceType);
});
});
describe("legacy voice migration", () => {
const load = async (stored: Record<string, unknown> | null) => {
localStorage.clear();
if (stored) localStorage.setItem("settings", JSON.stringify(stored));
vi.resetModules();
return import("./settings");
};
it("keeps the system engine for a profile that picked an OS voice", async () => {
const { defaultSettings: settings } = await load({
selectedVoiceId: "urn:moz-tts:speechd:English (America)",
});
expect(settings.textToSpeechEngine).toBe("system");
});
it("uses the local engine for a profile that never picked a voice", async () => {
const { defaultSettings: settings } = await load({ selectedVoiceId: "" });
expect(settings.textToSpeechEngine).toBe("local");
});
it("leaves an already-migrated profile alone", async () => {
const { defaultSettings: settings } = await load({
selectedVoiceId: "urn:moz-tts:speechd:English (America)",
textToSpeechEngine: "local",
});
expect(settings.textToSpeechEngine).toBe("local");
});
it("uses the local engine for a fresh profile", async () => {
const { defaultSettings: settings } = await load(null);
expect(settings.textToSpeechEngine).toBe("local");
});
});
describe("dictation model default", () => {
const loadWithLanguage = async (language: string) => {
// Scoped restore rather than vi.restoreAllMocks(): the setup file installs
// matchMedia as a mock, and a broad restore would take it down with the
// language spy for anything that runs after this describe.
const languageSpy = vi
.spyOn(navigator, "language", "get")
.mockReturnValue(language);
vi.resetModules();
const module = await import("./settings");
languageSpy.mockRestore();
return module;
};
it("starts a non-English profile on the browser recognizer", async () => {
// The wiring from prefersLocalDictationModel into defaultSettings: a
// literal `true` here would leave every non-English first visit on a
// model that cannot transcribe their speech, while the pure function
// kept passing its own tests.
const { defaultSettings: settings } = await loadWithLanguage("pt-BR");
expect(settings.enableLocalDictationModel).toBe(false);
});
it("starts an English profile on the on-device model", async () => {
const { defaultSettings: settings } = await loadWithLanguage("en-US");
expect(settings.enableLocalDictationModel).toBe(true);
});
});
|