session.test.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { describe, expect, it, vi } from "vitest";
  2. import ActionType from "#/types/ActionType";
  3. import { Settings, saveSettings } from "./settings";
  4. import Session from "./session";
  5. const sendSpy = vi.spyOn(Session, "send");
  6. // @ts-expect-error - spying on private function
  7. const setupSpy = vi.spyOn(Session, "_setupSocket").mockImplementation(() => {
  8. // @ts-expect-error - calling a private function
  9. Session._initializeAgent();
  10. });
  11. describe("startNewSession", () => {
  12. afterEach(() => {
  13. sendSpy.mockClear();
  14. setupSpy.mockClear();
  15. });
  16. it("Should start a new session with the current settings", () => {
  17. const settings: Settings = {
  18. LLM_MODEL: "llm_value",
  19. CUSTOM_LLM_MODEL: "",
  20. USING_CUSTOM_MODEL: false,
  21. AGENT: "agent_value",
  22. LANGUAGE: "language_value",
  23. LLM_API_KEY: "sk-...",
  24. CONFIRMATION_MODE: true,
  25. SECURITY_ANALYZER: "analyzer",
  26. };
  27. const event = {
  28. action: ActionType.INIT,
  29. args: settings,
  30. };
  31. saveSettings(settings);
  32. Session.startNewSession();
  33. expect(setupSpy).toHaveBeenCalledTimes(1);
  34. expect(sendSpy).toHaveBeenCalledWith(JSON.stringify(event));
  35. });
  36. it("should start with the custom llm if set", () => {
  37. const settings: Settings = {
  38. LLM_MODEL: "llm_value",
  39. CUSTOM_LLM_MODEL: "custom_llm_value",
  40. USING_CUSTOM_MODEL: true,
  41. AGENT: "agent_value",
  42. LANGUAGE: "language_value",
  43. LLM_API_KEY: "sk-...",
  44. CONFIRMATION_MODE: true,
  45. SECURITY_ANALYZER: "analyzer",
  46. };
  47. const event = {
  48. action: ActionType.INIT,
  49. args: settings,
  50. };
  51. saveSettings(settings);
  52. Session.startNewSession();
  53. expect(setupSpy).toHaveBeenCalledTimes(1);
  54. expect(sendSpy).toHaveBeenCalledWith(
  55. JSON.stringify({
  56. ...event,
  57. args: { ...settings, LLM_MODEL: "custom_llm_value" },
  58. }),
  59. );
  60. });
  61. });