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

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,157 +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
- /**
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
- });
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
+ });