systemService.test.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. import { describe, expect, it, vi } from "vitest";
  2. import { createSystemService } from "../src/system/systemService.js";
  3. // 探测目标只是被测入参:用保留域名与文档网段 IP,不绑定内网真实地址(开源导出里内网默认地址为空)。
  4. const PROBE_HOST = "intranet-probe.example";
  5. const PROBE_SERVICE_URL = "http://intranet-probe.example:3850/api/intranet/probe";
  6. const SECONDARY_PROBE_IP = "192.0.2.151";
  7. describe("systemService", () => {
  8. it("info 会返回当前平台和 home 目录", async () => {
  9. const service = createSystemService();
  10. const info = await service.info();
  11. expect(info.platform).toBe(process.platform);
  12. expect(typeof info.homedir).toBe("string");
  13. expect(info.homedir.length).toBeGreaterThan(0);
  14. });
  15. it("listIntegratedTerminalShells 在非 Windows host 返回空列表", async () => {
  16. const service = createSystemService({ platform: "darwin" });
  17. await expect(service.listIntegratedTerminalShells()).resolves.toEqual([]);
  18. });
  19. it("listIntegratedTerminalShells 在 Windows host 返回 cmd 和可用的 Git Bash", async () => {
  20. const service = createSystemService({
  21. env: {
  22. ComSpec: "C:\\Windows\\System32\\cmd.exe",
  23. PATH: "C:\\Program Files\\Git\\cmd",
  24. },
  25. isExecutable: (path) =>
  26. [
  27. "C:\\Windows\\System32\\cmd.exe",
  28. "C:\\Program Files\\Git\\bin\\bash.exe",
  29. ].includes(path),
  30. platform: "win32",
  31. });
  32. await expect(service.listIntegratedTerminalShells()).resolves.toEqual([
  33. {
  34. dialect: "cmd",
  35. id: "cmd:C:\\Windows\\System32\\cmd.exe",
  36. label: "CMD",
  37. path: "C:\\Windows\\System32\\cmd.exe",
  38. source: "system",
  39. },
  40. {
  41. dialect: "git-bash",
  42. id: "git-bash:C:\\Program Files\\Git\\bin\\bash.exe",
  43. label: "Git Bash",
  44. path: "C:\\Program Files\\Git\\bin\\bash.exe",
  45. source: "system",
  46. },
  47. ]);
  48. });
  49. it("listIntegratedTerminalShells 按 Windows 大小写无关语义读取 PATH 和 ComSpec", async () => {
  50. const service = createSystemService({
  51. env: {
  52. ComSpec: "C:\\Windows\\System32\\cmd.exe",
  53. Path: "D:\\Tools\\Git\\cmd",
  54. },
  55. isExecutable: (path) =>
  56. [
  57. "D:\\Tools\\Git\\cmd\\git.exe",
  58. "D:\\Tools\\Git\\bin\\bash.exe",
  59. ].includes(path),
  60. platform: "win32",
  61. });
  62. await expect(service.listIntegratedTerminalShells()).resolves.toEqual([
  63. {
  64. dialect: "cmd",
  65. id: "cmd:C:\\Windows\\System32\\cmd.exe",
  66. label: "CMD",
  67. path: "C:\\Windows\\System32\\cmd.exe",
  68. source: "system",
  69. },
  70. {
  71. dialect: "git-bash",
  72. id: "git-bash:D:\\Tools\\Git\\bin\\bash.exe",
  73. label: "Git Bash",
  74. path: "D:\\Tools\\Git\\bin\\bash.exe",
  75. source: "path",
  76. },
  77. ]);
  78. });
  79. it("listIntegratedTerminalShells 在缺少 ComSpec 时展示 CMD fallback", async () => {
  80. const service = createSystemService({
  81. env: {},
  82. isExecutable: () => false,
  83. platform: "win32",
  84. });
  85. await expect(service.listIntegratedTerminalShells()).resolves.toEqual([
  86. {
  87. dialect: "cmd",
  88. id: "cmd:cmd.exe",
  89. label: "CMD",
  90. path: "cmd.exe",
  91. source: "system",
  92. },
  93. ]);
  94. });
  95. it("probeIntranet 只要命中一个探测目标就会判定为内网", async () => {
  96. const tcpProbe = vi
  97. .fn<(
  98. params: {
  99. host: string;
  100. port: number;
  101. timeoutMs: number;
  102. },
  103. ) => Promise<number>>()
  104. .mockImplementationOnce(async () => 23);
  105. const service = createSystemService({ tcpProbe, now: () => 1700000000000 });
  106. const result = await service.probeIntranet({
  107. targets: [{ host: PROBE_HOST, port: 22 }],
  108. });
  109. expect(tcpProbe).toHaveBeenCalledWith({
  110. host: PROBE_HOST,
  111. port: 22,
  112. timeoutMs: 800,
  113. });
  114. expect(result).toMatchObject({
  115. isIntranet: true,
  116. reachedTargetCount: 1,
  117. requiredSuccessCount: 1,
  118. totalTargets: 1,
  119. checkedAt: 1700000000000,
  120. });
  121. expect(result.results).toEqual([
  122. {
  123. targetId: `${PROBE_HOST}:22`,
  124. kind: "tcp",
  125. host: PROBE_HOST,
  126. port: 22,
  127. reachable: true,
  128. attemptCount: 1,
  129. latencyMs: 23,
  130. },
  131. ]);
  132. });
  133. it("probeIntranet 会按 attempts 重试并在达到阈值前保持 false", async () => {
  134. const hostCallCount = new Map<string, number>();
  135. const tcpProbe = vi.fn<
  136. (
  137. params: {
  138. host: string;
  139. port: number;
  140. timeoutMs: number;
  141. },
  142. ) => Promise<number>
  143. >(async ({ host }) => {
  144. const nextCount = (hostCallCount.get(host) ?? 0) + 1;
  145. hostCallCount.set(host, nextCount);
  146. if (host === PROBE_HOST) {
  147. if (nextCount === 1) {
  148. throw new Error("ECONNREFUSED");
  149. }
  150. return 31;
  151. }
  152. throw new Error("timeout");
  153. });
  154. const service = createSystemService({ tcpProbe, now: () => 1700000001000 });
  155. const result = await service.probeIntranet({
  156. attempts: 2,
  157. requiredSuccessCount: 2,
  158. targets: [
  159. { host: PROBE_HOST, port: 22 },
  160. { host: SECONDARY_PROBE_IP, port: 22, timeoutMs: 400 },
  161. ],
  162. });
  163. expect(tcpProbe).toHaveBeenCalledTimes(4);
  164. const calls = tcpProbe.mock.calls.map(([params]) => params);
  165. expect(calls.filter((item) => item.host === PROBE_HOST)).toEqual([
  166. {
  167. host: PROBE_HOST,
  168. port: 22,
  169. timeoutMs: 800,
  170. },
  171. {
  172. host: PROBE_HOST,
  173. port: 22,
  174. timeoutMs: 800,
  175. },
  176. ]);
  177. expect(calls.filter((item) => item.host === SECONDARY_PROBE_IP)).toEqual([
  178. {
  179. host: SECONDARY_PROBE_IP,
  180. port: 22,
  181. timeoutMs: 400,
  182. },
  183. {
  184. host: SECONDARY_PROBE_IP,
  185. port: 22,
  186. timeoutMs: 400,
  187. },
  188. ]);
  189. expect(result).toMatchObject({
  190. isIntranet: false,
  191. reachedTargetCount: 1,
  192. requiredSuccessCount: 2,
  193. totalTargets: 2,
  194. });
  195. expect(result.results).toEqual([
  196. {
  197. targetId: `${PROBE_HOST}:22`,
  198. kind: "tcp",
  199. host: PROBE_HOST,
  200. port: 22,
  201. reachable: true,
  202. attemptCount: 2,
  203. latencyMs: 31,
  204. },
  205. {
  206. targetId: `${SECONDARY_PROBE_IP}:22`,
  207. kind: "tcp",
  208. host: SECONDARY_PROBE_IP,
  209. port: 22,
  210. reachable: false,
  211. attemptCount: 2,
  212. latencyMs: null,
  213. error: "timeout",
  214. },
  215. ]);
  216. });
  217. it("probeIntranet 支持基于内网服务的 marker 探测", async () => {
  218. const serviceProbe = vi
  219. .fn<(
  220. params: {
  221. url: string;
  222. timeoutMs: number;
  223. token?: string;
  224. },
  225. ) => Promise<{ latencyMs: number; marker?: string }>>()
  226. .mockResolvedValueOnce({
  227. latencyMs: 19,
  228. marker: "zcode-intranet-lab",
  229. });
  230. const service = createSystemService({
  231. serviceProbe,
  232. now: () => 1700000002000,
  233. });
  234. const result = await service.probeIntranet({
  235. targets: [
  236. {
  237. kind: "service",
  238. url: PROBE_SERVICE_URL,
  239. expectedMarker: "zcode-intranet-lab",
  240. },
  241. ],
  242. });
  243. expect(serviceProbe).toHaveBeenCalledWith({
  244. url: PROBE_SERVICE_URL,
  245. timeoutMs: 800,
  246. });
  247. expect(result).toMatchObject({
  248. isIntranet: true,
  249. strategy: "service-http",
  250. reachedTargetCount: 1,
  251. requiredSuccessCount: 1,
  252. totalTargets: 1,
  253. checkedAt: 1700000002000,
  254. });
  255. expect(result.results).toEqual([
  256. {
  257. targetId: PROBE_SERVICE_URL,
  258. kind: "service",
  259. url: PROBE_SERVICE_URL,
  260. reachable: true,
  261. attemptCount: 1,
  262. latencyMs: 19,
  263. marker: "zcode-intranet-lab",
  264. },
  265. ]);
  266. });
  267. it("probeIntranet 在 marker 不匹配时会判定失败", async () => {
  268. const serviceProbe = vi
  269. .fn<(
  270. params: {
  271. url: string;
  272. timeoutMs: number;
  273. token?: string;
  274. },
  275. ) => Promise<{ latencyMs: number; marker?: string }>>()
  276. .mockResolvedValue({
  277. latencyMs: 12,
  278. marker: "wrong-marker",
  279. });
  280. const service = createSystemService({
  281. serviceProbe,
  282. now: () => 1700000003000,
  283. });
  284. const result = await service.probeIntranet({
  285. attempts: 2,
  286. targets: [
  287. {
  288. kind: "service",
  289. url: PROBE_SERVICE_URL,
  290. expectedMarker: "expected-marker",
  291. },
  292. ],
  293. });
  294. expect(serviceProbe).toHaveBeenCalledTimes(2);
  295. expect(result).toMatchObject({
  296. isIntranet: false,
  297. strategy: "service-http",
  298. reachedTargetCount: 0,
  299. requiredSuccessCount: 1,
  300. totalTargets: 1,
  301. checkedAt: 1700000003000,
  302. });
  303. expect(result.results).toEqual([
  304. {
  305. targetId: PROBE_SERVICE_URL,
  306. kind: "service",
  307. url: PROBE_SERVICE_URL,
  308. reachable: false,
  309. attemptCount: 2,
  310. latencyMs: null,
  311. error: "marker mismatch(expected=expected-marker, actual=wrong-marker)",
  312. },
  313. ]);
  314. });
  315. });