summaryrefslogtreecommitdiffhomepage
path: root/packages/lsp/src/watched-files.test.ts
blob: 46298ad0097e3595588d3dda95e1c33e960a7dec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { describe, expect, it } from "vitest";
import { FileChangeType, globMatch, WatchedFilesRegistry } from "./watched-files.js";

describe("watched-files", () => {
  it("register stores workspace/didChangeWatchedFiles watchers", () => {
    const registry = new WatchedFilesRegistry();

    registry.applyRegister({
      id: "reg-1",
      method: "workspace/didChangeWatchedFiles",
      registerOptions: {
        watchers: [{ globPattern: "**/*.luau" }, { globPattern: "sourcemap.json" }],
      },
    });

    const watchers = registry.getAllWatchers();
    expect(watchers).toHaveLength(2);
    expect(watchers[0]?.globPattern).toBe("**/*.luau");
    expect(watchers[1]?.globPattern).toBe("sourcemap.json");
  });

  it("a changed path matching a registered glob is forwarded as a didChangeWatchedFiles notification with the correct FileChangeType", () => {
    const registry = new WatchedFilesRegistry();

    registry.applyRegister({
      id: "reg-1",
      method: "workspace/didChangeWatchedFiles",
      registerOptions: {
        watchers: [{ globPattern: "**/*.luau" }],
      },
    });

    expect(registry.matches("src/main.luau")).toBe(true);
    expect(registry.matches("src/nested/deep/file.luau")).toBe(true);
    expect(registry.matches("src/main.ts")).toBe(false);

    expect(FileChangeType.Created).toBe(1);
    expect(FileChangeType.Changed).toBe(2);
    expect(FileChangeType.Deleted).toBe(3);
  });

  it("unregisterCapability stops forwarding", () => {
    const registry = new WatchedFilesRegistry();

    registry.applyRegister({
      id: "reg-1",
      method: "workspace/didChangeWatchedFiles",
      registerOptions: {
        watchers: [{ globPattern: "**/*.luau" }],
      },
    });

    expect(registry.matches("src/main.luau")).toBe(true);

    registry.applyUnregister({
      id: "reg-1",
      method: "workspace/didChangeWatchedFiles",
    });

    expect(registry.matches("src/main.luau")).toBe(false);
    expect(registry.getAllWatchers()).toHaveLength(0);
  });

  it("glob matching covers double-star-star.luau, sourcemap.json, double-star/sourcemap.json", () => {
    // **/*.luau
    expect(globMatch("**/*.luau", "src/main.luau")).toBe(true);
    expect(globMatch("**/*.luau", "deep/nested/file.luau")).toBe(true);
    expect(globMatch("**/*.luau", "file.luau")).toBe(true);
    expect(globMatch("**/*.luau", "src/main.ts")).toBe(false);

    // sourcemap.json (literal)
    expect(globMatch("sourcemap.json", "sourcemap.json")).toBe(true);
    expect(globMatch("sourcemap.json", "other.json")).toBe(false);

    // **/sourcemap.json
    expect(globMatch("**/sourcemap.json", "sourcemap.json")).toBe(true);
    expect(globMatch("**/sourcemap.json", "build/sourcemap.json")).toBe(true);
    expect(globMatch("**/sourcemap.json", "deep/nested/sourcemap.json")).toBe(true);
    expect(globMatch("**/sourcemap.json", "other.json")).toBe(false);
  });
});