@bevel-software/platform-core-frontend 0.7.0 → 0.7.3

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.
@@ -1,122 +1,157 @@
1
- import { describe, it, expect, vi } from 'vitest';
2
- import { render, screen } from '@testing-library/react';
3
- import userEvent from '@testing-library/user-event';
4
- import type { FileDiffPayload } from '@bevel-software/platform-shared';
5
- import { MarkdownDiffViewer } from '../MarkdownDiffViewer';
6
-
7
- /**
8
- * The diff viewer renders through the shared KB markdown pipeline, so a diff
9
- * reads like the document it is a diff of. These tests pin the two things that
10
- * make that safe rather than merely nice: it must stay renderable with NO
11
- * providers, and it must never swallow content.
12
- */
13
-
14
- function payload(baseline: string, current: string): FileDiffPayload {
15
- return {
16
- path: 'Knowledge/Node.md',
17
- kind: 'modified',
18
- baseline,
19
- current,
20
- isBinary: false,
21
- } as FileDiffPayload;
22
- }
23
-
24
- describe('MarkdownDiffViewer', () => {
25
- /**
26
- * The load-bearing constraint. This component is rendered by the
27
- * change-request dialog and the file-history panel — both of which are
28
- * exercised bare in their own tests — and it is reachable from the embed
29
- * routes, which mount outside the Git/Workspace providers. If a future edit
30
- * reaches for `useFileNav()` instead of taking a resolver as a prop, this
31
- * test fails immediately rather than the embed crashing in production.
32
- */
33
- it('renders with no Router and no Git/Workspace providers', () => {
34
- render(<MarkdownDiffViewer payload={payload('# Old title\n', '# New title\n')} />);
35
- expect(screen.getByRole('heading', { name: 'New title' })).toBeInTheDocument();
36
- expect(screen.getByRole('heading', { name: 'Old title' })).toBeInTheDocument();
37
- });
38
-
39
- it('renders unchanged text once and changed text on both sides', () => {
40
- render(
41
- <MarkdownDiffViewer
42
- payload={payload('shared para\n\nold line\n', 'shared para\n\nnew line\n')}
43
- />,
44
- );
45
- expect(screen.getByText('shared para')).toBeInTheDocument();
46
- expect(screen.getByText('old line')).toBeInTheDocument();
47
- expect(screen.getByText('new line')).toBeInTheDocument();
48
- });
49
-
50
- describe('node-id links', () => {
51
- // The link exists only on the added side, so its text is unambiguous —
52
- // a diff renders both sides, so a fixture sharing text across them would
53
- // match twice.
54
- const withIdLink = payload('See nothing yet\n', 'See [the process](order-intake)\n');
55
-
56
- it('are inert when no resolver is supplied', () => {
57
- const { container } = render(<MarkdownDiffViewer payload={withIdLink} />);
58
- // A bare id must never fall through as a relative-URL anchor — that
59
- // would navigate the SPA to a path that does not exist.
60
- const anchors = Array.from(container.querySelectorAll('a'));
61
- expect(anchors.some((a) => a.getAttribute('href') === 'order-intake')).toBe(false);
62
- expect(screen.getAllByText('the process').length).toBeGreaterThan(0);
63
- });
64
-
65
- it('call the injected resolver when one is supplied', async () => {
66
- const onOpenNodeId = vi.fn();
67
- render(<MarkdownDiffViewer payload={withIdLink} onOpenNodeId={onOpenNodeId} />);
68
- await userEvent.click(screen.getByText('the process'));
69
- expect(onOpenNodeId).toHaveBeenCalledWith('order-intake');
70
- });
71
- });
72
-
73
- it('escapes spaces in link destinations so KB links resolve', () => {
74
- const onOpenFile = vi.fn();
75
- render(
76
- <MarkdownDiffViewer
77
- payload={payload('a\n', 'a\n\n[Foo](Some File.md)\n')}
78
- onOpenFile={onOpenFile}
79
- />,
80
- );
81
- // Without escaping, CommonMark rejects the unescaped space and the whole
82
- // link renders as literal text — the diff viewer's behaviour before it
83
- // adopted the KB pipeline.
84
- expect(screen.getByText('Foo')).toBeInTheDocument();
85
- });
86
-
87
- it('renders raw HTML blocks rather than escaping them', () => {
88
- const { container } = render(
89
- <MarkdownDiffViewer
90
- payload={payload('x\n', 'x\n\n<details><summary>Source</summary>body</details>\n')}
91
- />,
92
- );
93
- expect(container.querySelector('details')).toBeTruthy();
94
- });
95
-
96
- /**
97
- * rehype-raw is only safe alongside rehype-sanitize, and this surface renders
98
- * content someone else has PROPOSED — the one place an injected script would
99
- * be most valuable to an attacker and least expected by a reviewer.
100
- */
101
- it('sanitises dangerous markup in proposed content', () => {
102
- const { container } = render(
103
- <MarkdownDiffViewer
104
- payload={payload('x\n', 'x\n\n<img src=q onerror="alert(1)">\n')}
105
- />,
106
- );
107
- expect(container.innerHTML).not.toContain('onerror');
108
- });
109
-
110
- it('does not render a frontmatter panel for a body fragment that opens on a thematic break', () => {
111
- // `parseFrontmatter` matches ANY string starting with `---`, and diff
112
- // fragments are split at arbitrary change boundaries. Rendering a fragment
113
- // through a frontmatter-parsing view would swallow everything between the
114
- // two rules. The text must survive.
115
- render(
116
- <MarkdownDiffViewer
117
- payload={payload('intro\n', 'intro\n\n---\n\nvisible body text\n\n---\n')}
118
- />,
119
- );
120
- expect(screen.getByText('visible body text')).toBeInTheDocument();
121
- });
122
- });
1
+ import { describe, it, expect, vi } from 'vitest';
2
+ import { render, screen } from '@testing-library/react';
3
+ import userEvent from '@testing-library/user-event';
4
+ import type { FileDiffPayload } from '@bevel-software/platform-shared';
5
+ import { MarkdownDiffViewer } from '../MarkdownDiffViewer';
6
+
7
+ /**
8
+ * The diff viewer renders through the shared KB markdown pipeline, so a diff
9
+ * reads like the document it is a diff of. These tests pin the two things that
10
+ * make that safe rather than merely nice: it must stay renderable with NO
11
+ * providers, and it must never swallow content.
12
+ */
13
+
14
+ function payload(baseline: string, current: string): FileDiffPayload {
15
+ return {
16
+ path: 'Knowledge/Node.md',
17
+ kind: 'modified',
18
+ baseline,
19
+ current,
20
+ isBinary: false,
21
+ } as FileDiffPayload;
22
+ }
23
+
24
+ describe('MarkdownDiffViewer', () => {
25
+ /**
26
+ * The load-bearing constraint. This component is rendered by the
27
+ * change-request dialog and the file-history panel — both of which are
28
+ * exercised bare in their own tests — and it is reachable from the embed
29
+ * routes, which mount outside the Git/Workspace providers. If a future edit
30
+ * reaches for `useFileNav()` instead of taking a resolver as a prop, this
31
+ * test fails immediately rather than the embed crashing in production.
32
+ */
33
+ it('renders with no Router and no Git/Workspace providers', () => {
34
+ render(<MarkdownDiffViewer payload={payload('# Old title\n', '# New title\n')} />);
35
+ expect(screen.getByRole('heading', { name: 'New title' })).toBeInTheDocument();
36
+ expect(screen.getByRole('heading', { name: 'Old title' })).toBeInTheDocument();
37
+ });
38
+
39
+ /**
40
+ * `scroll={false}` is what lets a parent be the scroller. The review panel
41
+ * needs that: when the centred column scrolled instead, the gutters either
42
+ * side sat outside the hit area and the wheel did nothing over them. Two
43
+ * nested `h-full overflow-auto` boxes also leave the outer one unable to
44
+ * scroll and stack both paddings.
45
+ */
46
+ describe('scroll', () => {
47
+ it('owns a scroller and padding by default', () => {
48
+ const { container } = render(<MarkdownDiffViewer payload={payload('a\n', 'b\n')} />);
49
+ const root = container.firstElementChild as HTMLElement;
50
+ expect(root.className).toContain('overflow-auto');
51
+ expect(root.className).toContain('px-4');
52
+ });
53
+
54
+ it('yields both when the parent owns the scroller', () => {
55
+ const { container } = render(
56
+ <MarkdownDiffViewer payload={payload('a\n', 'b\n')} scroll={false} />,
57
+ );
58
+ const root = container.firstElementChild as HTMLElement;
59
+ expect(root.className).not.toContain('overflow-auto');
60
+ expect(root.className).not.toContain('px-4');
61
+ });
62
+
63
+ it('renders the same content either way', () => {
64
+ for (const scroll of [true, false]) {
65
+ const { getByText, unmount } = render(
66
+ <MarkdownDiffViewer payload={payload('old line\n', 'new line\n')} scroll={scroll} />,
67
+ );
68
+ expect(getByText('new line')).toBeInTheDocument();
69
+ unmount();
70
+ }
71
+ });
72
+ });
73
+
74
+ it('renders unchanged text once and changed text on both sides', () => {
75
+ render(
76
+ <MarkdownDiffViewer
77
+ payload={payload('shared para\n\nold line\n', 'shared para\n\nnew line\n')}
78
+ />,
79
+ );
80
+ expect(screen.getByText('shared para')).toBeInTheDocument();
81
+ expect(screen.getByText('old line')).toBeInTheDocument();
82
+ expect(screen.getByText('new line')).toBeInTheDocument();
83
+ });
84
+
85
+ describe('node-id links', () => {
86
+ // The link exists only on the added side, so its text is unambiguous —
87
+ // a diff renders both sides, so a fixture sharing text across them would
88
+ // match twice.
89
+ const withIdLink = payload('See nothing yet\n', 'See [the process](order-intake)\n');
90
+
91
+ it('are inert when no resolver is supplied', () => {
92
+ const { container } = render(<MarkdownDiffViewer payload={withIdLink} />);
93
+ // A bare id must never fall through as a relative-URL anchor — that
94
+ // would navigate the SPA to a path that does not exist.
95
+ const anchors = Array.from(container.querySelectorAll('a'));
96
+ expect(anchors.some((a) => a.getAttribute('href') === 'order-intake')).toBe(false);
97
+ expect(screen.getAllByText('the process').length).toBeGreaterThan(0);
98
+ });
99
+
100
+ it('call the injected resolver when one is supplied', async () => {
101
+ const onOpenNodeId = vi.fn();
102
+ render(<MarkdownDiffViewer payload={withIdLink} onOpenNodeId={onOpenNodeId} />);
103
+ await userEvent.click(screen.getByText('the process'));
104
+ expect(onOpenNodeId).toHaveBeenCalledWith('order-intake');
105
+ });
106
+ });
107
+
108
+ it('escapes spaces in link destinations so KB links resolve', () => {
109
+ const onOpenFile = vi.fn();
110
+ render(
111
+ <MarkdownDiffViewer
112
+ payload={payload('a\n', 'a\n\n[Foo](Some File.md)\n')}
113
+ onOpenFile={onOpenFile}
114
+ />,
115
+ );
116
+ // Without escaping, CommonMark rejects the unescaped space and the whole
117
+ // link renders as literal text — the diff viewer's behaviour before it
118
+ // adopted the KB pipeline.
119
+ expect(screen.getByText('Foo')).toBeInTheDocument();
120
+ });
121
+
122
+ it('renders raw HTML blocks rather than escaping them', () => {
123
+ const { container } = render(
124
+ <MarkdownDiffViewer
125
+ payload={payload('x\n', 'x\n\n<details><summary>Source</summary>body</details>\n')}
126
+ />,
127
+ );
128
+ expect(container.querySelector('details')).toBeTruthy();
129
+ });
130
+
131
+ /**
132
+ * rehype-raw is only safe alongside rehype-sanitize, and this surface renders
133
+ * content someone else has PROPOSED — the one place an injected script would
134
+ * be most valuable to an attacker and least expected by a reviewer.
135
+ */
136
+ it('sanitises dangerous markup in proposed content', () => {
137
+ const { container } = render(
138
+ <MarkdownDiffViewer
139
+ payload={payload('x\n', 'x\n\n<img src=q onerror="alert(1)">\n')}
140
+ />,
141
+ );
142
+ expect(container.innerHTML).not.toContain('onerror');
143
+ });
144
+
145
+ it('does not render a frontmatter panel for a body fragment that opens on a thematic break', () => {
146
+ // `parseFrontmatter` matches ANY string starting with `---`, and diff
147
+ // fragments are split at arbitrary change boundaries. Rendering a fragment
148
+ // through a frontmatter-parsing view would swallow everything between the
149
+ // two rules. The text must survive.
150
+ render(
151
+ <MarkdownDiffViewer
152
+ payload={payload('intro\n', 'intro\n\n---\n\nvisible body text\n\n---\n')}
153
+ />,
154
+ );
155
+ expect(screen.getByText('visible body text')).toBeInTheDocument();
156
+ });
157
+ });
@@ -39,7 +39,10 @@ function makeReview(overrides: Partial<ReviewContextValue> = {}): ReviewContextV
39
39
  }
40
40
 
41
41
  function renderPanel(review: ReviewContextValue) {
42
- const workspace = { refreshFileTree: vi.fn(async () => null) } as unknown as WorkspaceContextValue;
42
+ const workspace = {
43
+ refreshFileTree: vi.fn(async () => null),
44
+ kbDirName: 'knowledge-base',
45
+ } as unknown as WorkspaceContextValue;
43
46
  const git = { status: { branch: session.branchName } } as unknown as GitContextValue;
44
47
  const wrapper = ({ children }: { children: ReactNode }) => (
45
48
  <MemoryRouter>
@@ -53,6 +56,7 @@ function renderPanel(review: ReviewContextValue) {
53
56
  return render(<ReviewPanel />, { wrapper });
54
57
  }
55
58
 
59
+
56
60
  describe('ReviewPanel top-bar actions (BEVA-77)', () => {
57
61
  beforeEach(() => vi.clearAllMocks());
58
62
 
@@ -0,0 +1,127 @@
1
+ import { describe, it, expect, beforeEach } from 'vitest';
2
+ import { configureBranchModel } from '@bevel-software/platform-shared';
3
+ import { isGroupItemPath, shouldOpenBesideDiff } from '../ReviewPanel';
4
+
5
+ const DEFAULT = 'target-company-state';
6
+ const DRAFT = 'razvan/onboarding-tweaks';
7
+
8
+ beforeEach(() => {
9
+ configureBranchModel({
10
+ defaultBranch: DEFAULT,
11
+ protectedBranches: ['current-company-state', DEFAULT],
12
+ });
13
+ });
14
+
15
+ const KB = 'knowledge-base';
16
+
17
+ /**
18
+ * The predicate behind "selecting a changed skill must not switch apps".
19
+ *
20
+ * Tested here rather than through the panel because the panel's file picker
21
+ * does not open under the component harness — an earlier attempt to cover this
22
+ * through the UI passed with the guard DELETED, which is worse than no test.
23
+ *
24
+ * The first version of this check shipped broken for exactly the reason the
25
+ * first case below pins: review-session paths are WORKSPACE-relative, so they
26
+ * carry the KB clone as their first segment, and `stripJunkBeforeKbDir` keeps
27
+ * that segment rather than removing it. `groupOfPath` then saw
28
+ * `knowledge-base` where it wanted `Groups` and answered `null` for every
29
+ * path — a guard that could never fire.
30
+ */
31
+ describe('isGroupItemPath', () => {
32
+ it.each([
33
+ ['skill, workspace-relative', `${KB}/Groups/GTM/update-website/SKILL.md`],
34
+ ['tool, workspace-relative', `${KB}/Groups/Engineering/notion.tool`],
35
+ ['personal group', `${KB}/Groups/personal-u1/my-skill/SKILL.md`],
36
+ ])('recognises a %s', (_name, path) => {
37
+ expect(isGroupItemPath(path, KB)).toBe(true);
38
+ });
39
+
40
+ it.each([
41
+ ['knowledge document', `${KB}/KnowledgeBase/Product/Thing.md`],
42
+ ['repo-root file', `${KB}/roles.yaml`],
43
+ ['data record', `${KB}/Data/Engineering/Knowledge/Ticket.md`],
44
+ ])('leaves a %s alone', (_name, path) => {
45
+ expect(isGroupItemPath(path, KB)).toBe(false);
46
+ });
47
+
48
+ /**
49
+ * Not every caller hands over a workspace-relative path — the panel's own
50
+ * fixtures use repo-relative ones — so both shapes have to work.
51
+ */
52
+ it('accepts an already repo-relative path', () => {
53
+ expect(isGroupItemPath('Groups/GTM/x/SKILL.md', KB)).toBe(true);
54
+ expect(isGroupItemPath('KnowledgeBase/Product/Thing.md', KB)).toBe(false);
55
+ });
56
+
57
+ it('tolerates junk before the kb dir, which is what strip exists for', () => {
58
+ expect(isGroupItemPath(`some/prefix/${KB}/Groups/GTM/x/SKILL.md`, KB)).toBe(true);
59
+ });
60
+
61
+ it('does not treat the Groups folder itself as an item', () => {
62
+ // `groupOfPath` needs a segment BELOW the group; `Groups/GTM` is the
63
+ // folder, and a bare `Groups/x.tool` names no group at all.
64
+ expect(isGroupItemPath(`${KB}/Groups/GTM`, KB)).toBe(false);
65
+ expect(isGroupItemPath(`${KB}/Groups/slack.tool`, KB)).toBe(false);
66
+ });
67
+
68
+ /**
69
+ * A sibling directory whose name merely STARTS with the kb dir is not the
70
+ * clone, so nothing inside it is a group item. Both the segment-exact match
71
+ * in `stripJunkBeforeKbDir` and the `${kbDirName}/` prefix test added here
72
+ * have to agree on that — a `startsWith(kbDirName)` in either would strip
73
+ * `-backup/…` down to `Groups/…` and call a backup copy a live skill.
74
+ */
75
+ it('is not fooled by a directory whose name merely starts with the kb dir', () => {
76
+ expect(isGroupItemPath(`${KB}-backup/Groups/GTM/x/SKILL.md`, KB)).toBe(false);
77
+ });
78
+
79
+ it('handles a null kbDirName', () => {
80
+ expect(isGroupItemPath('Groups/GTM/x/SKILL.md', null)).toBe(true);
81
+ expect(isGroupItemPath('KnowledgeBase/Thing.md', null)).toBe(false);
82
+ });
83
+ });
84
+
85
+ /**
86
+ * The full decision behind "selecting a row also opens the file beside the
87
+ * diff". Tested here for the same reason as the predicate above: the call
88
+ * site is behind a dropdown the component harness cannot open.
89
+ */
90
+ describe('shouldOpenBesideDiff', () => {
91
+ const base = { kbDirName: KB, branch: DEFAULT } as const;
92
+ const SKILL = `${KB}/Groups/GTM/update-website/SKILL.md`;
93
+ const DOC = `${KB}/KnowledgeBase/Product/Thing.md`;
94
+
95
+ it('opens an ordinary knowledge document', () => {
96
+ expect(shouldOpenBesideDiff({ ...base, kind: 'modified', path: DOC })).toBe(true);
97
+ });
98
+
99
+ it('does NOT open a skill on the default branch — that switches apps', () => {
100
+ expect(shouldOpenBesideDiff({ ...base, kind: 'modified', path: SKILL })).toBe(false);
101
+ });
102
+
103
+ /**
104
+ * Only the DEFAULT branch's `Groups/` URLs are library locations
105
+ * (`isLibraryLocation` tests `segments[1] === DEFAULT_BRANCH`). On a draft
106
+ * branch the same skill opens in Knowledge and switches nothing, so the
107
+ * guard must not fire — refusing there would cost the context this call
108
+ * exists to give and buy nothing.
109
+ */
110
+ it('DOES open the same skill on a draft branch', () => {
111
+ expect(shouldOpenBesideDiff({ ...base, branch: DRAFT, kind: 'modified', path: SKILL })).toBe(true);
112
+ });
113
+
114
+ it.each(['modified', 'added', 'renamed'] as const)('opens a %s knowledge file', (kind) => {
115
+ expect(shouldOpenBesideDiff({ ...base, kind, path: DOC })).toBe(true);
116
+ });
117
+
118
+ it('never opens a deleted file, wherever it lives', () => {
119
+ expect(shouldOpenBesideDiff({ ...base, kind: 'deleted', path: DOC })).toBe(false);
120
+ expect(shouldOpenBesideDiff({ ...base, kind: 'deleted', path: SKILL })).toBe(false);
121
+ expect(shouldOpenBesideDiff({ ...base, branch: DRAFT, kind: 'deleted', path: SKILL })).toBe(false);
122
+ });
123
+
124
+ it('treats an unknown branch as not-the-default, so it opens', () => {
125
+ expect(shouldOpenBesideDiff({ ...base, branch: null, kind: 'modified', path: SKILL })).toBe(true);
126
+ });
127
+ });