|
| 1 | +import { beforeAll, afterAll, describe, expect, test } from "vitest"; |
| 2 | +import { ConfigurationRequest, SemanticTokensRequest, DidChangeConfigurationNotification } from "vscode-languageserver"; |
| 3 | +import { getTestClient, initializeServer, openDocument, closeDocument } from "../test-utils.js"; |
| 4 | +import semanticTokensFeature from "./semantic-tokens.js"; |
| 5 | +import workspace from "./workspace.js"; |
| 6 | +import documentSettings from "./document-settings.js"; |
| 7 | + |
| 8 | +import type { Connection } from "vscode-languageserver"; |
| 9 | + |
| 10 | + |
| 11 | +describe("Feature - Semantic Tokens", () => { |
| 12 | + let client: Connection; |
| 13 | + let documentUri: string; |
| 14 | + |
| 15 | + beforeAll(async () => { |
| 16 | + client = getTestClient([ |
| 17 | + semanticTokensFeature, |
| 18 | + documentSettings, |
| 19 | + workspace |
| 20 | + ]); |
| 21 | + |
| 22 | + const init = {}; |
| 23 | + const settings = { "schemaFilePatterns": ["**/subject.schema.json"] }; |
| 24 | + await initializeServer(client, init, settings); |
| 25 | + }); |
| 26 | + |
| 27 | + afterAll(async () => { |
| 28 | + await closeDocument(client, documentUri); |
| 29 | + }); |
| 30 | + |
| 31 | + test("semantic tokens on a watched file", async () => { |
| 32 | + documentUri = await openDocument(client, "subject.schema.json", `{"$schema":"http://json-schema.org/draft-07/schema#", |
| 33 | +"type": "string", |
| 34 | +"minLength": 10, |
| 35 | +"maxLength": 5 |
| 36 | +}`); |
| 37 | + |
| 38 | + const response = await client.sendRequest(SemanticTokensRequest.type, { |
| 39 | + textDocument: { uri: documentUri } |
| 40 | + }); |
| 41 | + |
| 42 | + expect(response?.data).to.eql(expectedData); |
| 43 | + }); |
| 44 | + |
| 45 | + test("no semantic tokens", async () => { |
| 46 | + documentUri = await openDocument(client, "subject.schema.json", `{ |
| 47 | +"type": "string", |
| 48 | +"minLength": 10, |
| 49 | +"maxLength": 5}`); |
| 50 | + |
| 51 | + const response = await client.sendRequest(SemanticTokensRequest.type, { |
| 52 | + textDocument: { uri: documentUri } |
| 53 | + }); |
| 54 | + |
| 55 | + expect(response?.data).to.eql([]); |
| 56 | + }); |
| 57 | + |
| 58 | + test("no semantic tokens on an unwatched file", async () => { |
| 59 | + documentUri = await openDocument(client, "subjectB.schema.json", `{"$schema":"http://json-schema.org/draft-07/schema#", |
| 60 | + "type": "string", |
| 61 | + "minLength": 10, |
| 62 | + "maxLength": 5 |
| 63 | + }`); |
| 64 | + |
| 65 | + const response = await client.sendRequest(SemanticTokensRequest.type, { |
| 66 | + textDocument: { uri: documentUri } |
| 67 | + }); |
| 68 | + |
| 69 | + expect(response?.data).to.eql([]); |
| 70 | + }); |
| 71 | + |
| 72 | + test("change in watch file patterns refreshes tokens", async () => { |
| 73 | + documentUri = await openDocument(client, "subjectC.schema.json", `{"$schema":"http://json-schema.org/draft-07/schema#", |
| 74 | +"type": "string", |
| 75 | +"minLength": 10, |
| 76 | +"maxLength": 5 |
| 77 | +}`); |
| 78 | + |
| 79 | + client.onRequest(ConfigurationRequest.type, () => { |
| 80 | + return [{ "schemaFilePatterns": ["**/subjectC.schema.json"] }]; |
| 81 | + }); |
| 82 | + |
| 83 | + await client.sendNotification(DidChangeConfigurationNotification.type, { settings: { "schemaFilePatterns": ["**/subjectC.schema.json"] } }); |
| 84 | + const response = await client.sendRequest(SemanticTokensRequest.type, { |
| 85 | + textDocument: { uri: documentUri } |
| 86 | + }); |
| 87 | + |
| 88 | + expect(response?.data).to.eql(expectedData); |
| 89 | + }); |
| 90 | +}); |
| 91 | + |
| 92 | +const expectedData = [ |
| 93 | + 0, 1, 9, 0, 0, 1, 0, |
| 94 | + 6, 0, 0, 1, 0, 11, 0, |
| 95 | + 0, 1, 0, 11, 0, 0 |
| 96 | +]; |
| 97 | + |
| 98 | + |
0 commit comments