@bevel-software/platform-core-frontend 0.1.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/LICENSE +202 -0
- package/package.json +70 -0
- package/src/core/CoreAppShell.tsx +444 -0
- package/src/core/__tests__/ShellRoutes.test.tsx +95 -0
- package/src/core/events.ts +29 -0
- package/src/core/registry.ts +336 -0
- package/src/css-side-effect-imports.d.ts +7 -0
- package/src/html-extensions.ts +12 -0
- package/src/index.css +49 -0
- package/src/index.ts +23 -0
- package/src/lib/api.ts +57 -0
- package/src/lib/utils.ts +138 -0
- package/src/modules/access/__tests__/ManageAccessDialog.test.tsx +130 -0
- package/src/modules/access/__tests__/useFileAccess.test.tsx +253 -0
- package/src/modules/access/api.ts +205 -0
- package/src/modules/access/components/AccessRestrictedBanner.tsx +31 -0
- package/src/modules/access/components/ManageAccessDialog.tsx +1232 -0
- package/src/modules/access/components/NodeOwnersBanner.tsx +30 -0
- package/src/modules/access/hooks/useFileAccess.ts +151 -0
- package/src/modules/admin/components/AdminRolesPage.tsx +943 -0
- package/src/modules/admin/components/RolesCorruptedBanner.tsx +74 -0
- package/src/modules/admin/services/admin.api.ts +92 -0
- package/src/modules/admin/services/roles.api.ts +136 -0
- package/src/modules/admin/state/admin.context.tsx +223 -0
- package/src/modules/auth/components/LoginScreen.tsx +135 -0
- package/src/modules/auth/hooks/useAuthState.ts +59 -0
- package/src/modules/auth/services/auth.api.ts +24 -0
- package/src/modules/auth/services/microsoft-oauth.ts +57 -0
- package/src/modules/auth/state/auth.context.ts +18 -0
- package/src/modules/git/__tests__/BranchSwitcher.test.tsx +347 -0
- package/src/modules/git/__tests__/FileHistoryPanel.test.tsx +201 -0
- package/src/modules/git/__tests__/ProtectedBranchBanner.test.tsx +82 -0
- package/src/modules/git/__tests__/PullNeededBanner.test.tsx +441 -0
- package/src/modules/git/__tests__/git.api.test.ts +113 -0
- package/src/modules/git/__tests__/pr.api.test.ts +44 -0
- package/src/modules/git/__tests__/useAutoPullUpdates.test.tsx +212 -0
- package/src/modules/git/components/BranchSwitcher.tsx +527 -0
- package/src/modules/git/components/DiffView.tsx +30 -0
- package/src/modules/git/components/FileComparisonPanel.tsx +377 -0
- package/src/modules/git/components/FileHistoryPanel.tsx +320 -0
- package/src/modules/git/components/ProtectedBranchBanner.tsx +42 -0
- package/src/modules/git/components/PullNeededBanner.tsx +204 -0
- package/src/modules/git/components/PullRequestsForMe.tsx +244 -0
- package/src/modules/git/components/UnifiedDiffView.tsx +37 -0
- package/src/modules/git/hooks/__tests__/useGitState.deleteBranch.test.tsx +99 -0
- package/src/modules/git/hooks/useAutoPullUpdates.ts +121 -0
- package/src/modules/git/hooks/useGitState.ts +335 -0
- package/src/modules/git/services/__tests__/error-messages.test.ts +134 -0
- package/src/modules/git/services/error-messages.ts +257 -0
- package/src/modules/git/services/git.api.ts +163 -0
- package/src/modules/git/services/pr-prompts.ts +159 -0
- package/src/modules/git/services/pr.api.ts +39 -0
- package/src/modules/git/state/auto-update.context.ts +21 -0
- package/src/modules/git/state/git.context.ts +82 -0
- package/src/modules/layout/components/AppLayout.tsx +40 -0
- package/src/modules/layout/components/DemoBanner.tsx +57 -0
- package/src/modules/layout/components/MaintenanceOverlay.tsx +104 -0
- package/src/modules/layout/components/MobileChatLayout.tsx +153 -0
- package/src/modules/layout/components/ResizableThreePaneLayout.tsx +203 -0
- package/src/modules/layout/components/SlideOverlay.tsx +224 -0
- package/src/modules/layout/components/__tests__/MobileChatLayout.test.tsx +77 -0
- package/src/modules/layout/components/__tests__/ResizableThreePaneLayout.test.tsx +57 -0
- package/src/modules/layout/hooks/useMediaQuery.ts +23 -0
- package/src/modules/layout/state/__tests__/layout.context.test.tsx +39 -0
- package/src/modules/layout/state/layout.context.ts +46 -0
- package/src/modules/library/__tests__/DetailDialog.test.tsx +200 -0
- package/src/modules/library/__tests__/diff.test.ts +62 -0
- package/src/modules/library/__tests__/loadout.test.tsx +72 -0
- package/src/modules/library/__tests__/status.test.ts +117 -0
- package/src/modules/library/components/ChangeRequestDock.tsx +47 -0
- package/src/modules/library/components/CompareView.tsx +357 -0
- package/src/modules/library/components/DetailDialog.tsx +566 -0
- package/src/modules/library/components/GearIcon.tsx +12 -0
- package/src/modules/library/components/LibraryCard.tsx +132 -0
- package/src/modules/library/components/LibraryPage.tsx +254 -0
- package/src/modules/library/components/LoadoutSidebar.tsx +156 -0
- package/src/modules/library/components/StatusGem.tsx +21 -0
- package/src/modules/library/components/SuggestChange.tsx +171 -0
- package/src/modules/library/components/particles.ts +50 -0
- package/src/modules/library/hooks/useLibraryData.ts +119 -0
- package/src/modules/library/hooks/useSkillDetail.ts +74 -0
- package/src/modules/library/library.css +320 -0
- package/src/modules/library/services/library.api.ts +164 -0
- package/src/modules/library/state/loadout.tsx +129 -0
- package/src/modules/library/state/toast.tsx +42 -0
- package/src/modules/library/utils/diff.ts +102 -0
- package/src/modules/library/utils/status.ts +116 -0
- package/src/modules/pr/__tests__/PrApprovalBadge.test.tsx +93 -0
- package/src/modules/pr/__tests__/PrCancelButton.test.tsx +158 -0
- package/src/modules/pr/__tests__/PrCancelConfirmDialog.test.tsx +44 -0
- package/src/modules/pr/__tests__/PrMergeButton.test.tsx +360 -0
- package/src/modules/pr/__tests__/bucketize-comments.test.ts +85 -0
- package/src/modules/pr/__tests__/parsePatch.test.ts +104 -0
- package/src/modules/pr/__tests__/usePrViewerState.test.ts +72 -0
- package/src/modules/pr/components/OpenChangeRequestDialog.tsx +193 -0
- package/src/modules/pr/components/PrApprovalBadge.tsx +80 -0
- package/src/modules/pr/components/PrCancelButton.tsx +99 -0
- package/src/modules/pr/components/PrCancelConfirmDialog.tsx +119 -0
- package/src/modules/pr/components/PrCommentComposer.tsx +92 -0
- package/src/modules/pr/components/PrCommentThread.tsx +168 -0
- package/src/modules/pr/components/PrCommentsPanel.tsx +143 -0
- package/src/modules/pr/components/PrDiffPane.tsx +425 -0
- package/src/modules/pr/components/PrFileRow.tsx +164 -0
- package/src/modules/pr/components/PrFilesList.tsx +53 -0
- package/src/modules/pr/components/PrHeader.tsx +45 -0
- package/src/modules/pr/components/PrHeaderOverflowMenu.tsx +58 -0
- package/src/modules/pr/components/PrMergeButton.tsx +268 -0
- package/src/modules/pr/components/PrMergeConfirmDialog.tsx +112 -0
- package/src/modules/pr/components/PrRefreshFromTargetButton.tsx +110 -0
- package/src/modules/pr/components/PrViewer.tsx +364 -0
- package/src/modules/pr/hooks/usePrViewerState.ts +150 -0
- package/src/modules/pr/services/pr-approvals.api.ts +31 -0
- package/src/modules/pr/services/pr-cancel.api.ts +19 -0
- package/src/modules/pr/services/pr-comments.api.ts +45 -0
- package/src/modules/pr/services/pr-detail.api.ts +17 -0
- package/src/modules/pr/services/pr-merge.api.ts +53 -0
- package/src/modules/pr/services/pr-open.api.ts +30 -0
- package/src/modules/pr/state/pr-viewer.context.ts +34 -0
- package/src/modules/pr/utils/bucketize-comments.ts +55 -0
- package/src/modules/pr/utils/parsePatch.ts +57 -0
- package/src/modules/review/__tests__/review.api.test.ts +121 -0
- package/src/modules/review/components/BinaryChangePlaceholder.tsx +15 -0
- package/src/modules/review/components/DiffViewer.tsx +78 -0
- package/src/modules/review/components/MarkdownDiffViewer.tsx +263 -0
- package/src/modules/review/components/ReviewFileRow.tsx +114 -0
- package/src/modules/review/components/ReviewPanel.tsx +260 -0
- package/src/modules/review/components/ReviewPanelSurface.tsx +52 -0
- package/src/modules/review/components/__tests__/ReviewPanel.test.tsx +106 -0
- package/src/modules/review/hooks/__tests__/useReviewState.test.ts +88 -0
- package/src/modules/review/hooks/useReviewState.ts +256 -0
- package/src/modules/review/services/review.api.ts +72 -0
- package/src/modules/review/state/ReviewProvider.tsx +28 -0
- package/src/modules/review/state/review.context.ts +29 -0
- package/src/modules/secrets-vault/components/ConnectToolsPage.tsx +488 -0
- package/src/modules/secrets-vault/components/SecretsPage.tsx +276 -0
- package/src/modules/secrets-vault/components/ToolSecretsPanel.tsx +362 -0
- package/src/modules/secrets-vault/components/__tests__/SecretsPage.test.tsx +51 -0
- package/src/modules/secrets-vault/services/connect.api.ts +113 -0
- package/src/modules/secrets-vault/services/secrets.api.ts +82 -0
- package/src/modules/secrets-vault/services/tool-secrets.api.ts +116 -0
- package/src/modules/toolbar/components/AdminMenu.tsx +250 -0
- package/src/modules/toolbar/components/AppSwitcher.tsx +108 -0
- package/src/modules/toolbar/components/ExternalAgentAccessPage.tsx +548 -0
- package/src/modules/toolbar/components/Toolbar.tsx +123 -0
- package/src/modules/toolbar/components/__tests__/AppSwitcher.test.tsx +122 -0
- package/src/modules/toolbar/components/__tests__/Toolbar.test.tsx +353 -0
- package/src/modules/toolbar/services/external-api-keys.api.ts +65 -0
- package/src/modules/tools/ToolsExplorerPage.tsx +380 -0
- package/src/modules/workflow/hooks/useEventBusSubscription.ts +24 -0
- package/src/modules/workflow/hooks/useFileLock.ts +430 -0
- package/src/modules/workflow/services/lock.api.ts +139 -0
- package/src/modules/workflow/state/EventBusFocusBinder.tsx +25 -0
- package/src/modules/workflow/state/EventBusProvider.tsx +232 -0
- package/src/modules/workflow/state/event-bus.context.ts +75 -0
- package/src/modules/workspace/__tests__/testFixtures.ts +54 -0
- package/src/modules/workspace/__tests__/useWorkspaceState.test.ts +91 -0
- package/src/modules/workspace/components/EditorTabs.tsx +384 -0
- package/src/modules/workspace/components/FileExplorer.tsx +1068 -0
- package/src/modules/workspace/components/FileRoute.tsx +392 -0
- package/src/modules/workspace/components/FileViewer.tsx +906 -0
- package/src/modules/workspace/components/__tests__/EditorTabs.test.tsx +241 -0
- package/src/modules/workspace/components/__tests__/FileExplorer.test.tsx +666 -0
- package/src/modules/workspace/components/__tests__/FileRoute.test.tsx +330 -0
- package/src/modules/workspace/components/__tests__/FileViewer.test.tsx +492 -0
- package/src/modules/workspace/components/renderers/CsvRenderer.tsx +174 -0
- package/src/modules/workspace/components/renderers/DocxRenderer.tsx +87 -0
- package/src/modules/workspace/components/renderers/HtmlRenderer.tsx +231 -0
- package/src/modules/workspace/components/renderers/ImageRenderer.tsx +50 -0
- package/src/modules/workspace/components/renderers/KbMarkdownView.tsx +287 -0
- package/src/modules/workspace/components/renderers/MarkdownRenderer.tsx +252 -0
- package/src/modules/workspace/components/renderers/MermaidDiagram.tsx +268 -0
- package/src/modules/workspace/components/renderers/PdfRenderer.tsx +81 -0
- package/src/modules/workspace/components/renderers/TextRenderer.tsx +86 -0
- package/src/modules/workspace/components/renderers/ToolForm.tsx +404 -0
- package/src/modules/workspace/components/renderers/ToolRenderer.tsx +417 -0
- package/src/modules/workspace/components/renderers/XlsxRenderer.tsx +157 -0
- package/src/modules/workspace/components/renderers/__tests__/CsvRenderer.test.ts +57 -0
- package/src/modules/workspace/components/renderers/__tests__/KbMarkdownView.test.tsx +95 -0
- package/src/modules/workspace/components/renderers/__tests__/MarkdownRenderer.test.tsx +328 -0
- package/src/modules/workspace/components/renderers/__tests__/PdfRenderer.test.tsx +167 -0
- package/src/modules/workspace/components/renderers/__tests__/htmlSandbox.test.ts +318 -0
- package/src/modules/workspace/components/renderers/csvUtils.ts +76 -0
- package/src/modules/workspace/components/renderers/htmlSandbox.ts +332 -0
- package/src/modules/workspace/components/renderers/index.ts +39 -0
- package/src/modules/workspace/components/renderers/types.ts +27 -0
- package/src/modules/workspace/hooks/__tests__/useWorkspaceState.test.tsx +298 -0
- package/src/modules/workspace/hooks/useWorkspaceState.ts +1315 -0
- package/src/modules/workspace/routing/__tests__/kb-routes.test.tsx +99 -0
- package/src/modules/workspace/routing/kb-routes.ts +266 -0
- package/src/modules/workspace/services/__tests__/workspace.api.test.ts +72 -0
- package/src/modules/workspace/services/workspace.api.ts +185 -0
- package/src/modules/workspace/state/workspace.context.ts +235 -0
- package/src/modules/workspace/utils/__tests__/frontmatter.test.ts +107 -0
- package/src/modules/workspace/utils/diff.ts +53 -0
- package/src/modules/workspace/utils/fileTree.ts +179 -0
- package/src/modules/workspace/utils/frontmatter.ts +80 -0
- package/src/modules/workspace/utils/readDroppedEntries.ts +119 -0
- package/src/modules/workspace/utils/tab-persistence.ts +23 -0
- package/src/shared/components/Dialog.tsx +198 -0
- package/src/shared/components/PageShell.tsx +51 -0
- package/src/shared/components/useModalLayer.ts +35 -0
- package/src/shared/markdown/Markdown.tsx +62 -0
- package/src/test-setup.ts +59 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
import { render, screen, fireEvent } from '@testing-library/react';
|
|
3
|
+
import { KbMarkdownView } from '../KbMarkdownView';
|
|
4
|
+
|
|
5
|
+
const SOURCE = `---
|
|
6
|
+
nodeType: "[Process](../NodeTypes/Process.md)"
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
Body text with an [Other Node](Other.md) link, a [Deep](Other.md#goal) anchor, and an [External](https://example.com) link.
|
|
10
|
+
`;
|
|
11
|
+
|
|
12
|
+
describe('KbMarkdownView', () => {
|
|
13
|
+
it('renders the body and a clickable frontmatter nodeType link', () => {
|
|
14
|
+
const onOpenFile = vi.fn();
|
|
15
|
+
render(<KbMarkdownView source={SOURCE} onOpenFile={onOpenFile} />);
|
|
16
|
+
|
|
17
|
+
expect(screen.getByText(/Body text with an/)).toBeInTheDocument();
|
|
18
|
+
fireEvent.click(screen.getByRole('link', { name: 'Process' }));
|
|
19
|
+
expect(onOpenFile).toHaveBeenCalledWith('../NodeTypes/Process.md');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('routes internal .md body links (incl. anchors) through onOpenFile', () => {
|
|
23
|
+
const onOpenFile = vi.fn();
|
|
24
|
+
render(<KbMarkdownView source={SOURCE} onOpenFile={onOpenFile} />);
|
|
25
|
+
|
|
26
|
+
fireEvent.click(screen.getByRole('link', { name: 'Other Node' }));
|
|
27
|
+
expect(onOpenFile).toHaveBeenLastCalledWith('Other.md');
|
|
28
|
+
|
|
29
|
+
fireEvent.click(screen.getByRole('link', { name: 'Deep' }));
|
|
30
|
+
expect(onOpenFile).toHaveBeenLastCalledWith('Other.md#goal');
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('routes id-links ([text](some-id)) through onOpenNodeId, not onOpenFile', () => {
|
|
34
|
+
const onOpenFile = vi.fn();
|
|
35
|
+
const onOpenNodeId = vi.fn();
|
|
36
|
+
render(
|
|
37
|
+
<KbMarkdownView
|
|
38
|
+
source={'Body with an [Availability SLA](bdl-gov-availability-sla) id-link.\n'}
|
|
39
|
+
onOpenFile={onOpenFile}
|
|
40
|
+
onOpenNodeId={onOpenNodeId}
|
|
41
|
+
/>,
|
|
42
|
+
);
|
|
43
|
+
fireEvent.click(screen.getByRole('link', { name: 'Availability SLA' }));
|
|
44
|
+
expect(onOpenNodeId).toHaveBeenCalledWith('bdl-gov-availability-sla');
|
|
45
|
+
expect(onOpenFile).not.toHaveBeenCalled();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('leaves id-links inert when onOpenNodeId is omitted (e.g. the embed)', () => {
|
|
49
|
+
const onOpenFile = vi.fn();
|
|
50
|
+
render(<KbMarkdownView source={'An [SLA](bdl-gov-availability-sla) link.\n'} onOpenFile={onOpenFile} />);
|
|
51
|
+
// The id-link must not render as a clickable link at all — not just be a no-op on click.
|
|
52
|
+
expect(screen.queryByRole('link', { name: 'SLA' })).toBeNull();
|
|
53
|
+
expect(screen.getByText('SLA')).toBeInTheDocument();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('leaves external links as plain anchors (no onOpenFile)', () => {
|
|
57
|
+
const onOpenFile = vi.fn();
|
|
58
|
+
render(<KbMarkdownView source={SOURCE} onOpenFile={onOpenFile} />);
|
|
59
|
+
|
|
60
|
+
const ext = screen.getByRole('link', { name: 'External' });
|
|
61
|
+
expect(ext).toHaveAttribute('href', 'https://example.com');
|
|
62
|
+
fireEvent.click(ext);
|
|
63
|
+
expect(onOpenFile).not.toHaveBeenCalled();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('shows a per-heading copy button that copies the heading deep-link', async () => {
|
|
67
|
+
const writeText = vi.fn().mockResolvedValue(undefined);
|
|
68
|
+
Object.defineProperty(navigator, 'clipboard', { value: { writeText }, configurable: true });
|
|
69
|
+
const onOpenFile = vi.fn();
|
|
70
|
+
render(
|
|
71
|
+
<KbMarkdownView
|
|
72
|
+
source={'# Goal\nThe goal.\n'}
|
|
73
|
+
onOpenFile={onOpenFile}
|
|
74
|
+
headingLink={(slug) => `https://ide.bevel.software/workspace/b/x.md#${slug}`}
|
|
75
|
+
/>,
|
|
76
|
+
);
|
|
77
|
+
const btn = screen.getByRole('button', { name: /copy link to this heading/i });
|
|
78
|
+
fireEvent.click(btn);
|
|
79
|
+
expect(writeText).toHaveBeenCalledWith('https://ide.bevel.software/workspace/b/x.md#goal');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('omits the copy button when no headingLink is provided', () => {
|
|
83
|
+
const onOpenFile = vi.fn();
|
|
84
|
+
render(<KbMarkdownView source={'# Goal\nThe goal.\n'} onOpenFile={onOpenFile} />);
|
|
85
|
+
expect(screen.queryByRole('button', { name: /copy link to this heading/i })).toBeNull();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('renders inline HTML details blocks (Source of Information)', () => {
|
|
89
|
+
const onOpenFile = vi.fn();
|
|
90
|
+
const src = `# Goal\nThe goal.\n\n<details><summary>Source of Information</summary>\n\n1. PROD-1 — goal (2026-06-09)\n\n</details>\n`;
|
|
91
|
+
render(<KbMarkdownView source={src} onOpenFile={onOpenFile} />);
|
|
92
|
+
// rehype-raw turns the <details> into a real element rather than literal text.
|
|
93
|
+
expect(screen.getByText('Source of Information').tagName.toLowerCase()).toBe('summary');
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
|
|
3
|
+
import userEvent from '@testing-library/user-event';
|
|
4
|
+
import { MemoryRouter } from 'react-router-dom';
|
|
5
|
+
import { WorkspaceContext, type WorkspaceContextValue } from '../../../state/workspace.context';
|
|
6
|
+
import { makeWorkspaceFixture } from '../../../__tests__/testFixtures';
|
|
7
|
+
import { GitContext, type GitContextValue } from '../../../../git/state/git.context';
|
|
8
|
+
import { MarkdownRenderer } from '../MarkdownRenderer';
|
|
9
|
+
|
|
10
|
+
// Capture navigation so we can assert link clicks resolve + open the right file.
|
|
11
|
+
const navMock = vi.hoisted(() => ({ openFile: vi.fn() }));
|
|
12
|
+
vi.mock('../../../routing/kb-routes', async (importActual) => ({
|
|
13
|
+
// Keep the real `resolveRelativePath` (and any other pure helpers) so link
|
|
14
|
+
// resolution is exercised for real; only the navigation hook is stubbed.
|
|
15
|
+
...(await importActual<typeof import('../../../routing/kb-routes')>()),
|
|
16
|
+
useFileNav: () => ({ openFile: navMock.openFile }),
|
|
17
|
+
KB_ROUTE_PREFIX: '/workspace',
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
function makeGit(): GitContextValue {
|
|
21
|
+
return {
|
|
22
|
+
status: { branch: 'alice/draft', hasUpstream: true, unmergedFromUpstream: false },
|
|
23
|
+
branches: [],
|
|
24
|
+
availability: 'ready',
|
|
25
|
+
lastError: null,
|
|
26
|
+
refreshStatus: async () => null,
|
|
27
|
+
refreshBranches: async () => {},
|
|
28
|
+
createBranch: async () => {},
|
|
29
|
+
deleteBranch: async () => {},
|
|
30
|
+
pull: async () => {},
|
|
31
|
+
fetchForkBase: async () => null,
|
|
32
|
+
revert: async () => ({ sha: 'a', authorName: 'n', authorEmail: 'e', subject: 's', committedAt: '2026-04-20T00:00:00.000Z' }),
|
|
33
|
+
fetchFileHistory: async () => [],
|
|
34
|
+
fetchFileDiff: async () => '',
|
|
35
|
+
fetchFileComparison: async () => '',
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function makeWorkspace(): WorkspaceContextValue {
|
|
40
|
+
const tab = {
|
|
41
|
+
path: 'Knowledge/Foo.md',
|
|
42
|
+
content: '# Hello',
|
|
43
|
+
savedContent: '# Hello',
|
|
44
|
+
isDirty: false,
|
|
45
|
+
pendingFileContent: null,
|
|
46
|
+
};
|
|
47
|
+
return makeWorkspaceFixture({
|
|
48
|
+
openTabs: [tab],
|
|
49
|
+
activeTab: tab,
|
|
50
|
+
openFilePath: 'Knowledge/Foo.md',
|
|
51
|
+
openFileContent: '# Hello',
|
|
52
|
+
openFileSavedContent: '# Hello',
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function renderMarkdown(
|
|
57
|
+
onSave: (content: string) => Promise<void>,
|
|
58
|
+
hooks: {
|
|
59
|
+
onDirtyChange?: (dirty: boolean) => void;
|
|
60
|
+
onSaveStateChange?: (state: 'idle' | 'saving' | 'error') => void;
|
|
61
|
+
} = {},
|
|
62
|
+
) {
|
|
63
|
+
return render(
|
|
64
|
+
<MemoryRouter>
|
|
65
|
+
<WorkspaceContext.Provider value={makeWorkspace()}>
|
|
66
|
+
<GitContext.Provider value={makeGit()}>
|
|
67
|
+
<MarkdownRenderer
|
|
68
|
+
content="hello"
|
|
69
|
+
filePath="Knowledge/Foo.md"
|
|
70
|
+
onSave={onSave}
|
|
71
|
+
onDirtyChange={hooks.onDirtyChange}
|
|
72
|
+
onSaveStateChange={hooks.onSaveStateChange}
|
|
73
|
+
/>
|
|
74
|
+
</GitContext.Provider>
|
|
75
|
+
</WorkspaceContext.Provider>
|
|
76
|
+
</MemoryRouter>,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
describe('MarkdownRenderer', () => {
|
|
81
|
+
// Mode is now fully driven by the parent's `readOnly` prop. `renderMarkdown`
|
|
82
|
+
// omits the prop, so the default (`readOnly={false}`) renders the textarea
|
|
83
|
+
// directly — no internal "Edit" toggle to click. The renderer's old
|
|
84
|
+
// edit/preview tabs were removed to avoid conflicting with the FileViewer's
|
|
85
|
+
// top-level Edit/Done button.
|
|
86
|
+
|
|
87
|
+
it('does NOT save on blur', async () => {
|
|
88
|
+
// Auto-save-on-blur was removed when multi-tabs landed: with cache-per-tab,
|
|
89
|
+
// every tab click blurs the textarea, and silently flushing to disk on
|
|
90
|
+
// every switch is the wrong behavior. Saves must be explicit (Cmd+S or a
|
|
91
|
+
// closeTab confirm).
|
|
92
|
+
const onSave = vi.fn(async () => {});
|
|
93
|
+
renderMarkdown(onSave);
|
|
94
|
+
|
|
95
|
+
const textarea = screen.getByRole('textbox');
|
|
96
|
+
fireEvent.change(textarea, { target: { value: 'hello world' } });
|
|
97
|
+
fireEvent.blur(textarea);
|
|
98
|
+
|
|
99
|
+
// Wait a tick to make sure no async save fires.
|
|
100
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
101
|
+
expect(onSave).not.toHaveBeenCalled();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('saves on Ctrl/Cmd+S', async () => {
|
|
105
|
+
const user = userEvent.setup();
|
|
106
|
+
const onSave = vi.fn(async () => {});
|
|
107
|
+
renderMarkdown(onSave);
|
|
108
|
+
|
|
109
|
+
const textarea = screen.getByRole('textbox');
|
|
110
|
+
await user.type(textarea, ' world');
|
|
111
|
+
await user.keyboard('{Control>}s{/Control}');
|
|
112
|
+
|
|
113
|
+
await waitFor(() => {
|
|
114
|
+
expect(onSave).toHaveBeenCalledWith('hello world');
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('keeps edits dirty and shows a stable error message after save failure (does not leak raw error)', async () => {
|
|
119
|
+
const user = userEvent.setup();
|
|
120
|
+
const onSave = vi.fn(async () => {
|
|
121
|
+
// Raw error contains text that would be unfriendly / leaky to render
|
|
122
|
+
// — the renderer must not surface it; only a stable user-facing
|
|
123
|
+
// message belongs in the DOM.
|
|
124
|
+
throw new Error('lock-not-held: internal path /tmp/foo');
|
|
125
|
+
});
|
|
126
|
+
renderMarkdown(onSave);
|
|
127
|
+
|
|
128
|
+
const textarea = screen.getByRole('textbox');
|
|
129
|
+
await user.type(textarea, ' world');
|
|
130
|
+
await user.keyboard('{Control>}s{/Control}');
|
|
131
|
+
|
|
132
|
+
await waitFor(() => {
|
|
133
|
+
expect(onSave).toHaveBeenCalledWith('hello world');
|
|
134
|
+
});
|
|
135
|
+
// Stable, user-facing message — backend detail is in console only.
|
|
136
|
+
const errorNode = await screen.findByRole('alert');
|
|
137
|
+
expect(errorNode).toHaveTextContent("Couldn't save your changes. Try again in a moment.");
|
|
138
|
+
expect(screen.queryByText(/lock-not-held/)).toBeNull();
|
|
139
|
+
expect(screen.queryByText(/\/tmp\/foo/)).toBeNull();
|
|
140
|
+
// Dirty buffer survives — the user's edits are still in the textarea
|
|
141
|
+
// so they can retry without losing work.
|
|
142
|
+
expect((screen.getByRole('textbox') as HTMLTextAreaElement).value).toBe('hello world');
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
// --- Link rendering in preview (readOnly) mode ---
|
|
146
|
+
|
|
147
|
+
function renderPreview(content: string, filePath: string) {
|
|
148
|
+
return render(
|
|
149
|
+
<MemoryRouter>
|
|
150
|
+
<WorkspaceContext.Provider value={makeWorkspace()}>
|
|
151
|
+
<GitContext.Provider value={makeGit()}>
|
|
152
|
+
<MarkdownRenderer content={content} filePath={filePath} onSave={async () => {}} readOnly />
|
|
153
|
+
</GitContext.Provider>
|
|
154
|
+
</WorkspaceContext.Provider>
|
|
155
|
+
</MemoryRouter>,
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
it('renders a nodeType frontmatter value as a clickable link that navigates', async () => {
|
|
160
|
+
navMock.openFile.mockClear();
|
|
161
|
+
const user = userEvent.setup();
|
|
162
|
+
renderPreview(
|
|
163
|
+
'---\nnodeType: "[Process](../NodeTypes/Process.md)"\n---\n\n# Body',
|
|
164
|
+
'Knowledge/Sub/Foo.md',
|
|
165
|
+
);
|
|
166
|
+
const link = screen.getByRole('link', { name: 'Process' });
|
|
167
|
+
await user.click(link);
|
|
168
|
+
expect(navMock.openFile).toHaveBeenCalledWith('Knowledge/NodeTypes/Process.md');
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('renders a bare body link whose path contains spaces as a working link', async () => {
|
|
172
|
+
navMock.openFile.mockClear();
|
|
173
|
+
const user = userEvent.setup();
|
|
174
|
+
renderPreview('[Open](Some File.md)', 'Knowledge/Foo.md');
|
|
175
|
+
const link = screen.getByRole('link', { name: 'Open' });
|
|
176
|
+
await user.click(link);
|
|
177
|
+
expect(navMock.openFile).toHaveBeenCalledWith('Knowledge/Some File.md');
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('renders an angle-bracketed body link with spaces as a working link', async () => {
|
|
181
|
+
navMock.openFile.mockClear();
|
|
182
|
+
const user = userEvent.setup();
|
|
183
|
+
renderPreview('[Open](<Some File.md>)', 'Knowledge/Foo.md');
|
|
184
|
+
const link = screen.getByRole('link', { name: 'Open' });
|
|
185
|
+
await user.click(link);
|
|
186
|
+
expect(navMock.openFile).toHaveBeenCalledWith('Knowledge/Some File.md');
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
// --- Background autosave must not disturb the editing UI (BEVA ticket) ---
|
|
190
|
+
|
|
191
|
+
// The autosave checkpoint round-trips the buffer back through the `content`
|
|
192
|
+
// prop and advances `savedContent`; on commit the bytes can be line-ending-
|
|
193
|
+
// normalized so they differ slightly from what's in the textarea. The editor
|
|
194
|
+
// must treat itself as the source of truth while editing and NOT re-assign the
|
|
195
|
+
// textarea from that echo — doing so yanked the caret to the end and scrolled
|
|
196
|
+
// the document to the top ("it always jumped to the start of the document").
|
|
197
|
+
function renderEditable(content: string, savedContent: string) {
|
|
198
|
+
const onSave = vi.fn(async () => {});
|
|
199
|
+
const ui = (c: string, s: string) => (
|
|
200
|
+
<MemoryRouter>
|
|
201
|
+
<WorkspaceContext.Provider value={makeWorkspace()}>
|
|
202
|
+
<GitContext.Provider value={makeGit()}>
|
|
203
|
+
<MarkdownRenderer
|
|
204
|
+
content={c}
|
|
205
|
+
savedContent={s}
|
|
206
|
+
filePath="Knowledge/Foo.md"
|
|
207
|
+
onSave={onSave}
|
|
208
|
+
readOnly={false}
|
|
209
|
+
/>
|
|
210
|
+
</GitContext.Provider>
|
|
211
|
+
</WorkspaceContext.Provider>
|
|
212
|
+
</MemoryRouter>
|
|
213
|
+
);
|
|
214
|
+
const utils = render(ui(content, savedContent));
|
|
215
|
+
return { ...utils, rerender: (c: string, s: string) => utils.rerender(ui(c, s)) };
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
it('keeps the editing caret and buffer stable when an autosave advances savedContent + re-emits normalized content', () => {
|
|
219
|
+
const buffer = 'line A\nline B\nline C';
|
|
220
|
+
const { rerender } = renderEditable(buffer, buffer);
|
|
221
|
+
const textarea = screen.getByRole('textbox') as HTMLTextAreaElement;
|
|
222
|
+
textarea.focus();
|
|
223
|
+
// Caret parked in the middle of "line B".
|
|
224
|
+
textarea.setSelectionRange(9, 9);
|
|
225
|
+
|
|
226
|
+
// Autosave round-trip: checkpoint commits the buffer, the parent re-renders
|
|
227
|
+
// with savedContent advanced to match AND content re-emitted with a commit-
|
|
228
|
+
// time trailing-newline normalization (bytes differ from the live buffer).
|
|
229
|
+
const normalized = `${buffer}\n`;
|
|
230
|
+
rerender(normalized, normalized);
|
|
231
|
+
|
|
232
|
+
// The editor owns `value` while editing: no overwrite, no caret jump.
|
|
233
|
+
expect(textarea.value).toBe(buffer);
|
|
234
|
+
expect(textarea.selectionStart).toBe(9);
|
|
235
|
+
expect(textarea.selectionEnd).toBe(9);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it('still mirrors new content into the buffer in read-only preview mode (external updates show immediately)', () => {
|
|
239
|
+
const onSave = vi.fn(async () => {});
|
|
240
|
+
const ui = (content: string) => (
|
|
241
|
+
<MemoryRouter>
|
|
242
|
+
<WorkspaceContext.Provider value={makeWorkspace()}>
|
|
243
|
+
<GitContext.Provider value={makeGit()}>
|
|
244
|
+
<MarkdownRenderer content={content} filePath="Knowledge/Foo.md" onSave={onSave} readOnly />
|
|
245
|
+
</GitContext.Provider>
|
|
246
|
+
</WorkspaceContext.Provider>
|
|
247
|
+
</MemoryRouter>
|
|
248
|
+
);
|
|
249
|
+
const { rerender } = render(ui('# One'));
|
|
250
|
+
expect(screen.getByText('One')).toBeInTheDocument();
|
|
251
|
+
rerender(ui('# Two'));
|
|
252
|
+
expect(screen.getByText('Two')).toBeInTheDocument();
|
|
253
|
+
expect(screen.queryByText('One')).toBeNull();
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it('keeps the save-state strip while a manual save is pending, even when an autosave echo advances savedContent', async () => {
|
|
257
|
+
// A background autosave checkpoint can advance `content`/`savedContent`
|
|
258
|
+
// (the parent re-renders) while the user's explicit Ctrl/Cmd+S is still
|
|
259
|
+
// in flight. The transient-save-UI reset effect must NOT flip 'saving'
|
|
260
|
+
// back to 'idle' in that window — `save()` owns the exit from 'saving'.
|
|
261
|
+
const user = userEvent.setup();
|
|
262
|
+
let resolveSave!: () => void;
|
|
263
|
+
const onSave = vi.fn(() => new Promise<void>((res) => { resolveSave = res; }));
|
|
264
|
+
const onSaveStateChange = vi.fn();
|
|
265
|
+
const buffer = 'draft body';
|
|
266
|
+
const ui = (c: string, s: string) => (
|
|
267
|
+
<MemoryRouter>
|
|
268
|
+
<WorkspaceContext.Provider value={makeWorkspace()}>
|
|
269
|
+
<GitContext.Provider value={makeGit()}>
|
|
270
|
+
<MarkdownRenderer
|
|
271
|
+
content={c}
|
|
272
|
+
savedContent={s}
|
|
273
|
+
filePath="Knowledge/Foo.md"
|
|
274
|
+
onSave={onSave}
|
|
275
|
+
onSaveStateChange={onSaveStateChange}
|
|
276
|
+
readOnly={false}
|
|
277
|
+
/>
|
|
278
|
+
</GitContext.Provider>
|
|
279
|
+
</WorkspaceContext.Provider>
|
|
280
|
+
</MemoryRouter>
|
|
281
|
+
);
|
|
282
|
+
const { rerender } = render(ui(buffer, buffer));
|
|
283
|
+
const textarea = screen.getByRole('textbox') as HTMLTextAreaElement;
|
|
284
|
+
await user.type(textarea, ' edit');
|
|
285
|
+
const edited = textarea.value;
|
|
286
|
+
|
|
287
|
+
await user.keyboard('{Control>}s{/Control}');
|
|
288
|
+
await waitFor(() => expect(onSave).toHaveBeenCalledWith(edited));
|
|
289
|
+
await waitFor(() => expect(onSaveStateChange).toHaveBeenLastCalledWith('saving'));
|
|
290
|
+
|
|
291
|
+
// Autosave echo lands mid-save: savedContent advances, content re-emitted.
|
|
292
|
+
// The strip must stay 'saving' — no premature 'idle'.
|
|
293
|
+
rerender(ui(edited, edited));
|
|
294
|
+
expect(onSaveStateChange).toHaveBeenLastCalledWith('saving');
|
|
295
|
+
|
|
296
|
+
// Once the save settles, `save()` clears the strip to idle.
|
|
297
|
+
resolveSave();
|
|
298
|
+
await waitFor(() => expect(onSaveStateChange).toHaveBeenLastCalledWith('idle'));
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
it('clears dirty state after successful save', async () => {
|
|
302
|
+
const user = userEvent.setup();
|
|
303
|
+
const onSave = vi.fn(async () => {});
|
|
304
|
+
const onDirtyChange = vi.fn();
|
|
305
|
+
const onSaveStateChange = vi.fn();
|
|
306
|
+
renderMarkdown(onSave, { onDirtyChange, onSaveStateChange });
|
|
307
|
+
|
|
308
|
+
const textarea = screen.getByRole('textbox');
|
|
309
|
+
await user.type(textarea, ' world');
|
|
310
|
+
|
|
311
|
+
await waitFor(() => {
|
|
312
|
+
expect(onDirtyChange).toHaveBeenCalledWith(true);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
await user.keyboard('{Control>}s{/Control}');
|
|
316
|
+
|
|
317
|
+
await waitFor(() => {
|
|
318
|
+
expect(onSave).toHaveBeenCalledWith('hello world');
|
|
319
|
+
});
|
|
320
|
+
await waitFor(() => {
|
|
321
|
+
expect(onDirtyChange).toHaveBeenLastCalledWith(false);
|
|
322
|
+
});
|
|
323
|
+
expect(onSaveStateChange.mock.calls.map((c) => c[0])).toContain('saving');
|
|
324
|
+
expect(onSaveStateChange).toHaveBeenLastCalledWith('idle');
|
|
325
|
+
// Error live region should NOT render after a successful save.
|
|
326
|
+
expect(screen.queryByRole('alert')).toBeNull();
|
|
327
|
+
});
|
|
328
|
+
});
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { render, screen, waitFor } from '@testing-library/react';
|
|
3
|
+
import {
|
|
4
|
+
WorkspaceContext,
|
|
5
|
+
type WorkspaceContextValue,
|
|
6
|
+
} from '../../../state/workspace.context';
|
|
7
|
+
import { PdfRenderer } from '../PdfRenderer';
|
|
8
|
+
|
|
9
|
+
// Minimal WorkspaceContext: PdfRenderer only reads `workspaceId`. Casting the
|
|
10
|
+
// stub via `unknown` keeps the test focused — the full context shape has 40+
|
|
11
|
+
// fields, none of which PdfRenderer touches.
|
|
12
|
+
function workspaceCtx(workspaceId: string | null): WorkspaceContextValue {
|
|
13
|
+
return { workspaceId } as unknown as WorkspaceContextValue;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const baseProps = {
|
|
17
|
+
filePath: 'Inbox/brief.pdf',
|
|
18
|
+
content: '',
|
|
19
|
+
onSave: async () => {},
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
function renderWithWorkspace(workspaceId: string | null = 'ws-1') {
|
|
23
|
+
return render(
|
|
24
|
+
<WorkspaceContext.Provider value={workspaceCtx(workspaceId)}>
|
|
25
|
+
<PdfRenderer {...baseProps} />
|
|
26
|
+
</WorkspaceContext.Provider>,
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let createObjectURLSpy: ReturnType<typeof vi.fn>;
|
|
31
|
+
let revokeObjectURLSpy: ReturnType<typeof vi.fn>;
|
|
32
|
+
const objectUrls: string[] = [];
|
|
33
|
+
|
|
34
|
+
// jsdom does not implement URL.createObjectURL / revokeObjectURL — install
|
|
35
|
+
// them as own-properties on the global URL class. We must NOT replace the
|
|
36
|
+
// URL global itself (vi.stubGlobal('URL', …)) because Response/Blob/fetch
|
|
37
|
+
// internals construct `new URL(…)` and would break.
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
objectUrls.length = 0;
|
|
40
|
+
createObjectURLSpy = vi.fn(() => {
|
|
41
|
+
const url = `blob:fake/${objectUrls.length}`;
|
|
42
|
+
objectUrls.push(url);
|
|
43
|
+
return url;
|
|
44
|
+
});
|
|
45
|
+
revokeObjectURLSpy = vi.fn();
|
|
46
|
+
(URL as unknown as { createObjectURL: typeof createObjectURLSpy }).createObjectURL =
|
|
47
|
+
createObjectURLSpy;
|
|
48
|
+
(URL as unknown as { revokeObjectURL: typeof revokeObjectURLSpy }).revokeObjectURL =
|
|
49
|
+
revokeObjectURLSpy;
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
afterEach(() => {
|
|
53
|
+
delete (URL as unknown as { createObjectURL?: unknown }).createObjectURL;
|
|
54
|
+
delete (URL as unknown as { revokeObjectURL?: unknown }).revokeObjectURL;
|
|
55
|
+
vi.unstubAllGlobals();
|
|
56
|
+
vi.restoreAllMocks();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe('PdfRenderer', () => {
|
|
60
|
+
it('fetches the PDF and renders an iframe with the issued blob URL', async () => {
|
|
61
|
+
const pdfBytes = new Blob([new Uint8Array([0x25, 0x50, 0x44, 0x46])], {
|
|
62
|
+
type: 'application/pdf',
|
|
63
|
+
});
|
|
64
|
+
const fetchSpy = vi.fn<typeof fetch>(async () => new Response(pdfBytes, { status: 200 }));
|
|
65
|
+
vi.stubGlobal('fetch', fetchSpy);
|
|
66
|
+
|
|
67
|
+
renderWithWorkspace('ws-1');
|
|
68
|
+
|
|
69
|
+
const iframe = await waitFor(() => screen.getByTitle('Inbox/brief.pdf'));
|
|
70
|
+
expect(iframe.tagName).toBe('IFRAME');
|
|
71
|
+
expect((iframe as HTMLIFrameElement).src).toBe(objectUrls[0]);
|
|
72
|
+
|
|
73
|
+
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
|
74
|
+
const calledUrl = String(fetchSpy.mock.calls[0]![0]);
|
|
75
|
+
expect(calledUrl).toContain('/api/workspace/ws-1/file/raw');
|
|
76
|
+
expect(calledUrl).toContain('path=Inbox%2Fbrief.pdf');
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('shows the loading state until the blob resolves', async () => {
|
|
80
|
+
let resolveFetch!: (res: Response) => void;
|
|
81
|
+
vi.stubGlobal(
|
|
82
|
+
'fetch',
|
|
83
|
+
vi.fn(
|
|
84
|
+
() =>
|
|
85
|
+
new Promise<Response>((resolve) => {
|
|
86
|
+
resolveFetch = resolve;
|
|
87
|
+
}),
|
|
88
|
+
),
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
renderWithWorkspace('ws-1');
|
|
92
|
+
expect(screen.getByText('Loading PDF...')).toBeInTheDocument();
|
|
93
|
+
|
|
94
|
+
resolveFetch(
|
|
95
|
+
new Response(new Blob([new Uint8Array([0x25])], { type: 'application/pdf' }), {
|
|
96
|
+
status: 200,
|
|
97
|
+
}),
|
|
98
|
+
);
|
|
99
|
+
await waitFor(() => screen.getByTitle('Inbox/brief.pdf'));
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('renders an error message when the fetch returns a non-OK response', async () => {
|
|
103
|
+
vi.stubGlobal(
|
|
104
|
+
'fetch',
|
|
105
|
+
vi.fn(async () => new Response('not found', { status: 404 })),
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
renderWithWorkspace('ws-1');
|
|
109
|
+
await waitFor(() =>
|
|
110
|
+
expect(screen.getByText(/Failed to load PDF \(HTTP 404\)/)).toBeInTheDocument(),
|
|
111
|
+
);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('renders an error message when fetch throws (network failure)', async () => {
|
|
115
|
+
vi.stubGlobal(
|
|
116
|
+
'fetch',
|
|
117
|
+
vi.fn(async () => {
|
|
118
|
+
throw new Error('network down');
|
|
119
|
+
}),
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
renderWithWorkspace('ws-1');
|
|
123
|
+
await waitFor(() => expect(screen.getByText('network down')).toBeInTheDocument());
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('revokes the object URL on unmount', async () => {
|
|
127
|
+
const pdfBytes = new Blob([new Uint8Array([0x25])], {
|
|
128
|
+
type: 'application/pdf',
|
|
129
|
+
});
|
|
130
|
+
vi.stubGlobal('fetch', vi.fn(async () => new Response(pdfBytes, { status: 200 })));
|
|
131
|
+
|
|
132
|
+
const { unmount } = renderWithWorkspace('ws-1');
|
|
133
|
+
await waitFor(() => screen.getByTitle('Inbox/brief.pdf'));
|
|
134
|
+
|
|
135
|
+
expect(createObjectURLSpy).toHaveBeenCalledTimes(1);
|
|
136
|
+
unmount();
|
|
137
|
+
expect(revokeObjectURLSpy).toHaveBeenCalledWith(objectUrls[0]);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('does not fetch when workspaceId is null', async () => {
|
|
141
|
+
const fetchSpy = vi.fn();
|
|
142
|
+
vi.stubGlobal('fetch', fetchSpy);
|
|
143
|
+
|
|
144
|
+
renderWithWorkspace(null);
|
|
145
|
+
|
|
146
|
+
// Give effects a chance to run.
|
|
147
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
148
|
+
expect(fetchSpy).not.toHaveBeenCalled();
|
|
149
|
+
expect(screen.getByText('Loading PDF...')).toBeInTheDocument();
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('URL-encodes filePath segments so paths with spaces and slashes work', async () => {
|
|
153
|
+
const pdfBytes = new Blob([new Uint8Array([0x25])], { type: 'application/pdf' });
|
|
154
|
+
const fetchSpy = vi.fn<typeof fetch>(async () => new Response(pdfBytes, { status: 200 }));
|
|
155
|
+
vi.stubGlobal('fetch', fetchSpy);
|
|
156
|
+
|
|
157
|
+
render(
|
|
158
|
+
<WorkspaceContext.Provider value={workspaceCtx('ws-1')}>
|
|
159
|
+
<PdfRenderer {...baseProps} filePath="My Folder/Q3 report.pdf" />
|
|
160
|
+
</WorkspaceContext.Provider>,
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
await waitFor(() => screen.getByTitle('My Folder/Q3 report.pdf'));
|
|
164
|
+
const calledUrl = String(fetchSpy.mock.calls[0]![0]);
|
|
165
|
+
expect(calledUrl).toContain('path=My%20Folder%2FQ3%20report.pdf');
|
|
166
|
+
});
|
|
167
|
+
});
|