test(app): add E2E tests and improve form state with test coverage
This commit is contained in:
@@ -45,6 +45,14 @@ function getParamValue(value: string | string[] | undefined) {
|
||||
const trimmed = value.trim();
|
||||
return trimmed.length > 0 ? trimmed : undefined;
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
for (const entry of value) {
|
||||
const trimmed = entry.trim();
|
||||
if (trimmed.length > 0) {
|
||||
return trimmed;
|
||||
}
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { __private__ } from "./use-agent-form-state";
|
||||
|
||||
describe("useAgentFormState", () => {
|
||||
describe("__private__.combineInitialValues", () => {
|
||||
it("returns undefined when no initial values and no initial server id", () => {
|
||||
expect(__private__.combineInitialValues(undefined, null)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("does not inject a null serverId override when initialValues are present but serverId is absent", () => {
|
||||
const combined = __private__.combineInitialValues({}, null);
|
||||
expect(combined).toEqual({});
|
||||
expect(Object.prototype.hasOwnProperty.call(combined, "serverId")).toBe(false);
|
||||
});
|
||||
|
||||
it("injects serverId from options when provided", () => {
|
||||
expect(__private__.combineInitialValues({}, "daemon-1")).toEqual({
|
||||
serverId: "daemon-1",
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps other initial values without forcing serverId", () => {
|
||||
const combined = __private__.combineInitialValues({ workingDir: "/repo" }, null);
|
||||
expect(combined).toEqual({ workingDir: "/repo" });
|
||||
expect(Object.prototype.hasOwnProperty.call(combined, "serverId")).toBe(false);
|
||||
});
|
||||
|
||||
it("respects an explicit serverId override (including null) over initialServerId", () => {
|
||||
expect(__private__.combineInitialValues({ serverId: null }, "daemon-1")).toEqual({
|
||||
serverId: null,
|
||||
});
|
||||
|
||||
expect(__private__.combineInitialValues({ serverId: "daemon-2" }, "daemon-1")).toEqual({
|
||||
serverId: "daemon-2",
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -193,6 +193,29 @@ function resolveFormState(
|
||||
return result;
|
||||
}
|
||||
|
||||
function combineInitialValues(
|
||||
initialValues: FormInitialValues | undefined,
|
||||
initialServerId: string | null
|
||||
): FormInitialValues | undefined {
|
||||
const hasExplicitServerId = initialValues?.serverId !== undefined;
|
||||
const serverIdFromOptions = initialServerId === null ? undefined : initialServerId;
|
||||
|
||||
// If nobody provided initial values or an explicit serverId, let preferences drive defaults.
|
||||
if (!initialValues && !hasExplicitServerId && serverIdFromOptions === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (hasExplicitServerId) {
|
||||
return { ...initialValues, serverId: initialValues?.serverId };
|
||||
}
|
||||
|
||||
if (serverIdFromOptions !== undefined) {
|
||||
return { ...initialValues, serverId: serverIdFromOptions };
|
||||
}
|
||||
|
||||
return initialValues;
|
||||
}
|
||||
|
||||
export function useAgentFormState(
|
||||
options: UseAgentFormStateOptions = {}
|
||||
): UseAgentFormStateResult {
|
||||
@@ -251,13 +274,7 @@ export function useAgentFormState(
|
||||
|
||||
// Combine initialValues with initialServerId for resolution
|
||||
const combinedInitialValues = useMemo((): FormInitialValues | undefined => {
|
||||
if (!initialValues && initialServerId === null) {
|
||||
return undefined;
|
||||
}
|
||||
return {
|
||||
...initialValues,
|
||||
serverId: initialValues?.serverId ?? initialServerId,
|
||||
};
|
||||
return combineInitialValues(initialValues, initialServerId);
|
||||
}, [initialValues, initialServerId]);
|
||||
|
||||
// Resolve form state when data sources change
|
||||
@@ -547,3 +564,8 @@ export function useAgentFormState(
|
||||
|
||||
// Re-export for backwards compatibility
|
||||
export type CreateAgentInitialValues = FormInitialValues;
|
||||
|
||||
export const __private__ = {
|
||||
combineInitialValues,
|
||||
resolveFormState,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user