settings-manager-bug.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "fs";
  2. import { join } from "path";
  3. import { afterEach, beforeEach, describe, expect, it } from "vitest";
  4. import { SettingsManager } from "../src/core/settings-manager.ts";
  5. /**
  6. * Tests for the fix to a bug where external file changes to arrays were overwritten.
  7. *
  8. * The bug scenario was:
  9. * 1. Pi starts with settings.json containing packages: ["npm:some-pkg"]
  10. * 2. User externally edits file to packages: []
  11. * 3. User changes an unrelated setting (e.g., theme) via UI
  12. * 4. save() would overwrite packages back to ["npm:some-pkg"] from stale in-memory state
  13. *
  14. * The fix tracks which fields were explicitly modified during the session, and only
  15. * those fields override file values during save().
  16. */
  17. describe("SettingsManager - External Edit Preservation", () => {
  18. const testDir = join(process.cwd(), "test-settings-bug-tmp");
  19. const agentDir = join(testDir, "agent");
  20. const projectDir = join(testDir, "project");
  21. beforeEach(() => {
  22. if (existsSync(testDir)) {
  23. rmSync(testDir, { recursive: true });
  24. }
  25. mkdirSync(agentDir, { recursive: true });
  26. mkdirSync(join(projectDir, ".pi"), { recursive: true });
  27. });
  28. afterEach(() => {
  29. if (existsSync(testDir)) {
  30. rmSync(testDir, { recursive: true });
  31. }
  32. });
  33. it("should preserve file changes to packages array when changing unrelated setting", async () => {
  34. const settingsPath = join(agentDir, "settings.json");
  35. // Initial state: packages has one item
  36. writeFileSync(
  37. settingsPath,
  38. JSON.stringify({
  39. theme: "dark",
  40. packages: ["npm:pi-mcp-adapter"],
  41. }),
  42. );
  43. // Pi starts up, loads settings into memory
  44. const manager = SettingsManager.create(projectDir, agentDir);
  45. // At this point, globalSettings.packages = ["npm:pi-mcp-adapter"]
  46. expect(manager.getPackages()).toEqual(["npm:pi-mcp-adapter"]);
  47. // User externally edits settings.json to remove the package
  48. const currentSettings = JSON.parse(readFileSync(settingsPath, "utf-8"));
  49. currentSettings.packages = []; // User wants to remove this!
  50. writeFileSync(settingsPath, JSON.stringify(currentSettings, null, 2));
  51. // Verify file was changed
  52. expect(JSON.parse(readFileSync(settingsPath, "utf-8")).packages).toEqual([]);
  53. // User changes an UNRELATED setting via UI (this triggers save)
  54. manager.setTheme("light");
  55. await manager.flush();
  56. // With the fix, packages should be preserved as [] (not reverted to startup value)
  57. const savedSettings = JSON.parse(readFileSync(settingsPath, "utf-8"));
  58. expect(savedSettings.packages).toEqual([]);
  59. expect(savedSettings.theme).toBe("light");
  60. });
  61. it("should preserve file changes to extensions array when changing unrelated setting", async () => {
  62. const settingsPath = join(agentDir, "settings.json");
  63. writeFileSync(
  64. settingsPath,
  65. JSON.stringify({
  66. theme: "dark",
  67. extensions: ["/old/extension.ts"],
  68. }),
  69. );
  70. const manager = SettingsManager.create(projectDir, agentDir);
  71. // User externally updates extensions
  72. const currentSettings = JSON.parse(readFileSync(settingsPath, "utf-8"));
  73. currentSettings.extensions = ["/new/extension.ts"];
  74. writeFileSync(settingsPath, JSON.stringify(currentSettings, null, 2));
  75. // Change unrelated setting
  76. manager.setDefaultThinkingLevel("high");
  77. await manager.flush();
  78. const savedSettings = JSON.parse(readFileSync(settingsPath, "utf-8"));
  79. // With the fix, extensions should be preserved (not reverted to startup value)
  80. expect(savedSettings.extensions).toEqual(["/new/extension.ts"]);
  81. });
  82. it("should preserve external project settings changes when updating unrelated project field", async () => {
  83. const projectSettingsPath = join(projectDir, ".pi", "settings.json");
  84. writeFileSync(
  85. projectSettingsPath,
  86. JSON.stringify({
  87. extensions: ["./old-extension.ts"],
  88. prompts: ["./old-prompt.md"],
  89. }),
  90. );
  91. const manager = SettingsManager.create(projectDir, agentDir);
  92. const currentProjectSettings = JSON.parse(readFileSync(projectSettingsPath, "utf-8"));
  93. currentProjectSettings.prompts = ["./new-prompt.md"];
  94. writeFileSync(projectSettingsPath, JSON.stringify(currentProjectSettings, null, 2));
  95. manager.setProjectExtensionPaths(["./updated-extension.ts"]);
  96. await manager.flush();
  97. const savedProjectSettings = JSON.parse(readFileSync(projectSettingsPath, "utf-8"));
  98. expect(savedProjectSettings.prompts).toEqual(["./new-prompt.md"]);
  99. expect(savedProjectSettings.extensions).toEqual(["./updated-extension.ts"]);
  100. });
  101. it("should let in-memory project changes override external changes for the same project field", async () => {
  102. const projectSettingsPath = join(projectDir, ".pi", "settings.json");
  103. writeFileSync(
  104. projectSettingsPath,
  105. JSON.stringify({
  106. extensions: ["./initial-extension.ts"],
  107. }),
  108. );
  109. const manager = SettingsManager.create(projectDir, agentDir);
  110. const currentProjectSettings = JSON.parse(readFileSync(projectSettingsPath, "utf-8"));
  111. currentProjectSettings.extensions = ["./external-extension.ts"];
  112. writeFileSync(projectSettingsPath, JSON.stringify(currentProjectSettings, null, 2));
  113. manager.setProjectExtensionPaths(["./in-memory-extension.ts"]);
  114. await manager.flush();
  115. const savedProjectSettings = JSON.parse(readFileSync(projectSettingsPath, "utf-8"));
  116. expect(savedProjectSettings.extensions).toEqual(["./in-memory-extension.ts"]);
  117. });
  118. });