@jsb188/ai-chat 1.1.2 → 1.1.13

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,13 +0,0 @@
1
- // Representative synthetic receipt rows: the plan does not contain screenshot transcriptions.
2
- export const receiptTable = `Here is your receipt:
3
-
4
- | Item | Quantity | Unit price | Tax | Total |
5
- | :--- | ---: | ---: | ---: | ---: |
6
- | Apples | 2 | $3.00 | $0.00 | $6.00 |
7
- | Coffee | 1 | $12.50 | $1.63 | $14.13 |
8
-
9
- The total is **$20.13**.`;
10
-
11
- export const csvTable = '```csv\nName,Quantity,Code\nApples,2,0012\nCoffee,1,0007\n```';
12
-
13
- export const mixedContent = `${receiptTable}\n\n## Details\n\n- First item\n - Nested item\n- Second item\n\n${csvTable}\n\n~~~typescript\nconst value = "a|b";\n~~~\n\nFinished.`;
@@ -1,227 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import type { ContentBlock, InlineNode, TableBlock } from '../src/types/content';
3
- import { parseContent } from '../src/utils/parseContent';
4
- import { csvTable, mixedContent, receiptTable } from './fixtures/content';
5
-
6
- /*
7
- * Read inline text independently of styling to compare source-preserved values.
8
- */
9
- function inlineText(nodes: InlineNode[]): string {
10
- return nodes
11
- .map((node) =>
12
- 'value' in node ? node.value : 'children' in node ? inlineText(node.children) : node.kind === 'break' ? '\n' : node.source,
13
- )
14
- .join('');
15
- }
16
-
17
- /*
18
- * Require a parsed table so assertions cannot silently accept a fallback block.
19
- */
20
- function table(source: string): TableBlock {
21
- const block = parseContent(source).blocks[0];
22
- expect(block.kind).toBe('table');
23
- return block as TableBlock;
24
- }
25
-
26
- /*
27
- * Count tables through list containers when validating unsupported nested syntax.
28
- */
29
- function tableCount(blocks: ContentBlock[]): number {
30
- return blocks.reduce(
31
- (count, block) =>
32
- count +
33
- (block.kind === 'table'
34
- ? 1
35
- : block.kind === 'list'
36
- ? block.items.reduce((total, item) => total + tableCount(item.blocks), 0)
37
- : 0),
38
- 0,
39
- );
40
- }
41
-
42
- describe('source-preserving block parsing', () => {
43
- it('renders the representative receipt and CSV shapes in source order', () => {
44
- const receipt = parseContent(receiptTable);
45
- expect(receipt.blocks.map((block) => block.kind)).toEqual(['paragraph', 'table', 'paragraph']);
46
- expect((receipt.blocks[1] as TableBlock).headers).toHaveLength(5);
47
- expect((receipt.blocks[1] as TableBlock).rows).toHaveLength(2);
48
- const csv = table(csvTable);
49
- expect(csv.origin).toBe('csv');
50
- expect(csv.headers).toHaveLength(3);
51
- expect(csv.rows).toHaveLength(2);
52
- expect(parseContent(mixedContent).blocks.map((block) => block.kind)).toEqual([
53
- 'paragraph',
54
- 'table',
55
- 'paragraph',
56
- 'heading',
57
- 'list',
58
- 'table',
59
- 'code',
60
- 'paragraph',
61
- ]);
62
- });
63
-
64
- it('supports optional outer pipes, alignment, and empty cells', () => {
65
- const parsed = table('left | mid | right\n:- | :-: | -:\n| | zero | 0 |');
66
- expect(parsed.alignments).toEqual(['left', 'center', 'right']);
67
- expect(parsed.rows[0].cells.map((cell) => (cell.kind === 'inline' ? inlineText(cell.children) : cell.value))).toEqual([
68
- '',
69
- 'zero',
70
- '0',
71
- ]);
72
- });
73
-
74
- it('splits escaped pipes using backslash parity even inside code spans', () => {
75
- const parsed = table('| a | b |\n|-|-|\n| `x\\|y` | z |');
76
- expect(parsed.rows[0].cells).toHaveLength(2);
77
- expect(parsed.rows[0].cells[0]).toMatchObject({ kind: 'inline', children: [{ kind: 'code', value: 'x|y' }] });
78
- expect(parseContent('| a | b |\n|-|-|\n| `x|y` | z |').blocks[0].kind).toBe('literal');
79
- expect(table('a|b\n-|-\nx\\\\|y').rows[0].cells).toHaveLength(2);
80
- });
81
-
82
- it('keeps headers and cells inline and rejects completed width mismatches', () => {
83
- const parsed = table('# Heading | *Weight*\n-|-\n# Cell | **Bold**');
84
- expect(parsed.headers[0].kind).toBe('inline');
85
- expect(parsed.rows[0].cells[0]).toMatchObject({ kind: 'inline', children: [{ kind: 'text', value: '# Cell' }] });
86
- const malformed = parseContent('a|b\n-|-\none|two|three\n\nAfter');
87
- expect(malformed.blocks.map((block) => block.kind)).toEqual(['literal', 'paragraph']);
88
- expect(malformed.blocks[0].source).toBe('a|b\n-|-\none|two|three\n');
89
- expect(malformed.diagnostics[0].code).toBe('malformed');
90
- });
91
-
92
- it('preserves physical line endings and offsets without empty paragraph blocks', () => {
93
- const source = 'First\r\nsecond\rthird\n\n\n## Heading\r\n';
94
- const parsed = parseContent(source);
95
- expect(parsed.blocks).toHaveLength(2);
96
- const paragraph = parsed.blocks[0];
97
- expect(paragraph.kind === 'paragraph' && inlineText(paragraph.children)).toBe('First\nsecond\nthird');
98
- for (const block of parsed.blocks) expect(source.slice(block.start, block.end)).toBe(block.source);
99
- });
100
-
101
- it('tracks matching fence characters and lengths before looking for tables', () => {
102
- const source = '````typescript\n```csv\na,b\n1,2\n```\n````\n~~~CSV extras\na,b\n1,2\n~~~';
103
- const parsed = parseContent(source);
104
- expect(parsed.blocks.map((block) => block.kind)).toEqual(['code', 'table']);
105
- expect(parsed.blocks[0]).toMatchObject({ language: 'typescript', fence: { length: 4, closed: true } });
106
- expect(parseContent('```csv\na,b\n1,2\n~~~').blocks[0].kind).toBe('code');
107
- });
108
-
109
- it('does not close a fence with trailing nonwhitespace text', () => {
110
- const block = parseContent('```js\n```not-a-close\nvalue\n```').blocks[0];
111
- expect(block).toMatchObject({ kind: 'code', body: '```not-a-close\nvalue\n', fence: { closed: true } });
112
- });
113
-
114
- it('tracks list indentation and prevents nested table promotion', () => {
115
- const source = '3. First\n - Nested\n Continued\n - Other\n4. Last\n\n- Table\n a|b\n -|-\n 1|2\n\n> a|b\n> -|-\n> 1|2';
116
- const parsed = parseContent(source);
117
- expect(tableCount(parsed.blocks)).toBe(0);
118
- expect(parsed.blocks[0]).toMatchObject({
119
- kind: 'list',
120
- ordered: true,
121
- startNumber: 3,
122
- items: [{ blocks: [{ kind: 'paragraph' }, { kind: 'list', ordered: false }] }, { blocks: [{ kind: 'paragraph' }] }],
123
- });
124
- expect(parsed.blocks[2].kind).toBe('paragraph');
125
- });
126
-
127
- it('preserves the existing bullet marker and handles tab-based nesting columns', () => {
128
- expect(parseContent('• First\n• Second').blocks[0]).toMatchObject({ kind: 'list', ordered: false, items: [{}, {}] });
129
- expect(parseContent('-\tParent\n\t- Nested').blocks[0]).toMatchObject({
130
- kind: 'list',
131
- items: [{ blocks: [{ kind: 'paragraph' }, { kind: 'list' }] }],
132
- });
133
- });
134
-
135
- it('leaves unsupported HTML, reference links, and unfenced CSV readable', () => {
136
- const source = '<script>alert(1)</script>\n[reference][id]\na,b,c\n1,2,3';
137
- const parsed = parseContent(source);
138
- expect(parsed.blocks.map((block) => block.kind)).toEqual(['paragraph']);
139
- expect(parsed.blocks[0].source).toBe(source);
140
- });
141
-
142
- it('does not promote quoted or list-marked headers with unprefixed continuation delimiters', () => {
143
- for (const source of ['> a|b\n-|-\nx|y', '- a|b\n-|-\nx|y']) expect(tableCount(parseContent(source).blocks)).toBe(0);
144
- });
145
-
146
- it('ends active tables at quote and list containers, allowing explicit outer pipes to disambiguate cells', () => {
147
- expect(parseContent('a|b\n-|-\nx|y\n> quoted|text').blocks.map((block) => block.kind)).toEqual(['table', 'paragraph']);
148
- expect(parseContent('a|b\n-|-\nx|y\n- list|text').blocks.map((block) => block.kind)).toEqual(['table', 'list']);
149
- expect(table('a|b\n-|-\n| > quoted | text |\n| - list | text |').rows).toHaveLength(2);
150
- });
151
-
152
- it('passes multiline inline spans intact through the block scanner', () => {
153
- const paragraph = parseContent('[hl]first\r\nsecond[/hl] and `third\nfourth`').blocks[0];
154
- expect(paragraph.kind).toBe('paragraph');
155
- if (paragraph.kind === 'paragraph') {
156
- expect(paragraph.children[0]).toMatchObject({ kind: 'style', styles: ['highlight'] });
157
- expect(paragraph.children[2]).toMatchObject({ kind: 'code', value: 'third\nfourth' });
158
- }
159
- });
160
- });
161
-
162
- describe('streaming finalization and source tails', () => {
163
- it('keeps a partial delimiter as prose and promotes only complete delimiter lines', () => {
164
- expect(parseContent('a|b\n-|-', 'streaming').blocks[0].kind).toBe('paragraph');
165
- expect(parseContent('a|b\n-|-\n', 'streaming').blocks[0].kind).toBe('table');
166
- expect(parseContent('a|b\n-|-', 'complete').blocks[0].kind).toBe('table');
167
- });
168
-
169
- it('consumes unfinished table rows exactly once when a newline or completion arrives', () => {
170
- const source = 'a|b\n-|-\n1|2\n3|4';
171
- const streaming = parseContent(source, 'streaming').blocks[0] as TableBlock;
172
- expect(streaming.rows).toHaveLength(1);
173
- expect(streaming.provisionalTail).toEqual({ source: '3|4', start: 12, end: 15 });
174
- const complete = parseContent(source, 'complete').blocks[0] as TableBlock;
175
- expect(complete.rows).toHaveLength(2);
176
- expect(complete.provisionalTail).toBeUndefined();
177
- expect((parseContent(`${source}\n`, 'streaming').blocks[0] as TableBlock).rows).toHaveLength(2);
178
- });
179
-
180
- it('separates open, closed, complete, and interrupted CSV policies', () => {
181
- const source = '```csv\na,b\n1,2';
182
- expect(parseContent(source, 'streaming').blocks[0].kind).toBe('code');
183
- expect(parseContent(source, 'interrupted').blocks[0].kind).toBe('code');
184
- expect(parseContent(source, 'complete').blocks[0].kind).toBe('table');
185
- expect(parseContent(`${source}\n\`\`\``, 'streaming').blocks[0].kind).toBe('table');
186
- expect(parseContent(`${source}\n\`\`\``, 'interrupted').blocks[0].kind).toBe('table');
187
- expect(parseContent('```csv\na,b\n"open,2', 'complete').blocks[0].kind).toBe('code');
188
- });
189
-
190
- it('retains a malformed complete CSV block and every fence for fallback', () => {
191
- const source = '```csv\na,b\n1,2,3\n```';
192
- const parsed = parseContent(source);
193
- expect(parsed.blocks[0]).toMatchObject({ kind: 'code', source, fallbackReason: expect.any(String) });
194
- expect(parsed.diagnostics[0].code).toBe('malformed');
195
- });
196
-
197
- it('handles empty messages in every lifecycle state', () => {
198
- for (const status of ['streaming', 'complete', 'interrupted'] as const) expect(parseContent('', status).blocks).toEqual([]);
199
- });
200
- });
201
-
202
- describe('bounded fallback', () => {
203
- it.each([
204
- ['a|b\n-|-\n1|2', { tableColumns: 1 }],
205
- ['a|b\n-|-\n1|2\n3|4', { tableRows: 1 }],
206
- ['long|b\n-|-', { cellLength: 3 }],
207
- ['long source', { inputLength: 3 }],
208
- ['one\n\ntwo', { nodes: 1 }],
209
- ] as const)('retains the entire affected source on resource exhaustion: %s', (source, limits) => {
210
- const parsed = parseContent(source, 'complete', limits);
211
- expect(parsed.blocks[0]).toMatchObject({ kind: 'literal', source });
212
- expect(parsed.diagnostics[0].code).toBe('limit-exceeded');
213
- });
214
-
215
- it('bounds recursive list parsing while preserving deeper source', () => {
216
- const parsed = parseContent('- One\n - Two\n - Three\n - Four', 'complete', { nestingDepth: 2 });
217
- expect(parsed.diagnostics.some((diagnostic) => diagnostic.code === 'limit-exceeded')).toBe(true);
218
- expect(parsed.blocks[0].source).toContain('- Four');
219
- });
220
-
221
- it('makes progress on adversarial marker and quote runs', () => {
222
- for (const marker of ['*', '\\', '|', '[', '"']) {
223
- const source = marker.repeat(10_000);
224
- expect(parseContent(source).source).toBe(source);
225
- }
226
- });
227
- });
@@ -1,82 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { parseCsv } from '../src/utils/parseCsv';
3
-
4
- /*
5
- * Extract decoded CSV values while asserting that every cell stays literal.
6
- */
7
- function values(source: string) {
8
- const result = parseCsv(source);
9
- expect(result.valid).toBe(true);
10
- return result.rows.map((row) =>
11
- row.cells.map((cell) => {
12
- expect(cell.kind).toBe('literal');
13
- return cell.kind === 'literal' ? cell.value : null;
14
- }),
15
- );
16
- }
17
-
18
- describe('literal CSV parsing', () => {
19
- it('preserves strings, Unicode, duplicate and empty headers, and trailing empty fields', () => {
20
- expect(values('\uFEFFname,name,\n0012,李,\n0,false,=SUM(A1)')).toEqual([
21
- ['name', 'name', ''],
22
- ['0012', '李', ''],
23
- ['0', 'false', '=SUM(A1)'],
24
- ]);
25
- });
26
-
27
- it('decodes quoted commas, doubled quotes, and original embedded line endings', () => {
28
- expect(values('a,b\r\n"one,two","say ""hi"""\r\n"first\r\n\r\nlast","_literal_"')).toEqual([
29
- ['a', 'b'],
30
- ['one,two', 'say "hi"'],
31
- ['first\r\n\r\nlast', '_literal_'],
32
- ]);
33
- });
34
-
35
- it('supports one-column and header-only data without requiring commas', () => {
36
- expect(values('Header')).toEqual([['Header']]);
37
- expect(values('Header\r001\r002\r')).toEqual([['Header'], ['001'], ['002']]);
38
- });
39
-
40
- it('ignores only completely blank outside-quote lines', () => {
41
- expect(values('\nH\n\n""\n \n"\n\n"\n')).toEqual([['H'], [''], [' '], ['\n\n']]);
42
- expect(values('a,b,c\n\n,,\n')).toEqual([
43
- ['a', 'b', 'c'],
44
- ['', '', ''],
45
- ]);
46
- });
47
-
48
- it.each(['a,b\n1', 'a,b\n1,2,3', 'a\nq"q', 'a\n"q"x', 'a\n"q" ', 'a\n"unfinished'])('rejects malformed records: %s', (source) => {
49
- const result = parseCsv(source);
50
- expect(result.valid).toBe(false);
51
- expect(result.rows).toEqual([]);
52
- expect(result.diagnostics.length).toBe(1);
53
- });
54
-
55
- it('keeps absolute source ranges for quoted fields and CRLF records', () => {
56
- const source = 'a,b\r\n"x,y",z';
57
- const result = parseCsv(source, 8);
58
- expect(result.rows[0]).toMatchObject({ start: 8, end: 13 });
59
- expect(result.rows[1].cells[0]).toEqual({ kind: 'literal', value: 'x,y', start: 13, end: 18 });
60
- });
61
-
62
- it('does not invent records after a terminal separator or empty input', () => {
63
- expect(values('a,b\n1,\n')).toEqual([
64
- ['a', 'b'],
65
- ['1', ''],
66
- ]);
67
- expect(parseCsv('').valid).toBe(false);
68
- expect(parseCsv('\n\r\n\r').valid).toBe(false);
69
- });
70
-
71
- it.each([
72
- ['a,b', { tableColumns: 1 }],
73
- ['a\n1\n2', { tableRows: 1 }],
74
- ['long', { cellLength: 3 }],
75
- ['a\n1', { inputLength: 2 }],
76
- ] as const)('returns explicit limit outcomes without partial data: %s', (source, limits) => {
77
- const result = parseCsv(source, 0, limits);
78
- expect(result.valid).toBe(false);
79
- expect(result.diagnostics[0].code).toBe('limit-exceeded');
80
- expect(result.rows).toEqual([]);
81
- });
82
- });
@@ -1,222 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import type { ContentDiagnostic, InlineNode, InlineStyle } from '../src/types/content.ts';
3
- import { parseInline } from '../src/utils/parseInline.ts';
4
- import { getSafeLinkDestination } from '../src/utils/parseInline.helpers.ts';
5
-
6
- /*
7
- * Read the visible values of parsed inline content without importing any rendering implementation.
8
- */
9
- function visibleText(nodes: InlineNode[]): string {
10
- return nodes
11
- .map((node) => {
12
- if (node.kind === 'text' || node.kind === 'code') return node.value;
13
- if (node.kind === 'break') return '\n';
14
- if (node.kind === 'emoji') return node.source;
15
- return 'children' in node ? visibleText(node.children) : '';
16
- })
17
- .join('');
18
- }
19
-
20
- describe('parseInline compatibility', () => {
21
- it.each<[string, InlineStyle[]]>([
22
- ['*Medium*', ['medium']],
23
- ['**Bold**', ['semibold']],
24
- ['***Semibold***', ['semibold']],
25
- ['_Italic_', ['italic']],
26
- ['__Underline__', ['underline']],
27
- ['___Underline italic___', ['underline', 'italic']],
28
- ['_*Bold italic*_', ['italic', 'medium']],
29
- ['*_Bold italic_*', ['medium', 'italic']],
30
- ['**___Bold underline italic___**', ['semibold', 'underline', 'italic']],
31
- ['***___Semibold underline italic___***', ['semibold', 'underline', 'italic']],
32
- ['~~Strikethrough~~', ['strike']],
33
- ['**~~Bold strikethrough~~**', ['semibold', 'strike']],
34
- ['***~~Semibold strikethrough~~***', ['semibold', 'strike']],
35
- ['**___~~All styles~~___**', ['semibold', 'underline', 'italic', 'strike']],
36
- ['~~___**All styles**___~~', ['strike', 'underline', 'italic', 'semibold']],
37
- ])('keeps the historical meaning of %s', (source, styles) => {
38
- const nodes = parseInline(source);
39
- expect(nodes).toHaveLength(1);
40
- expect(nodes[0]).toMatchObject({ kind: 'style', styles, start: 0, end: source.length });
41
- });
42
-
43
- it('preserves identifiers, literal unsupported tokens, and unbalanced formatting', () => {
44
- for (const source of [
45
- 'snake_case_identifier',
46
- 'élément_clé_valeur',
47
- '___unfinished',
48
- '~Not strikethrough~',
49
- '****literal****',
50
- '[anything##**source**]',
51
- '<b>html</b>',
52
- '{{one|two}} %{one|two}',
53
- ]) {
54
- expect(parseInline(source)).toEqual([{ kind: 'text', value: source, start: 0, end: source.length }]);
55
- }
56
- expect(parseInline('**Bold_with_underscore**')[0]).toMatchObject({
57
- kind: 'style',
58
- styles: ['semibold'],
59
- children: [{ kind: 'text', value: 'Bold_with_underscore' }],
60
- });
61
- });
62
-
63
- it('resolves escaped markers before styles while retaining precise source ranges', () => {
64
- const source = String.raw`\*plain\* and \_identifier\_ and \|`;
65
- expect(parseInline(source, 20)).toEqual([
66
- { kind: 'text', value: '*plain* and _identifier_ and |', start: 20, end: 20 + source.length },
67
- ]);
68
- expect(visibleText(parseInline(String.raw`C:\files\report`))).toBe(String.raw`C:\files\report`);
69
- });
70
-
71
- it('keeps code literal with matching-length delimiters and allows shorter backticks inside', () => {
72
- const source = '``**literal** ` inner :smile:`` and `code`';
73
- expect(parseInline(source)).toMatchObject([
74
- { kind: 'code', value: '**literal** ` inner :smile:' },
75
- { kind: 'text', value: ' and ' },
76
- { kind: 'code', value: 'code' },
77
- ]);
78
- expect(parseInline('`unfinished')).toEqual([{ kind: 'text', value: '`unfinished', start: 0, end: 11 }]);
79
- });
80
-
81
- it('supports highlights and deterministic emoji candidates without resolving random tags', () => {
82
- expect(parseInline('[HL]Important **bold**[/HL]')[0]).toMatchObject({ kind: 'style', styles: ['highlight'] });
83
- expect(parseInline(':SMILE: :not_real:')).toMatchObject([
84
- { kind: 'emoji', name: 'smile', source: ':SMILE:' },
85
- { kind: 'text', value: ' ' },
86
- { kind: 'emoji', name: 'not_real', source: ':not_real:' },
87
- ]);
88
- });
89
-
90
- it('preserves line endings as visible breaks without extending ordinary styles over lines', () => {
91
- expect(parseInline('a\r\nb\rc\nd', 10)).toEqual([
92
- { kind: 'text', value: 'a', start: 10, end: 11 },
93
- { kind: 'break', start: 11, end: 13 },
94
- { kind: 'text', value: 'b', start: 13, end: 14 },
95
- { kind: 'break', start: 14, end: 15 },
96
- { kind: 'text', value: 'c', start: 15, end: 16 },
97
- { kind: 'break', start: 16, end: 17 },
98
- { kind: 'text', value: 'd', start: 17, end: 18 },
99
- ]);
100
- expect(visibleText(parseInline('**a\nb**'))).toBe('**a\nb**');
101
- expect(parseInline('[hl]a\nb[/hl]')[0]).toMatchObject({ kind: 'style', styles: ['highlight'] });
102
- });
103
- });
104
-
105
- describe('explicit links', () => {
106
- it('balances escaped labels and nested destination parentheses', () => {
107
- const source = String.raw`[**Docs** \[v2\]](https://example.com/wiki/Foo_(bar))`;
108
- const node = parseInline(source, 8)[0];
109
- expect(node).toMatchObject({ kind: 'link', destination: 'https://example.com/wiki/Foo_(bar)', start: 8, end: source.length + 8 });
110
- expect(visibleText(parseInline(source))).toBe('Docs [v2]');
111
- expect(parseInline('[look [inside]](/relative)')[0]).toMatchObject({ kind: 'link', destination: '/relative' });
112
- expect(parseInline(String.raw`[a](https://example.com/a\(b\))`)[0]).toMatchObject({
113
- kind: 'link',
114
- destination: 'https://example.com/a(b)',
115
- });
116
- });
117
-
118
- it('keeps incomplete, rejected, and embedded-image links readable as literal source', () => {
119
- for (const source of [
120
- '[unfinished](https://example.com',
121
- '[unsafe](javascript:alert(1))',
122
- '[unsafe](data:text/html,bad)',
123
- '![alt](https://example.com/img.png)',
124
- '[bad](java\nscript:alert(1))',
125
- ]) {
126
- expect(parseInline(source)).toEqual([{ kind: 'text', value: source, start: 0, end: source.length }]);
127
- }
128
- });
129
-
130
- it('does not create nested anchors inside a link label', () => {
131
- const node = parseInline('[outer [inner](/other)](/target)')[0];
132
- expect(node).toMatchObject({ kind: 'link', destination: '/target', children: [{ kind: 'text', value: 'outer [inner](/other)' }] });
133
- });
134
-
135
- it.each([
136
- 'https://example.com',
137
- 'HTTP://example.com/a',
138
- 'mailto:hello@example.com',
139
- 'tel:+15551234567',
140
- '/profile',
141
- '../relative',
142
- './relative',
143
- '?query=yes',
144
- '#anchor',
145
- 'relative/path',
146
- ])('accepts safe destination %s', (destination) => {
147
- expect(getSafeLinkDestination(destination)).toBe(destination);
148
- });
149
-
150
- it.each([
151
- 'javascript:alert(1)',
152
- 'JAVASCRIPT:alert(1)',
153
- 'data:text/html,bad',
154
- 'vbscript:bad',
155
- 'file:///private',
156
- ' javaScript:alert(1)',
157
- 'java\tscript:alert(1)',
158
- 'java\rscript:alert(1)',
159
- '\u0000javascript:alert(1)',
160
- 'java%73cript:alert(1)',
161
- 'java&#x73;cript:alert(1)',
162
- String.raw`\javascript:alert(1)`,
163
- 'https://example.com/a b',
164
- 'https://',
165
- '',
166
- ])('rejects unsafe or malformed destination %s', (destination) => {
167
- expect(getSafeLinkDestination(destination)).toBeNull();
168
- });
169
- });
170
-
171
- describe('inline fallback and bounded scanning', () => {
172
- it('records resource and incomplete ranges without producing a diagnostic for every repeated marker', () => {
173
- const diagnostics: ContentDiagnostic[] = [];
174
- parseInline('**unfinished', 20, {}, diagnostics);
175
- expect(diagnostics).toEqual([{ code: 'incomplete', reason: 'Unclosed inline style delimiter.', start: 20, end: 32 }]);
176
- const repeated: ContentDiagnostic[] = [];
177
- parseInline('[hl]'.repeat(20_000), 0, {}, repeated);
178
- expect(repeated).toHaveLength(1);
179
- const limited: ContentDiagnostic[] = [];
180
- parseInline('**large**', 10, { inputLength: 5 }, limited);
181
- expect(limited).toMatchObject([{ code: 'limit-exceeded', start: 10, end: 19 }]);
182
- });
183
-
184
- it('caps recursion even when custom limits request an unsafe depth and ignores nonfinite overrides', () => {
185
- const source = '[hl]'.repeat(5_000) + 'x' + '[/hl]'.repeat(5_000);
186
- const diagnostics: ContentDiagnostic[] = [];
187
- expect(parseInline(source, 0, { nestingDepth: 100_000 }, diagnostics)).toEqual([
188
- { kind: 'text', value: source, start: 0, end: source.length },
189
- ]);
190
- expect(diagnostics).toMatchObject([{ code: 'limit-exceeded' }]);
191
- expect(parseInline('*normal*', 0, { nestingDepth: Number.NaN, nodes: Number.POSITIVE_INFINITY })[0]).toMatchObject({
192
- kind: 'style',
193
- styles: ['medium'],
194
- });
195
- });
196
-
197
- it('falls back to the entire literal source when input, nesting, or node limits are exceeded', () => {
198
- for (const [source, limits] of [
199
- ['**large**', { inputLength: 5 }],
200
- ['**bold** and _italic_', { nodes: 2 }],
201
- ['*__nested__*', { nestingDepth: 1 }],
202
- ] as const) {
203
- expect(parseInline(source, 10, limits)).toEqual([{ kind: 'text', value: source, start: 10, end: 10 + source.length }]);
204
- }
205
- });
206
-
207
- it('preserves adversarial unmatched delimiter runs and deeply balanced brackets', () => {
208
- for (const source of [
209
- '*'.repeat(100_000),
210
- '_'.repeat(100_000),
211
- '~'.repeat(100_000),
212
- '['.repeat(50_000) + ']'.repeat(50_000),
213
- ('`' + 'a').repeat(30_000) + '`',
214
- ':'.repeat(100_000),
215
- '\\'.repeat(100_000),
216
- ]) {
217
- const nodes = parseInline(source);
218
- expect(nodes[0].start).toBe(0);
219
- expect(nodes.at(-1)?.end).toBe(source.length);
220
- }
221
- }, 2_000);
222
- });
@@ -1,19 +0,0 @@
1
- import i18n, { i18nTranslations } from '@jsb188/app/i18n/index.ts';
2
- import { renderToStaticMarkup } from 'react-dom/server';
3
- import { expect, it } from 'vitest';
4
- import { AIChatContent } from '../src/modules/AIChatContent.tsx';
5
-
6
- /*
7
- * Exercise the real UI import and renderer in Node without window, document or ResizeObserver.
8
- */
9
- it('server renders deterministic tables and emoji without browser globals', () => {
10
- i18n.replace({ c: i18nTranslations.c.en });
11
- expect(typeof window).toBe('undefined');
12
- const text = 'Hello :smile:\n\n```csv\nName,Count\nRice,001\n```';
13
- const html = renderToStaticMarkup(<AIChatContent text={text} />);
14
- expect(html).toBe(renderToStaticMarkup(<AIChatContent text={text} />));
15
- expect(html).toContain('role="table"');
16
- expect(html).toContain('role="columnheader"');
17
- expect(html).toContain('001');
18
- expect(html).not.toContain('tabindex');
19
- });