-
Notifications
You must be signed in to change notification settings - Fork 312
Expand file tree
/
Copy pathApiChecker.test.ts
More file actions
161 lines (129 loc) · 5.74 KB
/
ApiChecker.test.ts
File metadata and controls
161 lines (129 loc) · 5.74 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
158
159
160
161
import { AbsoluteFilePath } from "@fern-api/fs-utils";
import { NOOP_LOGGER } from "@fern-api/logger";
import { join } from "path";
import { describe, expect, it } from "vitest";
import { ApiChecker } from "../api/checker/ApiChecker.js";
import { loadFernYml } from "../config/fern-yml/loadFernYml.js";
import { WorkspaceLoader } from "../workspace/WorkspaceLoader.js";
import { createTestContext } from "./utils/createTestContext.js";
import { loadWorkspace } from "./utils/loadWorkspace.js";
const FIXTURES_DIR = AbsoluteFilePath.of(join(__dirname, "fixtures"));
describe("ApiChecker", () => {
const logger = NOOP_LOGGER;
describe("check()", () => {
it("returns valid result for valid API definition", async () => {
const cwd = AbsoluteFilePath.of(join(FIXTURES_DIR, "simple-api"));
const fernYml = await loadFernYml({ cwd });
const loader = new WorkspaceLoader({ cwd, logger });
const result = await loader.load({ fernYml });
expect(result.success).toBe(true);
if (!result.success) {
return;
}
const checker = new ApiChecker({
context: await createTestContext({ cwd }),
cliVersion: "0.0.0"
});
const checkResult = await checker.check({
workspace: result.workspace
});
expect(checkResult.validApis.has("api")).toBe(true);
expect(checkResult.invalidApis.size).toBe(0);
expect(checkResult.errorCount).toBe(0);
expect(checkResult.warningCount).toBe(0);
expect(checkResult.violations).toHaveLength(0);
});
it("checks only specified APIs when apiNames is provided", async () => {
const cwd = AbsoluteFilePath.of(join(FIXTURES_DIR, "simple-api"));
const fernYml = await loadFernYml({ cwd });
const loader = new WorkspaceLoader({ cwd, logger });
const result = await loader.load({ fernYml });
expect(result.success).toBe(true);
if (!result.success) {
return;
}
const checker = new ApiChecker({
context: await createTestContext({ cwd }),
cliVersion: "0.0.0"
});
const checkResult = await checker.check({
workspace: result.workspace,
apiNames: ["api"]
});
expect(checkResult.validApis.has("api")).toBe(true);
expect(checkResult.invalidApis.size).toBe(0);
});
it("marks unknown API as invalid", async () => {
const cwd = AbsoluteFilePath.of(join(FIXTURES_DIR, "simple-api"));
const workspace = await loadWorkspace("simple-api");
const checker = new ApiChecker({
context: await createTestContext({ cwd }),
cliVersion: "0.0.0"
});
const checkResult = await checker.check({
workspace,
apiNames: ["nonexistent-api"]
});
expect(checkResult.invalidApis.has("nonexistent-api")).toBe(true);
expect(checkResult.validApis.size).toBe(0);
});
it("returns early with empty result when no APIs to check", async () => {
const cwd = AbsoluteFilePath.of(join(FIXTURES_DIR, "simple-api"));
const workspace = await loadWorkspace("simple-api");
const checker = new ApiChecker({
context: await createTestContext({ cwd }),
cliVersion: "0.0.0"
});
const checkResult = await checker.check({
workspace,
apiNames: []
});
expect(checkResult.validApis.size).toBe(0);
expect(checkResult.invalidApis.size).toBe(0);
expect(checkResult.errorCount).toBe(0);
expect(checkResult.warningCount).toBe(0);
expect(checkResult.violations).toHaveLength(0);
});
it("includes elapsed time in result", async () => {
const cwd = AbsoluteFilePath.of(join(FIXTURES_DIR, "simple-api"));
const workspace = await loadWorkspace("simple-api");
const checker = new ApiChecker({
context: await createTestContext({ cwd }),
cliVersion: "0.0.0"
});
const checkResult = await checker.check({
workspace
});
expect(checkResult.elapsedMillis).toBeGreaterThanOrEqual(0);
});
});
describe("violations returned in result", () => {
it("returns empty violations for valid workspace", async () => {
const cwd = AbsoluteFilePath.of(join(FIXTURES_DIR, "simple-api"));
const workspace = await loadWorkspace("simple-api");
const checker = new ApiChecker({
context: await createTestContext({ cwd }),
cliVersion: "0.0.0"
});
const checkResult = await checker.check({
workspace
});
expect(checkResult.violations).toHaveLength(0);
});
});
describe("duration formatting", () => {
it("formats duration in milliseconds for short times", async () => {
const cwd = AbsoluteFilePath.of(join(FIXTURES_DIR, "simple-api"));
const workspace = await loadWorkspace("simple-api");
const checker = new ApiChecker({
context: await createTestContext({ cwd }),
cliVersion: "0.0.0"
});
const checkResult = await checker.check({
workspace
});
// Just verify elapsed time is recorded.
expect(typeof checkResult.elapsedMillis).toBe("number");
});
});
});