-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathvalidator.test.ts
More file actions
157 lines (126 loc) · 5.25 KB
/
validator.test.ts
File metadata and controls
157 lines (126 loc) · 5.25 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import * as vscode from "vscode";
import { WitSyntaxValidator } from "../src/validator.js";
// Mock VS Code API
vi.mock("vscode", () => ({
languages: {
createDiagnosticCollection: vi.fn(() => ({
delete: vi.fn(),
clear: vi.fn(),
set: vi.fn(),
dispose: vi.fn(),
})),
},
DiagnosticCollection: vi.fn(),
extensions: {
getExtension: vi.fn(() => ({
extensionPath: "/mock/extension/path",
})),
},
}));
// Mock the WASM utilities
vi.mock("../src/wasmUtils.js", () => ({
validateWitSyntaxDetailedFromWasm: vi.fn(),
}));
describe("WitSyntaxValidator", () => {
let validator: WitSyntaxValidator;
beforeEach(() => {
validator = new WitSyntaxValidator();
});
afterEach(() => {
validator.dispose();
});
describe("getDiagnosticCollection", () => {
it("should return a diagnostic collection", () => {
const collection = validator.getDiagnosticCollection();
expect(collection).toBeDefined();
});
});
describe("validate", () => {
it("should return null for valid WIT files", async () => {
const { validateWitSyntaxDetailedFromWasm } = await import("../src/wasmUtils.js");
vi.mocked(validateWitSyntaxDetailedFromWasm).mockResolvedValueOnce({ valid: true });
const result = await validator.validate("/test/file.wit", "valid content");
expect(result).toBeNull();
});
it("should return error info for invalid WIT files", async () => {
const { validateWitSyntaxDetailedFromWasm } = await import("../src/wasmUtils.js");
vi.mocked(validateWitSyntaxDetailedFromWasm).mockResolvedValueOnce({
valid: false,
error: "undefined type `type4`",
errorType: "resolution",
});
const result = await validator.validate("/test/file.wit", "invalid content");
expect(result).not.toBeNull();
expect(result?.mainError).toBe("Undefined type error");
expect(result?.detailedError).toBe(
"Undefined type 'type4' - check if the type is defined or imported correctly"
);
expect(result?.filePath).toBe("/test/file.wit");
expect(result?.row).toBe(1);
expect(result?.column).toBe(1);
});
it("should handle WASM validation errors", async () => {
const { validateWitSyntaxDetailedFromWasm } = await import("../src/wasmUtils.js");
const mockError = new Error("WASM validation error");
mockError.stack = `Error: reading WIT file at [/test/file.wit]
Caused by:
expected '{', found keyword \`interface\`
--> /test/file.wit:3:1
|
3 | interface test {
| ^`;
vi.mocked(validateWitSyntaxDetailedFromWasm).mockRejectedValueOnce(mockError);
const result = await validator.validate("/test/file.wit", "invalid content");
expect(result).not.toBeNull();
expect(result?.mainError).toBe("reading WIT file at [/test/file.wit]");
expect(result?.detailedError).toBe("expected '{', found keyword `interface`");
expect(result?.filePath).toBe("/test/file.wit");
expect(result?.row).toBe(3);
expect(result?.column).toBe(1);
});
it("should detect undefined types in WIT files", async () => {
const { validateWitSyntaxDetailedFromWasm } = await import("../src/wasmUtils.js");
// Simulate the WASM validator returning false for invalid content with undefined types
vi.mocked(validateWitSyntaxDetailedFromWasm).mockResolvedValueOnce({
valid: false,
error: "undefined type `type4`",
errorType: "resolution",
});
const invalidWitContent = `package foo:foo;
interface i {
type type1 = u32;
}
world foo {
use i.{type1};
type type2 = u32;
record type3 {
r: u32,
}
export foo: func() -> tuple<type1, type2, type3, type4>;
}`;
const result = await validator.validate("/test/worlds-with-types.wit", invalidWitContent);
expect(result).not.toBeNull();
expect(result?.mainError).toBe("Undefined type error");
expect(result?.detailedError).toBe(
"Undefined type 'type4' - check if the type is defined or imported correctly"
);
expect(result?.filePath).toBe("/test/worlds-with-types.wit");
});
});
describe("clearDiagnostics", () => {
it("should call delete on diagnostic collection", () => {
const mockUri = {} as vscode.Uri;
const collection = validator.getDiagnosticCollection();
validator.clearDiagnostics(mockUri);
expect(collection.delete).toHaveBeenCalledWith(mockUri);
});
});
describe("clearAllDiagnostics", () => {
it("should call clear on diagnostic collection", () => {
const collection = validator.getDiagnosticCollection();
validator.clearAllDiagnostics();
expect(collection.clear).toHaveBeenCalled();
});
});
});