test(vscode): add integration tests for completion provider guard

This commit is contained in:
2026-08-18 21:28:38 +05:30
parent 2c8841cc5f
commit a8d8ac386f
+80 -2
View File
@@ -7,10 +7,48 @@ const Module = require("node:module");
// Mock the vscode module for unit tests
const originalLoad = Module._load;
Module._load = function load(request, parent, isMain) {
if (request === "vscode") return {};
if (request === "vscode") {
return {
Position: class Position {
constructor(line, character) {
this.line = line;
this.character = character;
}
},
Range: class Range {
constructor(start, end) {
this.start = start;
this.end = end;
}
},
CompletionItem: class CompletionItem {
constructor(label, kind) {
this.label = label;
this.kind = kind;
}
},
CompletionItemKind: {
Event: 23,
Property: 10,
Function: 12,
Keyword: 14,
Variable: 13,
},
SnippetString: class SnippetString {
constructor(text) {
this.value = text;
}
},
MarkdownString: class MarkdownString {
constructor(text) {
this.value = text;
}
},
};
}
return originalLoad.call(this, request, parent, isMain);
};
const { isInsideViewBlock } = require("../src/completion.js");
const { isInsideViewBlock, provideCompletionItems } = require("../src/completion.js");
Module._load = originalLoad;
const PAGE = `page Home {
@@ -30,3 +68,43 @@ test("a markup offset is inside a view block", () => {
test("a functions-block offset is not inside a view block", () => {
assert.equal(isInsideViewBlock(PAGE, PAGE.indexOf("function go")), false);
});
test("provideCompletionItems returns empty array when inside view block", () => {
const document = {
getText() {
return PAGE;
},
offsetAt(position) {
return PAGE.indexOf("<div");
},
lineAt() {
return { text: "<div>hello</div>" };
},
fileName: "test.wrn",
};
const position = { line: 2, character: 4 };
const result = provideCompletionItems(document, position);
assert.equal(Array.isArray(result), true);
assert.equal(result.length, 0);
});
test("provideCompletionItems returns non-empty array when inside functions block", () => {
const document = {
getText() {
return PAGE;
},
offsetAt(position) {
return PAGE.indexOf("function go");
},
lineAt() {
return { text: " function go() {}" };
},
fileName: "test.wrn",
};
const position = { line: 5, character: 4 };
const result = provideCompletionItems(document, position);
assert.equal(Array.isArray(result), true);
assert(result.length > 0, "should return non-empty completions outside view block");
});