settings.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { describe, expect, it, vi, Mock, afterEach } from "vitest";
  2. import {
  3. DEFAULT_SETTINGS,
  4. Settings,
  5. getSettings,
  6. saveSettings,
  7. } from "../../src/services/settings";
  8. Storage.prototype.getItem = vi.fn();
  9. Storage.prototype.setItem = vi.fn();
  10. describe("getSettings", () => {
  11. afterEach(() => {
  12. vi.resetAllMocks();
  13. });
  14. it("should get the stored settings", () => {
  15. (localStorage.getItem as Mock)
  16. .mockReturnValueOnce("llm_value")
  17. .mockReturnValueOnce("base_url")
  18. .mockReturnValueOnce("agent_value")
  19. .mockReturnValueOnce("language_value")
  20. .mockReturnValueOnce("api_key")
  21. .mockReturnValueOnce("true")
  22. .mockReturnValueOnce("invariant");
  23. const settings = getSettings();
  24. expect(settings).toEqual({
  25. LLM_MODEL: "llm_value",
  26. LLM_BASE_URL: "base_url",
  27. AGENT: "agent_value",
  28. LANGUAGE: "language_value",
  29. LLM_API_KEY: "api_key",
  30. CONFIRMATION_MODE: true,
  31. SECURITY_ANALYZER: "invariant",
  32. });
  33. });
  34. it("should handle return defaults if localStorage key does not exist", () => {
  35. (localStorage.getItem as Mock)
  36. .mockReturnValueOnce(null)
  37. .mockReturnValueOnce(null)
  38. .mockReturnValueOnce(null)
  39. .mockReturnValueOnce(null)
  40. .mockReturnValueOnce(null)
  41. .mockReturnValueOnce(null)
  42. .mockReturnValueOnce(null)
  43. .mockReturnValueOnce(null);
  44. const settings = getSettings();
  45. expect(settings).toEqual({
  46. LLM_MODEL: DEFAULT_SETTINGS.LLM_MODEL,
  47. AGENT: DEFAULT_SETTINGS.AGENT,
  48. LANGUAGE: DEFAULT_SETTINGS.LANGUAGE,
  49. LLM_API_KEY: "",
  50. LLM_BASE_URL: DEFAULT_SETTINGS.LLM_BASE_URL,
  51. CONFIRMATION_MODE: DEFAULT_SETTINGS.CONFIRMATION_MODE,
  52. SECURITY_ANALYZER: DEFAULT_SETTINGS.SECURITY_ANALYZER,
  53. });
  54. });
  55. });
  56. describe("saveSettings", () => {
  57. it("should save the settings", () => {
  58. const settings: Settings = {
  59. LLM_MODEL: "llm_value",
  60. LLM_BASE_URL: "base_url",
  61. AGENT: "agent_value",
  62. LANGUAGE: "language_value",
  63. LLM_API_KEY: "some_key",
  64. CONFIRMATION_MODE: true,
  65. SECURITY_ANALYZER: "invariant",
  66. };
  67. saveSettings(settings);
  68. expect(localStorage.setItem).toHaveBeenCalledWith("LLM_MODEL", "llm_value");
  69. expect(localStorage.setItem).toHaveBeenCalledWith("AGENT", "agent_value");
  70. expect(localStorage.setItem).toHaveBeenCalledWith(
  71. "LANGUAGE",
  72. "language_value",
  73. );
  74. expect(localStorage.setItem).toHaveBeenCalledWith(
  75. "LLM_API_KEY",
  76. "some_key",
  77. );
  78. });
  79. it.skip("should save partial settings", () => {
  80. const settings = {
  81. LLM_MODEL: "llm_value",
  82. };
  83. saveSettings(settings);
  84. expect(localStorage.setItem).toHaveBeenCalledTimes(2);
  85. expect(localStorage.setItem).toHaveBeenCalledWith("LLM_MODEL", "llm_value");
  86. expect(localStorage.setItem).toHaveBeenCalledWith("SETTINGS_VERSION", "2");
  87. });
  88. it("should not save invalid settings", () => {
  89. const settings = {
  90. LLM_MODEL: "llm_value",
  91. AGENT: "agent_value",
  92. LANGUAGE: "language_value",
  93. INVALID: "invalid_value",
  94. };
  95. saveSettings(settings);
  96. expect(localStorage.setItem).toHaveBeenCalledWith("LLM_MODEL", "llm_value");
  97. expect(localStorage.setItem).toHaveBeenCalledWith("AGENT", "agent_value");
  98. expect(localStorage.setItem).toHaveBeenCalledWith(
  99. "LANGUAGE",
  100. "language_value",
  101. );
  102. expect(localStorage.setItem).not.toHaveBeenCalledWith(
  103. "INVALID",
  104. "invalid_value",
  105. );
  106. });
  107. });