microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/npm/qsharp/test/languageService.js
88lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | // @ts-check |
| 5 | |
| 6 | import assert from "node:assert/strict"; |
| 7 | import { readFileSync } from "node:fs"; |
| 8 | import { test } from "node:test"; |
| 9 | import { log } from "../dist/log.js"; |
| 10 | import { getLanguageService, loadWasmModule } from "../dist/node.js"; |
| 11 | |
| 12 | // Load the wasm module before running any tests |
| 13 | const wasmPath = new URL("../lib/web/qsc_wasm_bg.wasm", import.meta.url); |
| 14 | await loadWasmModule(readFileSync(wasmPath).buffer); |
| 15 | |
| 16 | log.setLogLevel("warn"); |
| 17 | |
| 18 | // Minimal IProjectHost implementation for testing |
| 19 | const dummyHost = { |
| 20 | readFile: async () => null, |
| 21 | listDirectory: async () => [], |
| 22 | resolvePath: async (a, b) => b, |
| 23 | fetchGithub: async () => "", |
| 24 | findManifestDirectory: async () => null, |
| 25 | }; |
| 26 | |
| 27 | test("devDiagnostics configuration works", async () => { |
| 28 | const languageService = await getLanguageService(dummyHost); |
| 29 | |
| 30 | try { |
| 31 | // Collect diagnostics events as they are raised |
| 32 | const diagnosticEvents = []; |
| 33 | languageService.addEventListener("diagnostics", (event) => { |
| 34 | diagnosticEvents.push({ |
| 35 | uri: event.detail.uri, |
| 36 | diagnostics: event.detail.diagnostics.map((diag) => ({ |
| 37 | code: diag.code, |
| 38 | })), |
| 39 | }); |
| 40 | }); |
| 41 | |
| 42 | // Enable dev diagnostics |
| 43 | await languageService.updateConfiguration({ |
| 44 | devDiagnostics: true, |
| 45 | }); |
| 46 | |
| 47 | // Update a document |
| 48 | await languageService.updateDocument( |
| 49 | "test.qs", |
| 50 | 1, |
| 51 | "namespace Test { @EntryPoint() operation Main() : Unit {} }", |
| 52 | "qsharp", |
| 53 | ); |
| 54 | |
| 55 | await new Promise((resolve) => setTimeout(resolve, 0)); |
| 56 | |
| 57 | // Should have received diagnostic events |
| 58 | assert.deepEqual(diagnosticEvents, [ |
| 59 | { |
| 60 | diagnostics: [ |
| 61 | { |
| 62 | code: "Qdk.Dev.DocumentStatus", |
| 63 | }, |
| 64 | ], |
| 65 | uri: "test.qs", |
| 66 | }, |
| 67 | ]); |
| 68 | |
| 69 | // Test disabling dev diagnostics |
| 70 | diagnosticEvents.length = 0; |
| 71 | |
| 72 | await languageService.updateConfiguration({ |
| 73 | devDiagnostics: false, |
| 74 | }); |
| 75 | |
| 76 | await new Promise((resolve) => setTimeout(resolve, 0)); |
| 77 | |
| 78 | // Diagnostics should be cleared |
| 79 | assert.deepEqual(diagnosticEvents, [ |
| 80 | { |
| 81 | diagnostics: [], |
| 82 | uri: "test.qs", |
| 83 | }, |
| 84 | ]); |
| 85 | } finally { |
| 86 | await languageService.dispose(); |
| 87 | } |
| 88 | }); |
| 89 | |