import { render } from "../../../test/render";
import { DEFAULT_UNIFIED_SETTINGS } from "@t3tools/contracts/settings";
import { StrictMode, type ReactNode } from "react";
import { describe, expect, it, vi } from "vite-plus/test";
import { page, userEvent } from "vite-plus/test/browser";
import "../../index.css";
import type { ScopedSettingsPatch } from "./scopedSettings";
type WritingStyle = typeof DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle;
const state = vi.hoisted(() => ({
styles: [] as WritingStyle[],
updateSettings: vi.fn<(patch: ScopedSettingsPatch) => void>(),
}));
vi.mock(import("@tanstack/react-router"), async (original) => ({
...(await original()),
useNavigate: (() => vi.fn()) as never,
}));
vi.mock(import("./useScopedSettings"), async (original) => ({
...(await original()),
useScopedSettings: (() => ({
...DEFAULT_UNIFIED_SETTINGS,
sourceControlWritingStyle: state.styles[0],
})) as never,
useScopedSettingsMixed: (keys) =>
!keys.includes("reviewGuidesEnabled") &&
JSON.stringify(state.styles[0]) !== JSON.stringify(state.styles[1]),
useUpdateScopedSettings: () => state.updateSettings,
}));
vi.mock(import("./SettingsScopeContext"), async (original) => ({
...(await original()),
useSettingsScope: (() => ({
scope: { kind: "all", environmentIds: [] },
environment: null,
connectedEnvironments: [],
targets: state.styles.map((style) => ({
settings: { ...DEFAULT_UNIFIED_SETTINGS, sourceControlWritingStyle: style },
})),
target: null,
})) as never,
}));
vi.mock(import("../../state/environments"), async (original) => ({
...(await original()),
useEnvironmentProviders: () => new Map(),
}));
vi.mock(import("./settingsLayout"), async (original) => ({
...(await original()),
SettingsRow: ({ title, control, resetAction, children }) => (
<div>
<h3>{title}</h3>
{control}
{resetAction}
{children}
</div>
),
SettingsSection: (({ children }: { children: ReactNode }) => children) as never,
}));
import { SourceControlWritingSettingsSection } from "./SourceControlWritingSettings";
const resetButton = (label: string) =>
page.getByRole("button", { name: `Reset ${label} to default` });
const content = () => (
<StrictMode>
<SourceControlWritingSettingsSection />
</StrictMode>
);
async function setup() {
state.styles = [
{
...DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle,
mode: "custom",
customInstructions: "First environment instructions",
followChangeRequestTemplates: true,
},
{
...DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle,
mode: "custom",
customInstructions: "Second environment instructions",
followChangeRequestTemplates: false,
},
];
const screen = await render(content());
state.updateSettings.mockReset().mockImplementation((patch) => {
state.styles = state.styles.map((style) => ({ ...style, ...patch.sourceControlWritingStyle }));
void screen.rerender(content());
});
return screen;
}
describe("mixed source control instructions", () => {
it("resets mixed template preferences without replacing each environment's instructions", async () => {
await using _screen = await setup();
const initialInstructions = state.styles.map(({ mode, customInstructions }) => ({
mode,
customInstructions,
}));
await userEvent.click(page.getByRole("button", { name: "Advanced" }));
// Mid-open, Playwright scrolls the clipped panel to reach the button, then the growing panel
// shifts it between press and release and the click misses. Wait until the panel shows it.
await expect.element(resetButton("change request templates")).toBeInViewport({ ratio: 1 });
await userEvent.click(resetButton("change request templates"));
expect(state.updateSettings).toHaveBeenCalledTimes(1);
expect(state.styles.map((style) => style.followChangeRequestTemplates)).toEqual([
DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle.followChangeRequestTemplates,
DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle.followChangeRequestTemplates,
]);
expect(
state.styles.map(({ mode, customInstructions }) => ({ mode, customInstructions })),
).toEqual(initialInstructions);
});
});