-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Expand file tree
/
Copy pathuseWorkspaceContext.sign-in-refresh.test.tsx
More file actions
170 lines (151 loc) · 5.75 KB
/
Copy pathuseWorkspaceContext.sign-in-refresh.test.tsx
File metadata and controls
170 lines (151 loc) · 5.75 KB
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
168
169
170
// @vitest-environment jsdom
//
// #140: "signed into the cloud account during onboarding, but the home view's
// bottom-left corner still says signed out."
//
// The rail's sign-in callout is gated on `!context && !loading` (EntryShell:
// `footerNotice`). `loading` had already settled to false on the signed-out
// read that ran before onboarding, and the post-sign-in re-read did not raise
// it again — so for the whole duration of that (vela-backed, up-to-seconds)
// read the shell kept painting the signed-out callout even though the user had
// just signed in.
//
// The fix distinguishes an EXPLICIT identity-change refresh from ambient
// revalidation: only the deliberate signal may blank a stale signed-out answer.
import { act, cleanup, renderHook, waitFor } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import {
notifyWorkspaceContextRefresh,
resetWorkspaceContextCache,
useWorkspaceContext,
} from '../src/collab/useWorkspaceContext';
import {
workspaceContextFixture,
workspaceDirectoryFixture,
} from './helpers/workspace-context';
const SIGNED_IN = workspaceContextFixture({
workspaceId: 'ws-1',
workspaceMemberId: 'member-1',
teamName: 'Acme',
workspaceName: 'Acme',
});
/** A fetch whose every call resolves only when the test says so. */
function deferredContextFetch() {
const pending: Array<{
url: string;
resolve: (response: Response) => void;
}> = [];
const fetchMock = vi.fn(
(input: RequestInfo | URL) =>
new Promise<Response>((resolve) => {
pending.push({ url: String(input), resolve });
}),
);
vi.stubGlobal('fetch', fetchMock);
return {
fetchMock,
/** Settle every read issued so far with `context`. */
async settleAll(context: unknown) {
for (let pass = 0; pass < 4; pass += 1) {
const waiting = pending.splice(0, pending.length);
if (waiting.length === 0) {
await Promise.resolve();
if (pending.length === 0) break;
continue;
}
await act(async () => {
for (const request of waiting) {
const body =
request.url === '/api/workspace/directory'
? workspaceDirectoryFixture(
context ? [context as typeof SIGNED_IN] : [],
)
: { context };
request.resolve(
new Response(JSON.stringify(body), {
status: 200,
headers: { 'content-type': 'application/json' },
}),
);
}
await Promise.resolve();
});
}
},
};
}
describe('useWorkspaceContext sign-in refresh', () => {
beforeEach(() => {
resetWorkspaceContextCache();
});
afterEach(() => {
cleanup();
vi.unstubAllGlobals();
resetWorkspaceContextCache();
});
it('stops reporting signed-out while an explicit post-sign-in re-read is in flight', async () => {
const { settleAll } = deferredContextFetch();
const { result } = renderHook(() => useWorkspaceContext());
// Before onboarding: the user really is signed out. The callout belongs on
// screen here — `!context && !loading`.
await settleAll(null);
await waitFor(() => expect(result.current.loading).toBe(false));
expect(result.current.context).toBeNull();
// Onboarding finishes and announces the sign-in. The shell must stop
// asserting "signed out" until the re-read answers.
await act(async () => {
notifyWorkspaceContextRefresh();
});
expect(result.current.loading).toBe(true);
expect(result.current.context).toBeNull();
// ...and lands on the real workspace.
await settleAll(SIGNED_IN);
await waitFor(() => expect(result.current.context).toEqual(SIGNED_IN));
expect(result.current.loading).toBe(false);
});
it('leaves ambient revalidation silent so a signed-out user keeps their callout', async () => {
// A genuinely signed-out user must not have the callout flicker away on
// every window focus — focus is revalidation, not an identity change.
const { settleAll } = deferredContextFetch();
const { result } = renderHook(() => useWorkspaceContext());
await settleAll(null);
await waitFor(() => expect(result.current.loading).toBe(false));
await act(async () => {
window.dispatchEvent(new Event('focus'));
});
expect(result.current.loading).toBe(false);
expect(result.current.context).toBeNull();
await settleAll(null);
});
it('keeps showing a resolved workspace while an identity refresh re-reads', async () => {
// The explicit signal must only promote "no context" to "loading". A user
// who is already signed in has a context on screen, and blanking it would
// reintroduce the flash the module cache exists to prevent.
const { settleAll } = deferredContextFetch();
const { result } = renderHook(() => useWorkspaceContext());
await settleAll(SIGNED_IN);
await waitFor(() => expect(result.current.context).toEqual(SIGNED_IN));
await act(async () => {
notifyWorkspaceContextRefresh();
});
expect(result.current.context).toEqual(SIGNED_IN);
expect(result.current.loading).toBe(false);
await settleAll(SIGNED_IN);
});
it.each([
[404, 'unsupported'],
[503, 'unavailable'],
] as const)(
'distinguishes an old daemon (%s) from an unavailable workspace service',
async (status, failure) => {
vi.stubGlobal(
'fetch',
vi.fn<typeof fetch>(async () => new Response(null, { status })),
);
const { result } = renderHook(() => useWorkspaceContext());
await waitFor(() => expect(result.current.loading).toBe(false));
expect(result.current.context).toBeNull();
expect(result.current.failure).toBe(failure);
},
);
});