Repository navigation
Expand file tree
/
Copy pathmoduleSetup.test.ts
More file actions
133 lines (121 loc) · 4.71 KB
/
Copy pathmoduleSetup.test.ts
File metadata and controls
133 lines (121 loc) · 4.71 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
import assert from "node:assert/strict";
import test from "node:test";
import {
buildModuleSetupPlan,
executeModuleSetup,
moduleSetupConfirmation,
type ModuleSetupOperations,
type ModuleSetupStepResult,
} from "../src/moduleSetup";
test("只信任且不忙时可开始一键配置", () => {
assert.deepEqual(buildModuleSetupPlan(true, false), { kind: "ready" });
assert.deepEqual(buildModuleSetupPlan(false, false), { kind: "blocked", reason: "untrusted" });
assert.deepEqual(buildModuleSetupPlan(true, true), { kind: "blocked", reason: "busy" });
});
test("确认文案不再要求切换 LLVM、下载 llvm-tools 或配置 clangd", () => {
const confirmation = moduleSetupConfirmation();
assert.match(confirmation.message, /构建.*刷新 C\+\+ 模块语言服务/);
assert.match(confirmation.detail, /mcpp build/);
assert.match(confirmation.detail, /C\+\+ Modules/);
assert.doesNotMatch(confirmation.detail, /LLVM|llvm-tools/i);
assert.doesNotMatch(confirmation.detail, /配置 clangd/i);
});
test("成功路径严格执行 build 后刷新语言服务", async () => {
const calls: string[] = [];
const operations: ModuleSetupOperations = {
build: async () => {
calls.push("build");
return { stage: "build", state: "succeeded" };
},
refreshLanguageServer: async () => {
calls.push("language-server");
return { stage: "language-server", state: "succeeded" };
},
};
const decision = buildModuleSetupPlan(true, false);
assert.equal(decision.kind, "ready");
if (decision.kind !== "ready") return;
const outcome = await executeModuleSetup(decision, operations);
assert.deepEqual(outcome, { state: "succeeded", degraded: false, steps: [
{ stage: "build", state: "succeeded" },
{ stage: "language-server", state: "succeeded" },
] });
assert.deepEqual(calls, ["build", "language-server"]);
});
test("build 失败但未取消时仍刷新已有语言服务描述", async () => {
const calls: string[] = [];
const decision = buildModuleSetupPlan(true, false);
assert.equal(decision.kind, "ready");
if (decision.kind !== "ready") return;
const outcome = await executeModuleSetup(decision, {
build: async () => {
calls.push("build");
return { stage: "build", state: "failed", exitCode: 2 };
},
refreshLanguageServer: async () => {
calls.push("language-server");
return { stage: "language-server", state: "succeeded" };
},
});
assert.equal(outcome.state, "succeeded");
if (outcome.state === "succeeded") assert.equal(outcome.degraded, true);
assert.deepEqual(calls, ["build", "language-server"]);
});
test("build 取消后不刷新语言服务", async () => {
const calls: string[] = [];
const decision = buildModuleSetupPlan(true, false);
assert.equal(decision.kind, "ready");
if (decision.kind !== "ready") return;
const outcome = await executeModuleSetup(decision, {
build: async () => {
calls.push("build");
return { stage: "build", state: "cancelled" };
},
refreshLanguageServer: async () => {
calls.push("language-server");
return { stage: "build", state: "succeeded" };
},
});
assert.equal(outcome.state, "cancelled");
assert.equal(outcome.stage, "build");
assert.deepEqual(calls, ["build"]);
});
test("build 未启动时不刷新语言服务", async () => {
const calls: string[] = [];
const decision = buildModuleSetupPlan(true, false);
assert.equal(decision.kind, "ready");
if (decision.kind !== "ready") return;
const outcome = await executeModuleSetup(decision, {
build: async () => {
calls.push("build");
return { stage: "build", state: "not-started", detail: "未启动 mcpp 构建。" };
},
refreshLanguageServer: async () => {
calls.push("language-server");
return { stage: "language-server", state: "succeeded" };
},
});
assert.equal(outcome.state, "failed");
if (outcome.state === "failed") assert.equal(outcome.stage, "build");
assert.deepEqual(calls, ["build"]);
});
test("语言服务依赖缺失时不伪装成成功", async () => {
const result: ModuleSetupStepResult = {
stage: "language-server",
state: "failed",
detail: "C++ 模块语言服务依赖未安装或已禁用。",
};
const decision = buildModuleSetupPlan(true, false);
assert.equal(decision.kind, "ready");
if (decision.kind !== "ready") return;
const outcome = await executeModuleSetup(decision, {
build: async () => ({ stage: "build", state: "succeeded" }),
refreshLanguageServer: async () => result,
});
assert.equal(outcome.state, "failed");
if (outcome.state === "failed") assert.equal(outcome.stage, "language-server");
assert.deepEqual(outcome.steps, [
{ stage: "build", state: "succeeded" },
result,
]);
});