Skip to content
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
60 changes: 30 additions & 30 deletions FORK_CHANGES.md

Large diffs are not rendered by default.

68 changes: 68 additions & 0 deletions apps/web/src/components/settings/fork/CommitTextField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/** Fork: a text field that saves on blur or Enter (multi-line: blur only) and shows why a value cannot be saved. */
import { useState } from "react";

import { Input } from "../../ui/input";
import { Textarea } from "../../ui/textarea";

export function CommitTextField({
value,
onCommit,
ariaLabel,
placeholder,
multiline = false,
disabled = false,
}: {
readonly value: string;
/** Saves `text`; returns why it cannot be saved, or null. `revert` puts the stored value back. */
readonly onCommit: (text: string, revert: () => void) => string | null;
readonly ariaLabel: string;
readonly placeholder?: string;
readonly multiline?: boolean;
readonly disabled?: boolean;
}) {
const [draft, setDraft] = useState({ saved: value, text: value });
const [error, setError] = useState<string | null>(null);
if (draft.saved !== value) setDraft({ saved: value, text: value });

const commit = () => {
if (draft.text === value) return;
setError(onCommit(draft.text, () => setDraft({ saved: value, text: value })));
};
const change = (text: string) => {
setError(null);
setDraft({ ...draft, text });
};

return (
<div className="flex w-full flex-col gap-1 sm:w-72">
{multiline ? (
<Textarea
aria-label={ariaLabel}
placeholder={placeholder}
disabled={disabled}
value={draft.text}
onChange={(event) => change(event.target.value)}
onBlur={commit}
/>
) : (
<Input
size="sm"
aria-label={ariaLabel}
placeholder={placeholder}
disabled={disabled}
value={draft.text}
onChange={(event) => change(event.target.value)}
onBlur={commit}
onKeyDown={(event) => {
if (event.key === "Enter") commit();
}}
/>
)}
{error !== null ? (
<p role="alert" className="text-xs text-destructive-foreground">
{error}
</p>
) : null}
</div>
);
}
239 changes: 239 additions & 0 deletions apps/web/src/components/settings/fork/ForkPanels.browser.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
import { type ManagedPolicySummary, ServerSettings } from "@t3tools/contracts";
import * as Schema from "effect/Schema";
import { describe, expect, it, vi } from "vite-plus/test";
import { page, userEvent } from "vite-plus/test/browser";

import "~/index.css";
import { render } from "../../../../test/render";

type Settings = ServerSettings;
type UpdateOptions = { readonly onFailed?: () => void; readonly onSaved?: () => void };

const state = vi.hoisted(() => ({
settings: {} as Settings,
environments: [] as Array<{
environmentId: string;
providers: Array<{ instanceId: string; displayName: string }>;
managedPolicy?: ManagedPolicySummary;
}>,
update: vi.fn((_patch: unknown, _options?: UpdateOptions) => {}),
}));

function scope() {
const environments = state.environments.map(({ environmentId, managedPolicy }) => ({
environmentId,
label: environmentId,
connection: { phase: "connected" },
serverConfig: {
settings: state.settings,
environment: { capabilities: {}, platform: { os: "linux", arch: "x64", machine: "server" } },
...(managedPolicy ? { managedPolicy } : {}),
},
}));
return {
scope: {
kind: "all",
label: "All",
members: [],
environmentIds: environments.map(({ environmentId }) => environmentId),
},
environments,
connectedEnvironments: environments,
environment: environments[0] ?? null,
targets: environments.map(({ environmentId }) => ({
environmentId,
label: environmentId,
projectId: null,
settings: state.settings,
sources: {},
})),
target: null,
};
}

vi.mock(import("../SettingsScopeContext"), async (original) => ({
...(await original()),
useSettingsScope: scope as never,
useOptionalSettingsScope: scope as never,
}));
vi.mock(import("../useScopedSettings"), async (original) => ({
...(await original()),
useScopedSettings: ((select: (settings: Settings) => unknown) => select(state.settings)) as never,
useScopedSettingsMixed: () => false,
useScopedSettingsWriteAllowed: () => true,
useUpdateScopedSettings: (() => state.update) as never,
useClearScopedSettings: (() => vi.fn()) as never,
useClearProjectOverrides: (() => vi.fn()) as never,
}));
vi.mock(import("../../../state/environments"), async (original) => ({
...(await original()),
useEnvironmentProviders: (() =>
new Map(
state.environments.map(({ environmentId, providers }) => [environmentId, providers]),
)) as never,
}));
vi.mock(import("../../../state/entities"), async (original) => ({
...(await original()),
useThreadShells: (() => [{ id: "thread-1", title: "Fix the login page" }]) as never,
}));
vi.mock(import("../../../state/session"), async (original) => ({
...(await original()),
useEnvironmentsWithScope: ((environments: ReadonlyArray<{ environmentId: string }>) =>
new Set(environments.map(({ environmentId }) => environmentId))) as never,
}));

import { HandoffSettings } from "./HandoffSettings";
import { PlaytestSettings } from "./PlaytestSettings";
import { UpdatesSettings } from "./UpdatesSettings";

const decodeServerSettings = Schema.decodeUnknownSync(ServerSettings);

/** The settings a server reads from `file`, so an old or empty file is tested as it loads. */
function setup(file: object, managedPolicy?: ManagedPolicySummary) {
state.settings = decodeServerSettings(file);
state.environments = [
{ environmentId: "env-a", providers: [], ...(managedPolicy ? { managedPolicy } : {}) },
];
state.update.mockReset();
}

const STEPS = {
claim: { argv: ["/usr/local/bin/game", "claim"] },
renew: { argv: ["/usr/local/bin/game", "renew"] },
push: [{ name: "deploy", argv: ["/usr/local/bin/game", "deploy"] }],
release: { argv: ["/usr/local/bin/game", "release"] },
};

describe("UpdatesSettings", () => {
it("writes automatic updates and the quiet check from an empty settings file", async () => {
setup({});
await using screen = await render(<UpdatesSettings />);

const toggle = screen.getByRole("switch", { name: "Update automatically" });
await expect.element(toggle).not.toBeChecked();
await userEvent.click(toggle);
expect(state.update.mock.calls[0]![0]).toEqual({ automaticUpdates: true });

await userEvent.type(
screen.getByRole("textbox", { name: "Quiet check before an update" }),
" /usr/local/bin/quiet {Enter}",
);
expect(state.update.mock.calls[1]![0]).toEqual({
automaticUpdatesQuietCommand: "/usr/local/bin/quiet",
});
});
});

describe("HandoffSettings", () => {
it("writes the whole value with the new mode, and adds a thread by id", async () => {
setup({});
await using screen = await render(<HandoffSettings isProjectScope={false} />);
const empty = state.settings.handoff;

await userEvent.click(screen.getByRole("combobox", { name: "Handoff mode" }));
await userEvent.click(page.getByRole("option", { name: "Handoff" }));
expect(state.update.mock.calls[0]![0]).toEqual({ handoff: { ...empty, mode: "handoff" } });

const threadId = screen.getByRole("textbox", { name: "Thread id" });
await userEvent.type(threadId, "thread-1{Enter}");
expect(state.update.mock.calls[1]![0]).toEqual({
handoff: { ...empty, threads: { "thread-1": "compact" } },
});
// The typed id stays until the save is through: a refused save keeps it to try again.
state.update.mock.calls[1]![1]?.onFailed?.();
await expect.element(threadId).toHaveValue("thread-1");
state.update.mock.calls[1]![1]?.onSaved?.();
await expect.element(threadId).toHaveValue("");
});

it("greys out every control of a locked handoff, the thread list and command too", async () => {
setup(
{ handoff: { mode: "handoff", threads: { "thread-1": "compact" } } },
{ organization: "Example Corp", lockedSettings: ["handoff"] },
);
await using screen = await render(<HandoffSettings isProjectScope={false} />);

await expect.element(screen.getByText("Fix the login page")).toBeVisible();
for (const selector of [
'[aria-label="Handoff mode"]',
'[aria-label="Handoff mode for thread thread-1"]',
'[aria-label="Remove the handoff mode of thread thread-1"]',
'[aria-label="Thread id"]',
]) {
expect(screen.container.querySelector(selector)?.closest("[inert]"), selector).not.toBeNull();
}
const add = [...screen.container.querySelectorAll("button")].find(
(button) => button.textContent === "Add thread",
);
expect(add?.closest("[inert]")).not.toBeNull();
});

it("lists a stored thread by its title and removes it", async () => {
setup({ handoff: { mode: "handoff", threads: { "thread-1": "compact" } } });
await using screen = await render(<HandoffSettings isProjectScope={false} />);

await expect.element(screen.getByText("Fix the login page")).toBeVisible();
await userEvent.click(
screen.getByRole("button", { name: "Remove the handoff mode of thread thread-1" }),
);
expect(state.update.mock.calls[0]![0]).toEqual({
handoff: { ...state.settings.handoff, threads: {} },
});
});
});

describe("PlaytestSettings", () => {
it("keeps the switch off until the steps are set, then saves edited steps whole", async () => {
setup({});
await using screen = await render(<PlaytestSettings isProjectScope={false} />);

await expect.element(screen.getByRole("switch", { name: "Playtest sessions" })).toBeDisabled();
await userEvent.click(screen.getByRole("button", { name: "Edit steps" }));
const dialog = page.getByRole("dialog");
const steps = dialog.getByRole("textbox", { name: "Playtest steps" });
await userEvent.clear(steps);
await userEvent.type(steps, "not json");
await userEvent.click(dialog.getByRole("button", { name: "Save steps" }));
await expect.element(dialog.getByRole("alert")).toBeVisible();
expect(state.update).not.toHaveBeenCalled();

await userEvent.clear(steps);
await userEvent.fill(steps, JSON.stringify(STEPS));
await userEvent.click(dialog.getByRole("button", { name: "Save steps" }));
const saved = (state.update.mock.calls[0]![0] as { playtest: typeof state.settings.playtest })
.playtest;
expect(saved.enabled).toBe(false);
expect(saved.push.map((step) => step.name)).toEqual(["deploy"]);
expect(saved.release?.argv).toEqual(STEPS.release.argv);
});

it("switches a complete adapter on and off", async () => {
setup({ playtest: STEPS });
await using screen = await render(<PlaytestSettings isProjectScope={false} />);

await userEvent.click(screen.getByRole("switch", { name: "Playtest sessions" }));
expect(state.update.mock.calls[0]![0]).toEqual({
playtest: { ...state.settings.playtest, enabled: true },
});

// The server now reads it as on.
state.settings = decodeServerSettings({ playtest: { ...STEPS, enabled: true } });
await screen.rerender(<PlaytestSettings isProjectScope={false} />);
const toggle = screen.getByRole("switch", { name: "Playtest sessions" });
await expect.element(toggle).toBeChecked();
await userEvent.click(toggle);
expect(state.update.mock.calls[1]![0]).toEqual({
playtest: { ...state.settings.playtest, enabled: false },
});
});

it("resets the server's playtest to the real default, steps included", async () => {
setup({ playtest: { ...STEPS, enabled: true, renewMinutes: 3 } });
await using screen = await render(<PlaytestSettings isProjectScope={false} />);

await userEvent.click(screen.getByRole("button", { name: "Reset playtest to default" }));
expect(state.update.mock.calls[0]![0]).toEqual({
playtest: decodeServerSettings({}).playtest,
});
});
});
38 changes: 36 additions & 2 deletions apps/web/src/components/settings/fork/ForkRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
* too. The server refuses such a write anyway; this only keeps a click from ending in an error.
*/
/* oxlint-disable t3code/no-untranslated-copy -- Fork: the fork's own settings are English only; upstream's translation catalogs hold no fork keys. */
import { isManagedSettingPath } from "@t3tools/contracts";
import { isManagedSettingPath, type ServerSettings } from "@t3tools/contracts";
import { type ComponentProps, useState } from "react";

import { useEnvironmentProviders } from "../../../state/environments";
import { EMPTY_SERVER_PROVIDERS } from "../../../state/server";
import { IntervalSecondsField } from "../IntervalSecondsField";
import { useSettingsScope } from "../SettingsScopeContext";
import { SettingsRow } from "../settingsLayout";
import { SettingResetButton, SettingsRow } from "../settingsLayout";
import { useScopedSettingsWriteAllowed } from "../useScopedSettings";
import { subRowResetValue } from "./forkRowReset.logic";

/** Every selected environment's accounts, the primary's first, as the agent slots rows list them. */
export function useSelectedProviders() {
Expand Down Expand Up @@ -86,3 +87,36 @@ export function BoundedNumberField({
/>
);
}

/**
* A sub-row's reset on a project screen whose value is the project's own: it puts back only that
* row's fields from the environment's value and keeps the rest of the override (upstream's row
* reset would clear the whole override). Null where there is nothing to reset (see
* {@link subRowResetValue}).
*/
export function useSubRowReset<K extends "handoff" | "playtest">(
key: K,
shown: ServerSettings[K],
isOwn: boolean,
save: (next: ServerSettings[K]) => void,
) {
const { targets, connectedEnvironments } = useSettingsScope();
const target = targets[0];
const environmentValue =
target === undefined || target.projectId === null
? undefined
: connectedEnvironments.find(
(environment) => environment.environmentId === target.environmentId,
)?.serverConfig?.settings[key];
return (keys: ReadonlyArray<keyof ServerSettings[K]>, ariaLabel: string) => {
if (!isOwn || environmentValue === undefined) return null;
const next = subRowResetValue(key, shown, environmentValue, keys);
return next === null ? null : (
<SettingResetButton
ariaLabel={ariaLabel}
tooltip="Reset to the environment's value"
onClick={() => save(next)}
/>
);
};
}
7 changes: 7 additions & 0 deletions apps/web/src/components/settings/fork/ForkSettingsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import { SettingsPageContainer, SettingsRow, SettingsSection } from "../settings
import { useSearchableSetting } from "../settingsSearch";
import { WaitsAndSlotsSettings } from "../WaitsAndSlotsSettings";
import { forkFeatureStatuses } from "./forkStatus.logic";
import { HandoffSettings } from "./HandoffSettings";
import { PlaytestSettings } from "./PlaytestSettings";
import { UpdatesSettings } from "./UpdatesSettings";
import { ModelPolicySettings } from "./ModelPolicySettings";
import { UsagePacingSettings } from "./UsagePacingSettings";

Expand Down Expand Up @@ -41,6 +44,10 @@ export function ForkSettingsPanel() {
<ForkGroup title="Reviews">
<ReviewRulesSettings isProjectScope={isProjectScope} />
</ForkGroup>
{isProjectScope ? null : <UpdatesSettings />}
<HandoffSettings isProjectScope={isProjectScope} />
<PlaytestSettings isProjectScope={isProjectScope} />
{/* MCP profiles (server.mcpProfiles and the profile picks) get their section here. */}
</SettingsPageContainer>
);
}
Expand Down
Loading
Loading