Skip to content

Commit 159538c

Browse files
committed
WIP: Proxy auto select and user selection state
Lotta comments
1 parent 8f768f8 commit 159538c

File tree

9 files changed

+295
-78
lines changed

9 files changed

+295
-78
lines changed

site/src/components/AppLink/AppLink.stories.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ const Template: Story<AppLinkProps> = (args) => (
2626
setProxy: () => {
2727
return
2828
},
29+
clearProxy: () => {
30+
return
31+
},
2932
}}
3033
>
3134
<AppLink {...args} />

site/src/components/Resources/AgentRow.stories.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ const TemplateFC = (
6565
setProxy: () => {
6666
return
6767
},
68+
clearProxy: () => {
69+
return
70+
},
6871
}}
6972
>
7073
<AgentRow {...args} />

site/src/components/Resources/ResourceCard.stories.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ Example.args = {
3030
setProxy: () => {
3131
return
3232
},
33+
clearProxy: () => {
34+
return
35+
},
3336
}}
3437
>
3538
<AgentRow
@@ -97,6 +100,9 @@ BunchOfMetadata.args = {
97100
setProxy: () => {
98101
return
99102
},
103+
clearProxy: () => {
104+
return
105+
},
100106
}}
101107
>
102108
<AgentRow

site/src/components/Workspace/Workspace.stories.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ const Template: Story<WorkspaceProps> = (args) => (
3535
setProxy: () => {
3636
return
3737
},
38+
clearProxy: () => {
39+
return
40+
},
3841
}}
3942
>
4043
<Workspace {...args} />

site/src/contexts/ProxyContext.test.ts

-62
This file was deleted.
+192
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
import {
2+
MockPrimaryWorkspaceProxy,
3+
MockWorkspaceProxies,
4+
MockHealthyWildWorkspaceProxy,
5+
MockUnhealthyWildWorkspaceProxy,
6+
} from "testHelpers/entities"
7+
import {
8+
getPreferredProxy,
9+
ProxyProvider,
10+
saveUserSelectedProxy,
11+
useProxy,
12+
} from "./ProxyContext"
13+
import * as ProxyContextModule from "./ProxyContext"
14+
import {
15+
renderWithAuth,
16+
waitForLoaderToBeRemoved,
17+
} from "testHelpers/renderHelpers"
18+
import { screen } from "@testing-library/react"
19+
import { server } from "testHelpers/server"
20+
import "testHelpers/localstorage"
21+
import { rest } from "msw"
22+
import { Region } from "api/typesGenerated"
23+
24+
describe("ProxyContextGetURLs", () => {
25+
it.each([
26+
["empty", [], undefined, "", ""],
27+
// Primary has no path app URL. Uses relative links
28+
[
29+
"primary",
30+
[MockPrimaryWorkspaceProxy],
31+
MockPrimaryWorkspaceProxy,
32+
"",
33+
MockPrimaryWorkspaceProxy.wildcard_hostname,
34+
],
35+
[
36+
"regions selected",
37+
MockWorkspaceProxies,
38+
MockHealthyWildWorkspaceProxy,
39+
MockHealthyWildWorkspaceProxy.path_app_url,
40+
MockHealthyWildWorkspaceProxy.wildcard_hostname,
41+
],
42+
// Primary is the default if none selected
43+
[
44+
"no selected",
45+
[MockPrimaryWorkspaceProxy],
46+
undefined,
47+
"",
48+
MockPrimaryWorkspaceProxy.wildcard_hostname,
49+
],
50+
[
51+
"regions no select primary default",
52+
MockWorkspaceProxies,
53+
undefined,
54+
"",
55+
MockPrimaryWorkspaceProxy.wildcard_hostname,
56+
],
57+
// Primary is the default if the selected is unhealthy
58+
[
59+
"unhealthy selection",
60+
MockWorkspaceProxies,
61+
MockUnhealthyWildWorkspaceProxy,
62+
"",
63+
MockPrimaryWorkspaceProxy.wildcard_hostname,
64+
],
65+
// This should never happen, when there is no primary
66+
["no primary", [MockHealthyWildWorkspaceProxy], undefined, "", ""],
67+
])(
68+
`%p`,
69+
(_, regions, selected, preferredPathAppURL, preferredWildcardHostname) => {
70+
const preferred = getPreferredProxy(regions, selected)
71+
expect(preferred.preferredPathAppURL).toBe(preferredPathAppURL)
72+
expect(preferred.preferredWildcardHostname).toBe(
73+
preferredWildcardHostname,
74+
)
75+
},
76+
)
77+
})
78+
79+
// interface ProxySelectTest {
80+
// name: string
81+
// actions: ()
82+
// }
83+
84+
const TestingComponent = () => {
85+
return renderWithAuth(
86+
<ProxyProvider>
87+
<TestingScreen />
88+
</ProxyProvider>,
89+
{
90+
route: `/proxies`,
91+
path: "/proxies",
92+
},
93+
)
94+
}
95+
96+
// TestingScreen just mounts some components that we can check in the unit test.
97+
const TestingScreen = () => {
98+
const { proxy, isFetched, isLoading } = useProxy()
99+
return (
100+
<>
101+
<div data-testid="isFetched" title={isFetched.toString()}></div>
102+
<div data-testid="isLoading" title={isLoading.toString()}></div>
103+
<div
104+
data-testid="preferredProxy"
105+
title={proxy.selectedProxy && proxy.selectedProxy.id}
106+
></div>
107+
</>
108+
)
109+
}
110+
111+
interface ProxyContextSelectionTest {
112+
expSelectedProxyID: string
113+
regions: Region[]
114+
storageProxy: Region | undefined
115+
}
116+
117+
describe("ProxyContextSelection", () => {
118+
beforeEach(() => {
119+
window.localStorage.clear()
120+
})
121+
122+
it.each([
123+
[
124+
"empty",
125+
{
126+
expSelectedProxyID: "",
127+
regions: [],
128+
storageProxy: undefined,
129+
},
130+
],
131+
[
132+
"regions_no_selection",
133+
{
134+
expSelectedProxyID: MockPrimaryWorkspaceProxy.id,
135+
regions: MockWorkspaceProxies,
136+
storageProxy: undefined,
137+
},
138+
],
139+
[
140+
"regions_selected_unhealthy",
141+
{
142+
expSelectedProxyID: MockPrimaryWorkspaceProxy.id,
143+
regions: MockWorkspaceProxies,
144+
storageProxy: MockUnhealthyWildWorkspaceProxy,
145+
},
146+
],
147+
] as [string, ProxyContextSelectionTest][])(
148+
`%s`,
149+
async (_, { expSelectedProxyID, regions, storageProxy }) => {
150+
// Initial selection if present
151+
if (storageProxy) {
152+
saveUserSelectedProxy(storageProxy)
153+
}
154+
155+
// Mock the API response
156+
server.use(
157+
rest.get("/api/v2/regions", async (req, res, ctx) => {
158+
return res(
159+
ctx.status(200),
160+
ctx.json({
161+
regions: regions,
162+
}),
163+
)
164+
}),
165+
)
166+
167+
TestingComponent()
168+
await waitForLoaderToBeRemoved()
169+
170+
await screen.findByTestId("isFetched").then((x) => {
171+
expect(x.title).toBe("true")
172+
})
173+
await screen.findByTestId("isLoading").then((x) => {
174+
expect(x.title).toBe("false")
175+
})
176+
await screen.findByTestId("preferredProxy").then((x) => {
177+
expect(x.title).toBe(expSelectedProxyID)
178+
})
179+
180+
// const { proxy, proxies, isFetched, isLoading, proxyLatencies } = useProxy()
181+
// expect(isLoading).toBe(false)
182+
// expect(isFetched).toBe(true)
183+
184+
// expect(x).toBe(2)
185+
// const preferred = getPreferredProxy(regions, selected)
186+
// expect(preferred.preferredPathAppURL).toBe(preferredPathAppURL)
187+
// expect(preferred.preferredWildcardHostname).toBe(
188+
// preferredWildcardHostname,
189+
// )
190+
},
191+
)
192+
})

0 commit comments

Comments
 (0)