@bevel-software/platform-core-frontend 0.21.0 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/core/CoreAppShell.tsx +2 -0
- package/src/core/registry.ts +140 -0
- package/src/lib/api.ts +49 -0
- package/src/modules/access/__tests__/ManageAccessDialog.request.test.tsx +376 -0
- package/src/modules/access/api.ts +6 -4
- package/src/modules/access/components/AccessRequestControl.tsx +155 -0
- package/src/modules/{library → access}/components/AccessRequestsBanner.tsx +62 -24
- package/src/modules/access/components/ManageAccessDialog.tsx +81 -4
- package/src/modules/access/hooks/useAccessRequests.ts +183 -0
- package/src/modules/access/requests.api.ts +121 -0
- package/src/modules/admin/__tests__/UserAccountsPage.test.tsx +89 -1
- package/src/modules/admin/components/UserAccountsPage.tsx +93 -5
- package/src/modules/auth/components/LoginScreen.tsx +8 -0
- package/src/modules/auth/services/account.api.ts +26 -3
- package/src/modules/change-requests/__tests__/ChangeRequestDialog.brings-itself-up-to-date.test.tsx +171 -38
- package/src/modules/change-requests/__tests__/ChangeRequestDialog.fork-point.test.tsx +34 -19
- package/src/modules/change-requests/__tests__/ChangeRequestDialog.test.tsx +173 -6
- package/src/modules/change-requests/components/BranchFilePreview.tsx +7 -1
- package/src/modules/change-requests/components/ChangeRequestDialog.tsx +155 -98
- package/src/modules/git/__tests__/FileHistoryPanel.test.tsx +89 -10
- package/src/modules/git/__tests__/HistoryVersionPreview.test.tsx +479 -0
- package/src/modules/git/components/FileHistoryPanel.tsx +52 -65
- package/src/modules/git/components/HistoryVersionPreview.tsx +410 -0
- package/src/modules/layout/__tests__/ReadOnlyBanner.test.tsx +157 -0
- package/src/modules/layout/components/ReadOnlyBanner.tsx +96 -0
- package/src/modules/library/__tests__/SkillPage.test.tsx +15 -0
- package/src/modules/library/__tests__/ToolPage.test.tsx +15 -1
- package/src/modules/library/__tests__/title-row-actions.ts +53 -1
- package/src/modules/library/components/PluginJoinRequests.tsx +2 -2
- package/src/modules/library/components/skill-page/SkillPage.tsx +2 -2
- package/src/modules/library/components/tool-page/ToolPage.tsx +11 -1
- package/src/modules/library/hooks/useJoinRequests.ts +26 -101
- package/src/modules/pr/services/pr-merge.api.ts +10 -2
- package/src/modules/settings/components/DeploymentPage.tsx +22 -0
- package/src/modules/settings/components/__tests__/DeploymentPage.test.tsx +72 -4
- package/src/modules/setup/__tests__/setup.first-run.test.tsx +276 -0
- package/src/modules/setup/__tests__/setup.github.test.tsx +416 -0
- package/src/modules/setup/__tests__/setup.kept-draft.test.tsx +275 -0
- package/src/modules/setup/__tests__/setup.repository-change.test.tsx +296 -0
- package/src/modules/setup/__tests__/setup.repository.test.tsx +463 -0
- package/src/modules/setup/__tests__/setup.test.tsx +13 -17
- package/src/modules/setup/components/GitHubRepositoryPanel.tsx +369 -0
- package/src/modules/setup/components/SetupGate.tsx +1 -0
- package/src/modules/setup/components/SetupScreen.tsx +608 -41
- package/src/modules/setup/services/setup.api.ts +171 -3
- package/src/modules/setup/utils/kept-draft.ts +86 -0
- package/src/modules/workspace/components/FileExplorer.tsx +140 -12
- package/src/modules/workspace/components/FileViewer.tsx +11 -1
- package/src/modules/workspace/components/TreeActionConfirm.tsx +5 -7
- package/src/modules/workspace/components/__tests__/FileExplorer.test.tsx +603 -29
- package/src/modules/workspace/components/renderers/DocxRenderer.tsx +23 -6
- package/src/modules/workspace/components/renderers/DownloadFileButton.tsx +41 -8
- package/src/modules/workspace/components/renderers/EmailRenderer.tsx +20 -5
- package/src/modules/workspace/components/renderers/ImageRenderer.tsx +39 -16
- package/src/modules/workspace/components/renderers/PdfRenderer.tsx +27 -7
- package/src/modules/workspace/components/renderers/PptxRenderer.tsx +20 -5
- package/src/modules/workspace/components/renderers/RetryReadButton.tsx +37 -0
- package/src/modules/workspace/components/renderers/XlsxRenderer.tsx +20 -5
- package/src/modules/workspace/components/renderers/__tests__/htmlSandbox.test.ts +186 -7
- package/src/modules/workspace/components/renderers/__tests__/readPane.test.tsx +59 -0
- package/src/modules/workspace/components/renderers/htmlSandbox.ts +80 -20
- package/src/modules/workspace/components/renderers/index.ts +6 -1
- package/src/modules/workspace/components/renderers/readPane.ts +22 -0
- package/src/modules/workspace/components/renderers/rendererWorkspace.ts +50 -9
- package/src/modules/workspace/components/renderers/useReadRetry.ts +27 -0
- package/src/modules/workspace/routing/__tests__/kb-routes.test.tsx +79 -0
- package/src/modules/workspace/routing/kb-routes.ts +25 -10
- package/src/modules/workspace/services/workspace.api.ts +22 -1
- package/src/shared/components/SlotBoundary.tsx +51 -0
- package/src/shared/markdown/__tests__/hrefs.test.ts +111 -0
- package/src/shared/markdown/hrefs.ts +80 -4
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { cleanup, render, screen, waitFor } from '@testing-library/react';
|
|
3
|
+
import userEvent from '@testing-library/user-event';
|
|
4
|
+
|
|
5
|
+
const api = vi.hoisted(() => ({
|
|
6
|
+
fetchSetupStatus: vi.fn(),
|
|
7
|
+
saveSettings: vi.fn(),
|
|
8
|
+
testConnection: vi.fn(),
|
|
9
|
+
testOidc: vi.fn(),
|
|
10
|
+
syncNow: vi.fn(),
|
|
11
|
+
fetchGitHubApp: vi.fn(),
|
|
12
|
+
fetchGitHubRepositories: vi.fn(),
|
|
13
|
+
startGitHubAppRegistration: vi.fn(),
|
|
14
|
+
startGitHubAppInstallation: vi.fn(),
|
|
15
|
+
startGitHubAppRefresh: vi.fn(),
|
|
16
|
+
}));
|
|
17
|
+
vi.mock('../services/setup.api', async () => {
|
|
18
|
+
const actual = await vi.importActual<typeof import('../services/setup.api')>('../services/setup.api');
|
|
19
|
+
return { ...actual, ...api };
|
|
20
|
+
});
|
|
21
|
+
vi.mock('../../settings/services/github-facade.api', () => ({
|
|
22
|
+
fetchGitHubFacade: vi.fn(),
|
|
23
|
+
rotateGitHubFacade: vi.fn(),
|
|
24
|
+
fetchMarketplaceRegistration: vi.fn(async () => false),
|
|
25
|
+
setMarketplaceRegistration: vi.fn(),
|
|
26
|
+
}));
|
|
27
|
+
|
|
28
|
+
import { SetupScreen } from '../components/SetupScreen';
|
|
29
|
+
import type { GitHubAppStatus, GitMode, SettingStatus } from '../services/setup.api';
|
|
30
|
+
import { forgetDraft, keepDraft, keptDraft } from '../utils/kept-draft';
|
|
31
|
+
|
|
32
|
+
const setting = (key: string, section: SettingStatus['section'], extra: Partial<SettingStatus> = {}): SettingStatus => ({
|
|
33
|
+
key,
|
|
34
|
+
section,
|
|
35
|
+
source: 'unset',
|
|
36
|
+
value: '',
|
|
37
|
+
configured: false,
|
|
38
|
+
secret: false,
|
|
39
|
+
restartToApply: false,
|
|
40
|
+
...extra,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const SETTINGS: SettingStatus[] = [
|
|
44
|
+
setting('gitMode', 'knowledge-base'),
|
|
45
|
+
setting('githubRepository', 'knowledge-base'),
|
|
46
|
+
setting('kbRepoUrl', 'knowledge-base'),
|
|
47
|
+
setting('gitToken', 'knowledge-base', { secret: true, value: undefined }),
|
|
48
|
+
setting('knowledgeBaseDir', 'knowledge-base', { restartToApply: true }),
|
|
49
|
+
setting('defaultBranch', 'knowledge-base'),
|
|
50
|
+
setting('protectedBranches', 'knowledge-base'),
|
|
51
|
+
setting('oidcIssuerUrl', 'sign-in'),
|
|
52
|
+
setting('oidcClientId', 'sign-in'),
|
|
53
|
+
setting('oidcClientSecret', 'sign-in', { secret: true, value: undefined }),
|
|
54
|
+
];
|
|
55
|
+
const MODES: GitMode[] = ['managed', 'github-app', 'token'];
|
|
56
|
+
const APP = { slug: 'hexis-acme', url: 'https://github.com/apps/hexis-acme' };
|
|
57
|
+
const NO_APP: GitHubAppStatus = { registeredBy: null, app: null, installation: null, repository: null };
|
|
58
|
+
const REGISTERED: GitHubAppStatus = { registeredBy: 'setup', app: APP, installation: null, repository: null };
|
|
59
|
+
const INSTALLED: GitHubAppStatus = { ...REGISTERED, installation: { id: '77', account: 'acme' } };
|
|
60
|
+
|
|
61
|
+
const realLocation = window.location;
|
|
62
|
+
const assign = vi.fn();
|
|
63
|
+
function standAt(search = '') {
|
|
64
|
+
Object.defineProperty(window, 'location', {
|
|
65
|
+
configurable: true,
|
|
66
|
+
value: { ...realLocation, href: `https://kb.acme.test/${search}`, search, origin: 'https://kb.acme.test', reload: vi.fn(), assign },
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const show = (settings: SettingStatus[] = SETTINGS) =>
|
|
71
|
+
render(<SetupScreen settings={settings} onSaved={() => {}} repository={{ mode: null, chosen: null, modes: MODES }} />);
|
|
72
|
+
const field = (label: string) => screen.getByLabelText(label, { exact: false }) as HTMLInputElement;
|
|
73
|
+
const isSecret = (key: string) => SETTINGS.find((s) => s.key === key)?.secret !== false;
|
|
74
|
+
|
|
75
|
+
/** Fill the form the way someone might before turning to GitHub, then press the button that leaves. */
|
|
76
|
+
async function typeThenLeave(press: string) {
|
|
77
|
+
await userEvent.type(field('Knowledge folder'), 'Docs');
|
|
78
|
+
await userEvent.type(field('Provider address'), 'https://login.example.com');
|
|
79
|
+
await userEvent.type(field('Application ID'), 'an-app');
|
|
80
|
+
await userEvent.type(field('Application secret'), 'a-secret-nobody-should-store');
|
|
81
|
+
await userEvent.click(screen.getByRole('tab', { name: 'Address and token' }));
|
|
82
|
+
await userEvent.type(field('Repository address'), 'https://git.example.com/acme/kb.git');
|
|
83
|
+
await userEvent.type(field('Access token'), 'a-token-nobody-should-store');
|
|
84
|
+
await userEvent.click(screen.getByRole('tab', { name: 'GitHub' }));
|
|
85
|
+
await userEvent.click(await screen.findByRole('button', { name: press }));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The page that comes back from GitHub: a new one, in the same tab. */
|
|
89
|
+
function comeBack(outcome = 'connected') {
|
|
90
|
+
cleanup();
|
|
91
|
+
standAt(`?github=${outcome}`);
|
|
92
|
+
return show();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let submit: ReturnType<typeof vi.spyOn>;
|
|
96
|
+
let replace: ReturnType<typeof vi.spyOn>;
|
|
97
|
+
|
|
98
|
+
beforeEach(() => {
|
|
99
|
+
for (const fn of Object.values(api)) fn.mockReset();
|
|
100
|
+
assign.mockReset();
|
|
101
|
+
sessionStorage.clear();
|
|
102
|
+
api.fetchGitHubApp.mockResolvedValue(NO_APP);
|
|
103
|
+
api.fetchGitHubRepositories.mockResolvedValue({ repositories: [], more: false });
|
|
104
|
+
api.startGitHubAppRegistration.mockResolvedValue({ action: 'https://github.com/settings/apps/new?state=abc', manifest: {} });
|
|
105
|
+
api.startGitHubAppInstallation.mockResolvedValue('https://github.com/apps/hexis-acme/installations/new?state=abc');
|
|
106
|
+
submit = vi.spyOn(HTMLFormElement.prototype, 'submit').mockImplementation(() => undefined);
|
|
107
|
+
replace = vi.spyOn(window.history, 'replaceState').mockImplementation(() => undefined);
|
|
108
|
+
standAt();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
afterEach(() => {
|
|
112
|
+
submit.mockRestore();
|
|
113
|
+
replace.mockRestore();
|
|
114
|
+
Object.defineProperty(window, 'location', { configurable: true, value: realLocation });
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe('what was typed, across the trip to GitHub', () => {
|
|
118
|
+
it('is there again when the browser comes back from creating the app', async () => {
|
|
119
|
+
show();
|
|
120
|
+
await typeThenLeave('Create the GitHub App');
|
|
121
|
+
await waitFor(() => expect(submit).toHaveBeenCalled());
|
|
122
|
+
|
|
123
|
+
api.fetchGitHubApp.mockResolvedValue(REGISTERED);
|
|
124
|
+
comeBack();
|
|
125
|
+
expect(field('Knowledge folder')).toHaveValue('Docs');
|
|
126
|
+
// What was being typed about sign-in is shown, on the tab it was typed on.
|
|
127
|
+
expect(field('Provider address')).toHaveValue('https://login.example.com');
|
|
128
|
+
expect(field('Application ID')).toHaveValue('an-app');
|
|
129
|
+
await userEvent.click(screen.getByRole('tab', { name: 'Address and token' }));
|
|
130
|
+
expect(field('Repository address')).toHaveValue('https://git.example.com/acme/kb.git');
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('is there again after the second trip too, to install the app', async () => {
|
|
134
|
+
api.fetchGitHubApp.mockResolvedValue(REGISTERED);
|
|
135
|
+
show();
|
|
136
|
+
await typeThenLeave('Install the app on GitHub');
|
|
137
|
+
await waitFor(() => expect(assign).toHaveBeenCalled());
|
|
138
|
+
|
|
139
|
+
api.fetchGitHubApp.mockResolvedValue(INSTALLED);
|
|
140
|
+
comeBack();
|
|
141
|
+
expect(field('Knowledge folder')).toHaveValue('Docs');
|
|
142
|
+
expect(field('Provider address')).toHaveValue('https://login.example.com');
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('is what the form held when the browser left, not when the button was pressed', async () => {
|
|
146
|
+
let answer: (address: string) => void = () => undefined;
|
|
147
|
+
api.fetchGitHubApp.mockResolvedValue(REGISTERED);
|
|
148
|
+
api.startGitHubAppInstallation.mockReturnValue(new Promise<string>((resolve) => (answer = resolve)));
|
|
149
|
+
show();
|
|
150
|
+
await userEvent.click(screen.getByRole('tab', { name: 'GitHub' }));
|
|
151
|
+
await userEvent.click(await screen.findByRole('button', { name: 'Install the app on GitHub' }));
|
|
152
|
+
// Typed while GitHub was being asked where to go.
|
|
153
|
+
await userEvent.type(field('Knowledge folder'), 'Docs');
|
|
154
|
+
answer('https://github.com/apps/hexis-acme/installations/new?state=abc');
|
|
155
|
+
await waitFor(() => expect(assign).toHaveBeenCalled());
|
|
156
|
+
|
|
157
|
+
comeBack();
|
|
158
|
+
expect(field('Knowledge folder')).toHaveValue('Docs');
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('is saved with everything else, like anything typed', async () => {
|
|
162
|
+
api.saveSettings.mockResolvedValue({ restartRequired: false, complete: false, settings: SETTINGS, repository: { mode: null, chosen: null, modes: MODES } });
|
|
163
|
+
show();
|
|
164
|
+
await typeThenLeave('Create the GitHub App');
|
|
165
|
+
comeBack();
|
|
166
|
+
await userEvent.click(screen.getByRole('tab', { name: 'Managed for you' }));
|
|
167
|
+
await userEvent.click(screen.getByRole('button', { name: 'Save and continue' }));
|
|
168
|
+
await waitFor(() => expect(api.saveSettings).toHaveBeenCalled());
|
|
169
|
+
expect(api.saveSettings.mock.calls[0]![0]).toEqual({
|
|
170
|
+
gitMode: 'managed',
|
|
171
|
+
knowledgeBaseDir: 'Docs',
|
|
172
|
+
oidcIssuerUrl: 'https://login.example.com',
|
|
173
|
+
oidcClientId: 'an-app',
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
describe('a secret, across the trip to GitHub', () => {
|
|
179
|
+
it('is never written to the browser storage', async () => {
|
|
180
|
+
show();
|
|
181
|
+
await typeThenLeave('Create the GitHub App');
|
|
182
|
+
await waitFor(() => expect(submit).toHaveBeenCalled());
|
|
183
|
+
const stored = Object.keys(sessionStorage).map((key) => sessionStorage.getItem(key) ?? '').join('\n');
|
|
184
|
+
expect(stored).toContain('Docs');
|
|
185
|
+
expect(stored).not.toContain('a-secret-nobody-should-store');
|
|
186
|
+
expect(stored).not.toContain('a-token-nobody-should-store');
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('comes back empty, and the screen says which to enter again', async () => {
|
|
190
|
+
show();
|
|
191
|
+
await typeThenLeave('Create the GitHub App');
|
|
192
|
+
comeBack();
|
|
193
|
+
expect(field('Application secret')).toHaveValue('');
|
|
194
|
+
const notice = screen.getByTestId('enter-again');
|
|
195
|
+
expect(notice).toHaveTextContent('What you had entered is back, except');
|
|
196
|
+
expect(notice).toHaveTextContent('Application secret');
|
|
197
|
+
expect(notice).toHaveTextContent('Access token');
|
|
198
|
+
expect(notice).toHaveTextContent('enter them again');
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('stops being asked for as each is entered again', async () => {
|
|
202
|
+
show();
|
|
203
|
+
await typeThenLeave('Create the GitHub App');
|
|
204
|
+
comeBack();
|
|
205
|
+
await userEvent.type(field('Application secret'), 'typed-again');
|
|
206
|
+
expect(screen.getByTestId('enter-again')).not.toHaveTextContent('Application secret');
|
|
207
|
+
expect(screen.getByTestId('enter-again')).toHaveTextContent('enter it again');
|
|
208
|
+
await userEvent.click(screen.getByRole('tab', { name: 'Address and token' }));
|
|
209
|
+
await userEvent.type(field('Access token'), 'typed-again');
|
|
210
|
+
expect(screen.queryByTestId('enter-again')).toBeNull();
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('is not given back even if the storage was made to hold one', () => {
|
|
214
|
+
sessionStorage.setItem('hexis_setup_draft', JSON.stringify({ draft: { gitToken: 'planted', knowledgeBaseDir: 'Docs' }, dropped: [], at: Date.now() }));
|
|
215
|
+
expect(keptDraft(isSecret).draft).toEqual({ knowledgeBaseDir: 'Docs' });
|
|
216
|
+
// A setting the form has never heard of is treated as one.
|
|
217
|
+
expect(keptDraft(isSecret).draft).not.toHaveProperty('somethingElse');
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
describe('what is kept, and for how long', () => {
|
|
222
|
+
it('is given back once: a reload of the page that came back starts from what is stored', async () => {
|
|
223
|
+
show();
|
|
224
|
+
await typeThenLeave('Create the GitHub App');
|
|
225
|
+
comeBack();
|
|
226
|
+
expect(field('Knowledge folder')).toHaveValue('Docs');
|
|
227
|
+
expect(sessionStorage.getItem('hexis_setup_draft')).toBeNull();
|
|
228
|
+
comeBack();
|
|
229
|
+
expect(field('Knowledge folder')).toHaveValue('');
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it('is not sprung on someone who opens the screen without coming back from GitHub', async () => {
|
|
233
|
+
show();
|
|
234
|
+
await typeThenLeave('Create the GitHub App');
|
|
235
|
+
// The trip was abandoned; the screen is opened again later, in the same tab.
|
|
236
|
+
cleanup();
|
|
237
|
+
standAt();
|
|
238
|
+
show();
|
|
239
|
+
expect(field('Knowledge folder')).toHaveValue('');
|
|
240
|
+
expect(screen.queryByTestId('enter-again')).toBeNull();
|
|
241
|
+
// And it is gone, so a later return from GitHub does not find it either.
|
|
242
|
+
expect(sessionStorage.getItem('hexis_setup_draft')).toBeNull();
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it('is good for half an hour', () => {
|
|
246
|
+
const left = Date.parse('2026-09-29T10:00:00Z');
|
|
247
|
+
keepDraft({ knowledgeBaseDir: 'Docs' }, isSecret, left);
|
|
248
|
+
expect(keptDraft(isSecret, left + 29 * 60_000).draft).toEqual({ knowledgeBaseDir: 'Docs' });
|
|
249
|
+
expect(keptDraft(isSecret, left + 31 * 60_000).draft).toEqual({});
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it('keeps nothing when nothing was typed', () => {
|
|
253
|
+
keepDraft({ knowledgeBaseDir: ' ', gitMode: '' }, isSecret);
|
|
254
|
+
expect(sessionStorage.getItem('hexis_setup_draft')).toBeNull();
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it('gives back nothing for a setting the environment has taken over meanwhile', async () => {
|
|
258
|
+
show();
|
|
259
|
+
await typeThenLeave('Create the GitHub App');
|
|
260
|
+
cleanup();
|
|
261
|
+
standAt('?github=connected');
|
|
262
|
+
show(SETTINGS.map((s) => (s.key === 'knowledgeBaseDir' ? { ...s, source: 'env', envVar: 'KB_KNOWLEDGE_BASE_DIR' } : s)));
|
|
263
|
+
expect(screen.queryByLabelText('Knowledge folder', { exact: false })).toBeNull();
|
|
264
|
+
expect(field('Provider address')).toHaveValue('https://login.example.com');
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it('reads storage that holds something else as nothing kept', () => {
|
|
268
|
+
for (const junk of ['not json', '{"draft":"no"}', '{"at":"yesterday","draft":{}}', '[]', 'null']) {
|
|
269
|
+
sessionStorage.setItem('hexis_setup_draft', junk);
|
|
270
|
+
expect(keptDraft(isSecret), junk).toEqual({ draft: {}, dropped: [] });
|
|
271
|
+
}
|
|
272
|
+
forgetDraft();
|
|
273
|
+
expect(sessionStorage.getItem('hexis_setup_draft')).toBeNull();
|
|
274
|
+
});
|
|
275
|
+
});
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { render, screen, waitFor } from '@testing-library/react';
|
|
3
|
+
import userEvent from '@testing-library/user-event';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Pointing the deployment at a DIFFERENT knowledge-base repository.
|
|
7
|
+
*
|
|
8
|
+
* The server refuses such a save until it is confirmed, because confirming it
|
|
9
|
+
* is the only warning anyone gets: every working copy on the server stops
|
|
10
|
+
* being used and is cloned fresh, and anything committed there and never
|
|
11
|
+
* pushed goes out of the app with it. When change requests are open the same
|
|
12
|
+
* confirmation says how many and asks what to do with them.
|
|
13
|
+
*
|
|
14
|
+
* Only the transport is mocked, so what is under test is the whole path from
|
|
15
|
+
* the button to the request bodies the form sends.
|
|
16
|
+
*/
|
|
17
|
+
const transport = vi.hoisted(() => ({ authFetch: vi.fn() }));
|
|
18
|
+
vi.mock('../../../lib/api', async (importOriginal) => ({
|
|
19
|
+
...(await importOriginal<typeof import('../../../lib/api')>()),
|
|
20
|
+
authFetch: transport.authFetch,
|
|
21
|
+
}));
|
|
22
|
+
|
|
23
|
+
const facade = vi.hoisted(() => ({
|
|
24
|
+
fetchGitHubFacade: vi.fn(),
|
|
25
|
+
rotateGitHubFacade: vi.fn(),
|
|
26
|
+
fetchMarketplaceRegistration: vi.fn(async () => false),
|
|
27
|
+
setMarketplaceRegistration: vi.fn(),
|
|
28
|
+
}));
|
|
29
|
+
vi.mock('../../settings/services/github-facade.api', () => facade);
|
|
30
|
+
|
|
31
|
+
import { SetupScreen } from '../components/SetupScreen';
|
|
32
|
+
import type { SettingStatus } from '../services/setup.api';
|
|
33
|
+
|
|
34
|
+
const KB = 'knowledge-base' as const;
|
|
35
|
+
const CONFIGURED = 'https://example.com/acme/kb.git';
|
|
36
|
+
const REPLACEMENT = 'https://example.com/acme/replacement.git';
|
|
37
|
+
|
|
38
|
+
/** A deployment that has been running against CONFIGURED for a while. */
|
|
39
|
+
const SETTINGS: SettingStatus[] = [
|
|
40
|
+
{ key: 'kbRepoUrl', envVar: 'KB_REPO_URL', section: KB, source: 'stored', value: CONFIGURED, configured: true, secret: false, restartToApply: false },
|
|
41
|
+
{ key: 'gitToken', envVar: 'GIT_TOKEN', section: KB, source: 'stored', configured: true, secret: true, restartToApply: false },
|
|
42
|
+
{ key: 'gitUsername', envVar: 'GIT_USERNAME', section: KB, source: 'unset', value: '', configured: false, secret: false, restartToApply: false },
|
|
43
|
+
{ key: 'defaultBranch', envVar: 'DEFAULT_BRANCH', section: KB, source: 'stored', value: 'main', configured: true, secret: false, restartToApply: true },
|
|
44
|
+
{ key: 'protectedBranches', envVar: 'PROTECTED_BRANCHES', section: KB, source: 'stored', value: 'main', configured: true, secret: false, restartToApply: true },
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
const json = (status: number, body: unknown) =>
|
|
48
|
+
new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } });
|
|
49
|
+
|
|
50
|
+
/** The bodies of every POST to the save endpoint, oldest first. */
|
|
51
|
+
const saveBodies = (): Array<Record<string, unknown>> =>
|
|
52
|
+
transport.authFetch.mock.calls
|
|
53
|
+
.filter(([url]) => String(url) === '/api/setup/settings')
|
|
54
|
+
.map(([, init]) => JSON.parse(String((init as RequestInit).body)) as Record<string, unknown>);
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The server: the remote answers for the new address, and the save is refused
|
|
58
|
+
* with a 409 until a confirmation rides along.
|
|
59
|
+
*/
|
|
60
|
+
function serverWith(openChangeRequests: number) {
|
|
61
|
+
transport.authFetch.mockImplementation(async (url: string, init?: RequestInit) => {
|
|
62
|
+
if (url === '/api/setup/test-connection') {
|
|
63
|
+
return json(200, { ok: true, outcome: 'connected', branches: ['main'], defaultBranch: 'main', empty: false });
|
|
64
|
+
}
|
|
65
|
+
if (url === '/api/setup/settings') {
|
|
66
|
+
const body = JSON.parse(String(init?.body ?? '{}')) as { confirmRepositoryChange?: string };
|
|
67
|
+
if (!body.confirmRepositoryChange) {
|
|
68
|
+
return json(409, {
|
|
69
|
+
error: 'This moves the deployment to another repository.',
|
|
70
|
+
repositoryChange: { openChangeRequests },
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return json(200, {
|
|
74
|
+
ok: true,
|
|
75
|
+
restartRequired: false,
|
|
76
|
+
complete: true,
|
|
77
|
+
settings: SETTINGS,
|
|
78
|
+
repositoryChange: {
|
|
79
|
+
choice: body.confirmRepositoryChange,
|
|
80
|
+
closedChangeRequests: body.confirmRepositoryChange === 'close' ? openChangeRequests : 0,
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
return json(404, { error: 'not mocked' });
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Type the new address into a rendered settings form and press Save. */
|
|
89
|
+
async function typeTheNewAddressAndSave() {
|
|
90
|
+
const url = screen.getByLabelText('Repository address');
|
|
91
|
+
await userEvent.clear(url);
|
|
92
|
+
await userEvent.type(url, REPLACEMENT);
|
|
93
|
+
await userEvent.type(screen.getByLabelText('Access token'), 'ghp_new');
|
|
94
|
+
await userEvent.click(screen.getByRole('button', { name: 'Save and continue' }));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
beforeEach(() => {
|
|
98
|
+
transport.authFetch.mockReset();
|
|
99
|
+
Object.defineProperty(window, 'location', {
|
|
100
|
+
configurable: true,
|
|
101
|
+
value: { ...window.location, reload: vi.fn(), origin: 'https://example.test' },
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
describe('a changed repository address, with change requests open', () => {
|
|
106
|
+
it('asks before saving: what is replaced, what is lost, and how many requests', async () => {
|
|
107
|
+
serverWith(2);
|
|
108
|
+
render(<SetupScreen settings={SETTINGS} onSaved={vi.fn()} variant="settings" />);
|
|
109
|
+
|
|
110
|
+
await typeTheNewAddressAndSave();
|
|
111
|
+
|
|
112
|
+
const confirm = await screen.findByTestId('repository-change-confirm');
|
|
113
|
+
expect(confirm).toHaveTextContent(/stops being used and is cloned fresh/i);
|
|
114
|
+
// The warning has to name the cost, and name it truthfully: the copies are
|
|
115
|
+
// set aside on the server, so the work is out of the app but not destroyed.
|
|
116
|
+
expect(confirm).toHaveTextContent(/not yet pushed from this server goes out of the app/i);
|
|
117
|
+
expect(confirm).toHaveTextContent(/nothing is deleted/i);
|
|
118
|
+
expect(confirm).toHaveTextContent(/replaced-working-copies/);
|
|
119
|
+
expect(confirm).toHaveTextContent('There are 2 open change requests.');
|
|
120
|
+
// Two choices, and keeping them is the one already selected: nothing
|
|
121
|
+
// closes by hesitating.
|
|
122
|
+
const [keep, close] = screen.getAllByRole('radio');
|
|
123
|
+
expect(keep).toBeChecked();
|
|
124
|
+
expect(close).not.toBeChecked();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('keeps them when the admin says the repository only moved', async () => {
|
|
128
|
+
serverWith(2);
|
|
129
|
+
render(<SetupScreen settings={SETTINGS} onSaved={vi.fn()} variant="settings" />);
|
|
130
|
+
await typeTheNewAddressAndSave();
|
|
131
|
+
await screen.findByTestId('repository-change-confirm');
|
|
132
|
+
|
|
133
|
+
await userEvent.click(screen.getByRole('button', { name: 'Move the deployment' }));
|
|
134
|
+
|
|
135
|
+
await waitFor(() => expect(saveBodies()).toHaveLength(2));
|
|
136
|
+
expect(saveBodies()[1]).toMatchObject({
|
|
137
|
+
confirmRepositoryChange: 'keep',
|
|
138
|
+
settings: { kbRepoUrl: REPLACEMENT },
|
|
139
|
+
});
|
|
140
|
+
expect(await screen.findByTestId('repository-changed')).toHaveTextContent(/now works on the new repository/i);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('closes them as “repository replaced” when the admin picks that, and says so', async () => {
|
|
144
|
+
serverWith(2);
|
|
145
|
+
render(<SetupScreen settings={SETTINGS} onSaved={vi.fn()} variant="settings" />);
|
|
146
|
+
await typeTheNewAddressAndSave();
|
|
147
|
+
await screen.findByTestId('repository-change-confirm');
|
|
148
|
+
|
|
149
|
+
await userEvent.click(screen.getAllByRole('radio')[1]!);
|
|
150
|
+
await userEvent.click(screen.getByRole('button', { name: 'Move the deployment' }));
|
|
151
|
+
|
|
152
|
+
await waitFor(() => expect(saveBodies()).toHaveLength(2));
|
|
153
|
+
expect(saveBodies()[1]).toMatchObject({ confirmRepositoryChange: 'close' });
|
|
154
|
+
const done = await screen.findByTestId('repository-changed');
|
|
155
|
+
expect(done).toHaveTextContent('2 change requests were closed as “repository replaced”.');
|
|
156
|
+
expect(done).toHaveTextContent('Nothing was deleted.');
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('cancelling saves nothing and leaves the typed address on screen', async () => {
|
|
160
|
+
serverWith(2);
|
|
161
|
+
render(<SetupScreen settings={SETTINGS} onSaved={vi.fn()} variant="settings" />);
|
|
162
|
+
await typeTheNewAddressAndSave();
|
|
163
|
+
await screen.findByTestId('repository-change-confirm');
|
|
164
|
+
|
|
165
|
+
await userEvent.click(screen.getByRole('button', { name: 'Cancel' }));
|
|
166
|
+
|
|
167
|
+
expect(screen.queryByTestId('repository-change-confirm')).not.toBeInTheDocument();
|
|
168
|
+
// One refused attempt, and nothing since.
|
|
169
|
+
expect(saveBodies()).toHaveLength(1);
|
|
170
|
+
expect(screen.getByLabelText('Repository address')).toHaveValue(REPLACEMENT);
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
describe('a changed repository address with nothing open', () => {
|
|
175
|
+
it('still warns about the working copies, and offers no choice it cannot honour', async () => {
|
|
176
|
+
serverWith(0);
|
|
177
|
+
render(<SetupScreen settings={SETTINGS} onSaved={vi.fn()} variant="settings" />);
|
|
178
|
+
await typeTheNewAddressAndSave();
|
|
179
|
+
|
|
180
|
+
const confirm = await screen.findByTestId('repository-change-confirm');
|
|
181
|
+
expect(confirm).toHaveTextContent(/stops being used and is cloned fresh/i);
|
|
182
|
+
expect(screen.queryAllByRole('radio')).toEqual([]);
|
|
183
|
+
|
|
184
|
+
await userEvent.click(screen.getByRole('button', { name: 'Move the deployment' }));
|
|
185
|
+
await waitFor(() => expect(saveBodies()).toHaveLength(2));
|
|
186
|
+
expect(saveBodies()[1]).toMatchObject({ confirmRepositoryChange: 'keep' });
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
describe('a CONFIRMED save the server refuses', () => {
|
|
191
|
+
/**
|
|
192
|
+
* The probe passed and the confirmation was answered, and the store step
|
|
193
|
+
* still failed — a token revoked in between, a repository that went away, a
|
|
194
|
+
* close that could not be done. The admin has just agreed to lose their
|
|
195
|
+
* working copies; being told only that it "did not save" is the worst moment
|
|
196
|
+
* for that.
|
|
197
|
+
*/
|
|
198
|
+
it('shows the reason the server gave for refusing the confirmed save', async () => {
|
|
199
|
+
transport.authFetch.mockImplementation(async (url: string, init?: RequestInit) => {
|
|
200
|
+
if (url === '/api/setup/test-connection') {
|
|
201
|
+
return json(200, { ok: true, outcome: 'connected', branches: ['main'], defaultBranch: 'main', empty: false });
|
|
202
|
+
}
|
|
203
|
+
if (url === '/api/setup/settings') {
|
|
204
|
+
const body = JSON.parse(String(init?.body ?? '{}')) as { confirmRepositoryChange?: string };
|
|
205
|
+
if (!body.confirmRepositoryChange) {
|
|
206
|
+
return json(409, {
|
|
207
|
+
error: 'This moves the deployment to another repository.',
|
|
208
|
+
repositoryChange: { openChangeRequests: 2 },
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
return json(500, {
|
|
212
|
+
error:
|
|
213
|
+
'The open change requests could not be closed, so the repository was not changed. ' +
|
|
214
|
+
'Nothing was saved and no working copy was touched. Try again.',
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
return json(404, { error: 'not mocked' });
|
|
218
|
+
});
|
|
219
|
+
render(<SetupScreen settings={SETTINGS} onSaved={vi.fn()} variant="settings" />);
|
|
220
|
+
await typeTheNewAddressAndSave();
|
|
221
|
+
await screen.findByTestId('repository-change-confirm');
|
|
222
|
+
|
|
223
|
+
await userEvent.click(screen.getAllByRole('radio')[1]!);
|
|
224
|
+
await userEvent.click(screen.getByRole('button', { name: 'Move the deployment' }));
|
|
225
|
+
|
|
226
|
+
expect(
|
|
227
|
+
await screen.findByText(/The open change requests could not be closed/),
|
|
228
|
+
).toBeInTheDocument();
|
|
229
|
+
// And nothing claims the repository was replaced.
|
|
230
|
+
expect(screen.queryByTestId('repository-changed')).not.toBeInTheDocument();
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* The address IS stored and it is the initialization that failed. The
|
|
235
|
+
* confirmation has been answered and must go: left standing over a cleared
|
|
236
|
+
* draft, its Replace button would re-send the stored address, change
|
|
237
|
+
* nothing, and lose the "requests closed" outcome for good. The init banner
|
|
238
|
+
* owns the retry from here.
|
|
239
|
+
*/
|
|
240
|
+
it('takes the confirmation down when the save stored the address but initialization failed', async () => {
|
|
241
|
+
transport.authFetch.mockImplementation(async (url: string, init?: RequestInit) => {
|
|
242
|
+
if (url === '/api/setup/test-connection') {
|
|
243
|
+
return json(200, { ok: true, outcome: 'connected', branches: ['main'], defaultBranch: 'main', empty: false });
|
|
244
|
+
}
|
|
245
|
+
if (url === '/api/setup/settings') {
|
|
246
|
+
const body = JSON.parse(String(init?.body ?? '{}')) as { confirmRepositoryChange?: string };
|
|
247
|
+
if (!body.confirmRepositoryChange) {
|
|
248
|
+
return json(409, {
|
|
249
|
+
error: 'This moves the deployment to another repository.',
|
|
250
|
+
repositoryChange: { openChangeRequests: 1 },
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
return json(500, {
|
|
254
|
+
error: 'The knowledge base could not be initialized.',
|
|
255
|
+
kbInit: { kind: 'not-found', cause: 'There is no repository at that address.' },
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
return json(404, { error: 'not mocked' });
|
|
259
|
+
});
|
|
260
|
+
render(<SetupScreen settings={SETTINGS} onSaved={vi.fn()} variant="settings" />);
|
|
261
|
+
await typeTheNewAddressAndSave();
|
|
262
|
+
await screen.findByTestId('repository-change-confirm');
|
|
263
|
+
|
|
264
|
+
await userEvent.click(screen.getByRole('button', { name: 'Move the deployment' }));
|
|
265
|
+
|
|
266
|
+
await waitFor(() =>
|
|
267
|
+
expect(screen.queryByTestId('repository-change-confirm')).not.toBeInTheDocument(),
|
|
268
|
+
);
|
|
269
|
+
expect(screen.queryByRole('button', { name: 'Move the deployment' })).toBeNull();
|
|
270
|
+
expect(await screen.findByText(/There is no repository at that address\./)).toBeInTheDocument();
|
|
271
|
+
// Two requests went out; nothing re-sent the answered confirmation.
|
|
272
|
+
expect(saveBodies()).toHaveLength(2);
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
describe('a save the server refuses', () => {
|
|
277
|
+
it('shows the reason the server gave, not only that it did not save', async () => {
|
|
278
|
+
transport.authFetch.mockImplementation(async (url: string) => {
|
|
279
|
+
if (url === '/api/setup/test-connection') {
|
|
280
|
+
return json(400, {
|
|
281
|
+
ok: false,
|
|
282
|
+
error: 'There is no repository at that address, or the token cannot see it.',
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
return json(404, { error: 'not mocked' });
|
|
286
|
+
});
|
|
287
|
+
render(<SetupScreen settings={SETTINGS} onSaved={vi.fn()} variant="settings" />);
|
|
288
|
+
|
|
289
|
+
await typeTheNewAddressAndSave();
|
|
290
|
+
|
|
291
|
+
// "Not saved" alone leaves an admin rereading a form that looks correct.
|
|
292
|
+
const banner = await screen.findByText(/Not saved\./);
|
|
293
|
+
expect(banner).toHaveTextContent('There is no repository at that address, or the token cannot see it.');
|
|
294
|
+
expect(saveBodies()).toEqual([]);
|
|
295
|
+
});
|
|
296
|
+
});
|