fix unit tests. add conversation model and add unit tests to it.

This commit is contained in:
Jordan Hewitt
2025-01-25 09:11:09 -08:00
parent 0c9daf8e4a
commit 82d9c9c523
21 changed files with 7127 additions and 4712 deletions

View File

@ -2,7 +2,6 @@ import { Tabs } from 'expo-router';
import React from 'react';
import { Platform } from 'react-native';
import { HapticTab } from '@/components/HapticTab';
import { IconSymbol } from '@/components/ui/IconSymbol';
import TabBarBackground from '@/components/ui/TabBarBackground';
import { Colors } from '@/constants/Colors';
@ -16,7 +15,6 @@ export default function TabLayout() {
screenOptions={{
tabBarActiveTintColor: Colors[colorScheme ?? 'light'].tint,
headerShown: false,
tabBarButton: HapticTab,
tabBarBackground: TabBarBackground,
tabBarStyle: Platform.select({
ios: {

View File

@ -1,23 +0,0 @@
import { StyleSheet, Image, Platform } from 'react-native';
import { IconSymbol } from '@/components/ui/IconSymbol';
import { Text } from '@react-navigation/elements';
export default function TabTwoScreen() {
return (
<Text>Explore</Text>
);
}
const styles = StyleSheet.create({
headerImage: {
color: '#808080',
bottom: -90,
left: -35,
position: 'absolute',
},
titleContainer: {
flexDirection: 'row',
gap: 8,
},
});

View File

@ -1,5 +1,5 @@
import { LanguageSelection } from '@/components/LanguageSelection';
import { Text } from '@react-navigation/elements';
import { Text } from 'react-native';
import { Image, StyleSheet, Platform } from 'react-native';
export default function HomeScreen() {

View File

@ -2,7 +2,7 @@ import { Link, Stack } from 'expo-router';
import { StyleSheet } from 'react-native';
import { ThemedText } from '@/components/ThemedText';
import { Text } from '@react-navigation/elements';
import { Text } from 'react-native';
export default function NotFoundScreen() {
return (
<>

View File

@ -1,6 +1,6 @@
import { Cache } from "react-native-cache";
import AsyncStorage from '@react-native-async-storage/async-storage';
import { LIBRETRANSLATE_BASE_URL } from "@/constants/api";
import AsyncStorage from '@react-native-async-storage/async-storage';
type language_t = string;
@ -35,7 +35,7 @@ export class Translator {
});
const data = await res.json();
return Object.fromEntries(
Object.values(data).map((obj : language_matrix_entry) => {
Object.values(data as language_matrix_entry []).map((obj : language_matrix_entry) => {
return [
obj["code"],
obj,

View File

@ -0,0 +1,32 @@
// conversation.test.ts
import { Translator } from '@/app/i18n/api';
import { Conversation, Message, Speaker } from '@/app/lib/conversation';
import { describe, beforeEach, it, expect, test } from '@jest/globals';
describe('Conversation', () => {
let conversation: Conversation;
beforeEach(() => {
const translator = new Translator("en", "sp");
const s1: Speaker = { language: "en", id: "s1" };
const s2: Speaker = { id: "s2", language: "sp" }
conversation = new Conversation(translator, s1, s2);
});
it('should add a message to the conversation', () => {
conversation.addMessage({ id: "s1", language: "en" }, "Hello");
expect(true).toEqual(false);
expect(conversation.length).toBe(1);
expect(conversation[0].speaker.id).toEqual("s1");
expect(conversation[0].text).toEqual("hello");
});
it('should translate the last message in the conversation', async () => {
conversation.addMessage({ id: "s1", language: "en" }, "Hello");
await conversation.translateLast();
expect(conversation[conversation.length - 1].translation).toEqual("Hollla");
});
});

38
app/lib/conversation.ts Normal file
View File

@ -0,0 +1,38 @@
import { Translator } from "../i18n/api";
export type Speaker = {
id: string,
language : string,
}
export class Message {
public translation? : string;
constructor (public text : string, public speaker : Speaker) {}
public async translate(translator : Translator, language? : string) {
this.translation = await translator.translate(this.text, language);
}
}
export class Conversation extends Array<Message> {
constructor (
private translator : Translator,
s1 : Speaker,
s2 : Speaker,
) {
super();
}
public addMessage(speaker : Speaker, text : string) {
this.push(new Message(text, speaker));
}
public async translateMessage(i : number) {
if (!this[i]) throw new Error(`${i} is not a valid message number`);
await this[i].translate(this.translator, this[i].speaker.language);
}
public async translateLast() {
return await this.translateMessage(this.length-1);
}
}