updating code using knex.

This commit is contained in:
Jordan
2025-03-02 20:15:27 -08:00
parent d00e6d62ff
commit a9b5ccf84f
15 changed files with 2557 additions and 605 deletions

View File

@ -1,32 +1,26 @@
import {describe, expect, beforeEach} from '@jest/globals';
import {Settings} from '@/app/lib/settings';
import { getDb, migrateDb } from '@/app/lib/db';
import { Settings } from '@/app/lib/settings';
import { getDb } from '@/app/lib/db';
import { Knex } from 'knex';
describe('Settings', () => {
let settings: Settings;
let settings : Settings;
let db : Knex;
beforeEach(async () => {
// Initialize your Settings class here with a fresh database instance
await migrateDb();
const db = await getDb();
if (!db) throw new Error("Could not get db");
settings = new Settings(db);
db = await getDb("development");
settings = new Settings(db)
});
afterEach(async () => {
// Clean up the database after each test
settings && await settings.db.executeSql('DELETE FROM settings');
await db.migrate.down();
});
describe('setHostLanguage', () => {
it('should set the host language in the database', async () => {
const value = 'en';
await settings.db.runAsync("REPLACE INTO settings (host_language) VALUES (?)", "en");
await settings.setHostLanguage(value);
it('should set the host language in the database', async () => {
const value = 'en';
await settings.setHostLanguage(value);
const result = await settings.getHostLanguage();
expect(result).toEqual(value);
});
const result = await settings.getHostLanguage();
expect(result).toEqual(value);
});
describe('getHostLanguage', () => {
@ -40,7 +34,7 @@ describe('Settings', () => {
it('should return null if the host language is not set', async () => {
const result = await settings.getHostLanguage();
expect(result).toBeNull();
expect(result).toBeUndefined();
});
});
@ -57,7 +51,34 @@ describe('Settings', () => {
describe('getLibretranslateBaseUrl', () => {
it('should return null if the LibreTranslate base URL is not set', async () => {
const result = await settings.getLibretranslateBaseUrl();
expect(result).toBeNull();
expect(result).toBeUndefined();
});
});
});
describe('setWhisperModel', () => {
it('should set the Whisper model in the database', async () => {
const value = 'base';
await settings.setWhisperModel(value);
const result = await settings.getWhisperModel();
expect(result).toEqual(value);
});
});
describe('setWhisperModel', () => {
it('should set the Whisper model in the database', async () => {
const value = 'base';
await settings.setWhisperModel(value);
const result = await settings.getWhisperModel();
expect(result).toEqual(value);
});
});
describe('getWhisperModel', () => {
it('should return null if the Whisper model is not set', async () => {
const result = await settings.getWhisperModel();
expect(result).toBeUndefined();
});
});
});

View File

@ -0,0 +1,101 @@
// components/ui/__tests__/WhisperFile.spec.tsx
import React from "react";
import { render, act } from "@testing-library/react-native";
import { WhisperFile } from "@/app/lib/whisper"; // Adjust the import path as necessary
describe("WhisperFile", () => {
let whisperFile: WhisperFile;
beforeEach(() => {
whisperFile = new WhisperFile("small");
});
it("should initialize correctly", () => {
expect(whisperFile).toBeInstanceOf(WhisperFile);
});
describe("getModelFileSize", () => {
it("should return the correct model file size", async () => {
expect(whisperFile.size).toBeUndefined();
await whisperFile.updateMetadata();
expect(whisperFile.size).toBeGreaterThan(1000);
});
});
describe("getWhisperDownloadStatus", () => {
it("should return the correct download status", async () => {
const mockStatus = {
doesTargetExist: true,
isDownloadComplete: false,
hasDownloadStarted: true,
progress: {
current: 100,
total: 200,
remaining: 100,
percentRemaining: 50.0,
},
};
jest
.spyOn(whisperFile, "getDownloadStatus")
.mockResolvedValue(mockStatus);
const result = await whisperFile.getDownloadStatus();
expect(result).toEqual(mockStatus);
});
});
describe("initiateWhisperDownload", () => {
it("should initiate the download with default options", async () => {
const mockModelLabel = "small";
jest
.spyOn(whisperFile, "createDownloadResumable")
.mockResolvedValue(true);
await whisperFile.initiateWhisperDownload(mockModelLabel);
expect(whisperFile.createDownloadResumable).toHaveBeenCalledWith(
mockModelLabel
);
});
it("should initiate the download with custom options", async () => {
const mockModelLabel = "small";
const mockOptions = { force_redownload: true };
jest
.spyOn(whisperFile, "createDownloadResumable")
.mockResolvedValue(true);
await whisperFile.initiateWhisperDownload(mockModelLabel, mockOptions);
expect(whisperFile.createDownloadResumable).toHaveBeenCalledWith(
mockModelLabel,
mockOptions
);
});
it("should return the correct download status when target exists and is complete", async () => {
jest.spyOn(whisperFile, "doesTargetExist").mockResolvedValue(true);
jest.spyOn(whisperFile, "isDownloadComplete").mockResolvedValue(true);
expect(await whisperFile.doesTargetExist()).toEqual(true);
expect(await whisperFile.isDownloadComplete()).toEqual(true);
});
it("should return the correct download status when target does not exist", async () => {
jest.spyOn(whisperFile, "doesTargetExist").mockResolvedValue(false);
const result = await whisperFile.getDownloadStatus();
expect(result).toEqual({
doesTargetExist: false,
isDownloadComplete: false,
hasDownloadStarted: false,
progress: undefined,
});
});
});
// Add more tests as needed for other methods in WhisperFile
});