From d5c627c54a09a8915b3830f2bebfa14345a48004 Mon Sep 17 00:00:00 2001 From: cruldra Date: Thu, 15 Oct 2026 15:45:07 +1100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20test(vscode):=20=E8=A1=A5=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=AF=B9=E8=AF=9D=E9=A1=B5=E6=8C=89=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E9=98=85=E8=AF=BB=E5=9D=97=E6=89=BE=E7=AC=A6=E5=8F=B7=E7=9A=84?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codeReader/editor/previewEditor.test.ts | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/vscode/src/codeReader/editor/previewEditor.test.ts b/vscode/src/codeReader/editor/previewEditor.test.ts index 743147d..5473893 100644 --- a/vscode/src/codeReader/editor/previewEditor.test.ts +++ b/vscode/src/codeReader/editor/previewEditor.test.ts @@ -7,6 +7,8 @@ import type { CodeColors } from '../theme/codeColors' import type { ExtensionToWebview, WebviewToExtension } from './messages' import { describe, expect, it, vi } from 'vitest' import { commands, Range, Uri, window, workspace } from 'vscode' +import { createPythonAdapter } from '../languages/python' +import { runtime, WASM_DIR } from '../testSupport' import { languageDefinitions } from './definitions' import { documentMessage } from './documentMessage' import { documentSymbolNodes } from './documentSymbols' @@ -217,7 +219,7 @@ function themeColors(colors: CodeColors) { } /** 对比基准只认用「未提交的更改」文件行上的「打开更改」打开的:这样打开的对比上一次提交,别处打开的不比 */ -function opening(profiles = { ask: profileSet(''), rewrite: profileSet('') }, colors = themeColors(COLORS), languageFor: (languageId: string) => unknown = () => undefined) { +function opening(profiles = { ask: profileSet(''), rewrite: profileSet('') }, colors = themeColors(COLORS), languageFor: (languageId: string) => unknown = () => undefined, runtime: unknown = {}) { const subscribe = () => ({ dispose: () => {} }) const diffBase = vi.fn((_uri: unknown, from: OpenedFrom): DiffBase | undefined => from === 'uncommitted' ? HEAD_BASE : undefined) const qa = { @@ -231,7 +233,7 @@ function opening(profiles = { ask: profileSet(''), rewrite: profileSet('') }, co const provider = new PreviewEditorProvider(...[ {}, { selectedTranslator: () => undefined, languageFor, onDidChange: subscribe }, - {}, + runtime, { subscribe, current: { excluded: {}, onlyChanges: false } }, colors, async () => [], @@ -733,11 +735,12 @@ describe('「了解一块代码」对话页上的仓库链接按这个功能找 const WEB = { name: 'web', fsPath: '/ws/web' } const SERVER = { name: 'server', fsPath: '/srv/app' } - async function featurePage(folders: { name: string, fsPath: string }[]) { + /** 功能对话页,链接指到的文件都是 Python,内容是 `source`;`languageFor` 给的语言有代码阅读 */ + async function featurePage(folders: { name: string, fsPath: string }[], { source = '', languageFor }: { source?: string, languageFor?: (languageId: string) => unknown } = {}) { Object.assign(workspace, { workspaceFolders: folders.map(({ name, fsPath }) => ({ name, uri: { fsPath } })) }) vi.mocked(commands.executeCommand).mockClear() - vi.mocked(workspace.openTextDocument).mockImplementation((async (uri: { fsPath: string }) => ({ uri, languageId: 'python', lineCount: 20, lineAt: () => ({ text: '' }), positionAt: (offset: number) => ({ line: 0, character: offset }) })) as never) - const { provider, document } = opening() + vi.mocked(workspace.openTextDocument).mockImplementation((async (uri: { fsPath: string }) => ({ uri, languageId: 'python', lineCount: 20, lineAt: () => ({ text: '' }), getText: () => source, positionAt: (offset: number) => ({ line: 0, character: offset }) })) as never) + const { provider, document } = opening(undefined, undefined, languageFor, runtime) Object.assign(document, { uri: FEATURE_PAGE, fileName: FEATURE_PAGE.fsPath, languageId: 'plaintext', getText: () => '' }) const page = tab() await provider.resolveCustomTextEditor(document as never, page.panel) @@ -783,6 +786,22 @@ describe('「了解一块代码」对话页上的仓库链接按这个功能找 } }) + it('链接指到有代码阅读的语言:按代码阅读解析出的块找符号,语言扩展还没起来、文档符号还是空的时候照样点得开', async () => { + try { + const python = createPythonAdapter(WASM_DIR) + const source = ['class Router:', ' async def create(self):', ' pass', ''].join('\n') + const page = await featurePage([WEB, SERVER], { source, languageFor: languageId => languageId === 'python' ? python : undefined }) + vi.mocked(documentSymbolNodes).mockClear() + await page.send({ type: 'openRepoLink', path: 'server/app/routes.py', symbol: 'Router.create' }) + expect(page.posted()).toEqual([{ type: 'repoLinkOpened', path: 'server/app/routes.py', symbol: 'Router.create' }]) + expect(commands.executeCommand).toHaveBeenCalledWith('vscode.openWith', expect.objectContaining({ fsPath: '/srv/app/app/routes.py' }), PREVIEW_VIEW_TYPE, expect.anything()) + expect(documentSymbolNodes).not.toHaveBeenCalled() + } + finally { + reset() + } + }) + it('点链接按同一个算法打开文件;带符号的点了、悬停都在那个文件的文档符号里找', async () => { try { const page = await featurePage([WEB, SERVER])