|
1 |
| -import { afterAll, beforeAll, describe, expect, test } from "vitest"; |
2 |
| -import { |
3 |
| - ConfigurationRequest, |
4 |
| - DidChangeTextDocumentNotification, |
5 |
| - PublishDiagnosticsNotification |
6 |
| -} from "vscode-languageserver"; |
7 |
| -import { getTestClient, closeDocument, initializeServer, openDocument } from "../test-utils.js"; |
| 1 | +import { afterEach, beforeEach, describe, expect, test } from "vitest"; |
| 2 | +import { PublishDiagnosticsNotification } from "vscode-languageserver"; |
| 3 | +import { TestClient } from "../test-client.js"; |
8 | 4 | import documentSettings from "./document-settings.js";
|
| 5 | +import semanticTokens from "./semantic-tokens.js"; |
9 | 6 | import schemaRegistry from "./schema-registry.js";
|
10 | 7 | import workspace from "./workspace.js";
|
11 | 8 | import validationErrorsFeature from "./validation-errors.js";
|
12 | 9 |
|
13 |
| -import type { Connection, Diagnostic } from "vscode-languageserver"; |
| 10 | +import type { Diagnostic } from "vscode-languageserver"; |
| 11 | +import type { DocumentSettings } from "./document-settings.js"; |
14 | 12 |
|
15 | 13 |
|
16 | 14 | describe("Feature - Document Settings", () => {
|
17 |
| - let client: Connection; |
18 |
| - let documentUri: string; |
| 15 | + let client: TestClient<DocumentSettings>; |
19 | 16 |
|
20 |
| - beforeAll(async () => { |
21 |
| - client = getTestClient([ |
| 17 | + beforeEach(async () => { |
| 18 | + client = new TestClient([ |
22 | 19 | workspace,
|
23 | 20 | documentSettings,
|
| 21 | + semanticTokens, |
24 | 22 | schemaRegistry,
|
25 | 23 | validationErrorsFeature
|
26 | 24 | ]);
|
27 |
| - const init = {}; |
28 |
| - const settings = { "defaultDialect": "https://json-schema.org/draft/2020-12/schema" }; |
29 |
| - await initializeServer(client, init, settings); |
| 25 | + await client.start(); |
30 | 26 | });
|
31 | 27 |
|
32 |
| - afterAll(async () => { |
33 |
| - await closeDocument(client, documentUri); |
| 28 | + afterEach(async () => { |
| 29 | + await client.stop(); |
34 | 30 | });
|
35 | 31 |
|
36 | 32 | test("test default dialect", async () => {
|
37 |
| - documentUri = await openDocument(client, "./subject.schema.json", `{}`); |
| 33 | + await client.changeConfiguration({ defaultDialect: "https://json-schema.org/draft/2020-12/schema" }); |
38 | 34 |
|
39 | 35 | const diagnosticsPromise = new Promise<Diagnostic[]>((resolve) => {
|
40 | 36 | client.onNotification(PublishDiagnosticsNotification.type, (params) => {
|
41 | 37 | resolve(params.diagnostics);
|
42 | 38 | });
|
43 | 39 | });
|
44 | 40 |
|
45 |
| - await client.sendNotification(DidChangeTextDocumentNotification.type, { |
46 |
| - textDocument: { uri: documentUri, version: 1 }, |
47 |
| - contentChanges: [] |
48 |
| - }); |
| 41 | + await client.openDocument("./subject.schema.json", `{}`); |
49 | 42 |
|
50 | 43 | const diagnostics = await diagnosticsPromise;
|
51 | 44 | expect(diagnostics).to.eql([]);
|
52 | 45 | });
|
53 | 46 |
|
54 | 47 | test("test no dialect", async () => {
|
55 |
| - documentUri = await openDocument(client, "./subject.schema.json", `{}`); |
56 |
| - |
57 |
| - client.onRequest(ConfigurationRequest.type, () => { |
58 |
| - return [{}]; |
59 |
| - }); |
60 |
| - |
61 | 48 | const diagnosticsPromise = new Promise<Diagnostic[]>((resolve) => {
|
62 | 49 | client.onNotification(PublishDiagnosticsNotification.type, (params) => {
|
63 | 50 | resolve(params.diagnostics);
|
64 | 51 | });
|
65 | 52 | });
|
66 | 53 |
|
67 |
| - await client.sendNotification(DidChangeTextDocumentNotification.type, { |
68 |
| - textDocument: { uri: documentUri, version: 1 }, |
69 |
| - contentChanges: [] |
70 |
| - }); |
| 54 | + await client.openDocument("./subject.schema.json", `{}`); |
71 | 55 |
|
72 | 56 | const diagnostics = await diagnosticsPromise;
|
73 | 57 | expect(diagnostics[0].message).to.eql("No dialect");
|
74 | 58 | });
|
75 | 59 |
|
76 | 60 | test("test unknown dialect", async () => {
|
77 |
| - documentUri = await openDocument(client, "./subject.schema.json", `{ "$schema": "" }`); |
78 |
| - |
79 |
| - client.onRequest(ConfigurationRequest.type, () => { |
80 |
| - return [{}]; |
81 |
| - }); |
82 |
| - |
83 | 61 | const diagnosticsPromise = new Promise<Diagnostic[]>((resolve) => {
|
84 | 62 | client.onNotification(PublishDiagnosticsNotification.type, (params) => {
|
85 | 63 | resolve(params.diagnostics);
|
86 | 64 | });
|
87 | 65 | });
|
88 | 66 |
|
89 |
| - await client.sendNotification(DidChangeTextDocumentNotification.type, { |
90 |
| - textDocument: { uri: documentUri, version: 1 }, |
91 |
| - contentChanges: [] |
92 |
| - }); |
| 67 | + await client.openDocument("./subject.schema.json", `{ "$schema": "" }`); |
93 | 68 |
|
94 | 69 | const diagnostics = await diagnosticsPromise;
|
95 | 70 | expect(diagnostics[0].message).to.eql("Unknown dialect");
|
96 | 71 | });
|
97 | 72 |
|
98 | 73 | test("test unknown dialect when default dialect is unknown", async () => {
|
99 |
| - documentUri = await openDocument(client, "./subject.schema.json", `{}`); |
100 |
| - |
101 |
| - client.onRequest(ConfigurationRequest.type, () => { |
102 |
| - return [{ "defaultDialect": "" }]; |
103 |
| - }); |
| 74 | + await client.changeConfiguration({ defaultDialect: "https://example.com/unknown-dialect" }); |
104 | 75 |
|
105 | 76 | const diagnosticsPromise = new Promise<Diagnostic[]>((resolve) => {
|
106 | 77 | client.onNotification(PublishDiagnosticsNotification.type, (params) => {
|
107 | 78 | resolve(params.diagnostics);
|
108 | 79 | });
|
109 | 80 | });
|
110 | 81 |
|
111 |
| - await client.sendNotification(DidChangeTextDocumentNotification.type, { |
112 |
| - textDocument: { uri: documentUri, version: 1 }, |
113 |
| - contentChanges: [] |
114 |
| - }); |
| 82 | + await client.openDocument("./subject.schema.json", `{}`); |
115 | 83 |
|
116 | 84 | const diagnostics = await diagnosticsPromise;
|
117 | 85 | expect(diagnostics[0].message).to.eql("Unknown dialect");
|
118 | 86 | });
|
| 87 | + |
| 88 | + test("watches only specified files", async () => { |
| 89 | + await client.changeConfiguration({ "schemaFilePatterns": ["**/subjectB.schema.json"] }); |
| 90 | + |
| 91 | + const diagnosticsPromise = new Promise<string>((resolve) => { |
| 92 | + client.onNotification(PublishDiagnosticsNotification.type, (params) => { |
| 93 | + resolve(params.uri); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + await client.openDocument("./subject.schema.json", "{}"); |
| 98 | + const documentUriB = await client.openDocument("./subjectB.schema.json", "{}"); |
| 99 | + |
| 100 | + const diagnostics = await diagnosticsPromise; |
| 101 | + expect(diagnostics).to.equal(documentUriB); |
| 102 | + }); |
119 | 103 | });
|
0 commit comments