import { act, render, screen } from '@testing-library/react' import type { ComponentProps } from 'react' import { expect, it, vi } from 'vitest' import { AppShellProvider } from '@/context/AppShellContext' import { CODE_BLOCK_SETTINGS_EVENT, CODE_BLOCK_THEME_STORAGE_KEY, } from '@/context/app-shell-storage' import NoteEditor from '@/features/notes/components/NoteEditor' import type { NotePage } from '@/lib/notes-api' vi.mock('react-i18next', () => ({ useTranslation: () => ({ t: (key: string) => key, i18n: { language: 'en' } }), })) async function renderCodeBlock( theme: string, activeThread: ComponentProps['activeThread'] = null ) { vi.spyOn(globalThis, 'fetch').mockResolvedValue(new Response('{}')) localStorage.setItem(CODE_BLOCK_THEME_STORAGE_KEY, theme) const page: NotePage = { id: 'note', title: 'Code', content: '', blocks: [ { id: 'code', type: 'codeBlock', props: { language: 'javascript' }, content: 'let x = 1' }, ], revision: 1, parent_id: null, icon: '', cover: '', pinned: false, created_at: 1, updated_at: 1, } const onChange = vi.fn() await act(async () => { render( ) }) const codeSelector = '.bn-root .bn-block-content[data-content-type="codeBlock"] > pre > code' const code = screen.getByText('let x = 1', { selector: codeSelector }) const editor = code.closest('.dt-note-editor')! return { code, codeSelector, editor, onChange } } it('updates an existing Notebook code block when its Appearance theme changes', async () => { const { code, codeSelector, editor, onChange } = await renderCodeBlock('dracula') expect(document.documentElement.dataset.codeBlockTheme).toBe('dracula') act(() => { window.dispatchEvent( new CustomEvent(CODE_BLOCK_SETTINGS_EVENT, { detail: { codeBlockTheme: 'oneLight' }, }) ) }) expect(document.documentElement.dataset.codeBlockTheme).toBe('oneLight') expect(editor.querySelector(codeSelector)).toBe(code) expect(onChange).not.toHaveBeenCalled() }) it('keeps a code block’s themed surface when its comment thread is open', async () => { const { code, editor } = await renderCodeBlock('oneLight', { blockId: 'code', sessionId: null }) const block = code.closest('[data-content-type="codeBlock"]')! const rules = Array.from(editor.querySelector('style')!.sheet!.cssRules).filter( (rule): rule is CSSStyleRule => rule instanceof CSSStyleRule ) const matchingRules = rules.filter(rule => block.matches(rule.selectorText)) expect(matchingRules.some(rule => rule.style.getPropertyValue('box-shadow'))).toBe(true) expect(matchingRules.every(rule => !rule.style.getPropertyValue('background'))).toBe(true) expect(document.documentElement.dataset.codeBlockTheme).toBe('oneLight') })