imsd-messages/tests/Number/main.cpp
Jorijn van der Graaf dd1108da58
main: show the window; --screenshot and --quit-after
The executable shows the window at start unless --daemon (which
--headless and --send imply), on Activate and Open from a second launch
or a notification (of the URIs an Open carries, its first sms: link),
and on an sms: link, which opens the conversation of its first recipient
at once (Number's SmsUriRecipient: the scheme in any case, the fields
after '?' dropped, percent-escapes decoded; then ConversationKey with
the country code known at that moment — --country's, else the one imsd's
status gave once it is in, else what the number itself carries — which
takes NormalizeNumber's result as a phone number — E.164, or digits: a
short code, a national number no country code completed — and nothing
else), or just the window when it names no phone number; the
conversation remembers the address as dialled and the plan of the SIM it
was typed under, and that SIM's statuses move its key (Number's
AfterStatus, one step, applied in App::Status: another SIM's status —
another operator's, or without operators another own number's code,
Number's SameSim — changes nothing, so a SIM change leaves the key and
the plan the address was typed under; the same SIM's fills in what the
plan lacks, and the key follows from its digits to the centre's stand-in
and from any key to the own number's code once the plan has it, never
from one stand-in's key to another's and not while the code is lost, the
conversation it moves to shown at its latest message — and only while
nothing is stored under the key: a text stored before the code arrived
keeps the conversation on the key its rows carry). The shell forwards
Sync's events to the window and the notifier closes a conversation's
notification when it is opened (one closed while its Notify is on its
way goes when the answer comes). --screenshot renders one frame outside
the compositor's pacing 2.5 s after start — 600 ms later where it has to
make the window first — and saves it as a PNG before quitting, or ends
with exit 1 when no frame was captured — with a line when no window
could render it or another instance is running (a flag that acts on the
window finds none there: the run forwards nothing), and without one when
the run ended before the capture (the display gone, a close that quits);
a PNG the library cannot write is not noticed at this commit, and a
capture that throws ends the process — the verification path for a
machine without eyes, which must not depend on a desktop that repaints —
and --quit-after ends a bench on its own clock (a number of seconds its
timer can take: anything else is refused); a --send errand ended by
anything but its final state — that clock — exits 2, as it does on a
signal, its end latched before the loop stops; --screenshot beside
--send or --quit is refused, neither having a window. The window is torn
down in order before the bus client stops, and when the compositor's
connection breaks the process ends, so the next launch starts afresh. A
window the library cannot make (no display, no Vulkan driver) is logged
by Ui's Show and the start ends with exit 1, as a start without its
database does; a resident instance logs it and runs on without one, and
a later Activate or Open logs the same at once, the device's set-up
running only once.

Tested: tests/Number, the sms: links (a query, an escape, a list, the
scheme in capitals, no number), the keys of typed addresses, SameSim
(the same operator, another operator of one country, a status without
its operator, nothing known, own numbers' codes without operators) and
AfterStatus through the statuses a typed address meets: the stand-in
completing its digits while the plan learns the operator; another
stand-in, a lost code and another operator's status — its own number's
code the stand-in's or not — changing nothing; rows keeping the key
while the plan learns the code; the own number's code moving the
stand-in's key, at once or once nothing is stored; a key the own
number's code made, and its plan, staying under another SIM and under
its own SIM's stand-in; an own code that makes the stand-in's key; a
short code; --country before the operator; no operators; no address; an
international address, under another SIM and under its own; and the
address as dialled making the key the address as typed made.
2026-11-18 01:12:27 +01:00

207 lines
14 KiB
C++

// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// Number normalisation: the raw address a user types or the network sends
// becomes the conversation key — E.164 for numbers, short codes and
// alphanumeric senders verbatim — the display form of a key, the dialled
// form of an address (its separators gone, nothing added), an sms: link's
// recipient, and the key an open conversation moves to once the country
// code is known.
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;
}
}
int main() {
Check(!version.empty(), "the version constant is set");
struct Case {
std::string_view raw;
std::string_view countryCode;
std::string_view key;
};
constexpr std::array<Case, 25> cases = { {
{ "+31612345678", "31", "+31612345678" },
{ "0612345678", "31", "+31612345678" },
{ "06 1234 5678", "31", "+31612345678" },
{ "06-1234-5678", "31", "+31612345678" },
{ "0031612345678", "", "+31612345678" },
{ " +31612345678 ", "", "+31612345678" },
{ "+1 (555) 123-4567", "31", "+15551234567" },
{ "0612345678", "", "0612345678" },
{ "0612345678", "39", "+390612345678" },
{ "0549123456", "378", "+3780549123456" },
{ "021234567", "32", "+3221234567" },
{ "02123456", "32", "02123456" },
{ "1234", "31", "1234" },
{ "KPN", "31", "KPN" },
{ "Odido Info", "31", "Odido Info" },
{ "", "31", "" },
{ "+", "31", "+" },
{ "+3161234567890123456", "31", "+3161234567890123456" },
{ "+31612345678#", "31", "+31612345678#" },
{ "612345678", "34", "+34612345678" },
{ "555 123 4567", "1", "+15551234567" },
{ "41234567", "47", "+4741234567" },
{ "4123456", "47", "4123456" },
{ "612345678", "", "612345678" },
{ "123456789012345", "31", "123456789012345" },
} };
for (const Case& c : cases) CheckEqual(NormalizeNumber(c.raw, c.countryCode), c.key, std::format("NormalizeNumber(\"{}\", \"{}\")", c.raw, c.countryCode));
CheckEqual(CountryCodeOf("+31653131313"), "31", "country code of the KPN service centre");
CheckEqual(CountryCodeOf("+15551234567"), "1", "one-digit code 1");
CheckEqual(CountryCodeOf("+79161234567"), "7", "one-digit code 7");
CheckEqual(CountryCodeOf("+212612345678"), "212", "three-digit code 212");
CheckEqual(CountryCodeOf("+4915112345678"), "49", "two-digit code 49");
CheckEqual(CountryCodeOf("+3912345678"), "39", "two-digit code 39");
CheckEqual(CountryCodeOf("KPN"), "", "no code for an alphanumeric sender");
CheckEqual(CountryCodeOf("0612345678"), "", "no code for a national number");
const SimPlan kpn{ "31", "20408" };
const SimPlan vodafoneNl{ "31", "20404" };
const SimPlan telekomDe{ "49", "26201" };
Check(AnotherPlan("0612345678", kpn, telekomDe), "AnotherPlan: a national number under another country's SIM");
Check(AnotherPlan("3477123456", { "39", "22210" }, { "1", "310260" }), "AnotherPlan: a plan without a trunk prefix too");
Check(AnotherPlan("1234", kpn, telekomDe), "AnotherPlan: a short code too");
Check(AnotherPlan("0031612345678", kpn, { "1", "310260" }), "AnotherPlan: so is a number behind 00, one plan's international prefix and not another's");
Check(AnotherPlan("1234", kpn, vodafoneNl), "AnotherPlan: a short code is its operator's: another operator of the same country has another service there");
Check(!AnotherPlan("0612345678", kpn, vodafoneNl), "AnotherPlan: a national number means the same under another operator of its country");
Check(!AnotherPlan("612345678", { "34", "21407" }, { "34", "21401" }), "AnotherPlan: one of a plan without a trunk prefix too");
Check(!AnotherPlan("0031612345678", kpn, vodafoneNl), "AnotherPlan: and one behind the country's international prefix");
Check(!AnotherPlan("1234", { "", "20408" }, kpn), "AnotherPlan: the same operator is the same plan, a side's code known or not");
Check(!AnotherPlan("0612345678", kpn, kpn), "AnotherPlan: the same plan");
Check(!AnotherPlan("+31612345678", kpn, telekomDe), "AnotherPlan: an international address names its own plan");
Check(AnotherPlan("0612345678", { "", "20408" }, vodafoneNl), "AnotherPlan: under another operator a number written under no known code is not known to mean the same");
Check(AnotherPlan("0612345678", { "31", "" }, { "49", "" }), "AnotherPlan: without the operators the codes decide alone");
Check(!AnotherPlan("1234", { "31", "" }, vodafoneNl), "AnotherPlan: an operator missing on one side and the same code, nothing more is known");
Check(!AnotherPlan("0612345678", {}, telekomDe), "AnotherPlan: written under no known plan, nothing is known");
Check(!AnotherPlan("0612345678", kpn, {}), "AnotherPlan: no plan known now, nothing is known");
Check(!AnotherPlan("", kpn, telekomDe), "AnotherPlan: no address");
CheckEqual(NormalizeNumber("+31 (0)6 1234 5678", "31"), "+31612345678", "the parenthesised trunk zero of an international number is dropped");
CheckEqual(NormalizeNumber("+44 (0)20 7946 0018", "31"), "+442079460018", "the same under another country code");
CheckEqual(NormalizeNumber("0031 (0)6 1234 5678", "31"), "+31612345678", "and after the 00 prefix");
CheckEqual(NormalizeNumber("(06) 1234 5678", "31"), "+31612345678", "parentheses around a national number stay separators");
CheckEqual(NormalizeNumber("(+44) (0)20 7946 0018", "31"), "+442079460018", "a country code in parentheses is the international form too");
CheckEqual(NormalizeNumber("(+31) (0)6 1234 5678", ""), "+31612345678", "the same without a default code");
CheckEqual(NormalizeNumber("(+378) (0)549 123456", "31"), "(+378) (0)549 123456", "after San Marino's code, as after Italy's, no zero is a trunk prefix: not a number, the address is the key");
// The dialled form: the separators gone, nothing added — the network's
// own dialling plan reads a national number or a short code.
struct DialCase {
std::string_view raw;
std::string_view dialled;
};
constexpr std::array<DialCase, 12> dials = { {
{ "+31 6 1234 5678", "+31612345678" },
{ "(+44) (0)20 7946 0018", "+442079460018" },
{ "06 1234 5678", "0612345678" },
{ "(06) 1234-5678", "0612345678" },
{ "+31 (0)6 1234 5678", "+31612345678" },
{ "+39 (0)6 1234 5678", "+39 (0)6 1234 5678" },
{ "0039 (0)347 1234567", "0039 (0)347 1234567" },
{ "0031 6 1234 5678", "0031612345678" },
{ "1234", "1234" },
{ " KPN ", "KPN" },
{ "+", "+" },
{ "", "" },
} };
for (const DialCase& c : dials) CheckEqual(DialledNumber(c.raw), c.dialled, std::format("DialledNumber(\"{}\")", c.raw));
CheckEqual(DisplayNumber("+31612345678"), "+31 612345678", "display of an E.164 number");
CheckEqual(DisplayNumber("+15551234567"), "+1 5551234567", "display with a one-digit code");
CheckEqual(DisplayNumber("1234"), "1234", "display of a short code");
CheckEqual(DisplayNumber("KPN"), "KPN", "display of an alphanumeric sender");
// sms: links (RFC 5724): the first recipient, decoded, without the fields.
struct UriCase {
std::string_view uri;
std::string_view countryCode;
std::string_view key;
};
const std::array<UriCase, 11> uris = { {
{ "sms:+31612345678", "", "+31612345678" },
{ "SMS:+31612345678", "", "+31612345678" },
{ "sms:0612345678", "31", "+31612345678" },
{ "sms:+31612345678?body=Hi", "", "+31612345678" },
{ "sms:%2B31612345678", "", "+31612345678" },
{ "sms:+31612345678,+31687654321", "", "+31612345678" },
{ "sms://+31612345678", "", "+31612345678" },
{ "sms:1234", "", "1234" },
{ "sms:", "31", "" },
{ "sms:?body=Hi", "31", "" },
{ "sms:KPN", "31", "" },
} };
for (const UriCase& c : uris) CheckEqual(ConversationKey(SmsUriRecipient(c.uri), c.countryCode), c.key, std::format("ConversationKey(SmsUriRecipient(\"{}\"), \"{}\")", c.uri, c.countryCode));
CheckEqual(SmsUriRecipient("sms:06%2012345678?body=Hi"), "06 12345678", "the recipient as the link spelt it, decoded, for dialling");
CheckEqual(SmsUriRecipient("tel:+31612345678"), "", "no recipient from another scheme");
Check(IsSmsUri("Sms:1") && !IsSmsUri("smsto:1") && !IsSmsUri("sms"), "IsSmsUri takes the scheme in any case and nothing else");
struct KeyCase {
std::string_view address;
std::string_view countryCode;
std::string_view key;
};
constexpr std::array<KeyCase, 6> keys = { {
{ "0612345678", "31", "+31612345678" },
{ "+31 6 1234 5678", "", "+31612345678" },
{ "0612345678", "", "0612345678" },
{ "1234", "", "1234" },
{ "KPN", "31", "" },
{ "", "31", "" },
} };
for (const KeyCase& c : keys) CheckEqual(ConversationKey(c.address, c.countryCode), c.key, std::format("ConversationKey(\"{}\", \"{}\")", c.address, c.countryCode));
Check(SameSim({ "", "20408" }, kpn), "SameSim: the same operator, its own number's code read since");
Check(!SameSim(kpn, vodafoneNl), "SameSim: another operator of the same country is another SIM");
Check(!SameSim(kpn, { "31", "" }), "SameSim: a status without its operator is not known to be the typed SIM's");
Check(SameSim({}, telekomDe), "SameSim: typed before any status, nothing is known against it");
Check(SameSim({ "31", "" }, kpn) && !SameSim({ "31", "" }, telekomDe), "SameSim: typed under no known operator, the own numbers' codes tell");
{
const std::string_view typed = "0612345678";
const SimPlan unread{ "", "20408" }; // SIM A, its own number not read yet: the operator and no code
TypedKey t = AfterStatus(typed, { "0612345678", {} }, unread, "44", true);
Check(t == TypedKey{ "+44612345678", unread }, "AfterStatus: the centre's stand-in completes a key that is still its digits, and the plan learns the operator");
const TypedKey standIn = t;
Check(AfterStatus(typed, standIn, unread, "49", true) == standIn, "AfterStatus: one stand-in's key does not move to another's");
Check(AfterStatus(typed, standIn, unread, "", true) == standIn, "AfterStatus: a lost code leaves the key alone");
Check(AfterStatus(typed, standIn, { "44", "23410" }, "44", true) == standIn && AfterStatus(typed, standIn, telekomDe, "49", true) == standIn, "AfterStatus: another operator's status changes nothing, its own number's code the stand-in's or not");
Check(AfterStatus(typed, standIn, kpn, "31", false) == TypedKey{ "+44612345678", kpn }, "AfterStatus: rows under the key keep it there, and the plan still learns the code");
t = AfterStatus(typed, standIn, kpn, "31", true);
Check(t == TypedKey{ "+31612345678", kpn }, "AfterStatus: back on its SIM, the own number's code moves the key the stand-in made");
Check(AfterStatus(typed, AfterStatus(typed, standIn, kpn, "31", false), kpn, "31", true) == t, "AfterStatus: and moves it later, once nothing is stored under it");
Check(AfterStatus(typed, t, telekomDe, "49", true) == t && AfterStatus(typed, t, vodafoneNl, "31", true) == t, "AfterStatus: a key the own number's code made stays under another SIM, another country's or another operator's, and so does the plan it was typed under");
Check(AfterStatus(typed, t, unread, "44", true) == t, "AfterStatus: nor does its own SIM's stand-in move it back, the own number unread again");
Check(AfterStatus(typed, standIn, { "44", "20408" }, "44", true) == TypedKey{ "+44612345678", { "44", "20408" } }, "AfterStatus: an own number's code that makes the stand-in's key keeps it");
Check(AfterStatus("1234", { "1234", {} }, kpn, "31", true) == TypedKey{ "1234", kpn }, "AfterStatus: a short code keeps its key and learns its plan");
Check(AfterStatus(typed, { "+31612345678", { "31", "" } }, kpn, "31", true) == t, "AfterStatus: a code known before the operator (--country): the status completes the plan");
Check(AfterStatus(typed, { "+31612345678", { "31", "" } }, telekomDe, "49", true) == TypedKey{ "+31612345678", { "31", "" } }, "AfterStatus: and another own number's code is another SIM");
Check(AfterStatus(typed, { "+44612345678", {} }, { "31", "" }, "31", true) == TypedKey{ "+31612345678", { "31", "" } }, "AfterStatus: without operators the own number's code moves the stand-in's key all the same");
Check(AfterStatus("", { "+31612345678", {} }, kpn, "31", true).key == "+31612345678", "AfterStatus: no address, no other key (a conversation from the list or a notification)");
Check(AfterStatus("+31612345678", { "+31612345678", kpn }, telekomDe, "49", true) == TypedKey{ "+31612345678", kpn }, "AfterStatus: an international address never changes key");
Check(AfterStatus("+31612345678", { "+31612345678", { "", "20408" } }, { "44", "20408" }, "44", true).key == "+31612345678", "AfterStatus: nor under its own SIM's code, another country's");
// AfterStatus makes a key from the address as dialled; Open's is made from the address as typed.
bool sameKey = true;
for (const Case& c : cases) sameKey = sameKey && ConversationKey(DialledNumber(c.raw), c.countryCode) == ConversationKey(c.raw, c.countryCode);
for (const std::string_view raw : { "+31 (0)6 1234 5678", "0031 (0)6 1234 5678", "(06) 1234 5678", "(+44) (0)20 7946 0018" }) sameKey = sameKey && ConversationKey(DialledNumber(raw), "31") == ConversationKey(raw, "31");
Check(sameKey, "AfterStatus: the address as dialled makes the key the address as typed made, under every case's code");
}
return failures == 0 ? 0 : 1;
}