| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- import { describe, expect, it } from "vitest";
- import { normalizeRemoteResourcePackageSelection } from "@zcode/shared";
- import {
- buildRemoteTarget,
- withDefaultRemoteResourcePackages,
- } from "@/lib/remoteConnectionWizard.js";
- const intl = {
- formatMessage: ({ id }: { id: string }) => id,
- };
- describe("buildRemoteTarget", () => {
- it("私钥模式应携带私钥口令而不是登录密码", () => {
- const result = buildRemoteTarget(intl, {
- kind: "ssh",
- host: "demo.internal",
- port: "22",
- username: "root",
- sshAuthMethod: "privateKey",
- password: "",
- privateKeyPath: "~/.ssh/id_ed25519",
- privateKeyPassphrase: "key-secret",
- wslDistro: "",
- dockerContainer: "",
- });
- expect(result).toEqual({
- target: {
- kind: "ssh",
- host: "demo.internal",
- port: 22,
- username: "root",
- privateKeyPath: "~/.ssh/id_ed25519",
- privateKeyPassphrase: "key-secret",
- },
- });
- });
- it("includes the selected SSH asset install mode", () => {
- const result = buildRemoteTarget(intl, {
- kind: "ssh",
- host: "demo.internal",
- port: "22",
- username: "root",
- sshAuthMethod: "password",
- password: "secret",
- privateKeyPath: "",
- privateKeyPassphrase: "",
- assetInstallMode: "remote-download",
- wslDistro: "",
- dockerContainer: "",
- });
- expect(result.target).toMatchObject({
- kind: "ssh",
- assetInstallMode: "remote-download",
- });
- });
- it("includes the selected SSH config alias as target metadata", () => {
- const result = buildRemoteTarget(intl, {
- kind: "ssh",
- host: "localhost",
- port: "2223",
- username: "root",
- sshAuthMethod: "password",
- password: "secret",
- privateKeyPath: "",
- privateKeyPassphrase: "",
- selectedSshConfigAlias: " linux-arm64 ",
- wslDistro: "",
- dockerContainer: "",
- });
- expect(result.target).toMatchObject({
- kind: "ssh",
- sshConfigAlias: "linux-arm64",
- });
- });
- it("omits WSL user when the field is empty", () => {
- const result = buildRemoteTarget(intl, {
- kind: "wsl",
- host: "",
- port: "22",
- username: "",
- sshAuthMethod: "password",
- password: "",
- privateKeyPath: "",
- privateKeyPassphrase: "",
- wslDistro: "Ubuntu-24.04",
- wslUser: " ",
- dockerContainer: "",
- });
- expect(result).toEqual({
- target: {
- kind: "wsl",
- distro: "Ubuntu-24.04",
- },
- });
- });
- it("trims and includes an explicit WSL user", () => {
- const result = buildRemoteTarget(intl, {
- kind: "wsl",
- host: "",
- port: "22",
- username: "",
- sshAuthMethod: "password",
- password: "",
- privateKeyPath: "",
- privateKeyPassphrase: "",
- wslDistro: "Ubuntu-24.04",
- wslUser: " root ",
- dockerContainer: "",
- });
- expect(result).toEqual({
- target: {
- kind: "wsl",
- distro: "Ubuntu-24.04",
- user: "root",
- },
- });
- });
- it("rejects invalid WSL user before connecting", () => {
- const result = buildRemoteTarget(intl, {
- kind: "wsl",
- host: "",
- port: "22",
- username: "",
- sshAuthMethod: "password",
- password: "",
- privateKeyPath: "",
- privateKeyPassphrase: "",
- wslDistro: "Ubuntu-24.04",
- wslUser: "root/dev",
- dockerContainer: "",
- });
- expect(result).toEqual({
- errorMessage: "wsl.validation.invalidUser",
- });
- });
- it("defaults SSH resource packages when the resource step is skipped", () => {
- const result = buildRemoteTarget(intl, {
- kind: "ssh",
- host: "demo.internal",
- port: "22",
- username: "root",
- sshAuthMethod: "password",
- password: "secret",
- privateKeyPath: "",
- privateKeyPassphrase: "",
- wslDistro: "",
- dockerContainer: "",
- });
- const target = withDefaultRemoteResourcePackages(result.target!);
- expect(target).toMatchObject({
- kind: "ssh",
- resourcePackages: {
- selectedPackageIds: normalizeRemoteResourcePackageSelection(),
- },
- });
- });
- it("Docker 连接优先使用手动输入的容器名", () => {
- const result = buildRemoteTarget(intl, {
- kind: "docker",
- host: "",
- port: "22",
- username: "",
- sshAuthMethod: "password",
- password: "",
- privateKeyPath: "",
- privateKeyPassphrase: "",
- wslDistro: "",
- dockerContainer: "selected-container",
- manualDockerContainer: " manual-container ",
- });
- expect(result).toEqual({
- target: {
- kind: "docker",
- container: "manual-container",
- },
- });
- });
- it("Docker 手动输入为空时使用下拉选择的容器名", () => {
- const result = buildRemoteTarget(intl, {
- kind: "docker",
- host: "",
- port: "22",
- username: "",
- sshAuthMethod: "password",
- password: "",
- privateKeyPath: "",
- privateKeyPassphrase: "",
- wslDistro: "",
- dockerContainer: "selected-container",
- manualDockerContainer: " ",
- });
- expect(result).toEqual({
- target: {
- kind: "docker",
- container: "selected-container",
- },
- });
- });
- it("builds a server remote target from URL and optional token", () => {
- const result = buildRemoteTarget(intl, {
- kind: "server",
- host: "",
- port: "22",
- username: "",
- sshAuthMethod: "password",
- password: "",
- privateKeyPath: "",
- privateKeyPassphrase: "",
- wslDistro: "",
- dockerContainer: "",
- serverUrl: " https://studio.example.com/zcode/ ",
- serverName: " Studio ",
- serverToken: " secret-token ",
- serverWorkspacePath: " /srv/project ",
- });
- expect(result).toEqual({
- target: {
- kind: "server",
- url: "https://studio.example.com/zcode/",
- name: "Studio",
- token: "secret-token",
- workspacePath: "/srv/project",
- },
- });
- });
- it("rejects server targets without URL", () => {
- const result = buildRemoteTarget(intl, {
- kind: "server",
- host: "",
- port: "22",
- username: "",
- sshAuthMethod: "password",
- password: "",
- privateKeyPath: "",
- privateKeyPassphrase: "",
- wslDistro: "",
- dockerContainer: "",
- serverUrl: " ",
- });
- expect(result).toEqual({
- errorMessage: "server.validation.urlRequired",
- });
- });
- });
|