sshPasteTarget.test.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { describe, expect, it } from "vitest";
  2. import { parseSshPasteTarget } from "@/lib/remoteConnectionWizard.js";
  3. describe("parseSshPasteTarget", () => {
  4. it.each([
  5. ["root@10.0.0.5", { host: "10.0.0.5", username: "root" }],
  6. ["root@10.0.0.5:2222", { host: "10.0.0.5", username: "root", port: "2222" }],
  7. ["dev.example.com:2200", { host: "dev.example.com", port: "2200" }],
  8. [" deploy@staging.example.com ", { host: "staging.example.com", username: "deploy" }],
  9. ["ssh root@10.0.0.5 -p 2222", { host: "10.0.0.5", username: "root", port: "2222" }],
  10. ["ssh -p 2222 root@10.0.0.5", { host: "10.0.0.5", username: "root", port: "2222" }],
  11. ["ssh root@10.0.0.5", { host: "10.0.0.5", username: "root" }],
  12. ["ssh dev.example.com", { host: "dev.example.com" }],
  13. ["root@[::1]:2222", { host: "::1", username: "root", port: "2222" }],
  14. ["ssh root@[fe80::1] -p 22", { host: "fe80::1", username: "root", port: "22" }],
  15. ])("拆分 %s", (text, expected) => {
  16. expect(parseSshPasteTarget(text)).toEqual(expected);
  17. });
  18. it.each([
  19. // 纯主机名没有可拆分信息,交给浏览器原样粘贴。
  20. "10.0.0.5",
  21. "dev.example.com",
  22. "",
  23. "hello world",
  24. "root@10.0.0.5:0",
  25. "root@10.0.0.5:65536",
  26. "root@10.0.0.5:22a",
  27. "ssh -i ~/.ssh/id root@10.0.0.5",
  28. "ssh root@10.0.0.5 -p",
  29. "ssh root@a root@b",
  30. "a@b@c",
  31. "https://example.com:8080",
  32. // 用户名不含 ":" 或 "/":SSH URI 和 user:password@host 不拆分,原样粘贴。
  33. "ssh://root@10.0.0.5:22",
  34. "root:secret@10.0.0.5",
  35. ])("不匹配时返回 null:%s", (text) => {
  36. expect(parseSshPasteTarget(text)).toBeNull();
  37. });
  38. });