|
| 1 | +import { Reforge } from "../reforge"; |
| 2 | +import { ConfigType, ConfigValueType } from "../types"; |
| 3 | +import type { Config } from "../types"; |
| 4 | +import { projectEnvIdUnderTest, irrelevant } from "./testHelpers"; |
| 5 | + |
| 6 | +const createSimpleConfig = (key: string, value: string): Config => { |
| 7 | + return { |
| 8 | + id: "1", |
| 9 | + projectId: 1, |
| 10 | + key, |
| 11 | + changedBy: undefined, |
| 12 | + rows: [ |
| 13 | + { |
| 14 | + properties: {}, |
| 15 | + values: [ |
| 16 | + { |
| 17 | + criteria: [ |
| 18 | + { |
| 19 | + propertyName: "user.country", |
| 20 | + operator: "PROP_IS_ONE_OF" as any, |
| 21 | + valueToMatch: { |
| 22 | + stringList: { |
| 23 | + values: ["US"], |
| 24 | + }, |
| 25 | + }, |
| 26 | + }, |
| 27 | + ], |
| 28 | + value: { string: value }, |
| 29 | + }, |
| 30 | + { |
| 31 | + criteria: [], |
| 32 | + value: { string: "default" }, |
| 33 | + }, |
| 34 | + ], |
| 35 | + }, |
| 36 | + ], |
| 37 | + allowableValues: [], |
| 38 | + configType: ConfigType.Config, |
| 39 | + valueType: ConfigValueType.String, |
| 40 | + sendToClientSdk: false, |
| 41 | + }; |
| 42 | +}; |
| 43 | + |
| 44 | +describe("ReforgeClient", () => { |
| 45 | + describe("withContext", () => { |
| 46 | + it("returns a context-scoped client that doesn't expose internal resolver", () => { |
| 47 | + const reforge = new Reforge({ sdkKey: irrelevant }); |
| 48 | + const config = createSimpleConfig("test.key", "us-value"); |
| 49 | + reforge.setConfig([config], projectEnvIdUnderTest, new Map()); |
| 50 | + |
| 51 | + const scopedClient = reforge.withContext({ user: { country: "US" } }); |
| 52 | + |
| 53 | + // Should have the public API methods |
| 54 | + expect(typeof scopedClient.get).toBe("function"); |
| 55 | + expect(typeof scopedClient.isFeatureEnabled).toBe("function"); |
| 56 | + expect(typeof scopedClient.logger).toBe("function"); |
| 57 | + expect(typeof scopedClient.shouldLog).toBe("function"); |
| 58 | + expect(typeof scopedClient.getLogLevel).toBe("function"); |
| 59 | + expect(typeof scopedClient.withContext).toBe("function"); |
| 60 | + expect(typeof scopedClient.inContext).toBe("function"); |
| 61 | + expect(typeof scopedClient.updateIfStalerThan).toBe("function"); |
| 62 | + expect(typeof scopedClient.addConfigChangeListener).toBe("function"); |
| 63 | + |
| 64 | + // Should NOT expose internal resolver methods |
| 65 | + expect((scopedClient as any).raw).toBeUndefined(); |
| 66 | + expect((scopedClient as any).set).toBeUndefined(); |
| 67 | + expect((scopedClient as any).keys).toBeUndefined(); |
| 68 | + expect((scopedClient as any).cloneWithContext).toBeUndefined(); |
| 69 | + expect((scopedClient as any).update).toBeUndefined(); |
| 70 | + |
| 71 | + // Should apply context correctly |
| 72 | + expect(scopedClient.get("test.key")).toBe("us-value"); |
| 73 | + }); |
| 74 | + |
| 75 | + it("allows chaining withContext calls", () => { |
| 76 | + const reforge = new Reforge({ sdkKey: irrelevant }); |
| 77 | + const config = createSimpleConfig("test.key", "us-value"); |
| 78 | + reforge.setConfig([config], projectEnvIdUnderTest, new Map()); |
| 79 | + |
| 80 | + const client1 = reforge.withContext({ user: { country: "FR" } }); |
| 81 | + expect(client1.get("test.key")).toBe("default"); |
| 82 | + |
| 83 | + const client2 = client1.withContext({ user: { country: "US" } }); |
| 84 | + expect(client2.get("test.key")).toBe("us-value"); |
| 85 | + }); |
| 86 | + |
| 87 | + it("merges context when additional context is provided to methods", () => { |
| 88 | + const reforge = new Reforge({ sdkKey: irrelevant }); |
| 89 | + const config = createSimpleConfig("test.key", "us-value"); |
| 90 | + reforge.setConfig([config], projectEnvIdUnderTest, new Map()); |
| 91 | + |
| 92 | + const client = reforge.withContext({ user: { name: "Alice" } }); |
| 93 | + |
| 94 | + // Provide additional context that includes the country |
| 95 | + expect(client.get("test.key", { user: { country: "US" } })).toBe( |
| 96 | + "us-value" |
| 97 | + ); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe("inContext", () => { |
| 102 | + it("provides a context-scoped client to the callback", () => { |
| 103 | + const reforge = new Reforge({ sdkKey: irrelevant }); |
| 104 | + const config = createSimpleConfig("test.key", "us-value"); |
| 105 | + reforge.setConfig([config], projectEnvIdUnderTest, new Map()); |
| 106 | + |
| 107 | + const result = reforge.inContext( |
| 108 | + { user: { country: "US" } }, |
| 109 | + (client) => { |
| 110 | + // Should not expose internal methods |
| 111 | + expect((client as any).raw).toBeUndefined(); |
| 112 | + expect((client as any).set).toBeUndefined(); |
| 113 | + |
| 114 | + // Should apply context |
| 115 | + expect(client.get("test.key")).toBe("us-value"); |
| 116 | + |
| 117 | + return "success"; |
| 118 | + } |
| 119 | + ); |
| 120 | + |
| 121 | + expect(result).toBe("success"); |
| 122 | + }); |
| 123 | + |
| 124 | + it("allows nested inContext calls", () => { |
| 125 | + const reforge = new Reforge({ sdkKey: irrelevant }); |
| 126 | + const config = createSimpleConfig("test.key", "us-value"); |
| 127 | + reforge.setConfig([config], projectEnvIdUnderTest, new Map()); |
| 128 | + |
| 129 | + reforge.inContext({ user: { country: "FR" } }, (outer) => { |
| 130 | + expect(outer.get("test.key")).toBe("default"); |
| 131 | + |
| 132 | + outer.inContext({ user: { country: "US" } }, (inner) => { |
| 133 | + expect(inner.get("test.key")).toBe("us-value"); |
| 134 | + }); |
| 135 | + }); |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + describe("shared state", () => { |
| 140 | + it("shares telemetry with parent", () => { |
| 141 | + const reforge = new Reforge({ sdkKey: irrelevant }); |
| 142 | + reforge.setConfig([], projectEnvIdUnderTest, new Map()); |
| 143 | + |
| 144 | + const client = reforge.withContext({ user: { country: "US" } }); |
| 145 | + |
| 146 | + expect(client.telemetry).toBe(reforge.telemetry); |
| 147 | + }); |
| 148 | + |
| 149 | + it("delegates methods to parent reforge instance", () => { |
| 150 | + const reforge = new Reforge({ sdkKey: irrelevant }); |
| 151 | + reforge.setConfig([], projectEnvIdUnderTest, new Map()); |
| 152 | + |
| 153 | + const client = reforge.withContext({ user: { country: "US" } }); |
| 154 | + |
| 155 | + // Telemetry is shared |
| 156 | + expect(client.telemetry).toBe(reforge.telemetry); |
| 157 | + |
| 158 | + // getLogLevel delegates to parent |
| 159 | + expect(typeof client.getLogLevel).toBe("function"); |
| 160 | + }); |
| 161 | + }); |
| 162 | +}); |
0 commit comments