From fe5dfd756fc62f9b808c522c1d79bbc3c8f0554d Mon Sep 17 00:00:00 2001 From: Harsh Kashyap <55448981+Harsh23Kashyap@users.noreply.github.com> Date: Mon, 28 Sep 2026 08:54:54 +0000 Subject: [PATCH] fix(web): skip shiki WASM engine load for plain-text fences (#42957) Co-authored-by: Harsh Kashyap Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com> --- .../__tests__/shiki-highlight.spec.tsx | 100 ++++++++++++++++++ .../base/markdown-blocks/shiki-highlight.tsx | 43 ++++++++ 2 files changed, 143 insertions(+) diff --git a/web/app/components/base/markdown-blocks/__tests__/shiki-highlight.spec.tsx b/web/app/components/base/markdown-blocks/__tests__/shiki-highlight.spec.tsx index 7c30b275259..b33d060242a 100644 --- a/web/app/components/base/markdown-blocks/__tests__/shiki-highlight.spec.tsx +++ b/web/app/components/base/markdown-blocks/__tests__/shiki-highlight.spec.tsx @@ -1,7 +1,44 @@ +import type { createSingletonShorthands } from 'shiki/core' +import type * as shikiCore from 'shiki/core' import { renderToStaticMarkup } from 'react-dom/server' import { highlightCode } from '../shiki-highlight' +// `shiki/core` only exposes `getSingletonHighlighter` as a property of the +// shorthand bundle returned by `createSingletonShorthands`, not as a top-level +// symbol. Derive the accessor signature from the return type so wrapped calls +// remain correctly typed without importing a non-exported name. +type SingletonAccessor = ReturnType['getSingletonHighlighter'] + +// Mock the singleton accessor so tests can assert that the WASM engine is not +// loaded for plain-text fences while still letting supported-language tests +// observe a real `getSingletonHighlighter` invocation. +const { mockGetSingletonHighlighter } = vi.hoisted(() => ({ + mockGetSingletonHighlighter: vi.fn(), +})) + +vi.mock('shiki/core', async (importOriginal) => { + const actual = await importOriginal() + const realCreateSingletonShorthands = actual.createSingletonShorthands + return { + ...actual, + createSingletonShorthands: (factory: Parameters[0]) => { + const real = realCreateSingletonShorthands(factory) + // Wrap real `getSingletonHighlighter` so tests can observe invocations. + const realGet = real.getSingletonHighlighter + const wrapped: SingletonAccessor = (...args: Parameters) => { + mockGetSingletonHighlighter(...args) + return realGet(...args) + } + return { getSingletonHighlighter: wrapped } + }, + } +}) + describe('README code highlighting', () => { + beforeEach(() => { + mockGetSingletonHighlighter.mockReset() + }) + it.each(['github-light', 'github-dark'] as const)('highlights dotenv with %s', async (theme) => { const code = 'OPENAI_API_KEY=your-api-key\n# OPENAI_ORGANIZATION=org-id' const result = renderToStaticMarkup(await highlightCode({ code, language: 'dotenv', theme })) @@ -10,6 +47,7 @@ describe('README code highlighting', () => { expect(result).toContain('your-api-key') expect(result).toContain('OPENAI_ORGANIZATION=org-id') expect(result).toContain(' { @@ -36,5 +135,6 @@ describe('README code highlighting', () => { expect(result).toContain('const') expect(result).toContain('