File size: 2,778 Bytes
fd2c364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import {
  __resetActiveStoreForTests,
  setActiveSelection,
  setRegisteredBackends,
} from "#/api/backend-registry/active-store";
import type { Backend } from "#/api/backend-registry/types";
import { ActiveBackendProvider } from "#/contexts/active-backend-context";
import { IntegrationsSettingsLink } from "#/components/features/settings/integrations-settings-link";

const cloudBackend: Backend = {
  id: "cloud-1",
  name: "OpenHands Cloud",
  host: "https://app.all-hands.dev",
  apiKey: "token",
  kind: "cloud",
};

function renderWithProviders() {
  const queryClient = new QueryClient({
    defaultOptions: { queries: { retry: false } },
  });
  return render(
    <QueryClientProvider client={queryClient}>
      <ActiveBackendProvider>
        <IntegrationsSettingsLink />
      </ActiveBackendProvider>
    </QueryClientProvider>,
  );
}

beforeEach(() => {
  window.localStorage.clear();
  __resetActiveStoreForTests();
});

afterEach(() => {
  vi.unstubAllEnvs();
  window.localStorage.clear();
  __resetActiveStoreForTests();
});

describe("IntegrationsSettingsLink", () => {
  it("renders a link to {cloudHost}/settings/integrations when a cloud backend is active", () => {
    // Arrange
    setRegisteredBackends([cloudBackend]);
    setActiveSelection({ backendId: cloudBackend.id });

    // Act
    renderWithProviders();

    // Assert
    const link = screen.getByTestId("settings-integrations-link");
    expect(link).toHaveAttribute(
      "href",
      "https://app.all-hands.dev/settings/integrations",
    );
    expect(link).toHaveAttribute("target", "_blank");
  });

  it("appends the active org so cloud integrations open on the same organization", () => {
    // Arrange
    setRegisteredBackends([cloudBackend]);
    setActiveSelection({ backendId: cloudBackend.id, orgId: "org-123" });

    // Act
    renderWithProviders();

    // Assert
    expect(screen.getByTestId("settings-integrations-link")).toHaveAttribute(
      "href",
      "https://app.all-hands.dev/settings/integrations?org=org-123",
    );
  });

  it("renders nothing when the canvas is locked to a Cloud host", () => {
    // Arrange — SaaS / self-hosted OHE serve the canvas with `--lock-to-cloud`;
    // the OHE settings shell already exposes Integrations.
    vi.stubEnv("VITE_LOCK_TO_CLOUD", "https://app.all-hands.dev");
    setRegisteredBackends([cloudBackend]);
    setActiveSelection({ backendId: cloudBackend.id });

    // Act
    renderWithProviders();

    // Assert
    expect(screen.queryByTestId("settings-integrations-link")).toBeNull();
  });
});