remoteConnectionWizard.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. import { describe, expect, it } from "vitest";
  2. import { normalizeRemoteResourcePackageSelection } from "@zcode/shared";
  3. import {
  4. buildRemoteTarget,
  5. buildRemoteTargetFromProfile,
  6. collectRemoteFormFieldErrors,
  7. formatRemoteDraftPreview,
  8. findSourceServerUpgradeProfileId,
  9. withDefaultRemoteResourcePackages,
  10. } from "@/lib/remoteConnectionWizard.js";
  11. const intl = {
  12. formatMessage: ({ id }: { id: string }) => id,
  13. };
  14. describe("buildRemoteTarget", () => {
  15. it("按精确线路和必须升级状态定位连接向导的更新目标", () => {
  16. expect(
  17. findSourceServerUpgradeProfileId(
  18. {
  19. profiles: [
  20. {
  21. profileId: "17ef20b3-622d-4a53-98c1-0cdff29f3e6d",
  22. transport: "ssh",
  23. target: {
  24. kind: "ssh",
  25. host: "dev.internal",
  26. username: "dev",
  27. },
  28. createdAt: 1,
  29. },
  30. ],
  31. states: [
  32. {
  33. profileId: "17ef20b3-622d-4a53-98c1-0cdff29f3e6d",
  34. state: "needs-upgrade",
  35. updateAvailability: "required",
  36. upgradeTarget: "server",
  37. updatedAt: 2,
  38. },
  39. ],
  40. } as never,
  41. {
  42. kind: "ssh",
  43. host: "dev.internal",
  44. port: 22,
  45. username: "dev",
  46. password: "secret",
  47. },
  48. ),
  49. ).toBe("17ef20b3-622d-4a53-98c1-0cdff29f3e6d");
  50. });
  51. it("reuses saved profile metadata without exposing credential keys", () => {
  52. expect(
  53. buildRemoteTargetFromProfile({
  54. profileId: "17ef20b3-622d-4a53-98c1-0cdff29f3e6d",
  55. transport: "ssh",
  56. sourceServerId: "3cd4f9da-cd79-426c-9d70-75f36e9f6a87",
  57. displayName: "backup",
  58. target: {
  59. kind: "ssh",
  60. host: "dev.internal",
  61. username: "dev",
  62. passwordCredentialKey: "connection-profile:p:password",
  63. privateKeyPassphraseCredentialKey: "connection-profile:p:passphrase",
  64. },
  65. createdAt: 1,
  66. lifecycleState: "active",
  67. }),
  68. ).toEqual({
  69. kind: "ssh",
  70. host: "dev.internal",
  71. username: "dev",
  72. });
  73. });
  74. it("私钥模式应携带私钥口令而不是登录密码", () => {
  75. const result = buildRemoteTarget(intl, {
  76. kind: "ssh",
  77. host: "demo.internal",
  78. port: "22",
  79. username: "root",
  80. sshAuthMethod: "privateKey",
  81. password: "",
  82. privateKeyPath: "~/.ssh/id_ed25519",
  83. privateKeyPassphrase: "key-secret",
  84. wslDistro: "",
  85. dockerContainer: "",
  86. });
  87. expect(result).toEqual({
  88. target: {
  89. kind: "ssh",
  90. host: "demo.internal",
  91. port: 22,
  92. username: "root",
  93. privateKeyPath: "~/.ssh/id_ed25519",
  94. privateKeyPassphrase: "key-secret",
  95. },
  96. });
  97. });
  98. it("includes the selected SSH asset install mode", () => {
  99. const result = buildRemoteTarget(intl, {
  100. kind: "ssh",
  101. host: "demo.internal",
  102. port: "22",
  103. username: "root",
  104. sshAuthMethod: "password",
  105. password: "secret",
  106. privateKeyPath: "",
  107. privateKeyPassphrase: "",
  108. assetInstallMode: "remote-download",
  109. wslDistro: "",
  110. dockerContainer: "",
  111. });
  112. expect(result.target).toMatchObject({
  113. kind: "ssh",
  114. assetInstallMode: "remote-download",
  115. });
  116. });
  117. it("includes the selected SSH config alias as target metadata", () => {
  118. const result = buildRemoteTarget(intl, {
  119. kind: "ssh",
  120. host: "localhost",
  121. port: "2223",
  122. username: "root",
  123. sshAuthMethod: "password",
  124. password: "secret",
  125. privateKeyPath: "",
  126. privateKeyPassphrase: "",
  127. selectedSshConfigAlias: " linux-arm64 ",
  128. wslDistro: "",
  129. dockerContainer: "",
  130. });
  131. expect(result.target).toMatchObject({
  132. kind: "ssh",
  133. sshConfigAlias: "linux-arm64",
  134. });
  135. });
  136. it("omits WSL user when the field is empty", () => {
  137. const result = buildRemoteTarget(intl, {
  138. kind: "wsl",
  139. host: "",
  140. port: "22",
  141. username: "",
  142. sshAuthMethod: "password",
  143. password: "",
  144. privateKeyPath: "",
  145. privateKeyPassphrase: "",
  146. wslDistro: "Ubuntu-24.04",
  147. wslUser: " ",
  148. dockerContainer: "",
  149. });
  150. expect(result).toEqual({
  151. target: {
  152. kind: "wsl",
  153. distro: "Ubuntu-24.04",
  154. },
  155. });
  156. });
  157. it("trims and includes an explicit WSL user", () => {
  158. const result = buildRemoteTarget(intl, {
  159. kind: "wsl",
  160. host: "",
  161. port: "22",
  162. username: "",
  163. sshAuthMethod: "password",
  164. password: "",
  165. privateKeyPath: "",
  166. privateKeyPassphrase: "",
  167. wslDistro: "Ubuntu-24.04",
  168. wslUser: " root ",
  169. dockerContainer: "",
  170. });
  171. expect(result).toEqual({
  172. target: {
  173. kind: "wsl",
  174. distro: "Ubuntu-24.04",
  175. user: "root",
  176. },
  177. });
  178. });
  179. it("rejects invalid WSL user before connecting", () => {
  180. const result = buildRemoteTarget(intl, {
  181. kind: "wsl",
  182. host: "",
  183. port: "22",
  184. username: "",
  185. sshAuthMethod: "password",
  186. password: "",
  187. privateKeyPath: "",
  188. privateKeyPassphrase: "",
  189. wslDistro: "Ubuntu-24.04",
  190. wslUser: "root/dev",
  191. dockerContainer: "",
  192. });
  193. expect(result).toEqual({
  194. errorMessage: "wsl.validation.invalidUser",
  195. });
  196. });
  197. it("defaults SSH resource packages when the resource step is skipped", () => {
  198. const result = buildRemoteTarget(intl, {
  199. kind: "ssh",
  200. host: "demo.internal",
  201. port: "22",
  202. username: "root",
  203. sshAuthMethod: "password",
  204. password: "secret",
  205. privateKeyPath: "",
  206. privateKeyPassphrase: "",
  207. wslDistro: "",
  208. dockerContainer: "",
  209. });
  210. const target = withDefaultRemoteResourcePackages(result.target!);
  211. expect(target).toMatchObject({
  212. kind: "ssh",
  213. resourcePackages: {
  214. selectedPackageIds: normalizeRemoteResourcePackageSelection(),
  215. },
  216. });
  217. });
  218. it("Docker 连接优先使用手动输入的容器名", () => {
  219. const result = buildRemoteTarget(intl, {
  220. kind: "docker",
  221. host: "",
  222. port: "22",
  223. username: "",
  224. sshAuthMethod: "password",
  225. password: "",
  226. privateKeyPath: "",
  227. privateKeyPassphrase: "",
  228. wslDistro: "",
  229. dockerContainer: "selected-container",
  230. manualDockerContainer: " manual-container ",
  231. });
  232. expect(result).toEqual({
  233. target: {
  234. kind: "docker",
  235. container: "manual-container",
  236. },
  237. });
  238. });
  239. it("Docker 手动输入为空时使用下拉选择的容器名", () => {
  240. const result = buildRemoteTarget(intl, {
  241. kind: "docker",
  242. host: "",
  243. port: "22",
  244. username: "",
  245. sshAuthMethod: "password",
  246. password: "",
  247. privateKeyPath: "",
  248. privateKeyPassphrase: "",
  249. wslDistro: "",
  250. dockerContainer: "selected-container",
  251. manualDockerContainer: " ",
  252. });
  253. expect(result).toEqual({
  254. target: {
  255. kind: "docker",
  256. container: "selected-container",
  257. },
  258. });
  259. });
  260. it("builds a server remote target from URL and optional token", () => {
  261. const result = buildRemoteTarget(intl, {
  262. kind: "server",
  263. host: "",
  264. port: "22",
  265. username: "",
  266. sshAuthMethod: "password",
  267. password: "",
  268. privateKeyPath: "",
  269. privateKeyPassphrase: "",
  270. wslDistro: "",
  271. dockerContainer: "",
  272. serverUrl: " https://studio.example.com/zcode/ ",
  273. serverName: " Studio ",
  274. serverToken: " secret-token ",
  275. serverWorkspacePath: " /srv/project ",
  276. });
  277. expect(result).toEqual({
  278. target: {
  279. kind: "server",
  280. url: "https://studio.example.com/zcode/",
  281. name: "Studio",
  282. token: "secret-token",
  283. workspacePath: "/srv/project",
  284. },
  285. });
  286. });
  287. it("rejects server targets without URL", () => {
  288. const result = buildRemoteTarget(intl, {
  289. kind: "server",
  290. host: "",
  291. port: "22",
  292. username: "",
  293. sshAuthMethod: "password",
  294. password: "",
  295. privateKeyPath: "",
  296. privateKeyPassphrase: "",
  297. wslDistro: "",
  298. dockerContainer: "",
  299. serverUrl: " ",
  300. });
  301. expect(result).toEqual({
  302. errorMessage: "server.validation.urlRequired",
  303. });
  304. });
  305. });
  306. const emptySsh = {
  307. kind: "ssh" as const,
  308. host: "",
  309. port: "22",
  310. username: "",
  311. sshAuthMethod: "password" as const,
  312. password: "",
  313. privateKeyPath: "",
  314. privateKeyPassphrase: "",
  315. wslDistro: "",
  316. dockerContainer: "",
  317. };
  318. describe("collectRemoteFormFieldErrors", () => {
  319. it("SSH 按页面顺序列出所有字段错误,buildRemoteTarget 返回第一条", () => {
  320. const snapshot = { ...emptySsh, port: "70000" };
  321. expect(collectRemoteFormFieldErrors(intl, snapshot)).toEqual([
  322. { field: "host", message: "ssh.validation.hostRequired" },
  323. { field: "port", message: "ssh.validation.port" },
  324. { field: "username", message: "ssh.validation.usernameRequired" },
  325. { field: "password", message: "ssh.validation.passwordRequired" },
  326. ]);
  327. expect(buildRemoteTarget(intl, snapshot)).toEqual({
  328. errorMessage: "ssh.validation.hostRequired",
  329. });
  330. });
  331. it("端口只有空白时按未填写处理,和校验读取同一个值", () => {
  332. const snapshot = { ...emptySsh, host: "h", username: "u", password: "p", port: " " };
  333. expect(collectRemoteFormFieldErrors(intl, snapshot)).toEqual([]);
  334. expect(buildRemoteTarget(intl, snapshot).target).toMatchObject({
  335. kind: "ssh",
  336. port: undefined,
  337. });
  338. expect(buildRemoteTarget(intl, { ...snapshot, port: " 2222 " }).target).toMatchObject({
  339. port: 2222,
  340. });
  341. });
  342. it("私钥模式要求私钥路径;已保存密码时可不填密码", () => {
  343. expect(
  344. collectRemoteFormFieldErrors(intl, {
  345. ...emptySsh,
  346. host: "h",
  347. username: "u",
  348. sshAuthMethod: "privateKey",
  349. }),
  350. ).toEqual([{ field: "privateKey", message: "ssh.validation.privateKeyRequired" }]);
  351. expect(
  352. collectRemoteFormFieldErrors(
  353. intl,
  354. { ...emptySsh, host: "h", username: "u" },
  355. { passwordOptional: true },
  356. ),
  357. ).toEqual([]);
  358. });
  359. it("Docker、WSL、Server 各自映射到对应字段", () => {
  360. expect(collectRemoteFormFieldErrors(intl, { ...emptySsh, kind: "docker" })).toEqual([
  361. { field: "docker", message: "docker.validation.required" },
  362. ]);
  363. expect(
  364. collectRemoteFormFieldErrors(intl, { ...emptySsh, kind: "wsl", wslUser: "bad:user" }),
  365. ).toEqual([{ field: "wslUser", message: "wsl.validation.invalidUser" }]);
  366. expect(
  367. collectRemoteFormFieldErrors(intl, { ...emptySsh, kind: "server", serverUrl: "nope" }),
  368. ).toEqual([{ field: "serverUrl", message: "server.validation.invalidUrl" }]);
  369. });
  370. });
  371. describe("formatRemoteDraftPreview", () => {
  372. it("SSH 有主机才生成预览,缺用户名和端口时使用占位", () => {
  373. expect(formatRemoteDraftPreview(emptySsh)).toBeNull();
  374. expect(formatRemoteDraftPreview({ ...emptySsh, host: " 10.0.0.5 ", port: "" })).toBe(
  375. "user@10.0.0.5:22",
  376. );
  377. expect(
  378. formatRemoteDraftPreview({ ...emptySsh, host: "h", username: "root", port: "2222" }),
  379. ).toBe("root@h:2222");
  380. });
  381. it("Docker 优先手动输入,WSL 使用发行版,Server 使用 URL", () => {
  382. expect(
  383. formatRemoteDraftPreview({
  384. ...emptySsh,
  385. kind: "docker",
  386. dockerContainer: "listed",
  387. manualDockerContainer: " manual ",
  388. }),
  389. ).toBe("manual");
  390. expect(formatRemoteDraftPreview({ ...emptySsh, kind: "docker", dockerContainer: "web" })).toBe(
  391. "web",
  392. );
  393. expect(formatRemoteDraftPreview({ ...emptySsh, kind: "wsl" })).toBeNull();
  394. expect(formatRemoteDraftPreview({ ...emptySsh, kind: "wsl", wslDistro: "Ubuntu" })).toBe(
  395. "Ubuntu",
  396. );
  397. expect(
  398. formatRemoteDraftPreview({ ...emptySsh, kind: "server", serverUrl: "https://example.com" }),
  399. ).toBe("https://example.com");
  400. });
  401. });