|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import { Effect } from "effect"; |
| 3 | + |
| 4 | +import { GOOGLE_OAUTH_REVIEW_FLAG, makeGoogleOAuthListing } from "./google-oauth-listing"; |
| 5 | + |
| 6 | +const context = { userId: "review-user", organizationId: "review-org" }; |
| 7 | + |
| 8 | +describe("Google OAuth review listing", () => { |
| 9 | + it.effect("targets the authenticated user and re-evaluates after revocation", () => |
| 10 | + Effect.gen(function* () { |
| 11 | + let enabled = true; |
| 12 | + const policy = makeGoogleOAuthListing({ |
| 13 | + projectKey: "public-project-key", |
| 14 | + host: "https://flags.example.com", |
| 15 | + fetch: async (url, init) => { |
| 16 | + expect(url).toBe("https://flags.example.com/flags?v=2"); |
| 17 | + expect(await new Response(String(init?.body)).json()).toMatchObject({ |
| 18 | + distinct_id: context.userId, |
| 19 | + person_properties: { executor_user_id: context.userId }, |
| 20 | + flag_keys_to_evaluate: [GOOGLE_OAUTH_REVIEW_FLAG], |
| 21 | + }); |
| 22 | + expect(init?.signal).toBeInstanceOf(AbortSignal); |
| 23 | + return Response.json({ flags: { [GOOGLE_OAUTH_REVIEW_FLAG]: { enabled } } }); |
| 24 | + }, |
| 25 | + }); |
| 26 | + expect(yield* policy(context)).toBe(true); |
| 27 | + enabled = false; |
| 28 | + expect(yield* policy(context)).toBe(false); |
| 29 | + }), |
| 30 | + ); |
| 31 | + |
| 32 | + for (const body of [ |
| 33 | + { flags: {} }, |
| 34 | + { flags: { [GOOGLE_OAUTH_REVIEW_FLAG]: { enabled: "true" } } }, |
| 35 | + {}, |
| 36 | + ]) { |
| 37 | + it.effect(`withholds an absent or invalid flag: ${JSON.stringify(body)}`, () => |
| 38 | + Effect.gen(function* () { |
| 39 | + const policy = makeGoogleOAuthListing({ |
| 40 | + projectKey: "key", |
| 41 | + host: "https://flags.example.com", |
| 42 | + fetch: async () => Response.json(body), |
| 43 | + }); |
| 44 | + expect(yield* policy(context)).toBe(false); |
| 45 | + }), |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + it.effect("withholds on HTTP and transport failures", () => |
| 50 | + Effect.gen(function* () { |
| 51 | + const unavailable = makeGoogleOAuthListing({ |
| 52 | + projectKey: "key", |
| 53 | + host: "https://flags.example.com", |
| 54 | + fetch: async () => new Response(null, { status: 503 }), |
| 55 | + }); |
| 56 | + const failed = makeGoogleOAuthListing({ |
| 57 | + projectKey: "key", |
| 58 | + host: "https://flags.example.com", |
| 59 | + fetch: () => |
| 60 | + Effect.runPromise(Effect.fail(new DOMException("Network unavailable", "NetworkError"))), |
| 61 | + }); |
| 62 | + expect(yield* unavailable(context)).toBe(false); |
| 63 | + expect(yield* failed(context)).toBe(false); |
| 64 | + }), |
| 65 | + ); |
| 66 | + |
| 67 | + it.effect("does not evaluate without a user or project key", () => |
| 68 | + Effect.gen(function* () { |
| 69 | + let requests = 0; |
| 70 | + const fetch: typeof globalThis.fetch = async () => { |
| 71 | + requests += 1; |
| 72 | + return Response.json({ flags: {} }); |
| 73 | + }; |
| 74 | + const configured = makeGoogleOAuthListing({ |
| 75 | + projectKey: "key", |
| 76 | + host: "https://flags.example.com", |
| 77 | + fetch, |
| 78 | + }); |
| 79 | + const unconfigured = makeGoogleOAuthListing({ |
| 80 | + projectKey: undefined, |
| 81 | + host: "https://flags.example.com", |
| 82 | + fetch, |
| 83 | + }); |
| 84 | + expect(yield* configured({ ...context, userId: null })).toBe(false); |
| 85 | + expect(yield* unconfigured(context)).toBe(false); |
| 86 | + expect(requests).toBe(0); |
| 87 | + }), |
| 88 | + ); |
| 89 | +}); |
0 commit comments