feat(connection): test SSH tunnel profiles

This commit is contained in:
rainhan authored and GitHub committed 2026-07-13 17:51:51 +08:00
1 parent 27c60f0d30
commit c7991b316e
19 files changed
+256 -4

No files matched your search

@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { createTunnelProfile, detachTunnelProfileLayer, tunnelProfileReferenceLayer, tunnelProfileSummary } from "@/lib/connection/tunnelProfiles";
import { createTunnelProfile, createTunnelProfileTestGuard, detachTunnelProfileLayer, tunnelProfileReferenceLayer, tunnelProfileSummary } from "@/lib/connection/tunnelProfiles";
import type { TunnelProfile } from "@/types/database";
function sshProfile(overrides: Partial<TunnelProfile> = {}): TunnelProfile {
@@ -34,6 +34,37 @@ describe("tunnelProfileSummary", () => {
});
});
describe("createTunnelProfileTestGuard", () => {
it("invalidates a request when the tested profile changes", () => {
const guard = createTunnelProfileTestGuard();
const profile = sshProfile();
const requestId = guard.start(profile);
expect(guard.isCurrent(requestId, profile)).toBe(true);
expect(guard.isCurrent(requestId, { ...profile, password: "changed" } as TunnelProfile)).toBe(false);
});
it("invalidates a request after switching or removing the profile", () => {
const guard = createTunnelProfileTestGuard();
const requestId = guard.start(sshProfile());
guard.invalidate();
expect(guard.isCurrent(requestId, sshProfile())).toBe(false);
expect(guard.isCurrent(requestId, null)).toBe(false);
});
it("ignores an older response after a newer request starts", () => {
const guard = createTunnelProfileTestGuard();
const profile = sshProfile();
const olderRequestId = guard.start(profile);
const newerRequestId = guard.start(profile);
expect(guard.isCurrent(olderRequestId, profile)).toBe(false);
expect(guard.isCurrent(newerRequestId, profile)).toBe(true);
});
});
describe("tunnelProfileReferenceLayer", () => {
it("keeps only identity, enabled state, and the reference", () => {
const profile = sshProfile();
+1
View File
@@ -61,6 +61,7 @@ export const saveConnections = forward("saveConnections");
export const loadConnections = forward("loadConnections");
export const loadTunnelProfiles = forward("loadTunnelProfiles");
export const saveTunnelProfiles = forward("saveTunnelProfiles");
export const testTunnelProfile = forward("testTunnelProfile");
export const readKeychainPassword = forward("readKeychainPassword");
export const readKeychainPasswords = forward("readKeychainPasswords");
export const decryptConfig = forward("decryptConfig");
+4
View File
@@ -245,6 +245,10 @@ export async function saveTunnelProfiles(profiles: TunnelProfile[]): Promise<voi
return post("/api/tunnel-profiles/save", { profiles });
}
export async function testTunnelProfile(profile: TunnelProfile): Promise<string> {
return post("/api/tunnel-profiles/test", profile);
}
export async function readKeychainPassword(_service: string): Promise<string> {
return ""; // Not available in web backend
}
+4
View File
@@ -1120,6 +1120,10 @@ export async function saveTunnelProfiles(profiles: TunnelProfile[]): Promise<voi
return invoke("save_tunnel_profiles", { profiles });
}
export async function testTunnelProfile(profile: TunnelProfile): Promise<string> {
return invoke("test_tunnel_profile", { profile });
}
export async function readKeychainPassword(service: string): Promise<string> {
return invoke("read_keychain_password", { service, account: null });
}
@@ -3,6 +3,29 @@ import type { TransportLayerConfig, TunnelProfile } from "@/types/database";
export type TunnelProfileType = TunnelProfile["type"];
export interface TunnelProfileTestGuard {
invalidate: () => void;
start: (profile: TunnelProfile) => number;
isCurrent: (requestId: number, profile: TunnelProfile | null) => boolean;
}
export function createTunnelProfileTestGuard(): TunnelProfileTestGuard {
let requestId = 0;
let testedProfile = "";
return {
invalidate: () => {
requestId++;
testedProfile = "";
},
start: (profile) => {
testedProfile = JSON.stringify(profile);
return ++requestId;
},
isCurrent: (candidateRequestId, profile) => candidateRequestId === requestId && profile !== null && JSON.stringify(profile) === testedProfile,
};
}
export function createTunnelProfile(type: TunnelProfileType): TunnelProfile {
if (type === "proxy") {
return {