Skip to content

Commit 95e1e7b

Browse files
authored
fix: Add usage tracking to config method (#151)
1 parent a83c5fa commit 95e1e7b

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

pkgs/sdk/server-ai/src/LdAiClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public LdAiClient(ILaunchDarklyClient client)
4747
public ILdAiConfigTracker Config(string key, Context context, LdAiConfig defaultValue,
4848
IReadOnlyDictionary<string, object> variables = null)
4949
{
50+
_client.Track("$ld:ai:config:function:single", context, LdValue.Of(key), 1);
5051

5152
var result = _client.JsonVariation(key, context, defaultValue.ToLdValue());
5253

pkgs/sdk/server-ai/test/LdAiClientTest.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,53 @@ public void ReturnsDefaultConfigWhenGivenInvalidVariation()
4949
Assert.Equal(defaultConfig, tracker.Config);
5050
}
5151

52+
[Fact]
53+
public void ConfigMethodCallsTrackWithCorrectParameters()
54+
{
55+
var mockClient = new Mock<ILaunchDarklyClient>();
56+
var context = Context.New(ContextKind.Default, "user-key");
57+
var configKey = "test-config-key";
58+
59+
mockClient.Setup(c => c.JsonVariation(
60+
It.IsAny<string>(),
61+
It.IsAny<Context>(),
62+
It.IsAny<LdValue>()))
63+
.Returns(LdValue.ObjectFrom(new Dictionary<string, LdValue>
64+
{
65+
["_ldMeta"] = LdValue.ObjectFrom(new Dictionary<string, LdValue>
66+
{
67+
["enabled"] = LdValue.Of(true),
68+
["variationKey"] = LdValue.Of("test-variation"),
69+
["version"] = LdValue.Of(1)
70+
}),
71+
["model"] = LdValue.ObjectFrom(new Dictionary<string, LdValue>
72+
{
73+
["name"] = LdValue.Of("test-model")
74+
}),
75+
["provider"] = LdValue.ObjectFrom(new Dictionary<string, LdValue>
76+
{
77+
["name"] = LdValue.Of("test-provider")
78+
}),
79+
["messages"] = LdValue.ArrayOf()
80+
}));
81+
82+
var mockLogger = new Mock<ILogger>();
83+
mockClient.Setup(x => x.GetLogger()).Returns(mockLogger.Object);
84+
85+
var client = new LdAiClient(mockClient.Object);
86+
var defaultConfig = LdAiConfig.New().Build();
87+
88+
var tracker = client.Config(configKey, context, defaultConfig);
89+
90+
mockClient.Verify(c => c.Track(
91+
"$ld:ai:config:function:single",
92+
context,
93+
LdValue.Of(configKey),
94+
1), Times.Once);
95+
96+
Assert.NotNull(tracker);
97+
}
98+
5299
private const string MetaDisabledExplicitly = """
53100
{
54101
"_ldMeta": {"variationKey": "1", "enabled": false},

0 commit comments

Comments
 (0)