Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/__tests__/root/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
Config as ConfigType,
CaretStyleSchema,
} from "@monkeytype/schemas/configs";
import * as FunboxValidation from "../../src/ts/test/funbox/funbox-validation";
import * as FunboxValidation from "../../src/ts/config/funbox-validation";
import * as ConfigValidation from "../../src/ts/config/validation";
import * as ConfigEvent from "../../src/ts/observables/config-event";
import * as ApeConfig from "../../src/ts/ape/config";
Expand Down
47 changes: 21 additions & 26 deletions frontend/__tests__/test/funbox/funbox-validation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
import { describe, it, expect, afterEach, vi } from "vitest";
import { canSetConfigWithCurrentFunboxes } from "../../../src/ts/test/funbox/funbox-validation";
import { describe, it, expect } from "vitest";
import { canSetConfigWithCurrentFunboxes } from "../../../src/ts/config/funbox-validation";

import * as Notifications from "../../../src/ts/states/notifications";
import { FunboxName } from "@monkeytype/schemas/configs";
describe("funbox-validation", () => {
describe("canSetConfigWithCurrentFunboxes", () => {
const addNotificationMock = vi.spyOn(
Notifications,
"showNoticeNotification",
);
afterEach(() => {
addNotificationMock.mockClear();
});

const testCases = [
//checks for frontendForcedConfig
{
key: "mode",
value: "zen",
funbox: ["memory"],
error: "You can't set mode to zen with currently active funboxes.",
expected: false,
},
{ key: "mode", value: "words", funbox: ["memory"] }, //ok
{ key: "mode", value: "words", funbox: ["memory"], expected: true },

//checks for zen mode
...[
Expand All @@ -40,10 +31,15 @@ describe("funbox-validation", () => {
key: "mode",
value: "zen",
funbox: [funbox],
error: "You can't set mode to zen with currently active funboxes.",
expected: false,
})),
{ key: "mode", value: "zen", funbox: ["mirror"] }, //ok
{ key: "mode", value: "zen", funbox: ["space_balls"] }, //no frontendFunctions
{ key: "mode", value: "zen", funbox: ["mirror"], expected: true },
{
key: "mode",
value: "zen",
funbox: ["space_balls"],
expected: true,
},

//checks for words and custom
...["quote", "custom"].flatMap((value) =>
Expand All @@ -56,23 +52,22 @@ describe("funbox-validation", () => {
key: "mode",
value,
funbox: [funbox],
error: `You can't set mode to ${value} with currently active funboxes.`,
expected: false,
})),
),
{ key: "mode", value: "quote", funbox: ["space_balls"] }, //no frontendFunctions
{
key: "mode",
value: "quote",
funbox: ["space_balls"],
expected: true,
},
];
it.for(testCases)(
`check $funbox with $key=$value`,
({ key, value, funbox, error }) => {
({ key, value, funbox, expected }) => {
expect(
canSetConfigWithCurrentFunboxes(key, value, funbox as FunboxName[]),
).toBe(error === undefined);

if (error !== undefined) {
expect(addNotificationMock).toHaveBeenCalledWith(error, {
durationMs: 5000,
});
}
).toBe(expected);
},
);
});
Expand Down
42 changes: 22 additions & 20 deletions frontend/src/terms-of-service.html
Original file line number Diff line number Diff line change
Expand Up @@ -211,26 +211,28 @@ <h1>Limitations</h1>
Users as a whole.
</p>
<h1>Privacy Policy</h1>
If you use our Services, you must abide by our Privacy Policy. You
acknowledge that you have read our
<a
href="https://monkeytype.com/privacy-policy"
target="_blank"
rel="noreferrer noopener"
>
Privacy Policy
</a>
&nbsp;and understand that it sets forth how we collect, use, and store
your information. If you do not agree with our Privacy Statement, then
you must stop using the Services immediately. Any person, entity, or
service collecting data from the Services must comply with our Privacy
Statement. Misuse of any User's Personal Information is prohibited. If
you collect any Personal Information from a User, you agree that you
will only use the Personal Information you gather for the purpose for
which the User has authorized it. You agree that you will reasonably
secure any Personal Information you have gathered from the Services, and
you will respond promptly to complaints, removal requests, and 'do not
contact' requests from us or Users.
<div>
If you use our Services, you must abide by our Privacy Policy. You
acknowledge that you have read our
<a
href="https://monkeytype.com/privacy-policy"
target="_blank"
rel="noreferrer noopener"
>
Privacy Policy
</a>
&nbsp;and understand that it sets forth how we collect, use, and store
your information. If you do not agree with our Privacy Statement, then
you must stop using the Services immediately. Any person, entity, or
service collecting data from the Services must comply with our Privacy
Statement. Misuse of any User's Personal Information is prohibited. If
you collect any Personal Information from a User, you agree that you
will only use the Personal Information you gather for the purpose for
which the User has authorized it. You agree that you will reasonably
secure any Personal Information you have gathered from the Services,
and you will respond promptly to complaints, removal requests, and 'do
not contact' requests from us or Users.
</div>

<h1>Limitations on Automated Use</h1>
You shouldn't use bots or access our Services in malicious or prohibited
Expand Down
89 changes: 89 additions & 0 deletions frontend/src/ts/config/funbox-validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { checkForcedConfig, getFunbox } from "@monkeytype/funbox";
import { Config, ConfigValue, FunboxName } from "@monkeytype/schemas/configs";

export function canSetConfigWithCurrentFunboxes(
key: string,
value: ConfigValue,
funbox: FunboxName[] = [],
): boolean {
const funboxes = getFunbox(funbox);
if (key === "mode") {
let fb = getFunbox(funbox).filter(
(f) =>
f.frontendForcedConfig?.["mode"] !== undefined &&
!(f.frontendForcedConfig["mode"] as ConfigValue[]).includes(value),
);
if (value === "zen") {
fb = fb.concat(
funboxes.filter((f) => {
const funcs = f.frontendFunctions ?? [];
const props = f.properties ?? [];
return (
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")
);
}),
);
}
if (value === "quote" || value === "custom") {
fb = fb.concat(
funboxes.filter((f) => {
const funcs = f.frontendFunctions ?? [];
const props = f.properties ?? [];
return (
funcs.includes("getWord") ||
funcs.includes("pullSection") ||
funcs.includes("withWords") ||
props.includes("changesWordsFrequency")
);
}),
);
}

if (fb.length > 0) {
return false;
}
}
if (!checkForcedConfig(key, value, funboxes).result) {
return false;
}

return true;
}

export type FunboxConfigError = {
key: string;
value: ConfigValue;
};

export function canSetFunboxWithConfig(
funbox: FunboxName,
config: Config,
): { ok: true } | { ok: false; errors: FunboxConfigError[] } {
const funboxToCheck = [...config.funbox, funbox];

const errors: FunboxConfigError[] = [];
for (const [configKey, configValue] of Object.entries(config)) {
if (
!canSetConfigWithCurrentFunboxes(configKey, configValue, funboxToCheck)
) {
errors.push({
key: configKey,
value: configValue,
});
}
}
if (errors.length > 0) {
return { ok: false, errors };
}
return { ok: true };
}
7 changes: 4 additions & 3 deletions frontend/src/ts/config/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { checkCompatibility } from "@monkeytype/funbox";
import * as DB from "../db";
import { showNoticeNotification } from "../states/notifications";
import { isAuthenticated } from "../firebase";
import { canSetFunboxWithConfig } from "../test/funbox/funbox-validation";
import { canSetFunboxWithConfig } from "./funbox-validation";
import { reloadAfter } from "../utils/misc";
import { isDevEnvironment } from "../utils/env";
import * as ConfigSchemas from "@monkeytype/schemas/configs";
Expand Down Expand Up @@ -307,9 +307,10 @@ export const configMetadata: ConfigMetadataObject = {
}

for (const funbox of value) {
if (!canSetFunboxWithConfig(funbox, currentConfig)) {
const check = canSetFunboxWithConfig(funbox, currentConfig);
if (!check.ok) {
showNoticeNotification(
`${value}" cannot be enabled with the current config`,
`"${funbox}" cannot be enabled with the current config`,
);
return true;
}
Expand Down
28 changes: 25 additions & 3 deletions frontend/src/ts/config/setters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import { showNoticeNotification } from "../states/notifications";
import {
canSetConfigWithCurrentFunboxes,
canSetFunboxWithConfig,
} from "../test/funbox/funbox-validation";
} from "./funbox-validation";
import * as TestState from "../test/test-state";
import { typedKeys, triggerResize } from "../utils/misc";
import { typedKeys, triggerResize, escapeHTML } from "../utils/misc";
import { camelCaseToWords, capitalizeFirstLetter } from "../utils/strings";
import { Config, setConfigStore } from "./store";
import { FunboxName } from "@monkeytype/schemas/configs";

Expand Down Expand Up @@ -77,6 +78,16 @@ export function setConfig<T extends keyof ConfigSchemas.Config>(
}

if (!canSetConfigWithCurrentFunboxes(key, value, Config.funbox)) {
if (key === "words" || key === "time") {
showNoticeNotification("Active funboxes do not support infinite tests");
} else {
showNoticeNotification(
`You can't set ${camelCaseToWords(
key,
)} to ${String(value)} with currently active funboxes.`,
{ durationMs: 5000 },
);
}
console.warn(
`Could not set config key "${key}" with value "${JSON.stringify(
value,
Expand Down Expand Up @@ -152,7 +163,18 @@ export function toggleFunbox(funbox: FunboxName, nosave?: boolean): boolean {
return false;
}

if (!canSetFunboxWithConfig(funbox, Config)) {
const funboxCheck = canSetFunboxWithConfig(funbox, Config);
if (!funboxCheck.ok) {
const errorStrings = funboxCheck.errors.map(
(e) =>
`${capitalizeFirstLetter(
camelCaseToWords(e.key),
)} cannot be set to ${String(e.value)}.`,
);
showNoticeNotification(
`You can't enable ${escapeHTML(funbox.replace(/_/g, " "))}:<br />${errorStrings.map((s) => escapeHTML(s)).join("<br />")}`,
{ durationMs: 5000, useInnerHtml: true },
);
return false;
}

Expand Down
Loading
Loading