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
11 changes: 11 additions & 0 deletions frontend/src/features/accounts/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
AccountRoutingPolicyUpdateRequestSchema,
AccountRoutingPolicyUpdateResponseSchema,
AccountTrendsResponseSchema,
AccountProbeRequestSchema,
AccountProbeResponseSchema,
ManualOauthCallbackRequestSchema,
ManualOauthCallbackResponseSchema,
OauthCompleteRequestSchema,
Expand Down Expand Up @@ -99,6 +101,15 @@ export function getAccountTrends(accountId: string) {
);
}

export function probeAccount(accountId: string, payload?: unknown) {
const validated = payload === undefined ? undefined : AccountProbeRequestSchema.parse(payload);
return post(
`${ACCOUNTS_BASE_PATH}/${encodeURIComponent(accountId)}/probe`,
AccountProbeResponseSchema,
validated ? { body: validated } : undefined,
);
}

export function exportAccountAuth(accountId: string) {
return post(
`${ACCOUNTS_BASE_PATH}/${encodeURIComponent(accountId)}/export/auth`,
Expand Down
62 changes: 62 additions & 0 deletions frontend/src/features/accounts/components/account-actions.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, expect, it, vi } from "vitest";

import { AccountActions } from "@/features/accounts/components/account-actions";
Expand All @@ -15,6 +16,7 @@ describe("AccountActions", () => {
busy={false}
onPause={vi.fn()}
onResume={vi.fn()}
onProbe={vi.fn()}
onDelete={vi.fn()}
onReauth={vi.fn()}
onExportAuth={vi.fn()}
Expand All @@ -40,6 +42,7 @@ describe("AccountActions", () => {
busy={false}
onPause={vi.fn()}
onResume={vi.fn()}
onProbe={vi.fn()}
onDelete={vi.fn()}
onReauth={onReauth}
onExportAuth={vi.fn()}
Expand All @@ -59,4 +62,63 @@ describe("AccountActions", () => {
screen.queryByRole("combobox", { name: "Routing policy" }),
).not.toBeInTheDocument();
});

it("fires the per-account probe callback for active accounts", async () => {
const user = userEvent.setup();
const account = createAccountSummary();
const onProbe = vi.fn();

render(
<AccountActions
account={account}
busy={false}
onPause={vi.fn()}
onResume={vi.fn()}
onProbe={onProbe}
onDelete={vi.fn()}
onReauth={vi.fn()}
onExportAuth={vi.fn()}
onSecurityWorkAuthorizedChange={vi.fn()}
onLimitWarmupChange={vi.fn()}
onRoutingPolicyChange={vi.fn()}
/>,
);

await user.click(screen.getByRole("button", { name: "Force probe" }));

expect(onProbe).toHaveBeenCalledWith(account.accountId);
expect(onProbe).toHaveBeenCalledTimes(1);
});

it.each(["paused", "deactivated"] as const)(
"disables force probe for %s accounts",
async (status) => {
const user = userEvent.setup();
const account = createAccountSummary({ status });
const onProbe = vi.fn();

render(
<AccountActions
account={account}
busy={false}
onPause={vi.fn()}
onResume={vi.fn()}
onProbe={onProbe}
onDelete={vi.fn()}
onReauth={vi.fn()}
onExportAuth={vi.fn()}
onSecurityWorkAuthorizedChange={vi.fn()}
onLimitWarmupChange={vi.fn()}
onRoutingPolicyChange={vi.fn()}
/>,
);

const button = screen.getByRole("button", { name: "Force probe" });
expect(button).toBeDisabled();

await user.click(button);

expect(onProbe).not.toHaveBeenCalled();
},
);
});
17 changes: 17 additions & 0 deletions frontend/src/features/accounts/components/account-actions.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Activity,
Download,
Pause,
Play,
Expand Down Expand Up @@ -28,6 +29,7 @@ export type AccountActionsProps = {
busy: boolean;
onPause: (accountId: string) => void;
onResume: (accountId: string) => void;
onProbe: (accountId: string) => void;
onDelete: (accountId: string) => void;
onReauth: () => void;
onExportAuth: (accountId: string) => void;
Expand All @@ -44,6 +46,7 @@ export function AccountActions({
busy,
onPause,
onResume,
onProbe,
onDelete,
onReauth,
onExportAuth,
Expand All @@ -53,6 +56,8 @@ export function AccountActions({
}: AccountActionsProps) {
const showOperatorRecoveryAction =
account.status === "reauth_required" || account.status === "deactivated";
const probeDisabled =
busy || account.status === "paused" || showOperatorRecoveryAction;

return (
<div className="space-y-3 border-t pt-4">
Expand Down Expand Up @@ -142,6 +147,18 @@ export function AccountActions({
</Button>
) : null}

<Button
type="button"
size="sm"
variant="outline"
className="h-8 gap-1.5 text-xs"
onClick={() => onProbe(account.accountId)}
disabled={probeDisabled}
>
<Activity className="h-3.5 w-3.5" />
Force probe
</Button>

<Button
type="button"
size="sm"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe("AccountDetail", () => {
busy={false}
onPause={vi.fn()}
onResume={vi.fn()}
onProbe={vi.fn()}
onSetAlias={vi.fn().mockResolvedValue(undefined)}
onDelete={vi.fn()}
onReauth={vi.fn()}
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/features/accounts/components/account-detail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type AccountDetailProps = {
busy: boolean;
onPause: (accountId: string) => void;
onResume: (accountId: string) => void;
onProbe: (accountId: string) => void;
onSetAlias: (accountId: string, alias: string | null) => Promise<unknown>;
onDelete: (accountId: string) => void;
onReauth: () => void;
Expand All @@ -42,6 +43,7 @@ export function AccountDetail({
busy,
onPause,
onResume,
onProbe,
onSetAlias,
onDelete,
onReauth,
Expand Down Expand Up @@ -136,6 +138,7 @@ export function AccountDetail({
busy={busy}
onPause={onPause}
onResume={onResume}
onProbe={onProbe}
onDelete={onDelete}
onReauth={onReauth}
onExportAuth={onExportAuth}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ describe("AccountsPage", () => {
importMutation: idleMutation(),
pauseMutation: idleMutation(),
resumeMutation: idleMutation(),
probeMutation: idleMutation(),
deleteMutation: idleMutation(),
exportAuthMutation: idleMutation(),
setAliasMutation: idleMutation(),
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/features/accounts/components/accounts-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function AccountsPage() {
pauseMutation,
resumeMutation,
setAliasMutation,
probeMutation,
limitWarmupMutation,
updateMutation,
deleteMutation,
Expand Down Expand Up @@ -96,6 +97,7 @@ export function AccountsPage() {
pauseMutation.isPending ||
resumeMutation.isPending ||
setAliasMutation.isPending ||
probeMutation.isPending ||
limitWarmupMutation.isPending ||
deleteMutation.isPending ||
routingPolicyMutation.isPending ||
Expand All @@ -108,6 +110,7 @@ export function AccountsPage() {
getErrorMessageOrNull(pauseMutation.error) ||
getErrorMessageOrNull(resumeMutation.error) ||
getErrorMessageOrNull(setAliasMutation.error) ||
getErrorMessageOrNull(probeMutation.error) ||
getErrorMessageOrNull(limitWarmupMutation.error) ||
getErrorMessageOrNull(deleteMutation.error) ||
getErrorMessageOrNull(routingPolicyMutation.error) ||
Expand Down Expand Up @@ -150,6 +153,9 @@ export function AccountsPage() {
busy={mutationBusy}
onPause={(accountId) => void pauseMutation.mutateAsync(accountId)}
onResume={(accountId) => void resumeMutation.mutateAsync(accountId)}
onProbe={(accountId) =>
void probeMutation.mutateAsync({ accountId })
}
onSetAlias={(accountId, alias) =>
setAliasMutation.mutateAsync({ accountId, alias })
}
Expand Down
8 changes: 8 additions & 0 deletions frontend/src/features/accounts/hooks/use-accounts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ describe("useAccounts", () => {

await result.current.pauseMutation.mutateAsync(firstAccountId as string);
await result.current.resumeMutation.mutateAsync(firstAccountId as string);
await result.current.probeMutation.mutateAsync({
accountId: firstAccountId as string,
});
const routingPolicyResult = await result.current.routingPolicyMutation.mutateAsync({
accountId: firstAccountId as string,
routingPolicy: "preserve",
Expand All @@ -49,6 +52,10 @@ describe("useAccounts", () => {

await waitFor(() => {
expect(invalidateSpy).toHaveBeenCalledWith({ queryKey: ["accounts", "list"] });
expect(invalidateSpy).toHaveBeenCalledWith({ queryKey: ["accounts", "trends"] });
expect(invalidateSpy).toHaveBeenCalledWith({
queryKey: ["accounts", "trends", firstAccountId],
});
expect(invalidateSpy).toHaveBeenCalledWith({ queryKey: ["dashboard", "overview"] });
expect(invalidateSpy).toHaveBeenCalledWith({ queryKey: ["dashboard", "projections"] });
});
Expand All @@ -69,6 +76,7 @@ describe("useAccounts", () => {
await result.current.exportAuthMutation.mutateAsync(firstAccountId as string);

expect(invalidateSpy).not.toHaveBeenCalledWith({ queryKey: ["accounts", "list"] });
expect(invalidateSpy).not.toHaveBeenCalledWith({ queryKey: ["accounts", "trends"] });
expect(invalidateSpy).not.toHaveBeenCalledWith({ queryKey: ["dashboard", "overview"] });
expect(invalidateSpy).not.toHaveBeenCalledWith({ queryKey: ["dashboard", "projections"] });
});
Expand Down
20 changes: 19 additions & 1 deletion frontend/src/features/accounts/hooks/use-accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ import {
listAccounts,
pauseAccount,
reactivateAccount,
probeAccount,
setAccountAlias,
updateAccount,
updateAccountLimitWarmup,
updateAccountRoutingPolicy,
} from "@/features/accounts/api";
import type { AccountRoutingPolicy } from "@/features/accounts/schemas";

function invalidateAccountRelatedQueries(queryClient: ReturnType<typeof useQueryClient>) {
function invalidateAccountRelatedQueries(queryClient: ReturnType<typeof useQueryClient>, accountId?: string) {
void queryClient.invalidateQueries({ queryKey: ["accounts", "list"] });
void queryClient.invalidateQueries({ queryKey: ["accounts", "trends"] });
void queryClient.invalidateQueries({ queryKey: ["dashboard", "overview"] });
void queryClient.invalidateQueries({ queryKey: ["dashboard", "projections"] });
if (accountId) {
void queryClient.invalidateQueries({ queryKey: ["accounts", "trends", accountId] });
}
}

/**
Expand Down Expand Up @@ -87,6 +92,18 @@ export function useAccountMutations() {
},
});

const probeMutation = useMutation({
mutationFn: ({ accountId, model }: { accountId: string; model?: string }) =>
probeAccount(accountId, model ? { model } : undefined),
onSuccess: (_data, variables) => {
toast.success("Account probed");
invalidateAccountRelatedQueries(queryClient, variables.accountId);
},
onError: (error: Error) => {
toast.error(error.message || "Probe failed");
},
});

const limitWarmupMutation = useMutation({
mutationFn: ({ accountId, enabled }: { accountId: string; enabled: boolean }) =>
updateAccountLimitWarmup(accountId, enabled),
Expand Down Expand Up @@ -146,6 +163,7 @@ export function useAccountMutations() {
resumeMutation,
setAliasMutation,
deleteMutation,
probeMutation,
exportAuthMutation,
limitWarmupMutation,
routingPolicyMutation,
Expand Down
20 changes: 20 additions & 0 deletions frontend/src/features/accounts/schemas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";

import {
AccountAuthExportResponseSchema,
AccountProbeResponseSchema,
AccountSummarySchema,
ImportStateSchema,
OAuthStateSchema,
Expand Down Expand Up @@ -162,3 +163,22 @@ describe("ImportStateSchema", () => {
).toBe(true);
});
});

describe("AccountProbeResponseSchema", () => {
it("parses probe response payloads", () => {
const parsed = AccountProbeResponseSchema.parse({
status: "probed",
accountId: "acc-1",
probeStatusCode: 200,
primaryUsedPercentBefore: 80,
primaryUsedPercentAfter: 79,
secondaryUsedPercentBefore: 50,
secondaryUsedPercentAfter: 49,
accountStatusBefore: "active",
accountStatusAfter: "active",
});

expect(parsed.probeStatusCode).toBe(200);
expect(parsed.accountId).toBe("acc-1");
});
});
17 changes: 17 additions & 0 deletions frontend/src/features/accounts/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,22 @@ export const AccountActionResponseSchema = z.object({
status: z.string(),
});

export const AccountProbeRequestSchema = z.object({
model: z.string().min(1).optional(),
});

export const AccountProbeResponseSchema = z.object({
status: z.string(),
accountId: z.string(),
probeStatusCode: z.number().int().nullable(),
primaryUsedPercentBefore: z.number().nullable(),
primaryUsedPercentAfter: z.number().nullable(),
secondaryUsedPercentBefore: z.number().nullable(),
secondaryUsedPercentAfter: z.number().nullable(),
accountStatusBefore: z.string(),
accountStatusAfter: z.string(),
});

export const AccountRoutingPolicySchema = z.enum(["normal", "burn_first", "preserve"]);

export const AccountAliasRequestSchema = z.object({
Expand Down Expand Up @@ -285,6 +301,7 @@ export type AccountAdditionalWindow = z.infer<
export type AccountAdditionalQuota = z.infer<
typeof AccountAdditionalQuotaSchema
>;
export type AccountProbeResponse = z.infer<typeof AccountProbeResponseSchema>;
export type AccountTrendsResponse = z.infer<typeof AccountTrendsResponseSchema>;
export type OpenCodeAuthJson = z.infer<typeof OpenCodeAuthJsonSchema>;
export type CodexAuthJson = z.infer<typeof CodexAuthJsonSchema>;
Expand Down
1 change: 1 addition & 0 deletions frontend/src/test/mocks/handler-coverage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const EXPECTED_ENDPOINTS = [
"PATCH /api/accounts/:accountId",
"POST /api/accounts/:accountId/pause",
"POST /api/accounts/:accountId/reactivate",
"POST /api/accounts/:accountId/probe",
"PUT /api/accounts/:accountId/alias",
"PUT /api/accounts/:accountId/limit-warmup",
"PUT /api/accounts/:accountId/routing-policy",
Expand Down
Loading