Skip to content
This repository was archived by the owner on Oct 21, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings";
import { describe, expect, it } from "vite-plus/test";

import {
buildCreateProviderProfilePatch,
buildDeleteProviderProfilePatch,
buildProjectProfilePatch,
decodeProviderProfileValue,
encodeProviderProfileValue,
NO_PROVIDER_PROFILE_VALUE,
Expand Down Expand Up @@ -92,3 +94,63 @@ describe("provider profile select values", () => {
expect(decodeProviderProfileValue("none")).toBeUndefined();
});
});

describe("buildProjectProfilePatch", () => {
const resolve = (patch: ReturnType<typeof buildProjectProfilePatch>, projectId: ProjectId) =>
resolveProjectProviderProfileId(applyServerSettingsPatch(settings, patch), projectId);

it("drops the override when the environment default already gives the target", () => {
const next = applyServerSettingsPatch(
settings,
buildProjectProfilePatch(settings, other, acme),
);
expect(next.projectSettingsOverrides).not.toHaveProperty(other);
expect(resolveProjectProviderProfileId(next, other)).toBe(acme);
});

it("writes an explicit override otherwise, keeping the project's other overrides", () => {
const patch = buildProjectProfilePatch(settings, optedIn, globex);
expect(applyServerSettingsPatch(settings, patch).projectSettingsOverrides[optedIn]).toEqual({
providerProfileId: globex,
defaultRuntimeMode: "approval-required",
});
});

it("removes a project from the default profile with an explicit opt-out", () => {
expect(resolve(buildProjectProfilePatch(settings, inheriting, null), inheriting)).toBeNull();
});
});

describe("buildCreateProviderProfilePatch", () => {
const initech = ProviderProfileId.make("initech");
const profile = { name: "Initech", instanceIds: [], defaultModelSelection: null };

it("creates the profile with its projects and can make it the default", () => {
const next = applyServerSettingsPatch(
settings,
buildCreateProviderProfilePatch(settings, {
id: initech,
profile,
projectIds: [other, inheriting],
makeDefault: true,
}),
);
expect(next.providerProfiles[initech]).toEqual(profile);
expect(next.providerProfileId).toBe(initech);
expect(resolveProjectProviderProfileId(next, other)).toBe(initech);
expect(resolveProjectProviderProfileId(next, inheriting)).toBe(initech);
// Projects left out keep what they had; the opt-out stays an opt-out.
expect(resolveProjectProviderProfileId(next, optedOut)).toBeNull();
});

it("leaves the environment default alone unless asked", () => {
const patch = buildCreateProviderProfilePatch(settings, {
id: initech,
profile,
projectIds: [],
makeDefault: false,
});
expect(patch).not.toHaveProperty("providerProfileId");
expect(patch).not.toHaveProperty("projectSettingsOverrides");
});
});
53 changes: 46 additions & 7 deletions apps/web/src/components/settings/ProviderProfilesSettings.logic.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
type ProjectId,
type ProjectSettingsOverrides,
type ProviderProfile,
ProviderProfileId,
type ServerSettings,
type ServerSettingsPatch,
Expand Down Expand Up @@ -62,17 +63,55 @@ export function buildDeleteProviderProfilePatch(
};
}

/** The profile a project uses: its own override, including an explicit "no profile", else the environment's. */
export function resolveProjectProviderProfileId(
/**
* Makes a project use `target` (a profile, or `null` for every provider) with the smallest
* override: none when the environment default already gives it, else an explicit one. Other
* overrides on the project are kept.
*/
export function buildProjectProfilePatch(
settings: Pick<ServerSettings, "providerProfileId" | "projectSettingsOverrides">,
projectId: ProjectId,
): ProviderProfileId | null {
const entry = settings.projectSettingsOverrides[projectId];
return entry !== undefined && Object.hasOwn(entry, "providerProfileId")
? (entry.providerProfileId ?? null)
: settings.providerProfileId;
target: ProviderProfileId | null,
): ServerSettingsPatch {
const next =
target === settings.providerProfileId
? clearProjectSettingsOverrides(settings, projectId, ["providerProfileId"])
: { ...settings.projectSettingsOverrides[projectId], providerProfileId: target };
return { projectSettingsOverrides: { [projectId]: next } };
}

/**
* Saves a new profile in one write, with the projects picked for it and, optionally, as the
* profile projects without their own use.
*/
export function buildCreateProviderProfilePatch(
settings: Pick<ServerSettings, "providerProfileId" | "projectSettingsOverrides">,
input: {
readonly id: ProviderProfileId;
readonly profile: ProviderProfile;
readonly projectIds: ReadonlyArray<ProjectId>;
readonly makeDefault: boolean;
},
): ServerSettingsPatch {
const next = input.makeDefault ? { ...settings, providerProfileId: input.id } : settings;
const overrides: Record<ProjectId, ProjectSettingsOverrides | null> = {};
for (const projectId of input.projectIds) {
Object.assign(
overrides,
buildProjectProfilePatch(next, projectId, input.id).projectSettingsOverrides,
);
}
return {
providerProfiles: { [input.id]: input.profile },
...(input.makeDefault ? { providerProfileId: input.id } : {}),
...(Object.keys(overrides).length > 0 ? { projectSettingsOverrides: overrides } : {}),
};
}

// Kept importable from here for the settings screens; the shared client runtime owns it so web
// and mobile resolve a project's profile identically.
export { resolveProjectProviderProfileId } from "@t3tools/client-runtime/state/profile-scope";

/**
* Select values for the profile picker. Profile ids are user-derived slugs
* (a profile named "None" gets id `none`), so they are prefixed to never
Expand Down
Loading
Loading