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
64 changes: 54 additions & 10 deletions apps/server/src/keybindings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
KeybindingsLive,
ResolvedKeybindingFromConfig,
compileResolvedKeybindingRule,
compileResolvedKeybindingsConfig,
parseKeybindingShortcut,
} from "./keybindings";

Expand Down Expand Up @@ -166,6 +167,55 @@ it.layer(NodeServices.layer)("keybindings", (it) => {
}).pipe(Effect.provide(makeKeybindingsLayer())),
);

it.effect("uses defaults in runtime when config is malformed without overriding file", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const { keybindingsConfigPath } = yield* ServerConfig;
yield* fs.writeFileString(keybindingsConfigPath, "{ not-json");

const configState = yield* Effect.gen(function* () {
const keybindings = yield* Keybindings;
return yield* keybindings.loadConfigState;
});

assert.deepEqual(configState.keybindings, compileResolvedKeybindingsConfig(DEFAULT_KEYBINDINGS));
assert.deepEqual(configState.issues, [
{
kind: "keybindings.malformed-config",
message: configState.issues[0]?.message ?? "",
},
]);
assert.equal(yield* fs.readFileString(keybindingsConfigPath), "{ not-json");
}).pipe(Effect.provide(makeKeybindingsLayer())),
);

it.effect("ignores invalid entries in runtime and reports them as issues", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const { keybindingsConfigPath } = yield* ServerConfig;
yield* fs.writeFileString(
keybindingsConfigPath,
JSON.stringify([
{ key: "mod+j", command: "terminal.toggle" },
{ key: "mod+shift+d+o", command: "terminal.new" },
{ key: "mod+x", command: "invalid.command" },
]),
);

const configState = yield* Effect.gen(function* () {
const keybindings = yield* Keybindings;
return yield* keybindings.loadConfigState;
});

assert.isTrue(configState.keybindings.some((entry) => entry.command === "terminal.toggle"));
assert.isFalse(configState.keybindings.some((entry) => entry.command === "invalid.command"));
assert.deepEqual(configState.issues, [
{ kind: "keybindings.invalid-entry", index: 1, message: configState.issues[0]?.message ?? "" },
{ kind: "keybindings.invalid-entry", index: 2, message: configState.issues[1]?.message ?? "" },
]);
}).pipe(Effect.provide(makeKeybindingsLayer())),
);

it.effect(
"upserts missing default keybindings on startup without overriding existing command rules",
() =>
Expand Down Expand Up @@ -365,37 +415,31 @@ it.layer(NodeServices.layer)("keybindings", (it) => {

const [first, second] = yield* Effect.gen(function* () {
const keybindings = yield* Keybindings;
const firstLoad = yield* keybindings.loadResolvedKeybindingsConfig;
yield* writeKeybindingsConfig(keybindingsConfigPath, [
{ key: "mod+x", command: "script.setup.run" },
]);
const secondLoad = yield* keybindings.loadResolvedKeybindingsConfig;
const firstLoad = (yield* keybindings.loadConfigState).keybindings;
const secondLoad = (yield* keybindings.loadConfigState).keybindings;
return [firstLoad, secondLoad] as const;
});

assert.deepEqual(first, second);
assert.isTrue(second.some((entry) => entry.command === "terminal.toggle"));
assert.isFalse(second.some((entry) => entry.command === "script.setup.run"));
}).pipe(Effect.provide(makeKeybindingsLayer())),
);

it.effect("updates cached resolved config after upsert", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const { keybindingsConfigPath } = yield* ServerConfig;
yield* writeKeybindingsConfig(keybindingsConfigPath, [
{ key: "mod+j", command: "terminal.toggle" },
]);

const loadedAfterUpsert = yield* Effect.gen(function* () {
const keybindings = yield* Keybindings;
yield* keybindings.loadResolvedKeybindingsConfig;
yield* keybindings.loadConfigState;
yield* keybindings.upsertKeybindingRule({
key: "mod+shift+r",
command: "script.run-tests.run",
});
yield* fs.writeFileString(keybindingsConfigPath, "{ not-json");
return yield* keybindings.loadResolvedKeybindingsConfig;
return (yield* keybindings.loadConfigState).keybindings;
});

assert.isTrue(loadedAfterUpsert.some((entry) => entry.command === "script.run-tests.run"));
Expand Down
Loading
Loading