|
| 1 | +import { afterAll, beforeAll, describe, expect, test } from "vitest"; |
| 2 | +import { |
| 3 | + PublishDiagnosticsNotification, |
| 4 | + DidChangeWatchedFilesNotification, |
| 5 | + WorkDoneProgress, |
| 6 | + WorkDoneProgressCreateRequest |
| 7 | +} from "vscode-languageserver"; |
| 8 | +import { getTestClient, closeDocument, initializeServer, openDocument, setupWorkspace, tearDownWorkspace } 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 | +import { resolveIri } from "@hyperjump/uri"; |
| 14 | + |
| 15 | +import type { Connection } from "vscode-languageserver"; |
| 16 | + |
| 17 | + |
| 18 | +describe("Feature - Watch File Patterns", () => { |
| 19 | + let client: Connection; |
| 20 | + let documentUri: string; |
| 21 | + let documentUriB: string; |
| 22 | + let workspaceFolder: string; |
| 23 | + |
| 24 | + beforeAll(async () => { |
| 25 | + client = getTestClient([ |
| 26 | + workspace, |
| 27 | + documentSettings, |
| 28 | + schemaRegistry, |
| 29 | + validationErrorsFeature |
| 30 | + ]); |
| 31 | + |
| 32 | + workspaceFolder = await setupWorkspace({ |
| 33 | + "subject.schema.json": `{}`, |
| 34 | + "subjectB.schema.json": `{}` |
| 35 | + }); |
| 36 | + |
| 37 | + const init = { |
| 38 | + workspaceFolders: [ |
| 39 | + { |
| 40 | + name: "root", |
| 41 | + uri: workspaceFolder |
| 42 | + } |
| 43 | + ] |
| 44 | + }; |
| 45 | + |
| 46 | + await initializeServer(client, init); |
| 47 | + |
| 48 | + documentUri = resolveIri("./subject.schema.json", `${workspaceFolder}/`); |
| 49 | + documentUriB = resolveIri("./subjectB.schema.json", `${workspaceFolder}/`); |
| 50 | + await openDocument(client, documentUri); |
| 51 | + await openDocument(client, documentUriB); |
| 52 | + }); |
| 53 | + |
| 54 | + afterAll(async () => { |
| 55 | + await closeDocument(client, documentUri); |
| 56 | + await closeDocument(client, documentUriB); |
| 57 | + |
| 58 | + await tearDownWorkspace(workspaceFolder); |
| 59 | + }); |
| 60 | + |
| 61 | + test("watches all files", async () => { |
| 62 | + const validatedSchemasPromise = new Promise((resolve) => { |
| 63 | + let schemaUris: string[]; |
| 64 | + |
| 65 | + client.onRequest(WorkDoneProgressCreateRequest.type, ({ token }) => { |
| 66 | + client.onProgress(WorkDoneProgress.type, token, ({ kind }) => { |
| 67 | + if (kind === "begin") { |
| 68 | + schemaUris = []; |
| 69 | + } else if (kind === "end") { |
| 70 | + resolve(schemaUris); |
| 71 | + } |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + client.onNotification(PublishDiagnosticsNotification.type, (params) => { |
| 76 | + schemaUris.push(params.uri); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + await client.sendNotification(DidChangeWatchedFilesNotification.type, { |
| 81 | + changes: [] |
| 82 | + }); |
| 83 | + |
| 84 | + const validatedSchemas = await validatedSchemasPromise; |
| 85 | + expect(validatedSchemas).to.contains(documentUri); |
| 86 | + expect(validatedSchemas).to.contains(documentUriB); |
| 87 | + }); |
| 88 | +}); |
0 commit comments