Skip to content

Commit 63321c1

Browse files
juliusmarmingecodex
andcommitted
Structure keybindings failure diagnostics
Co-authored-by: codex <[email protected]>
1 parent 6ea4fd3 commit 63321c1

5 files changed

Lines changed: 255 additions & 95 deletions

File tree

‎apps/server/src/keybindings.test.ts‎

Lines changed: 96 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import { KeybindingCommand, KeybindingRule, KeybindingsConfig } from "@t3tools/contracts";
22
import * as NodeServices from "@effect/platform-node/NodeServices";
33
import { assert, it } from "@effect/vitest";
4-
import { assertFailure } from "@effect/vitest/utils";
54
import * as Cause from "effect/Cause";
65
import * as Effect from "effect/Effect";
76
import * as FileSystem from "effect/FileSystem";
87
import * as Layer from "effect/Layer";
98
import * as Logger from "effect/Logger";
109
import * as Path from "effect/Path";
10+
import * as Result from "effect/Result";
1111
import * as Schema from "effect/Schema";
1212
import * as ServerConfig from "./config.ts";
1313
import * as Keybindings from "./keybindings.ts";
14-
import { KeybindingsConfigError } from "@t3tools/contracts";
1514

1615
const KeybindingsConfigJson = Schema.fromJsonString(KeybindingsConfig);
1716
const encodeKeybindingsConfigJson = Schema.encodeEffect(KeybindingsConfigJson);
@@ -34,12 +33,6 @@ const makeKeybindingsLayer = () => {
3433
);
3534
};
3635

37-
const toDetailResult = <A, R>(effect: Effect.Effect<A, KeybindingsConfigError, R>) =>
38-
effect.pipe(
39-
Effect.mapError((error) => error.detail),
40-
Effect.result,
41-
);
42-
4336
const writeKeybindingsConfig = (configPath: string, rules: readonly KeybindingRule[]) =>
4437
Effect.gen(function* () {
4538
const fileSystem = yield* FileSystem.FileSystem;
@@ -223,15 +216,21 @@ it.layer(NodeServices.layer)("keybindings", (it) => {
223216
assert.deepEqual(configState.issues, [
224217
{
225218
kind: "keybindings.malformed-config",
226-
message: configState.issues[0]?.message ?? "",
219+
message: "Expected the keybindings configuration to be a JSON array.",
227220
},
228221
]);
229222
assert.equal(yield* fs.readFileString(keybindingsConfigPath), "{ not-json");
230223
}).pipe(Effect.provide(makeKeybindingsLayer())),
231224
);
232225

233-
it.effect("ignores invalid entries in runtime and reports them as issues", () =>
234-
Effect.gen(function* () {
226+
it.effect("ignores invalid entries in runtime and reports them as issues", () => {
227+
const logs: ReadonlyArray<unknown>[] = [];
228+
const logger = Logger.make(({ message }) => {
229+
logs.push(Array.isArray(message) ? message : [message]);
230+
});
231+
const secret = "private-shortcut-payload";
232+
233+
return Effect.gen(function* () {
235234
const fs = yield* FileSystem.FileSystem;
236235
const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
237236
yield* fs.writeFileString(
@@ -240,7 +239,7 @@ it.layer(NodeServices.layer)("keybindings", (it) => {
240239
JSON.stringify([
241240
{ key: "mod+j", command: "terminal.toggle" },
242241
{ key: "mod+shift+d+o", command: "terminal.new" },
243-
{ key: "mod+x", command: "invalid.command" },
242+
{ key: "mod+x", command: secret },
244243
]),
245244
);
246245

@@ -250,23 +249,55 @@ it.layer(NodeServices.layer)("keybindings", (it) => {
250249
});
251250

252251
assert.isTrue(configState.keybindings.some((entry) => entry.command === "terminal.toggle"));
253-
assert.isFalse(
254-
configState.keybindings.some((entry) => String(entry.command) === "invalid.command"),
255-
);
252+
assert.isFalse(configState.keybindings.some((entry) => String(entry.command) === secret));
256253
assert.deepEqual(configState.issues, [
257254
{
258255
kind: "keybindings.invalid-entry",
259256
index: 1,
260-
message: configState.issues[0]?.message ?? "",
257+
message: "The keybinding entry contains an invalid shortcut or when expression.",
261258
},
262259
{
263260
kind: "keybindings.invalid-entry",
264261
index: 2,
265-
message: configState.issues[1]?.message ?? "",
262+
message: "Expected a keybinding entry with key, command, and optional when fields.",
266263
},
267264
]);
268-
}).pipe(Effect.provide(makeKeybindingsLayer())),
269-
);
265+
const invalidEntryLog = logs.find((log) => {
266+
const attributes = log[1];
267+
return (
268+
String(log[0]).includes("ignoring invalid keybinding entry") &&
269+
typeof attributes === "object" &&
270+
attributes !== null &&
271+
Reflect.get(attributes, "entryIndex") === 2
272+
);
273+
});
274+
if (!invalidEntryLog) {
275+
return assert.fail("Expected invalid keybinding warning");
276+
}
277+
const attributes = invalidEntryLog[1];
278+
if (typeof attributes !== "object" || attributes === null) {
279+
return assert.fail("Expected structured invalid keybinding attributes");
280+
}
281+
assert.equal(Reflect.get(attributes, "validationStage"), "entry-schema");
282+
assert.equal(Reflect.get(attributes, "validationInputKind"), "object");
283+
assert.equal(Reflect.get(attributes, "validationInputSize"), 2);
284+
assert.equal(Reflect.get(attributes, "validationHasKeyField"), true);
285+
assert.equal(Reflect.get(attributes, "validationHasCommandField"), true);
286+
assert.equal(Reflect.get(attributes, "validationHasWhenField"), false);
287+
assert.equal(Reflect.get(attributes, "causeReasonCount"), 1);
288+
assert.isFalse("entry" in attributes);
289+
assert.isFalse("cause" in attributes);
290+
assert.isFalse(String(invalidEntryLog[0]).includes(secret));
291+
assert.isFalse(Object.values(attributes).some((value) => String(value).includes(secret)));
292+
}).pipe(
293+
Effect.provide(
294+
Layer.mergeAll(
295+
makeKeybindingsLayer(),
296+
Logger.layer([logger], { mergeWithExisting: false }),
297+
),
298+
),
299+
);
300+
});
270301

271302
it.effect(
272303
"upserts missing default keybindings on startup without overriding existing command rules",
@@ -301,9 +332,9 @@ it.layer(NodeServices.layer)("keybindings", (it) => {
301332
);
302333

303334
it.effect("skips conflicting default keybindings on startup and logs a detailed warning", () => {
304-
const messages: string[] = [];
335+
const logs: ReadonlyArray<unknown>[] = [];
305336
const logger = Logger.make(({ message }) => {
306-
messages.push(String(message));
337+
logs.push(Array.isArray(message) ? message : [message]);
307338
});
308339

309340
return Effect.gen(function* () {
@@ -321,11 +352,21 @@ it.layer(NodeServices.layer)("keybindings", (it) => {
321352
assert.isFalse(persisted.some((entry) => entry.command === "terminal.toggle"));
322353
assert.isTrue(persisted.some((entry) => entry.command === "script.custom-action.run"));
323354

324-
assert.isTrue(
325-
messages.some((message) =>
326-
message.includes("skipping default keybinding due to shortcut conflict"),
327-
),
355+
const warning = logs.find((log) =>
356+
String(log[0]).includes("skipping default keybinding due to shortcut conflict"),
328357
);
358+
if (!warning) {
359+
return assert.fail("Expected shortcut conflict warning");
360+
}
361+
const attributes = warning[1];
362+
if (typeof attributes !== "object" || attributes === null) {
363+
return assert.fail("Expected structured shortcut conflict attributes");
364+
}
365+
assert.equal(Reflect.get(attributes, "defaultCommand"), "terminal.toggle");
366+
assert.equal(Reflect.get(attributes, "conflictingCommand"), "script.custom-action.run");
367+
assert.equal(Reflect.get(attributes, "hasWhenContext"), false);
368+
assert.isFalse("key" in attributes);
369+
assert.isFalse("when" in attributes);
329370
}).pipe(
330371
Effect.provide(
331372
Layer.mergeAll(
@@ -443,15 +484,24 @@ it.layer(NodeServices.layer)("keybindings", (it) => {
443484
key: "mod+shift+r",
444485
command: "script.run-tests.run",
445486
});
446-
}).pipe(toDetailResult);
447-
assertFailure(result, "expected JSON array");
487+
}).pipe(Effect.result);
488+
if (Result.isSuccess(result)) {
489+
return assert.fail("Expected malformed config update to fail");
490+
}
491+
assert.equal(result.failure._tag, "KeybindingsConfigError");
492+
assert.equal(result.failure.operation, "decode");
493+
assert.isTrue(Schema.isSchemaError(result.failure.cause));
494+
assert.equal(
495+
result.failure.message,
496+
`Keybindings config operation 'decode' failed at ${keybindingsConfigPath}.`,
497+
);
448498

449499
const persistedRaw = yield* fs.readFileString(keybindingsConfigPath);
450500
assert.equal(persistedRaw, "{ not-json");
451501
}).pipe(Effect.provide(makeKeybindingsLayer())),
452502
);
453503

454-
it.effect("reports non-array config parse errors without duplicate prefix", () =>
504+
it.effect("returns stable structured decode errors across retries", () =>
455505
Effect.gen(function* () {
456506
const fs = yield* FileSystem.FileSystem;
457507
const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
@@ -466,17 +516,26 @@ it.layer(NodeServices.layer)("keybindings", (it) => {
466516
key: "mod+shift+r",
467517
command: "script.run-tests.run",
468518
});
469-
}).pipe(toDetailResult);
470-
assertFailure(firstResult, "expected JSON array");
519+
}).pipe(Effect.result);
520+
if (Result.isSuccess(firstResult)) {
521+
return assert.fail("Expected first malformed config update to fail");
522+
}
523+
assert.equal(firstResult.failure.operation, "decode");
524+
assert.isTrue(Schema.isSchemaError(firstResult.failure.cause));
471525

472526
const secondResult = yield* Effect.gen(function* () {
473527
const keybindings = yield* Keybindings.Keybindings;
474528
return yield* keybindings.upsertKeybindingRule({
475529
key: "mod+shift+r",
476530
command: "script.run-tests.run",
477531
});
478-
}).pipe(toDetailResult);
479-
assertFailure(secondResult, "expected JSON array");
532+
}).pipe(Effect.result);
533+
if (Result.isSuccess(secondResult)) {
534+
return assert.fail("Expected second malformed config update to fail");
535+
}
536+
assert.equal(secondResult.failure.operation, "decode");
537+
assert.isTrue(Schema.isSchemaError(secondResult.failure.cause));
538+
assert.equal(secondResult.failure.message, firstResult.failure.message);
480539
}).pipe(Effect.provide(makeKeybindingsLayer())),
481540
);
482541

@@ -496,8 +555,11 @@ it.layer(NodeServices.layer)("keybindings", (it) => {
496555
key: "mod+shift+r",
497556
command: "script.run-tests.run",
498557
});
499-
}).pipe(toDetailResult);
500-
assertFailure(result, "failed to write keybindings config");
558+
}).pipe(Effect.result);
559+
if (Result.isSuccess(result)) {
560+
return assert.fail("Expected update in a read-only directory to fail");
561+
}
562+
assert.equal(result.failure.operation, "write");
501563

502564
yield* fs.chmod(dirname(keybindingsConfigPath), 0o700);
503565

0 commit comments

Comments
 (0)