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

browserImportWizard.logic.test.ts

193 lines6.4 KB

1import { describe, expect, it } from "vite-plus/test";
2import type { BrowserImportSource } from "@t3tools/contracts";
3​
4import {
5 canCloseWizard,
6 initialWizardStep,
7 initialTargetSelection,
8 isRetryableReason,
9 formatSkippedDomains,
10 outcomeToStep,
11 refreshedSourceProfileDirectory,
12 refreshedSourceStep,
13 resolveWizardTarget,
14} from "./browserImportWizard.logic";
15​
16describe("wizard target selection", () => {
17 it("treats an existing profile whose id is new as an existing target", () => {
18 const profiles = [{ id: "new", name: "Existing new" }];
19 const selection = initialTargetSelection(false, profiles);
20​
21 expect(selection).toEqual({ kind: "existing", profileId: "new" });
22 expect(resolveWizardTarget(selection, "generated", profiles)).toEqual({
23 kind: "existing",
24 profileId: "new",
25 name: "Existing new",
26 });
27 });
28​
29 it("rejects an existing target that is no longer listed", () => {
30 expect(
31 resolveWizardTarget({ kind: "existing", profileId: "removed" }, "generated", [
32 { id: "default", name: "Default" },
33 ]),
34 ).toBeUndefined();
35 });
36});
37​
38const source = (over: Partial<BrowserImportSource> = {}): BrowserImportSource => ({
39 id: "helium",
40 name: "Helium",
41 profiles: [{ directory: "Default", name: "You" }],
42 ...over,
43});
44​
45describe("initialWizardStep", () => {
46 it("opens on the quit screen when the browser is running", () => {
47 expect(initialWizardStep(source({ unavailable: "browserRunning" }))).toEqual({ step: "quit" });
48 });
49​
50 it("opens on configure when the source is ready", () => {
51 expect(initialWizardStep(source())).toEqual({ step: "configure" });
52 });
53​
54 it("blocks on a reason nothing local can fix", () => {
55 expect(initialWizardStep(source({ unavailable: "unsupportedPlatform" }))).toEqual({
56 step: "blocked",
57 reason: "unsupportedPlatform",
58 });
59 });
60​
61 it("blocks a ready source that reports no importable profiles", () => {
62 expect(initialWizardStep(source({ profiles: [] }))).toEqual({
63 step: "blocked",
64 reason: "unknownSourceProfile",
65 });
66 });
67​
68 it("handles source availability before checking its profiles", () => {
69 expect(
70 initialWizardStep(source({ profiles: [], unavailable: "needsKeychainApproval" })),
71 ).toEqual({ step: "blocked", reason: "needsKeychainApproval" });
72 });
73});
74​
75describe("canCloseWizard", () => {
76 it("keeps the wizard open while an import writes its target profile", () => {
77 expect(canCloseWizard({ step: "importing" })).toBe(false);
78 expect(canCloseWizard({ step: "configure" })).toBe(true);
79 expect(canCloseWizard({ step: "blocked", reason: "readFailed" })).toBe(true);
80 });
81});
82​
83describe("outcomeToStep", () => {
84 it("lands on done after a successful import", () => {
85 expect(
86 outcomeToStep({
87 kind: "imported",
88 imported: 12,
89 skipped: 3,
90 skippedDomains: ["example.com"],
91 targetName: "Work",
92 }),
93 ).toEqual({
94 step: "done",
95 imported: 12,
96 skipped: 3,
97 skippedDomains: ["example.com"],
98 targetName: "Work",
99 });
100 });
101​
102 it("routes a reopened browser back to the quit screen", () => {
103 expect(outcomeToStep({ kind: "blocked", reason: "browserRunning" })).toEqual({ step: "quit" });
104 });
105​
106 it("surfaces every other failure on the blocked screen", () => {
107 expect(outcomeToStep({ kind: "blocked", reason: "readFailed" })).toEqual({
108 step: "blocked",
109 reason: "readFailed",
110 });
111 });
112});
113​
114describe("refreshedSourceStep", () => {
115 it("moves to configure once a quit browser frees its cookies", () => {
116 expect(refreshedSourceStep(source())).toEqual({ step: "configure" });
117 });
118​
119 it("stays on quit while the browser is still running", () => {
120 expect(refreshedSourceStep(source({ unavailable: "browserRunning" }))).toEqual({
121 step: "quit",
122 });
123 });
124​
125 it("blocks when the source vanished from the list", () => {
126 expect(refreshedSourceStep(undefined)).toEqual({ step: "blocked", reason: "unknownSource" });
127 });
128​
129 it("blocks when the refreshed source has no profiles", () => {
130 expect(refreshedSourceStep(source({ profiles: [] }))).toEqual({
131 step: "blocked",
132 reason: "unknownSourceProfile",
133 });
134 });
135});
136​
137describe("refreshedSourceProfileDirectory", () => {
138 const refreshed = source({
139 profiles: [
140 { directory: "Default", name: "Personal" },
141 { directory: "Profile 2", name: "Work" },
142 ],
143 });
144​
145 it("preserves a selected non-first profile after the browser quits", () => {
146 expect(refreshedSourceProfileDirectory("Profile 2", refreshed)).toBe("Profile 2");
147 });
148​
149 it("falls back only when the old profile vanished and clears an empty result", () => {
150 expect(refreshedSourceProfileDirectory("Removed", refreshed)).toBe("Default");
151 expect(refreshedSourceProfileDirectory("Removed", source({ profiles: [] }))).toBe("");
152 });
153});
154​
155describe("isRetryableReason", () => {
156 it("offers a retry for failures a second attempt can clear", () => {
157 expect(isRetryableReason("needsKeychainApproval")).toBe(true);
158 expect(isRetryableReason("keychainUnavailable")).toBe(true);
159 expect(isRetryableReason("readFailed")).toBe(true);
160 });
161​
162 it("does not offer a retry for a permanent failure", () => {
163 expect(isRetryableReason("unsupportedPlatform")).toBe(false);
164 expect(isRetryableReason("unknownSourceProfile")).toBe(false);
165 // Retrying the same new-profile import cannot lower the profile count.
166 expect(isRetryableReason("profileLimitReached")).toBe(false);
167 });
168​
169 it("offers a retry once the user has signed in to create the missing key", () => {
170 // The blocked copy tells the user to sign in and retry, so the screen
171 // has to offer the retry it asks for.
172 expect(isRetryableReason("keychainItemMissing")).toBe(true);
173 });
174​
175 it("offers a retry when the cookies landed but the new profile was not saved", () => {
176 expect(isRetryableReason("profileNotSaved")).toBe(true);
177 });
178});
179​
180describe("formatSkippedDomains", () => {
181 it("joins a short list naturally", () => {
182 expect(formatSkippedDomains([])).toBe("");
183 expect(formatSkippedDomains(["a.com"])).toBe("a.com");
184 expect(formatSkippedDomains(["a.com", "b.com"])).toBe("a.com and b.com");
185 expect(formatSkippedDomains(["a.com", "b.com", "c.com"])).toBe("a.com, b.com and c.com");
186 });
187​
188 it("summarizes a long list", () => {
189 expect(formatSkippedDomains(["a.com", "b.com", "c.com", "d.com", "e.com"])).toBe(
190 "a.com, b.com, c.com and 2 more",
191 );
192 });
193});