@@ -27,6 +27,7 @@ import com.ichi2.utils.BASIC_MODEL_NAME
2727import net.ankiweb.rsdroid.withoutUnicodeIsolation
2828import org.hamcrest.CoreMatchers.equalTo
2929import org.hamcrest.MatcherAssert.assertThat
30+ import org.json.JSONArray
3031import org.json.JSONObject
3132import org.junit.Ignore
3233import org.junit.Test
@@ -417,6 +418,78 @@ class AnkiDroidJsAPITest : RobolectricTest() {
417418 assertEquals(CardType .New , cardAfterReset.type, " Card type after reset" )
418419 }
419420
421+ @Test
422+ fun ankiGetNoteTagsTest () =
423+ runTest {
424+ val n =
425+ addBasicNote(" Front" , " Back" ).update {
426+ tags = mutableListOf (" tag1" , " tag2" , " tag3" )
427+ }
428+
429+ val reviewer: Reviewer = startReviewer()
430+ waitForAsyncTasksToComplete()
431+
432+ val jsapi = reviewer.jsApi
433+
434+ // test get tags for note
435+ val expectedTags = n.tags
436+ val response = getDataFromRequest(" getNoteTags" , jsapi, jsonObjectOf(" noteId" to n.id))
437+ val jsonResponse = JSONObject (response)
438+ val actualTags = JSONArray (jsonResponse.getString(" value" ))
439+
440+ assertEquals(expectedTags.size, actualTags.length())
441+ for (i in 0 until actualTags.length()) {
442+ assertEquals(expectedTags[i], actualTags.getString(i))
443+ }
444+ }
445+
446+ @Test
447+ fun ankiSetNoteTagsTest () =
448+ runTest {
449+ val n =
450+ addBasicNote(" Front" , " Back" ).update {
451+ tags = mutableListOf (" tag1" , " tag2" , " tag3" )
452+ }
453+
454+ val reviewer: Reviewer = startReviewer()
455+ waitForAsyncTasksToComplete()
456+
457+ val jsapi = reviewer.jsApi
458+
459+ // test set tags for note
460+ val newTags =
461+ JSONArray ().apply {
462+ put(" tag4" ) // leading whitespace
463+ put(" tag5 " ) // trailing whitespace
464+ put(" tag 6 " ) // spaces in tag
465+ }
466+
467+ val newTagsList =
468+ (0 until newTags.length()).map { index ->
469+ newTags
470+ .getString(index)
471+ .trim()
472+ .replace(" " , " _" )
473+ .replace(" \u3000 " , " _" )
474+ }
475+
476+ assertThat(
477+ getDataFromRequest(" setNoteTags" , jsapi, jsonObjectOf(" noteId" to n.id, " tags" to newTags)),
478+ equalTo(formatApiResult(true )),
479+ )
480+ waitForAsyncTasksToComplete()
481+
482+ // Reload the note to ensure the tags are updated
483+ val updatedNote = col.getNote(n.id)
484+ println (updatedNote.tags)
485+
486+ // Verify the tags are updated
487+ assertEquals(newTagsList.size, updatedNote.tags.size)
488+ newTagsList.zip(updatedNote.tags).forEach { (newTag, updatedTag) ->
489+ assertEquals(newTag, updatedTag)
490+ }
491+ }
492+
420493 companion object {
421494 fun jsApiContract (data : String = ""): ByteArray =
422495 JSONObject ()
@@ -450,5 +523,21 @@ class AnkiDroidJsAPITest : RobolectricTest() {
450523 jsAPI
451524 .handleJsApiRequest(methodName, jsApiContract(apiData), false )
452525 .decodeToString()
526+
527+ suspend fun getDataFromRequest (
528+ methodName : String ,
529+ jsAPI : AnkiDroidJsAPI ,
530+ apiData : JSONObject ,
531+ ): String =
532+ jsAPI
533+ .handleJsApiRequest(methodName, jsApiContract(apiData.toString()), false )
534+ .decodeToString()
453535 }
454536}
537+
538+ private fun jsonObjectOf (vararg pairs : Pair <String , Any >): JSONObject =
539+ JSONObject ().apply {
540+ for ((key, value) in pairs) {
541+ put(key, value)
542+ }
543+ }
0 commit comments