@bevel-software/platform-core-frontend 0.33.0 → 0.34.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.
Files changed (59) hide show
  1. package/package.json +2 -2
  2. package/src/lib/__tests__/api.test.ts +28 -1
  3. package/src/lib/api.ts +4 -2
  4. package/src/modules/access/__tests__/ManageAccessDialog.add-row-picking.test.tsx +53 -1
  5. package/src/modules/access/api.ts +3 -4
  6. package/src/modules/access/components/ManageAccessDialog.tsx +7 -7
  7. package/src/modules/admin/__tests__/DirectoryGroupsPage.test.tsx +51 -0
  8. package/src/modules/admin/components/DirectoryGroupsPage.tsx +42 -5
  9. package/src/modules/admin/services/groups.api.ts +11 -1
  10. package/src/modules/agents/__tests__/useConnectAsChoice.test.ts +56 -0
  11. package/src/modules/agents/components/ConnectAsChoice.tsx +81 -0
  12. package/src/modules/agents/connect-as.ts +15 -0
  13. package/src/modules/agents/index.ts +5 -0
  14. package/src/modules/agents/services/agent-choices.api.ts +39 -0
  15. package/src/modules/agents/useConnectAsChoice.ts +66 -0
  16. package/src/modules/audit/__tests__/AuditLogPage.test.tsx +13 -0
  17. package/src/modules/audit/components/AuditLogPage.tsx +9 -0
  18. package/src/modules/audit/services/audit.api.ts +5 -0
  19. package/src/modules/auth/__tests__/AccountPage.test.tsx +21 -0
  20. package/src/modules/auth/__tests__/account.api.test.ts +69 -0
  21. package/src/modules/auth/components/AccountPage.tsx +8 -2
  22. package/src/modules/auth/services/account.api.ts +18 -2
  23. package/src/modules/git/__tests__/BranchSwitcher.test.tsx +0 -14
  24. package/src/modules/git/__tests__/git.api.test.ts +2 -2
  25. package/src/modules/git/hooks/__tests__/useGitState.deleteBranch.test.tsx +1 -1
  26. package/src/modules/library/__tests__/SkillPage.test.tsx +229 -24
  27. package/src/modules/library/components/skill-page/SkillFilePane.tsx +57 -9
  28. package/src/modules/library/components/skill-page/SkillPage.tsx +65 -25
  29. package/src/modules/review/components/MarkdownDiffViewer.tsx +23 -1
  30. package/src/modules/review/components/ReviewPanel.tsx +8 -2
  31. package/src/modules/review/components/__tests__/MarkdownDiffViewer.test.tsx +31 -0
  32. package/src/modules/secrets-vault/components/ConnectToolsPage.tsx +79 -16
  33. package/src/modules/secrets-vault/components/__tests__/ConnectToolsPage.test.tsx +112 -2
  34. package/src/modules/secrets-vault/services/connect.api.ts +20 -5
  35. package/src/modules/toolbar/components/ExternalAgentAccessPage.tsx +38 -4
  36. package/src/modules/toolbar/components/__tests__/ExternalAgentAccessPage.test.tsx +91 -2
  37. package/src/modules/toolbar/services/external-api-keys.api.ts +5 -2
  38. package/src/modules/workspace/components/__tests__/FileExplorer.test.tsx +26 -0
  39. package/src/modules/workspace/components/renderers/HtmlRenderer.tsx +40 -7
  40. package/src/modules/workspace/components/renderers/KbAppLink.tsx +56 -0
  41. package/src/modules/workspace/components/renderers/KbMarkdownView.tsx +30 -10
  42. package/src/modules/workspace/components/renderers/MarkdownRenderer.tsx +37 -40
  43. package/src/modules/workspace/components/renderers/__tests__/HtmlRenderer.links.test.tsx +185 -0
  44. package/src/modules/workspace/components/renderers/__tests__/MarkdownRenderer.sectionReclick.test.tsx +88 -0
  45. package/src/modules/workspace/components/renderers/__tests__/MarkdownRenderer.test.tsx +199 -29
  46. package/src/modules/workspace/components/renderers/__tests__/bridgeHarness.ts +37 -4
  47. package/src/modules/workspace/components/renderers/__tests__/htmlSandbox.test.ts +59 -0
  48. package/src/modules/workspace/components/renderers/__tests__/htmlViewsGuide.test.ts +15 -1
  49. package/src/modules/workspace/components/renderers/__tests__/liveDataReferencePage.test.ts +397 -0
  50. package/src/modules/workspace/components/renderers/htmlSandbox.ts +48 -15
  51. package/src/modules/workspace/components/renderers/htmlToolBridge.ts +19 -0
  52. package/src/modules/workspace/components/renderers/kbMarkdownPipeline.tsx +35 -28
  53. package/src/modules/workspace/components/renderers/scrollToHeading.ts +35 -0
  54. package/src/modules/workspace/hooks/__tests__/useLinksOut.test.ts +6 -0
  55. package/src/modules/workspace/hooks/useLinksOut.ts +12 -6
  56. package/src/modules/workspace/index.ts +21 -2
  57. package/src/modules/workspace/routing/__tests__/kb-routes.test.tsx +262 -25
  58. package/src/modules/workspace/routing/kb-routes.ts +221 -53
  59. package/src/modules/workspace/utils/fileTree.ts +1 -1
@@ -1,4 +1,5 @@
1
1
  import { useMemo } from 'react';
2
+ import { isExternalHref } from '../../../shared/markdown/hrefs';
2
3
 
3
4
  export interface LinkOut {
4
5
  /** The link's visible text. */
@@ -22,13 +23,18 @@ const MARKDOWN_LINK_RE = /\[([^\]]+)\]\(<?([^)>\s]+(?:\s[^)>]*)?)>?\)/g;
22
23
  * backlinks would need an index that does not exist. If one ever lands, the
23
24
  * rail gains a second section; it does not change this one.
24
25
  *
25
- * The predicate is `KbMarkdownView.tsx:239`'s — the real internal-link test
26
- * the renderer itself uses to decide what opens in the workspace:
26
+ * The predicate is the markdown pipeline's `.md` rule — the internal-link
27
+ * test the renderer uses for a page link where it has no link-target
28
+ * resolver:
27
29
  *
28
- * !href.startsWith('http') && !href.startsWith('#') && /\.md(#|$)/.test(href)
30
+ * !isExternalHref(href) && !href.startsWith('#') && /\.md(#|$)/.test(href)
29
31
  *
30
- * so the rail lists exactly the links that are clickable as files, and
31
- * nothing that would take the reader off the workspace.
32
+ * (a scheme, not a prefix: `http-notes.md` is a page of the workspace)
33
+ * so the rail lists the PAGES this one links to, and nothing that would take
34
+ * the reader off the workspace. It is narrower than the file page's clicks on
35
+ * purpose: with a resolver the pipeline also opens a link to an HTML page, a
36
+ * PDF or an image in a new tab, and follows a bare node-id link, none of
37
+ * which this rail lists — it is a list of pages, not of everything clickable.
32
38
  *
33
39
  * Non-markdown content yields `[]`, which the rail renders as no section
34
40
  * rather than an empty one.
@@ -43,7 +49,7 @@ export function useLinksOut(path: string | null, content: string | null): LinkOu
43
49
  for (const match of content.matchAll(MARKDOWN_LINK_RE)) {
44
50
  const label = match[1].trim();
45
51
  const target = match[2].trim();
46
- if (target.startsWith('http') || target.startsWith('#')) continue;
52
+ if (isExternalHref(target) || target.startsWith('#')) continue;
47
53
  if (!/\.md(#|$)/.test(target)) continue;
48
54
  // One row per destination: a node linked three times is one place you
49
55
  // can go, not three.
@@ -27,11 +27,18 @@
27
27
  * - `OpenPageInPlatformContext`: how an HTML page mounted there is taken
28
28
  * to the file page when it needs tools and the viewer picks Open in the
29
29
  * platform (a skill page provides it; without it the renderer opens the
30
- * file's own address).
30
+ * file's own address). `PageLinkHomeContext`: the file such a page's
31
+ * links resolve against, where its `filePath` is not one (a skill page).
31
32
  * - `useHtmlToolBridge`, `PageToolsContext` and their types: the page
32
33
  * tool bridge a distribution's own HTML renderer calls to answer a page's
33
34
  * `window.bevel.tools` exactly as the app's renderer does, and the
34
35
  * file page's provider its tests mount it under.
36
+ * - `useFileNav` and `useNodeIdNav` (with `KbLinkTarget`, `OpenLinkOptions`
37
+ * and `stripJunkBeforeKbDir`): where a link inside rendered content leads
38
+ * and how it is followed — the one rule for those links, which a surface
39
+ * rendering a file of the knowledge base outside the file page (a skill
40
+ * page, a review diff) follows too. `scrollToHeading`: how such a surface
41
+ * goes to a section of its own markdown file.
35
42
  */
36
43
  export {
37
44
  CanDownloadContext,
@@ -50,12 +57,24 @@ export {
50
57
  } from './components/renderers';
51
58
  export { RendererWorkspaceContext } from './components/renderers/rendererWorkspace';
52
59
  export { asksForFilePage, kbFilePageUrl, kbFileUrl, resolveKbHref } from './routing/kb-routes';
60
+ export { PageLinkHomeContext } from './components/renderers/htmlToolBridge';
61
+ export type { KbLinkTarget } from './components/renderers/KbAppLink';
62
+ export { scrollToHeading } from './components/renderers/scrollToHeading';
53
63
  // Reached by other modules; exported here so they stop importing the files.
54
64
  export { useCreatePage } from './hooks/useCreatePage';
55
65
  export { useMergedWorkspaceTree } from './hooks/useMergedWorkspaceTree';
56
66
  export { useOpenChangeRequests } from './hooks/useOpenChangeRequests';
57
67
  export { useWorkspaceImageResolver } from './hooks/useWorkspaceImageResolver';
58
- export { KB_ROUTE_PREFIX, openExternalHref, safeDecode, stripJunkBeforeKbDir, useFileNav, useNodeIdNav } from './routing/kb-routes';
68
+ export {
69
+ KB_ROUTE_PREFIX,
70
+ openExternalHref,
71
+ safeDecode,
72
+ stripJunkBeforeKbDir,
73
+ useFileNav,
74
+ useFileNodeId,
75
+ useNodeIdNav,
76
+ type OpenLinkOptions,
77
+ } from './routing/kb-routes';
59
78
  export { WorkspaceApiError, deleteFile, getOrCreateWorkspace, listFiles, readFile, writeFile } from './services/workspace.api';
60
79
  export type { SkillToolWarning } from './services/workspace.api';
61
80
  export { useEditablePage } from './state/editable-page';
@@ -1,19 +1,36 @@
1
- import { describe, it, expect, vi } from 'vitest';
1
+ import { describe, it, expect, vi, beforeEach, afterEach, type MockInstance } from 'vitest';
2
2
  import { renderHook } from '@testing-library/react';
3
3
  import { GitContext, type GitContextValue } from '../../../git/state/git.context';
4
4
  import { WorkspaceContext } from '../../state/workspace.context';
5
5
  import { makeWorkspaceFixture } from '../../__tests__/testFixtures';
6
- import { useFileNav, resolveKbHref } from '../kb-routes';
6
+ import { useFileNav, useNodeIdNav, resolveKbHref } from '../kb-routes';
7
7
 
8
8
  // Capture what openFile navigates to, and stand in for the URL it reads the
9
9
  // branch out of.
10
10
  const navigateMock = vi.hoisted(() => vi.fn());
11
- const routerMock = vi.hoisted(() => ({ pathname: '/' }));
11
+ const routerMock = vi.hoisted(() => ({ pathname: '/', search: '' }));
12
12
  vi.mock('react-router-dom', () => ({
13
13
  useNavigate: () => navigateMock,
14
- useLocation: () => ({ pathname: routerMock.pathname, search: '', hash: '', state: null, key: 'k' }),
14
+ useLocation: () => ({ pathname: routerMock.pathname, search: routerMock.search, hash: '', state: null, key: 'k' }),
15
15
  }));
16
16
 
17
+ // `window.open` answering with a tab, as a browser does when it opens one.
18
+ // A link inside a file opens its target there and never navigates this tab.
19
+ let openMock: MockInstance<typeof window.open>;
20
+ let openedTab: { opener: unknown };
21
+ beforeEach(() => {
22
+ navigateMock.mockClear();
23
+ routerMock.search = '';
24
+ openedTab = { opener: window };
25
+ openMock = vi.spyOn(window, 'open').mockImplementation(() => openedTab as unknown as Window);
26
+ });
27
+ afterEach(() => openMock.mockRestore());
28
+
29
+ function expectNewTab(url: string) {
30
+ expect(openMock).toHaveBeenCalledWith(url, '_blank');
31
+ expect(navigateMock).not.toHaveBeenCalled();
32
+ }
33
+
17
34
  function gitOnBranch(branch: string): GitContextValue {
18
35
  return {
19
36
  status: { branch, hasUpstream: true, unmergedFromUpstream: false },
@@ -42,8 +59,10 @@ function renderNav(
42
59
  branch: string,
43
60
  kbDirName: string | null = 'knowledge-base',
44
61
  pathname = '/',
62
+ search = '',
45
63
  ) {
46
64
  routerMock.pathname = pathname;
65
+ routerMock.search = search;
47
66
  return renderHook(() => useFileNav(), {
48
67
  wrapper: ({ children }) => (
49
68
  <GitContext.Provider value={gitOnBranch(branch)}>
@@ -121,11 +140,12 @@ describe('useFileNav.openFile', () => {
121
140
  );
122
141
  });
123
142
 
124
- it('opens a page on its FILE PAGE by an address that asks for it — a new tab carries no router state', () => {
125
- navigateMock.mockClear();
143
+ // A page's "Open in the platform", asked for from inside the page: a new
144
+ // tab, which carries no router state, so the ask rides in the address.
145
+ it('opens a page on its FILE PAGE in a new tab, by an address that asks for it', () => {
126
146
  const { result } = renderNav('main');
127
147
  result.current.openFilePage('knowledge-base/Skills/qa-bridge/view.html');
128
- expect(navigateMock).toHaveBeenCalledWith('/workspace/main/knowledge-base/Skills/qa-bridge/view.html?as=file');
148
+ expectNewTab('/workspace/main/knowledge-base/Skills/qa-bridge/view.html?as=file');
129
149
  });
130
150
 
131
151
  it('opens a tree path verbatim even when a folder inside it is named like the kbDirName', () => {
@@ -292,7 +312,7 @@ describe('useFileNav.openLink', () => {
292
312
  navigateMock.mockClear();
293
313
  const { result } = renderNav('alice/draft');
294
314
  result.current.openLink('Some%20File.md#goal', 'knowledge-base/Knowledge/Foo.md');
295
- expect(navigateMock).toHaveBeenCalledWith(
315
+ expectNewTab(
296
316
  '/workspace/alice%2Fdraft/knowledge-base/Knowledge/Some%20File.md#goal',
297
317
  );
298
318
  });
@@ -305,7 +325,23 @@ describe('useFileNav.openLink', () => {
305
325
  '/workspace/target-company-state/knowledge-base/x.md',
306
326
  'knowledge-base/Knowledge/Foo.md',
307
327
  );
308
- expect(navigateMock).toHaveBeenCalledWith('/workspace/target-company-state/knowledge-base/x.md');
328
+ expectNewTab('/workspace/target-company-state/knowledge-base/x.md');
329
+ });
330
+
331
+ // A new tab the reader asked for (a modified or middle click a host had to
332
+ // relay): every in-app destination opens in one at its plain address, a
333
+ // section of this file included, and `onSection` does not scroll this tab.
334
+ it('opens every in-app destination in a new tab when the reader asked for one', () => {
335
+ navigateMock.mockClear();
336
+ const { result } = renderNav('alice/draft');
337
+ const onSection = vi.fn();
338
+ result.current.openLink('#goal', 'knowledge-base/Knowledge/Foo.md', { newTab: true, onSection });
339
+ expectNewTab('/workspace/alice%2Fdraft/knowledge-base/Knowledge/Foo.md#goal');
340
+ openMock.mockClear();
341
+ result.current.openLink('Other.md', 'knowledge-base/Knowledge/Foo.md', { newTab: true, homeBranch: 'main', onSection });
342
+ expectNewTab('/workspace/main/knowledge-base/Knowledge/Other.md');
343
+ expect(onSection).not.toHaveBeenCalled();
344
+ expect(navigateMock).not.toHaveBeenCalled();
309
345
  });
310
346
 
311
347
  // Every caller reaches `openLink` by CANCELLING the browser's navigation
@@ -359,7 +395,7 @@ describe('useFileNav.openLink', () => {
359
395
  navigateMock.mockClear();
360
396
  const { result } = renderNav('alice/draft');
361
397
  result.current.openLink('Board.html', 'knowledge-base/Dashboards/a.html');
362
- expect(navigateMock).toHaveBeenCalledWith(
398
+ expectNewTab(
363
399
  '/workspace/alice%2Fdraft/knowledge-base/Dashboards/Board.html',
364
400
  );
365
401
  });
@@ -368,7 +404,7 @@ describe('useFileNav.openLink', () => {
368
404
  navigateMock.mockClear();
369
405
  const { result } = renderNav('alice/draft');
370
406
  result.current.openLink('../Reports/Q3.html#totals', 'knowledge-base/Dashboards/a.html');
371
- expect(navigateMock).toHaveBeenCalledWith(
407
+ expectNewTab(
372
408
  '/workspace/alice%2Fdraft/knowledge-base/Reports/Q3.html#totals',
373
409
  );
374
410
  });
@@ -381,7 +417,7 @@ describe('useFileNav.openLink', () => {
381
417
  navigateMock.mockClear();
382
418
  const { result } = renderNav('alice/draft');
383
419
  result.current.openLink('Missing.html', 'knowledge-base/Dashboards/a.html');
384
- expect(navigateMock).toHaveBeenCalledWith(
420
+ expectNewTab(
385
421
  '/workspace/alice%2Fdraft/knowledge-base/Dashboards/Missing.html',
386
422
  );
387
423
  });
@@ -393,7 +429,7 @@ describe('useFileNav.openLink', () => {
393
429
  navigateMock.mockClear();
394
430
  const { result } = renderNav('alice/draft');
395
431
  result.current.openLink('\n Board.html\n ', 'knowledge-base/Dashboards/a.html');
396
- expect(navigateMock).toHaveBeenCalledWith(
432
+ expectNewTab(
397
433
  '/workspace/alice%2Fdraft/knowledge-base/Dashboards/Board.html',
398
434
  );
399
435
  });
@@ -405,18 +441,16 @@ describe('useFileNav.openLink', () => {
405
441
  ' /workspace/bob%2Fmain/knowledge-base/Reports/Q3.html#totals ',
406
442
  'knowledge-base/Dashboards/a.html',
407
443
  );
408
- expect(navigateMock).toHaveBeenCalledWith(
444
+ expectNewTab(
409
445
  '/workspace/bob%2Fmain/knowledge-base/Reports/Q3.html#totals',
410
446
  );
411
447
  });
412
448
 
413
449
  it('keeps a padded same-document anchor on the file it sits in', () => {
414
- navigateMock.mockClear();
415
- const { result } = renderNav('alice/draft');
450
+ const { result } = renderNav('alice/draft', 'knowledge-base', '/workspace/alice%2Fdraft/knowledge-base/Dashboards/a.html');
416
451
  result.current.openLink(' #goal ', 'knowledge-base/Dashboards/a.html');
417
- expect(navigateMock).toHaveBeenCalledWith(
418
- '/workspace/alice%2Fdraft/knowledge-base/Dashboards/a.html#goal',
419
- );
452
+ expect(navigateMock).toHaveBeenCalledWith('/workspace/alice%2Fdraft/knowledge-base/Dashboards/a.html#goal');
453
+ expect(openMock).not.toHaveBeenCalled();
420
454
  });
421
455
 
422
456
  // `window.open` would strip the padding itself, but the address handed over
@@ -433,12 +467,99 @@ describe('useFileNav.openLink', () => {
433
467
 
434
468
  // A section link is a scroll, not a navigation to somewhere else.
435
469
  it('keeps a same-document anchor on the file it sits in', () => {
436
- navigateMock.mockClear();
437
- const { result } = renderNav('alice/draft');
470
+ const { result } = renderNav('alice/draft', 'knowledge-base', '/workspace/alice%2Fdraft/knowledge-base/Knowledge/Foo.md');
438
471
  result.current.openLink('#overview', 'knowledge-base/Knowledge/Foo.md');
439
- expect(navigateMock).toHaveBeenCalledWith(
440
- '/workspace/alice%2Fdraft/knowledge-base/Knowledge/Foo.md#overview',
441
- );
472
+ expect(navigateMock).toHaveBeenCalledWith('/workspace/alice%2Fdraft/knowledge-base/Knowledge/Foo.md#overview');
473
+ expect(openMock).not.toHaveBeenCalled();
474
+ });
475
+
476
+ // The section link moves this tab by its fragment ONLY: a node shown at its
477
+ // id URL stays there, and full screen (`?fullscreen=1`) stays on.
478
+ it('scrolls a section link in place on this tab\'s own address, query kept', () => {
479
+ const { result } = renderNav('alice/draft', 'knowledge-base', '/workspace/alice%2Fdraft/foo-node', '?fullscreen=1');
480
+ result.current.openLink('Foo.md#overview', 'knowledge-base/Knowledge/Foo.md');
481
+ expect(navigateMock).toHaveBeenCalledWith('/workspace/alice%2Fdraft/foo-node?fullscreen=1#overview');
482
+ expect(openMock).not.toHaveBeenCalled();
483
+ });
484
+
485
+ // The same file on ANOTHER branch is another file.
486
+ it('opens the same path on another branch in a new tab', () => {
487
+ const { result } = renderNav('alice/draft', 'knowledge-base', '/workspace/alice%2Fdraft/knowledge-base/Knowledge/Foo.md');
488
+ result.current.openLink('/workspace/main/knowledge-base/Knowledge/Foo.md#overview', 'knowledge-base/Knowledge/Foo.md');
489
+ expectNewTab('/workspace/main/knowledge-base/Knowledge/Foo.md#overview');
490
+ });
491
+
492
+ it('in full screen, opens another file at its plain address and leaves this tab alone', () => {
493
+ const { result } = renderNav('alice/draft', 'knowledge-base', '/workspace/alice%2Fdraft/knowledge-base/Dashboards/a.html', '?fullscreen=1');
494
+ result.current.openLink('../Knowledge/Plan.md', 'knowledge-base/Dashboards/a.html');
495
+ expectNewTab('/workspace/alice%2Fdraft/knowledge-base/Knowledge/Plan.md');
496
+ });
497
+
498
+ // The new tab is the app's own page, opened without `noopener` only so a
499
+ // refusal can be told from a success; the opener is cut the same moment.
500
+ it('severs the new tab\'s opener', () => {
501
+ const { result } = renderNav('alice/draft');
502
+ result.current.openLink('Board.html', 'knowledge-base/Dashboards/a.html');
503
+ expect(openedTab.opener).toBeNull();
504
+ });
505
+
506
+ it('reports a tab the browser refused to open, rather than swallowing the click', () => {
507
+ openMock.mockImplementation(() => null);
508
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
509
+ const { result } = renderNav('alice/draft');
510
+ result.current.openLink('Board.html', 'knowledge-base/Dashboards/a.html');
511
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining('/workspace/alice%2Fdraft/knowledge-base/Dashboards/Board.html'));
512
+ expect(navigateMock).not.toHaveBeenCalled();
513
+ warn.mockRestore();
514
+ });
515
+
516
+ // What an anchor carries is what the click opens.
517
+ it('answers linkTarget with the address and the tab openLink uses', () => {
518
+ const { result } = renderNav('alice/draft', 'knowledge-base', '/workspace/alice%2Fdraft/knowledge-base/Knowledge/Foo.md');
519
+ expect(result.current.linkTarget('Other.md', 'knowledge-base/Knowledge/Foo.md')).toEqual({
520
+ href: '/workspace/alice%2Fdraft/knowledge-base/Knowledge/Other.md',
521
+ newTab: true,
522
+ });
523
+ expect(result.current.linkTarget('#a', 'knowledge-base/Knowledge/Foo.md')).toEqual({
524
+ href: '/workspace/alice%2Fdraft/knowledge-base/Knowledge/Foo.md#a',
525
+ newTab: false,
526
+ });
527
+ expect(result.current.linkTarget('https://example.com', 'knowledge-base/Knowledge/Foo.md')).toBeNull();
528
+ });
529
+
530
+ // The skill page reads its files from the default branch whatever is
531
+ // checked out: told so, a relative link opens there, and a link to the
532
+ // file itself is still a section of this tab.
533
+ it('resolves a relative link on the home branch it is told the file was read from', () => {
534
+ const { result } = renderNav('alice/draft', 'knowledge-base', '/skills-and-tools/skills/newsletter');
535
+ const base = 'knowledge-base/Skills/newsletter/SKILL.md';
536
+ expect(result.current.linkTarget('ref.md', base, 'main')).toEqual({
537
+ href: '/workspace/main/knowledge-base/Skills/newsletter/ref.md',
538
+ newTab: true,
539
+ });
540
+ expect(result.current.linkTarget('SKILL.md#steps', base, 'main')).toEqual({
541
+ href: '/skills-and-tools/skills/newsletter#steps',
542
+ newTab: false,
543
+ });
544
+ result.current.openLink('ref.md', base, { homeBranch: 'main' });
545
+ expectNewTab('/workspace/main/knowledge-base/Skills/newsletter/ref.md');
546
+ });
547
+
548
+ // A host whose address does not scroll to a fragment (an HTML page's frame,
549
+ // the skill page) takes a section link itself: handed the fragment, while
550
+ // this tab stays where it is. Another file still opens a new tab.
551
+ it('hands a section link to the host that scrolls it, and moves nothing', () => {
552
+ navigateMock.mockClear();
553
+ const { result } = renderNav('main', 'knowledge-base', '/workspace/main/knowledge-base/Dashboards/Roadmap.html');
554
+ const base = 'knowledge-base/Dashboards/Roadmap.html';
555
+ const onSection = vi.fn();
556
+ result.current.openLink('Roadmap.html#q3', base, { onSection });
557
+ result.current.openLink('/workspace/main/knowledge-base/Dashboards/Roadmap.html#q4', base, { onSection });
558
+ expect(onSection.mock.calls).toEqual([['#q3'], ['#q4']]);
559
+ expect(navigateMock).not.toHaveBeenCalled();
560
+ result.current.openLink('Board.html#q3', base, { onSection });
561
+ expect(onSection).toHaveBeenCalledTimes(2);
562
+ expectNewTab('/workspace/main/knowledge-base/Dashboards/Board.html#q3');
442
563
  });
443
564
  });
444
565
 
@@ -482,7 +603,7 @@ describe('useFileNav: which branch a click lands on', () => {
482
603
  '/workspace/target-company-state/knowledge-base/Knowledge/Old.md',
483
604
  );
484
605
  result.current.openLink('./New.md', 'knowledge-base/Knowledge/Old.md');
485
- expect(navigateMock).toHaveBeenCalledWith(
606
+ expectNewTab(
486
607
  '/workspace/target-company-state/knowledge-base/Knowledge/New.md',
487
608
  );
488
609
  });
@@ -494,3 +615,119 @@ describe('useFileNav: which branch a click lands on', () => {
494
615
  expect(navigateMock).toHaveBeenCalledWith('/workspace/alice%2Fdraft/Knowledge/New.md');
495
616
  });
496
617
  });
618
+
619
+ /**
620
+ * An id-link is a link inside a file too. The new tab gets the node's id URL
621
+ * and resolves it there (its route answers not-found for an id the reader
622
+ * cannot see), so the tab opens inside the click rather than after a round
623
+ * trip, at the address a middle-click on the same anchor would open.
624
+ */
625
+ describe('useNodeIdNav', () => {
626
+ function renderNodeNav(pathname: string, search = '') {
627
+ routerMock.pathname = pathname;
628
+ routerMock.search = search;
629
+ return renderHook(() => useNodeIdNav(), {
630
+ wrapper: ({ children }) => (
631
+ <GitContext.Provider value={gitOnBranch('alice/draft')}>
632
+ <WorkspaceContext.Provider value={makeWorkspaceFixture({ kbDirName: 'knowledge-base' })}>
633
+ {children}
634
+ </WorkspaceContext.Provider>
635
+ </GitContext.Provider>
636
+ ),
637
+ });
638
+ }
639
+
640
+ it('opens another node in a new tab at its id URL, heading kept', () => {
641
+ const { result } = renderNodeNav('/workspace/alice%2Fdraft/knowledge-base/Knowledge/Foo.md');
642
+ result.current.openNodeId('plan-node#goal');
643
+ expectNewTab('/workspace/alice%2Fdraft/plan-node#goal');
644
+ });
645
+
646
+ it('in full screen, opens the plain id URL and leaves this tab alone', () => {
647
+ const { result } = renderNodeNav('/workspace/alice%2Fdraft/foo-node', '?fullscreen=1');
648
+ result.current.openNodeId('plan-node');
649
+ expectNewTab('/workspace/alice%2Fdraft/plan-node');
650
+ });
651
+
652
+ it('scrolls a link to the node on screen in place, address and query kept', () => {
653
+ const { result } = renderNodeNav('/workspace/alice%2Fdraft/foo-node', '?fullscreen=1');
654
+ result.current.openNodeId('foo-node#goal');
655
+ expect(navigateMock).toHaveBeenCalledWith('/workspace/alice%2Fdraft/foo-node?fullscreen=1#goal');
656
+ expect(openMock).not.toHaveBeenCalled();
657
+ });
658
+
659
+ // The node on screen told by its id: a tab still on the file's path URL —
660
+ // canonicalizing, or a `FileRoute` that never canonicalizes — keeps a link
661
+ // to it in place; any other id is still a new tab.
662
+ it('scrolls a link to the node on screen in place on its path URL when told its id', () => {
663
+ const path = '/workspace/alice%2Fdraft/knowledge-base/Knowledge/Foo.md';
664
+ const { result } = renderNodeNav(path, '?fullscreen=1');
665
+ expect(result.current.nodeIdTarget('foo-node#goal', 'foo-node')).toEqual({
666
+ href: `${path}?fullscreen=1#goal`,
667
+ newTab: false,
668
+ });
669
+ result.current.openNodeId('foo-node#goal', 'foo-node');
670
+ expect(navigateMock).toHaveBeenCalledWith(`${path}?fullscreen=1#goal`);
671
+ expect(openMock).not.toHaveBeenCalled();
672
+ navigateMock.mockClear();
673
+ result.current.openNodeId('plan-node', 'foo-node');
674
+ expectNewTab('/workspace/alice%2Fdraft/plan-node');
675
+ });
676
+
677
+ // A host whose address has no heading scroll (the skill page) is handed
678
+ // the fragment of a link to its own node and scrolls itself; the tab does
679
+ // not move. A link to any other node still opens a new tab.
680
+ it('hands a link to the node on screen to onSection, and moves nothing', () => {
681
+ const { result } = renderNodeNav('/skills-and-tools/skills/newsletter');
682
+ const onSection = vi.fn();
683
+ result.current.openNodeId('newsletter#steps', 'newsletter', { onSection });
684
+ expect(onSection).toHaveBeenCalledWith('#steps');
685
+ expect(navigateMock).not.toHaveBeenCalled();
686
+ expect(openMock).not.toHaveBeenCalled();
687
+ result.current.openNodeId('plan-node#goal', 'newsletter', { onSection });
688
+ expect(onSection).toHaveBeenCalledTimes(1);
689
+ expectNewTab('/workspace/alice%2Fdraft/plan-node#goal');
690
+ });
691
+
692
+ // A host that read its file from another branch than this tab's (the skill
693
+ // page reads the default branch while a draft is checked out) names it,
694
+ // and the id resolves there — on the click and on the anchor alike.
695
+ it('opens an id on the branch its file was read from when told one', () => {
696
+ const { result } = renderNodeNav('/skills-and-tools/skills/newsletter');
697
+ expect(result.current.nodeIdTarget('plan-node#goal', null, 'main')).toEqual({
698
+ href: '/workspace/main/plan-node#goal',
699
+ newTab: true,
700
+ });
701
+ result.current.openNodeId('plan-node#goal', null, { homeBranch: 'main' });
702
+ expectNewTab('/workspace/main/plan-node#goal');
703
+ });
704
+
705
+ it('answers nodeIdTarget with the same address', () => {
706
+ const { result } = renderNodeNav('/workspace/alice%2Fdraft/foo-node');
707
+ expect(result.current.nodeIdTarget('plan-node#goal')).toEqual({
708
+ href: '/workspace/alice%2Fdraft/plan-node#goal',
709
+ newTab: true,
710
+ });
711
+ });
712
+ });
713
+
714
+ /**
715
+ * The app's own navigation is not a link inside a file: the tree, the tabs,
716
+ * search and the library open in place, through `openFile` and
717
+ * `openWorkspacePath`, in full screen or not.
718
+ */
719
+ describe('useFileNav: the app\'s own navigation stays in this tab', () => {
720
+ it('opens a tree path in place', () => {
721
+ const { result } = renderNav('alice/draft', 'knowledge-base', '/workspace/alice%2Fdraft/knowledge-base/Knowledge/Old.md', '?fullscreen=1');
722
+ result.current.openWorkspacePath('knowledge-base/Knowledge/New.md');
723
+ expect(navigateMock).toHaveBeenCalledWith('/workspace/alice%2Fdraft/knowledge-base/Knowledge/New.md');
724
+ expect(openMock).not.toHaveBeenCalled();
725
+ });
726
+
727
+ it('opens a tab\'s file in place', () => {
728
+ const { result } = renderNav('alice/draft', 'knowledge-base', '/workspace/alice%2Fdraft/knowledge-base/Knowledge/Old.md');
729
+ result.current.openFile('knowledge-base/Knowledge/New.md');
730
+ expect(navigateMock).toHaveBeenCalledWith('/workspace/alice%2Fdraft/knowledge-base/Knowledge/New.md');
731
+ expect(openMock).not.toHaveBeenCalled();
732
+ });
733
+ });