File size: 4,079 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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 { CloudSettingsLink } from "#/components/features/settings/cloud-settings-link";

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

const localBackend: Backend = {
  id: "local-1",
  name: "Local",
  host: "http://localhost:3001",
  apiKey: "local-key",
  kind: "local",
};

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

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

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

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

    renderWithProviders();

    const link = screen.getByTestId("settings-cloud-link");
    expect(link).toHaveAttribute("href", "https://app.all-hands.dev/settings");
    expect(link).toHaveAttribute("target", "_blank");
    expect(link).toHaveAttribute("rel", "noopener noreferrer");
    // The test i18n mock returns the key verbatim; the human label
    // ("Cloud") is exercised via the translation.json entry instead.
    expect(link).toHaveTextContent("SETTINGS$CLOUD_SETTINGS_LINK");
  });

  it("strips a trailing slash when building the cloud settings URL", () => {
    setRegisteredBackends([
      { ...cloudBackend, host: "https://app.all-hands.dev/" },
    ]);
    setActiveSelection({ backendId: cloudBackend.id });

    renderWithProviders();

    expect(screen.getByTestId("settings-cloud-link")).toHaveAttribute(
      "href",
      "https://app.all-hands.dev/settings",
    );
  });

  it("opens in the same tab without the external-link icon when locked to Cloud", () => {
    // Arrange: an OHE/SaaS-hosted canvas locked to the active cloud host
    vi.stubEnv("VITE_LOCK_TO_CLOUD", "https://app.all-hands.dev");
    setRegisteredBackends([cloudBackend]);
    setActiveSelection({ backendId: cloudBackend.id });

    // Act
    renderWithProviders();

    // Assert: same tab (Back returns to the canvas) and no "new tab" hint
    const link = screen.getByTestId("settings-cloud-link");
    expect(link).toHaveAttribute("href", "https://app.all-hands.dev/settings");
    expect(link).not.toHaveAttribute("target");
    expect(link).not.toHaveAttribute("rel");
    expect(link.querySelector("svg.lucide-external-link")).toBeNull();
  });

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

    renderWithProviders();

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

  it("renders nothing when a local backend is active", () => {
    setRegisteredBackends([localBackend]);
    setActiveSelection({ backendId: localBackend.id });

    renderWithProviders();

    expect(screen.queryByTestId("settings-cloud-link")).toBeNull();
  });

  it("renders nothing when no backend is configured", () => {
    renderWithProviders();

    expect(screen.queryByTestId("settings-cloud-link")).toBeNull();
  });
});