|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +package org.mozilla.fenix.components.llm.ext |
| 6 | + |
| 7 | +import kotlinx.coroutines.test.runTest |
| 8 | +import mozilla.components.lib.llm.mlpa.MlpaTokenProvider |
| 9 | +import mozilla.components.lib.llm.mlpa.service.AuthorizationToken |
| 10 | +import org.junit.Assert.assertEquals |
| 11 | +import org.junit.Assert.assertSame |
| 12 | +import org.junit.Assert.assertTrue |
| 13 | +import org.junit.Assert.fail |
| 14 | +import org.junit.Test |
| 15 | + |
| 16 | +class MlpaTokenProviderTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + fun `GIVEN the first provider succeeds WHEN fetching THEN its token is returned`() = runTest { |
| 20 | + val expected = AuthorizationToken.Integrity("first") |
| 21 | + val provider = MlpaTokenProvider.choose( |
| 22 | + { Result.success(expected) }, |
| 23 | + { Result.failure(IllegalStateException("integrity failed")) }, |
| 24 | + ) |
| 25 | + |
| 26 | + assertEquals(expected, provider.fetchToken().getOrThrow() as AuthorizationToken.Integrity) |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + fun `GIVEN the first provider fails WHEN fetching THEN the next provider's token is returned`() = runTest { |
| 31 | + val expected = AuthorizationToken.Integrity("second") |
| 32 | + val provider = MlpaTokenProvider.choose( |
| 33 | + { Result.failure(FxaMissingAccessToken()) }, |
| 34 | + { Result.success(expected) }, |
| 35 | + ) |
| 36 | + |
| 37 | + assertEquals(expected, provider.fetchToken().getOrThrow()) |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + fun `GIVEN all providers fail WHEN fetching THEN the last provider's failure is propagated`() = runTest { |
| 42 | + val terminal = IllegalStateException("integrity failed") |
| 43 | + val provider = MlpaTokenProvider.choose( |
| 44 | + { Result.failure(FxaMissingAccessToken()) }, |
| 45 | + { Result.failure(terminal) }, |
| 46 | + ) |
| 47 | + |
| 48 | + val result = provider.fetchToken() |
| 49 | + |
| 50 | + assertTrue(result.isFailure) |
| 51 | + assertSame(terminal, result.exceptionOrNull()) |
| 52 | + } |
| 53 | +} |
0 commit comments