Skip to content

Commit 55a0a84

Browse files
diyaayayjdesrosiers
authored andcommitted
Test for default dialect
1 parent 774d8a9 commit 55a0a84

File tree

7 files changed

+144
-14
lines changed

7 files changed

+144
-14
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import { afterAll, beforeAll, describe, expect, test } from "vitest";
2+
import { ConfigurationRequest, DidChangeTextDocumentNotification, PublishDiagnosticsNotification } from "vscode-languageserver/node";
3+
import {
4+
getTestClient,
5+
closeDocument,
6+
initializeServer,
7+
openDocument
8+
} from "../test-utils.js";
9+
import documentSettings from "./document-settings.js";
10+
import schemaRegistry from "./schema-registry.js";
11+
import workspace from "./workspace.js";
12+
import validationErrorsFeature from "./validation-errors.js";
13+
14+
15+
describe("Feature - Document Settings", () => {
16+
let client;
17+
let documentUri;
18+
19+
beforeAll(async () => {
20+
client = getTestClient([
21+
workspace,
22+
documentSettings,
23+
schemaRegistry,
24+
validationErrorsFeature
25+
]);
26+
const init = {};
27+
const settings = { "defaultDialect": "https://json-schema.org/draft/2020-12/schema" };
28+
await initializeServer(client, init, settings);
29+
});
30+
31+
afterAll(async () => {
32+
await closeDocument(client, documentUri);
33+
});
34+
35+
test("test default dialect", async () => {
36+
documentUri = "file:///path/to/workspace/subjectA.schema.json";
37+
await openDocument(client, documentUri, `{}`);
38+
39+
const diagnosticsPromise = new Promise((resolve) => {
40+
client.onNotification(PublishDiagnosticsNotification, (params) => {
41+
resolve(params.diagnostics);
42+
});
43+
});
44+
45+
const params = {
46+
textDocument: { uri: documentUri },
47+
contentChanges: []
48+
};
49+
50+
await client.sendNotification(DidChangeTextDocumentNotification, params);
51+
52+
const diagnostics = await diagnosticsPromise;
53+
expect(diagnostics).to.eql([]);
54+
});
55+
56+
test("test no dialect", async () => {
57+
documentUri = "file:///path/to/workspace/subjectB.schema.json";
58+
await openDocument(client, documentUri, `{}`);
59+
60+
await client.onRequest(ConfigurationRequest, () => {
61+
return [{}];
62+
});
63+
64+
const diagnosticsPromise = new Promise((resolve) => {
65+
client.onNotification(PublishDiagnosticsNotification, (params) => {
66+
resolve(params.diagnostics);
67+
});
68+
});
69+
70+
const params = {
71+
textDocument: { uri: documentUri },
72+
contentChanges: []
73+
};
74+
75+
await client.sendNotification(DidChangeTextDocumentNotification, params);
76+
77+
const diagnostics = await diagnosticsPromise;
78+
expect(diagnostics[0].message).to.eql("No dialect");
79+
});
80+
81+
test("test unknown dialect", async () => {
82+
documentUri = "file:///path/to/workspace/subjectC.schema.json";
83+
await openDocument(client, documentUri, `{"$schema":""}`);
84+
85+
await client.onRequest(ConfigurationRequest, () => {
86+
return [{}];
87+
});
88+
89+
const diagnosticsPromise = new Promise((resolve) => {
90+
client.onNotification(PublishDiagnosticsNotification, (params) => {
91+
resolve(params.diagnostics);
92+
});
93+
});
94+
95+
const params = {
96+
textDocument: { uri: documentUri },
97+
contentChanges: []
98+
};
99+
100+
await client.sendNotification(DidChangeTextDocumentNotification, params);
101+
102+
const diagnostics = await diagnosticsPromise;
103+
expect(diagnostics[0].message).to.eql("Unknown dialect");
104+
});
105+
106+
test("test unknown dialect when default dialect is unknown", async () => {
107+
documentUri = "file:///path/to/workspace/subjectD.schema.json";
108+
await openDocument(client, documentUri, `{}`);
109+
110+
await client.onRequest(ConfigurationRequest, () => {
111+
return [{ "defaultDialect": "" }];
112+
});
113+
114+
const diagnosticsPromise = new Promise((resolve) => {
115+
client.onNotification(PublishDiagnosticsNotification, (params) => {
116+
resolve(params.diagnostics);
117+
});
118+
});
119+
120+
const params = {
121+
textDocument: { uri: documentUri },
122+
contentChanges: []
123+
};
124+
125+
await client.sendNotification(DidChangeTextDocumentNotification, params);
126+
127+
const diagnostics = await diagnosticsPromise;
128+
expect(diagnostics[0].message).to.eql("Unknown dialect");
129+
});
130+
});

language-server/src/features/if-then-completion.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe("Feature - if/then completion", () => {
1414
});
1515

1616
test("if/then completion with colon", async () => {
17-
const documentUri = "file://path/to/workspace/subject1.schema.json";
17+
const documentUri = "file:///path/to/workspace/subject1.schema.json";
1818
await openDocument(client, documentUri, `{
1919
"if":
2020
}`);
@@ -36,7 +36,7 @@ describe("Feature - if/then completion", () => {
3636
});
3737

3838
test("if/then completion with space", async () => {
39-
const documentUri = "file://path/to/workspace/subject2.schema.json";
39+
const documentUri = "file:///path/to/workspace/subject2.schema.json";
4040
await openDocument(client, documentUri, `{
4141
"if":
4242
}`);
@@ -58,7 +58,7 @@ describe("Feature - if/then completion", () => {
5858
});
5959

6060
test("if/then completion on property key", async () => {
61-
const documentUri = "file://path/to/workspace/subject3.schema.json";
61+
const documentUri = "file:///path/to/workspace/subject3.schema.json";
6262
await openDocument(client, documentUri, `{
6363
"if":
6464
}`);
@@ -80,7 +80,7 @@ describe("Feature - if/then completion", () => {
8080
});
8181

8282
test("if/then completion on property value", async () => {
83-
const documentUri = "file://path/to/workspace/subject4.schema.json";
83+
const documentUri = "file:///path/to/workspace/subject4.schema.json";
8484
await openDocument(client, documentUri, `{
8585
"if": ""
8686
}`);
@@ -102,7 +102,7 @@ describe("Feature - if/then completion", () => {
102102
});
103103

104104
test("if/then completion without colon", async () => {
105-
const documentUri = "file://path/to/workspace/subject5.schema.json";
105+
const documentUri = "file:///path/to/workspace/subject5.schema.json";
106106
await openDocument(client, documentUri, `{
107107
"if"
108108
}`);

language-server/src/features/schema-completion.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe("Feature - $schema completion", () => {
1414
});
1515

1616
test("$schema completion with string", async () => {
17-
const documentUri = "file://path/to/workspace/subject1.schema.json";
17+
const documentUri = "file:///path/to/workspace/subject1.schema.json";
1818
await openDocument(client, documentUri, `{
1919
"$schema": ""
2020
}`);
@@ -35,7 +35,7 @@ describe("Feature - $schema completion", () => {
3535
});
3636

3737
test("$schema completion with colon", async () => {
38-
const documentUri = "file://path/to/workspace/subject2.schema.json";
38+
const documentUri = "file:///path/to/workspace/subject2.schema.json";
3939
await openDocument(client, documentUri, `{
4040
"$schema":
4141
}`);
@@ -56,7 +56,7 @@ describe("Feature - $schema completion", () => {
5656
});
5757

5858
test("$schema completion with colon and space", async () => {
59-
const documentUri = "file://path/to/workspace/subject3.schema.json";
59+
const documentUri = "file:///path/to/workspace/subject3.schema.json";
6060
await openDocument(client, documentUri, `{
6161
"$schema":
6262
}`);
@@ -77,7 +77,7 @@ describe("Feature - $schema completion", () => {
7777
});
7878

7979
test("$schema completion without colon", async () => {
80-
const documentUri = "file://path/to/workspace/subject4.schema.json";
80+
const documentUri = "file:///path/to/workspace/subject4.schema.json";
8181
await openDocument(client, documentUri, `{
8282
"$schema"
8383
}`);

language-server/src/features/workspace.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe("Feature - workspace", () => {
2929
client = getTestClient([workspace, documentSettings, schemaRegistry]);
3030

3131
workspaceFolder = await setupWorkspace({
32-
"subject.schema.json": `{ "$schema": "https://json-schema.org/draft/2020-12/cshema" }`
32+
"subject.schema.json": `{ "$schema": "https://json-schema.org/draft/2020-12/schema" }`
3333
});
3434

3535
/**

language-server/src/schema-document.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const fromTextDocument = async (textDocument, contextDialectUri) => {
3737
instanceNode: $schema,
3838
message: "Unknown dialect"
3939
});
40-
} else if (schemaResource.dialectUri) {
40+
} else if (schemaResource.dialectUri !== undefined) {
4141
document.errors.push({
4242
keyword: "https://json-schema.org/keyword/schema",
4343
instanceNode: schemaResource,
@@ -72,7 +72,7 @@ export const fromTextDocument = async (textDocument, contextDialectUri) => {
7272
return document;
7373
};
7474

75-
const buildSchemaResources = (document, node, uri = "", dialectUri = "", pointer = "", parent = undefined, anchors = {}) => {
75+
const buildSchemaResources = (document, node, uri = "", dialectUri = undefined, pointer = "", parent = undefined, anchors = {}) => {
7676
const schemaNode = SchemaNode.cons(uri, pointer, getNodeValue(node), node.type, [], parent, node.offset, node.length, dialectUri, anchors);
7777

7878
switch (node.type) {

language-server/src/schema-node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const cons = (uri, pointer, value, type, children, parent, offset, textLe
1616

1717
export const get = (uri, node) => {
1818
const schemaId = toAbsoluteUri(resolveIri(uri, node?.baseUri));
19-
const schemaResource = getSchemaResource(schemaId);
19+
const schemaResource = node.baseUri === schemaId ? node : getSchemaResource(schemaId);
2020
if (!schemaResource) {
2121
return;
2222
}

language-server/src/test-utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export const initializeServer = async (client, initParams = {}, settings = null)
8585
// TODO: This is a hack. Find a way to know when initialized is finished
8686
// Block for a while to allow InitializedNotification time to finish
8787
await new Promise((resolve) => {
88-
setTimeout(resolve, 10);
88+
setTimeout(resolve, 15);
8989
});
9090

9191
return response.capabilities;

0 commit comments

Comments
 (0)