// SPDX-License-Identifier: GPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® // The message field's editing by code point: one to four bytes of UTF-8, // and the lines of a text with newlines. import std; import ImsdMessages; using namespace ImsdMessages; namespace { std::int32_t failures = 0; void Check(bool ok, std::string_view what) { std::println("{} {}", ok ? "PASS" : "FAIL", what); if (!ok) ++failures; } void CheckEqual(std::string_view actual, std::string_view expected, std::string_view what) { const bool ok = actual == expected; if (ok) std::println("PASS {}", what); else std::println("FAIL {} — expected \"{}\", got \"{}\"", what, expected, actual); if (!ok) ++failures; } ComposeState At(std::string value, std::size_t cursor) { ComposeState s; s.value = std::move(value); s.cursor = cursor; return s; } } int main() { // a, é, € and 😀: one, two, three and four bytes; boundaries 0 1 3 6 10. const std::string mixed = "aé€\U0001F600"; Check(mixed.size() == 10, "the sample is ten bytes"); Check(NextCodepoint(mixed, 0) == 1 && NextCodepoint(mixed, 1) == 3 && NextCodepoint(mixed, 3) == 6 && NextCodepoint(mixed, 6) == 10, "NextCodepoint steps over one to four bytes"); Check(NextCodepoint(mixed, 10) == 10, "NextCodepoint stays at the end"); Check(PreviousCodepoint(mixed, 10) == 6 && PreviousCodepoint(mixed, 6) == 3 && PreviousCodepoint(mixed, 3) == 1 && PreviousCodepoint(mixed, 1) == 0, "PreviousCodepoint steps back over one to four bytes"); Check(PreviousCodepoint(mixed, 0) == 0, "PreviousCodepoint stays at the start"); ComposeState s = At(mixed, 10); Check(ComposeEdit(s, EditKey::Backspace) && s.cursor == 6, "Backspace takes the four-byte emoji"); CheckEqual(s.value, "aé€", "what is left after Backspace"); s = At(mixed, 1); Check(ComposeEdit(s, EditKey::Delete) && s.cursor == 1, "Delete takes the two-byte é"); CheckEqual(s.value, "a€\U0001F600", "what is left after Delete"); s = At("abc", 0); Check(!ComposeEdit(s, EditKey::Backspace) && s.value == "abc" && s.cursor == 0, "Backspace at the start deletes nothing and says so"); s = At("abc", 3); Check(!ComposeEdit(s, EditKey::Delete) && s.value == "abc" && s.cursor == 3, "Delete at the end deletes nothing and says so"); s = At(mixed, 6); Check(ComposeEdit(s, EditKey::Left) && s.cursor == 3, "Left moves back over the three-byte €"); Check(ComposeEdit(s, EditKey::Right) && s.cursor == 6, "Right moves over it again"); s = At("ab", 0); Check(ComposeEdit(s, EditKey::Left) && s.cursor == 0, "Left at the start stays, and is still a move"); s = At("ab", 2); Check(ComposeEdit(s, EditKey::Right) && s.cursor == 2, "Right at the end stays, and is still a move"); // "one\ntwo\nthree": the lines start at 0, 4 and 8; the newlines are at 3 and 7. s = At("one\ntwo\nthree", 6); ComposeEdit(s, EditKey::Home); Check(s.cursor == 4, "Home goes to the start of the cursor's line"); ComposeEdit(s, EditKey::End); Check(s.cursor == 7, "End goes to the end of the line, before its newline"); ComposeEdit(s, EditKey::Home); Check(s.cursor == 4, "Home from the end of a line"); s = At("one\ntwo\nthree", 4); ComposeEdit(s, EditKey::Home); Check(s.cursor == 4, "Home at the start of a line stays"); s = At("one\ntwo\nthree", 3); ComposeEdit(s, EditKey::Home); Check(s.cursor == 0, "Home from the end of the first line"); s = At("one\ntwo\nthree", 9); ComposeEdit(s, EditKey::End); Check(s.cursor == 13, "End on the last line goes to the end of the text"); s = At("\nx", 0); ComposeEdit(s, EditKey::Home); Check(s.cursor == 0, "Home at the very start, before a newline, stays"); s = At("ab", 1); Check(ComposeEdit(s, EditKey::Newline) && s.cursor == 2, "Newline inserts at the cursor and moves past it"); CheckEqual(s.value, "a\nb", "the text after Newline"); s = At("ab", 1); ComposeInsert(s, "é"); Check(s.cursor == 3, "an insert moves the cursor past its bytes"); CheckEqual(s.value, "aéb", "the text after an insert"); // A tap at or past a line's end: the text's end on the last line, the // newline after a hung space before a hard break, the line's end at a // soft wrap (the lines as tests/Wrap pins them, 10 px a glyph, 50 px wide). const std::vector hung = { { 0, 5, 50 }, { 7, 8, 10 } }; // "abcde \nx" Check(ComposeLineEnd(hung, 0, "abcde \nx") == 6, "past the end of a line that a hung space and a newline follow: before the newline, after the space"); Check(ComposeLineEnd(hung, 1, "abcde \nx") == 8, "past the end of the last line: the text's end"); const std::vector soft = { { 0, 5, 50 }, { 6, 8, 20 } }; // "ab cd ef" Check(ComposeLineEnd(soft, 0, "ab cd ef") == 5, "at a soft wrap the line's end, before the space that broke it"); const std::vector trailing = { { 0, 5, 50 } }; // "abcde " Check(ComposeLineEnd(trailing, 0, "abcde ") == 6, "a trailing hung space: the text's end"); const std::vector ended = { { 0, 2, 20 }, { 3, 3, 0 } }; // "ab\n" Check(ComposeLineEnd(ended, 0, "ab\n") == 2, "past the end of the line before a text's empty last line: before the newline, not on the empty line below"); Check(ComposeLineEnd(ended, 1, "ab\n") == 3, "past the empty last line: the text's end"); const std::vector hungEnd = { { 0, 5, 50 }, { 7, 7, 0 } }; // "abcde \n" Check(ComposeLineEnd(hungEnd, 0, "abcde \n") == 6, "the same with a hung space: after the space, before the newline"); // The field shows five lines at most and keeps the caret's line among them. Check(ComposeFirstLine(0, 5) == 0 && ComposeFirstLine(4, 5) == 0, "a caret within the first five lines: the field starts at the first"); Check(ComposeFirstLine(5, 5) == 1 && ComposeFirstLine(6, 5) == 2, "a caret below them: its line is the fifth shown"); Check(ComposeFirstLine(19, 5) == 15, "the same far down a long text"); Check(ComposeFirstLine(0, 1) == 0 && ComposeFirstLine(7, 1) == 7, "a one-line field shows the caret's line"); // The drafts of the conversations left: kept by key, taken up at the // next opening. { Drafts drafts; ComposeState typed; ComposeInsert(typed, "half a thought"); typed.cursor = 4; typed.focused = !ComposeState{}.focused; drafts.Keep("+31612345678", typed); Check(drafts.Size() == 1, "a left conversation's text is kept"); const ComposeState back = drafts.Take("+31612345678"); Check(back.value == "half a thought" && back.cursor == 4 && back.focused == ComposeState{}.focused, "and taken up as typed, the cursor where it was, the focus as a field is opened"); Check(drafts.Size() == 0 && drafts.Take("+31612345678").value.empty(), "taken, it is the field's: nothing is kept twice"); drafts.Keep("+31612345678", typed); drafts.Keep("+31612345678", ComposeState{}); Check(drafts.Take("+31612345678").value == "half a thought", "an empty field keeps nothing and drops nothing"); ComposeState other; ComposeInsert(other, "to another"); drafts.Keep("+31612345678", typed); drafts.Keep("1234", other); Check(drafts.Size() == 2 && drafts.Take("1234").value == "to another" && drafts.Take("+31612345678").value == "half a thought", "each conversation has its own"); drafts.Keep("+31612345678", typed); drafts.Keep("+31612345678", other); const ComposeState both = drafts.Take("+31612345678"); Check(both.value == "half a thought\nto another" && both.cursor == both.value.size(), "a second text kept under one key goes after the first, on a line of its own"); } // A kept typed address follows imsd's status as an open conversation's // key does: typed before the country code was known, found after. { const SimPlan kpn{ "31", "20408" }; const auto nothing = [](std::string_view) { return true; }; Drafts drafts; ComposeState typed; ComposeInsert(typed, "typed before the status"); drafts.Keep("0612345678", typed, "0612345678", SimPlan{}); drafts.Status(kpn, "31", nothing); Check(drafts.Take("0612345678").value.empty() && drafts.Take("+31612345678").value == "typed before the status", "the status brings the code: a kept typed address moves to the key it makes now"); drafts.Keep("0612345678", typed, "0612345678", SimPlan{}); drafts.Status(kpn, "31", [](std::string_view) { return false; }); Check(drafts.Take("0612345678").value == "typed before the status", "not while texts are stored under its key: the conversation stays on the key its rows carry"); drafts.Keep("0612345678", typed, "0612345678", SimPlan{ "32", "20601" }); drafts.Status(kpn, "31", nothing); Check(drafts.Take("0612345678").value == "typed before the status", "nor under another SIM's status"); std::size_t asked = 0; drafts.Keep("0612345678", typed); drafts.Status(kpn, "31", [&asked](std::string_view) { ++asked; return true; }); Check(asked == 0 && drafts.Take("0612345678").value == "typed before the status", "nor a conversation opened from the list, which has no typed address: the store is not asked about it"); drafts.Keep("0612345678", typed, "0612345678", SimPlan{}); drafts.Status(kpn, "31", nothing); drafts.Status(SimPlan{ "32", "20601" }, "32", nothing); Check(drafts.Take("+31612345678").value == "typed before the status", "a moved draft keeps the plan it learned: another SIM's status leaves it where it is"); ComposeState earlier; ComposeInsert(earlier, "in the thread"); drafts.Keep("+31612345678", earlier); drafts.Keep("0612345678", typed, "0612345678", SimPlan{}); drafts.Status(kpn, "31", nothing); Check(drafts.Size() == 1 && drafts.Take("+31612345678").value == "in the thread\ntyped before the status", "two keys that became one: both texts, each on its line"); } return failures == 0 ? 0 : 1; }