keybindings-migration.test.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import * as fs from "node:fs";
  2. import * as os from "node:os";
  3. import * as path from "node:path";
  4. import { afterEach, describe, expect, it } from "vitest";
  5. import { ENV_AGENT_DIR } from "../src/config.ts";
  6. import { KeybindingsManager } from "../src/core/keybindings.ts";
  7. import { runMigrations } from "../src/migrations.ts";
  8. describe("keybindings migration", () => {
  9. const tempDirs: string[] = [];
  10. afterEach(() => {
  11. for (const dir of tempDirs.splice(0)) {
  12. fs.rmSync(dir, { recursive: true, force: true });
  13. }
  14. });
  15. function createAgentDir(config: Record<string, unknown>): string {
  16. const agentDir = fs.mkdtempSync(path.join(os.tmpdir(), "pi-keybindings-test-"));
  17. tempDirs.push(agentDir);
  18. fs.writeFileSync(path.join(agentDir, "keybindings.json"), `${JSON.stringify(config, null, 2)}\n`, "utf-8");
  19. return agentDir;
  20. }
  21. it("rewrites old key names to namespaced ids", () => {
  22. const agentDir = createAgentDir({
  23. cursorUp: ["up", "ctrl+p"],
  24. expandTools: "ctrl+x",
  25. });
  26. const previousAgentDir = process.env[ENV_AGENT_DIR];
  27. process.env[ENV_AGENT_DIR] = agentDir;
  28. runMigrations(agentDir);
  29. if (previousAgentDir === undefined) {
  30. delete process.env[ENV_AGENT_DIR];
  31. } else {
  32. process.env[ENV_AGENT_DIR] = previousAgentDir;
  33. }
  34. const migrated = JSON.parse(fs.readFileSync(path.join(agentDir, "keybindings.json"), "utf-8")) as Record<
  35. string,
  36. unknown
  37. >;
  38. expect(migrated).toEqual({
  39. "tui.editor.cursorUp": ["up", "ctrl+p"],
  40. "app.tools.expand": "ctrl+x",
  41. });
  42. });
  43. it("keeps the namespaced value when old and new names both exist", () => {
  44. const agentDir = createAgentDir({
  45. expandTools: "ctrl+x",
  46. "app.tools.expand": "ctrl+y",
  47. });
  48. const previousAgentDir = process.env[ENV_AGENT_DIR];
  49. process.env[ENV_AGENT_DIR] = agentDir;
  50. runMigrations(agentDir);
  51. if (previousAgentDir === undefined) {
  52. delete process.env[ENV_AGENT_DIR];
  53. } else {
  54. process.env[ENV_AGENT_DIR] = previousAgentDir;
  55. }
  56. const migrated = JSON.parse(fs.readFileSync(path.join(agentDir, "keybindings.json"), "utf-8")) as Record<
  57. string,
  58. unknown
  59. >;
  60. expect(migrated).toEqual({
  61. "app.tools.expand": "ctrl+y",
  62. });
  63. });
  64. it("loads old key names in memory before the file is rewritten", () => {
  65. const agentDir = createAgentDir({
  66. selectConfirm: "enter",
  67. interrupt: "ctrl+x",
  68. });
  69. const keybindings = KeybindingsManager.create(agentDir);
  70. expect(keybindings.getUserBindings()).toEqual({
  71. "tui.select.confirm": "enter",
  72. "app.interrupt": "ctrl+x",
  73. });
  74. const effective = keybindings.getEffectiveConfig();
  75. expect(effective["tui.select.confirm"]).toBe("enter");
  76. expect(effective["app.interrupt"]).toBe("ctrl+x");
  77. });
  78. });