microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
fedimser/sparsesim-version

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/npm/qsharp/test/diagnostics.js

101lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//@ts-check
5
6import assert from "node:assert/strict";
7import { readFileSync } from "node:fs";
8import { test } from "node:test";
9import { QdkDiagnostics } from "../dist/diagnostics.js";
10import { log } from "../dist/log.js";
11import {
12 getCompiler,
13 getCompilerWorker,
14 getProjectLoader,
15 loadWasmModule,
16} from "../dist/main.js";
17
18const distDir = new URL("../dist/", import.meta.url);
19const compilerWorkerPath = new URL("compiler/worker.js", distDir).href;
20
21// Load the wasm module before running any tests
22const wasmPath = new URL("../lib/web/qsc_wasm_bg.wasm", import.meta.url);
23await loadWasmModule(readFileSync(wasmPath).buffer);
24
25/** @type {import("../dist/log.js").TelemetryEvent[]} */
26const telemetryEvents = [];
27log.setLogLevel("warn");
28log.setTelemetryCollector((event) => telemetryEvents.push(event));
29
30/**
31 * @returns {import ("../dist/compiler/compiler.js").ProgramConfig}
32 */
33function 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
45test("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
60test("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
80const dummyHost = {
81 readFile: async () => null,
82 listDirectory: async () => [],
83 resolvePath: async (a, b) => b,
84 fetchGithub: async () => "",
85 findManifestDirectory: async () => null,
86};
87
88test("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