|
| 1 | +// @ts-nocheck |
| 2 | +import { beforeEach, describe, expect, it, jest } from '@jest/globals'; |
| 3 | + |
| 4 | +import * as lsp from '@designliquido/delegua-lsp'; |
| 5 | +import * as cacheAnalise from '@designliquido/delegua-lsp/analise/cache-analise'; |
| 6 | +import * as cacheDefinicoes from '@designliquido/delegua-lsp/analise/cache-definicoes'; |
| 7 | + |
| 8 | +import { ambienteVscode } from '../../fontes/ambiente/ambiente-vscode'; |
| 9 | +import { DeleguaProvedorCompletude } from '../../fontes/completude/delegua-provedor-completude'; |
| 10 | +import { DeleguaProvedorRenomeacao } from '../../fontes/renomeacao/delegua-provedor-renomeacao'; |
| 11 | + |
| 12 | +jest.unmock('@designliquido/delegua-lsp'); |
| 13 | +jest.unmock('@designliquido/delegua-lsp/analise/cache-analise'); |
| 14 | +jest.unmock('@designliquido/delegua-lsp/analise/cache-definicoes'); |
| 15 | + |
| 16 | +const arquivos = new Map<string, string>(); |
| 17 | + |
| 18 | +jest.mock('vscode', () => ({ |
| 19 | + CompletionItem: class CompletionItem { |
| 20 | + constructor(public label: string, public kind?: number) {} |
| 21 | + documentation: unknown; |
| 22 | + detail: string | undefined; |
| 23 | + insertText: unknown; |
| 24 | + sortText: string | undefined; |
| 25 | + }, |
| 26 | + MarkdownString: class MarkdownString { |
| 27 | + constructor(public value: string) {} |
| 28 | + }, |
| 29 | + Uri: { |
| 30 | + parse: (caminho: string) => ({ caminho }), |
| 31 | + file: (caminho: string) => ({ caminho }), |
| 32 | + }, |
| 33 | + Range: class Range { |
| 34 | + start: { line: number; character: number }; |
| 35 | + end: { line: number; character: number }; |
| 36 | + |
| 37 | + constructor(startLine: number, startCharacter: number, endLine: number, endCharacter: number) { |
| 38 | + this.start = { line: startLine, character: startCharacter }; |
| 39 | + this.end = { line: endLine, character: endCharacter }; |
| 40 | + } |
| 41 | + }, |
| 42 | + TextEdit: class TextEdit { |
| 43 | + constructor(public range: unknown, public newText: string) {} |
| 44 | + }, |
| 45 | + WorkspaceEdit: class WorkspaceEdit { |
| 46 | + edicoes = new Map<string, unknown[]>(); |
| 47 | + |
| 48 | + set(uri: { caminho: string }, edicoes: unknown[]) { |
| 49 | + this.edicoes.set(uri.caminho, edicoes); |
| 50 | + } |
| 51 | + }, |
| 52 | + workspace: { |
| 53 | + workspaceFolders: [{ uri: { fsPath: '/workspace' } }], |
| 54 | + fs: { |
| 55 | + readFile: async (uri: { caminho: string }) => { |
| 56 | + const conteudo = arquivos.get(uri.caminho); |
| 57 | + if (conteudo === undefined) { |
| 58 | + throw new Error(`Arquivo não encontrado: ${uri.caminho}`); |
| 59 | + } |
| 60 | + return new TextEncoder().encode(conteudo); |
| 61 | + }, |
| 62 | + readDirectory: async () => [], |
| 63 | + }, |
| 64 | + }, |
| 65 | +}), { virtual: true }); |
| 66 | + |
| 67 | +function criarDocumento(texto: string) { |
| 68 | + return { |
| 69 | + uri: { toString: () => 'file:///workspace/teste.delegua' }, |
| 70 | + fileName: '/workspace/teste.delegua', |
| 71 | + getText: () => texto, |
| 72 | + version: 1, |
| 73 | + languageId: 'delegua', |
| 74 | + offsetAt: (posicao: { line: number; character: number }) => texto |
| 75 | + .split('\n') |
| 76 | + .slice(0, posicao.line) |
| 77 | + .reduce((total, linha) => total + linha.length + 1, 0) + posicao.character, |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +describe('contrato com @designliquido/delegua-lsp', () => { |
| 82 | + beforeEach(() => { |
| 83 | + arquivos.clear(); |
| 84 | + jest.clearAllMocks(); |
| 85 | + }); |
| 86 | + |
| 87 | + describe('verificação do contrato de exports (API pública do LSP)', () => { |
| 88 | + it('expõe as capacidades principais usadas pela extensão', () => { |
| 89 | + expect(lsp.proverItensCompletude).toEqual(expect.any(Function)); |
| 90 | + expect(lsp.proverDefinicao).toEqual(expect.any(Function)); |
| 91 | + expect(lsp.proverReferencias).toEqual(expect.any(Function)); |
| 92 | + expect(lsp.prepareRename).toEqual(expect.any(Function)); |
| 93 | + expect(lsp.provideRenameEdits).toEqual(expect.any(Function)); |
| 94 | + }); |
| 95 | + |
| 96 | + it('expõe as funções do módulo de cache de análise', () => { |
| 97 | + expect(cacheAnalise.expirarResultado).toEqual(expect.any(Function)); |
| 98 | + expect(cacheAnalise.expirarResultados).toEqual(expect.any(Function)); |
| 99 | + expect(cacheAnalise.expirarResultadosPorDependenciaArquivo).toEqual(expect.any(Function)); |
| 100 | + expect(cacheAnalise.expirarTudo).toEqual(expect.any(Function)); |
| 101 | + expect(cacheAnalise.obterResultado).toEqual(expect.any(Function)); |
| 102 | + expect(cacheAnalise.definirResultado).toEqual(expect.any(Function)); |
| 103 | + expect(cacheAnalise.obterDiagnosticos).toEqual(expect.any(Function)); |
| 104 | + expect(cacheAnalise.obterResultadoValido).toEqual(expect.any(Function)); |
| 105 | + }); |
| 106 | + |
| 107 | + it('expõe as funções do módulo de cache de definições', () => { |
| 108 | + expect(cacheDefinicoes.expirarTodasDefinicoes).toEqual(expect.any(Function)); |
| 109 | + expect(cacheDefinicoes.definirDefinicoes).toEqual(expect.any(Function)); |
| 110 | + expect(cacheDefinicoes.obterDefinicoesPorContexto).toEqual(expect.any(Function)); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('integração comportamental', () => { |
| 115 | + it('fornece itens reais de completude por meio do provedor da extensão', () => { |
| 116 | + const provedor = new DeleguaProvedorCompletude(); |
| 117 | + const itens = provedor.provideCompletionItems(criarDocumento('esc'), { line: 0, character: 3 }, {} as any); |
| 118 | + |
| 119 | + expect(itens).toEqual(expect.arrayContaining([ |
| 120 | + expect.objectContaining({ label: 'escreva' }), |
| 121 | + ])); |
| 122 | + }); |
| 123 | + |
| 124 | + it('gera edições de renomeação reais e as converte para WorkspaceEdit', async () => { |
| 125 | + const provedor = new DeleguaProvedorRenomeacao(); |
| 126 | + const documento = criarDocumento('var contador = 1\nescreva(contador)'); |
| 127 | + |
| 128 | + const resultado = await provedor.provideRenameEdits( |
| 129 | + documento, |
| 130 | + { line: 1, character: 10 }, |
| 131 | + 'total', |
| 132 | + {} as any |
| 133 | + ); |
| 134 | + |
| 135 | + expect(resultado?.edicoes.get('file:///workspace/teste.delegua')).toHaveLength(2); |
| 136 | + expect(resultado?.edicoes.get('file:///workspace/teste.delegua')).toEqual( |
| 137 | + expect.arrayContaining([ |
| 138 | + expect.objectContaining({ newText: 'total' }), |
| 139 | + ]) |
| 140 | + ); |
| 141 | + }); |
| 142 | + |
| 143 | + it('disponibiliza ao LSP o sistema de arquivos da API do VS Code', async () => { |
| 144 | + arquivos.set('/workspace/biblioteca.delegua', 'var resposta = 42'); |
| 145 | + |
| 146 | + await expect(ambienteVscode.sistemaArquivos.lerArquivoTexto('/workspace/biblioteca.delegua')) |
| 147 | + .resolves.toBe('var resposta = 42'); |
| 148 | + await expect(ambienteVscode.sistemaArquivos.lerArquivoTexto('/workspace/inexistente.delegua')) |
| 149 | + .resolves.toBeUndefined(); |
| 150 | + }); |
| 151 | + }); |
| 152 | +}); |
0 commit comments