Adds some tests, cleanup a bit

This commit is contained in:
Guillaume Tâche
2024-09-20 08:36:52 +02:00
parent 17086a87ef
commit 703a4c71ae
47 changed files with 1122 additions and 182 deletions

View File

@@ -10,7 +10,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
class TestWhisperExtractionModelProvider {
private final ExtractionModelProvider provider = new WhisperExtractionModelProvider();
private final ExtractionModelProvider provider;
TestWhisperExtractionModelProvider() {
this.provider = new WhisperExtractionModelProvider();
}
@Test
void testGetAvailableExtractionModels() {

View File

@@ -4,6 +4,7 @@ import com.github.gtache.autosubtitle.impl.OS;
import com.github.gtache.autosubtitle.process.ProcessRunner;
import com.github.gtache.autosubtitle.setup.SetupException;
import com.github.gtache.autosubtitle.setup.conda.CondaSetupManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
@@ -34,13 +35,17 @@ class TestAbstractWhisperSetupManager {
TestAbstractWhisperSetupManager(@Mock final CondaSetupManager condaSetupManager, @Mock final WhisperSetupConfiguration configuration,
@Mock final ProcessRunner processRunner, @Mock final HttpClient httpClient) {
this.condaSetupManager = Objects.requireNonNull(condaSetupManager);
when(condaSetupManager.name()).thenReturn("conda");
this.configuration = Objects.requireNonNull(configuration);
this.processRunner = Objects.requireNonNull(processRunner);
this.httpClient = Objects.requireNonNull(httpClient);
this.setupManager = spy(new DummyWhisperSetupManager(condaSetupManager, configuration, processRunner, httpClient));
}
@BeforeEach
void beforeEach() {
when(condaSetupManager.name()).thenReturn("conda");
}
@Test
void testGetStatus() throws SetupException {
assertEquals(NOT_INSTALLED, setupManager.getStatus());
@@ -155,11 +160,11 @@ class TestAbstractWhisperSetupManager {
}
@Override
protected void installWhisper() throws SetupException {
protected void installWhisper() {
}
@Override
protected boolean isWhisperInstalled() throws SetupException {
protected boolean isWhisperInstalled() {
return false;
}

View File

@@ -19,17 +19,18 @@ class TestWhisperSetupConfiguration {
private final Path venvPath;
private final String pythonVersion;
private final OS os;
private final WhisperSetupConfiguration configuration;
TestWhisperSetupConfiguration(@Mock final Path root, @Mock final Path venvPath) {
this.root = Objects.requireNonNull(root);
this.venvPath = Objects.requireNonNull(venvPath);
this.pythonVersion = "3.10";
this.os = OS.LINUX;
this.configuration = new WhisperSetupConfiguration(root, venvPath, pythonVersion, os);
}
@Test
void testGetters() {
final var configuration = new WhisperSetupConfiguration(root, venvPath, pythonVersion, os);
assertEquals(root, configuration.root());
assertEquals(venvPath, configuration.venvPath());
assertEquals(pythonVersion, configuration.pythonVersion());

View File

@@ -20,6 +20,7 @@ import com.github.gtache.autosubtitle.subtitle.extractor.ExtractException;
import com.github.gtache.autosubtitle.subtitle.extractor.ExtractionModel;
import com.github.gtache.autosubtitle.subtitle.extractor.SubtitleExtractorListener;
import com.github.gtache.autosubtitle.subtitle.extractor.impl.ExtractEventImpl;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
@@ -47,7 +48,7 @@ class TestAbstractWhisperSubtitleExtractor {
private final ProcessListener processListener;
private final ProcessResult processResult;
private final OS os;
private final DummyWhisperSubtitleExtractor extractor;
private DummyWhisperSubtitleExtractor extractor;
private final AudioInfo audioInfo;
private final VideoInfo videoInfo;
@@ -59,27 +60,30 @@ class TestAbstractWhisperSubtitleExtractor {
@Mock final ProcessRunner processRunner, @Mock final ProcessListener processListener,
@Mock final ProcessResult processResult, @Mock final VideoInfo videoInfo,
@Mock final AudioInfo audioInfo, @Mock final ExtractionModel extractionModel,
@Mock final SubtitleCollection<Subtitle> collection) throws IOException {
@Mock final SubtitleCollection<Subtitle> collection) {
this.venvPath = Path.of("venv");
this.os = OS.LINUX;
this.converterProvider = Objects.requireNonNull(converterProvider);
this.converter = Objects.requireNonNull(converter);
doReturn(converter).when(converterProvider).getConverter("json");
this.processRunner = Objects.requireNonNull(processRunner);
this.processListener = Objects.requireNonNull(processListener);
this.processResult = Objects.requireNonNull(processResult);
when(processRunner.startListen(anyList())).thenReturn(processListener);
when(processListener.join(Duration.ofHours(1))).thenReturn(processResult);
this.extractor = new DummyWhisperSubtitleExtractor(venvPath, converterProvider, processRunner, os);
this.audioInfo = Objects.requireNonNull(audioInfo);
when(audioInfo.format()).thenReturn("mp3");
this.videoInfo = Objects.requireNonNull(videoInfo);
when(videoInfo.format()).thenReturn("mp4");
this.extractionModel = Objects.requireNonNull(extractionModel);
this.collection = Objects.requireNonNull(collection);
}
@BeforeEach
void beforeEach() throws IOException {
doReturn(converter).when(converterProvider).getConverter("json");
when(processRunner.startListen(anyList())).thenReturn(processListener);
when(processListener.join(Duration.ofHours(1))).thenReturn(processResult);
when(audioInfo.format()).thenReturn("mp3");
when(videoInfo.format()).thenReturn("mp4");
this.extractor = new DummyWhisperSubtitleExtractor(venvPath, converterProvider, processRunner, os);
}
@Test
void testNotifyListeners() {
final var listener1 = mock(SubtitleExtractorListener.class);

View File

@@ -14,7 +14,7 @@ class TestWhisperXSetupModule {
}
@Test
void testWHisperXBundledRoot() {
void testWhisperXBundledRoot() {
final var root = Paths.get("root");
assertEquals(root.resolve("whisperx"), WhisperXSetupModule.providesWhisperXBundledRoot(root));
}

View File

@@ -7,6 +7,7 @@ import com.github.gtache.autosubtitle.subtitle.Subtitle;
import com.github.gtache.autosubtitle.subtitle.converter.SubtitleConverter;
import com.github.gtache.autosubtitle.subtitle.converter.SubtitleConverterProvider;
import com.github.gtache.autosubtitle.whisper.WhisperModels;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
@@ -26,17 +27,23 @@ class TestWhisperXSubtitleExtractor {
private final Path venvPath;
private final SubtitleConverterProvider converterProvider;
private final SubtitleConverter<Subtitle> converter;
private final ProcessRunner processRunner;
private final OS os;
private final WhisperXSubtitleExtractor whisperXSubtitleExtractor;
private WhisperXSubtitleExtractor whisperXSubtitleExtractor;
TestWhisperXSubtitleExtractor(@Mock final SubtitleConverterProvider converterProvider, @Mock final SubtitleConverter<Subtitle> converter,
@Mock final ProcessRunner processRunner) {
this.converterProvider = Objects.requireNonNull(converterProvider);
doReturn(converter).when(converterProvider).getConverter("json");
this.converter = Objects.requireNonNull(converter);
this.processRunner = Objects.requireNonNull(processRunner);
this.venvPath = Paths.get("path");
this.os = OS.LINUX;
}
@BeforeEach
void beforeEach() {
doReturn(converter).when(converterProvider).getConverter("json");
this.whisperXSubtitleExtractor = new WhisperXSubtitleExtractor(venvPath, converterProvider, processRunner, os);
}

View File

@@ -6,6 +6,7 @@ import com.github.gtache.autosubtitle.subtitle.converter.ParseException;
import com.github.gtache.autosubtitle.subtitle.impl.SubtitleCollectionImpl;
import com.github.gtache.autosubtitle.subtitle.impl.SubtitleImpl;
import com.google.gson.Gson;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
@@ -22,20 +23,22 @@ import static org.mockito.Mockito.when;
class TestJSONSubtitleConverter {
private final Gson gson;
private final Preferences preferences;
private final int defaultMaxLineLength;
private final int defaultMaxLines;
private final JSONSubtitleConverter converter;
private JSONSubtitleConverter converter;
TestJSONSubtitleConverter() {
this.gson = new Gson();
this.preferences = mock(Preferences.class);
this.defaultMaxLineLength = 100;
when(preferences.getInt("maxLineLength", defaultMaxLineLength)).thenReturn(defaultMaxLineLength);
this.defaultMaxLines = 2;
}
@BeforeEach
void beforeEach() {
when(preferences.getInt("maxLineLength", defaultMaxLineLength)).thenReturn(defaultMaxLineLength);
when(preferences.getInt("maxLines", defaultMaxLines)).thenReturn(defaultMaxLines);
this.converter = new JSONSubtitleConverter(gson, preferences, defaultMaxLineLength, defaultMaxLines);
this.converter = new JSONSubtitleConverter(new Gson(), preferences, defaultMaxLineLength, defaultMaxLines);
}
@Test
@@ -45,30 +48,46 @@ class TestJSONSubtitleConverter {
@Test
void testParseFormat() throws IOException, ParseException {
final var in = new String(getClass().getResourceAsStream("whisperx-in.json").readAllBytes(), StandardCharsets.UTF_8);
final var out = new String(getClass().getResourceAsStream("whisperx-out.json").readAllBytes(), StandardCharsets.UTF_8);
final var expected = new SubtitleCollectionImpl<Subtitle>("This is a test. Yes.", List.of(new SubtitleImpl("This is a test.", 9, 410, null, null), new SubtitleImpl("Yes.", 450, 6963, null, null)), Language.FR);
assertEquals(expected, converter.parse(in));
assertEquals(out, converter.format(expected, null));
try (final var inStream = getClass().getResourceAsStream("whisperx-in.json");
final var outStream = getClass().getResourceAsStream("whisperx-out.json")) {
if (inStream == null || outStream == null) {
throw new IOException("File not found");
}
final var in = new String(inStream.readAllBytes(), StandardCharsets.UTF_8);
final var out = new String(outStream.readAllBytes(), StandardCharsets.UTF_8);
final var expected = new SubtitleCollectionImpl<Subtitle>("This is a test. Yes.", List.of(new SubtitleImpl("This is a test.", 9, 410, null, null), new SubtitleImpl("Yes.", 450, 6963, null, null)), Language.FR);
assertEquals(expected, converter.parse(in));
assertEquals(out, converter.format(expected, null));
}
}
@Test
void testParseOverMaxWords() throws IOException, ParseException {
final var in = new String(getClass().getResourceAsStream("whisperx-max-words.json").readAllBytes(), StandardCharsets.UTF_8);
final var expected = new SubtitleCollectionImpl<Subtitle>("aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg hhhhhhhhhh iiiiiiiiii\njjjjjjjjjj kkkkkkkkkk llllllllll mmmmmmmmmm nnnnnnnnnn oooooooooo pppppppppp qqqqqqqqqq rrrrrrrrrr ssssssssss tttttttttt uuuuuuuuuu vvvvvvvvvv wwwwwwwwww xxxxxxxxxx yyyyyyyyyy zzzzzzzzzz Yes.",
List.of(new SubtitleImpl("aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg hhhhhhhhhh iiiiiiiiii\njjjjjjjjjj kkkkkkkkkk llllllllll mmmmmmmmmm nnnnnnnnnn oooooooooo pppppppppp qqqqqqqqqq rrrrrrrrrr", 0, 18000, null, null),
new SubtitleImpl("ssssssssss tttttttttt uuuuuuuuuu vvvvvvvvvv wwwwwwwwww xxxxxxxxxx yyyyyyyyyy zzzzzzzzzz", 18000, 26000, null, null),
new SubtitleImpl("Yes.", 30000, 31000, null, null)), Language.EN);
assertEquals(expected, converter.parse(in));
try (final var inStream = getClass().getResourceAsStream("whisperx-max-words.json")) {
if (inStream == null) {
throw new IOException("File not found");
}
final var in = new String(inStream.readAllBytes(), StandardCharsets.UTF_8);
final var expected = new SubtitleCollectionImpl<Subtitle>("aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg hhhhhhhhhh iiiiiiiiii\njjjjjjjjjj kkkkkkkkkk llllllllll mmmmmmmmmm nnnnnnnnnn oooooooooo pppppppppp qqqqqqqqqq rrrrrrrrrr ssssssssss tttttttttt uuuuuuuuuu vvvvvvvvvv wwwwwwwwww xxxxxxxxxx yyyyyyyyyy zzzzzzzzzz Yes.",
List.of(new SubtitleImpl("aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg hhhhhhhhhh iiiiiiiiii\njjjjjjjjjj kkkkkkkkkk llllllllll mmmmmmmmmm nnnnnnnnnn oooooooooo pppppppppp qqqqqqqqqq rrrrrrrrrr", 0, 18000, null, null),
new SubtitleImpl("ssssssssss tttttttttt uuuuuuuuuu vvvvvvvvvv wwwwwwwwww xxxxxxxxxx yyyyyyyyyy zzzzzzzzzz", 18000, 26000, null, null),
new SubtitleImpl("Yes.", 30000, 31000, null, null)), Language.EN);
assertEquals(expected, converter.parse(in));
}
}
@Test
void testParseOverMaxLines() throws IOException, ParseException {
final var in = new String(getClass().getResourceAsStream("whisperx-max-lines.json").readAllBytes(), StandardCharsets.UTF_8);
final var expected = new SubtitleCollectionImpl<Subtitle>("aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg hhhhhhhhhh iiiiiiiiii\njjjjjjjjjj kkkkkkkkkk llllllllll Yes.",
List.of(new SubtitleImpl("aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg hhhhhhhhhh iiiiiiiiii\njjjjjjjjjj kkkkkkkkkk llllllllll", 0, 18000, null, null),
new SubtitleImpl("Yes.", 30000, 31000, null, null)), Language.EN);
assertEquals(expected, converter.parse(in));
try (final var inStream = getClass().getResourceAsStream("whisperx-max-lines.json")) {
if (inStream == null) {
throw new IOException("File not found");
}
final var in = new String(inStream.readAllBytes(), StandardCharsets.UTF_8);
final var expected = new SubtitleCollectionImpl<Subtitle>("aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg hhhhhhhhhh iiiiiiiiii\njjjjjjjjjj kkkkkkkkkk llllllllll Yes.",
List.of(new SubtitleImpl("aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg hhhhhhhhhh iiiiiiiiii\njjjjjjjjjj kkkkkkkkkk llllllllll", 0, 18000, null, null),
new SubtitleImpl("Yes.", 30000, 31000, null, null)), Language.EN);
assertEquals(expected, converter.parse(in));
}
}
@ParameterizedTest