microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/npm/qsharp/test/diagnostics.js
101lines · 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 { QdkDiagnostics } from "../dist/diagnostics.js"; |
| 10 | import { log } from "../dist/log.js"; |
| 11 | import { |
| 12 | getCompiler, |
| 13 | getCompilerWorker, |
| 14 | getProjectLoader, |
| 15 | loadWasmModule, |
| 16 | } from "../dist/main.js"; |
| 17 | |
| 18 | const distDir = new URL("../dist/", import.meta.url); |
| 19 | const compilerWorkerPath = new URL("compiler/worker.js", distDir).href; |
| 20 | |
| 21 | // Load the wasm module before running any tests |
| 22 | const wasmPath = new URL("../lib/web/qsc_wasm_bg.wasm", import.meta.url); |
| 23 | await loadWasmModule(readFileSync(wasmPath).buffer); |
| 24 | |
| 25 | /** @type {import("../dist/log.js").TelemetryEvent[]} */ |
| 26 | const telemetryEvents = []; |
| 27 | log.setLogLevel("warn"); |
| 28 | log.setTelemetryCollector((event) => telemetryEvents.push(event)); |
| 29 | |
| 30 | /** |
| 31 | * @returns {import ("../dist/compiler/compiler.js").ProgramConfig} |
| 32 | */ |
| 33 | function getInvalidQirProgramConfig() { |
| 34 | /** @type {[string, string][]} */ |
| 35 | const sources = [ |
| 36 | ["test.qs", `namespace Test { function Main() : Int { return 1; } }`], |
| 37 | ]; |
| 38 | return { |
| 39 | sources, |
| 40 | languageFeatures: [], |
| 41 | profile: "base", |
| 42 | }; |
| 43 | } |
| 44 | |
| 45 | test("getQir throws QdkDiagnostics", async () => { |
| 46 | const compiler = await getCompiler(); |
| 47 | const invalidConfig = getInvalidQirProgramConfig(); |
| 48 | await assert.rejects( |
| 49 | () => compiler.getQir(invalidConfig), |
| 50 | (err) => { |
| 51 | assert(err instanceof QdkDiagnostics, "Error should be QdkDiagnostics"); |
| 52 | assert(err.diagnostics.length > 0, "diagnostics should not be empty"); |
| 53 | assert.match(err.message, /cannot use an integer value as an output/); |
| 54 | return true; |
| 55 | }, |
| 56 | "getQir should throw on invalid input", |
| 57 | ); |
| 58 | }); |
| 59 | |
| 60 | test("getQir throws QdkDiagnostics - worker", async () => { |
| 61 | const compiler = getCompilerWorker(compilerWorkerPath); |
| 62 | const invalidConfig = getInvalidQirProgramConfig(); |
| 63 | try { |
| 64 | await assert.rejects( |
| 65 | () => compiler.getQir(invalidConfig), |
| 66 | (err) => { |
| 67 | assert(err instanceof QdkDiagnostics, "Error should be QdkDiagnostics"); |
| 68 | assert(err.diagnostics.length > 0, "diagnostics should not be empty"); |
| 69 | assert.match(err.message, /cannot use an integer value as an output/); |
| 70 | return true; |
| 71 | }, |
| 72 | "getQir should throw on invalid input", |
| 73 | ); |
| 74 | } finally { |
| 75 | compiler.terminate(); |
| 76 | } |
| 77 | }); |
| 78 | |
| 79 | // Minimal IProjectHost implementation for testing |
| 80 | const dummyHost = { |
| 81 | readFile: async () => null, |
| 82 | listDirectory: async () => [], |
| 83 | resolvePath: async (a, b) => b, |
| 84 | fetchGithub: async () => "", |
| 85 | findManifestDirectory: async () => null, |
| 86 | }; |
| 87 | |
| 88 | test("loadQSharpProject throws QdkDiagnostics", async () => { |
| 89 | const loader = await getProjectLoader(dummyHost); |
| 90 | await assert.rejects( |
| 91 | () => loader.loadQSharpProject("/not/a/real/dir"), |
| 92 | (err) => { |
| 93 | assert(err instanceof QdkDiagnostics, "Error should be QdkDiagnostics"); |
| 94 | assert(err.diagnostics.length > 0, "diagnostics should not be empty"); |
| 95 | assert.match(err.message, /Failed to parse manifest/i); |
| 96 | return true; |
| 97 | }, |
| 98 | "loadQSharpProject should throw on invalid directory", |
| 99 | ); |
| 100 | loader.dispose(); |
| 101 | }); |
| 102 | |