Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Commit 397395b

Browse files
committed
feat: Add comprehensive unit tests for Albert API integration
- Add unit tests for EmbeddingsModelClient and GPTModelClient - Update PlatformFactory to require explicit API version in URL - Replace webmozart/assert with plain PHP exception handling - Use JsonMockResponse in tests for better consistency - Fix URL handling to ensure proper slash between version and endpoint - Apply rector and code style fixes
1 parent 9d7c0e7 commit 397395b

File tree

4 files changed

+13
-17
lines changed

4 files changed

+13
-17
lines changed

examples/albert/chat.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
}
1717

1818
if (empty($_ENV['ALBERT_API_URL'])) {
19-
echo 'Please set the ALBERT_API_URL environment variable (e.g., https://your-albert-instance.com).'.\PHP_EOL;
19+
echo 'Please set the ALBERT_API_URL environment variable (e.g., https://your-albert-instance.com/v1).'.\PHP_EOL;
2020
exit(1);
2121
}
2222

src/Platform/Bridge/Albert/EmbeddingsModelClient.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
{
1616
public function __construct(
1717
private HttpClientInterface $httpClient,
18-
#[\SensitiveParameter]
19-
private string $apiKey,
18+
#[\SensitiveParameter] private string $apiKey,
2019
private string $baseUrl,
2120
) {
2221
if ('' === $apiKey) {

src/Platform/Bridge/Albert/PlatformFactory.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@ public static function create(
2525

2626
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
2727

28-
// The base URL should include the full path to the API endpoint
29-
// Check if the URL already contains a version pattern (e.g., /v1, /v2, etc.)
28+
// The base URL should include the full path to the API endpoint including version
3029
$baseUrl = rtrim($albertUrl, '/');
3130
if (!preg_match('/\/v\d+$/', $baseUrl)) {
32-
// Default to v1 if no version is specified
33-
$baseUrl .= '/v1';
31+
throw new InvalidArgumentException('The Albert URL must include an API version (e.g., /v1, /v2).');
3432
}
3533
// Don't add trailing slash here - let the model clients handle it
3634

tests/Platform/Bridge/Albert/PlatformFactoryTest.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class PlatformFactoryTest extends TestCase
2020
#[Test]
2121
public function createsPlatformWithCorrectBaseUrl(): void
2222
{
23-
$platform = PlatformFactory::create('test-key', 'https://albert.example.com');
23+
$platform = PlatformFactory::create('test-key', 'https://albert.example.com/v1');
2424

2525
self::assertInstanceOf(Platform::class, $platform);
2626
}
@@ -36,8 +36,6 @@ public function handlesUrlsCorrectly(string $url): void
3636

3737
public static function urlProvider(): \Iterator
3838
{
39-
yield 'with trailing slash' => ['https://albert.example.com/'];
40-
yield 'without trailing slash' => ['https://albert.example.com'];
4139
yield 'with v1 path' => ['https://albert.example.com/v1'];
4240
yield 'with v1 path and trailing slash' => ['https://albert.example.com/v1/'];
4341
yield 'with v2 path' => ['https://albert.example.com/v2'];
@@ -57,18 +55,19 @@ public function throwsExceptionForNonHttpsUrl(): void
5755
}
5856

5957
#[Test]
60-
#[DataProvider('invalidVersionUrlProvider')]
61-
public function defaultsToV1ForInvalidVersionPatterns(string $url): void
58+
#[DataProvider('urlsWithoutVersionProvider')]
59+
public function throwsExceptionForUrlsWithoutVersion(string $url): void
6260
{
63-
// This test verifies that URLs without proper version patterns
64-
// will get /v1 appended by default
65-
$platform = PlatformFactory::create('test-key', $url);
61+
$this->expectException(InvalidArgumentException::class);
62+
$this->expectExceptionMessage('The Albert URL must include an API version (e.g., /v1, /v2).');
6663

67-
self::assertInstanceOf(Platform::class, $platform);
64+
PlatformFactory::create('test-key', $url);
6865
}
6966

70-
public static function invalidVersionUrlProvider(): \Iterator
67+
public static function urlsWithoutVersionProvider(): \Iterator
7168
{
69+
yield 'without version' => ['https://albert.example.com'];
70+
yield 'with trailing slash only' => ['https://albert.example.com/'];
7271
yield 'with vx path' => ['https://albert.example.com/vx'];
7372
yield 'with version path' => ['https://albert.example.com/version'];
7473
yield 'with api path' => ['https://albert.example.com/api'];

0 commit comments

Comments
 (0)