File size: 4,648 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { describe, expect, it } from "vitest";
import {
  BACKEND_QUERY_PARAM,
  ORG_QUERY_PARAM,
  readBackendSelectionFromUrl,
  withBackendSelectionParams,
} from "#/api/backend-registry/url-selection";
import type { Backend } from "#/api/backend-registry/types";

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

const cloudBackend: Backend = {
  id: "prod",
  name: "Production",
  host: "https://app.all-hands.dev",
  apiKey: "bearer-key",
  kind: "cloud",
};

const backends = [localBackend, cloudBackend];

describe("withBackendSelectionParams", () => {
  it("pins the active backend id onto the path", () => {
    const path = withBackendSelectionParams("/conversations/abc", {
      backend: localBackend,
      orgId: null,
    });

    expect(path).toBe(`/conversations/abc?${BACKEND_QUERY_PARAM}=local-1`);
  });

  it("includes the org id for a cloud backend", () => {
    const path = withBackendSelectionParams("/conversations/abc", {
      backend: cloudBackend,
      orgId: "org-7",
    });

    expect(path).toBe(
      `/conversations/abc?${BACKEND_QUERY_PARAM}=prod&${ORG_QUERY_PARAM}=org-7`,
    );
  });

  it("omits the org id when there is none", () => {
    const path = withBackendSelectionParams("/conversations/abc", {
      backend: cloudBackend,
      orgId: null,
    });

    expect(path).not.toContain(ORG_QUERY_PARAM);
  });

  it("preserves query parameters already on the path", () => {
    const path = withBackendSelectionParams("/conversations/abc?tab=files", {
      backend: localBackend,
      orgId: null,
    });

    expect(path).toBe(
      `/conversations/abc?tab=files&${BACKEND_QUERY_PARAM}=local-1`,
    );
  });

  it("keeps a fragment after existing query parameters intact and after the query", () => {
    const path = withBackendSelectionParams(
      "/conversations/abc?tab=files#detail",
      {
        backend: localBackend,
        orgId: null,
      },
    );

    expect(path).toBe(
      `/conversations/abc?tab=files&${BACKEND_QUERY_PARAM}=local-1#detail`,
    );
  });

  it("keeps a fragment on a path without query parameters after the query", () => {
    const path = withBackendSelectionParams("/conversations/abc#detail", {
      backend: localBackend,
      orgId: null,
    });

    expect(path).toBe(
      `/conversations/abc?${BACKEND_QUERY_PARAM}=local-1#detail`,
    );
  });

  it("does not treat a ? inside the fragment as a query separator", () => {
    const path = withBackendSelectionParams("/conversations/abc#detail?x=1", {
      backend: localBackend,
      orgId: null,
    });

    expect(path).toBe(
      `/conversations/abc?${BACKEND_QUERY_PARAM}=local-1#detail?x=1`,
    );
  });

  it("round-trips an empty fragment verbatim", () => {
    const path = withBackendSelectionParams("/conversations/abc#", {
      backend: localBackend,
      orgId: null,
    });

    expect(path).toBe(`/conversations/abc?${BACKEND_QUERY_PARAM}=local-1#`);
  });

  it("keeps the org id and the fragment together", () => {
    const path = withBackendSelectionParams("/conversations/abc#detail", {
      backend: cloudBackend,
      orgId: "org-7",
    });

    expect(path).toBe(
      `/conversations/abc?${BACKEND_QUERY_PARAM}=prod&${ORG_QUERY_PARAM}=org-7#detail`,
    );
  });

  it("keeps query data that itself contains a ?", () => {
    const path = withBackendSelectionParams("/conversations/abc?next=/a?b=1", {
      backend: localBackend,
      orgId: null,
    });

    expect(path).toBe(
      `/conversations/abc?next=%2Fa%3Fb%3D1&${BACKEND_QUERY_PARAM}=local-1`,
    );
  });
});

describe("readBackendSelectionFromUrl", () => {
  it("reads a registered backend id", () => {
    expect(
      readBackendSelectionFromUrl(backends, `?${BACKEND_QUERY_PARAM}=local-1`),
    ).toEqual({ backendId: "local-1", orgId: null });
  });

  it("reads the org id alongside the backend id", () => {
    expect(
      readBackendSelectionFromUrl(
        backends,
        `?${BACKEND_QUERY_PARAM}=prod&${ORG_QUERY_PARAM}=org-7`,
      ),
    ).toEqual({ backendId: "prod", orgId: "org-7" });
  });

  it("ignores a backend id that is not registered", () => {
    expect(
      readBackendSelectionFromUrl(backends, `?${BACKEND_QUERY_PARAM}=gone`),
    ).toBeNull();
  });

  it("ignores an empty or absent parameter", () => {
    expect(readBackendSelectionFromUrl(backends, "")).toBeNull();
    expect(readBackendSelectionFromUrl(backends, "?tab=files")).toBeNull();
    expect(
      readBackendSelectionFromUrl(backends, `?${BACKEND_QUERY_PARAM}=`),
    ).toBeNull();
  });
});