Add workaround to fix note language recognition and reduce wasteful translation requests

This commit is contained in:
2024-01-07 11:19:59 -05:00
parent 84cfeb1604
commit 1110ffa8af
8 changed files with 161 additions and 25 deletions

View File

@@ -0,0 +1,26 @@
//
// BlocksExtensionTests.swift
// damusTests
//
// Created by Terry Yiu on 1/8/24.
//
import XCTest
import NaturalLanguage
@testable import damus
final class BlocksExtensionTests: XCTestCase {
func testLanguageHypothesisIsCorrectWithRightSingleQuotationMark() throws {
let note = try XCTUnwrap(NdbNote.owned_from_json(json: test_english_text_note_with_right_single_quotation_mark))
let blocks = note.blocks(test_keypair)
XCTAssertEqual(blocks.languageHypothesis, NLLanguage.english)
}
func testLanguageHypothesisIsCorrectWithNonEnglishLocale() throws {
let note = try XCTUnwrap(NdbNote.owned_from_json(json: test_japanese_text_note))
let blocks = note.blocks(test_keypair)
XCTAssertEqual(blocks.languageHypothesis, NLLanguage.japanese)
}
}

View File

@@ -0,0 +1,40 @@
//
// TranslatorTests.swift
// damusTests
//
// Created by Terry Yiu on 1/8/24.
//
import XCTest
@testable import damus
final class TranslatorTests: XCTestCase {
func testShouldTranslateWhenLanguagesAreDifferent() throws {
let userSettingsStore = UserSettingsStore()
userSettingsStore.translation_service = .purple
let translator = Translator(userSettingsStore, purple: DamusPurple(environment: .local_test, keypair: test_keypair))
XCTAssertTrue(translator.shouldTranslate(from: "en", to: "es"))
XCTAssertTrue(translator.shouldTranslate(from: "es", to: "fr"))
}
func testShouldNotTranslateWhenLanguagesAreTheSame() throws {
let userSettingsStore = UserSettingsStore()
userSettingsStore.translation_service = .purple
let translator = Translator(userSettingsStore, purple: DamusPurple(environment: .local_test, keypair: test_keypair))
XCTAssertFalse(translator.shouldTranslate(from: "en", to: "en"))
XCTAssertFalse(translator.shouldTranslate(from: "es", to: "es"))
}
func testShouldNotTranslateWhenNoTranslationServiceSelected() throws {
let userSettingsStore = UserSettingsStore()
userSettingsStore.translation_service = .none
let translator = Translator(userSettingsStore, purple: DamusPurple(environment: .local_test, keypair: test_keypair))
XCTAssertFalse(translator.shouldTranslate(from: "en", to: "es"))
XCTAssertFalse(translator.shouldTranslate(from: "es", to: "fr"))
}
}