remoteConnectionWizard.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import { describe, expect, it } from "vitest";
  2. import { normalizeRemoteResourcePackageSelection } from "@zcode/shared";
  3. import {
  4. buildRemoteTarget,
  5. withDefaultRemoteResourcePackages,
  6. } from "@/lib/remoteConnectionWizard.js";
  7. const intl = {
  8. formatMessage: ({ id }: { id: string }) => id,
  9. };
  10. describe("buildRemoteTarget", () => {
  11. it("私钥模式应携带私钥口令而不是登录密码", () => {
  12. const result = buildRemoteTarget(intl, {
  13. kind: "ssh",
  14. host: "demo.internal",
  15. port: "22",
  16. username: "root",
  17. sshAuthMethod: "privateKey",
  18. password: "",
  19. privateKeyPath: "~/.ssh/id_ed25519",
  20. privateKeyPassphrase: "key-secret",
  21. wslDistro: "",
  22. dockerContainer: "",
  23. });
  24. expect(result).toEqual({
  25. target: {
  26. kind: "ssh",
  27. host: "demo.internal",
  28. port: 22,
  29. username: "root",
  30. privateKeyPath: "~/.ssh/id_ed25519",
  31. privateKeyPassphrase: "key-secret",
  32. },
  33. });
  34. });
  35. it("includes the selected SSH asset install mode", () => {
  36. const result = buildRemoteTarget(intl, {
  37. kind: "ssh",
  38. host: "demo.internal",
  39. port: "22",
  40. username: "root",
  41. sshAuthMethod: "password",
  42. password: "secret",
  43. privateKeyPath: "",
  44. privateKeyPassphrase: "",
  45. assetInstallMode: "remote-download",
  46. wslDistro: "",
  47. dockerContainer: "",
  48. });
  49. expect(result.target).toMatchObject({
  50. kind: "ssh",
  51. assetInstallMode: "remote-download",
  52. });
  53. });
  54. it("includes the selected SSH config alias as target metadata", () => {
  55. const result = buildRemoteTarget(intl, {
  56. kind: "ssh",
  57. host: "localhost",
  58. port: "2223",
  59. username: "root",
  60. sshAuthMethod: "password",
  61. password: "secret",
  62. privateKeyPath: "",
  63. privateKeyPassphrase: "",
  64. selectedSshConfigAlias: " linux-arm64 ",
  65. wslDistro: "",
  66. dockerContainer: "",
  67. });
  68. expect(result.target).toMatchObject({
  69. kind: "ssh",
  70. sshConfigAlias: "linux-arm64",
  71. });
  72. });
  73. it("omits WSL user when the field is empty", () => {
  74. const result = buildRemoteTarget(intl, {
  75. kind: "wsl",
  76. host: "",
  77. port: "22",
  78. username: "",
  79. sshAuthMethod: "password",
  80. password: "",
  81. privateKeyPath: "",
  82. privateKeyPassphrase: "",
  83. wslDistro: "Ubuntu-24.04",
  84. wslUser: " ",
  85. dockerContainer: "",
  86. });
  87. expect(result).toEqual({
  88. target: {
  89. kind: "wsl",
  90. distro: "Ubuntu-24.04",
  91. },
  92. });
  93. });
  94. it("trims and includes an explicit WSL user", () => {
  95. const result = buildRemoteTarget(intl, {
  96. kind: "wsl",
  97. host: "",
  98. port: "22",
  99. username: "",
  100. sshAuthMethod: "password",
  101. password: "",
  102. privateKeyPath: "",
  103. privateKeyPassphrase: "",
  104. wslDistro: "Ubuntu-24.04",
  105. wslUser: " root ",
  106. dockerContainer: "",
  107. });
  108. expect(result).toEqual({
  109. target: {
  110. kind: "wsl",
  111. distro: "Ubuntu-24.04",
  112. user: "root",
  113. },
  114. });
  115. });
  116. it("rejects invalid WSL user before connecting", () => {
  117. const result = buildRemoteTarget(intl, {
  118. kind: "wsl",
  119. host: "",
  120. port: "22",
  121. username: "",
  122. sshAuthMethod: "password",
  123. password: "",
  124. privateKeyPath: "",
  125. privateKeyPassphrase: "",
  126. wslDistro: "Ubuntu-24.04",
  127. wslUser: "root/dev",
  128. dockerContainer: "",
  129. });
  130. expect(result).toEqual({
  131. errorMessage: "wsl.validation.invalidUser",
  132. });
  133. });
  134. it("defaults SSH resource packages when the resource step is skipped", () => {
  135. const result = buildRemoteTarget(intl, {
  136. kind: "ssh",
  137. host: "demo.internal",
  138. port: "22",
  139. username: "root",
  140. sshAuthMethod: "password",
  141. password: "secret",
  142. privateKeyPath: "",
  143. privateKeyPassphrase: "",
  144. wslDistro: "",
  145. dockerContainer: "",
  146. });
  147. const target = withDefaultRemoteResourcePackages(result.target!);
  148. expect(target).toMatchObject({
  149. kind: "ssh",
  150. resourcePackages: {
  151. selectedPackageIds: normalizeRemoteResourcePackageSelection(),
  152. },
  153. });
  154. });
  155. it("Docker 连接优先使用手动输入的容器名", () => {
  156. const result = buildRemoteTarget(intl, {
  157. kind: "docker",
  158. host: "",
  159. port: "22",
  160. username: "",
  161. sshAuthMethod: "password",
  162. password: "",
  163. privateKeyPath: "",
  164. privateKeyPassphrase: "",
  165. wslDistro: "",
  166. dockerContainer: "selected-container",
  167. manualDockerContainer: " manual-container ",
  168. });
  169. expect(result).toEqual({
  170. target: {
  171. kind: "docker",
  172. container: "manual-container",
  173. },
  174. });
  175. });
  176. it("Docker 手动输入为空时使用下拉选择的容器名", () => {
  177. const result = buildRemoteTarget(intl, {
  178. kind: "docker",
  179. host: "",
  180. port: "22",
  181. username: "",
  182. sshAuthMethod: "password",
  183. password: "",
  184. privateKeyPath: "",
  185. privateKeyPassphrase: "",
  186. wslDistro: "",
  187. dockerContainer: "selected-container",
  188. manualDockerContainer: " ",
  189. });
  190. expect(result).toEqual({
  191. target: {
  192. kind: "docker",
  193. container: "selected-container",
  194. },
  195. });
  196. });
  197. it("builds a server remote target from URL and optional token", () => {
  198. const result = buildRemoteTarget(intl, {
  199. kind: "server",
  200. host: "",
  201. port: "22",
  202. username: "",
  203. sshAuthMethod: "password",
  204. password: "",
  205. privateKeyPath: "",
  206. privateKeyPassphrase: "",
  207. wslDistro: "",
  208. dockerContainer: "",
  209. serverUrl: " https://studio.example.com/zcode/ ",
  210. serverName: " Studio ",
  211. serverToken: " secret-token ",
  212. serverWorkspacePath: " /srv/project ",
  213. });
  214. expect(result).toEqual({
  215. target: {
  216. kind: "server",
  217. url: "https://studio.example.com/zcode/",
  218. name: "Studio",
  219. token: "secret-token",
  220. workspacePath: "/srv/project",
  221. },
  222. });
  223. });
  224. it("rejects server targets without URL", () => {
  225. const result = buildRemoteTarget(intl, {
  226. kind: "server",
  227. host: "",
  228. port: "22",
  229. username: "",
  230. sshAuthMethod: "password",
  231. password: "",
  232. privateKeyPath: "",
  233. privateKeyPassphrase: "",
  234. wslDistro: "",
  235. dockerContainer: "",
  236. serverUrl: " ",
  237. });
  238. expect(result).toEqual({
  239. errorMessage: "server.validation.urlRequired",
  240. });
  241. });
  242. });