|
| 1 | +package io.codemodder.plugins.llm; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.mockito.Mockito.mock; |
| 5 | +import static org.mockito.Mockito.when; |
| 6 | +import static org.mockito.Mockito.withSettings; |
| 7 | + |
| 8 | +import java.util.Map; |
| 9 | +import org.junit.jupiter.api.BeforeAll; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.junit.jupiter.api.TestInstance; |
| 12 | +import org.junit.jupiter.params.ParameterizedTest; |
| 13 | +import org.junit.jupiter.params.provider.EnumSource; |
| 14 | + |
| 15 | +/** Unit tests for {@link EnvironmentBasedModelMapper}. */ |
| 16 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 17 | +final class EnvironmentBasedModelMapperTest { |
| 18 | + |
| 19 | + private EnvironmentBasedModelMapper mapper; |
| 20 | + |
| 21 | + @BeforeAll |
| 22 | + void before() { |
| 23 | + final var environment = |
| 24 | + Map.of( |
| 25 | + "CODEMODDER_AZURE_OPENAI_GPT_3_5_TURBO_0125_DEPLOYMENT", |
| 26 | + "my-gpt-3.5-turbo", |
| 27 | + "CODEMODDER_AZURE_OPENAI_GPT_4_0613_DEPLOYMENT", |
| 28 | + "my-gpt-4", |
| 29 | + "CODEMODDER_AZURE_OPENAI_GPT_4_TURBO_2024_04_09_DEPLOYMENT", |
| 30 | + "my-gpt-4-turbo", |
| 31 | + "CODEMODDER_AZURE_OPENAI_GPT_4O_2024_05_13_DEPLOYMENT", |
| 32 | + "my-gpt-4o"); |
| 33 | + mapper = new EnvironmentBasedModelMapper(environment); |
| 34 | + } |
| 35 | + |
| 36 | + /** Spot checks one of the standard models to make sure the mapping works as expected */ |
| 37 | + @Test |
| 38 | + void it_maps_model_name_to_deployment() { |
| 39 | + final var name = mapper.getModelName(StandardModel.GPT_3_5_TURBO_0125); |
| 40 | + assertThat(name).isEqualTo("my-gpt-3.5-turbo"); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * This is a meta-test that fails when we add a new standard model but forget to update the |
| 45 | + * mapping in {@link #before()} to ensure that all standard models are covered. |
| 46 | + */ |
| 47 | + @EnumSource(StandardModel.class) |
| 48 | + @ParameterizedTest |
| 49 | + void it_looks_up_all_standard_models(final Model model) { |
| 50 | + final var name = mapper.getModelName(model); |
| 51 | + assertThat(name).isNotEqualTo(model.id()).startsWith("my-gpt"); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void it_returns_model_id_when_no_mapping_exists() { |
| 56 | + // GIVEN some model that doesn't have a mapping |
| 57 | + final var model = mock(Model.class, withSettings().stubOnly()); |
| 58 | + when(model.id()).thenReturn("test"); |
| 59 | + final var name = mapper.getModelName(model); |
| 60 | + assertThat(name).isEqualTo(model.id()); |
| 61 | + } |
| 62 | +} |
0 commit comments