add more mocking

This commit is contained in:
Jordan 2025-03-04 07:20:00 -08:00
parent 61e54ded3c
commit 5352ae8eb1
3 changed files with 41 additions and 23 deletions

View File

@ -1,6 +1,6 @@
import React, { useState, useEffect } from "react";
import { View, Text, TextInput, Pressable, StyleSheet } from "react-native";
import { WhisperFile } from "@/app/lib/whisper";
import { WhisperFile, download_status_t, whisper_tag_t } from "@/app/lib/whisper";
import { Settings } from "@/app/lib/settings";
import { Picker } from "@react-native-picker/picker";
import { LanguageServer, language_matrix, language_matrix_entry } from "@/app/i18n/api";
@ -25,9 +25,9 @@ const SettingsComponent = () => {
const [whisperModel, setWhisperModel] =
useState<keyof typeof WHISPER_MODELS>("small");
const [downloader, setDownloader] = useState<any>(null);
const [whisperDownloadProgress, setWhisperDownloadProgress] = useState<
any | null
>(null);
const [whisperFile, setWhisperFile] = useState<WhisperFile>(null);
const [downloadStatus, setDownloadStatus] = useState<undefined | download_status_t>();
const [downloadStatusChecker, setDownloadStatusChecker] = useState<undefined | any>();
useEffect(() => {
loadSettings();
@ -74,17 +74,22 @@ const SettingsComponent = () => {
}
};
const intervalUpdateDownloadStatus = async () => {
if (!whisperFile) return;
const status = await whisperFile.getDownloadStatus();
setDownloadStatus(status);
}
const handleWhisperModelChange = async (
model: keyof typeof WHISPER_MODELS
model: whisper_tag_t,
) => {
const settings = await Settings.getDefault();
setWhisperModel(model);
await settings.setWhisperModel(model);
checkDownloadStatus(model);
setWhisperModel(model);
setWhisperFile(new WhisperFile(model));
};
const doDownload = async () => {
const whisperFile = WHISPER_MODELS[whisperModel];
const resumable = await whisperFile.createDownloadResumable({
onData: (progress) => setWhisperDownloadProgress(progress),
});
@ -122,16 +127,16 @@ const SettingsComponent = () => {
return hostLanguage && libretranslateBaseUrl ? (
<View style={styles.container}>
<Text style={styles.label}>Host Language:</Text>
<Picker
{languageOptions && (<Picker
selectedValue={hostLanguage}
style={{ height: 50, width: "100%" }}
onValueChange={handleHostLanguageChange}
accessibilityHint="hostLanguage"
>
{ languag }
<Picker.Item label="English" value="en" />
<Picker.Item label="Spanish" value="es" />
</Picker>
{languageOptions && Object.entries(languageOptions).map(([key, value]) => {
return (<Picker.Item label={value.name} value={value.code} />)
})}
</Picker>)}
<Text style={styles.label}>LibreTranslate Base URL:</Text>
<TextInput

View File

@ -8,20 +8,33 @@ import { Knex } from "knex";
const RENDER_TIME = 1000;
jest.mock('@/app/lib/whisper', () => {
const originalModule = jest.requireActual('@/app/lib/whisper');
// Mock the WhisperFile class
jest.mock("@/app/lib/whisper", () => {
const originalModule = jest.requireActual("@/app/lib/whisper");
return {
...originalModule,
Whisper: class {
constructor(...args) {
originalModule.Whisper.apply(this, args);
}
WhisperFile: jest.fn().mockImplementation((tag, targetFileName, label, size) => ({
tag,
targetFileName,
label,
size,
doesTargetExist: jest.fn(),
getDownloadStatus: jest.fn(), // Mock other methods as needed
isDownloadcomplete: () => true,
})),
};
});
targetFile = {
bytes: jest.fn().mockReturnValue(new Uint8Array([/* mock data */])),
};
},
jest.mock("expo-file-system", () => {
const originalModule = jest.requireActual("expo-file-system");
return {
...originalModule,
File: jest.fn().mockImplementation(() => ({
bytes: jest.fn(),
exists: jest.fn(),
})),
};
});

Binary file not shown.