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

View File

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