File size: 2,606 Bytes
246d201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { createRoutesStub } from "react-router";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import { renderWithProviders } from "test-utils";
import { screen, waitFor } from "@testing-library/react";
import toast from "react-hot-toast";
import App from "#/routes/_oh.app/route";
import OpenHands from "#/api/open-hands";
import { MULTI_CONVERSATION_UI } from "#/utils/feature-flags";

describe("App", () => {
  const RouteStub = createRoutesStub([
    { Component: App, path: "/conversation/:conversationId" },
  ]);

  const { endSessionMock } = vi.hoisted(() => ({
    endSessionMock: vi.fn(),
  }));

  beforeAll(() => {
    vi.mock("#/hooks/use-end-session", () => ({
      useEndSession: vi.fn(() => endSessionMock),
    }));

    vi.mock("#/hooks/use-terminal", () => ({
      useTerminal: vi.fn(),
    }));
  });

  afterEach(() => {
    vi.clearAllMocks();
  });

  it("should render", async () => {
    renderWithProviders(<RouteStub initialEntries={["/conversation/123"]} />);
    await screen.findByTestId("app-route");
  });

  it.skipIf(!MULTI_CONVERSATION_UI)(
    "should call endSession if the user does not have permission to view conversation",
    async () => {
      const errorToastSpy = vi.spyOn(toast, "error");
      const getConversationSpy = vi.spyOn(OpenHands, "getConversation");

      getConversationSpy.mockResolvedValue(null);
      renderWithProviders(
        <RouteStub initialEntries={["/conversation/9999"]} />,
      );

      await waitFor(() => {
        expect(endSessionMock).toHaveBeenCalledOnce();
        expect(errorToastSpy).toHaveBeenCalledOnce();
      });
    },
  );

  it("should not call endSession if the user has permission", async () => {
    const errorToastSpy = vi.spyOn(toast, "error");
    const getConversationSpy = vi.spyOn(OpenHands, "getConversation");

    getConversationSpy.mockResolvedValue({
      conversation_id: "9999",
      last_updated_at: "",
      created_at: "",
      title: "",
      selected_repository: "",
      status: "STOPPED",
    });
    const { rerender } = renderWithProviders(
      <RouteStub initialEntries={["/conversation/9999"]} />,
    );

    await waitFor(() => {
      expect(endSessionMock).not.toHaveBeenCalled();
      expect(errorToastSpy).not.toHaveBeenCalled();
    });

    rerender(<RouteStub initialEntries={["/conversation"]} />);

    await waitFor(() => {
      expect(endSessionMock).not.toHaveBeenCalled();
      expect(errorToastSpy).not.toHaveBeenCalled();
    });
  });
});