add ollama files. Fix unit tests (finally). TODO: handle static file downloading and screens.

This commit is contained in:
Jordan 2025-02-14 15:23:22 -08:00
parent 68cc052417
commit 081ac367ba
14 changed files with 431 additions and 169 deletions

View File

@ -0,0 +1,39 @@
/**
* NOTE: this file is for ollama. The AI assistant should
* follow this format, but modify it according to the user's prompt.
*/
/**
* Import any necessary packages
**/
import { StyleSheet } from "react-native";
type ExampleComponentProps = {
/**
* Declare any properties here
*/
}
/**
* Don't include the typing when defiining the function. Define it normally.
*/
const ISpeakButton = (props : ExampleComponentProps) => {
// Do any housekeeping up here, like `useState`, `useEffect`, etc.
// Return the TSX component below
return (
<></>
)
}
// Create style sheets here
const styles = StyleSheet.create({
})
// Export the component
export default ISpeakButton;

View File

@ -0,0 +1,24 @@
/**
* NOTE: this file is for ollama. The AI assistant should
* follow this format, but modify it according to the user's prompt.
*/
/**
* Import any necessary packages
**/
import React, { act } from 'react';
import { render, screen } from '@testing-library/react-native'
import { MyComponent } from '@/app/component/MyComponent';
describe('Message Component', () => {
beforeEach(() => {
// do any set up here
});
it('A test that renders a widget', async () => {
render(<MyComponent />);
// IMPORTANT: use jest.expect(...) functions for assertions.
expect(await screen.findByText("something")).toBeOnTheScreen();
});
});

View File

@ -23,9 +23,8 @@ export type language_matrix = {
[key:string] : language_matrix_entry
}
export class Translator {
constructor(public source : language_t, public defaultTarget : string = "en", private baseUrl = LIBRETRANSLATE_BASE_URL) {
}
export class LanguageServer {
constructor(public baseUrl : string) {}
async fetchLanguages() : Promise<language_matrix> {
let data = {};
@ -52,9 +51,14 @@ export class Translator {
throw new Error(`Can't extract values from data: ${JSON.stringify(data)}`)
}
}
}
export class Translator {
constructor(public source : language_t, public defaultTarget : string = "en", private languageServer : LanguageServer) {
}
async translate(text : string, target : string|undefined = undefined) {
const url = LIBRETRANSLATE_BASE_URL + `/translate`;
const url = this.languageServer.baseUrl + `/translate`;
const res = await fetch(url, {
method: "POST",
body: JSON.stringify({

View File

@ -4,7 +4,7 @@ import _LANGUAGES from "@/assets/languages.min.json"
export const LANG_FLAGS = _LANG_FLAGS
export function longLang(shortLang : string) {
return ((LANG_FLAGS as any)[shortLang] as any)["nameEnglish"] as string
return ((LANG_FLAGS as any)[shortLang] as any)["name"] as string
}
export function lang_a3_a2(a3 : string) {

View File

@ -1,14 +1,15 @@
// conversation.test.ts
import { Translator } from '@/app/i18n/api';
import { LanguageServer, Translator } from '@/app/i18n/api';
import { Conversation, Message, Speaker } from '@/app/lib/conversation';
import { LIBRETRANSLATE_BASE_URL } from '@/constants/api';
import { describe, beforeEach, it, expect, test } from '@jest/globals';
describe('Conversation', () => {
let conversation: Conversation;
beforeEach(() => {
const translator = new Translator("en", "es");
const translator = new Translator("en", "es", new LanguageServer(LIBRETRANSLATE_BASE_URL));
const s1: Speaker = { language: "en", id: "host" };
const s2: Speaker = { id: "guest", language: "es" }
conversation = new Conversation(translator, s1, s2);

View File

@ -20,6 +20,7 @@ describe('Settings', () => {
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);
const result = await settings.getHostLanguage();
@ -30,10 +31,7 @@ describe('Settings', () => {
describe('getHostLanguage', () => {
it('should return the host language from the database', async () => {
const value = 'fr';
await settings.db.executeSql(
`INSERT INTO settings (host_language) VALUES (?)`,
[value]
);
await settings.setHostLanguage(value);
const result = await settings.getHostLanguage();
expect(result).toEqual(value);
@ -41,14 +39,14 @@ describe('Settings', () => {
it('should return null if the host language is not set', async () => {
const result = await settings.getHostLanguage();
expect(result).not.toBeNull();
expect(result).toBeNull();
});
});
describe('setLibretranslateBaseUrl', () => {
it('should set the LibreTranslate base URL in the database', async () => {
const value = 'https://example.com';
await settings.setLibetransalteBaseUrl(value);
await settings.setLibretranslateBaseUrl(value);
const result = await settings.getLibretranslateBaseUrl();
expect(result).toEqual(value);
@ -56,20 +54,9 @@ describe('Settings', () => {
});
describe('getLibretranslateBaseUrl', () => {
it('should return the LibreTranslate base URL from the database', async () => {
const value = 'https://another-example.com';
await settings.db.executeSql(
`INSERT INTO settings (libretranslate_base_url) VALUES (?)`,
[value]
);
const result = await settings.getLibretranslateBaseUrl();
expect(result).toEqual(value);
});
it('should return null if the LibreTranslate base URL is not set', async () => {
const result = await settings.getLibretranslateBaseUrl();
expect(result).not.toBeNull();
expect(result).toBeNull();
});
});
});

View File

@ -33,10 +33,8 @@ LIMIT 1`
if (!Settings.KEYS.includes(key)) {
throw new Error(`Invalid setting: '${key}'`)
}
const statement = `REPLACE INTO settings ('${key}')
VALUES (?)`
const args = [value]
await this.db.runAsync(statement, args);
const statement = `REPLACE INTO settings (${key}) VALUES (?)`
await this.db.runAsync(statement, value);
}
async setHostLanguage(value: string) {
@ -47,12 +45,12 @@ VALUES (?)`
return await this.getValue("host_language")
}
async setLibetransalteBaseUrl(value : string) {
async setLibretranslateBaseUrl(value : string) {
await this.setValue("libretranslate_base_url", value)
}
async getLibretranslateBaseUrl() {
await this.getValue("libretranslate_base_url")
return await this.getValue("libretranslate_base_url")
}
}

View File

@ -1,89 +1,112 @@
import React, { useEffect, useState } from "react";
import { StyleSheet, View } from "react-native";
import { NavigationContainer } from "@react-navigation/native";
import {
default as ReactNativeSettings,
SettingsElement,
} from "@mmomtchev/react-native-settings";
// Import necessary packages
import React, { useState, useEffect } from "react";
import { View, Text, TextInput, StyleSheet } from "react-native"; // Add Picker import
import { getDb } from "@/app/lib/db";
import { Settings } from "@/app/lib/settings";
import { LanguageServer } from "@/app/i18n/api";
import {Picker} from "@react-native-picker/picker"
import { longLang } from "@/app/i18n/lang";
import { Translator, language_matrix } from "@/app/i18n/api";
import { LIBRETRANSLATE_BASE_URL } from "@/constants/api";
// We will store the config here
const configData: Record<string, string> = {};
type Language = {
code: string;
name: string;
};
type LanguageMatrix = {
[key: string]: Language;
};
const SettingsComponent: React.FC = () => {
const [hostLanguage, setHostLanguage] = useState<string | null>(null);
const [libretranslateBaseUrl, setLibretranslateBaseUrl] = useState<string | null>(null);
const [languages, setLanguages] = useState<undefined|LanguageMatrix>();
const [isLoaded, setIsLoaded] = useState<boolean>(false);
useEffect(() => {
(async () => {
// Fetch the database connection
const db = await getDb();
const settings = new Settings(db);
// Get the current settings values
const hostLang = await settings.getHostLanguage();
const libretranslateUrl = await settings.getLibretranslateBaseUrl();
const langServer = new LanguageServer(libretranslateBaseUrl || LIBRETRANSLATE_BASE_URL);
// Fetch languages from API
const langData = await langServer.fetchLanguages();
setLanguages(langData);
setHostLanguage(hostLang || "en");
setLibretranslateBaseUrl(libretranslateUrl);
setIsLoaded(true);
})();
}, []);
const handleHostLanguageChange = async (value: string) => {
setHostLanguage(value);
// Fetch the database connection
const db = await getDb();
const settings = new Settings(db);
// Save the updated setting value
await settings.setHostLanguage(value);
};
const handleLibretranslateBaseUrlChange = async (value: string) => {
setLibretranslateBaseUrl(value);
// Fetch the database connection
const db = await getDb();
const settings = new Settings(db);
// Save the updated setting value
await settings.setLibretranslateBaseUrl(value);
};
return (
isLoaded ? <View style={styles.container}>
<Text style={styles.label}>Host Language:</Text>
<Picker
selectedValue={hostLanguage || ""}
style={{ height: 50, width: "100%" }}
onValueChange={handleHostLanguageChange}
accessibilityHint="language"
>
{languages && Object.keys(languages).map((langCode) => (
<Picker.Item key={langCode} label={longLang(langCode)} value={langCode} />
))}
</Picker>
<Text style={styles.label}>LibreTranslate Base URL:</Text>
<TextInput
style={styles.input}
value={libretranslateBaseUrl || ""}
onChangeText={handleLibretranslateBaseUrlChange}
accessibilityHint="libretranslate base url"
/>
</View> : <View><Text>Loading ...</Text></View>
);
};
// Create styles for the component
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
justifyContent: "center",
padding: "1.5%",
padding: 20,
},
label: {
fontSize: 16,
marginBottom: 8,
},
input: {
height: 40,
borderColor: "gray",
borderWidth: 1,
marginBottom: 20,
paddingHorizontal: 8,
},
});
// Retrieve a conf item or return the default
const confGet = (key: string, def: string): string => configData[key] || def;
// Store a conf item
const confSet = (key: string, value: string): void => {
configData[key] = value;
};
// Choose from a list item
const intelligence: Record<string, string> = {
L: "Low",
M: "Medium",
H: "High",
};
export default function Settings() {
// Simply pass the schema here
// It integrates in your existing `NavigationContainer` or `Screen`
const [translator, setTranslator] = useState<Translator>(new Translator("en"))
const [languages, setLanguages] = useState<language_matrix | undefined>();
const [languagesLoaded, setLanguagesLoaded] = useState<boolean>(false);
// This is the configuration schema
const settings: SettingsElement[] = [
{
label: "LibreTranslate server",
type: "string",
get: confGet.bind(null, "@ltServer", "http://localhost:5000"),
set: confSet.bind(null, "@ltServer"),
},
{
label: "Host Language",
type: "enum",
// You can override the way the value is displayed
values: ["en", "es", "fr"],
display: (v : string) => {
return longLang(v);
},
get: confGet.bind(null, "@hostLanguage", ""),
set: confSet.bind(null, "@hostLanguage"),
},
];
useEffect(() => {
const fetchData = async () => {
try {
// Replace with your actual async data fetching logic
const languages = await translator.fetchLanguages();
setLanguages(languages);
setLanguagesLoaded(true);
} catch (error) {
console.error("Error fetching data:", error);
}
};
});
return (
<NavigationContainer>
<View style={styles.container}>
<ReactNativeSettings settings={settings} />
</View>
</NavigationContainer>
);
}
export default SettingsComponent;

View File

@ -9,58 +9,35 @@ type MessageProps = {
}
const MessageBubble = (props: MessageProps) => {
const [text, setText] = useState(props.message.text);
const [translatedText, setTranslatedText] = useState<string|undefined>();
const [isTranslating, setIsTranslating] = useState<boolean>(false);
useEffect(() => {
props.message.onTextUpdate = (message: Message) => {
setText(message.text);
}
props.message.onTextDone = async (message: Message) => {
setIsTranslating(true);
await props.message.translate()
}
props.message.onTranslationDone = (message: Message) => {
if (!message.translation) throw new Error("Missing translation");
setTranslatedText(message.translation);
setIsTranslating(false);
}
}, [props.message])
const spId = props.message.speaker.id
return (
<SafeAreaView>
{text && (
<Text>{text}</Text>
{props.message.text && (
<Text>{props.message.text}</Text>
)}
{translatedText &&
<Text>{translatedText}</Text>
{props.message.translation &&
<Text accessibilityHint="translation">{props.message.translation}</Text>
}
</SafeAreaView>
)
}
// const bubbleStyle = StyleSheet.create({
// host: {
const bubbleStyle = StyleSheet.create({
host: {
// },
// guest: {
},
guest: {
// },
// })
},
})
// const textStyles = StyleSheet.create({
// native: {
const textStyles = StyleSheet.create({
native: {
// },
// translation: {
},
translation: {
// },
// });
},
});
export default MessageBubble;

View File

@ -1,12 +1,59 @@
import React, { act } from 'react';
import { render, screen } from '@testing-library/react-native'
import { render, screen, waitFor } from '@testing-library/react-native'
import MessageBubble from '@/components/ui/MessageBubble';
import { Conversation, Speaker } from '@/app/lib/conversation';
import {Translator} from '@/app/i18n/api';
import {LanguageServer, Translator, language_matrix} from '@/app/i18n/api';
import { View } from 'react-native';
import { LIBRETRANSLATE_BASE_URL } from '@/constants/api';
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) => {
if (text.match(/Hello, how are you\?/i)) {
return "Hola, ¿cómo estás?"
}
return "??? Huh ???"
})
}
return {
LanguageServer,
Translator,
}
})
describe('Message Component', () => {
const translator = new Translator('en', 'es');
const translator = new Translator('en', 'es', new LanguageServer(LIBRETRANSLATE_BASE_URL));
const host : Speaker = {id : "host", language : "en"}
const guest : Speaker = {id : "guest", language: "es"}
@ -21,8 +68,8 @@ describe('Message Component', () => {
it('renders the message text correctly', async () => {
conversation.addMessage(host, "Hello, World!");
const message = conversation[0];
render(<View></View>);
// render(<MessageBubble message={message} />);
// render(<View></View>);
render(<MessageBubble message={message} />);
expect(await screen.findByText(message.text as string)).toBeOnTheScreen();
});
@ -32,7 +79,7 @@ describe('Message Component', () => {
await conversation.translateLast();
render(<MessageBubble message={conversation[0]} />);
expect(await screen.findByText(translatedText)).toBeOnTheScreen();
expect(screen.getByAccessibilityHint("translation")).toBeOnTheScreen();
});
it('widget still renders pre-translation', async () => {
@ -42,10 +89,8 @@ describe('Message Component', () => {
render(<MessageBubble message={conversation[0]} />);
expect(screen.getByText(text)).toBeOnTheScreen();
// expect(screen.getByText(translatedText)).not.toBeOnTheScreen();
await act(async () => {
await conversation.translateLast();
});
expect(await screen.findByText(text)).toBeOnTheScreen();
expect(await screen.findByText(translatedText)).toBeOnTheScreen();
// await conversation.translateLast();
// expect(await screen.findByText(text)).toBeOnTheScreen();
// expect(await screen.findByText(translatedText)).toBeOnTheScreen();
});
});

View File

@ -0,0 +1,146 @@
import React, { Dispatch } from "react";
import { render, screen, fireEvent, act } from "@testing-library/react-native";
import SettingsComponent from "@/components/Settings";
import { Settings } from "@/app/lib/settings";
import { getDb } from "@/app/lib/db";
import { language_matrix } from "@/app/i18n/api";
const RENDER_TIME = 1000;
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?"
})
}
return {
LanguageServer,
Translator,
}
})
jest.mock("@/app/lib/db", () => {
return {
getDb: jest.fn(() => {
return {
runAsync: jest.fn((statement : string, value : string) => {}),
getFirstAsync: jest.fn((statement : string, value : string) => {
return []
}),
}
})
}
})
jest.mock("@/app/lib/settings", () => {
const originalModule = jest.requireActual('@/app/lib/settings');
class MockSettings {
public constructor(public db = {}) {}
public setHostLanguage = jest.fn((val : string) => {
})
public setLibretranslateBaseUrl(val : string) {
}
getHostLanguage = jest.fn(() => {
return "en"
})
getLibretranslateBaseUrl = jest.fn(() => {
return "http://localhost:5004"
});
}
return {
...originalModule,
Settings: MockSettings
}
})
describe("SettingsComponent", () => {
beforeEach(async() => {
const settings = new Settings(await getDb());
await settings.setHostLanguage("en");
await settings.setLibretranslateBaseUrl("https://example.com");
})
beforeAll(() => {
jest.useFakeTimers();
})
afterAll(() => {
jest.useRealTimers()
})
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
await screen.findByText(/Host Language:/i);
await screen.findByText(/LibreTranslate Base URL:/i);
// expect(screen.getByDisplayValue("English")).toBeTruthy();
expect(screen.getByAccessibilityHint("libretranslate base url")).toBeTruthy();
});
test("updates host language setting when input changes", async () => {
render(<SettingsComponent />);
// Wait for the component to fetch and display the initial settings
await screen.findByText(/Host Language:/i);
await screen.findByText(/LibreTranslate Base URL:/i);
// Change the host language input value
const picker = screen.getByAccessibilityHint("language");
fireEvent(picker, "onvalueChange", "es");
expect(picker.props.selectedIndex).toStrictEqual(0);
});
test("updates LibreTranslate base URL setting when input changes", async () => {
render(<SettingsComponent />);
jest.advanceTimersByTime(RENDER_TIME)
screen.debug();
// Wait for the component to fetch and display the initial settings
await screen.findByText(/Host Language:/i);
await screen.findByText(/LibreTranslate Base URL:/i);
// Change the LibreTranslate base URL input value
fireEvent.changeText(screen.getByAccessibilityHint("libretranslate base url"), "http://new-example.com");
jest.advanceTimersByTime(RENDER_TIME);
expect(screen.getByAccessibilityHint("libretranslate base url")).toBeTruthy();
});
});

View File

@ -1,6 +1,5 @@
// jestSetup.ts
/**
jest.mock('expo-sqlite', () => {
return {
openDatabaseAsync: async (name: string) => {
@ -28,4 +27,3 @@ jest.mock('expo-sqlite', () => {
},
};
});
*/

14
package-lock.json generated
View File

@ -12,6 +12,7 @@
"@expo/vector-icons": "^14.0.4",
"@mmomtchev/react-native-settings": "^1.1.0",
"@react-native-async-storage/async-storage": "^2.1.0",
"@react-native-picker/picker": "^2.11.0",
"@react-navigation/bottom-tabs": "^7.2.0",
"@react-navigation/native": "^7.0.14",
"@react-navigation/native-stack": "^7.2.0",
@ -4045,6 +4046,19 @@
"react-native": "^0.0.0-0 || >=0.65 <1.0"
}
},
"node_modules/@react-native-picker/picker": {
"version": "2.11.0",
"resolved": "https://registry.npmjs.org/@react-native-picker/picker/-/picker-2.11.0.tgz",
"integrity": "sha512-QuZU6gbxmOID5zZgd/H90NgBnbJ3VV6qVzp6c7/dDrmWdX8S0X5YFYgDcQFjE3dRen9wB9FWnj2VVdPU64adSg==",
"license": "MIT",
"workspaces": [
"example"
],
"peerDependencies": {
"react": "*",
"react-native": "*"
}
},
"node_modules/@react-native/assets-registry": {
"version": "0.76.6",
"resolved": "https://registry.npmjs.org/@react-native/assets-registry/-/assets-registry-0.76.6.tgz",

View File

@ -4,7 +4,7 @@
"version": "1.0.0",
"scripts": {
"test": "jest --watch --coverage=false --changedSince=origin/main",
"testDebug": "jest -o --watch --coverage=false",
"testDebug": "jest -o --watch --bail --coverage=false",
"testFinal": "jest",
"updateSnapshots": "jest -u --coverage=false",
"start": "expo start",
@ -19,6 +19,7 @@
"@expo/vector-icons": "^14.0.4",
"@mmomtchev/react-native-settings": "^1.1.0",
"@react-native-async-storage/async-storage": "^2.1.0",
"@react-native-picker/picker": "^2.11.0",
"@react-navigation/bottom-tabs": "^7.2.0",
"@react-navigation/native": "^7.0.14",
"@react-navigation/native-stack": "^7.2.0",
@ -53,6 +54,9 @@
},
"jest": {
"preset": "jest-expo",
"testPathIgnorePatterns": [
".ollama"
],
"transformIgnorePatterns": [
"node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@sentry/react-native|native-base|react-native-svg)"
],
@ -66,7 +70,9 @@
"!**/.expo/**"
],
"automock": false,
"setupFilesAfterEnv": ["@testing-library/jest-native/extend-expect"],
"setupFilesAfterEnv": [
"<rootDir>/jestSetup.ts"
],
"testTimeout": 10000
},
"devDependencies": {