cc-haha/desktop/src/components/chat/CodeViewer.test.tsx
程序员阿江(Relakkes) 4fce6014a6 fix: preserve Shiki code rendering with legacy fallback
Code blocks should keep the richer Shiki rendering in modern WebUI while avoiding the old WebKit startup crash path by loading Shiki lazily and falling back to Prism only when the runtime cannot safely parse or execute it.

Constraint: Issue #461 is triggered by older Safari/WebKit parsing modern RegExp syntax during startup.
Rejected: Replace Shiki entirely with Prism | visual quality regressed for normal WebUI usage.
Confidence: high
Scope-risk: moderate
Directive: Keep Shiki out of the startup path unless macOS 12 WebView compatibility is re-verified.
Tested: cd desktop && bun run test -- --run src/components/chat/CodeViewer.test.tsx
Tested: cd desktop && bun run lint
Tested: cd desktop && bun run build
Tested: Real WebUI smoke on http://127.0.0.1:1420/ verified six code blocks rendered with data-highlight-engine=shiki.
Not-tested: Physical macOS 12 WebView runtime.
Related: https://github.com/NanmiCoder/cc-haha/issues/461
2026-05-22 17:55:33 +08:00

26 lines
1.1 KiB
TypeScript

import { render, screen } from '@testing-library/react'
import { describe, expect, it } from 'vitest'
import { CodeViewer } from './CodeViewer'
describe('CodeViewer', () => {
it('keeps the same inner padding for highlighted code content', () => {
const { container } = render(
<CodeViewer code={'cd testb\nnpm run dev'} language="bash" showLineNumbers />,
)
expect(screen.getByText('cd testb')).toBeTruthy()
expect(screen.getByText('npm run dev')).toBeTruthy()
const contentWrapper = container.querySelector('[data-code-viewer-content]') as HTMLElement | null
expect(contentWrapper).toBeTruthy()
expect(contentWrapper?.style.padding).toBe('0.5rem 12px')
expect(contentWrapper?.style.whiteSpace).toBe('pre')
expect(contentWrapper?.style.wordBreak).toBe('normal')
const codeArea = container.querySelector('.code-viewer-area') as HTMLElement | null
expect(codeArea?.getAttribute('data-has-line-numbers')).toBe('true')
expect(container.querySelector('[data-line-number="1"]')).toBeTruthy()
expect(container.querySelector('[data-line-number="2"]')).toBeTruthy()
})
})