@bevel-software/platform-core-frontend 0.28.0 → 0.29.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 +11 -4
- package/src/core/CoreAppShell.tsx +29 -11
- package/src/core/__tests__/ShellRoutes.test.tsx +43 -1
- package/src/core/events.ts +4 -2
- package/src/core/registry.ts +25 -3
- package/src/modules/embed/__tests__/EmbedView.test.tsx +560 -17
- package/src/modules/embed/__tests__/embed-config.test.ts +77 -1
- package/src/modules/embed/__tests__/mcp-app-view.test.ts +203 -1
- package/src/modules/embed/components/EmbedView.tsx +233 -47
- package/src/modules/embed/embed-config.ts +126 -2
- package/src/modules/embed/embed-host.ts +11 -2
- package/src/modules/embed/mount.tsx +1 -0
- package/src/modules/library/__tests__/LibraryRoutes.test.tsx +213 -4
- package/src/modules/library/__tests__/PluginPage.test.tsx +1 -1
- package/src/modules/library/__tests__/SkillPage.test.tsx +10 -0
- package/src/modules/library/components/LibraryLayout.tsx +2 -2
- package/src/modules/library/components/PluginPage.tsx +2 -1
- package/src/modules/library/components/skill-page/SkillPage.tsx +2 -2
- package/src/modules/library/components/tool-page/ToolPage.tsx +1 -1
- package/src/modules/library/index.ts +18 -0
- package/src/modules/library/routes/LibraryRoutes.tsx +17 -1
- package/src/modules/library/routes/library-paths.ts +16 -2
- package/src/modules/onboarding/__tests__/get-started-column.test.tsx +3 -3
- package/src/modules/secrets-vault/components/ConnectToolsPage.tsx +39 -32
- package/src/modules/secrets-vault/components/SecretsPage.tsx +9 -24
- package/src/modules/secrets-vault/components/__tests__/ConnectToolsPage.test.tsx +111 -8
- package/src/modules/secrets-vault/components/__tests__/connect-lists-what-the-banner-counted.test.tsx +1 -1
- package/src/modules/secrets-vault/index.ts +7 -0
- package/src/modules/settings/settings-nav-items.tsx +3 -3
- package/src/modules/toolbar/commands/__tests__/actions.test.tsx +20 -37
- package/src/modules/toolbar/commands/actions.tsx +62 -50
- package/src/modules/toolbar/commands/command-menu.ts +37 -3
- package/src/modules/toolbar/commands/useCommandShortcuts.ts +29 -76
- package/src/modules/toolbar/components/ExternalAgentAccessPage.tsx +4 -2
- package/src/modules/toolbar/components/SearchPalette.tsx +20 -21
- package/src/modules/toolbar/components/__tests__/ExternalAgentAccessPage.test.tsx +5 -2
- package/src/modules/toolbar/components/__tests__/SearchPalette.test.tsx +242 -103
- package/src/modules/toolbar/components/__tests__/Toolbar.test.tsx +51 -1
- package/src/modules/toolbar/search/__tests__/rank.test.ts +7 -3
- package/src/modules/workflow/hooks/__tests__/useFileLock.test.tsx +475 -0
- package/src/modules/workflow/hooks/__tests__/useFileLock.wire.test.tsx +115 -0
- package/src/modules/workflow/hooks/useFileLock.ts +321 -57
- package/src/modules/workflow/index.ts +8 -0
- package/src/modules/workflow/services/__tests__/lock.api.test.ts +4 -0
- package/src/modules/workflow/services/lock.api.ts +28 -9
- package/src/modules/workspace/components/FileViewer.tsx +30 -16
- package/src/modules/workspace/components/KbPageHeader.tsx +8 -1
- package/src/modules/workspace/components/__tests__/FileViewer.test.tsx +107 -4
- package/src/modules/workspace/components/__tests__/KbPageHeader.test.tsx +12 -0
- package/src/modules/workspace/components/renderers/HtmlRenderer.tsx +118 -13
- package/src/modules/workspace/components/renderers/__tests__/HtmlRenderer.test.tsx +189 -0
- package/src/modules/workspace/components/renderers/__tests__/htmlSandbox.test.ts +9 -0
- package/src/modules/workspace/components/renderers/__tests__/kbGraphLoader.test.tsx +60 -0
- package/src/modules/workspace/components/renderers/frameResizeHandle.ts +116 -0
- package/src/modules/workspace/components/renderers/index.ts +6 -0
- package/src/modules/workspace/components/renderers/kbGraphLoader.ts +27 -0
- package/src/modules/workspace/components/renderers/lazyRenderer.tsx +1 -1
- package/src/modules/workspace/components/renderers/rendererSurface.ts +19 -0
- package/src/modules/workspace/index.ts +33 -0
|
@@ -4,7 +4,10 @@ import userEvent from '@testing-library/user-event';
|
|
|
4
4
|
import { MemoryRouter } from 'react-router-dom';
|
|
5
5
|
import { AppRegistryContext, type AppRegistry } from '../../../core/registry';
|
|
6
6
|
import type { EmbedFileView } from '../services/embed.api';
|
|
7
|
-
import {
|
|
7
|
+
import { useEffect, useState } from 'react';
|
|
8
|
+
import { EMBED_EXPIRED, EMBED_HEIGHT_MESSAGE } from '../embed-host';
|
|
9
|
+
import { configureEmbed, resetEmbedConfig } from '../embed-config';
|
|
10
|
+
import { useKbGraphLoader, useRendererSurface } from '../../workspace';
|
|
8
11
|
|
|
9
12
|
/**
|
|
10
13
|
* The HTTP surface, stubbed. The error CLASS is hoisted with the rest: the
|
|
@@ -101,6 +104,120 @@ afterEach(() => {
|
|
|
101
104
|
// on its way out, and that call must still meet the stubbed API.
|
|
102
105
|
cleanup();
|
|
103
106
|
vi.restoreAllMocks();
|
|
107
|
+
resetEmbedConfig();
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
/** Heartbeats captured instead of scheduled, so a test can fire one. */
|
|
111
|
+
function captureHeartbeats(): Array<() => void> {
|
|
112
|
+
const ticks: Array<() => void> = [];
|
|
113
|
+
const realSetInterval = window.setInterval.bind(window);
|
|
114
|
+
vi.spyOn(window, 'setInterval').mockImplementation(((fn: () => void, ms?: number) => {
|
|
115
|
+
if (ms === 30_000) {
|
|
116
|
+
ticks.push(fn);
|
|
117
|
+
return 0;
|
|
118
|
+
}
|
|
119
|
+
return realSetInterval(fn, ms);
|
|
120
|
+
}) as typeof window.setInterval);
|
|
121
|
+
return ticks;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** The page as a test edits it. */
|
|
125
|
+
const EDITED = '# Thing\n\nWhat it is, edited.\n';
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* A host that sizes its frame to the content (the Atlassian issue panel)
|
|
129
|
+
* follows the height the embed reports. Only when framed: with no host there
|
|
130
|
+
* is nobody to tell.
|
|
131
|
+
*/
|
|
132
|
+
describe('reporting the content height to the host', () => {
|
|
133
|
+
const realParent = Object.getOwnPropertyDescriptor(window, 'parent');
|
|
134
|
+
// The connector's mint puts `sizing=content` on the address: this host
|
|
135
|
+
// sizes its frame to the content.
|
|
136
|
+
beforeEach(() => window.history.replaceState({}, '', '/embed?token=tok&sizing=content'));
|
|
137
|
+
afterEach(() => {
|
|
138
|
+
// The host put in place by a case goes with it: left behind, a later
|
|
139
|
+
// "not framed" case would run framed and pass without being exercised.
|
|
140
|
+
if (realParent) Object.defineProperty(window, 'parent', realParent);
|
|
141
|
+
else delete (window as { parent?: unknown }).parent;
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it('keeps a fixed reading pane as it is: nothing relaxed, nothing posted, when the host did not ask for content sizing', async () => {
|
|
145
|
+
window.history.replaceState({}, '', '/embed?token=tok');
|
|
146
|
+
const host = { postMessage: vi.fn() };
|
|
147
|
+
Object.defineProperty(window, 'parent', { configurable: true, value: host });
|
|
148
|
+
api.loadEmbed.mockResolvedValue(view());
|
|
149
|
+
mount();
|
|
150
|
+
await screen.findByRole('heading', { name: 'Thing' });
|
|
151
|
+
expect(host.postMessage.mock.calls.filter(([m]) => (m as { type?: string }).type === EMBED_HEIGHT_MESSAGE)).toEqual([]);
|
|
152
|
+
expect(document.documentElement.style.height).toBe('');
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('posts its height to the host when framed, and restores the page styles when it leaves', async () => {
|
|
156
|
+
const host = { postMessage: vi.fn() };
|
|
157
|
+
Object.defineProperty(window, 'parent', { configurable: true, value: host });
|
|
158
|
+
api.loadEmbed.mockResolvedValue(view());
|
|
159
|
+
const { unmount } = mount();
|
|
160
|
+
await screen.findByRole('heading', { name: 'Thing' });
|
|
161
|
+
const heights = () => host.postMessage.mock.calls.filter(([m]) => (m as { type?: string }).type === EMBED_HEIGHT_MESSAGE);
|
|
162
|
+
// Once at mount, and again once the page is on screen — without relying
|
|
163
|
+
// on a ResizeObserver, which jsdom (like some hosts) does not have.
|
|
164
|
+
expect(heights().length).toBeGreaterThanOrEqual(2);
|
|
165
|
+
expect(heights()[0]![0]).toEqual({ type: EMBED_HEIGHT_MESSAGE, height: expect.any(Number) });
|
|
166
|
+
const beforeEdit = heights().length;
|
|
167
|
+
fireEvent.click(screen.getByRole('button', { name: 'Edit' }));
|
|
168
|
+
await waitFor(() => expect(heights().length).toBeGreaterThan(beforeEdit));
|
|
169
|
+
// The three-pane shell's 100% height is relaxed while the embed is up…
|
|
170
|
+
expect(document.documentElement.style.height).toBe('auto');
|
|
171
|
+
unmount();
|
|
172
|
+
// …and put back when it is gone.
|
|
173
|
+
expect(document.documentElement.style.height).toBe('');
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('posts the new height when the content grows without anything on screen changing', async () => {
|
|
177
|
+
// The observer is what carries growth that changes no state — an image
|
|
178
|
+
// loading, a renderer settling. jsdom has none, so a stand-in captures
|
|
179
|
+
// the callback and the measured height is driven by hand.
|
|
180
|
+
let onResize: (() => void) | null = null;
|
|
181
|
+
class FakeResizeObserver {
|
|
182
|
+
constructor(cb: () => void) {
|
|
183
|
+
onResize = cb;
|
|
184
|
+
}
|
|
185
|
+
observe() {}
|
|
186
|
+
disconnect() {}
|
|
187
|
+
}
|
|
188
|
+
vi.stubGlobal('ResizeObserver', FakeResizeObserver);
|
|
189
|
+
let measured = 240;
|
|
190
|
+
vi.spyOn(document.documentElement, 'getBoundingClientRect').mockImplementation(
|
|
191
|
+
() => ({ height: measured }) as DOMRect,
|
|
192
|
+
);
|
|
193
|
+
const host = { postMessage: vi.fn() };
|
|
194
|
+
Object.defineProperty(window, 'parent', { configurable: true, value: host });
|
|
195
|
+
api.loadEmbed.mockResolvedValue(view());
|
|
196
|
+
try {
|
|
197
|
+
mount();
|
|
198
|
+
await screen.findByRole('heading', { name: 'Thing' });
|
|
199
|
+
const last = () =>
|
|
200
|
+
host.postMessage.mock.calls
|
|
201
|
+
.map(([m]) => m as { type?: string; height?: number })
|
|
202
|
+
.filter((m) => m.type === EMBED_HEIGHT_MESSAGE)
|
|
203
|
+
.at(-1)?.height;
|
|
204
|
+
expect(last()).toBe(240);
|
|
205
|
+
expect(onResize).not.toBeNull();
|
|
206
|
+
measured = 640;
|
|
207
|
+
onResize!();
|
|
208
|
+
expect(last()).toBe(640);
|
|
209
|
+
} finally {
|
|
210
|
+
vi.unstubAllGlobals();
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it('posts nothing when it is not framed', async () => {
|
|
215
|
+
const spy = vi.spyOn(window, 'postMessage');
|
|
216
|
+
api.loadEmbed.mockResolvedValue(view());
|
|
217
|
+
mount();
|
|
218
|
+
await screen.findByRole('heading', { name: 'Thing' });
|
|
219
|
+
expect(spy.mock.calls.filter(([m]) => (m as { type?: string })?.type === EMBED_HEIGHT_MESSAGE)).toEqual([]);
|
|
220
|
+
});
|
|
104
221
|
});
|
|
105
222
|
|
|
106
223
|
describe('a view with no usable token', () => {
|
|
@@ -212,22 +329,6 @@ describe('a writer', () => {
|
|
|
212
329
|
* stays open with the draft, Save becomes Send proposal, and the reader is
|
|
213
330
|
* told why — never an editor that quietly stopped saving.
|
|
214
331
|
*/
|
|
215
|
-
/** Heartbeats captured instead of scheduled, so a test can fire one. */
|
|
216
|
-
function captureHeartbeats(): Array<() => void> {
|
|
217
|
-
const ticks: Array<() => void> = [];
|
|
218
|
-
const realSetInterval = window.setInterval.bind(window);
|
|
219
|
-
vi.spyOn(window, 'setInterval').mockImplementation(((fn: () => void, ms?: number) => {
|
|
220
|
-
if (ms === 30_000) {
|
|
221
|
-
ticks.push(fn);
|
|
222
|
-
return 0;
|
|
223
|
-
}
|
|
224
|
-
return realSetInterval(fn, ms);
|
|
225
|
-
}) as typeof window.setInterval);
|
|
226
|
-
return ticks;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
const EDITED = '# Thing\n\nWhat it is, edited.\n';
|
|
230
|
-
|
|
231
332
|
async function editThenLoseWrite() {
|
|
232
333
|
const ticks = captureHeartbeats();
|
|
233
334
|
api.loadEmbed.mockResolvedValueOnce(view());
|
|
@@ -394,6 +495,61 @@ describe('the renderer', () => {
|
|
|
394
495
|
expect(await screen.findByTestId('enterprise-html')).toBeTruthy();
|
|
395
496
|
});
|
|
396
497
|
|
|
498
|
+
/**
|
|
499
|
+
* A renderer that draws the knowledge graph asks the SURFACE for it, and
|
|
500
|
+
* the embed answers from the registry's graph source with its token —
|
|
501
|
+
* the renderer never picks an address, so the same one draws inside an
|
|
502
|
+
* issue panel as in the app. With no source registered the surface
|
|
503
|
+
* offers nothing, and the renderer draws its fallback.
|
|
504
|
+
*/
|
|
505
|
+
it('gives a registered renderer the knowledge graph through the surface, read with the embed token', async () => {
|
|
506
|
+
api.loadEmbed.mockResolvedValue(
|
|
507
|
+
view({ repoRelative: 'Pages/Dash.html', workspacePath: `${KB}/Pages/Dash.html`, content: '<p>dash</p>' }),
|
|
508
|
+
);
|
|
509
|
+
const inEmbed = vi.fn(async () => ({ nodes: { a: {} }, edges: [] }));
|
|
510
|
+
const inApp = vi.fn(async () => ({ nodes: {}, edges: [] }));
|
|
511
|
+
const GraphReader = () => {
|
|
512
|
+
// As a dashboard renderer asks: never an address, only the loader.
|
|
513
|
+
const load = useKbGraphLoader('ws-never-dialled');
|
|
514
|
+
const [state, setState] = useState('no source');
|
|
515
|
+
useEffect(() => {
|
|
516
|
+
if (!load) return;
|
|
517
|
+
void load().then((graph) => setState(`nodes:${Object.keys((graph as { nodes: object }).nodes).length}`));
|
|
518
|
+
}, [load]);
|
|
519
|
+
return <div data-testid="graph-reader">{state}</div>;
|
|
520
|
+
};
|
|
521
|
+
const renderers = [{ extensions: ['.html', '.htm'], Component: GraphReader }];
|
|
522
|
+
mount({ ...EMPTY_REGISTRY, renderers, kbGraphSource: { inApp, inEmbed } } as unknown as AppRegistry);
|
|
523
|
+
await waitFor(() => expect(screen.getByTestId('graph-reader').textContent).toBe('nodes:1'));
|
|
524
|
+
expect(inEmbed).toHaveBeenCalledWith('tok');
|
|
525
|
+
expect(inApp).not.toHaveBeenCalled();
|
|
526
|
+
cleanup();
|
|
527
|
+
|
|
528
|
+
mount({ ...EMPTY_REGISTRY, renderers } as unknown as AppRegistry);
|
|
529
|
+
expect((await screen.findByTestId('graph-reader')).textContent).toBe('no source');
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
it('tells a registered renderer, through the surface, whether the host grows with the content', async () => {
|
|
533
|
+
api.loadEmbed.mockResolvedValue(
|
|
534
|
+
view({ repoRelative: 'Pages/Dash.html', workspacePath: `${KB}/Pages/Dash.html`, content: '<p>dash</p>' }),
|
|
535
|
+
);
|
|
536
|
+
const SizingReader = () => {
|
|
537
|
+
const surface = useRendererSurface();
|
|
538
|
+
return <div data-testid="sizing-reader">{String(surface?.sizesToContent)}</div>;
|
|
539
|
+
};
|
|
540
|
+
const renderers = [{ extensions: ['.html', '.htm'], Component: SizingReader }];
|
|
541
|
+
// The connector's mint put `sizing=content` on the address.
|
|
542
|
+
window.history.replaceState({}, '', '/embed?token=tok&sizing=content');
|
|
543
|
+
mount({ ...EMPTY_REGISTRY, renderers } as unknown as AppRegistry);
|
|
544
|
+
expect((await screen.findByTestId('sizing-reader')).textContent).toBe('true');
|
|
545
|
+
cleanup();
|
|
546
|
+
|
|
547
|
+
// A host with a fixed reading pane asked nothing.
|
|
548
|
+
window.history.replaceState({}, '', '/embed?token=tok');
|
|
549
|
+
mount({ ...EMPTY_REGISTRY, renderers } as unknown as AppRegistry);
|
|
550
|
+
expect((await screen.findByTestId('sizing-reader')).textContent).toBe('false');
|
|
551
|
+
});
|
|
552
|
+
|
|
397
553
|
it('falls back to the built-in renderer for the type when nothing is registered', async () => {
|
|
398
554
|
api.loadEmbed.mockResolvedValue(view());
|
|
399
555
|
mount();
|
|
@@ -471,3 +627,390 @@ describe('the renderer', () => {
|
|
|
471
627
|
}
|
|
472
628
|
});
|
|
473
629
|
});
|
|
630
|
+
|
|
631
|
+
/**
|
|
632
|
+
* The chat view's token lives minutes. When a call is refused for it (401),
|
|
633
|
+
* the view asks its host for a fresh one — `open_page` again, through the
|
|
634
|
+
* `renew` the MCP App view handed over — and runs the refused call once more
|
|
635
|
+
* with it. The reader sees no expired sentence and loses nothing typed.
|
|
636
|
+
*/
|
|
637
|
+
describe('a chat view whose token runs out', () => {
|
|
638
|
+
const expired = () => new FakeEmbedApiError(401, 'Invalid or expired embed token');
|
|
639
|
+
|
|
640
|
+
/** The chat view's handoff: a token, and a way to renew it. */
|
|
641
|
+
function inChat(renew: () => Promise<string | null>) {
|
|
642
|
+
const renewMock = vi.fn(renew);
|
|
643
|
+
configureEmbed({ baseUrl: 'https://hexis.example', token: 'tok', renew: renewMock });
|
|
644
|
+
return renewMock;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
async function editing() {
|
|
648
|
+
await userEvent.click(await screen.findByRole('button', { name: 'Edit' }));
|
|
649
|
+
await screen.findByRole('button', { name: 'Save' });
|
|
650
|
+
fireEvent.change(screen.getByRole('textbox'), { target: { value: EDITED } });
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
it('loads with a renewed token when the first load is refused for it', async () => {
|
|
654
|
+
const renew = inChat(async () => 'tok-2');
|
|
655
|
+
api.loadEmbed.mockImplementation(async (token: string) => {
|
|
656
|
+
if (token === 'tok') throw expired();
|
|
657
|
+
return view();
|
|
658
|
+
});
|
|
659
|
+
mount();
|
|
660
|
+
expect(await screen.findByRole('button', { name: 'Edit' })).toBeTruthy();
|
|
661
|
+
expect(renew).toHaveBeenCalledTimes(1);
|
|
662
|
+
expect(api.loadEmbed.mock.calls.map((c) => c[0])).toEqual(['tok', 'tok-2']);
|
|
663
|
+
expect(screen.queryByText(EMBED_EXPIRED)).toBeNull();
|
|
664
|
+
});
|
|
665
|
+
|
|
666
|
+
it('takes the edit lock with a renewed token when Edit is refused for it', async () => {
|
|
667
|
+
inChat(async () => 'tok-2');
|
|
668
|
+
api.loadEmbed.mockResolvedValue(view());
|
|
669
|
+
api.lockEmbed.mockRejectedValueOnce(expired()).mockResolvedValue({ acquired: true });
|
|
670
|
+
mount();
|
|
671
|
+
await userEvent.click(await screen.findByRole('button', { name: 'Edit' }));
|
|
672
|
+
expect(await screen.findByRole('button', { name: 'Save' })).toBeTruthy();
|
|
673
|
+
expect(api.lockEmbed.mock.calls.map((c) => c[0])).toEqual(['tok', 'tok-2']);
|
|
674
|
+
});
|
|
675
|
+
|
|
676
|
+
it('saves the draft on the retry when Save is refused for the token, and keeps the editor open meanwhile', async () => {
|
|
677
|
+
const renew = inChat(async () => 'tok-2');
|
|
678
|
+
api.loadEmbed.mockResolvedValue(view());
|
|
679
|
+
api.lockEmbed.mockResolvedValue({ acquired: true });
|
|
680
|
+
let release!: () => void;
|
|
681
|
+
const renewed = new Promise<void>((resolve) => (release = resolve));
|
|
682
|
+
renew.mockImplementation(async () => {
|
|
683
|
+
await renewed;
|
|
684
|
+
return 'tok-2';
|
|
685
|
+
});
|
|
686
|
+
api.saveEmbed.mockRejectedValueOnce(expired()).mockResolvedValue(undefined);
|
|
687
|
+
mount();
|
|
688
|
+
await editing();
|
|
689
|
+
await userEvent.click(screen.getByRole('button', { name: 'Save' }));
|
|
690
|
+
|
|
691
|
+
// While the host renews, the editor is still the editor, on the draft.
|
|
692
|
+
await waitFor(() => expect(renew).toHaveBeenCalledTimes(1));
|
|
693
|
+
expect((screen.getByRole('textbox') as HTMLTextAreaElement).value).toBe(EDITED);
|
|
694
|
+
expect(screen.queryByText(EMBED_EXPIRED)).toBeNull();
|
|
695
|
+
release();
|
|
696
|
+
|
|
697
|
+
await waitFor(() => expect(api.saveEmbed).toHaveBeenCalledTimes(2));
|
|
698
|
+
expect(api.saveEmbed.mock.calls).toEqual([
|
|
699
|
+
['tok', EDITED],
|
|
700
|
+
['tok-2', EDITED],
|
|
701
|
+
]);
|
|
702
|
+
expect(screen.queryByText(EMBED_EXPIRED)).toBeNull();
|
|
703
|
+
expect(screen.queryByRole('alert')).toBeNull();
|
|
704
|
+
});
|
|
705
|
+
|
|
706
|
+
it('sends the proposal on the retry when it is refused for the token', async () => {
|
|
707
|
+
inChat(async () => 'tok-2');
|
|
708
|
+
api.loadEmbed.mockResolvedValue(view({ canWrite: false }));
|
|
709
|
+
api.proposeEmbed.mockRejectedValueOnce(expired()).mockResolvedValue({ url: '/change-requests/7' });
|
|
710
|
+
mount();
|
|
711
|
+
await userEvent.click(await screen.findByRole('button', { name: 'Propose changes' }));
|
|
712
|
+
fireEvent.change(screen.getByRole('textbox'), { target: { value: EDITED } });
|
|
713
|
+
await userEvent.click(screen.getByRole('button', { name: 'Send proposal' }));
|
|
714
|
+
expect(await screen.findByText(/sent for approval/i)).toBeTruthy();
|
|
715
|
+
expect(api.proposeEmbed.mock.calls).toEqual([
|
|
716
|
+
['tok', EDITED],
|
|
717
|
+
['tok-2', EDITED],
|
|
718
|
+
]);
|
|
719
|
+
});
|
|
720
|
+
|
|
721
|
+
it('keeps the lock alive with a renewed token when a heartbeat is refused for it', async () => {
|
|
722
|
+
const ticks = captureHeartbeats();
|
|
723
|
+
inChat(async () => 'tok-2');
|
|
724
|
+
api.loadEmbed.mockResolvedValue(view());
|
|
725
|
+
api.lockEmbed.mockResolvedValue({ acquired: true });
|
|
726
|
+
api.heartbeatEmbed.mockRejectedValueOnce(expired()).mockResolvedValue(undefined);
|
|
727
|
+
mount();
|
|
728
|
+
await editing();
|
|
729
|
+
ticks.at(-1)!();
|
|
730
|
+
await waitFor(() => expect(api.heartbeatEmbed).toHaveBeenCalledTimes(2));
|
|
731
|
+
expect(api.heartbeatEmbed.mock.calls.map((c) => c[0])).toEqual(['tok', 'tok-2']);
|
|
732
|
+
// The next one goes out with the fresh token, without asking again.
|
|
733
|
+
ticks.at(-1)!();
|
|
734
|
+
await waitFor(() => expect(api.heartbeatEmbed).toHaveBeenCalledTimes(3));
|
|
735
|
+
expect(api.heartbeatEmbed.mock.calls[2][0]).toBe('tok-2');
|
|
736
|
+
expect((screen.getByRole('textbox') as HTMLTextAreaElement).value).toBe(EDITED);
|
|
737
|
+
expect(screen.queryByText(EMBED_EXPIRED)).toBeNull();
|
|
738
|
+
});
|
|
739
|
+
|
|
740
|
+
/**
|
|
741
|
+
* A file whose renderer fetches its own bytes — an email, through the
|
|
742
|
+
* app's own email renderer, which reads them off the surface the view
|
|
743
|
+
* mounts it on. Refused for the token, the bytes are fetched again under
|
|
744
|
+
* the renewed one, and the message shows.
|
|
745
|
+
*/
|
|
746
|
+
const EML_BYTES = [
|
|
747
|
+
'From: Ada Lovelace <ada@example.com>',
|
|
748
|
+
'To: bob@example.com',
|
|
749
|
+
'Subject: Quarterly numbers',
|
|
750
|
+
'MIME-Version: 1.0',
|
|
751
|
+
'Content-Type: text/plain; charset=utf-8',
|
|
752
|
+
'',
|
|
753
|
+
'Please see attached.',
|
|
754
|
+
'',
|
|
755
|
+
].join('\r\n');
|
|
756
|
+
const emailView = () =>
|
|
757
|
+
view({
|
|
758
|
+
nodeName: 'Numbers',
|
|
759
|
+
repoRelative: 'Mail/Numbers.eml',
|
|
760
|
+
workspacePath: `${KB}/Mail/Numbers.eml`,
|
|
761
|
+
content: '',
|
|
762
|
+
contentIsText: false,
|
|
763
|
+
});
|
|
764
|
+
|
|
765
|
+
it('fetches a renderer\'s bytes again with a renewed token, and the file shows', async () => {
|
|
766
|
+
const renew = inChat(async () => 'tok-2');
|
|
767
|
+
api.loadEmbed.mockResolvedValue(emailView());
|
|
768
|
+
const fetchMock = vi.fn(async (url: string) =>
|
|
769
|
+
/[?&]token=tok(&|$)/.test(url) ? new Response('', { status: 401 }) : new Response(EML_BYTES, { status: 200 }),
|
|
770
|
+
);
|
|
771
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
772
|
+
try {
|
|
773
|
+
mount();
|
|
774
|
+
// The email renderer and its parser are lazy-loaded: longer than the
|
|
775
|
+
// default wait under a full suite's load.
|
|
776
|
+
expect(await screen.findByText('Quarterly numbers', {}, { timeout: 5_000 })).toBeTruthy();
|
|
777
|
+
expect(renew).toHaveBeenCalledTimes(1);
|
|
778
|
+
// Refused once under the old token; the retry, and the renderer's own
|
|
779
|
+
// re-read once the surface carries the fresh token, go under the new.
|
|
780
|
+
const urls = fetchMock.mock.calls.map((c) => c[0] as string);
|
|
781
|
+
expect(urls[0]).toMatch(/[?&]token=tok(&|$)/);
|
|
782
|
+
expect(urls.length).toBeGreaterThanOrEqual(2);
|
|
783
|
+
for (const url of urls.slice(1)) expect(url).toMatch(/[?&]token=tok-2(&|$)/);
|
|
784
|
+
expect(screen.queryByText(EMBED_EXPIRED)).toBeNull();
|
|
785
|
+
} finally {
|
|
786
|
+
vi.unstubAllGlobals();
|
|
787
|
+
}
|
|
788
|
+
});
|
|
789
|
+
|
|
790
|
+
it('shows the expired sentence when a renderer\'s bytes are refused and no fresh token can be had', async () => {
|
|
791
|
+
const renew = inChat(async () => null);
|
|
792
|
+
api.loadEmbed.mockResolvedValue(emailView());
|
|
793
|
+
const fetchMock = vi.fn(async () => new Response('', { status: 401 }));
|
|
794
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
795
|
+
try {
|
|
796
|
+
mount();
|
|
797
|
+
expect(await screen.findByText(EMBED_EXPIRED, {}, { timeout: 5_000 })).toBeTruthy();
|
|
798
|
+
expect(renew).toHaveBeenCalledTimes(1);
|
|
799
|
+
// Once: the refusal after a failed renewal is the end, never a loop.
|
|
800
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
801
|
+
expect(screen.queryByText('Quarterly numbers')).toBeNull();
|
|
802
|
+
} finally {
|
|
803
|
+
vi.unstubAllGlobals();
|
|
804
|
+
}
|
|
805
|
+
});
|
|
806
|
+
|
|
807
|
+
/**
|
|
808
|
+
* A picture the browser loads itself (`<img src>`) never reaches
|
|
809
|
+
* `withEmbedToken`; one that fails is asked once more to learn why, and a
|
|
810
|
+
* refusal for the token renews it and gives the picture the fresh address.
|
|
811
|
+
*/
|
|
812
|
+
it('renews when a picture is refused for the token, and loads it again under the fresh one', async () => {
|
|
813
|
+
const renew = inChat(async () => 'tok-2');
|
|
814
|
+
api.embedRawUrl.mockImplementation(
|
|
815
|
+
(token: string, path?: string) =>
|
|
816
|
+
`https://hexis.example/api/embed/raw?token=${token}${path ? `&path=${path}` : ''}`,
|
|
817
|
+
);
|
|
818
|
+
api.loadEmbed.mockResolvedValue(view({ content: '# Thing\n\n\n' }));
|
|
819
|
+
const fetchMock = vi.fn(async () => new Response('', { status: 401 }));
|
|
820
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
821
|
+
try {
|
|
822
|
+
mount();
|
|
823
|
+
const img = await screen.findByRole('img', { name: 'shot' });
|
|
824
|
+
fireEvent.error(img);
|
|
825
|
+
await waitFor(() =>
|
|
826
|
+
expect(screen.getByRole('img', { name: 'shot' }).getAttribute('src')).toBe(
|
|
827
|
+
'https://hexis.example/api/embed/raw?token=tok-2&path=/Data/shot.png',
|
|
828
|
+
),
|
|
829
|
+
);
|
|
830
|
+
expect(renew).toHaveBeenCalledTimes(1);
|
|
831
|
+
// Refused again under the fresh token: left broken, never another renewal.
|
|
832
|
+
fireEvent.error(screen.getByRole('img', { name: 'shot' }));
|
|
833
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
834
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
835
|
+
expect(renew).toHaveBeenCalledTimes(1);
|
|
836
|
+
expect(screen.queryByText(EMBED_EXPIRED)).toBeNull();
|
|
837
|
+
} finally {
|
|
838
|
+
vi.unstubAllGlobals();
|
|
839
|
+
}
|
|
840
|
+
});
|
|
841
|
+
|
|
842
|
+
it('shows the expired sentence when a picture is refused for the token and no renewal can be had', async () => {
|
|
843
|
+
const renew = inChat(async () => null);
|
|
844
|
+
api.embedRawUrl.mockImplementation(
|
|
845
|
+
(token: string, path?: string) =>
|
|
846
|
+
`https://hexis.example/api/embed/raw?token=${token}${path ? `&path=${path}` : ''}`,
|
|
847
|
+
);
|
|
848
|
+
api.loadEmbed.mockResolvedValue(view({ content: '# Thing\n\n\n' }));
|
|
849
|
+
vi.stubGlobal(
|
|
850
|
+
'fetch',
|
|
851
|
+
vi.fn(async () => new Response('', { status: 401 })),
|
|
852
|
+
);
|
|
853
|
+
try {
|
|
854
|
+
mount();
|
|
855
|
+
fireEvent.error(await screen.findByRole('img', { name: 'shot' }));
|
|
856
|
+
expect(await screen.findByText(EMBED_EXPIRED)).toBeTruthy();
|
|
857
|
+
expect(renew).toHaveBeenCalledTimes(1);
|
|
858
|
+
} finally {
|
|
859
|
+
vi.unstubAllGlobals();
|
|
860
|
+
}
|
|
861
|
+
});
|
|
862
|
+
|
|
863
|
+
it('leaves a picture refused on the SPA page alone: no probe, no renewal, no expired sentence', async () => {
|
|
864
|
+
// The SPA `/embed` page: a token in the address and no way to renew.
|
|
865
|
+
window.history.replaceState(null, '', '/embed?token=tok');
|
|
866
|
+
api.embedRawUrl.mockImplementation(
|
|
867
|
+
(token: string, path?: string) =>
|
|
868
|
+
`${window.location.origin}/api/embed/raw?token=${token}${path ? `&path=${path}` : ''}`,
|
|
869
|
+
);
|
|
870
|
+
api.loadEmbed.mockResolvedValue(view({ content: '# Thing\n\n\n' }));
|
|
871
|
+
const fetchMock = vi.fn(async () => new Response('', { status: 401 }));
|
|
872
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
873
|
+
try {
|
|
874
|
+
mount();
|
|
875
|
+
fireEvent.error(await screen.findByRole('img', { name: 'shot' }));
|
|
876
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
877
|
+
expect(fetchMock).not.toHaveBeenCalled();
|
|
878
|
+
expect(screen.queryByText(EMBED_EXPIRED)).toBeNull();
|
|
879
|
+
} finally {
|
|
880
|
+
vi.unstubAllGlobals();
|
|
881
|
+
window.history.replaceState(null, '', '/');
|
|
882
|
+
}
|
|
883
|
+
});
|
|
884
|
+
|
|
885
|
+
it('asks for no renewal when a picture fails for a reason that is not the token', async () => {
|
|
886
|
+
const renew = inChat(async () => 'tok-2');
|
|
887
|
+
api.embedRawUrl.mockImplementation(
|
|
888
|
+
(token: string, path?: string) =>
|
|
889
|
+
`https://hexis.example/api/embed/raw?token=${token}${path ? `&path=${path}` : ''}`,
|
|
890
|
+
);
|
|
891
|
+
api.loadEmbed.mockResolvedValue(view({ content: '# Thing\n\n\n' }));
|
|
892
|
+
const fetchMock = vi.fn(async () => new Response('', { status: 404 }));
|
|
893
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
894
|
+
try {
|
|
895
|
+
mount();
|
|
896
|
+
fireEvent.error(await screen.findByRole('img', { name: 'shot' }));
|
|
897
|
+
await waitFor(() => expect(fetchMock).toHaveBeenCalledTimes(1));
|
|
898
|
+
await Promise.resolve();
|
|
899
|
+
expect(renew).not.toHaveBeenCalled();
|
|
900
|
+
} finally {
|
|
901
|
+
vi.unstubAllGlobals();
|
|
902
|
+
}
|
|
903
|
+
});
|
|
904
|
+
|
|
905
|
+
/**
|
|
906
|
+
* No renewal to be had — the host does not run tools for a view, the call
|
|
907
|
+
* failed, or `open_page` refused (read access withdrawn): the expired
|
|
908
|
+
* sentence as before, with the draft kept on screen above it to copy.
|
|
909
|
+
*/
|
|
910
|
+
it('shows the expired sentence with the draft kept above it when no renewal can be had', async () => {
|
|
911
|
+
const renew = inChat(async () => null);
|
|
912
|
+
api.loadEmbed.mockResolvedValue(view());
|
|
913
|
+
api.lockEmbed.mockResolvedValue({ acquired: true });
|
|
914
|
+
api.saveEmbed.mockRejectedValue(expired());
|
|
915
|
+
mount();
|
|
916
|
+
await editing();
|
|
917
|
+
await userEvent.click(screen.getByRole('button', { name: 'Save' }));
|
|
918
|
+
expect(await screen.findByText(EMBED_EXPIRED)).toBeTruthy();
|
|
919
|
+
expect(renew).toHaveBeenCalledTimes(1);
|
|
920
|
+
expect(api.saveEmbed).toHaveBeenCalledTimes(1);
|
|
921
|
+
const draft = screen.getByRole('textbox', { name: 'Your draft' }) as HTMLTextAreaElement;
|
|
922
|
+
expect(draft.value).toBe(EDITED);
|
|
923
|
+
expect(draft.readOnly).toBe(true);
|
|
924
|
+
// Nothing can be sent from it any more, and the page itself is gone.
|
|
925
|
+
expect(screen.queryByRole('button', { name: 'Save' })).toBeNull();
|
|
926
|
+
expect(screen.queryByRole('heading', { name: 'Thing' })).toBeNull();
|
|
927
|
+
});
|
|
928
|
+
|
|
929
|
+
/**
|
|
930
|
+
* Once the view has expired with an editor open, nothing keeps beating:
|
|
931
|
+
* a heartbeat would be refused, ask the host for another token, and do so
|
|
932
|
+
* again every beat — a renewal loop by another name.
|
|
933
|
+
*/
|
|
934
|
+
it('stops the heartbeat once a refused heartbeat could not be renewed, so it never asks again', async () => {
|
|
935
|
+
const ticks = captureHeartbeats();
|
|
936
|
+
const clear = vi.spyOn(window, 'clearInterval');
|
|
937
|
+
const renew = inChat(async () => null);
|
|
938
|
+
api.loadEmbed.mockResolvedValue(view());
|
|
939
|
+
api.lockEmbed.mockResolvedValue({ acquired: true });
|
|
940
|
+
api.heartbeatEmbed.mockRejectedValue(expired());
|
|
941
|
+
mount();
|
|
942
|
+
await editing();
|
|
943
|
+
const beats = ticks.length;
|
|
944
|
+
ticks.at(-1)!();
|
|
945
|
+
expect(await screen.findByText(EMBED_EXPIRED)).toBeTruthy();
|
|
946
|
+
expect(renew).toHaveBeenCalledTimes(1);
|
|
947
|
+
// The interval was taken down and no new one was set up.
|
|
948
|
+
expect(clear).toHaveBeenCalled();
|
|
949
|
+
expect(ticks).toHaveLength(beats);
|
|
950
|
+
// Coming back to the tab does not take the lock again either.
|
|
951
|
+
Object.defineProperty(document, 'visibilityState', { configurable: true, value: 'visible' });
|
|
952
|
+
document.dispatchEvent(new Event('visibilitychange'));
|
|
953
|
+
expect(api.lockEmbed).toHaveBeenCalledTimes(1);
|
|
954
|
+
expect(renew).toHaveBeenCalledTimes(1);
|
|
955
|
+
expect((screen.getByRole('textbox', { name: 'Your draft' }) as HTMLTextAreaElement).value).toBe(EDITED);
|
|
956
|
+
});
|
|
957
|
+
|
|
958
|
+
it('shows the expired sentence when the renewal itself fails', async () => {
|
|
959
|
+
inChat(async () => {
|
|
960
|
+
throw new Error('the chat app did not answer');
|
|
961
|
+
});
|
|
962
|
+
api.loadEmbed.mockRejectedValue(expired());
|
|
963
|
+
mount();
|
|
964
|
+
expect(await screen.findByText(EMBED_EXPIRED)).toBeTruthy();
|
|
965
|
+
expect(screen.queryByRole('textbox')).toBeNull();
|
|
966
|
+
});
|
|
967
|
+
|
|
968
|
+
/** At most one renewal per refused call: a second refusal ends it, never a loop. */
|
|
969
|
+
it('renews once per refused call, and a second refusal in a row shows the expired sentence', async () => {
|
|
970
|
+
const renew = inChat(async () => 'tok-2');
|
|
971
|
+
api.loadEmbed.mockResolvedValue(view());
|
|
972
|
+
api.lockEmbed.mockResolvedValue({ acquired: true });
|
|
973
|
+
api.saveEmbed.mockRejectedValue(expired());
|
|
974
|
+
mount();
|
|
975
|
+
await editing();
|
|
976
|
+
await userEvent.click(screen.getByRole('button', { name: 'Save' }));
|
|
977
|
+
expect(await screen.findByText(EMBED_EXPIRED)).toBeTruthy();
|
|
978
|
+
expect(renew).toHaveBeenCalledTimes(1);
|
|
979
|
+
expect(api.saveEmbed.mock.calls.map((c) => c[0])).toEqual(['tok', 'tok-2']);
|
|
980
|
+
expect((screen.getByRole('textbox', { name: 'Your draft' }) as HTMLTextAreaElement).value).toBe(EDITED);
|
|
981
|
+
});
|
|
982
|
+
|
|
983
|
+
it('shares one renewal between calls refused together', async () => {
|
|
984
|
+
let release!: (token: string) => void;
|
|
985
|
+
const renew = inChat(() => new Promise<string>((resolve) => (release = resolve)));
|
|
986
|
+
const ticks = captureHeartbeats();
|
|
987
|
+
api.loadEmbed.mockResolvedValue(view());
|
|
988
|
+
api.lockEmbed.mockResolvedValue({ acquired: true });
|
|
989
|
+
api.heartbeatEmbed.mockRejectedValueOnce(expired()).mockResolvedValue(undefined);
|
|
990
|
+
api.saveEmbed.mockRejectedValueOnce(expired()).mockResolvedValue(undefined);
|
|
991
|
+
mount();
|
|
992
|
+
await editing();
|
|
993
|
+
ticks.at(-1)!();
|
|
994
|
+
await userEvent.click(screen.getByRole('button', { name: 'Save' }));
|
|
995
|
+
await waitFor(() => expect(api.saveEmbed).toHaveBeenCalledTimes(1));
|
|
996
|
+
release('tok-2');
|
|
997
|
+
await waitFor(() => expect(api.saveEmbed).toHaveBeenCalledTimes(2));
|
|
998
|
+
expect(renew).toHaveBeenCalledTimes(1);
|
|
999
|
+
expect(api.saveEmbed.mock.calls[1]).toEqual(['tok-2', EDITED]);
|
|
1000
|
+
expect(api.heartbeatEmbed.mock.calls.map((c) => c[0])).toEqual(['tok', 'tok-2']);
|
|
1001
|
+
});
|
|
1002
|
+
});
|
|
1003
|
+
|
|
1004
|
+
/**
|
|
1005
|
+
* The SPA's `/embed` page, which an Atlassian panel frames, has no host to
|
|
1006
|
+
* ask: a refused token there is the expired sentence, as it always was.
|
|
1007
|
+
*/
|
|
1008
|
+
describe('the SPA embed page', () => {
|
|
1009
|
+
it('asks nobody for a token and shows the expired sentence when a call is refused for it', async () => {
|
|
1010
|
+
api.loadEmbed.mockRejectedValue(new FakeEmbedApiError(401, 'Invalid or expired embed token'));
|
|
1011
|
+
mount();
|
|
1012
|
+
expect(await screen.findByText(EMBED_EXPIRED)).toBeTruthy();
|
|
1013
|
+
expect(api.loadEmbed).toHaveBeenCalledTimes(1);
|
|
1014
|
+
expect(api.loadEmbed).toHaveBeenCalledWith('tok');
|
|
1015
|
+
});
|
|
1016
|
+
});
|