Skip to content

Commit 4c052b9

Browse files
test(audience-sdk): centralise ConstantsTests path and key fixtures as file-local consts
ConstantsTests had inline literals for the publishable-key fixtures the BaseUrl resolution tests pass through Constants.BaseUrl ("pk_imapik-test-abc", "pk_imapik-prod-abc"), the custom override URL ("https://api.dev.immutable.com"), and the package.json discovery-walk components ("src", "Packages", "Audience", "package.json", and the JSON field names "version" / "name"). Adds nine file-local consts at the top of the fixture (matching the PublishableKeyPrefixTests TestPrefixKey / NonTestKey precedent) and migrates eleven references across the BaseUrl tests, the package.json LibraryVersion / LibraryName tests, and the ReadPackageJson upward-walk helper. Per the user's "everything random goes in a constant" stance. Follow-up to SDK-272 (centralisation of duplicated literals).
1 parent 404d180 commit 4c052b9

1 file changed

Lines changed: 23 additions & 11 deletions

File tree

src/Packages/Audience/Tests/Runtime/ConstantsTests.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ namespace Immutable.Audience.Tests
66
[TestFixture]
77
internal class ConstantsTests
88
{
9+
// Publishable key fixtures used by the BaseUrl resolution tests.
10+
private const string TestPrefixKeyFixture = "pk_imapik-test-abc";
11+
private const string ProdPrefixKeyFixture = "pk_imapik-prod-abc";
12+
private const string CustomBaseUrlOverride = "https://api.dev.immutable.com";
13+
14+
// package.json upward-walk components and the JSON fields parsed back.
15+
private const string SrcDirectoryName = "src";
16+
private const string PackagesDirectoryName = "Packages";
17+
private const string AudiencePackageDirectoryName = "Audience";
18+
private const string PackageJsonFileName = "package.json";
19+
private const string PackageJsonVersionField = "version";
20+
private const string PackageJsonNameField = "name";
21+
922
// -----------------------------------------------------------------
1023
// BaseUrl resolution
1124
// -----------------------------------------------------------------
@@ -14,14 +27,14 @@ internal class ConstantsTests
1427
public void BaseUrl_TestKey_ResolvesToSandbox()
1528
{
1629
Assert.AreEqual(Constants.SandboxBaseUrl,
17-
Constants.BaseUrl("pk_imapik-test-abc"));
30+
Constants.BaseUrl(TestPrefixKeyFixture));
1831
}
1932

2033
[Test]
2134
public void BaseUrl_NonTestKey_ResolvesToProduction()
2235
{
2336
Assert.AreEqual(Constants.ProductionBaseUrl,
24-
Constants.BaseUrl("pk_imapik-prod-abc"));
37+
Constants.BaseUrl(ProdPrefixKeyFixture));
2538
}
2639

2740
[Test]
@@ -36,9 +49,8 @@ public void BaseUrl_Override_WinsOverKeyPrefix()
3649
{
3750
// Override wins even for a test-prefixed key that would
3851
// otherwise derive to Sandbox.
39-
const string custom = "https://api.dev.immutable.com";
40-
Assert.AreEqual(custom,
41-
Constants.BaseUrl("pk_imapik-test-abc", custom));
52+
Assert.AreEqual(CustomBaseUrlOverride,
53+
Constants.BaseUrl(TestPrefixKeyFixture, CustomBaseUrlOverride));
4254
}
4355

4456
[Test]
@@ -47,7 +59,7 @@ public void BaseUrl_EmptyOverride_FallsBackToKeyDerivation()
4759
// Empty-string override is treated as "no override" so the
4860
// key-prefix fallback still kicks in.
4961
Assert.AreEqual(Constants.SandboxBaseUrl,
50-
Constants.BaseUrl("pk_imapik-test-abc", ""));
62+
Constants.BaseUrl(TestPrefixKeyFixture, ""));
5163
}
5264

5365
// -----------------------------------------------------------------
@@ -63,7 +75,7 @@ public void LibraryVersion_MatchesPackageJson()
6375
var packageJson = ReadPackageJson();
6476
var parsed = JsonReader.DeserializeObject(packageJson);
6577

66-
Assert.IsTrue(parsed.TryGetValue("version", out var versionObj),
78+
Assert.IsTrue(parsed.TryGetValue(PackageJsonVersionField, out var versionObj),
6779
"package.json is missing a \"version\" field");
6880
Assert.AreEqual(Constants.LibraryVersion, versionObj,
6981
"Constants.LibraryVersion must match package.json version");
@@ -77,7 +89,7 @@ public void LibraryName_MatchesPackageJson()
7789
var packageJson = ReadPackageJson();
7890
var parsed = JsonReader.DeserializeObject(packageJson);
7991

80-
Assert.IsTrue(parsed.TryGetValue("name", out var nameObj),
92+
Assert.IsTrue(parsed.TryGetValue(PackageJsonNameField, out var nameObj),
8193
"package.json is missing a \"name\" field");
8294
Assert.AreEqual(Constants.LibraryName, nameObj,
8395
"Constants.LibraryName must match package.json name");
@@ -95,14 +107,14 @@ private static string ReadPackageJson()
95107
var current = new DirectoryInfo(TestContext.CurrentContext.TestDirectory);
96108
while (current != null)
97109
{
98-
var candidate = Path.Combine(current.FullName, "src", "Packages", "Audience", "package.json");
110+
var candidate = Path.Combine(current.FullName, SrcDirectoryName, PackagesDirectoryName, AudiencePackageDirectoryName, PackageJsonFileName);
99111
if (File.Exists(candidate)) return File.ReadAllText(candidate);
100112

101113
// Also try the direct-inside case (package root itself is
102114
// the ancestor), which handles consuming-project layouts
103115
// that embed the package without the src/Packages prefix.
104-
var direct = Path.Combine(current.FullName, "package.json");
105-
if (File.Exists(direct) && current.Name == "Audience") return File.ReadAllText(direct);
116+
var direct = Path.Combine(current.FullName, PackageJsonFileName);
117+
if (File.Exists(direct) && current.Name == AudiencePackageDirectoryName) return File.ReadAllText(direct);
106118

107119
current = current.Parent;
108120
}

0 commit comments

Comments
 (0)