Files
t3code/apps/web/src/lib/projectScriptKeybindings.test.ts
T
Alex Southwellandgithub-actions[bot] d8bc6831cd fix(projects): prevent invalid script IDs from crashing threads (#10019)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2026-09-07 00:16:53 -07:00

93 lines
2.6 KiB
TypeScript

import { MAX_KEYBINDING_VALUE_LENGTH, type KeybindingCommand } from "@t3tools/contracts";
import { describe, expect, it } from "vite-plus/test";
import { commandForProjectScript } from "../projectScripts";
import {
decodeProjectScriptKeybindingRule,
keybindingValueForCommand,
PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE,
} from "./projectScriptKeybindings";
describe("projectScriptKeybindings", () => {
it("decodes and trims valid keybinding rules", () => {
const rule = decodeProjectScriptKeybindingRule({
keybinding: " mod+k ",
command: commandForProjectScript("lint"),
});
expect(rule).toEqual({
key: "mod+k",
command: "script.lint.run",
});
});
it("returns null when keybinding is empty", () => {
expect(
decodeProjectScriptKeybindingRule({
keybinding: " ",
command: commandForProjectScript("lint"),
}),
).toBeNull();
});
it("rejects invalid keybinding values", () => {
expect(() =>
decodeProjectScriptKeybindingRule({
keybinding: "k".repeat(MAX_KEYBINDING_VALUE_LENGTH + 1),
command: commandForProjectScript("lint"),
}),
).toThrowError(PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE);
});
it("rejects invalid commands", () => {
expect(() =>
decodeProjectScriptKeybindingRule({
keybinding: "mod+k",
command: "script.BAD.run" as KeybindingCommand,
}),
).toThrowError(PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE);
});
it("can edit or delete a legacy script without a shortcut", () => {
const command = commandForProjectScript("install-javascript-dependencies");
expect(keybindingValueForCommand([], command)).toBeNull();
expect(decodeProjectScriptKeybindingRule({ keybinding: null, command })).toBeNull();
expect(() => decodeProjectScriptKeybindingRule({ keybinding: "mod+k", command })).toThrowError(
PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE,
);
});
it("reads latest matching keybinding value for a command", () => {
const command = "script.test.run" as const;
const value = keybindingValueForCommand(
[
{
command,
shortcut: {
key: "escape",
metaKey: false,
ctrlKey: false,
shiftKey: false,
altKey: false,
modKey: true,
},
},
{
command,
shortcut: {
key: "k",
metaKey: false,
ctrlKey: false,
shiftKey: true,
altKey: false,
modKey: true,
},
},
],
command,
);
expect(value).toBe("mod+shift+k");
});
});