deer-flow/frontend/tests/unit/components/workspace/settings/user-settings-page.dom.test.tsx
hataa c89fed0be4
fix(frontend): static-website gate and deep-link fallback for user management (#6641 follow-ups) (#6691)
* fix(frontend): static-website gate and deep-link fallback for user management (#6641 follow-ups)

Close the two leftovers from the #6641 review:

- user-settings-page now gates on isStaticWebsiteOnly() like model management
  (the static demo user is nominally an admin, but there is no Gateway to
  query), and the users adminOnly copy gains the same 'unavailable in demos'
  sentence the models copy already carries.
- the settings dialog derives the active section from the nav actually offered
  to the current user, so a ?settings=users deep link (or a stale dialog
  store) for a non-admin falls back to the first section instead of rendering
  an orphan panel with no highlighted entry.

Tests: static demo loads nothing (mutation-verified red), non-admin deep link
falls back to models, admin deep link lands on user management; full unit
suite green.

* test(frontend): pin the static-on x admin matrix cell at the dialog level

Dialog-level coverage for the real demo path (review follow-up on #6691):
admin + isStaticWebsiteOnly() + ?settings=users keeps the Users nav entry
(the splice keys on the role only) while the page-level gate shows the
admin-only note and issues no query. Mutation-verified: adding the mode
flag to the nav splice turns this test red and only this test.
2026-10-12 09:37:15 +08:00

221 lines
6.4 KiB
TypeScript

import { afterEach, beforeEach, expect, test, rs } from "@rstest/core";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import {
cleanup,
fireEvent,
render,
screen,
waitFor,
within,
} from "@testing-library/react";
import { UserSettingsPage } from "@/components/workspace/settings/user-settings-page";
import { GatewayApiError } from "@/core/api/errors";
import type * as AdminUsers from "@/core/auth/admin-users";
import {
loadAdminUsers,
updateUserAccount,
type AdminUser,
} from "@/core/auth/admin-users";
import { useAuth } from "@/core/auth/AuthProvider";
import { enUS } from "@/core/i18n/locales/en-US";
import { isStaticWebsiteOnly } from "@/core/static-mode";
rs.mock("@/core/auth/AuthProvider", () => ({ useAuth: rs.fn() }));
rs.mock("@/core/i18n/hooks", () => ({ useI18n: () => ({ t: enUS }) }));
rs.mock("@/core/auth/admin-users", () => ({
...(rs.requireActual<typeof AdminUsers>("@/core/auth/admin-users") as object),
loadAdminUsers: rs.fn(),
updateUserAccount: rs.fn(),
}));
rs.mock("@/core/static-mode", () => ({ isStaticWebsiteOnly: rs.fn() }));
const admin: AdminUser = {
id: "u-admin",
email: "admin@example.com",
system_role: "admin",
needs_setup: false,
disabled: false,
oauth_provider: null,
};
const suspended: AdminUser = {
id: "u-user",
email: "user@example.com",
system_role: "user",
needs_setup: false,
disabled: true,
oauth_provider: null,
};
// Pre-gap-3 backends omit `disabled` entirely — absent means not disabled.
const legacyRow: AdminUser = {
id: "u-legacy",
email: "legacy@example.com",
system_role: "user",
needs_setup: false,
oauth_provider: null,
};
const auth = (role: "admin" | "user") =>
({ user: { id: "u-admin", system_role: role } }) as ReturnType<
typeof useAuth
>;
function rowFor(email: string) {
const row = screen.getByText(email).closest("li");
expect(row).not.toBeNull();
return row as HTMLElement;
}
beforeEach(() => {
rs.mocked(useAuth).mockReturnValue(auth("admin"));
// Seed the static-mode gate per test so a `true` override cannot leak into
// the next case via a surviving mock implementation.
rs.mocked(isStaticWebsiteOnly).mockReturnValue(false);
rs.mocked(loadAdminUsers).mockResolvedValue([admin, suspended, legacyRow]);
});
afterEach(() => {
cleanup();
rs.clearAllMocks();
});
function mount() {
const client = new QueryClient({
defaultOptions: { queries: { retry: false } },
});
render(
<QueryClientProvider client={client}>
<UserSettingsPage />
</QueryClientProvider>,
);
return client;
}
test("non-admin sees the admin-only note and loads nothing", () => {
rs.mocked(useAuth).mockReturnValue(auth("user"));
mount();
expect(screen.getByText(enUS.settings.users.adminOnly)).toBeTruthy();
expect(loadAdminUsers).not.toHaveBeenCalled();
});
test("static-website demo shows the admin-only note and loads nothing", () => {
// The static demo user is nominally an admin; only the mode gate stops
// the doomed /api/v1/admin/users query.
rs.mocked(isStaticWebsiteOnly).mockReturnValue(true);
mount();
expect(screen.getByText(enUS.settings.users.adminOnly)).toBeTruthy();
expect(loadAdminUsers).not.toHaveBeenCalled();
});
test("lists email, read-only role, and disabled state per account", async () => {
mount();
await screen.findByText("admin@example.com");
const activeRow = rowFor("admin@example.com");
expect(
within(activeRow).getByText(`Role: admin · ${enUS.settings.users.active}`),
).toBeTruthy();
const disabledRow = rowFor("user@example.com");
expect(
within(disabledRow).getByText(
`Role: user · ${enUS.settings.users.disabled}`,
),
).toBeTruthy();
const legacy = rowFor("legacy@example.com");
expect(
within(legacy).getByText(`Role: user · ${enUS.settings.users.active}`),
).toBeTruthy();
});
test("disable toggle PATCHes only the disabled field and refreshes", async () => {
mount();
rs.mocked(updateUserAccount).mockResolvedValue({ ...admin, disabled: true });
await screen.findByText("admin@example.com");
fireEvent.click(
within(rowFor("admin@example.com")).getByRole("button", {
name: enUS.settings.users.disable,
}),
);
await waitFor(() =>
expect(updateUserAccount).toHaveBeenCalledWith("u-admin", {
disabled: true,
}),
);
// The invalidation refires the active list query, so the row's status
// flips without a manual reload.
await waitFor(() => expect(loadAdminUsers).toHaveBeenCalledTimes(2));
});
test("enable toggle targets the suspended account", async () => {
mount();
rs.mocked(updateUserAccount).mockResolvedValue({
...suspended,
disabled: false,
});
await screen.findByText("user@example.com");
fireEvent.click(
within(rowFor("user@example.com")).getByRole("button", {
name: enUS.settings.users.enable,
}),
);
await waitFor(() =>
expect(updateUserAccount).toHaveBeenCalledWith("u-user", {
disabled: false,
}),
);
});
test("a 409 rejection surfaces the gateway detail verbatim", async () => {
mount();
rs.mocked(updateUserAccount).mockRejectedValueOnce(
new GatewayApiError({
message: "cannot disable the last remaining active admin",
status: 409,
code: null,
params: {},
rawMessage: "",
}),
);
await screen.findByText("admin@example.com");
fireEvent.click(
within(rowFor("admin@example.com")).getByRole("button", {
name: enUS.settings.users.disable,
}),
);
expect(
await screen.findByText("cannot disable the last remaining active admin"),
).toBeTruthy();
});
test("a non-gateway failure falls back to the localized copy", async () => {
mount();
rs.mocked(updateUserAccount).mockRejectedValueOnce(new Error("boom"));
await screen.findByText("admin@example.com");
fireEvent.click(
within(rowFor("admin@example.com")).getByRole("button", {
name: enUS.settings.users.disable,
}),
);
expect(
await screen.findByText(enUS.settings.users.updateFailed),
).toBeTruthy();
expect(screen.queryByText("boom")).toBeNull();
});
test("a failed load shows one alert with the localized copy", async () => {
// mockRejectedValueOnce outranks the beforeEach default for the mount query.
rs.mocked(loadAdminUsers).mockRejectedValueOnce(new Error("gateway down"));
mount();
expect(await screen.findByRole("alert")).toBeTruthy();
expect(screen.getByText(enUS.settings.users.failed)).toBeTruthy();
expect(screen.queryByText("gateway down")).toBeNull();
});