@celestia-island/hikari 0.55.397 → 0.55.399
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.
- package/package.json +1 -1
- package/src/components/HkDiffViewer.epoch.test.tsx +46 -0
- package/src/components/HkDiffViewer.test.tsx +103 -0
- package/src/components/HkDiffViewer.tsx +48 -2
- package/src/components/HkMarkdownRenderer.mermaid.test.tsx +78 -1
- package/src/components/HkMarkdownRenderer.overlap.test.tsx +40 -0
- package/src/components/HkMarkdownRenderer.stream.test.tsx +160 -0
- package/src/components/HkMarkdownRenderer.tsx +170 -35
- package/src/components/HkModal.scss +42 -0
- package/src/components/HkModal.styles.test.ts +54 -1
- package/src/components/HkModal.test.tsx +43 -0
- package/src/components/HkModal.tsx +10 -1
- package/src/components/HkTooltip.test.tsx +96 -0
- package/src/components/HkTooltip.tsx +197 -17
package/package.json
CHANGED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { createApp, defineComponent, h } from "vue";
|
|
3
|
+
|
|
4
|
+
// Mock the wasm module at the top level: the composable's lazy import()
|
|
5
|
+
// resolves to this factory, exactly like the real module landing after
|
|
6
|
+
// the first plaintext render.
|
|
7
|
+
vi.mock("../wasm/highlight/hikari_wasm_highlight.js", () => ({
|
|
8
|
+
highlight: (text: string, lang: string) => `<wasm>${lang}:${text}</wasm>`,
|
|
9
|
+
}));
|
|
10
|
+
|
|
11
|
+
import HkDiffViewer from "./HkDiffViewer";
|
|
12
|
+
import { useHighlight } from "../composables/useHighlight";
|
|
13
|
+
|
|
14
|
+
const DIFF = `--- a/rs
|
|
15
|
+
+++ b/rs
|
|
16
|
+
@@ -10,4 +10,5 @@
|
|
17
|
+
ctx-a
|
|
18
|
+
-removed
|
|
19
|
+
ctx-b
|
|
20
|
+
+added`;
|
|
21
|
+
|
|
22
|
+
describe("HkDiffViewer wasm-landing recolor (R2 S1-A)", () => {
|
|
23
|
+
it("memoized plaintext rows recolor once the wasm module lands and bumps the epoch", async () => {
|
|
24
|
+
// Advance the epoch first so this file starts with a clean memo
|
|
25
|
+
// (the Map is module-global and shared across files in a worker).
|
|
26
|
+
useHighlight().version.value += 1;
|
|
27
|
+
const container = document.createElement("div");
|
|
28
|
+
document.body.appendChild(container);
|
|
29
|
+
const Root = defineComponent({
|
|
30
|
+
setup() {
|
|
31
|
+
return () => h(HkDiffViewer, { diff: DIFF, language: "rust" } as never);
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
const app = createApp(Root);
|
|
35
|
+
app.mount(container);
|
|
36
|
+
|
|
37
|
+
// The composable bumps version itself when the (mocked) module lands;
|
|
38
|
+
// the memo must then drop its plaintext entries and serve wasm html.
|
|
39
|
+
await vi.waitFor(() => {
|
|
40
|
+
const html = container.querySelector(".hk-diff-viewer__code")?.innerHTML ?? "";
|
|
41
|
+
expect(html).toContain("<wasm>rust:");
|
|
42
|
+
});
|
|
43
|
+
app.unmount();
|
|
44
|
+
container.remove();
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -2,6 +2,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
|
|
|
2
2
|
import { createApp, defineComponent, h, nextTick, ref, type App } from "vue";
|
|
3
3
|
|
|
4
4
|
import HkDiffViewer from "./HkDiffViewer";
|
|
5
|
+
import * as useHighlightModule from "../composables/useHighlight";
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* HkDiffViewer — the rendered half: hunk grouping, collapsed unchanged
|
|
@@ -350,3 +351,105 @@ describe("HkDiffViewer scroll map", () => {
|
|
|
350
351
|
expect(hunkRows.length).toBeGreaterThan(4);
|
|
351
352
|
});
|
|
352
353
|
});
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
// ── Line-highlight memo (S2) ───────────────────────────────────────────
|
|
357
|
+
// A fold toggle re-renders the rows it kept; it must not re-run the wasm
|
|
358
|
+
// pest parser over them. The memo keys on (language, text) and is
|
|
359
|
+
// module-scoped (one page, one set of grammars), so each case below uses
|
|
360
|
+
// its own language id — that keeps the counts in this file independent
|
|
361
|
+
// while still exercising the shared cache.
|
|
362
|
+
describe("HkDiffViewer highlight memo", () => {
|
|
363
|
+
/** The wasm parser's per-line entry point (see useHighlight). */
|
|
364
|
+
const spyOnHighlight = () => vi.spyOn(useHighlightModule, "highlight");
|
|
365
|
+
|
|
366
|
+
it("re-expanding a fold re-renders rows without re-parsing them", async () => {
|
|
367
|
+
const spy = spyOnHighlight();
|
|
368
|
+
try {
|
|
369
|
+
const root = mount({ diff: DIFF, totalLines: 19, language: "memo-iron" });
|
|
370
|
+
await nextTick();
|
|
371
|
+
expect(root.querySelectorAll(".hk-diff-viewer__line")).toHaveLength(6);
|
|
372
|
+
// First paint: one parse per rendered row, no more.
|
|
373
|
+
expect(spy).toHaveBeenCalledTimes(6);
|
|
374
|
+
|
|
375
|
+
const head = root.querySelector(".hk-diff-viewer__hunk-head") as HTMLButtonElement;
|
|
376
|
+
head.click(); // collapse: the body leaves the tree, nothing to parse
|
|
377
|
+
await nextTick();
|
|
378
|
+
expect(root.querySelectorAll(".hk-diff-viewer__line")).toHaveLength(0);
|
|
379
|
+
expect(spy).toHaveBeenCalledTimes(6);
|
|
380
|
+
|
|
381
|
+
head.click(); // expand: same rows, served from the memo
|
|
382
|
+
await nextTick();
|
|
383
|
+
expect(root.querySelectorAll(".hk-diff-viewer__line")).toHaveLength(6);
|
|
384
|
+
// Three passes over the same rows used to mean 3 × 6 parses.
|
|
385
|
+
expect(spy).toHaveBeenCalledTimes(6);
|
|
386
|
+
} finally {
|
|
387
|
+
spy.mockRestore();
|
|
388
|
+
}
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
it("keeps one memo entry per (language, text) pair, not per pane", async () => {
|
|
392
|
+
const spy = spyOnHighlight();
|
|
393
|
+
try {
|
|
394
|
+
const root = mount({ diff: DIFF, totalLines: 19, language: "memo-silver" });
|
|
395
|
+
await nextTick();
|
|
396
|
+
expect(spy).toHaveBeenCalledTimes(6);
|
|
397
|
+
// A second viewer on the same content rides the shared memo: the rows
|
|
398
|
+
// are identical bytes, so a list of diffs pays once.
|
|
399
|
+
const second = mount({ diff: DIFF, totalLines: 19, language: "memo-silver" });
|
|
400
|
+
await nextTick();
|
|
401
|
+
expect(second.querySelectorAll(".hk-diff-viewer__line")).toHaveLength(6);
|
|
402
|
+
expect(spy).toHaveBeenCalledTimes(6);
|
|
403
|
+
// …while a different language is a different key.
|
|
404
|
+
const other = mount({ diff: DIFF, totalLines: 19, language: "memo-gold" });
|
|
405
|
+
await nextTick();
|
|
406
|
+
expect(other.querySelectorAll(".hk-diff-viewer__line")).toHaveLength(6);
|
|
407
|
+
expect(spy).toHaveBeenCalledTimes(12);
|
|
408
|
+
expect(root.querySelectorAll(".hk-diff-viewer__line")).toHaveLength(6);
|
|
409
|
+
} finally {
|
|
410
|
+
spy.mockRestore();
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
it("hovering a row is a CSS-only state change (no render, no parse)", async () => {
|
|
415
|
+
const spy = spyOnHighlight();
|
|
416
|
+
try {
|
|
417
|
+
const root = mount({ diff: DIFF, totalLines: 19, language: "memo-jade" });
|
|
418
|
+
await nextTick();
|
|
419
|
+
const row = root.querySelector(".hk-diff-viewer__line") as HTMLElement;
|
|
420
|
+
expect(spy).toHaveBeenCalledTimes(6);
|
|
421
|
+
// Hover carries no component state — the class/colour comes from the
|
|
422
|
+
// stylesheet — so the same node must survive the interaction.
|
|
423
|
+
row.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));
|
|
424
|
+
row.dispatchEvent(new MouseEvent("mouseenter"));
|
|
425
|
+
await nextTick();
|
|
426
|
+
expect(root.querySelector(".hk-diff-viewer__line")).toBe(row);
|
|
427
|
+
expect(spy).toHaveBeenCalledTimes(6);
|
|
428
|
+
} finally {
|
|
429
|
+
spy.mockRestore();
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
it("parses only the newly revealed rows when a gap fetches context", async () => {
|
|
434
|
+
const spy = spyOnHighlight();
|
|
435
|
+
try {
|
|
436
|
+
const loadContext = vi.fn(async (start: number, count: number) =>
|
|
437
|
+
Array.from({ length: count }, (_, i) => `line-${start + i}`),
|
|
438
|
+
);
|
|
439
|
+
const root = mount({
|
|
440
|
+
diff: DIFF, totalLines: 19, loadContext, contextChunk: 2, language: "memo-onyx",
|
|
441
|
+
});
|
|
442
|
+
await nextTick();
|
|
443
|
+
expect(spy).toHaveBeenCalledTimes(6);
|
|
444
|
+
(root.querySelectorAll(".hk-diff-viewer__gap")[1] as HTMLButtonElement).click();
|
|
445
|
+
await tick();
|
|
446
|
+
await nextTick();
|
|
447
|
+
// The two revealed rows are new bytes (exactly two parses); the six
|
|
448
|
+
// rows the fold kept are memo hits.
|
|
449
|
+
expect(spy).toHaveBeenCalledTimes(8);
|
|
450
|
+
} finally {
|
|
451
|
+
spy.mockRestore();
|
|
452
|
+
}
|
|
453
|
+
});
|
|
454
|
+
});
|
|
455
|
+
|
|
@@ -2,7 +2,7 @@ import { computed, defineComponent, onMounted, ref, watch } from "vue";
|
|
|
2
2
|
import { AlertTriangle, ChevronDown, ChevronRight, UnfoldVertical } from "lucide-vue-next";
|
|
3
3
|
import HkSpinner from "./HkSpinner";
|
|
4
4
|
import { useI18n } from "../i18n/context";
|
|
5
|
-
import { highlight } from "../composables/useHighlight";
|
|
5
|
+
import { highlight, useHighlight } from "../composables/useHighlight";
|
|
6
6
|
import { computeGaps, parseUnifiedDiff, type DiffLine, type UnchangedGap } from "./diffParse";
|
|
7
7
|
import type { OverlayScrollMapSegment } from "../composables/useOverlayScrollbar";
|
|
8
8
|
import "./HkDiffViewer.scss";
|
|
@@ -29,6 +29,45 @@ import "./HkDiffViewer.scss";
|
|
|
29
29
|
* grammar lands, exactly like the markdown code blocks.
|
|
30
30
|
*/
|
|
31
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Line-highlight memo.
|
|
34
|
+
*
|
|
35
|
+
* A diff pane re-renders on every fold toggle, every revealed context
|
|
36
|
+
* batch and every scroll-map emission, and each pass used to run the wasm
|
|
37
|
+
* pest parser once per VISIBLE row — so collapsing and re-expanding one
|
|
38
|
+
* hunk re-highlighted the same bytes three times, and a long file paid for
|
|
39
|
+
* its whole window on every interaction. A row's html is a pure function of
|
|
40
|
+
* (language, text) and rows come straight out of the immutable parsed diff,
|
|
41
|
+
* so the result is computed once per pair and reused for the lifetime of
|
|
42
|
+
* the page (bounded, and keyed by language so two panes on different
|
|
43
|
+
* languages never share an entry).
|
|
44
|
+
*
|
|
45
|
+
* Invalidation: `highlight()` falls back to escaped text until the wasm
|
|
46
|
+
* module lands, and the module bumps a reactive version when it does — the
|
|
47
|
+
* epoch below drops every pre-load entry so the colored html is recomputed
|
|
48
|
+
* exactly once instead of the plaintext being served forever.
|
|
49
|
+
*/
|
|
50
|
+
const HIGHLIGHT_MEMO_LIMIT = 4000;
|
|
51
|
+
const highlightMemo = new Map<string, string>();
|
|
52
|
+
let highlightMemoEpoch = -1;
|
|
53
|
+
|
|
54
|
+
function memoHighlight(text: string, lang: string, epoch: number): string {
|
|
55
|
+
if (epoch !== highlightMemoEpoch) {
|
|
56
|
+
highlightMemoEpoch = epoch;
|
|
57
|
+
highlightMemo.clear();
|
|
58
|
+
}
|
|
59
|
+
const key = `${lang}\u0000${text}`;
|
|
60
|
+
const hit = highlightMemo.get(key);
|
|
61
|
+
if (hit !== undefined) return hit;
|
|
62
|
+
const html = highlight(text, lang);
|
|
63
|
+
// Drop the whole batch rather than track an LRU: the memo is shared
|
|
64
|
+
// module-level across panes, so an overflow clears every pane's entries
|
|
65
|
+
// (bounded, not locality-preserving) and the next pass refills it.
|
|
66
|
+
if (highlightMemo.size >= HIGHLIGHT_MEMO_LIMIT) highlightMemo.clear();
|
|
67
|
+
highlightMemo.set(key, html);
|
|
68
|
+
return html;
|
|
69
|
+
}
|
|
70
|
+
|
|
32
71
|
interface GapState {
|
|
33
72
|
/** Lines already revealed. */
|
|
34
73
|
revealed: number;
|
|
@@ -75,6 +114,10 @@ export default defineComponent({
|
|
|
75
114
|
setup(props, { emit }) {
|
|
76
115
|
const host = ref<HTMLElement | null>(null);
|
|
77
116
|
const { t } = useI18n();
|
|
117
|
+
// Reading this inside the render effect keeps the rows subscribed to the
|
|
118
|
+
// wasm load: when the module lands the epoch changes, the memo drops its
|
|
119
|
+
// plaintext entries and every row re-colors in the same pass.
|
|
120
|
+
const { version: highlightEpoch } = useHighlight();
|
|
78
121
|
|
|
79
122
|
const parsed = computed(() => parseUnifiedDiff(props.diff));
|
|
80
123
|
const gaps = computed(() => computeGaps(parsed.value, props.totalLines ?? null));
|
|
@@ -253,7 +296,10 @@ export default defineComponent({
|
|
|
253
296
|
|
|
254
297
|
function renderLine(line: DiffLine, key: string) {
|
|
255
298
|
const sign = line.kind === "add" ? "+" : line.kind === "del" ? "−" : "";
|
|
256
|
-
|
|
299
|
+
// Memoized: a fold toggle re-renders the row, it does not re-parse it.
|
|
300
|
+
const html = props.language
|
|
301
|
+
? memoHighlight(line.text, props.language, highlightEpoch.value)
|
|
302
|
+
: null;
|
|
257
303
|
return (
|
|
258
304
|
<div class="hk-diff-viewer__line" data-kind={line.kind} key={key}>
|
|
259
305
|
<span class="hk-diff-viewer__no">{line.oldNo ?? ""}</span>
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { afterEach, describe, expect, it } from "vitest";
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
2
|
import { createApp, h, nextTick, ref } from "vue";
|
|
3
|
+
import { marked } from "marked";
|
|
3
4
|
|
|
4
5
|
import HkMarkdownRenderer from "./HkMarkdownRenderer";
|
|
5
6
|
import { setMermaidEngineForTests } from "../mermaid/engine";
|
|
@@ -146,3 +147,79 @@ describe("mermaid sub-app lifecycle pins", () => {
|
|
|
146
147
|
expect(host.isConnected).toBe(false);
|
|
147
148
|
});
|
|
148
149
|
});
|
|
150
|
+
|
|
151
|
+
// ── Streaming reuse (S2) ───────────────────────────────────────────────
|
|
152
|
+
// A streamed reply re-emits every fence above the growing tail byte for
|
|
153
|
+
// byte on every chunk. The diagram under such a fence must survive the
|
|
154
|
+
// re-parse: unmounting a live board and mounting a byte-identical one is
|
|
155
|
+
// the expensive half of a mermaid block (engine layout), and it is the
|
|
156
|
+
// half the fence text itself can rule out.
|
|
157
|
+
describe("mermaid streaming reuse", () => {
|
|
158
|
+
/** Poll for a text token (the body is innerHTML, not vdom). */
|
|
159
|
+
async function settleForText(root: HTMLElement, text: string, tries = 160): Promise<void> {
|
|
160
|
+
for (let i = 0; i < tries; i += 1) {
|
|
161
|
+
if ((root.textContent ?? "").includes(text)) return;
|
|
162
|
+
await new Promise((r) => setTimeout(r, 5));
|
|
163
|
+
await nextTick();
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
it("reuses the mounted diagram when the fence source is unchanged", async () => {
|
|
168
|
+
setMermaidEngineForTests(mermaidParser);
|
|
169
|
+
const parseSpy = vi.spyOn(marked, "parse");
|
|
170
|
+
try {
|
|
171
|
+
const { container, set } = mount(DOC);
|
|
172
|
+
await settleFor(container, ".hk-md-mermaid[data-mermaid='mounted']");
|
|
173
|
+
const host = container.querySelector(".hk-md-mermaid") as HTMLElement;
|
|
174
|
+
const board = host.querySelector(".hk-board");
|
|
175
|
+
expect(board).not.toBeNull();
|
|
176
|
+
expect(parseSpy).toHaveBeenCalledTimes(1);
|
|
177
|
+
|
|
178
|
+
// The stream appends prose BELOW the fence — the fence text is
|
|
179
|
+
// byte-identical, so the rendered board must ride along.
|
|
180
|
+
set(`${DOC}\n\nmore streamed prose`);
|
|
181
|
+
await settleForText(container, "more streamed prose");
|
|
182
|
+
await nextTick();
|
|
183
|
+
expect(parseSpy).toHaveBeenCalledTimes(2); // the BODY was re-parsed…
|
|
184
|
+
|
|
185
|
+
const after = container.querySelector(".hk-md-mermaid") as HTMLElement;
|
|
186
|
+
expect(after).toBe(host); // …and the diagram node was not rebuilt
|
|
187
|
+
expect(host.isConnected).toBe(true);
|
|
188
|
+
expect(host.querySelector(".hk-board")).toBe(board); // same live board
|
|
189
|
+
expect(container.querySelectorAll(".hk-md-mermaid")).toHaveLength(1);
|
|
190
|
+
} finally {
|
|
191
|
+
parseSpy.mockRestore();
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("still rebuilds the diagram when the fence source itself changes", async () => {
|
|
196
|
+
setMermaidEngineForTests(mermaidParser);
|
|
197
|
+
const { container, set } = mount(DOC);
|
|
198
|
+
await settleFor(container, ".hk-md-mermaid[data-mermaid='mounted']");
|
|
199
|
+
const first = container.querySelector(".hk-md-mermaid") as HTMLElement;
|
|
200
|
+
|
|
201
|
+
// Streaming INTO the fence: the source grows, so the board must be new.
|
|
202
|
+
set(DOC.replace("A[Alpha] --> B[Beta]", "A[Alpha] --> B[Beta]\n B --> C[Gamma]"));
|
|
203
|
+
await settleForText(container, "Gamma");
|
|
204
|
+
await new Promise((r) => setTimeout(r, 60));
|
|
205
|
+
await nextTick();
|
|
206
|
+
const fresh = container.querySelector(".hk-md-mermaid") as HTMLElement;
|
|
207
|
+
expect(fresh).not.toBe(first);
|
|
208
|
+
expect(first.isConnected).toBe(false);
|
|
209
|
+
expect((first as unknown as Record<string, unknown>).__vue_app__).toBeUndefined();
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("retires the sub-app when the fence disappears from the body", async () => {
|
|
213
|
+
setMermaidEngineForTests(mermaidParser);
|
|
214
|
+
const { container, set } = mount(DOC);
|
|
215
|
+
await settleFor(container, ".hk-md-mermaid[data-mermaid='mounted']");
|
|
216
|
+
const host = container.querySelector(".hk-md-mermaid") as HTMLElement;
|
|
217
|
+
|
|
218
|
+
set("prose only now");
|
|
219
|
+
await settleForText(container, "prose only now");
|
|
220
|
+
await new Promise((r) => setTimeout(r, 60));
|
|
221
|
+
await nextTick();
|
|
222
|
+
expect(container.querySelector(".hk-md-mermaid")).toBeNull();
|
|
223
|
+
expect((host as unknown as Record<string, unknown>).__vue_app__).toBeUndefined();
|
|
224
|
+
});
|
|
225
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { createApp, h, ref } from "vue";
|
|
3
|
+
|
|
4
|
+
// NO static `import { marked }` here: the delayed factory below must apply
|
|
5
|
+
// to the COMPONENT's lazy dynamic import, not to this file's load.
|
|
6
|
+
vi.mock("marked", async (importOriginal) => {
|
|
7
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
8
|
+
return importOriginal<typeof import("marked")>();
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
describe("HkMarkdownRenderer stale-parse overlap (R2 S1-B)", () => {
|
|
12
|
+
it("a parse left behind by a newer body never overwrites it", async () => {
|
|
13
|
+
vi.resetModules();
|
|
14
|
+
const { default: Renderer } = await import("./HkMarkdownRenderer");
|
|
15
|
+
|
|
16
|
+
const holder = ref("A-markdown-body");
|
|
17
|
+
const plain = ref(false);
|
|
18
|
+
const container = document.createElement("div");
|
|
19
|
+
document.body.appendChild(container);
|
|
20
|
+
const app = createApp({
|
|
21
|
+
render: () => h(Renderer, { content: holder.value, plain: plain.value }),
|
|
22
|
+
});
|
|
23
|
+
app.mount(container);
|
|
24
|
+
|
|
25
|
+
// The A parse is in flight, hung on the delayed marked import; flip to
|
|
26
|
+
// plain with a new body — renderPlain is synchronous, so B lands at
|
|
27
|
+
// once. When the import resolves, A's parse is stale and must NOT own
|
|
28
|
+
// the body (the parseToken guard is what rejects it).
|
|
29
|
+
await new Promise((r) => setTimeout(r, 30));
|
|
30
|
+
plain.value = true;
|
|
31
|
+
holder.value = "B-plain-body";
|
|
32
|
+
await new Promise((r) => setTimeout(r, 350));
|
|
33
|
+
|
|
34
|
+
const text = container.textContent ?? "";
|
|
35
|
+
expect(text).toContain("B-plain-body");
|
|
36
|
+
expect(text).not.toContain("A-markdown-body");
|
|
37
|
+
app.unmount();
|
|
38
|
+
container.remove();
|
|
39
|
+
}, 15000);
|
|
40
|
+
});
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// AI-generated: developed with the DeepSeek Harness agent (model DeepSeek V4.1 Flash); human-directed.
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
import { createApp, h, nextTick, ref } from "vue";
|
|
4
|
+
import { marked } from "marked";
|
|
5
|
+
|
|
6
|
+
import HkMarkdownRenderer from "./HkMarkdownRenderer";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Streaming (S2) contract for HkMarkdownRenderer.
|
|
10
|
+
*
|
|
11
|
+
* A streamed body (chat delta, agent trace, report being assembled) arrives
|
|
12
|
+
* one chunk per token batch. Every chunk used to run the whole pipeline —
|
|
13
|
+
* marked.parse over the entire body, DOMPurify, the DOMParser URL sweep and
|
|
14
|
+
* an innerHTML replacement — so n chunks over a body of length L cost
|
|
15
|
+
* O(n·L) parse work for O(L) of text: quadratic in the reply length. These
|
|
16
|
+
* cases pin the two fixes:
|
|
17
|
+
*
|
|
18
|
+
* - coalescing: a burst of chunks renders at most twice (leading edge +
|
|
19
|
+
* one trailing parse of the LATEST content), never once per chunk;
|
|
20
|
+
* - memo: a chrome flip (inlineChips) re-seats the markup the component
|
|
21
|
+
* already holds instead of paying a second full parse for the same body.
|
|
22
|
+
*
|
|
23
|
+
* `marked.parse` is the honest counter for "a full body render": it is the
|
|
24
|
+
* first and most expensive stage of the pipeline, and it runs exactly once
|
|
25
|
+
* per parse that actually happens.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
const mounts: Array<{ app: ReturnType<typeof createApp>; container: HTMLElement }> = [];
|
|
29
|
+
|
|
30
|
+
function mountContent(initial: string) {
|
|
31
|
+
const holder = ref(initial);
|
|
32
|
+
const container = document.createElement("div");
|
|
33
|
+
document.body.appendChild(container);
|
|
34
|
+
const app = createApp({
|
|
35
|
+
render: () => h(HkMarkdownRenderer, { content: holder.value }),
|
|
36
|
+
});
|
|
37
|
+
app.mount(container);
|
|
38
|
+
mounts.push({ app, container });
|
|
39
|
+
return { container, set: (v: string) => { holder.value = v; } };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
43
|
+
|
|
44
|
+
async function settleFor(root: HTMLElement, text: string, tries = 240): Promise<void> {
|
|
45
|
+
for (let i = 0; i < tries; i += 1) {
|
|
46
|
+
if ((root.textContent ?? "").includes(text)) return;
|
|
47
|
+
await sleep(5);
|
|
48
|
+
await nextTick();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let parseSpy: ReturnType<typeof vi.spyOn>;
|
|
53
|
+
|
|
54
|
+
beforeEach(() => {
|
|
55
|
+
// Same module instance the renderer lazily imports and parses through.
|
|
56
|
+
parseSpy = vi.spyOn(marked, "parse");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
afterEach(() => {
|
|
60
|
+
parseSpy.mockRestore();
|
|
61
|
+
for (const { app, container } of mounts.splice(0)) {
|
|
62
|
+
app.unmount();
|
|
63
|
+
container.remove();
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe("HkMarkdownRenderer streaming", () => {
|
|
68
|
+
it("collapses a burst of content updates into at most two full parses", async () => {
|
|
69
|
+
const { container, set } = mountContent("stream start");
|
|
70
|
+
await settleFor(container, "stream start");
|
|
71
|
+
const initialParses = parseSpy.mock.calls.length;
|
|
72
|
+
expect(initialParses).toBe(1);
|
|
73
|
+
|
|
74
|
+
// Let the coalescing window close so the burst starts from an idle
|
|
75
|
+
// renderer — the leading edge is then immediate, as it is for the first
|
|
76
|
+
// paint of a settled body.
|
|
77
|
+
await sleep(80);
|
|
78
|
+
let text = "stream start";
|
|
79
|
+
for (let i = 1; i <= 10; i += 1) {
|
|
80
|
+
text = `${text} chunk-${i}`;
|
|
81
|
+
set(text);
|
|
82
|
+
// One Vue flush per chunk: the cadence a token stream really has
|
|
83
|
+
// (chunks land in successive ticks, far faster than a frame).
|
|
84
|
+
await nextTick();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Trailing edge: the LAST chunk is always rendered, whatever the window.
|
|
88
|
+
await settleFor(container, "chunk-10");
|
|
89
|
+
const extra = parseSpy.mock.calls.length - initialParses;
|
|
90
|
+
// Ten chunks used to mean ten parses of the whole growing body.
|
|
91
|
+
expect(extra).toBeGreaterThanOrEqual(1);
|
|
92
|
+
expect(extra).toBeLessThanOrEqual(2);
|
|
93
|
+
expect(container.textContent).toContain("chunk-1 chunk-2 chunk-3");
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("renders every chunk that arrives after a quiet period (leading edge stays live)", async () => {
|
|
97
|
+
const { container, set } = mountContent("idle body");
|
|
98
|
+
await settleFor(container, "idle body");
|
|
99
|
+
const initialParses = parseSpy.mock.calls.length;
|
|
100
|
+
// Two chunks paced well beyond the window are two separate renders — a
|
|
101
|
+
// throttle that sat on them would delay a non-streaming update for no
|
|
102
|
+
// reason.
|
|
103
|
+
set("idle body plus one");
|
|
104
|
+
await settleFor(container, "plus one");
|
|
105
|
+
set("idle body plus one and two");
|
|
106
|
+
await settleFor(container, "and two");
|
|
107
|
+
expect(parseSpy.mock.calls.length - initialParses).toBe(2);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("re-seats the held markup for an inlineChips flip instead of re-parsing", async () => {
|
|
111
|
+
const content = "Badge {{container_badge}} here";
|
|
112
|
+
const holder = ref(content);
|
|
113
|
+
const chips = ref(false);
|
|
114
|
+
const container = document.createElement("div");
|
|
115
|
+
document.body.appendChild(container);
|
|
116
|
+
const app = createApp({
|
|
117
|
+
render: () => h(HkMarkdownRenderer, { content: holder.value, inlineChips: chips.value }),
|
|
118
|
+
});
|
|
119
|
+
app.mount(container);
|
|
120
|
+
mounts.push({ app, container });
|
|
121
|
+
|
|
122
|
+
await settleFor(container, "here");
|
|
123
|
+
const parses = parseSpy.mock.calls.length;
|
|
124
|
+
expect(parses).toBe(1);
|
|
125
|
+
expect(container.querySelector(".hk-tpl-chip")).toBeNull();
|
|
126
|
+
|
|
127
|
+
chips.value = true;
|
|
128
|
+
await nextTick();
|
|
129
|
+
await sleep(30);
|
|
130
|
+
await nextTick();
|
|
131
|
+
|
|
132
|
+
// The chip pass ran on the SAME body: markup, not a second parse.
|
|
133
|
+
expect(container.querySelector(".hk-tpl-chip")).not.toBeNull();
|
|
134
|
+
expect(parseSpy.mock.calls.length).toBe(parses);
|
|
135
|
+
|
|
136
|
+
// …and flipping it back re-seats the un-chipped html again, still free.
|
|
137
|
+
chips.value = false;
|
|
138
|
+
await nextTick();
|
|
139
|
+
await sleep(30);
|
|
140
|
+
await nextTick();
|
|
141
|
+
expect(container.querySelector(".hk-tpl-chip")).toBeNull();
|
|
142
|
+
expect(container.textContent).toContain("Badge");
|
|
143
|
+
expect(parseSpy.mock.calls.length).toBe(parses);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("a chunk landing mid-parse supersedes the older render (no stale body)", async () => {
|
|
147
|
+
const { container, set } = mountContent("first body");
|
|
148
|
+
await settleFor(container, "first body");
|
|
149
|
+
// Two updates: the second lands while the first parse is still resolving
|
|
150
|
+
// its imports/markup. The body must end on the newer text, never on the
|
|
151
|
+
// older one that finished last.
|
|
152
|
+
set("second body");
|
|
153
|
+
set("third body");
|
|
154
|
+
await settleFor(container, "third body");
|
|
155
|
+
await sleep(80);
|
|
156
|
+
await nextTick();
|
|
157
|
+
expect(container.textContent).toContain("third body");
|
|
158
|
+
expect(container.textContent).not.toContain("second body");
|
|
159
|
+
});
|
|
160
|
+
});
|