-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathstreamingMarkdownRepair.test.ts
More file actions
98 lines (82 loc) · 4.33 KB
/
Copy pathstreamingMarkdownRepair.test.ts
File metadata and controls
98 lines (82 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { describe, expect, it, vi, beforeEach } from 'vitest';
import { fallbackRepair, loadRemend, resetRemendCache } from './streamingMarkdownRepair';
describe('streamingMarkdownRepair', () => {
beforeEach(() => {
resetRemendCache();
});
describe('fallbackRepair (dep-free, CJS-safe baseline)', () => {
it('closes an unbalanced code fence', () => {
expect(fallbackRepair('```ts\nconst x = 1;')).to.equal('```ts\nconst x = 1;\n```');
});
it('leaves balanced markdown untouched', () => {
expect(fallbackRepair('**bold** and `code`')).to.equal('**bold** and `code`');
});
it('does not corrupt ordinary prose with lone markers', () => {
// Conservative: never guesses at inline `*`/`_`, so math/prose survives.
expect(fallbackRepair('2 * 3 = 6 and a_b')).to.equal('2 * 3 = 6 and a_b');
});
});
describe('loadRemend (CJS-safe dynamic loader)', () => {
it('uses remend (default export) when it can be imported', async () => {
const remend = vi.fn((text: string) => `${text}**`);
const repair = await loadRemend(() => Promise.resolve({ default: remend }));
expect(repair('a **b')).to.equal('a **b**');
// Math completion is disabled so the renderer never sees literal `$…$`.
expect(remend).toHaveBeenCalledWith('a **b', { katex: false });
});
it('accepts a module whose namespace IS the function', async () => {
const remend = vi.fn((text: string) => `${text}!`);
const repair = await loadRemend(() => Promise.resolve(remend));
expect(repair('x')).to.equal('x!');
});
it('falls back when the import rejects (CJS require-of-ESM / not installed)', async () => {
const repair = await loadRemend(() =>
Promise.reject(new Error('ERR_REQUIRE_ESM: cannot require() ES Module remend')),
);
expect(repair).to.equal(fallbackRepair);
// Still functional via the baseline.
expect(repair('```ts\nx')).to.equal('```ts\nx\n```');
});
it('falls back when the module does not export a function', async () => {
const repair = await loadRemend(() => Promise.resolve({ default: { not: 'a function' } }));
expect(repair).to.equal(fallbackRepair);
});
it('caches: the importer runs once across calls', async () => {
const importer = vi.fn(() => Promise.resolve({ default: (t: string) => t }));
await loadRemend(importer);
await loadRemend(importer);
expect(importer).toHaveBeenCalledTimes(1);
});
it('resetRemendCache lets a new importer take effect', async () => {
await loadRemend(() => Promise.resolve({ default: (t: string) => `${t}-1` }));
resetRemendCache();
const repair = await loadRemend(() => Promise.resolve({ default: (t: string) => `${t}-2` }));
expect(repair('x')).to.equal('x-2');
});
});
// Regression coverage for the `remend` specifier resolving in bundled apps.
// Importing it under a specifier a bundler can't statically resolve left the upgrade
// as dead code in every browser bundle and — where the bundler wraps dynamic imports
// in a preload helper — dispatched a global load-error event on each render.
// See https://github.com/mui/mui-x/issues/23160.
describe('remend specifier resolution', () => {
it('resolves the real remend through the default importer', async () => {
// No injected importer: exercises `import('#remend')` for real, so a specifier
// that stopped resolving would fail here rather than silently degrade.
const repair = await loadRemend();
expect(repair).not.to.equal(fallbackRepair);
// remend completes the unterminated inline marker; fallbackRepair never would.
expect(repair('a **bold')).to.equal('a **bold**');
});
it('degrades to fallbackRepair on the CommonJS `#remend` stub', async () => {
// What `require('#remend')` resolves to in the CJS build, where the ESM-only
// `remend` cannot be named at all.
const stub = await import('./remendUnavailable');
const repair = await loadRemend(() => Promise.resolve(stub));
expect(repair).to.equal(fallbackRepair);
});
// The packaging side of this — that the specifier stays statically analyzable and
// that `#remend` maps per module format — reads the source tree off disk, so it
// lives in `src/tests/packagingGuard/remendSpecifier.test.ts` (Node-only).
});
});