[Fix] UI - Policies: Type accuracy and mutation guard for usePolicyVersions

- Make PolicyVersionsResponse.versions optional (Policy[] | undefined)
  to match real API shape — select fallback handles normalization
- Add policyName guard to useUpdatePolicyVersionStatus mutationFn
  to fail loudly instead of silently skipping cache invalidation
- Add test for null policyName in updateStatus mutation

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
yuneng-jiang 2026-03-20 23:27:02 -07:00
parent 8aa1ebfb07
commit 80e55804af
2 changed files with 14 additions and 3 deletions

View file

@ -304,4 +304,15 @@ describe("useUpdatePolicyVersionStatus", () => {
"Failed to promote to production: Not found"
);
});
it("throws when policyName is null", async () => {
const { result } = renderHook(
() => useUpdatePolicyVersionStatus(null),
{ wrapper }
);
await expect(
result.current.mutateAsync({ policyId: "v2", status: "published" })
).rejects.toThrow("Missing access token or policy name");
});
});

View file

@ -17,7 +17,7 @@ export const policyVersionKeys = createQueryKeys("policyVersions");
export interface PolicyVersionsResponse {
policy_name: string;
versions: Policy[];
versions: Policy[] | undefined;
total_count: number;
}
@ -95,8 +95,8 @@ export const useUpdatePolicyVersionStatus = (
{ policyId: string; status: "published" | "production" }
>({
mutationFn: async ({ policyId, status }) => {
if (!accessToken) {
throw new Error("Missing access token");
if (!accessToken || !policyName) {
throw new Error("Missing access token or policy name");
}
return await updatePolicyVersionStatus(accessToken, policyId, status);
},