|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 4 | +import type { TextEditor, Uri, WebviewView } from 'vscode' |
| 5 | +import type { LSPClient } from '../lsp/lsp' |
| 6 | +import { LineagePanel } from './lineagePanel' |
| 7 | + |
| 8 | +const state = vi.hoisted(() => ({ |
| 9 | + isWindows: true, |
| 10 | + editor: undefined as TextEditor | undefined, |
| 11 | + focus: undefined as ((editor: TextEditor | undefined) => void) | undefined, |
| 12 | + saved: undefined as |
| 13 | + | ((document: { uri: { toString: () => string } }) => void) |
| 14 | + | undefined, |
| 15 | + receive: undefined as ((message: unknown) => Promise<void>) | undefined, |
| 16 | + post: vi.fn(), |
| 17 | + dispose: vi.fn(), |
| 18 | +})) |
| 19 | + |
| 20 | +vi.mock('vscode', () => ({ |
| 21 | + Uri: { joinPath: () => 'asset' }, |
| 22 | + window: { |
| 23 | + get activeTextEditor() { |
| 24 | + return state.editor |
| 25 | + }, |
| 26 | + onDidChangeActiveTextEditor: (callback: typeof state.focus) => { |
| 27 | + state.focus = callback |
| 28 | + return { dispose: state.dispose } |
| 29 | + }, |
| 30 | + }, |
| 31 | + workspace: { |
| 32 | + onDidSaveTextDocument: (callback: typeof state.saved) => { |
| 33 | + state.saved = callback |
| 34 | + return { dispose: state.dispose } |
| 35 | + }, |
| 36 | + }, |
| 37 | +})) |
| 38 | +vi.mock('@bus/result', () => ({ isErr: () => false })) |
| 39 | +vi.mock('../utilities/isWindows', () => ({ |
| 40 | + get IS_WINDOWS() { |
| 41 | + return state.isWindows |
| 42 | + }, |
| 43 | +})) |
| 44 | + |
| 45 | +function editor(fsPath: string): TextEditor { |
| 46 | + return { document: { uri: { fsPath } } } as TextEditor |
| 47 | +} |
| 48 | + |
| 49 | +function panel() { |
| 50 | + const instance = new LineagePanel({} as Uri, {} as LSPClient) |
| 51 | + instance.resolveWebviewView({ |
| 52 | + webview: { |
| 53 | + postMessage: state.post, |
| 54 | + asWebviewUri: (uri: unknown) => uri, |
| 55 | + onDidReceiveMessage: (callback: typeof state.receive) => { |
| 56 | + state.receive = callback |
| 57 | + return { dispose: state.dispose } |
| 58 | + }, |
| 59 | + }, |
| 60 | + } as unknown as WebviewView) |
| 61 | + return instance |
| 62 | +} |
| 63 | + |
| 64 | +describe('lineage editor path messages', () => { |
| 65 | + beforeEach(() => { |
| 66 | + vi.clearAllMocks() |
| 67 | + state.editor = undefined |
| 68 | + }) |
| 69 | + |
| 70 | + it.each([true, false])( |
| 71 | + 'sends extension-host platform %s with active-file RPC and focus events', |
| 72 | + async isWindows => { |
| 73 | + state.isWindows = isWindows |
| 74 | + state.editor = editor('C:\\Project\\A %20#.sql') |
| 75 | + const instance = panel() |
| 76 | + await state.receive?.({ |
| 77 | + key: 'rpcRequest', |
| 78 | + payload: { method: 'get_active_file', requestId: 'active', params: {} }, |
| 79 | + }) |
| 80 | + expect(state.post).toHaveBeenLastCalledWith({ |
| 81 | + key: 'rpcResponse', |
| 82 | + payload: { |
| 83 | + requestId: 'active', |
| 84 | + result: { fileUri: 'C:\\Project\\A %20#.sql', isWindows }, |
| 85 | + }, |
| 86 | + }) |
| 87 | + state.focus?.(editor('C:\\Project\\B.sql')) |
| 88 | + expect(state.post).toHaveBeenLastCalledWith({ |
| 89 | + key: 'vscode_send', |
| 90 | + payload: { |
| 91 | + key: 'changeFocusOnFile', |
| 92 | + payload: { path: 'C:\\Project\\B.sql', isWindows }, |
| 93 | + }, |
| 94 | + }) |
| 95 | + instance.dispose() |
| 96 | + expect(state.dispose).toHaveBeenCalledTimes(3) |
| 97 | + }, |
| 98 | + ) |
| 99 | + |
| 100 | + it('reports no editor and preserves saved-file URI payloads', async () => { |
| 101 | + state.isWindows = true |
| 102 | + const instance = panel() |
| 103 | + await state.receive?.({ |
| 104 | + key: 'rpcRequest', |
| 105 | + payload: { method: 'get_active_file', requestId: 'none', params: {} }, |
| 106 | + }) |
| 107 | + expect(state.post).toHaveBeenLastCalledWith({ |
| 108 | + key: 'rpcResponse', |
| 109 | + payload: { |
| 110 | + requestId: 'none', |
| 111 | + result: { fileUri: undefined, isWindows: true }, |
| 112 | + }, |
| 113 | + }) |
| 114 | + state.focus?.(undefined) |
| 115 | + expect(state.post).toHaveBeenCalledOnce() |
| 116 | + state.saved?.({ |
| 117 | + uri: { toString: () => 'file:///c%3A/Project/A%20%23.sql' }, |
| 118 | + }) |
| 119 | + expect(state.post).toHaveBeenLastCalledWith({ |
| 120 | + key: 'vscode_send', |
| 121 | + payload: { |
| 122 | + key: 'savedFile', |
| 123 | + payload: { fileUri: 'file:///c%3A/Project/A%20%23.sql' }, |
| 124 | + }, |
| 125 | + }) |
| 126 | + instance.dispose() |
| 127 | + }) |
| 128 | +}) |
0 commit comments