Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ConnectionsSettings.logic.test.ts

75 lines2.7 KB

1import type { DesktopWslState } from "@t3tools/contracts";
2import { describe, expect, it, vi } from "vite-plus/test";
3import { applyWslEnableSelection } from "./ConnectionsSettings.logic";
4​
5const baseWslState: DesktopWslState = {
6 enabled: false,
7 distro: null,
8 available: true,
9 wslOnly: true,
10 distros: [],
11 preflightError: null,
12};
13​
14describe("applyWslEnableSelection", () => {
15 it("clears WSL-only and updates the distro before enabling both backends", async () => {
16 const calls: Array<string> = [];
17 let persistedWslOnly = true;
18 let persistedDistro: string | null = "Ubuntu";
19 const setWslDistro = vi.fn(async (distro: string | null) => {
20 calls.push(`setWslDistro:${distro ?? "default"}`);
21 persistedDistro = distro;
22 return { ...baseWslState, distro, wslOnly: persistedWslOnly };
23 });
24 const setWslBackendEnabled = vi.fn(async (enabled: boolean) => {
25 calls.push(`setWslBackendEnabled:${enabled}`);
26 return {
27 ...baseWslState,
28 enabled,
29 distro: persistedDistro,
30 wslOnly: persistedWslOnly,
31 };
32 });
33 const setWslOnly = vi.fn(async (enabled: boolean) => {
34 calls.push(`setWslOnly:${enabled}`);
35 persistedWslOnly = enabled;
36 return { ...baseWslState, distro: persistedDistro, wslOnly: enabled };
37 });
38​
39 const state = await applyWslEnableSelection({
40 bridge: { setWslDistro, setWslBackendEnabled, setWslOnly },
41 mode: "both",
42 nextDistro: "Debian",
43 persistedDistro: "Ubuntu",
44 });
45​
46 expect(calls).toEqual(["setWslOnly:false", "setWslDistro:Debian", "setWslBackendEnabled:true"]);
47 expect(state).toMatchObject({ enabled: true, distro: "Debian", wslOnly: false });
48 });
49​
50 it("stages WSL-only before enabling without rewriting an unchanged distro", async () => {
51 const calls: Array<string> = [];
52 let persistedWslOnly = false;
53 const setWslDistro = vi.fn(async () => baseWslState);
54 const setWslOnly = vi.fn(async (enabled: boolean) => {
55 calls.push(`setWslOnly:${enabled}`);
56 persistedWslOnly = enabled;
57 return { ...baseWslState, wslOnly: enabled };
58 });
59 const setWslBackendEnabled = vi.fn(async (enabled: boolean) => {
60 calls.push(`setWslBackendEnabled:${enabled}`);
61 return { ...baseWslState, enabled, wslOnly: persistedWslOnly };
62 });
63​
64 const state = await applyWslEnableSelection({
65 bridge: { setWslDistro, setWslBackendEnabled, setWslOnly },
66 mode: "wsl-only",
67 nextDistro: null,
68 persistedDistro: null,
69 });
70​
71 expect(calls).toEqual(["setWslOnly:true", "setWslBackendEnabled:true"]);
72 expect(setWslDistro).not.toHaveBeenCalled();
73 expect(state).toMatchObject({ enabled: true, wslOnly: true });
74 });
75});