| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- import { describe, expect, it } from "vitest";
- import { normalizeRemoteResourcePackageSelection } from "@zcode/shared";
- import {
- buildRemoteTarget,
- buildRemoteTargetFromProfile,
- collectRemoteFormFieldErrors,
- formatRemoteDraftPreview,
- findSourceServerUpgradeProfileId,
- withDefaultRemoteResourcePackages,
- } from "@/lib/remoteConnectionWizard.js";
- const intl = {
- formatMessage: ({ id }: { id: string }) => id,
- };
- describe("buildRemoteTarget", () => {
- it("按精确线路和必须升级状态定位连接向导的更新目标", () => {
- expect(
- findSourceServerUpgradeProfileId(
- {
- profiles: [
- {
- profileId: "17ef20b3-622d-4a53-98c1-0cdff29f3e6d",
- transport: "ssh",
- target: {
- kind: "ssh",
- host: "dev.internal",
- username: "dev",
- },
- createdAt: 1,
- },
- ],
- states: [
- {
- profileId: "17ef20b3-622d-4a53-98c1-0cdff29f3e6d",
- state: "needs-upgrade",
- updateAvailability: "required",
- upgradeTarget: "server",
- updatedAt: 2,
- },
- ],
- } as never,
- {
- kind: "ssh",
- host: "dev.internal",
- port: 22,
- username: "dev",
- password: "secret",
- },
- ),
- ).toBe("17ef20b3-622d-4a53-98c1-0cdff29f3e6d");
- });
- it("reuses saved profile metadata without exposing credential keys", () => {
- expect(
- buildRemoteTargetFromProfile({
- profileId: "17ef20b3-622d-4a53-98c1-0cdff29f3e6d",
- transport: "ssh",
- sourceServerId: "3cd4f9da-cd79-426c-9d70-75f36e9f6a87",
- displayName: "backup",
- target: {
- kind: "ssh",
- host: "dev.internal",
- username: "dev",
- passwordCredentialKey: "connection-profile:p:password",
- privateKeyPassphraseCredentialKey: "connection-profile:p:passphrase",
- },
- createdAt: 1,
- lifecycleState: "active",
- }),
- ).toEqual({
- kind: "ssh",
- host: "dev.internal",
- username: "dev",
- });
- });
- 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",
- });
- });
- });
- const emptySsh = {
- kind: "ssh" as const,
- host: "",
- port: "22",
- username: "",
- sshAuthMethod: "password" as const,
- password: "",
- privateKeyPath: "",
- privateKeyPassphrase: "",
- wslDistro: "",
- dockerContainer: "",
- };
- describe("collectRemoteFormFieldErrors", () => {
- it("SSH 按页面顺序列出所有字段错误,buildRemoteTarget 返回第一条", () => {
- const snapshot = { ...emptySsh, port: "70000" };
- expect(collectRemoteFormFieldErrors(intl, snapshot)).toEqual([
- { field: "host", message: "ssh.validation.hostRequired" },
- { field: "port", message: "ssh.validation.port" },
- { field: "username", message: "ssh.validation.usernameRequired" },
- { field: "password", message: "ssh.validation.passwordRequired" },
- ]);
- expect(buildRemoteTarget(intl, snapshot)).toEqual({
- errorMessage: "ssh.validation.hostRequired",
- });
- });
- it("端口只有空白时按未填写处理,和校验读取同一个值", () => {
- const snapshot = { ...emptySsh, host: "h", username: "u", password: "p", port: " " };
- expect(collectRemoteFormFieldErrors(intl, snapshot)).toEqual([]);
- expect(buildRemoteTarget(intl, snapshot).target).toMatchObject({
- kind: "ssh",
- port: undefined,
- });
- expect(buildRemoteTarget(intl, { ...snapshot, port: " 2222 " }).target).toMatchObject({
- port: 2222,
- });
- });
- it("私钥模式要求私钥路径;已保存密码时可不填密码", () => {
- expect(
- collectRemoteFormFieldErrors(intl, {
- ...emptySsh,
- host: "h",
- username: "u",
- sshAuthMethod: "privateKey",
- }),
- ).toEqual([{ field: "privateKey", message: "ssh.validation.privateKeyRequired" }]);
- expect(
- collectRemoteFormFieldErrors(
- intl,
- { ...emptySsh, host: "h", username: "u" },
- { passwordOptional: true },
- ),
- ).toEqual([]);
- });
- it("Docker、WSL、Server 各自映射到对应字段", () => {
- expect(collectRemoteFormFieldErrors(intl, { ...emptySsh, kind: "docker" })).toEqual([
- { field: "docker", message: "docker.validation.required" },
- ]);
- expect(
- collectRemoteFormFieldErrors(intl, { ...emptySsh, kind: "wsl", wslUser: "bad:user" }),
- ).toEqual([{ field: "wslUser", message: "wsl.validation.invalidUser" }]);
- expect(
- collectRemoteFormFieldErrors(intl, { ...emptySsh, kind: "server", serverUrl: "nope" }),
- ).toEqual([{ field: "serverUrl", message: "server.validation.invalidUrl" }]);
- });
- });
- describe("formatRemoteDraftPreview", () => {
- it("SSH 有主机才生成预览,缺用户名和端口时使用占位", () => {
- expect(formatRemoteDraftPreview(emptySsh)).toBeNull();
- expect(formatRemoteDraftPreview({ ...emptySsh, host: " 10.0.0.5 ", port: "" })).toBe(
- "user@10.0.0.5:22",
- );
- expect(
- formatRemoteDraftPreview({ ...emptySsh, host: "h", username: "root", port: "2222" }),
- ).toBe("root@h:2222");
- });
- it("Docker 优先手动输入,WSL 使用发行版,Server 使用 URL", () => {
- expect(
- formatRemoteDraftPreview({
- ...emptySsh,
- kind: "docker",
- dockerContainer: "listed",
- manualDockerContainer: " manual ",
- }),
- ).toBe("manual");
- expect(formatRemoteDraftPreview({ ...emptySsh, kind: "docker", dockerContainer: "web" })).toBe(
- "web",
- );
- expect(formatRemoteDraftPreview({ ...emptySsh, kind: "wsl" })).toBeNull();
- expect(formatRemoteDraftPreview({ ...emptySsh, kind: "wsl", wslDistro: "Ubuntu" })).toBe(
- "Ubuntu",
- );
- expect(
- formatRemoteDraftPreview({ ...emptySsh, kind: "server", serverUrl: "https://example.com" }),
- ).toBe("https://example.com");
- });
- });
|