refactor: improve funbox-validation, add tests (@fehmer) (#6478)

This commit is contained in:
Christian Fehmer 2025-04-23 15:32:54 +02:00 committed by GitHub
parent d651f28256
commit 0c4352ee5f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 93 additions and 15 deletions

View file

@ -0,0 +1,74 @@
import { canSetConfigWithCurrentFunboxes } from "../../../src/ts/test/funbox/funbox-validation";
import * as Notifications from "../../../src/ts/elements/notifications";
describe("funbox-validation", () => {
describe("canSetConfigWithCurrentFunboxes", () => {
const addNotificationMock = vi.spyOn(Notifications, "add");
afterEach(() => {
addNotificationMock.mockReset();
});
const testCases = [
//checks for frontendForcedConfig
{
key: "mode",
value: "zen",
funbox: ["memory"],
error: "You can't set mode to zen with currently active funboxes.",
},
{ key: "mode", value: "words", funbox: ["memory"] }, //ok
//checks for zen mode
...[
"58008", //getWord
"wikipedia", //pullSection
"morse", //alterText
"polyglot", //withWords
"rAnDoMcAsE", //changesCapitalisation
"nospace", //nospace
"plus_one", //toPush:
"read_ahead_easy", //changesWordVisibility
"tts", //speaks
"layout_mirror", //changesLayout
"zipf", //changesWordsFrequency
].map((funbox) => ({
key: "mode",
value: "zen",
funbox: [funbox],
error: "You can't set mode to zen with currently active funboxes.",
})),
{ key: "mode", value: "zen", funbox: ["mirror"] }, //ok
{ key: "mode", value: "zen", funbox: ["space_balls"] }, //no frontendFunctions
//checks for words and custom
...["quote", "custom"].flatMap((value) =>
[
"58008", //getWord
"wikipedia", //pullSection
"polyglot", //withWords
"zipf", //changesWordsFrequency
].map((funbox) => ({
key: "mode",
value,
funbox: [funbox],
error: `You can't set mode to ${value} with currently active funboxes.`,
}))
),
{ key: "mode", value: "quote", funbox: ["space_balls"] }, //no frontendFunctions
];
it.for(testCases)(
`check $funbox with $key=$value`,
({ key, value, funbox, error }) => {
expect(
canSetConfigWithCurrentFunboxes(key, value, funbox.join("#"))
).toBe(error === undefined);
if (error !== undefined) {
expect(addNotificationMock).toHaveBeenCalledWith(error, 0, {
duration: 5,
});
}
}
);
});
});

View file

@ -86,18 +86,20 @@ export function canSetConfigWithCurrentFunboxes(
if (value === "zen") {
fb = fb.concat(
getFunboxesFromString(funbox).filter((f) => {
const funcs = f.frontendFunctions ?? [];
const props = f.properties ?? [];
return (
f.frontendFunctions?.includes("getWord") ||
f.frontendFunctions?.includes("pullSection") ||
f.frontendFunctions?.includes("alterText") ||
f.frontendFunctions?.includes("withWords") ||
f.properties?.includes("changesCapitalisation") ||
f.properties?.includes("nospace") ||
f.properties?.some((fp) => fp.startsWith("toPush:")) ||
f.properties?.includes("changesWordsVisibility") ||
f.properties?.includes("speaks") ||
f.properties?.includes("changesLayout") ||
f.properties?.includes("changesWordsFrequency")
funcs.includes("getWord") ||
funcs.includes("pullSection") ||
funcs.includes("alterText") ||
funcs.includes("withWords") ||
props.includes("changesCapitalisation") ||
props.includes("nospace") ||
props.some((fp) => fp.startsWith("toPush:")) ||
props.includes("changesWordsVisibility") ||
props.includes("speaks") ||
props.includes("changesLayout") ||
props.includes("changesWordsFrequency")
);
})
);
@ -105,11 +107,13 @@ export function canSetConfigWithCurrentFunboxes(
if (value === "quote" || value === "custom") {
fb = fb.concat(
getFunboxesFromString(funbox).filter((f) => {
const funcs = f.frontendFunctions ?? [];
const props = f.properties ?? [];
return (
f.frontendFunctions?.includes("getWord") ||
f.frontendFunctions?.includes("pullSection") ||
f.frontendFunctions?.includes("withWords") ||
f.properties?.includes("changesWordsFrequency")
funcs.includes("getWord") ||
funcs.includes("pullSection") ||
funcs.includes("withWords") ||
props.includes("changesWordsFrequency")
);
})
);