import type { DesktopWslState } from "@t3tools/contracts";import { describe, expect, it, vi } from "vite-plus/test";import { applyWslEnableSelection } from "./ConnectionsSettings.logic"; const baseWslState: DesktopWslState = { enabled: false, distro: null, available: true, wslOnly: true, distros: [], preflightError: null,}; describe("applyWslEnableSelection", () => { it("clears WSL-only and updates the distro before enabling both backends", async () => { const calls: Array<string> = []; let persistedWslOnly = true; let persistedDistro: string | null = "Ubuntu"; const setWslDistro = vi.fn(async (distro: string | null) => { calls.push(`setWslDistro:${distro ?? "default"}`); persistedDistro = distro; return { ...baseWslState, distro, wslOnly: persistedWslOnly }; }); const setWslBackendEnabled = vi.fn(async (enabled: boolean) => { calls.push(`setWslBackendEnabled:${enabled}`); return { ...baseWslState, enabled, distro: persistedDistro, wslOnly: persistedWslOnly, }; }); const setWslOnly = vi.fn(async (enabled: boolean) => { calls.push(`setWslOnly:${enabled}`); persistedWslOnly = enabled; return { ...baseWslState, distro: persistedDistro, wslOnly: enabled }; }); const state = await applyWslEnableSelection({ bridge: { setWslDistro, setWslBackendEnabled, setWslOnly }, mode: "both", nextDistro: "Debian", persistedDistro: "Ubuntu", }); expect(calls).toEqual(["setWslOnly:false", "setWslDistro:Debian", "setWslBackendEnabled:true"]); expect(state).toMatchObject({ enabled: true, distro: "Debian", wslOnly: false }); }); it("stages WSL-only before enabling without rewriting an unchanged distro", async () => { const calls: Array<string> = []; let persistedWslOnly = false; const setWslDistro = vi.fn(async () => baseWslState); const setWslOnly = vi.fn(async (enabled: boolean) => { calls.push(`setWslOnly:${enabled}`); persistedWslOnly = enabled; return { ...baseWslState, wslOnly: enabled }; }); const setWslBackendEnabled = vi.fn(async (enabled: boolean) => { calls.push(`setWslBackendEnabled:${enabled}`); return { ...baseWslState, enabled, wslOnly: persistedWslOnly }; }); const state = await applyWslEnableSelection({ bridge: { setWslDistro, setWslBackendEnabled, setWslOnly }, mode: "wsl-only", nextDistro: null, persistedDistro: null, }); expect(calls).toEqual(["setWslOnly:true", "setWslBackendEnabled:true"]); expect(setWslDistro).not.toHaveBeenCalled(); expect(state).toMatchObject({ enabled: true, wslOnly: true }); });});
import type { DesktopWslState } from "@t3tools/contracts";
import { describe, expect, it, vi } from "vite-plus/test";
import { applyWslEnableSelection } from "./ConnectionsSettings.logic";
const baseWslState: DesktopWslState = {
enabled: false,
distro: null,
available: true,
wslOnly: true,
distros: [],
preflightError: null,
};
describe("applyWslEnableSelection", () => {
it("clears WSL-only and updates the distro before enabling both backends", async () => {
const calls: Array<string> = [];
let persistedWslOnly = true;
let persistedDistro: string | null = "Ubuntu";
const setWslDistro = vi.fn(async (distro: string | null) => {
calls.push(`setWslDistro:${distro ?? "default"}`);
persistedDistro = distro;
return { ...baseWslState, distro, wslOnly: persistedWslOnly };
});
const setWslBackendEnabled = vi.fn(async (enabled: boolean) => {
calls.push(`setWslBackendEnabled:${enabled}`);
return {
...baseWslState,
enabled,
distro: persistedDistro,
wslOnly: persistedWslOnly,
};
});
const setWslOnly = vi.fn(async (enabled: boolean) => {
calls.push(`setWslOnly:${enabled}`);
persistedWslOnly = enabled;
return { ...baseWslState, distro: persistedDistro, wslOnly: enabled };
});
const state = await applyWslEnableSelection({
bridge: { setWslDistro, setWslBackendEnabled, setWslOnly },
mode: "both",
nextDistro: "Debian",
persistedDistro: "Ubuntu",
});
expect(calls).toEqual(["setWslOnly:false", "setWslDistro:Debian", "setWslBackendEnabled:true"]);
expect(state).toMatchObject({ enabled: true, distro: "Debian", wslOnly: false });
});
it("stages WSL-only before enabling without rewriting an unchanged distro", async () => {
const calls: Array<string> = [];
let persistedWslOnly = false;
const setWslDistro = vi.fn(async () => baseWslState);
const setWslOnly = vi.fn(async (enabled: boolean) => {
calls.push(`setWslOnly:${enabled}`);
persistedWslOnly = enabled;
return { ...baseWslState, wslOnly: enabled };
});
const setWslBackendEnabled = vi.fn(async (enabled: boolean) => {
calls.push(`setWslBackendEnabled:${enabled}`);
return { ...baseWslState, enabled, wslOnly: persistedWslOnly };
});
const state = await applyWslEnableSelection({
bridge: { setWslDistro, setWslBackendEnabled, setWslOnly },
mode: "wsl-only",
nextDistro: null,
persistedDistro: null,
});
expect(calls).toEqual(["setWslOnly:true", "setWslBackendEnabled:true"]);
expect(setWslDistro).not.toHaveBeenCalled();
expect(state).toMatchObject({ enabled: true, wslOnly: true });
});
});