@bevel-software/platform-core-frontend 0.25.2 → 0.27.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 +87 -25
- package/src/core/ProductName.tsx +49 -0
- package/src/core/__tests__/ShellRoutes.test.tsx +22 -0
- package/src/core/__tests__/core-contributions.test.ts +16 -0
- package/src/core/core-contributions.ts +17 -0
- package/src/core/registry.ts +51 -1
- package/src/index.css +23 -5
- package/src/index.ts +5 -0
- package/src/modules/access/__tests__/ManageAccessDialog.proposal.test.tsx +1 -1
- package/src/modules/access/components/ManageAccessDialog.tsx +1 -1
- package/src/modules/admin/__tests__/RolesCorruptedBanner.test.tsx +70 -0
- package/src/modules/admin/components/RolesCorruptedBanner.tsx +9 -4
- package/src/modules/auth/components/LoginScreen.tsx +4 -1
- package/src/modules/auth/services/account.api.ts +28 -1
- package/src/modules/auth/services/sso.ts +1 -2
- package/src/modules/change-requests/__tests__/ChangeRequestDialog.sync-banner.test.tsx +176 -0
- package/src/modules/change-requests/__tests__/ChangeRequestDialog.unreadable.test.tsx +68 -0
- package/src/modules/change-requests/__tests__/ChangeRequestLink.test.tsx +182 -0
- package/src/modules/change-requests/__tests__/FileChangeBoxes.test.tsx +1 -1
- package/src/modules/change-requests/__tests__/readFileOnBranch.test.ts +79 -0
- package/src/modules/change-requests/components/ChangeBox.tsx +6 -1
- package/src/modules/change-requests/components/ChangeRequestDialog.tsx +45 -15
- package/src/modules/change-requests/components/ChangeRequestLink.tsx +151 -0
- package/src/modules/change-requests/components/CrFileTree.tsx +2 -1
- package/src/modules/change-requests/components/FileChangeBoxes.tsx +4 -2
- package/src/modules/change-requests/hooks/useApplyChangeRequest.ts +6 -3
- package/src/modules/change-requests/services/change-requests.api.ts +15 -2
- package/src/modules/change-requests/utils/readFailure.ts +22 -0
- package/src/modules/embed/__tests__/EmbedLinkPage.test.tsx +74 -0
- package/src/modules/embed/__tests__/EmbedView.test.tsx +473 -0
- package/src/modules/embed/__tests__/embed-config.test.ts +128 -0
- package/src/modules/embed/__tests__/embed-host.test.ts +111 -0
- package/src/modules/embed/__tests__/mcp-app-view.test.ts +295 -0
- package/src/modules/embed/__tests__/mount.test.tsx +157 -0
- package/src/modules/embed/components/EmbedLinkPage.tsx +109 -0
- package/src/modules/embed/components/EmbedView.tsx +545 -0
- package/src/modules/embed/embed-config.ts +132 -0
- package/src/modules/embed/embed-host.ts +120 -0
- package/src/modules/embed/mount.tsx +80 -0
- package/src/modules/embed/services/embed-token.ts +22 -0
- package/src/modules/embed/services/embed.api.ts +158 -0
- package/src/modules/git/__tests__/BranchSwitcher.test.tsx +202 -4
- package/src/modules/git/__tests__/GitSyncFailedBanner.test.tsx +59 -1
- package/src/modules/git/__tests__/PullNeededBanner.test.tsx +39 -22
- package/src/modules/git/__tests__/useAutoPullUpdates.test.tsx +7 -2
- package/src/modules/git/branch-delete-menu-item.tsx +24 -0
- package/src/modules/git/components/BranchSwitcher.tsx +29 -5
- package/src/modules/git/components/GitSyncFailedBanner.tsx +53 -15
- package/src/modules/git/components/HistoryVersionPreview.tsx +5 -3
- package/src/modules/git/components/PullNeededBanner.tsx +15 -49
- package/src/modules/git/hooks/useAutoPullUpdates.ts +9 -5
- package/src/modules/git/index.ts +15 -0
- package/src/modules/git/services/__tests__/error-messages.test.ts +67 -0
- package/src/modules/git/services/__tests__/pull-failure.test.ts +36 -0
- package/src/modules/git/services/error-messages.ts +58 -1
- package/src/modules/git/services/pull-failure.ts +68 -0
- package/src/modules/git/state/branch-delete-confirm.ts +46 -0
- package/src/modules/layout/__tests__/HeaderAlignment.test.tsx +1 -0
- package/src/modules/layout/__tests__/breakpoints.test.ts +7 -1
- package/src/modules/layout/breakpoints.ts +8 -0
- package/src/modules/library/__tests__/PluginExtras.test.tsx +9 -9
- package/src/modules/library/__tests__/PluginPage.test.tsx +8 -1
- package/src/modules/library/__tests__/SkillsTree.test.tsx +13 -4
- package/src/modules/library/__tests__/ToolPage.test.tsx +53 -0
- package/src/modules/library/components/LibraryPage.tsx +22 -0
- package/src/modules/library/components/PluginExtras.tsx +23 -6
- package/src/modules/library/components/PluginPage.tsx +6 -1
- package/src/modules/library/components/tool-page/ToolPage.tsx +45 -1
- package/src/modules/library/routes/LibraryRoutes.tsx +8 -7
- package/src/modules/library/services/plugins.api.ts +4 -1
- package/src/modules/library/services/tools.api.ts +27 -0
- package/src/modules/library/state/plugins-revision.ts +28 -0
- package/src/modules/onboarding/__tests__/agent-connection-api.test.ts +55 -0
- package/src/modules/onboarding/__tests__/creator-welcome.test.tsx +87 -82
- package/src/modules/onboarding/__tests__/first-page-prompt.test.ts +109 -0
- package/src/modules/onboarding/__tests__/get-started-column.test.tsx +1001 -0
- package/src/modules/onboarding/__tests__/invite-dialog.test.tsx +392 -0
- package/src/modules/onboarding/__tests__/onboarding.test.tsx +226 -193
- package/src/modules/onboarding/__tests__/starter-pack-card.test.tsx +136 -0
- package/src/modules/onboarding/components/CreatorWelcomePage.tsx +7 -11
- package/src/modules/onboarding/components/EmailChipsInput.tsx +138 -0
- package/src/modules/onboarding/components/GetStartedColumn.tsx +651 -0
- package/src/modules/onboarding/components/InviteButton.tsx +35 -0
- package/src/modules/onboarding/components/InviteDialog.tsx +424 -0
- package/src/modules/onboarding/components/RootLanding.tsx +8 -24
- package/src/modules/onboarding/components/StarterPackCard.tsx +98 -0
- package/src/modules/onboarding/components/WelcomePage.tsx +65 -154
- package/src/modules/onboarding/first-page-prompt.ts +89 -0
- package/src/modules/onboarding/invite-emails.ts +175 -0
- package/src/modules/onboarding/services/agent-connection.api.ts +53 -0
- package/src/modules/onboarding/services/starter-packs.api.ts +83 -0
- package/src/modules/onboarding/state/agent-connection.ts +167 -0
- package/src/modules/onboarding/state/invite-dialog.context.ts +32 -0
- package/src/modules/onboarding/state/invite-dialog.tsx +31 -0
- package/src/modules/onboarding/state/onboarding.ts +133 -59
- package/src/modules/onboarding/state/starter-packs.ts +169 -0
- package/src/modules/settings/settings-nav-items.tsx +8 -3
- package/src/modules/setup/__tests__/setup.first-run-storage.test.tsx +389 -0
- package/src/modules/setup/__tests__/setup.github.test.tsx +5 -5
- package/src/modules/setup/__tests__/setup.kept-draft.test.tsx +1 -1
- package/src/modules/setup/__tests__/setup.repository-change.test.tsx +1 -1
- package/src/modules/setup/__tests__/setup.repository.test.tsx +21 -21
- package/src/modules/setup/__tests__/setup.test.tsx +65 -81
- package/src/modules/setup/__tests__/useConnectionProbe.test.tsx +123 -0
- package/src/modules/setup/components/FirstRunStorage.tsx +560 -0
- package/src/modules/setup/components/SetupGate.tsx +46 -1
- package/src/modules/setup/components/SetupScreen.tsx +92 -152
- package/src/modules/setup/hooks/useConnectionProbe.ts +93 -0
- package/src/modules/setup/services/setup.api.ts +3 -3
- package/src/modules/setup/utils/suggested-branch.ts +27 -0
- package/src/modules/toolbar/commands/__tests__/actions.test.tsx +210 -0
- package/src/modules/toolbar/commands/actions.tsx +324 -0
- package/src/modules/toolbar/commands/command-menu.ts +44 -0
- package/src/modules/toolbar/commands/useCommandActions.ts +69 -0
- package/src/modules/toolbar/commands/useCommandShortcuts.ts +121 -0
- package/src/modules/toolbar/components/AppSwitcher.tsx +106 -22
- package/src/modules/toolbar/components/ExternalAgentAccessPage.tsx +1 -1
- package/src/modules/toolbar/components/ProfileMenu.tsx +9 -4
- package/src/modules/toolbar/components/SearchPalette.tsx +540 -0
- package/src/modules/toolbar/components/Toolbar.tsx +10 -0
- package/src/modules/toolbar/components/__tests__/AppSwitcher.test.tsx +187 -54
- package/src/modules/toolbar/components/__tests__/ExternalAgentAccessPage.test.tsx +4 -4
- package/src/modules/toolbar/components/__tests__/ProfileMenu.test.tsx +64 -0
- package/src/modules/toolbar/components/__tests__/SearchPalette.test.tsx +832 -0
- package/src/modules/toolbar/components/__tests__/Toolbar.test.tsx +57 -2
- package/src/modules/toolbar/search/__tests__/rank.test.ts +93 -0
- package/src/modules/toolbar/search/__tests__/sources.test.ts +131 -0
- package/src/modules/toolbar/search/rank.ts +111 -0
- package/src/modules/toolbar/search/sources.ts +169 -0
- package/src/modules/workflow/hooks/useFileLock.ts +10 -7
- package/src/modules/workflow/services/__tests__/lock.api.test.ts +41 -0
- package/src/modules/workflow/services/lock.api.ts +17 -2
- package/src/modules/workspace/__tests__/useWorkspaceState.test.ts +49 -1
- package/src/modules/workspace/components/EditorTabs.tsx +39 -15
- package/src/modules/workspace/components/FileExplorer.tsx +39 -6
- package/src/modules/workspace/components/FilePaneCard.tsx +6 -2
- package/src/modules/workspace/components/FileRoute.tsx +12 -7
- package/src/modules/workspace/components/FileViewer.tsx +271 -74
- package/src/modules/workspace/components/KbPageHeader.tsx +13 -6
- package/src/modules/workspace/components/__tests__/EditorTabs.test.tsx +88 -21
- package/src/modules/workspace/components/__tests__/FileExplorer.test.tsx +101 -11
- package/src/modules/workspace/components/__tests__/FileRoute.test.tsx +23 -3
- package/src/modules/workspace/components/__tests__/FileViewer.test.tsx +330 -6
- package/src/modules/workspace/components/__tests__/KbPageHeader.test.tsx +17 -2
- package/src/modules/workspace/components/renderers/DocxRenderer.tsx +11 -16
- package/src/modules/workspace/components/renderers/DownloadFileButton.tsx +6 -0
- package/src/modules/workspace/components/renderers/EmailRenderer.tsx +23 -21
- package/src/modules/workspace/components/renderers/ImageRenderer.tsx +13 -29
- package/src/modules/workspace/components/renderers/KbMarkdownView.tsx +9 -2
- package/src/modules/workspace/components/renderers/MarkdownRenderer.tsx +8 -0
- package/src/modules/workspace/components/renderers/PdfRenderer.tsx +14 -19
- package/src/modules/workspace/components/renderers/PptxRenderer.tsx +11 -17
- package/src/modules/workspace/components/renderers/ToolRenderer.tsx +35 -7
- package/src/modules/workspace/components/renderers/XlsxRenderer.tsx +11 -17
- package/src/modules/workspace/components/renderers/__tests__/EmailRenderer.links.test.tsx +73 -0
- package/src/modules/workspace/components/renderers/__tests__/ToolRenderer.scaffold.test.tsx +64 -0
- package/src/modules/workspace/components/renderers/__tests__/ToolRenderer.surface.test.tsx +82 -0
- package/src/modules/workspace/components/renderers/__tests__/bridgeHarness.ts +99 -0
- package/src/modules/workspace/components/renderers/__tests__/htmlSandbox.test.ts +129 -35
- package/src/modules/workspace/components/renderers/__tests__/htmlViewsGuide.test.ts +149 -0
- package/src/modules/workspace/components/renderers/__tests__/kbMarkdownLinkPolicy.test.tsx +100 -0
- package/src/modules/workspace/components/renderers/htmlSandbox.ts +76 -20
- package/src/modules/workspace/components/renderers/index.ts +26 -0
- package/src/modules/workspace/components/renderers/kbMarkdownPipeline.tsx +51 -1
- package/src/modules/workspace/components/renderers/rendererRawRead.ts +53 -0
- package/src/modules/workspace/components/renderers/rendererSurface.ts +109 -0
- package/src/modules/workspace/components/renderers/rendererWorkspace.ts +8 -2
- package/src/modules/workspace/hooks/__tests__/useWorkspaceState.test.tsx +126 -21
- package/src/modules/workspace/hooks/useCreatePage.ts +116 -0
- package/src/modules/workspace/hooks/useWorkspaceImageResolver.ts +24 -7
- package/src/modules/workspace/hooks/useWorkspaceState.ts +42 -8
- package/src/modules/workspace/routing/__tests__/kb-routes.test.tsx +10 -0
- package/src/modules/workspace/routing/__tests__/md-links-parity.test.tsx +113 -0
- package/src/modules/workspace/routing/kb-routes.ts +66 -12
- package/src/modules/workspace/state/editable-page.ts +42 -0
- package/src/modules/workspace/state/workspace.context.ts +13 -4
- package/src/modules/workspace/utils/__tests__/display-file-name.test.ts +60 -0
- package/src/modules/workspace/utils/__tests__/fileTree.test.ts +75 -0
- package/src/modules/workspace/utils/__tests__/treeConfirm.test.ts +26 -14
- package/src/modules/workspace/utils/display-file-name.ts +102 -0
- package/src/modules/workspace/utils/fileTree.ts +49 -14
- package/src/modules/workspace/utils/treeConfirm.ts +22 -5
- package/src/shared/__tests__/no-builtin-confirm.test.ts +102 -0
- package/src/shared/components/ConfirmProvider.tsx +156 -0
- package/src/shared/components/Dialog.tsx +7 -0
- package/src/shared/components/__tests__/ConfirmProvider.test.tsx +198 -0
- package/src/shared/components/confirm-context.ts +94 -0
- package/src/shared/components/index.ts +3 -0
- package/src/shared/mcp/__tests__/connect-snippets.test.ts +2 -2
- package/src/shared/mcp/connect-snippets.ts +8 -5
- package/src/modules/onboarding/components/WelcomeRoute.tsx +0 -57
- package/src/modules/onboarding/welcome-state.ts +0 -36
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { render, screen } from '@testing-library/react';
|
|
3
|
+
import { ToolRenderer } from '../ToolRenderer';
|
|
4
|
+
import { RendererSurfaceContext, type RendererSurface } from '../rendererSurface';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The `.tool` renderer's right-hand panels — the Secrets Vault palette, the
|
|
8
|
+
* tool's own secrets and the live Preview — act under the signed-in session.
|
|
9
|
+
* On a renderer surface (the embed, in a chat or an Atlassian panel) there is
|
|
10
|
+
* no session, only the embed token, which none of those routes take: the
|
|
11
|
+
* panels are not drawn and their requests are not made. In the app nothing
|
|
12
|
+
* changes.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const TOOL = ['---', 'id: my_tool', 'type: http', 'remote: true', 'url: https://api.example.com/utcp', '---', ''].join('\n');
|
|
16
|
+
|
|
17
|
+
const SURFACE: RendererSurface = {
|
|
18
|
+
kbDirName: 'knowledge-base',
|
|
19
|
+
openLink: () => undefined,
|
|
20
|
+
openWorkspacePath: () => undefined,
|
|
21
|
+
openNodeId: () => undefined,
|
|
22
|
+
canonicalUrlFor: () => null,
|
|
23
|
+
rawUrl: () => '',
|
|
24
|
+
rawFetch: async () => new Response(),
|
|
25
|
+
offersDownload: false,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
let fetchMock: ReturnType<typeof vi.fn>;
|
|
29
|
+
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
fetchMock = vi.fn(async () => ({ ok: true, status: 200, json: async () => ({ secrets: [], tools: [] }) }));
|
|
32
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
afterEach(() => {
|
|
36
|
+
vi.unstubAllGlobals();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const settle = () => new Promise((r) => setTimeout(r, 0));
|
|
40
|
+
|
|
41
|
+
describe('the .tool renderer on a renderer surface', () => {
|
|
42
|
+
it('draws no session-backed panel and makes no session call', async () => {
|
|
43
|
+
const { container } = render(
|
|
44
|
+
<RendererSurfaceContext.Provider value={SURFACE}>
|
|
45
|
+
<ToolRenderer content={TOOL} filePath="knowledge-base/Tools/my_tool.tool" onSave={async () => undefined} />
|
|
46
|
+
</RendererSurfaceContext.Provider>,
|
|
47
|
+
);
|
|
48
|
+
await settle();
|
|
49
|
+
expect(screen.queryByText('Preview')).toBeNull();
|
|
50
|
+
expect(screen.queryByText('Secrets for this tool')).toBeNull();
|
|
51
|
+
expect(screen.queryByText('Secret variables')).toBeNull();
|
|
52
|
+
expect(fetchMock).not.toHaveBeenCalled();
|
|
53
|
+
// The file itself is still there to read and edit, and a writer keeps the
|
|
54
|
+
// scaffolds — the one panel that needs no session.
|
|
55
|
+
expect(screen.getByText('my_tool.tool')).toBeTruthy();
|
|
56
|
+
expect(screen.getByText('Scaffold')).toBeTruthy();
|
|
57
|
+
expect(container.querySelector('aside')).not.toBeNull();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('read-only on a surface, draws no side column at all', async () => {
|
|
61
|
+
const { container } = render(
|
|
62
|
+
<RendererSurfaceContext.Provider value={SURFACE}>
|
|
63
|
+
<ToolRenderer content={TOOL} filePath="knowledge-base/Tools/my_tool.tool" onSave={async () => undefined} readOnly />
|
|
64
|
+
</RendererSurfaceContext.Provider>,
|
|
65
|
+
);
|
|
66
|
+
await settle();
|
|
67
|
+
expect(container.querySelector('aside')).toBeNull();
|
|
68
|
+
expect(screen.getByText('my_tool.tool')).toBeTruthy();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('in the app draws them and asks the session for the vault', async () => {
|
|
72
|
+
render(<ToolRenderer content={TOOL} filePath="knowledge-base/Tools/my_tool.tool" onSave={async () => undefined} />);
|
|
73
|
+
await settle();
|
|
74
|
+
expect(screen.getByText('Preview')).toBeTruthy();
|
|
75
|
+
expect(screen.getByText('Secrets for this tool')).toBeTruthy();
|
|
76
|
+
expect(screen.getByText('Secret variables')).toBeTruthy();
|
|
77
|
+
const urls = fetchMock.mock.calls.map((c) => String(c[0]));
|
|
78
|
+
// The palette's own call, exactly — not satisfied by the tool-secrets
|
|
79
|
+
// catalog call that shares the prefix.
|
|
80
|
+
expect(urls).toContain('/api/secrets');
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import vm from 'node:vm';
|
|
2
|
+
import { buildSandboxedHtml } from '../htmlSandbox';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Runs the frame's runtime — the inline module `buildSandboxedHtml` emits, nav
|
|
6
|
+
* bridge included — against a stub document, and records what it does: what
|
|
7
|
+
* it posts to the parent, which element it scrolls into view, whether it
|
|
8
|
+
* scrolled to the top, and whether a click's default was cancelled. Shared by
|
|
9
|
+
* the bridge's own tests and by the drift test that holds the agent guide's
|
|
10
|
+
* `html-views` section to it.
|
|
11
|
+
*/
|
|
12
|
+
export interface BridgeRun {
|
|
13
|
+
/**
|
|
14
|
+
* Click an `<a>` whose written href is `href`; answers whether the default
|
|
15
|
+
* was cancelled. `alreadyPrevented` is a click a handler of the page's own
|
|
16
|
+
* cancelled before it bubbled up to the bridge. `noAnchor` is a click on
|
|
17
|
+
* something that is not inside an `<a>` at all — a button, a div — which
|
|
18
|
+
* the bridge must leave alone.
|
|
19
|
+
*/
|
|
20
|
+
click: (href: string, opts?: { alreadyPrevented?: boolean; noAnchor?: boolean }) => { prevented: boolean };
|
|
21
|
+
/** `window.bevel` as the page's own scripts see it. */
|
|
22
|
+
bevel: Record<string, unknown>;
|
|
23
|
+
/** Messages posted to the parent. */
|
|
24
|
+
posted: unknown[];
|
|
25
|
+
/** Ids (or names) of the elements scrolled into view, in order. */
|
|
26
|
+
scrolled: string[];
|
|
27
|
+
/** The tag of each element scrolled into view, beside `scrolled`: `A`, `INPUT`, `DIV`. */
|
|
28
|
+
scrolledTags: string[];
|
|
29
|
+
/** How many times the page was scrolled to its top. */
|
|
30
|
+
scrolledToTop: () => number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* `ids` are elements with that id; `names` are `<a name="…">` anchors;
|
|
35
|
+
* `namedControls` are `<input name="…">` fields, which a fragment must NOT
|
|
36
|
+
* scroll to — the standard's fallback is the named anchor alone.
|
|
37
|
+
*/
|
|
38
|
+
export function runBridge(page: { ids?: string[]; names?: string[]; namedControls?: string[] } = {}): BridgeRun {
|
|
39
|
+
// No lib sources: a vm script cannot parse an `export`. The bridge is
|
|
40
|
+
// appended after the lib either way.
|
|
41
|
+
const out = buildSandboxedHtml({ title: 't', libModuleSources: [], bodyHtml: '' });
|
|
42
|
+
const scriptBody = out.match(/<script type="module">([\s\S]*?)<\/script>/)![1];
|
|
43
|
+
|
|
44
|
+
type ClickEvent = { target: { closest: () => unknown }; defaultPrevented: boolean; preventDefault: () => void };
|
|
45
|
+
const handlers: ((e: ClickEvent) => void)[] = [];
|
|
46
|
+
const posted: unknown[] = [];
|
|
47
|
+
const scrolled: string[] = [];
|
|
48
|
+
const scrolledTags: string[] = [];
|
|
49
|
+
let toTop = 0;
|
|
50
|
+
const element = (key: string, tagName = 'DIV') => ({
|
|
51
|
+
tagName,
|
|
52
|
+
scrollIntoView: () => {
|
|
53
|
+
scrolled.push(key);
|
|
54
|
+
scrolledTags.push(tagName);
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
const ids = new Set(page.ids ?? []);
|
|
58
|
+
const names = new Set(page.names ?? []);
|
|
59
|
+
const controls = new Set(page.namedControls ?? []);
|
|
60
|
+
const ctx: Record<string, unknown> = {
|
|
61
|
+
document: {
|
|
62
|
+
addEventListener: (type: string, fn: (e: ClickEvent) => void) => {
|
|
63
|
+
if (type === 'click') handlers.push(fn);
|
|
64
|
+
},
|
|
65
|
+
getElementById: (id: string) => (ids.has(id) ? element(id) : null),
|
|
66
|
+
// In document order a control comes first here, so a fallback that
|
|
67
|
+
// took "the first named element" would scroll the wrong thing.
|
|
68
|
+
getElementsByName: (name: string) => [
|
|
69
|
+
...(controls.has(name) ? [element(name, 'INPUT')] : []),
|
|
70
|
+
...(names.has(name) ? [element(name, 'A')] : []),
|
|
71
|
+
],
|
|
72
|
+
},
|
|
73
|
+
parent: { postMessage: (msg: unknown) => posted.push(msg) },
|
|
74
|
+
scrollTo: () => {
|
|
75
|
+
toTop += 1;
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
vm.createContext(ctx);
|
|
79
|
+
vm.runInContext(`"use strict"; (function(){ ${scriptBody} }).call(undefined);`, ctx);
|
|
80
|
+
if (handlers.length !== 1) throw new Error(`the bridge registered ${handlers.length} click listeners`);
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
click: (href: string, opts = {}) => {
|
|
84
|
+
let prevented = opts.alreadyPrevented ?? false;
|
|
85
|
+
const anchor = opts.noAnchor ? null : { getAttribute: () => href };
|
|
86
|
+
handlers[0]({
|
|
87
|
+
target: { closest: () => anchor },
|
|
88
|
+
defaultPrevented: prevented,
|
|
89
|
+
preventDefault: () => (prevented = true),
|
|
90
|
+
});
|
|
91
|
+
return { prevented };
|
|
92
|
+
},
|
|
93
|
+
bevel: ctx.bevel as Record<string, unknown>,
|
|
94
|
+
posted,
|
|
95
|
+
scrolled,
|
|
96
|
+
scrolledTags,
|
|
97
|
+
scrolledToTop: () => toTop,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { describe, it, expect } from 'vitest';
|
|
2
2
|
import vm from 'node:vm';
|
|
3
3
|
import { sanitizeAgentHtml, buildSandboxedHtml } from '../htmlSandbox';
|
|
4
|
+
import { runBridge } from './bridgeHarness';
|
|
4
5
|
|
|
5
6
|
describe('sanitizeAgentHtml', () => {
|
|
6
7
|
it('keeps inline scripts and tags them as modules so they run after the lib', () => {
|
|
@@ -418,50 +419,143 @@ describe('buildSandboxedHtml', () => {
|
|
|
418
419
|
// Posts to the parent rather than navigating the (sandboxed) host window.
|
|
419
420
|
expect(out).toContain("type: 'bevel.navigate'");
|
|
420
421
|
expect(out).toContain('parent.postMessage');
|
|
421
|
-
// Delegated anchor-click interception
|
|
422
|
+
// Delegated anchor-click interception; a bare fragment is jumped to here.
|
|
422
423
|
expect(out).toContain("a[href]");
|
|
423
|
-
expect(out).toContain(
|
|
424
|
+
expect(out).toContain('jumpTo(href)');
|
|
424
425
|
});
|
|
425
426
|
|
|
426
427
|
// The bridge half of the one-rule promise. The sanitizer keeps a padded
|
|
427
428
|
// href by reading past the padding; the bridge has to hand the parent the
|
|
428
429
|
// same reading, or the resolver builds a path out of the spaces. A padded
|
|
429
|
-
// `#goal` is still an in-page anchor
|
|
430
|
-
it('posts the href a browser would follow, and
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
if (type === 'click') handlers.push(fn);
|
|
443
|
-
},
|
|
444
|
-
},
|
|
445
|
-
parent: { postMessage: (msg: unknown) => posted.push(msg) },
|
|
446
|
-
};
|
|
447
|
-
vm.createContext(ctx);
|
|
448
|
-
vm.runInContext(`"use strict"; (function(){ ${scriptBody} }).call(undefined);`, ctx);
|
|
449
|
-
expect(handlers).toHaveLength(1);
|
|
450
|
-
|
|
451
|
-
const click = (href: string) => {
|
|
452
|
-
const anchor = { getAttribute: () => href };
|
|
453
|
-
handlers[0]({ target: { closest: () => anchor }, preventDefault: () => {} });
|
|
454
|
-
};
|
|
455
|
-
|
|
456
|
-
click('\n Board.html\n ');
|
|
457
|
-
click(' #goal ');
|
|
458
|
-
click(' https://example.com/docs ');
|
|
459
|
-
click(' ');
|
|
460
|
-
|
|
461
|
-
expect(posted).toEqual([
|
|
430
|
+
// `#goal` is still an in-page anchor, scrolled to in the frame.
|
|
431
|
+
it('posts the href a browser would follow, and scrolls to a padded fragment', () => {
|
|
432
|
+
const bridge = runBridge({ ids: ['goal'] });
|
|
433
|
+
|
|
434
|
+
bridge.click('\n Board.html\n ');
|
|
435
|
+
bridge.click(' #goal ');
|
|
436
|
+
bridge.click(' https://example.com/docs ');
|
|
437
|
+
// An empty href goes nowhere, and its default is still cancelled: left to
|
|
438
|
+
// the browser it would reload the frame against the app's URL.
|
|
439
|
+
expect(bridge.click(' ')).toEqual({ prevented: true });
|
|
440
|
+
expect(bridge.click('')).toEqual({ prevented: true });
|
|
441
|
+
|
|
442
|
+
expect(bridge.posted).toEqual([
|
|
462
443
|
{ type: 'bevel.navigate', href: 'Board.html' },
|
|
463
444
|
{ type: 'bevel.navigate', href: 'https://example.com/docs' },
|
|
464
445
|
]);
|
|
446
|
+
expect(bridge.scrolled).toEqual(['goal']);
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* A bare fragment is an in-page jump by either route. Left to the browser, a
|
|
451
|
+
* `srcdoc` frame resolves `#totals` against the APP's URL and navigates
|
|
452
|
+
* itself away; handed to the parent, the app re-opens the same file with
|
|
453
|
+
* the fragment on its URL and the page does not move. So the bridge
|
|
454
|
+
* cancels the click, scrolls in the frame, and posts nothing.
|
|
455
|
+
*/
|
|
456
|
+
describe('a bare fragment scrolls the page inside the frame', () => {
|
|
457
|
+
it('on a click: the element scrolls into view, the default is cancelled, nothing is posted', () => {
|
|
458
|
+
const bridge = runBridge({ ids: ['totals'] });
|
|
459
|
+
expect(bridge.click('#totals')).toEqual({ prevented: true });
|
|
460
|
+
expect(bridge.scrolled).toEqual(['totals']);
|
|
461
|
+
expect(bridge.posted).toEqual([]);
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
it('through window.bevel.openNode and window.bevel.navigate the same', () => {
|
|
465
|
+
for (const member of ['openNode', 'navigate']) {
|
|
466
|
+
const bridge = runBridge({ ids: ['totals'] });
|
|
467
|
+
(bridge.bevel[member] as (href: string) => void)('#totals');
|
|
468
|
+
expect(bridge.scrolled, member).toEqual(['totals']);
|
|
469
|
+
expect(bridge.posted, member).toEqual([]);
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
it('a fragment naming no element does nothing: no scroll, no navigation, and the default still cancelled', () => {
|
|
474
|
+
const bridge = runBridge({ ids: ['totals'] });
|
|
475
|
+
expect(bridge.click('#nowhere')).toEqual({ prevented: true });
|
|
476
|
+
(bridge.bevel.openNode as (href: string) => void)('#nowhere');
|
|
477
|
+
expect(bridge.scrolled).toEqual([]);
|
|
478
|
+
expect(bridge.scrolledToTop()).toBe(0);
|
|
479
|
+
expect(bridge.posted).toEqual([]);
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
it('`#` and `#top` with no such element scroll to the top; an element named top wins', () => {
|
|
483
|
+
const bridge = runBridge();
|
|
484
|
+
bridge.click('#');
|
|
485
|
+
bridge.click('#top');
|
|
486
|
+
bridge.click('#TOP');
|
|
487
|
+
expect(bridge.scrolledToTop()).toBe(3);
|
|
488
|
+
expect(bridge.posted).toEqual([]);
|
|
489
|
+
// An id and a named anchor both win over the top of the page.
|
|
490
|
+
for (const page of [{ ids: ['top'] }, { names: ['top'] }]) {
|
|
491
|
+
const withTop = runBridge(page);
|
|
492
|
+
withTop.click('#top');
|
|
493
|
+
expect(withTop.scrolled, JSON.stringify(page)).toEqual(['top']);
|
|
494
|
+
expect(withTop.scrolledToTop(), JSON.stringify(page)).toBe(0);
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
it('finds the target by a percent-encoded id and by an anchor name', () => {
|
|
499
|
+
const bridge = runBridge({ ids: ['Q3 totals'], names: ['legacy'] });
|
|
500
|
+
bridge.click('#Q3%20totals');
|
|
501
|
+
bridge.click('#legacy');
|
|
502
|
+
expect(bridge.scrolled).toEqual(['Q3 totals', 'legacy']);
|
|
503
|
+
// A malformed escape is looked up as written, and nothing breaks.
|
|
504
|
+
bridge.click('#100%');
|
|
505
|
+
expect(bridge.scrolled).toEqual(['Q3 totals', 'legacy']);
|
|
506
|
+
expect(bridge.posted).toEqual([]);
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
// The standard's fallback is the named ANCHOR: a form control carrying
|
|
510
|
+
// the name is not a target, even when it comes first in the document.
|
|
511
|
+
it('a name on a form control is not a fragment target; only <a name> is', () => {
|
|
512
|
+
const bridge = runBridge({ names: ['legacy'], namedControls: ['legacy', 'query'] });
|
|
513
|
+
const { prevented } = bridge.click('#query');
|
|
514
|
+
expect(bridge.scrolled).toEqual([]);
|
|
515
|
+
expect(prevented).toBe(true);
|
|
516
|
+
// The control comes first in document order; the anchor is the target.
|
|
517
|
+
bridge.click('#legacy');
|
|
518
|
+
expect(bridge.scrolled).toEqual(['legacy']);
|
|
519
|
+
expect(bridge.scrolledTags).toEqual(['A']);
|
|
520
|
+
expect(bridge.posted).toEqual([]);
|
|
521
|
+
});
|
|
522
|
+
|
|
523
|
+
it('leaves a click that is not on a link alone — a button, a div — whatever it carries', () => {
|
|
524
|
+
const bridge = runBridge({ ids: ['goal'] });
|
|
525
|
+
const { prevented } = bridge.click('#goal', { noAnchor: true });
|
|
526
|
+
expect(prevented).toBe(false);
|
|
527
|
+
expect(bridge.scrolled).toEqual([]);
|
|
528
|
+
expect(bridge.posted).toEqual([]);
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
// The HTML standard's order: the fragment as written names the target
|
|
532
|
+
// first, its decoding only when that finds nothing.
|
|
533
|
+
it('prefers the id as written over its percent-decoding, as a browser does', () => {
|
|
534
|
+
const bridge = runBridge({ ids: ['Q3%20totals', 'Q3 totals'] });
|
|
535
|
+
bridge.click('#Q3%20totals');
|
|
536
|
+
expect(bridge.scrolled).toEqual(['Q3%20totals']);
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
it('leaves a click the page already cancelled to the page: no scroll, nothing posted', () => {
|
|
540
|
+
const bridge = runBridge({ ids: ['panel'] });
|
|
541
|
+
bridge.click('#panel', { alreadyPrevented: true });
|
|
542
|
+
bridge.click('#', { alreadyPrevented: true });
|
|
543
|
+
bridge.click('../Knowledge/Alice.md', { alreadyPrevented: true });
|
|
544
|
+
expect(bridge.scrolled).toEqual([]);
|
|
545
|
+
expect(bridge.scrolledToTop()).toBe(0);
|
|
546
|
+
expect(bridge.posted).toEqual([]);
|
|
547
|
+
});
|
|
548
|
+
|
|
549
|
+
it('a document link with a trailing fragment still goes to the app, and scrolls nothing', () => {
|
|
550
|
+
const bridge = runBridge({ ids: ['goal'] });
|
|
551
|
+
expect(bridge.click('../Knowledge/Alice.md#goal')).toEqual({ prevented: true });
|
|
552
|
+
(bridge.bevel.openNode as (href: string) => void)('../Knowledge/Alice.md#goal');
|
|
553
|
+
expect(bridge.posted).toEqual([
|
|
554
|
+
{ type: 'bevel.navigate', href: '../Knowledge/Alice.md#goal' },
|
|
555
|
+
{ type: 'bevel.navigate', href: '../Knowledge/Alice.md#goal' },
|
|
556
|
+
]);
|
|
557
|
+
expect(bridge.scrolled).toEqual([]);
|
|
558
|
+
});
|
|
465
559
|
});
|
|
466
560
|
|
|
467
561
|
it('embeds the body HTML verbatim (sanitization happens upstream)', () => {
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { describe, expect, it } from 'vitest';
|
|
5
|
+
import { isOpenableExternalHref, isPageLinkExternalHref } from '../../../../../shared/markdown/hrefs';
|
|
6
|
+
import { sanitizeAgentHtml } from '../htmlSandbox';
|
|
7
|
+
import { runBridge } from './bridgeHarness';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The `html-views` section of the agent guide states the frame's link rules
|
|
11
|
+
* to every agent that writes a page; this file holds it to the renderer that
|
|
12
|
+
* enforces them. The section is core-backend's file and is read here as TEXT,
|
|
13
|
+
* in a test only — the two packages still do not import each other. When the
|
|
14
|
+
* section and the sanitizer or the bridge disagree, this fails, whichever of
|
|
15
|
+
* the two moved.
|
|
16
|
+
*/
|
|
17
|
+
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
18
|
+
const SECTION_FILE = join(HERE, '../../../../../../../core-backend/agent-guide/html-views.md');
|
|
19
|
+
// Any knowledge folder name will do: the placeholder only sits inside a path.
|
|
20
|
+
const guide = readFileSync(SECTION_FILE, 'utf8').replace(/\{\{\w+\}\}/g, 'KnowledgeBase');
|
|
21
|
+
const flat = guide.replace(/\s+/g, ' ');
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The example hrefs of the bullet list under the line starting `intro`: each
|
|
25
|
+
* bullet names a shape, then ` — ` and the shape's examples in backticks.
|
|
26
|
+
*/
|
|
27
|
+
function examplesUnder(intro: string): string[][] {
|
|
28
|
+
const lines = guide.split('\n');
|
|
29
|
+
let at = lines.findIndex((l) => l.startsWith(intro));
|
|
30
|
+
expect(at, `the section has no line starting "${intro}"`).toBeGreaterThan(-1);
|
|
31
|
+
while (at < lines.length && !lines[at]!.startsWith('- ')) at += 1;
|
|
32
|
+
const bullets: string[][] = [];
|
|
33
|
+
for (; at < lines.length && lines[at]!.startsWith('- '); at += 1) {
|
|
34
|
+
const tail = lines[at]!.split(' — ')[1];
|
|
35
|
+
expect(tail, `bullet without examples: ${lines[at]}`).toBeTruthy();
|
|
36
|
+
bullets.push([...tail!.matchAll(/`([^`]+)`/g)].map((m) => m[1]!));
|
|
37
|
+
}
|
|
38
|
+
expect(bullets.length).toBeGreaterThan(0);
|
|
39
|
+
return bullets;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The href an `<a>` keeps after the sanitizer, or null when it was removed. */
|
|
43
|
+
function hrefAfterSanitizing(href: string): string | null {
|
|
44
|
+
const html = sanitizeAgentHtml(`<a href="${href.replace(/&/g, '&').replace(/"/g, '"')}">x</a>`);
|
|
45
|
+
const a = new DOMParser().parseFromString(html, 'text/html').querySelector('a')!;
|
|
46
|
+
return a.getAttribute('href');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
describe('the html-views guide section agrees with the renderer', () => {
|
|
50
|
+
const kept = examplesUnder('An `<a href>` in the page keeps its address only when').flat();
|
|
51
|
+
const removed = examplesUnder('Every other address is removed').flat();
|
|
52
|
+
|
|
53
|
+
it('every link it lists as surviving is kept by the sanitizer, as written', () => {
|
|
54
|
+
for (const href of kept) expect(hrefAfterSanitizing(href), href).toBe(href);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('every link it lists as removed loses its href', () => {
|
|
58
|
+
for (const href of removed) expect(hrefAfterSanitizing(href), href).toBeNull();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('lists every shape the sanitizer keeps, and the ones it removes', () => {
|
|
62
|
+
// The kept list is complete: one example of each shape `isKeptLink` keeps.
|
|
63
|
+
const keptShapes: [string, (h: string) => boolean][] = [
|
|
64
|
+
['.md document', (h) => /\.md(#|$)/.test(h)],
|
|
65
|
+
['.md document with a heading', (h) => /\.md#/.test(h)],
|
|
66
|
+
['.html document', (h) => /\.html$/.test(h)],
|
|
67
|
+
['.htm document', (h) => /\.htm$/.test(h)],
|
|
68
|
+
['/workspace/ URL', (h) => h.startsWith('/workspace/')],
|
|
69
|
+
['http:', (h) => h.startsWith('http:')],
|
|
70
|
+
['https:', (h) => h.startsWith('https:')],
|
|
71
|
+
['mailto:', (h) => h.startsWith('mailto:')],
|
|
72
|
+
['bare fragment', (h) => h.startsWith('#')],
|
|
73
|
+
];
|
|
74
|
+
for (const [shape, is] of keptShapes) expect(kept.some(is), shape).toBe(true);
|
|
75
|
+
const removedShapes: [string, (h: string) => boolean][] = [
|
|
76
|
+
['javascript:', (h) => h.startsWith('javascript:')],
|
|
77
|
+
['data:', (h) => h.startsWith('data:')],
|
|
78
|
+
['protocol-relative', (h) => h.startsWith('//')],
|
|
79
|
+
['relative path to a non-document', (h) => !/^[a-z]+:|^\/|^#/i.test(h)],
|
|
80
|
+
];
|
|
81
|
+
for (const [shape, is] of removedShapes) expect(removed.some(is), shape).toBe(true);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('says a bare fragment scrolls the page, and the bridge does exactly that', () => {
|
|
85
|
+
expect(flat).toContain('**A bare fragment scrolls the page.**');
|
|
86
|
+
expect(flat).toContain('the page stays loaded and the app does not move');
|
|
87
|
+
expect(flat).toContain('A fragment that names no element by `id`, and no `<a name>`, does nothing. `#` and `#top` scroll to the top of the page only when no target matches');
|
|
88
|
+
expect(flat).toContain('The same rules apply to a click on a link and to `window.bevel.openNode(href)` or `window.bevel.navigate(href)`');
|
|
89
|
+
|
|
90
|
+
for (const route of ['click', 'openNode', 'navigate'] as const) {
|
|
91
|
+
const bridge = runBridge({ ids: ['totals'] });
|
|
92
|
+
const go = (href: string) =>
|
|
93
|
+
route === 'click' ? bridge.click(href) : (bridge.bevel[route] as (h: string) => void)(href);
|
|
94
|
+
const clicks = [go('#totals'), go('#nowhere'), go('#'), go('#top')];
|
|
95
|
+
// A click's default is cancelled every time: left to the browser, the
|
|
96
|
+
// frame would navigate away after the scroll.
|
|
97
|
+
if (route === 'click') expect(clicks, route).toEqual(Array(4).fill({ prevented: true }));
|
|
98
|
+
expect(bridge.scrolled, route).toEqual(['totals']);
|
|
99
|
+
expect(bridge.scrolledToTop(), route).toBe(2);
|
|
100
|
+
expect(bridge.posted, route).toEqual([]);
|
|
101
|
+
}
|
|
102
|
+
expect(flat).toContain('an element with `id="top"`, or an `<a name="top">`, wins over the top of the page');
|
|
103
|
+
for (const page of [{ ids: ['top'] }, { names: ['top'] }]) {
|
|
104
|
+
const withTop = runBridge(page);
|
|
105
|
+
withTop.click('#top');
|
|
106
|
+
expect(withTop.scrolled, JSON.stringify(page)).toEqual(['top']);
|
|
107
|
+
expect(withTop.scrolledToTop(), JSON.stringify(page)).toBe(0);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('says a click the page already cancelled is left to the page, and the bridge leaves it', () => {
|
|
112
|
+
expect(flat).toContain(
|
|
113
|
+
'A click that a handler of the page has already cancelled with `event.preventDefault()` is left to that handler: the bridge neither scrolls nor opens anything for it.',
|
|
114
|
+
);
|
|
115
|
+
const bridge = runBridge({ ids: ['totals'] });
|
|
116
|
+
bridge.click('#totals', { alreadyPrevented: true });
|
|
117
|
+
bridge.click('../Knowledge/Alice.md', { alreadyPrevented: true });
|
|
118
|
+
expect(bridge.scrolled).toEqual([]);
|
|
119
|
+
expect(bridge.posted).toEqual([]);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('names the addresses only a script may open, and the app opens exactly those beyond the markup', () => {
|
|
123
|
+
expect(flat).toContain(
|
|
124
|
+
'A call from a script may also open `tel:`, `sms:`, `geo:` and a protocol-relative `//host/path` address in a new tab, though a link written in the markup cannot keep one.',
|
|
125
|
+
);
|
|
126
|
+
for (const href of ['tel:+15550100', 'sms:+15550100', 'geo:0,0', '//example.com/docs']) {
|
|
127
|
+
expect(isOpenableExternalHref(href), href).toBe(true);
|
|
128
|
+
expect(isPageLinkExternalHref(href), href).toBe(false);
|
|
129
|
+
expect(hrefAfterSanitizing(href), href).toBeNull();
|
|
130
|
+
}
|
|
131
|
+
for (const href of ['javascript:alert(1)', 'data:text/html,hi', 'file:///etc/hosts']) {
|
|
132
|
+
expect(isOpenableExternalHref(href), href).toBe(false);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('every non-fragment link it lists as surviving goes to the app through the bridge', () => {
|
|
137
|
+
const bridge = runBridge();
|
|
138
|
+
const leaving = kept.filter((h) => !h.startsWith('#'));
|
|
139
|
+
for (const href of leaving) expect(bridge.click(href), href).toEqual({ prevented: true });
|
|
140
|
+
expect(bridge.posted).toEqual(leaving.map((href) => ({ type: 'bevel.navigate', href })));
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('names exactly the window.bevel members core defines, and leaves data members to a distribution', () => {
|
|
144
|
+
const named = new Set([...guide.matchAll(/window\.bevel\??\.(\w+)/g)].map((m) => m[1]!));
|
|
145
|
+
expect([...named].sort()).toEqual(Object.keys(runBridge().bevel).sort());
|
|
146
|
+
expect([...named].sort()).toEqual(['navigate', 'openNode']);
|
|
147
|
+
expect(flat).toContain('A distribution may add data members of its own to `window.bevel`');
|
|
148
|
+
});
|
|
149
|
+
});
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { render, screen } from '@testing-library/react';
|
|
3
|
+
import userEvent from '@testing-library/user-event';
|
|
4
|
+
import { KbMarkdownView } from '../KbMarkdownView';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* WHICH links a surface takes over, and which the browser keeps.
|
|
8
|
+
*
|
|
9
|
+
* The app renders in a tab, so an `http(s)` destination is best left to the
|
|
10
|
+
* browser: `target="_blank"` opens a new tab and nothing needs intercepting.
|
|
11
|
+
* A renderer surface — the embed, inside a chat host's iframe — cannot do
|
|
12
|
+
* that: a host's sandbox has no `allow-popups`, so `target="_blank"` is
|
|
13
|
+
* ignored and the plain anchor NAVIGATES THE FRAME. The reader loses the page
|
|
14
|
+
* and has no way back, which is the one thing the embed must never do.
|
|
15
|
+
*
|
|
16
|
+
* Found on a real boot: an external link in an embedded page navigated the
|
|
17
|
+
* frame away and no `bevel-embed-open` ever reached the host.
|
|
18
|
+
*/
|
|
19
|
+
function view(source: string, props: Partial<Parameters<typeof KbMarkdownView>[0]> = {}) {
|
|
20
|
+
const onOpenFile = vi.fn();
|
|
21
|
+
render(<KbMarkdownView source={source} onOpenFile={onOpenFile} {...props} />);
|
|
22
|
+
return { onOpenFile };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe("the app's link policy (the default)", () => {
|
|
26
|
+
it('leaves an external link to the browser, in a new tab', async () => {
|
|
27
|
+
const { onOpenFile } = view('[docs](https://example.test/docs)');
|
|
28
|
+
const link = screen.getByRole('link', { name: 'docs' });
|
|
29
|
+
expect(link.getAttribute('target')).toBe('_blank');
|
|
30
|
+
expect(link.getAttribute('rel')).toContain('noopener');
|
|
31
|
+
await userEvent.click(link);
|
|
32
|
+
expect(onOpenFile).not.toHaveBeenCalled();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('takes an internal .md link', async () => {
|
|
36
|
+
const { onOpenFile } = view('[other](Other.md)');
|
|
37
|
+
await userEvent.click(screen.getByRole('link', { name: 'other' }));
|
|
38
|
+
expect(onOpenFile).toHaveBeenCalledWith('Other.md');
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe("a surface's link policy", () => {
|
|
43
|
+
const surface = { linkPolicy: 'surface' as const };
|
|
44
|
+
|
|
45
|
+
it('takes an external link rather than letting it navigate the frame', async () => {
|
|
46
|
+
const { onOpenFile } = view('[docs](https://example.test/docs)', surface);
|
|
47
|
+
const link = screen.getByRole('link', { name: 'docs' });
|
|
48
|
+
// No `target="_blank"` to be ignored by a sandbox, and no navigation:
|
|
49
|
+
// the click is cancelled and the destination is handed over.
|
|
50
|
+
expect(link.getAttribute('target')).toBeNull();
|
|
51
|
+
await userEvent.click(link);
|
|
52
|
+
expect(onOpenFile).toHaveBeenCalledWith('https://example.test/docs');
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The narrower half of the same bug: the internal rule matched `.md` only,
|
|
57
|
+
* so a knowledge-base link to an image, a PDF or a document was left to the
|
|
58
|
+
* browser as a relative URL — in a frame, another way to navigate away.
|
|
59
|
+
*/
|
|
60
|
+
it.each([
|
|
61
|
+
['an image', '[shot](assets/shot.png)', 'assets/shot.png'],
|
|
62
|
+
['a PDF', '[report](Docs/Report.pdf)', 'Docs/Report.pdf'],
|
|
63
|
+
['a document', '[spec](Docs/Spec.docx)', 'Docs/Spec.docx'],
|
|
64
|
+
['a markdown page', '[other](Other.md)', 'Other.md'],
|
|
65
|
+
['a page with an anchor', '[goal](Other.md#goal)', 'Other.md#goal'],
|
|
66
|
+
])('takes a knowledge-base link to %s', async (_label, source, href) => {
|
|
67
|
+
const { onOpenFile } = view(source, surface);
|
|
68
|
+
await userEvent.click(screen.getByRole('link'));
|
|
69
|
+
expect(onOpenFile).toHaveBeenCalledWith(href);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* A same-page anchor is the browser's under BOTH policies: it scrolls
|
|
74
|
+
* inside this view and leaves nothing, so relaying it to the host would
|
|
75
|
+
* open a second copy of the page in a tab.
|
|
76
|
+
*/
|
|
77
|
+
it('leaves a same-page anchor alone', async () => {
|
|
78
|
+
const { onOpenFile } = view('## Goal\n\n[jump](#goal)', surface);
|
|
79
|
+
await userEvent.click(screen.getByRole('link', { name: 'jump' }));
|
|
80
|
+
expect(onOpenFile).not.toHaveBeenCalled();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Judged as the browser judges it: a destination padded with whitespace is
|
|
85
|
+
* still a same-page anchor once normalised, so it scrolls here rather than
|
|
86
|
+
* being relayed out as another page.
|
|
87
|
+
*/
|
|
88
|
+
it('leaves a whitespace-padded same-page anchor alone too', async () => {
|
|
89
|
+
const { onOpenFile } = view('## Goal\n\n<a href=" #goal">jump</a>', surface);
|
|
90
|
+
const link = screen.getByRole('link', { name: 'jump' });
|
|
91
|
+
expect(link.getAttribute('href')).toBe(' #goal');
|
|
92
|
+
await userEvent.click(link);
|
|
93
|
+
expect(onOpenFile).not.toHaveBeenCalled();
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('still renders a bare node id inert when there is no resolver for it', () => {
|
|
97
|
+
view('[a node](some-node-id)', surface);
|
|
98
|
+
expect(screen.queryByRole('link', { name: 'a node' })).toBeNull();
|
|
99
|
+
});
|
|
100
|
+
});
|