add knex-expo-sqlite-dialect submodule. migrate to expo (possibly a mistake)
This commit is contained in:
@ -5,10 +5,10 @@ import { language_matrix } from "@/app/i18n/api";
|
||||
import { Settings } from "@/app/lib/settings";
|
||||
import { getDb } from "@/app/lib/db";
|
||||
import { Knex } from "knex";
|
||||
import { WhisperFile } from "@/app/lib/whisper";
|
||||
|
||||
const RENDER_TIME = 1000;
|
||||
|
||||
// Mock the WhisperFile class
|
||||
jest.mock("@/app/lib/whisper", () => {
|
||||
const originalModule = jest.requireActual("@/app/lib/whisper");
|
||||
|
||||
@ -21,7 +21,10 @@ jest.mock("@/app/lib/whisper", () => {
|
||||
size,
|
||||
doesTargetExist: jest.fn(),
|
||||
getDownloadStatus: jest.fn(), // Mock other methods as needed
|
||||
isDownloadcomplete: () => true,
|
||||
isDownloadComplete: jest.fn(() => false), // Initially assume download is not complete
|
||||
createDownloadResumable: jest.fn().mockResolvedValue({
|
||||
startAsync: jest.fn().mockResolvedValue({}),
|
||||
}),
|
||||
})),
|
||||
};
|
||||
});
|
||||
@ -39,32 +42,33 @@ jest.mock("expo-file-system", () => {
|
||||
});
|
||||
|
||||
jest.mock("@/app/i18n/api", () => {
|
||||
class LanguageServer {
|
||||
fetchLanguages = () => {
|
||||
return {
|
||||
en: {
|
||||
code: "en",
|
||||
name: "English",
|
||||
targets: ["fr", "es"],
|
||||
},
|
||||
fr: {
|
||||
code: "fr",
|
||||
name: "French",
|
||||
targets: ["en", "es"],
|
||||
},
|
||||
es: {
|
||||
code: "es",
|
||||
name: "Spanish",
|
||||
targets: ["en", "fr"],
|
||||
},
|
||||
} as language_matrix;
|
||||
};
|
||||
}
|
||||
class Translator {
|
||||
translate = jest.fn((text: string, target: string) => {
|
||||
return "Hola, como estas?";
|
||||
});
|
||||
}
|
||||
const LanguageServer = jest.fn();
|
||||
const Translator = jest.fn();
|
||||
|
||||
// Mock the fetchLanguages method to return a predefined language matrix
|
||||
LanguageServer.prototype.fetchLanguages = jest.fn(() => ({
|
||||
en: {
|
||||
code: "en",
|
||||
name: "English",
|
||||
targets: ["fr", "es"],
|
||||
},
|
||||
fr: {
|
||||
code: "fr",
|
||||
name: "French",
|
||||
targets: ["en", "es"],
|
||||
},
|
||||
es: {
|
||||
code: "es",
|
||||
name: "Spanish",
|
||||
targets: ["en", "fr"],
|
||||
},
|
||||
} as language_matrix));
|
||||
|
||||
// Mock the translate method
|
||||
Translator.prototype.translate = jest.fn((text: string, target: string) => {
|
||||
return "Hola, como estas?";
|
||||
});
|
||||
|
||||
return {
|
||||
LanguageServer,
|
||||
Translator,
|
||||
@ -100,7 +104,6 @@ describe("SettingsComponent", () => {
|
||||
|
||||
test("renders correctly with initial settings", async () => {
|
||||
render(<SettingsComponent />);
|
||||
jest.advanceTimersByTime(RENDER_TIME);
|
||||
screen.debug();
|
||||
|
||||
// Wait for the component to fetch and display the initial settings
|
||||
@ -121,7 +124,7 @@ describe("SettingsComponent", () => {
|
||||
await screen.findByText(/LibreTranslate Base URL:/i);
|
||||
|
||||
// Change the host language input value
|
||||
const picker = screen.getByAccessibilityHint("hostLanguage");
|
||||
const picker = screen.getByAccessibilityHint("host language");
|
||||
fireEvent(picker, "onvalueChange", "es");
|
||||
expect(picker.props.selectedIndex).toStrictEqual(0);
|
||||
});
|
||||
@ -147,4 +150,72 @@ describe("SettingsComponent", () => {
|
||||
screen.getByAccessibilityHint("libretranslate base url")
|
||||
).toBeTruthy();
|
||||
});
|
||||
|
||||
|
||||
describe("Download Whisper Model", () => {
|
||||
it("should trigger download when model is not present", async () => {
|
||||
const whisperFile = new WhisperFile("small");
|
||||
(whisperFile.doesTargetExist as jest.Mock).mockResolvedValue(false);
|
||||
|
||||
render(<SettingsComponent />);
|
||||
await screen.findByText(/\s*Download Small\s*/i);
|
||||
// Assuming there's a button or trigger to start download
|
||||
act(() => {
|
||||
fireEvent.press(screen.getByText(/\s*Download Small\s*/i));
|
||||
})
|
||||
|
||||
expect(whisperFile.createDownloadResumable).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should show progress when download is in progress", async () => {
|
||||
const whisperFile = new WhisperFile("small");
|
||||
(whisperFile.doesTargetExist as jest.Mock).mockResolvedValue(false);
|
||||
(whisperFile.getDownloadStatus as jest.Mock).mockResolvedValue({
|
||||
doesTargetExist: false,
|
||||
isDownloadComplete: false,
|
||||
hasDownloadStarted: true,
|
||||
progress: {
|
||||
current: 1024,
|
||||
total: 2048,
|
||||
remaining: 1024,
|
||||
percentRemaining: 50,
|
||||
},
|
||||
});
|
||||
|
||||
render(<SettingsComponent />);
|
||||
await screen.findByText(/Host Language:/i);
|
||||
fireEvent.press(screen.getByText(/Download Model/i));
|
||||
|
||||
expect(await screen.findByText("50%")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("should indicate download is complete", async () => {
|
||||
const whisperFile = new WhisperFile("small");
|
||||
(whisperFile.doesTargetExist as jest.Mock).mockResolvedValue(false);
|
||||
(whisperFile.getDownloadStatus as jest.Mock)
|
||||
.mockResolvedValueOnce({
|
||||
doesTargetExist: false,
|
||||
isDownloadComplete: false,
|
||||
hasDownloadStarted: true,
|
||||
progress: {
|
||||
current: 1024,
|
||||
total: 2048,
|
||||
remaining: 1024,
|
||||
percentRemaining: 50,
|
||||
},
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
doesTargetExist: true,
|
||||
isDownloadComplete: true,
|
||||
hasDownloadStarted: false,
|
||||
progress: undefined,
|
||||
});
|
||||
|
||||
render(<SettingsComponent />);
|
||||
await screen.findByText(/Host Language:/i);
|
||||
fireEvent.press(screen.getByText(/Download Model/i));
|
||||
|
||||
expect(await screen.findByText("Download Complete")).toBeTruthy();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user