feat(connection): add reusable tunnel profiles

This commit is contained in:
rainhan authored and GitHub committed 2026-07-12 22:03:46 +08:00
1 parent 4dcee1aca0
commit 977e7f78ca
34 files changed
+1335 -22

No files matched your search

@@ -0,0 +1,83 @@
import { describe, expect, it } from "vitest";
import { createTunnelProfile, detachTunnelProfileLayer, tunnelProfileReferenceLayer, tunnelProfileSummary } from "@/lib/connection/tunnelProfiles";
import type { TunnelProfile } from "@/types/database";
function sshProfile(overrides: Partial<TunnelProfile> = {}): TunnelProfile {
return {
...createTunnelProfile("ssh"),
id: "profile-1",
name: "Bastion",
host: "bastion.example.com",
user: "deploy",
password: "s3cret",
...overrides,
} as TunnelProfile;
}
describe("tunnelProfileSummary", () => {
it("formats ssh profiles as user@host:port", () => {
expect(tunnelProfileSummary(sshProfile())).toBe("deploy@bastion.example.com:22");
});
it("formats proxy profiles as scheme://host:port", () => {
const proxy = { ...createTunnelProfile("proxy"), host: "127.0.0.1", port: 1080 } as TunnelProfile;
expect(tunnelProfileSummary(proxy)).toBe("socks5://127.0.0.1:1080");
});
it("returns the url for http tunnel profiles", () => {
const http = { ...createTunnelProfile("http_tunnel"), url: "https://example.com/dbx_tunnel.php" } as TunnelProfile;
expect(tunnelProfileSummary(http)).toBe("https://example.com/dbx_tunnel.php");
});
it("returns an empty string when the target is not configured yet", () => {
expect(tunnelProfileSummary(createTunnelProfile("ssh"))).toBe("");
});
});
describe("tunnelProfileReferenceLayer", () => {
it("keeps only identity, enabled state, and the reference", () => {
const profile = sshProfile();
const stub = tunnelProfileReferenceLayer(profile, { id: "layer-1", enabled: false });
expect(stub.id).toBe("layer-1");
expect(stub.enabled).toBe(false);
expect(stub.profile_id).toBe("profile-1");
expect(stub.name).toBe("Bastion");
// Credentials must not be copied into the stub stored on the connection.
expect(stub.type).toBe("ssh");
if (stub.type === "ssh") {
expect(stub.host).toBe("");
expect(stub.password).toBe("");
}
});
it("generates a fresh id when there is no previous layer", () => {
const stub = tunnelProfileReferenceLayer(sshProfile());
expect(stub.id).toBeTruthy();
expect(stub.enabled).toBe(true);
});
});
describe("detachTunnelProfileLayer", () => {
it("copies the profile configuration onto the layer and drops the reference", () => {
const profile = sshProfile();
const stub = tunnelProfileReferenceLayer(profile, { id: "layer-1", enabled: true });
const detached = detachTunnelProfileLayer(stub, profile);
expect(detached.id).toBe("layer-1");
expect(detached.profile_id).toBeUndefined();
if (detached.type === "ssh") {
expect(detached.host).toBe("bastion.example.com");
expect(detached.password).toBe("s3cret");
} else {
throw new Error("expected ssh layer");
}
});
it("only drops the reference when the profile no longer exists", () => {
const stub = tunnelProfileReferenceLayer(sshProfile(), { id: "layer-1", enabled: true });
const detached = detachTunnelProfileLayer(stub, undefined);
expect(detached.profile_id).toBeUndefined();
expect(detached.id).toBe("layer-1");
});
});