|
| 1 | +import { describe, expect, it, vi, beforeEach } from "vitest"; |
| 2 | +import { renderHook, act } from "@testing-library/react"; |
| 3 | + |
| 4 | +// ── Mocks ────────────────────────────────────────────────────────────── |
| 5 | +let convexAuth = { isAuthenticated: true, isLoading: false }; |
| 6 | +let queryReturn: unknown = undefined; |
| 7 | +let hostedMode = true; |
| 8 | +const upsertAction = vi.fn(async () => ({ id: "app_new" })); |
| 9 | +const removeAction = vi.fn(async () => undefined); |
| 10 | +let lastQueryArgs: unknown; |
| 11 | + |
| 12 | +vi.mock("convex/react", () => ({ |
| 13 | + useConvexAuth: () => convexAuth, |
| 14 | + useQuery: (_name: unknown, args: unknown) => { |
| 15 | + lastQueryArgs = args; |
| 16 | + return args === "skip" ? undefined : queryReturn; |
| 17 | + }, |
| 18 | + useAction: (name: unknown) => |
| 19 | + name === "xaaResourceApps:upsert" ? upsertAction : removeAction, |
| 20 | +})); |
| 21 | + |
| 22 | +vi.mock("@/lib/config", () => ({ |
| 23 | + get HOSTED_MODE() { |
| 24 | + return hostedMode; |
| 25 | + }, |
| 26 | +})); |
| 27 | + |
| 28 | +// Import AFTER mocks are set up. |
| 29 | +import { useXaaResourceApps } from "../useXaaResourceApps"; |
| 30 | + |
| 31 | +const ORG_ID = "org_test"; |
| 32 | + |
| 33 | +const VALID_ROW = { |
| 34 | + id: "app_1", |
| 35 | + name: "My Resource", |
| 36 | + resourceType: "mcp", |
| 37 | + resourceUrl: "https://resource.example.com/mcp", |
| 38 | + authServerMode: "own", |
| 39 | + tokenEndpoint: "https://as.example.com/oauth/token", |
| 40 | + hasSecret: true, |
| 41 | + createdAt: 100, |
| 42 | + updatedAt: 200, |
| 43 | +}; |
| 44 | + |
| 45 | +describe("useXaaResourceApps", () => { |
| 46 | + beforeEach(() => { |
| 47 | + convexAuth = { isAuthenticated: true, isLoading: false }; |
| 48 | + queryReturn = undefined; |
| 49 | + hostedMode = true; |
| 50 | + lastQueryArgs = undefined; |
| 51 | + upsertAction.mockClear(); |
| 52 | + removeAction.mockClear(); |
| 53 | + }); |
| 54 | + |
| 55 | + describe("auth + hosted gate", () => { |
| 56 | + it("fetches and reports isAuthenticated when authenticated, org-scoped, hosted", () => { |
| 57 | + queryReturn = { resourceApps: [VALID_ROW] }; |
| 58 | + const { result } = renderHook(() => useXaaResourceApps(ORG_ID)); |
| 59 | + expect(result.current.isAuthenticated).toBe(true); |
| 60 | + expect(lastQueryArgs).toEqual({ organizationId: ORG_ID }); |
| 61 | + expect(result.current.resourceApps).toHaveLength(1); |
| 62 | + }); |
| 63 | + |
| 64 | + it("skips the query and reports isAuthenticated=false when not hosted", () => { |
| 65 | + hostedMode = false; |
| 66 | + const { result } = renderHook(() => useXaaResourceApps(ORG_ID)); |
| 67 | + expect(result.current.isAuthenticated).toBe(false); |
| 68 | + expect(lastQueryArgs).toBe("skip"); |
| 69 | + expect(result.current.isLoading).toBe(false); |
| 70 | + }); |
| 71 | + |
| 72 | + it("skips when unauthenticated", () => { |
| 73 | + convexAuth = { isAuthenticated: false, isLoading: false }; |
| 74 | + const { result } = renderHook(() => useXaaResourceApps(ORG_ID)); |
| 75 | + expect(result.current.isAuthenticated).toBe(false); |
| 76 | + expect(lastQueryArgs).toBe("skip"); |
| 77 | + }); |
| 78 | + |
| 79 | + it("skips when no organization is selected", () => { |
| 80 | + const { result } = renderHook(() => useXaaResourceApps(null)); |
| 81 | + expect(result.current.isAuthenticated).toBe(false); |
| 82 | + expect(lastQueryArgs).toBe("skip"); |
| 83 | + }); |
| 84 | + |
| 85 | + it("reports isLoading while the gated query is in flight", () => { |
| 86 | + queryReturn = undefined; // query returns undefined => still loading |
| 87 | + const { result } = renderHook(() => useXaaResourceApps(ORG_ID)); |
| 88 | + expect(result.current.isLoading).toBe(true); |
| 89 | + }); |
| 90 | + |
| 91 | + it("reports isLoading during the auth-bootstrap window (hosted)", () => { |
| 92 | + convexAuth = { isAuthenticated: false, isLoading: true }; |
| 93 | + const { result } = renderHook(() => useXaaResourceApps(ORG_ID)); |
| 94 | + expect(result.current.isLoading).toBe(true); |
| 95 | + expect(result.current.isAuthenticated).toBe(false); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe("normalize", () => { |
| 100 | + it("accepts a bare array as well as a wrapped envelope", () => { |
| 101 | + queryReturn = [VALID_ROW]; |
| 102 | + const { result } = renderHook(() => useXaaResourceApps(ORG_ID)); |
| 103 | + expect(result.current.resourceApps).toHaveLength(1); |
| 104 | + expect(result.current.resourceApps[0]).toMatchObject({ |
| 105 | + id: "app_1", |
| 106 | + resourceType: "mcp", |
| 107 | + authServerMode: "own", |
| 108 | + hasSecret: true, |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + it("drops malformed rows (bad enum / missing id)", () => { |
| 113 | + queryReturn = { |
| 114 | + resourceApps: [ |
| 115 | + VALID_ROW, |
| 116 | + { ...VALID_ROW, id: "app_2", resourceType: "grpc" }, |
| 117 | + { ...VALID_ROW, id: undefined }, |
| 118 | + ], |
| 119 | + }; |
| 120 | + const { result } = renderHook(() => useXaaResourceApps(ORG_ID)); |
| 121 | + expect(result.current.resourceApps).toHaveLength(1); |
| 122 | + expect(result.current.resourceApps[0].id).toBe("app_1"); |
| 123 | + }); |
| 124 | + |
| 125 | + it("never surfaces a secret value even if the wire carried one", () => { |
| 126 | + queryReturn = { |
| 127 | + resourceApps: [{ ...VALID_ROW, secret: "leaked", vaultObjectId: "v" }], |
| 128 | + }; |
| 129 | + const { result } = renderHook(() => useXaaResourceApps(ORG_ID)); |
| 130 | + const app = result.current.resourceApps[0] as Record<string, unknown>; |
| 131 | + expect(app).not.toHaveProperty("secret"); |
| 132 | + expect(app).not.toHaveProperty("vaultObjectId"); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + describe("mutations", () => { |
| 137 | + it("upsert forwards organizationId and the input", async () => { |
| 138 | + const { result } = renderHook(() => useXaaResourceApps(ORG_ID)); |
| 139 | + await act(async () => { |
| 140 | + await result.current.upsert({ |
| 141 | + name: "New", |
| 142 | + resourceType: "rest", |
| 143 | + resourceUrl: "https://r.example.com/api", |
| 144 | + authServerMode: "mcpjam", |
| 145 | + }); |
| 146 | + }); |
| 147 | + expect(upsertAction).toHaveBeenCalledWith( |
| 148 | + expect.objectContaining({ |
| 149 | + organizationId: ORG_ID, |
| 150 | + name: "New", |
| 151 | + authServerMode: "mcpjam", |
| 152 | + }), |
| 153 | + ); |
| 154 | + }); |
| 155 | + |
| 156 | + it("remove forwards id + organizationId", async () => { |
| 157 | + const { result } = renderHook(() => useXaaResourceApps(ORG_ID)); |
| 158 | + await act(async () => { |
| 159 | + await result.current.remove("app_1"); |
| 160 | + }); |
| 161 | + expect(removeAction).toHaveBeenCalledWith({ |
| 162 | + id: "app_1", |
| 163 | + organizationId: ORG_ID, |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + it("captures the error message when a mutation throws", async () => { |
| 168 | + upsertAction.mockRejectedValueOnce(new Error("boom")); |
| 169 | + const { result } = renderHook(() => useXaaResourceApps(ORG_ID)); |
| 170 | + await act(async () => { |
| 171 | + await expect( |
| 172 | + result.current.upsert({ |
| 173 | + name: "x", |
| 174 | + resourceType: "rest", |
| 175 | + resourceUrl: "https://r.example.com/api", |
| 176 | + authServerMode: "mcpjam", |
| 177 | + }), |
| 178 | + ).rejects.toThrow("boom"); |
| 179 | + }); |
| 180 | + expect(result.current.error).toBe("boom"); |
| 181 | + }); |
| 182 | + }); |
| 183 | +}); |
0 commit comments