trying to figure out jest issue.

This commit is contained in:
Jordan Hewitt
2025-01-29 17:17:59 -08:00
parent 0876db2dea
commit 889d898062
18 changed files with 7410 additions and 4790 deletions

View File

@ -4,14 +4,24 @@ import { Text } from 'react-native';
import { Image, StyleSheet, Platform } from 'react-native';
import { Conversation, Speaker } from '../lib/conversation';
import { language_matrix_entry, Translator } from '../i18n/api';
import ConversationThread from '@/components/ConversationThread';
export default function HomeScreen() {
const [language, setLanguage] = useState<language_matrix_entry | undefined>()
const [conversation, setConversation] = useState<Conversation|undefined>();
return (
language ? <ConversationPlay language={language} /> :
<LanguageSelection onLanguageSelected={(l) => setLanguage(l)} />
conversation ? <ConversationThread conversation={conversation} /> :
<LanguageSelection onLanguageSelected={
(language) => {
setConversation(new Conversation(
new Translator("en", language.code),
{id: "host", language: "en"},
{id: "guest", language: language.code},
))
}
} />
);
}

View File

@ -9,21 +9,21 @@ describe('Conversation', () => {
beforeEach(() => {
const translator = new Translator("en", "es");
const s1: Speaker = { language: "en", id: "s1" };
const s2: Speaker = { id: "s2", language: "es" }
const s1: Speaker = { language: "en", id: "host" };
const s2: Speaker = { id: "guest", language: "es" }
conversation = new Conversation(translator, s1, s2);
});
it('should add a message to the conversation', () => {
conversation.addMessage({ id: "s1", language: "en" }, "Hello");
conversation.addMessage({ id: "host", language: "en" }, "Hello");
expect(conversation.length).toBe(1);
expect(conversation[0].speaker.id).toEqual("s1");
expect(conversation[0].speaker.id).toEqual("host");
expect(conversation[0].text).toEqual("Hello");
});
it('should translate the last message in the conversation', async () => {
conversation.addMessage({ id: "s1", language: "en" }, "Hello");
conversation.addMessage({ id: "host", language: "en" }, "Hello");
await conversation.translateLast();
expect(conversation[conversation.length - 1].translation).toEqual("Hola");

View File

@ -5,33 +5,54 @@ export type Speaker = {
language : string,
}
export class Message {
public translation? : string;
constructor (public text : string, public speaker : Speaker) {}
public onTextUpdate?: (message: Message) => void;
public onTextDone?: (message: Message) => Promise<void>;
public onTranslationDone?: (message: Message) => void;
constructor (public conversation : Conversation, public speaker : Speaker, public text? : string) {}
public async translate(translator : Translator, language? : string) {
if (!this.text) throw new Error("No text")
this.translation = await translator.translate(this.text, language);
}
get otherSpeaker() {
return this.speaker.id === this.conversation.host.id ? this.conversation.guest : this.conversation.host
}
get otherLanguage() {
return this.otherSpeaker.language
}
}
export class Conversation extends Array<Message> {
public onAddMessage? : (conversation : Conversation) => Promise<any>;
public onTranslationDone? : (conversation : Conversation) => Promise<any>;
constructor (
private translator : Translator,
public s1 : Speaker,
public s2 : Speaker,
public host : Speaker,
public guest : Speaker,
) {
super();
}
public addMessage(speaker : Speaker, text : string) {
this.push(new Message(text, speaker));
public addMessage(speaker : Speaker, text? : string) {
this.push(new Message(this, speaker, text));
}
public async translateMessage(i : number) {
if (!this[i]) throw new Error(`${i} is not a valid message number`);
const otherSpeaker = this[i].speaker.id === this.s1.id ? this.s2 : this.s1
console.log(`Translating sentence to %s: %s`, otherSpeaker.language, this[i].text)
await this[i].translate(this.translator, otherSpeaker.language);
console.log(`Translating sentence to %s: %s`, this[i].otherLanguage, this[i].text)
await this[i].translate(this.translator, this[i].otherLanguage);
}
get lastMessage() {
return this[this.length-1]
}
public async translateLast() {