StaticSettingsService.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // npx vitest run src/__tests__/StaticSettingsService.test.ts
  2. import { StaticSettingsService } from "../StaticSettingsService"
  3. describe("StaticSettingsService", () => {
  4. const validSettings = {
  5. version: 1,
  6. cloudSettings: {
  7. recordTaskMessages: true,
  8. enableTaskSharing: true,
  9. taskShareExpirationDays: 30,
  10. },
  11. defaultSettings: {
  12. enableCheckpoints: true,
  13. maxOpenTabsContext: 10,
  14. },
  15. allowList: {
  16. allowAll: false,
  17. providers: {
  18. anthropic: {
  19. allowAll: true,
  20. },
  21. },
  22. },
  23. }
  24. const validBase64 = Buffer.from(JSON.stringify(validSettings)).toString("base64")
  25. describe("constructor", () => {
  26. it("should parse valid base64 encoded JSON settings", () => {
  27. const service = new StaticSettingsService(validBase64)
  28. expect(service.getSettings()).toEqual(validSettings)
  29. })
  30. it("should throw error for invalid base64", () => {
  31. expect(() => new StaticSettingsService("invalid-base64!@#")).toThrow("Failed to parse static settings")
  32. })
  33. it("should throw error for invalid JSON", () => {
  34. const invalidJson = Buffer.from("{ invalid json }").toString("base64")
  35. expect(() => new StaticSettingsService(invalidJson)).toThrow("Failed to parse static settings")
  36. })
  37. it("should throw error for invalid schema", () => {
  38. const invalidSettings = { invalid: "schema" }
  39. const invalidBase64 = Buffer.from(JSON.stringify(invalidSettings)).toString("base64")
  40. expect(() => new StaticSettingsService(invalidBase64)).toThrow("Failed to parse static settings")
  41. })
  42. })
  43. describe("getAllowList", () => {
  44. it("should return the allow list from settings", () => {
  45. const service = new StaticSettingsService(validBase64)
  46. expect(service.getAllowList()).toEqual(validSettings.allowList)
  47. })
  48. })
  49. describe("getSettings", () => {
  50. it("should return the parsed settings", () => {
  51. const service = new StaticSettingsService(validBase64)
  52. expect(service.getSettings()).toEqual(validSettings)
  53. })
  54. })
  55. describe("dispose", () => {
  56. it("should be a no-op for static settings", () => {
  57. const service = new StaticSettingsService(validBase64)
  58. expect(() => service.dispose()).not.toThrow()
  59. })
  60. })
  61. describe("logging", () => {
  62. it("should use provided logger for errors", () => {
  63. const mockLog = vi.fn()
  64. expect(() => new StaticSettingsService("invalid-base64!@#", mockLog)).toThrow()
  65. expect(mockLog).toHaveBeenCalledWith(
  66. expect.stringContaining("[StaticSettingsService] failed to parse static settings:"),
  67. expect.any(Error),
  68. )
  69. })
  70. it("should use console.log as default logger for errors", () => {
  71. const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {})
  72. expect(() => new StaticSettingsService("invalid-base64!@#")).toThrow()
  73. expect(consoleSpy).toHaveBeenCalledWith(
  74. expect.stringContaining("[StaticSettingsService] failed to parse static settings:"),
  75. expect.any(Error),
  76. )
  77. consoleSpy.mockRestore()
  78. })
  79. it("should not log anything for successful parsing", () => {
  80. const mockLog = vi.fn()
  81. new StaticSettingsService(validBase64, mockLog)
  82. expect(mockLog).not.toHaveBeenCalled()
  83. })
  84. })
  85. })