Skip to content

Commit 5e52c2d

Browse files
XuechunHHHimykhai
authored andcommitted
add test for onCompletion handler
1 parent d17712c commit 5e52c2d

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

server/aws-lsp-partiql/src/server/completion-hint/parser-completion.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ function getCandidates(parser: PartiQLParser, index: number) {
114114
return core.collectCandidates(index)
115115
}
116116

117+
// Get the position of the last space in the line before the given position
118+
// Optimize completion on the middle of a word
117119
function getPosition(
118120
content: string,
119121
position: { line: number; character: number }

server/aws-lsp-partiql/src/server/language-server.test.ts

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Server } from '@aws/language-server-runtimes/server-interface'
22
import { TestFeatures } from '@aws/language-server-runtimes/testing'
33
import { TextDocument } from 'vscode-languageserver-textdocument'
4-
import { TextDocumentItem, Hover, SignatureHelp } from 'vscode-languageserver-types'
4+
import { TextDocumentItem, Hover, SignatureHelp, CompletionList } from 'vscode-languageserver-types'
55
import { PartiQLServerFactory } from './language-server'
66
import { createPartiQLLanguageService } from './language-service'
77
import { CancellationTokenSource } from 'vscode-languageserver'
@@ -268,3 +268,70 @@ describe('PartiQL Server - SignatureHelp Functionality', () => {
268268
expect(result).toBeNull()
269269
})
270270
})
271+
272+
describe('PartiQL Server - Completion Functionality', () => {
273+
let service: ReturnType<typeof createPartiQLLanguageService>
274+
let server: Server
275+
let features: TestFeatures
276+
let completionSpy: jest.SpyInstance
277+
278+
const testContent = `SELECT * FR'`
279+
const testDocument = TextDocument.create('file:///completionTest.partiql', 'partiql', 1, testContent)
280+
281+
beforeEach(async () => {
282+
service = createPartiQLLanguageService()
283+
server = PartiQLServerFactory(service)
284+
features = new TestFeatures()
285+
286+
await features.start(server)
287+
features.openDocument(testDocument)
288+
289+
completionSpy = jest.spyOn(service, 'doComplete').mockImplementation((doc, position) => {
290+
if (position.line === 0 && position.character === 11) {
291+
return CompletionList.create([{ label: 'FROM' }], false)
292+
}
293+
return CompletionList.create([], false) // Return an empty list for other positions
294+
})
295+
})
296+
297+
afterEach(() => {
298+
jest.clearAllMocks()
299+
features.dispose()
300+
})
301+
302+
it('should provide correct completions within FROM clause', async () => {
303+
const completionParams = {
304+
textDocument: testDocument,
305+
position: { line: 0, character: 11 },
306+
}
307+
const cancellationToken = new CancellationTokenSource().token
308+
309+
const result = await features.doCompletion(completionParams, cancellationToken)
310+
311+
expect(completionSpy).toHaveBeenCalledWith(testDocument, { line: 0, character: 11 })
312+
expect(result).toEqual(
313+
expect.objectContaining({
314+
items: [expect.objectContaining({ label: 'FROM' })],
315+
isIncomplete: false,
316+
})
317+
)
318+
})
319+
320+
it('should not provide completions outside FROM clause', async () => {
321+
const completionParams = {
322+
textDocument: testDocument,
323+
position: { line: 0, character: 3 }, // Position outside the 'LIKE' clause
324+
}
325+
const cancellationToken = new CancellationTokenSource().token
326+
327+
const result = await features.doCompletion(completionParams, cancellationToken)
328+
329+
expect(completionSpy).toHaveBeenCalledWith(testDocument, { line: 0, character: 3 })
330+
expect(result).toEqual(
331+
expect.objectContaining({
332+
items: [],
333+
isIncomplete: false,
334+
})
335+
)
336+
})
337+
})

0 commit comments

Comments
 (0)