Skip to content

Commit 9f399ae

Browse files
juliusmarmingecodex
andcommitted
Complete validation paths for PR #3100
Gate both footer and forward step-header navigation through Identity validation while preserving backward navigation. Keep the navigation rule independently testable and surface the existing inline error on blocked skips. Co-authored-by: codex <[email protected]>
1 parent db9a000 commit 9f399ae

5 files changed

Lines changed: 239 additions & 131 deletions
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export type WizardNavigation =
2+
| { readonly kind: "navigate"; readonly step: number }
3+
| { readonly kind: "blocked"; readonly step: number; readonly error: string };
4+
5+
const IDENTITY_STEP = 1;
6+
7+
export const ADD_PROVIDER_WIZARD_STEPS = ["Driver", "Identity", "Config"] as const;
8+
9+
/**
10+
* Resolve navigation within the add-provider wizard.
11+
*
12+
* Moving forward past Identity requires a valid instance id, whether the user
13+
* advances one step at a time or skips directly to Config from a step header.
14+
* A blocked skip lands on Identity so its existing inline validation is
15+
* visible. Backward navigation is always preserved.
16+
*/
17+
export function resolveWizardNavigation(
18+
currentStep: number,
19+
requestedStep: number,
20+
stepCount: number,
21+
validation: { readonly instanceIdError: string | null },
22+
): WizardNavigation {
23+
const lastStep = Math.max(0, stepCount - 1);
24+
const targetStep = Math.max(0, Math.min(lastStep, requestedStep));
25+
const movesForwardPastIdentity = currentStep <= IDENTITY_STEP && targetStep > IDENTITY_STEP;
26+
27+
if (movesForwardPastIdentity && validation.instanceIdError !== null) {
28+
return {
29+
kind: "blocked",
30+
step: Math.min(IDENTITY_STEP, lastStep),
31+
error: validation.instanceIdError,
32+
};
33+
}
34+
35+
return { kind: "navigate", step: targetStep };
36+
}
Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
11
import { describe, expect, it } from "vite-plus/test";
22

3-
import { advanceInstanceWizard } from "./AddProviderInstanceDialog";
4-
5-
describe("advanceInstanceWizard", () => {
6-
it("advances from the Driver step even when the instance id is invalid", () => {
7-
// The Driver step (index 0) does not own the Instance ID field, so an
8-
// invalid id must not block leaving it.
9-
expect(advanceInstanceWizard(0, 3, { instanceIdError: "Instance ID is required." })).toEqual({
10-
kind: "advance",
3+
import { resolveWizardNavigation } from "./AddProviderInstanceDialog.logic";
4+
5+
describe("resolveWizardNavigation", () => {
6+
const invalidId = { instanceIdError: "Instance ID is required." };
7+
const validId = { instanceIdError: null };
8+
9+
it("allows moving from Driver to Identity before the instance id is valid", () => {
10+
expect(resolveWizardNavigation(0, 1, 3, invalidId)).toEqual({ kind: "navigate", step: 1 });
11+
});
12+
13+
it("blocks Next from Identity to Config while the instance id is invalid", () => {
14+
expect(resolveWizardNavigation(1, 2, 3, invalidId)).toEqual({
15+
kind: "blocked",
1116
step: 1,
17+
error: "Instance ID is required.",
1218
});
1319
});
1420

15-
it("blocks leaving the Identity step while the instance id is invalid", () => {
16-
// Regression for #2813: clicking Next on the Identity step used to advance
17-
// unconditionally, so the user reached the final step only for "Add
18-
// instance" to silently no-op.
19-
expect(advanceInstanceWizard(1, 3, { instanceIdError: "Instance ID is required." })).toEqual({
21+
it("stops a direct Driver-to-Config skip at Identity and surfaces its error", () => {
22+
expect(resolveWizardNavigation(0, 2, 3, invalidId)).toEqual({
2023
kind: "blocked",
24+
step: 1,
2125
error: "Instance ID is required.",
2226
});
2327
});
2428

25-
it("advances from the Identity step once the instance id is valid", () => {
26-
expect(advanceInstanceWizard(1, 3, { instanceIdError: null })).toEqual({
27-
kind: "advance",
28-
step: 2,
29-
});
29+
it("allows advancing and skipping forward once the instance id is valid", () => {
30+
expect(resolveWizardNavigation(1, 2, 3, validId)).toEqual({ kind: "navigate", step: 2 });
31+
expect(resolveWizardNavigation(0, 2, 3, validId)).toEqual({ kind: "navigate", step: 2 });
3032
});
3133

32-
it("never advances past the last step", () => {
33-
expect(advanceInstanceWizard(2, 3, { instanceIdError: null })).toEqual({
34-
kind: "advance",
35-
step: 2,
36-
});
34+
it("always preserves backward Driver and Identity navigation", () => {
35+
expect(resolveWizardNavigation(2, 1, 3, invalidId)).toEqual({ kind: "navigate", step: 1 });
36+
expect(resolveWizardNavigation(2, 0, 3, invalidId)).toEqual({ kind: "navigate", step: 0 });
37+
expect(resolveWizardNavigation(1, 0, 3, invalidId)).toEqual({ kind: "navigate", step: 0 });
38+
});
39+
40+
it("clamps requested steps to the wizard bounds", () => {
41+
expect(resolveWizardNavigation(2, 8, 3, validId)).toEqual({ kind: "navigate", step: 2 });
42+
expect(resolveWizardNavigation(0, -1, 3, invalidId)).toEqual({ kind: "navigate", step: 0 });
3743
});
3844
});

‎apps/web/src/components/settings/AddProviderInstanceDialog.tsx‎

Lines changed: 43 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
"use client";
22

3-
import { CheckIcon } from "lucide-react";
43
import { Radio as RadioPrimitive } from "@base-ui/react/radio";
5-
import { useCallback, useMemo, useState } from "react";
4+
import { useMemo, useState } from "react";
65
import {
76
ProviderInstanceId,
87
ProviderDriverKind,
@@ -29,6 +28,12 @@ import { toastManager } from "../ui/toast";
2928
import { DRIVER_OPTION_BY_VALUE, DRIVER_OPTIONS } from "./providerDriverMeta";
3029
import { ProviderSettingsForm, deriveProviderSettingsFields } from "./ProviderSettingsForm";
3130
import { AnimatedHeight } from "../AnimatedHeight";
31+
import {
32+
ADD_PROVIDER_WIZARD_STEPS,
33+
resolveWizardNavigation,
34+
type WizardNavigation,
35+
} from "./AddProviderInstanceDialog.logic";
36+
import { AddProviderInstanceWizardSteps } from "./AddProviderInstanceWizardSteps";
3237

3338
const PROVIDER_ACCENT_SWATCHES = [
3439
"#2563eb",
@@ -108,30 +113,6 @@ function validateInstanceId(id: string, existing: ReadonlySet<string>): string |
108113
return null;
109114
}
110115

111-
export type WizardAdvance =
112-
| { readonly kind: "advance"; readonly step: number }
113-
| { readonly kind: "blocked"; readonly error: string };
114-
115-
/**
116-
* Decide what clicking "Next" should do in the add-instance wizard. The
117-
* Identity step (index 1) owns the Instance ID, so the wizard must not advance
118-
* past it while the id is invalid — otherwise the user reaches the final step
119-
* only for "Add instance" to silently no-op. Returns either the next step to
120-
* move to, or the error that blocks advancing (mirrors the gate `handleSave`
121-
* applies before submit).
122-
*/
123-
export function advanceInstanceWizard(
124-
currentStep: number,
125-
stepCount: number,
126-
validation: { readonly instanceIdError: string | null },
127-
): WizardAdvance {
128-
const blockingError = currentStep === 1 ? validation.instanceIdError : null;
129-
if (blockingError !== null) {
130-
return { kind: "blocked", error: blockingError };
131-
}
132-
return { kind: "advance", step: Math.min(stepCount - 1, currentStep + 1) };
133-
}
134-
135116
interface AddProviderInstanceDialogProps {
136117
open: boolean;
137118
onOpenChange: (open: boolean) => void;
@@ -167,26 +148,37 @@ export function AddProviderInstanceDialog({ open, onOpenChange }: AddProviderIns
167148
const instanceIdError = validateInstanceId(instanceId, existingIds);
168149
const showInstanceIdError = hasAttemptedSubmit && instanceIdError !== null;
169150
const previewLabel = label.trim() || `${driverOption.label} Workspace`;
170-
const wizardSteps = ["Driver", "Identity", "Config"] as const;
171151
const wizardStepSummaries = [driverOption.label, previewLabel, null] as const;
172152

173153
const configDraft = configByDriver[driver] ?? EMPTY_CONFIG_DRAFT;
174-
const setConfigDraft = useCallback(
175-
(config: Record<string, unknown> | undefined) => {
176-
setConfigByDriver((existing) => {
177-
const next = { ...existing };
178-
if (config === undefined || Object.keys(config).length === 0) {
179-
delete next[driver];
180-
} else {
181-
next[driver] = config;
182-
}
183-
return next;
184-
});
185-
},
186-
[driver],
187-
);
154+
const setConfigDraft = (config: Record<string, unknown> | undefined) => {
155+
setConfigByDriver((existing) => {
156+
const next = { ...existing };
157+
if (config === undefined || Object.keys(config).length === 0) {
158+
delete next[driver];
159+
} else {
160+
next[driver] = config;
161+
}
162+
return next;
163+
});
164+
};
165+
166+
const applyWizardNavigation = (navigation: WizardNavigation) => {
167+
if (navigation.kind === "blocked") {
168+
setHasAttemptedSubmit(true);
169+
}
170+
setWizardStep(navigation.step);
171+
};
172+
173+
const navigateToStep = (requestedStep: number) => {
174+
applyWizardNavigation(
175+
resolveWizardNavigation(wizardStep, requestedStep, ADD_PROVIDER_WIZARD_STEPS.length, {
176+
instanceIdError,
177+
}),
178+
);
179+
};
188180

189-
const handleSave = useCallback(() => {
181+
const handleSave = () => {
190182
setHasAttemptedSubmit(true);
191183
if (instanceIdError !== null) return;
192184

@@ -225,18 +217,7 @@ export function AddProviderInstanceDialog({ open, onOpenChange }: AddProviderIns
225217
description: error instanceof Error ? error.message : "Update failed.",
226218
});
227219
}
228-
}, [
229-
driver,
230-
driverOption,
231-
configByDriver,
232-
instanceId,
233-
instanceIdError,
234-
label,
235-
accentColor,
236-
onOpenChange,
237-
settings.providerInstances,
238-
updateSettings,
239-
]);
220+
};
240221

241222
return (
242223
<Dialog open={open} onOpenChange={onOpenChange}>
@@ -248,46 +229,12 @@ export function AddProviderInstanceDialog({ open, onOpenChange }: AddProviderIns
248229
Configure an additional provider instance — for example, a second Codex install
249230
pointed at a different workspace.
250231
</DialogDescription>
251-
<div className="grid grid-cols-3 gap-2">
252-
{wizardSteps.map((step, index) => (
253-
<button
254-
key={step}
255-
type="button"
256-
className={cn(
257-
"grid min-w-0 grid-cols-[1rem_minmax(0,1fr)] gap-x-2 rounded-lg border px-3 py-2 text-left",
258-
index === wizardStep
259-
? "border-primary bg-primary/10 ring-1 ring-primary/25"
260-
: index < wizardStep
261-
? "border-border bg-background"
262-
: "border-border bg-muted/40",
263-
)}
264-
onClick={() => setWizardStep(index)}
265-
>
266-
<span
267-
className={cn(
268-
"row-span-2 mt-0.5 grid size-4 place-items-center rounded-full border",
269-
index < wizardStep
270-
? "border-primary bg-primary text-primary-foreground"
271-
: index === wizardStep
272-
? "border-primary bg-background"
273-
: "border-muted-foreground/35 bg-background",
274-
)}
275-
aria-hidden
276-
>
277-
{index < wizardStep ? <CheckIcon className="size-3" /> : null}
278-
</span>
279-
<span className="text-[10px] font-medium uppercase text-muted-foreground">
280-
Step {index + 1}
281-
</span>
282-
<span className="truncate text-xs font-semibold text-foreground">
283-
{step}
284-
{index < wizardStep && wizardStepSummaries[index]
285-
? `: ${wizardStepSummaries[index]}`
286-
: ""}
287-
</span>
288-
</button>
289-
))}
290-
</div>
232+
<AddProviderInstanceWizardSteps
233+
currentStep={wizardStep}
234+
summaries={wizardStepSummaries}
235+
instanceIdError={instanceIdError}
236+
onNavigation={applyWizardNavigation}
237+
/>
291238
</DialogHeader>
292239

293240
<div
@@ -476,20 +423,8 @@ export function AddProviderInstanceDialog({ open, onOpenChange }: AddProviderIns
476423
>
477424
{wizardStep === 0 ? "Cancel" : "Back"}
478425
</Button>
479-
{wizardStep < wizardSteps.length - 1 ? (
480-
<Button
481-
size="sm"
482-
onClick={() => {
483-
const advance = advanceInstanceWizard(wizardStep, wizardSteps.length, {
484-
instanceIdError,
485-
});
486-
if (advance.kind === "blocked") {
487-
setHasAttemptedSubmit(true);
488-
return;
489-
}
490-
setWizardStep(advance.step);
491-
}}
492-
>
426+
{wizardStep < ADD_PROVIDER_WIZARD_STEPS.length - 1 ? (
427+
<Button size="sm" onClick={() => navigateToStep(wizardStep + 1)}>
493428
Next
494429
</Button>
495430
) : (
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { Children, isValidElement, type ReactElement } from "react";
2+
import { describe, expect, it, vi } from "vite-plus/test";
3+
4+
import { ADD_PROVIDER_WIZARD_STEPS } from "./AddProviderInstanceDialog.logic";
5+
import { AddProviderInstanceWizardSteps } from "./AddProviderInstanceWizardSteps";
6+
7+
interface StepButtonProps {
8+
readonly "aria-current"?: string;
9+
readonly onClick: () => void;
10+
}
11+
12+
function renderStepButtons(
13+
currentStep: number,
14+
instanceIdError: string | null,
15+
onNavigation: Parameters<typeof AddProviderInstanceWizardSteps>[0]["onNavigation"],
16+
): ReactElement<StepButtonProps>[] {
17+
const header = AddProviderInstanceWizardSteps({
18+
currentStep,
19+
summaries: ["Codex", "Codex Workspace", null],
20+
instanceIdError,
21+
onNavigation,
22+
});
23+
24+
return Children.toArray(header.props.children).filter(
25+
(child): child is ReactElement<StepButtonProps> => isValidElement(child),
26+
);
27+
}
28+
29+
describe("AddProviderInstanceWizardSteps", () => {
30+
it("gates the actual Config header click through Identity validation", () => {
31+
const onNavigation = vi.fn();
32+
const buttons = renderStepButtons(0, "Instance ID is required.", onNavigation);
33+
34+
expect(buttons).toHaveLength(ADD_PROVIDER_WIZARD_STEPS.length);
35+
buttons[2]!.props.onClick();
36+
37+
expect(onNavigation).toHaveBeenCalledOnce();
38+
expect(onNavigation).toHaveBeenCalledWith({
39+
kind: "blocked",
40+
step: 1,
41+
error: "Instance ID is required.",
42+
});
43+
});
44+
45+
it("marks the wizard step separately from the clicked button focus", () => {
46+
const buttons = renderStepButtons(1, "Instance ID is required.", vi.fn());
47+
48+
expect(buttons[0]!.props["aria-current"]).toBeUndefined();
49+
expect(buttons[1]!.props["aria-current"]).toBe("step");
50+
expect(buttons[2]!.props["aria-current"]).toBeUndefined();
51+
});
52+
53+
it("preserves the actual backward header click", () => {
54+
const onNavigation = vi.fn();
55+
const buttons = renderStepButtons(2, "Instance ID is required.", onNavigation);
56+
57+
buttons[0]!.props.onClick();
58+
59+
expect(onNavigation).toHaveBeenCalledWith({ kind: "navigate", step: 0 });
60+
});
61+
});

0 commit comments

Comments
 (0)